Author: ivol37 at gmail.com
Date: Mon Nov 8 11:25:09 2010
New Revision: 253
Log:
[AMDATU-154] Added integration tests to test filters, servlets and JSPs running
in the Amdatu HTTP context
Added:
trunk/integration-tests/src/test/java/org/amdatu/test/integration/tests/HttpServiceTest.java
trunk/integration-tests/src/test/resources/
trunk/integration-tests/src/test/resources/jsp/
trunk/integration-tests/src/test/resources/jsp/test.jsp
Modified:
trunk/amdatu-web/jsp-support-bundle/src/main/java/org/amdatu/web/jsp/service/ResourceProviderListener.java
trunk/integration-tests/pom.xml
trunk/integration-tests/src/test/java/org/amdatu/test/integration/base/IntegrationTestBase.java
Modified:
trunk/amdatu-web/jsp-support-bundle/src/main/java/org/amdatu/web/jsp/service/ResourceProviderListener.java
==============================================================================
---
trunk/amdatu-web/jsp-support-bundle/src/main/java/org/amdatu/web/jsp/service/ResourceProviderListener.java
(original)
+++
trunk/amdatu-web/jsp-support-bundle/src/main/java/org/amdatu/web/jsp/service/ResourceProviderListener.java
Mon Nov 8 11:25:09 2010
@@ -96,7 +96,7 @@
component.add(m_dependencyManager.createServiceDependency().setService(LogService.class).setRequired(true));
// Add the component to the dependency manager
- m_components.put(provider.getResourceId(), component);
+ m_components.put(normalizeResourceId(provider), component);
m_dependencyManager.add(component);
try {
@@ -119,15 +119,27 @@
public void onRemoved(ResourceProvider provider) {
try {
m_httpService.unregister(getServletAlias(provider));
-
m_dependencyManager.remove(m_components.get(provider.getResourceId()));
+
m_dependencyManager.remove(m_components.get(normalizeResourceId(provider)));
}
finally {
m_resourceProviders.remove(provider);
}
}
+
+ // Returns the resource Id trimming leading and ending slashes
+ private String normalizeResourceId(ResourceProvider provider) {
+ String resId = provider.getResourceId();
+ if (resId.startsWith("/")) {
+ resId = resId.substring(1);
+ }
+ if (resId.endsWith("/")) {
+ resId = resId.substring(0, provider.getResourceId().length()-1);
+ }
+ return resId;
+ }
private String getServletAlias(ResourceProvider provider) {
- return "/" + provider.getResourceId() + "/" + JSP_SERVLET_PATH;
+ return "/" + normalizeResourceId(provider) + "/" + JSP_SERVLET_PATH;
}
public URL getResource(String name) {
Modified: trunk/integration-tests/pom.xml
==============================================================================
--- trunk/integration-tests/pom.xml (original)
+++ trunk/integration-tests/pom.xml Mon Nov 8 11:25:09 2010
@@ -133,9 +133,20 @@
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
- <scope>compile</scope>
+ <scope>test</scope>
</dependency>
-
+ <dependency>
+ <groupId>commons-logging</groupId>
+ <artifactId>commons-logging</artifactId>
+ <version>1.1.1</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>commons-codec</groupId>
+ <artifactId>commons-codec</artifactId>
+ <version>1.4</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
@@ -147,6 +158,7 @@
<instructions>
<Bundle-Activator>org.amdatu.test.osgi.Activator</Bundle-Activator>
<Bundle-SymbolicName>org.amdatu.test</Bundle-SymbolicName>
+
</instructions>
</configuration>
</plugin>
Modified:
trunk/integration-tests/src/test/java/org/amdatu/test/integration/base/IntegrationTestBase.java
==============================================================================
---
trunk/integration-tests/src/test/java/org/amdatu/test/integration/base/IntegrationTestBase.java
(original)
+++
trunk/integration-tests/src/test/java/org/amdatu/test/integration/base/IntegrationTestBase.java
Mon Nov 8 11:25:09 2010
@@ -24,6 +24,7 @@
import static org.ops4j.pax.exam.CoreOptions.options;
import static org.ops4j.pax.exam.CoreOptions.provision;
import static org.ops4j.pax.exam.CoreOptions.systemProperty;
+import static org.ops4j.pax.exam.CoreOptions.wrappedBundle;
import java.io.File;
import java.io.FileFilter;
@@ -40,6 +41,7 @@
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.container.def.options.VMOption;
import org.ops4j.pax.exam.options.MavenArtifactProvisionOption;
+import org.ops4j.pax.exam.options.WrappedUrlProvisionOption;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.InvalidSyntaxException;
@@ -102,6 +104,9 @@
felixScr(),
paxSwissbox(),
ops4jBaseLang(),
+ commonsHttpClient(),
+ commonsLogging(),
+ commonsCodec(),
// Amdatu platform bundles
// TODO: this works fine when running 'mvn install' since the
artifacts will then be deployed to the maven
@@ -347,5 +352,15 @@
return
mavenBundle().groupId("org.amdatu.web").artifactId("jsp-support").versionAsInProject();
}
+ protected static WrappedUrlProvisionOption commonsHttpClient() {
+ return
wrappedBundle(mavenBundle().groupId("commons-httpclient").artifactId("commons-httpclient"));
+ }
+
+ protected static WrappedUrlProvisionOption commonsLogging() {
+ return
wrappedBundle(mavenBundle().groupId("commons-logging").artifactId("commons-logging"));
+ }
+ protected static WrappedUrlProvisionOption commonsCodec() {
+ return
wrappedBundle(mavenBundle().groupId("commons-codec").artifactId("commons-codec"));
+ }
}
Added:
trunk/integration-tests/src/test/java/org/amdatu/test/integration/tests/HttpServiceTest.java
==============================================================================
--- (empty file)
+++
trunk/integration-tests/src/test/java/org/amdatu/test/integration/tests/HttpServiceTest.java
Mon Nov 8 11:25:09 2010
@@ -0,0 +1,221 @@
+package org.amdatu.test.integration.tests;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.Servlet;
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletResponse;
+
+import junit.framework.Assert;
+
+import org.amdatu.platform.httpcontext.HttpContextServiceFactory;
+import org.amdatu.platform.httpcontext.ResourceProvider;
+import org.amdatu.test.integration.base.IntegrationTestBase;
+import org.apache.commons.httpclient.Header;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.HttpStatus;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.felix.dm.Component;
+import org.apache.felix.dm.DependencyManager;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.Configuration;
+import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+import org.osgi.service.http.HttpContext;
+import org.osgi.service.http.HttpService;
+
+ at RunWith(JUnit4TestRunner.class)
+public class HttpServiceTest extends IntegrationTestBase {
+ private final static String RESOURCE_ID = "test";
+ private final static String SERVLET_ALIAS = "/test/servlet";
+ private final static String TEST_TEXT = "Integration test successfull!";
+ private final static String TEST_HEADER_BEFORE = "testFilterBefore";
+ private final static String TEST_HEADER_BEFORE_VALUE = "before";
+ private final static String TEST_HEADER_AFTER = "testFilterAfter";
+ private final static String TEST_HEADER_AFTER_VALUE = "after";
+ private final static String TEST_JSP_CONTENT = "<html><body>Test JSP
body</body></html>";
+
+ private volatile HttpContextServiceFactory m_httpContextFactoryService;
+
+ private Component m_httpContextComponent;
+ private String m_baseUrl;
+ private TestServlet m_testServlet;
+ private HttpContext m_httpContext;
+
+ @Configuration
+ public Option[] configure() {
+ return super.configure();
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ m_baseUrl = "http://" + IntegrationTestBase.HOST_NAME + ":" +
IntegrationTestBase.PORT_NR;
+
+ // Initialize the HTTP Context in which the filter, servlet and JSP
under test are registered
+ m_testServlet = new TestServlet();
+ m_httpContextFactoryService =
getService(HttpContextServiceFactory.class);
+ m_httpContextComponent =
m_httpContextFactoryService.create(m_bundleContext, m_testServlet);
+ m_httpContext = (HttpContext) m_httpContextComponent.getService();
+ }
+
+ @Override
+ protected Component[] getDependencies(DependencyManager manager) {
+ // We register a servlet filter (whiteboard-style) that adds a test
header to each HTTP response
+ Dictionary<String, String> filterProperties = new Hashtable<String,
String>();
+ filterProperties.put("pattern", ".*");
+ filterProperties.put("contextId", SERVLET_ALIAS);
+ filterProperties.put("service.ranking", "0");
+ manager.add(manager.createComponent()
+ .setImplementation(new TestFilter())
+ .setInterface(new String[] { Filter.class.getName(),
TestFilterInterface.class.getName() },
+ filterProperties));
+
+ // Now we register a test servlet, also whiteboard-style
+ Dictionary<String, String> servletProperties = new Hashtable<String,
String>();
+ servletProperties.put("alias", SERVLET_ALIAS);
+ servletProperties.put("contextId", SERVLET_ALIAS);
+ manager.add(manager.createComponent()
+ .setImplementation(m_testServlet)
+ .setInterface(
+ new String[] { Servlet.class.getName(),
TestServletInterface.class.getName(),
+ ResourceProvider.class.getName() },
+ servletProperties));
+
+ return new Component[] { manager.createComponent()
+ .setImplementation(this)
+
.add(manager.createServiceDependency().setService(TestFilterInterface.class).setRequired(true))
+
.add(manager.createServiceDependency().setService(TestServletInterface.class).setRequired(true))
+
.add(manager.createServiceDependency().setService(HttpService.class).setRequired(true))
};
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ m_httpContextComponent.stop();
+ }
+
+ @Test
+ public void testFilter() throws Exception {
+ // Our filter should be registered, use httpclient to do a GET
request. The filter should
+ // set some response header and we check if this header has been set
+ HttpClient httpClient = new HttpClient();
+ String url = m_baseUrl + SERVLET_ALIAS;
+ HttpMethod method = new GetMethod(url);
+ try {
+ // Execute the method, this should return a 200
+ int statusCode = httpClient.executeMethod(method);
+ Assert.assertTrue("HTTP GET to '" + url + "' returned " +
statusCode, statusCode == HttpStatus.SC_OK);
+
+ // Read the response body.
+ byte[] responseBody = method.getResponseBody();
+
+ // Check if the response is our test text
+ Assert.assertTrue(TEST_TEXT.equals(new String(responseBody)));
+
+ // Now check if the filter did his job properly
+ Header[] resHeaders =
method.getResponseHeaders(TEST_HEADER_BEFORE);
+ Assert.assertEquals("No response header was set by filter before
filterChain invocation",
+ resHeaders.length, 1);
+ Assert.assertTrue("Repsonse header was not properly set by filter
before filterChain invocation",
+ TEST_HEADER_BEFORE_VALUE.equals(resHeaders[0].getValue()));
+ resHeaders = method.getResponseHeaders(TEST_HEADER_AFTER);
+ Assert.assertEquals("No response header was set by filter after
filterChain invocation", resHeaders.length,
+ 1);
+ Assert.assertTrue("Repsonse header was not properly set by filter
after filterChain invocation",
+ TEST_HEADER_AFTER_VALUE.equals(resHeaders[0].getValue()));
+ }
+ finally {
+ // Release the connection.
+ method.releaseConnection();
+ }
+ }
+
+ @Test
+ public void testJsp() throws Exception {
+ // Our filter should be registered, use httpclient to do a GET
request. The filter should
+ // set some response header and we check if this header has been set
+ HttpClient httpClient = new HttpClient();
+ String url = m_baseUrl + "/" + RESOURCE_ID + "/jsp/test.jsp";
+ HttpMethod method = new GetMethod(url);
+ try {
+ // Execute the method, this should return a 200
+ int statusCode = httpClient.executeMethod(method);
+ Assert.assertTrue("HTTP GET to '" + url + "' returned " +
statusCode, statusCode == HttpStatus.SC_OK);
+
+ // Read the response body.
+ String responseBody = new String(method.getResponseBody(),
"UTF-8");
+
+ // Check if the response is our test text
+ Assert.assertTrue(responseBody.indexOf(TEST_JSP_CONTENT) != -1);
+ }
+ finally {
+ // Release the connection.
+ method.releaseConnection();
+ }
+ }
+
+ /**
+ * A simple TestFilter that sets a header testFilterBefore to 'true'
before executing the rest of
+ * the filter chain and sets testFilterAfter to 'true' afterwards.
+ */
+ class TestFilter implements Filter, TestFilterInterface {
+ public void init(FilterConfig filterconfig) throws ServletException {
+ }
+
+ public void destroy() {
+ }
+
+ public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException,
+ ServletException {
+ ((HttpServletResponse) res).setHeader(TEST_HEADER_BEFORE,
TEST_HEADER_BEFORE_VALUE);
+ chain.doFilter(req, res);
+ ((HttpServletResponse) res).setHeader(TEST_HEADER_AFTER,
TEST_HEADER_AFTER_VALUE);
+ }
+ }
+
+ interface TestFilterInterface {
+ }
+
+ class TestServlet implements Servlet, TestServletInterface,
ResourceProvider {
+ public void destroy() {
+ }
+
+ public ServletConfig getServletConfig() {
+ return null;
+ }
+
+ public String getServletInfo() {
+ return null;
+ }
+
+ public void init(ServletConfig arg0) throws ServletException {
+ }
+
+ public void service(ServletRequest req, ServletResponse res) throws
ServletException, IOException {
+ res.getWriter().append(TEST_TEXT);
+ }
+
+ public URL getResource(String name) {
+ return null;
+ }
+
+ public String getResourceId() {
+ return RESOURCE_ID;
+ }
+ }
+
+ interface TestServletInterface {
+ }
+}
\ No newline at end of file
Added: trunk/integration-tests/src/test/resources/jsp/test.jsp
==============================================================================
--- (empty file)
+++ trunk/integration-tests/src/test/resources/jsp/test.jsp Mon Nov 8
11:25:09 2010
@@ -0,0 +1,3 @@
+<%@ page %>
+<!DOCTYPE html>
+<html><body>Test JSP body</body></html>
\ No newline at end of file