diff --git a/README.md b/README.md index 7bbddcf7..a92da4c5 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ # Junit-Servers ---------------------- [![Build Status](https://travis-ci.org/mjeanroy/junit-servers.svg?branch=master)](https://travis-ci.org/mjeanroy/junit-servers) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.mjeanroy/junit-servers/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.mjeanroy/junit-servers) @@ -15,7 +14,7 @@ With Maven, add explicit dependency: com.github.mjeanroy junit-servers-jetty - 0.4.2 + 0.4.3 test ``` @@ -24,7 +23,7 @@ With Maven, add explicit dependency: com.github.mjeanroy junit-servers-tomcat - 0.4.2 + 0.4.3 test ``` @@ -132,6 +131,8 @@ public class MyTest { When embedded jetty is created, you can easyly use a custom configuration using a fluent API: - Define custom enironment properties that will be initialized before server startup and removed after server shutdown. +- Define an override descriptor for web.xml, for example for loading a spring configuration with additional setup from test resources. +- Define additional parent classpath from either existing Maven dependencies or jar files in a directory, helping with JSPs. - Define custom hooks to execute custom code before server startup and after server shutdown. ```java @@ -153,6 +154,13 @@ public class MyTest { .withWebapp("webapp") .withPort(9090) .withProperty("spring.profiles.active", "test") + .withOverrideDescriptor("src/test/resources/WEB-INF/web.xml") + .withParentClasspath(WebAppContext.class, new FileFilter() { + @Override + public boolean accept(File pathname) { + return pathname.getName().startsWith("apache-jstl"); + } + }) .withHook(new Hook() { @Override public void pre(EmbeddedServer server) { @@ -418,10 +426,10 @@ public class MyTest { } ``` -## Licence +## Licence MIT License (MIT) -## Contributing +## Contributing If you found a bug or you thing something is missing, feel free to contribute and submit an issue or a pull request. diff --git a/junit-servers-core/src/main/java/com/github/mjeanroy/junit/servers/servers/configuration/AbstractConfiguration.java b/junit-servers-core/src/main/java/com/github/mjeanroy/junit/servers/servers/configuration/AbstractConfiguration.java index a94d0e5e..943f2101 100644 --- a/junit-servers-core/src/main/java/com/github/mjeanroy/junit/servers/servers/configuration/AbstractConfiguration.java +++ b/junit-servers-core/src/main/java/com/github/mjeanroy/junit/servers/servers/configuration/AbstractConfiguration.java @@ -34,6 +34,8 @@ import static java.util.Collections.unmodifiableList; import static java.util.Collections.unmodifiableMap; +import java.net.URL; + /** * Generic configuration that should be extended for * each custom embedded server. @@ -79,6 +81,16 @@ public abstract class AbstractConfiguration { */ private final String classpath; + /** + * Additional parent (classloader) classpath. + * + * The path will be added to application parent classlodaer classpath + * before server is started. + * + */ + + private final List parentClasspath; + /** * Map of environment properties to set before server start. * @@ -101,6 +113,8 @@ public abstract class AbstractConfiguration { */ private final List hooks; + private String overrideDescriptor; + /** * Initialize configuration. * @@ -114,6 +128,8 @@ protected AbstractConfiguration(AbstractConfigurationBuilder builder) { this.port = builder.getPort(); this.envProperties = builder.getEnvProperties(); this.hooks = builder.getHooks(); + this.parentClasspath = builder.getParentClasspath(); + this.overrideDescriptor = builder.getOverrideDescriptor(); } public String getPath() { @@ -128,9 +144,17 @@ public String getClasspath() { return classpath; } + public List getParentClasspath() { + return parentClasspath; + } + public int getPort() { return port; } + + public String getOverrideDescriptor() { + return overrideDescriptor ; + } public Map getEnvProperties() { return unmodifiableMap(envProperties); @@ -152,7 +176,9 @@ public boolean equals(Object o) { Objects.equals(webapp, c.webapp) && Objects.equals(classpath, c.classpath) && Objects.equals(envProperties, c.envProperties) && - Objects.equals(hooks, c.hooks); + Objects.equals(hooks, c.hooks) && + Objects.equals(overrideDescriptor, c.overrideDescriptor) && + Objects.equals(parentClasspath, c.parentClasspath); } return false; } diff --git a/junit-servers-core/src/main/java/com/github/mjeanroy/junit/servers/servers/configuration/AbstractConfigurationBuilder.java b/junit-servers-core/src/main/java/com/github/mjeanroy/junit/servers/servers/configuration/AbstractConfigurationBuilder.java index 939fc494..a0112b60 100644 --- a/junit-servers-core/src/main/java/com/github/mjeanroy/junit/servers/servers/configuration/AbstractConfigurationBuilder.java +++ b/junit-servers-core/src/main/java/com/github/mjeanroy/junit/servers/servers/configuration/AbstractConfigurationBuilder.java @@ -27,10 +27,17 @@ import com.github.mjeanroy.junit.servers.servers.Hook; import java.io.File; +import java.io.FileFilter; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Set; import static com.github.mjeanroy.junit.servers.commons.Preconditions.notBlank; import static com.github.mjeanroy.junit.servers.commons.Preconditions.notNull; @@ -73,6 +80,13 @@ public abstract class AbstractConfigurationBuilder parentClasspath; /** * Map of properties. @@ -88,6 +102,8 @@ public abstract class AbstractConfigurationBuilder hooks; + private String overrideDescriptor; + /** * Build default configuration. */ @@ -216,4 +232,59 @@ public T withHook(Hook hook) { this.hooks.add(notNull(hook, "hook")); return self(); } + + + /** + * Change parent classpath value. + * + * @param classpath New webapp value. + * @return this + */ + public T withParentClasspath(List classpath) { + this.parentClasspath = classpath; + return self(); + } + + /** + * Change parent classpath value. + * + * @param classpath New webapp value. + * @return this + */ + public T withParentClasspath(Class cls, FileFilter filter) { + URLClassLoader urlClassLoader = (URLClassLoader) cls.getClassLoader(); + + Set urls = new HashSet<>(); + for(URL url : urlClassLoader.getURLs()) { + if(filter.accept(new File(url.getFile()))) { + urls.add(url); + } + } + this.parentClasspath = new ArrayList<>(urls); + return self(); + } + + /** + * Change parent classpath value. + * + * @param classpath New webapp value. + * @return this + */ + public T withParentClasspath(URL ... classpath) { + this.parentClasspath = Arrays.asList(classpath); + return self(); + } + + public List getParentClasspath() { + return parentClasspath; + } + + public T withOverrideDescriptor (String overrideDescriptor) { + this.overrideDescriptor = overrideDescriptor; + return self(); + } + + public String getOverrideDescriptor() { + return overrideDescriptor; + } } diff --git a/junit-servers-jetty/src/main/java/com/github/mjeanroy/junit/servers/jetty/EmbeddedJetty.java b/junit-servers-jetty/src/main/java/com/github/mjeanroy/junit/servers/jetty/EmbeddedJetty.java index 45743f40..ee8735a9 100644 --- a/junit-servers-jetty/src/main/java/com/github/mjeanroy/junit/servers/jetty/EmbeddedJetty.java +++ b/junit-servers-jetty/src/main/java/com/github/mjeanroy/junit/servers/jetty/EmbeddedJetty.java @@ -24,15 +24,23 @@ package com.github.mjeanroy.junit.servers.jetty; -import com.github.mjeanroy.junit.servers.exceptions.ServerInitializationException; -import com.github.mjeanroy.junit.servers.exceptions.ServerStartException; -import com.github.mjeanroy.junit.servers.exceptions.ServerStopException; -import com.github.mjeanroy.junit.servers.servers.AbstractEmbeddedServer; +import static com.github.mjeanroy.junit.servers.commons.Strings.isNotBlank; +import static com.github.mjeanroy.junit.servers.jetty.EmbeddedJettyConfiguration.defaultConfiguration; +import static org.eclipse.jetty.util.resource.Resource.newResource; + +import java.io.File; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.List; + +import javax.servlet.ServletContext; + import org.eclipse.jetty.annotations.AnnotationConfiguration; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.util.resource.FileResource; +import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.webapp.Configuration; import org.eclipse.jetty.webapp.FragmentConfiguration; import org.eclipse.jetty.webapp.JettyWebXmlConfiguration; @@ -41,12 +49,10 @@ import org.eclipse.jetty.webapp.WebInfConfiguration; import org.eclipse.jetty.webapp.WebXmlConfiguration; -import javax.servlet.ServletContext; -import java.io.File; - -import static com.github.mjeanroy.junit.servers.commons.Strings.isNotBlank; -import static com.github.mjeanroy.junit.servers.jetty.EmbeddedJettyConfiguration.defaultConfiguration; -import static org.eclipse.jetty.util.resource.Resource.newResource; +import com.github.mjeanroy.junit.servers.exceptions.ServerInitializationException; +import com.github.mjeanroy.junit.servers.exceptions.ServerStartException; +import com.github.mjeanroy.junit.servers.exceptions.ServerStopException; +import com.github.mjeanroy.junit.servers.servers.AbstractEmbeddedServer; /** * Jetty Embedded Server. @@ -128,23 +134,42 @@ protected WebAppContext createdWebAppContext() throws Exception { final String path = configuration.getPath(); final String webapp = configuration.getWebapp(); final String classpath = configuration.getClasspath(); + final List parentClasspath = configuration.getParentClasspath(); + final String overrideDescriptor = configuration.getOverrideDescriptor (); + final Resource baseResource = configuration.getBaseResource(); + + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + + if(parentClasspath != null && !parentClasspath.isEmpty()) { + classLoader = new URLClassLoader(parentClasspath.toArray(new URL[parentClasspath.size()]), classLoader); + } WebAppContext ctx = new WebAppContext(); - ctx.setClassLoader(Thread.currentThread().getContextClassLoader()); + ctx.setClassLoader(classLoader); ctx.setContextPath(path); - // Useful for WebXmlConfiguration - ctx.setBaseResource(newResource(webapp)); + if(baseResource == null) { + // use default base resource + ctx.setBaseResource(newResource(webapp)); + } else { + ctx.setBaseResource(baseResource); + } + + if(overrideDescriptor != null) { + ctx.setOverrideDescriptor(overrideDescriptor); + } ctx.setConfigurations(new Configuration[]{ - new WebInfConfiguration(), + new WebInfConfiguration(), new WebXmlConfiguration(), new AnnotationConfiguration(), new JettyWebXmlConfiguration(), new MetaInfConfiguration(), - new FragmentConfiguration(), + new FragmentConfiguration() }); + ctx.addOverrideDescriptor(overrideDescriptor); + if (isNotBlank(classpath)) { // Fix to scan Spring WebApplicationInitializer // This will add compiled classes to jetty classpath diff --git a/junit-servers-jetty/src/main/java/com/github/mjeanroy/junit/servers/jetty/EmbeddedJettyConfiguration.java b/junit-servers-jetty/src/main/java/com/github/mjeanroy/junit/servers/jetty/EmbeddedJettyConfiguration.java index befdad2e..2a262c0c 100644 --- a/junit-servers-jetty/src/main/java/com/github/mjeanroy/junit/servers/jetty/EmbeddedJettyConfiguration.java +++ b/junit-servers-jetty/src/main/java/com/github/mjeanroy/junit/servers/jetty/EmbeddedJettyConfiguration.java @@ -29,6 +29,8 @@ import static com.github.mjeanroy.junit.servers.commons.Preconditions.positive; +import org.eclipse.jetty.util.resource.Resource; + /** * Jetty configuration settings. */ @@ -46,6 +48,8 @@ public final class EmbeddedJettyConfiguration extends AbstractConfiguration { */ private final boolean stopAtShutdown; + private Resource baseResource; + /** * Get configuration builder. * @@ -69,6 +73,7 @@ private EmbeddedJettyConfiguration(Builder builder) { super(builder); this.stopTimeout = builder.getStopTimeout(); this.stopAtShutdown = builder.isStopAtShutdown(); + this.baseResource = builder.getBaseResource(); } public int getStopTimeout() { @@ -89,12 +94,18 @@ public int hashCode() { return super.hashCode(); } + public Resource getBaseResource() { + return baseResource; + } + public static class Builder extends AbstractConfigurationBuilder { private int stopTimeout; private boolean stopAtShutdown; + private Resource baseResource; + private Builder() { stopTimeout = 30000; stopAtShutdown = true; @@ -118,6 +129,10 @@ public boolean isStopAtShutdown() { return stopAtShutdown; } + public Resource getBaseResource() { + return baseResource; + } + /** * Update stop timeout value. * @param stopTimeout New stop timeout value. @@ -141,5 +156,11 @@ private Builder toggleStopAtShutdown(boolean stopAtShutdown) { this.stopAtShutdown = stopAtShutdown; return this; } + + public Builder withBaseResource(Resource resource) { + this.baseResource = resource; + return this; + } + } } diff --git a/junit-servers-jetty/src/main/webapp/index.html b/junit-servers-jetty/src/main/webapp/index.html new file mode 100644 index 00000000..e69de29b diff --git a/samples/pom.xml b/samples/pom.xml index 2c70b873..eac719be 100644 --- a/samples/pom.xml +++ b/samples/pom.xml @@ -42,6 +42,7 @@ spring-java-jetty spring-webxml-jetty + spring-webxml-jetty-jsp spring-java-tomcat spring-webxml-tomcat @@ -106,4 +107,4 @@ - \ No newline at end of file + diff --git a/samples/spring-webxml-jetty-jsp/pom.xml b/samples/spring-webxml-jetty-jsp/pom.xml new file mode 100644 index 00000000..02eb40b3 --- /dev/null +++ b/samples/spring-webxml-jetty-jsp/pom.xml @@ -0,0 +1,172 @@ + + + + + + + 4.0.0 + + + junit-servers-samples + com.github.mjeanroy + 0.4.3-SNAPSHOT + + + spring-webxml-jetty-jsp + 0.4.3-SNAPSHOT + junit-servers-spring-webxml-jetty-jsp + war + https://github.com/mjeanroy/junit-servers + + + 1.2.1 + r239 + + + + + org.springframework + spring-webmvc + + + org.owasp.encoder + encoder + ${owasp-xss-encoder.version} + + + org.owasp.encoder + encoder-jsp + ${owasp-xss-encoder.version} + + + com.googlecode.owasp-java-html-sanitizer + owasp-java-html-sanitizer + ${owasp-html-sanitizer.version} + + + org.eclipse.jetty + apache-jstl + ${jetty.version} + + + org.eclipse.jetty + jetty-jsp + + + javax.servlet + javax.servlet-api + provided + + + + com.github.mjeanroy + junit-servers-jetty + test + + + org.assertj + assertj-core + test + + + + + + + org.eclipse.jetty + jetty-maven-plugin + ${jetty.version} + + 1 + stop-jetty + 9999 + + + + + org.apache.maven.plugins + maven-deploy-plugin + ${maven-deploy-plugin.version} + + true + + + + + org.apache.maven.plugins + maven-war-plugin + ${maven-war-plugin} + + false + + + + + maven-dependency-plugin + + + validate + + copy + + + ${project.build.directory}/lib + + + org.eclipse.jetty + apache-jstl + ${jetty.version} + jar + true + ${basedir}/target/lib + + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + false + + + + + org.eclipse.jetty + jetty-jspc-maven-plugin + ${jetty.version} + + + validate + jspc + + jspc + + + + + + + + diff --git a/samples/spring-webxml-jetty-jsp/src/main/java/com/github/mjeanroy/junit/servers/samples/jetty/webxml/configuration/SpringConfiguration.java b/samples/spring-webxml-jetty-jsp/src/main/java/com/github/mjeanroy/junit/servers/samples/jetty/webxml/configuration/SpringConfiguration.java new file mode 100644 index 00000000..0227f748 --- /dev/null +++ b/samples/spring-webxml-jetty-jsp/src/main/java/com/github/mjeanroy/junit/servers/samples/jetty/webxml/configuration/SpringConfiguration.java @@ -0,0 +1,34 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2014 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.github.mjeanroy.junit.servers.samples.jetty.webxml.configuration; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan(basePackages = "com.github.mjeanroy.junit.servers.samples") +public class SpringConfiguration { + +} diff --git a/samples/spring-webxml-jetty-jsp/src/main/java/com/github/mjeanroy/junit/servers/samples/jetty/webxml/configuration/SpringMvcConfiguration.java b/samples/spring-webxml-jetty-jsp/src/main/java/com/github/mjeanroy/junit/servers/samples/jetty/webxml/configuration/SpringMvcConfiguration.java new file mode 100644 index 00000000..4ade5df4 --- /dev/null +++ b/samples/spring-webxml-jetty-jsp/src/main/java/com/github/mjeanroy/junit/servers/samples/jetty/webxml/configuration/SpringMvcConfiguration.java @@ -0,0 +1,54 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2014 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.github.mjeanroy.junit.servers.samples.jetty.webxml.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; +import org.springframework.web.servlet.view.InternalResourceViewResolver; + +@Configuration +@EnableWebMvc +@ComponentScan(basePackages = "com.github.mjeanroy.junit.servers.samples") +public class SpringMvcConfiguration extends WebMvcConfigurationSupport { + + @Override + public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){ + configurer.enable(); + } + + @Bean + public ViewResolver getViewResolver(){ + InternalResourceViewResolver resolver = new InternalResourceViewResolver(); + resolver.setPrefix("/WEB-INF/views/"); + resolver.setSuffix(".jsp"); + return resolver; + } +} diff --git a/samples/spring-webxml-jetty-jsp/src/main/java/com/github/mjeanroy/junit/servers/samples/jetty/webxml/controllers/IndexController.java b/samples/spring-webxml-jetty-jsp/src/main/java/com/github/mjeanroy/junit/servers/samples/jetty/webxml/controllers/IndexController.java new file mode 100644 index 00000000..38692d05 --- /dev/null +++ b/samples/spring-webxml-jetty-jsp/src/main/java/com/github/mjeanroy/junit/servers/samples/jetty/webxml/controllers/IndexController.java @@ -0,0 +1,45 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2014 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.github.mjeanroy.junit.servers.samples.jetty.webxml.controllers; + +import static org.springframework.web.bind.annotation.RequestMethod.GET; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.servlet.ModelAndView; + +@Controller +public class IndexController { + + @RequestMapping(value = {"/", "/index"}, method = GET) + @ResponseBody + public ModelAndView index() { + String message = "Hello"; + ModelAndView view = new ModelAndView("welcome"); + view.addObject("message", message); + return view; + } +} diff --git a/samples/spring-webxml-jetty-jsp/src/main/webapp/WEB-INF/views/welcome.jsp b/samples/spring-webxml-jetty-jsp/src/main/webapp/WEB-INF/views/welcome.jsp new file mode 100644 index 00000000..a0bec3db --- /dev/null +++ b/samples/spring-webxml-jetty-jsp/src/main/webapp/WEB-INF/views/welcome.jsp @@ -0,0 +1,17 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> +<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="e" uri="https://www.owasp.org/index.php/OWASP_Java_Encoder_Project" %> + + + + Embedded Jetty taglibs. + + +
Unsafe:
+
Safe:
+ + + The current URL is HERE + + diff --git a/samples/spring-webxml-jetty-jsp/src/main/webapp/WEB-INF/web.xml b/samples/spring-webxml-jetty-jsp/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 00000000..130ea1c3 --- /dev/null +++ b/samples/spring-webxml-jetty-jsp/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,62 @@ + + + + + + contextClass + org.springframework.web.context.support.AnnotationConfigWebApplicationContext + + + contextConfigLocation + com.github.mjeanroy.junit.servers.samples.jetty.webxml.configuration.SpringConfiguration + + + + org.springframework.web.context.ContextLoaderListener + + + + spring + spring + org.springframework.web.servlet.DispatcherServlet + + contextClass + org.springframework.web.context.support.AnnotationConfigWebApplicationContext + + + contextConfigLocation + com.github.mjeanroy.junit.servers.samples.jetty.webxml.configuration + + 1 + + + + + /views/* + UTF-8 + + + + + + spring + / + + + \ No newline at end of file diff --git a/samples/spring-webxml-jetty-jsp/src/test/java/com/github/mjeanroy/junit/servers/samples/jetty/webxml/IndexWithRulesTest.java b/samples/spring-webxml-jetty-jsp/src/test/java/com/github/mjeanroy/junit/servers/samples/jetty/webxml/IndexWithRulesTest.java new file mode 100644 index 00000000..99afeb62 --- /dev/null +++ b/samples/spring-webxml-jetty-jsp/src/test/java/com/github/mjeanroy/junit/servers/samples/jetty/webxml/IndexWithRulesTest.java @@ -0,0 +1,94 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2014 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.github.mjeanroy.junit.servers.samples.jetty.webxml; + +import com.github.mjeanroy.junit.servers.jetty.EmbeddedJetty; +import com.github.mjeanroy.junit.servers.jetty.EmbeddedJettyConfiguration; +import com.github.mjeanroy.junit.servers.rules.JettyServerRule; + +import org.eclipse.jetty.webapp.WebAppContext; +import org.junit.ClassRule; +import org.junit.Test; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.WebApplicationContextUtils; + +import javax.servlet.ServletContext; +import java.io.File; +import java.io.FileFilter; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +public class IndexWithRulesTest { + + private static final String PATH = "samples/spring-webxml-jetty-jsp/"; + + private static EmbeddedJettyConfiguration configuration = initConfiguration(); + + private static EmbeddedJettyConfiguration initConfiguration() { + try { + String current = new File(".").getCanonicalPath(); + if (!current.endsWith("/")) { + current += "/"; + } + + String path = current.endsWith(PATH) ? current : current + PATH; + + return EmbeddedJettyConfiguration.builder() + .withWebapp(path + "src/main/webapp") + .withClasspath(path + "target/classes") + .withParentClasspath(WebAppContext.class, new FileFilter() { + @Override + public boolean accept(File pathname) { + return pathname.getName().startsWith("apache-jstl"); + } + }) + .build(); + } + catch (Exception ex) { + throw new RuntimeException(ex); + } + } + + private static EmbeddedJetty jetty = new EmbeddedJetty(configuration); + + private static RestTemplate restTemplate = new RestTemplate(); + + @ClassRule + public static JettyServerRule serverRule = new JettyServerRule(jetty); + + @Test + public void it_should_have_an_index() { + String url = jetty.getUrl(); + String message = restTemplate.getForObject(url, String.class); + assertThat(message).isNotEmpty().contains("Hello"); + } + +} diff --git a/samples/spring-webxml-jetty-jsp/src/test/java/com/github/mjeanroy/junit/servers/samples/jetty/webxml/IndexWithRunnerTest.java b/samples/spring-webxml-jetty-jsp/src/test/java/com/github/mjeanroy/junit/servers/samples/jetty/webxml/IndexWithRunnerTest.java new file mode 100644 index 00000000..8eb95391 --- /dev/null +++ b/samples/spring-webxml-jetty-jsp/src/test/java/com/github/mjeanroy/junit/servers/samples/jetty/webxml/IndexWithRunnerTest.java @@ -0,0 +1,77 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2014 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.github.mjeanroy.junit.servers.samples.jetty.webxml; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.File; +import java.net.URL; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.web.client.RestTemplate; + +import com.github.mjeanroy.junit.servers.annotations.TestServer; +import com.github.mjeanroy.junit.servers.annotations.TestServerConfiguration; +import com.github.mjeanroy.junit.servers.jetty.EmbeddedJettyConfiguration; +import com.github.mjeanroy.junit.servers.runner.JunitServerRunner; +import com.github.mjeanroy.junit.servers.servers.EmbeddedServer; + +@RunWith(JunitServerRunner.class) +public class IndexWithRunnerTest { + + @TestServer + private static EmbeddedServer jetty; + + @TestServerConfiguration + private static EmbeddedJettyConfiguration configuration() throws Exception { + String current = new File(".").getCanonicalPath(); + if (!current.endsWith("/")) { + current += "/"; + } + + String subProjectPath = "samples/spring-webxml-jetty-jsp/"; + String path = current.endsWith(subProjectPath) ? current : current + subProjectPath; + + // note use of maven plugin to copy a maven dependency to this directory + URL urlParentClasspath = new File("target/lib/").toURI().toURL(); + + return EmbeddedJettyConfiguration.builder() + .withWebapp(path + "src/main/webapp") + .withParentClasspath(urlParentClasspath) + .withClasspath(path + "target/classes") + .build(); + } + + private static RestTemplate restTemplate = new RestTemplate(); + + @Test + public void it_should_have_an_index() { + String url = jetty.getUrl(); + String message = restTemplate.getForObject(url, String.class); + assertThat(message).isNotEmpty().contains("Hello"); + } + +}