From bdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a Mon Sep 17 00:00:00 2001 From: dims Date: Tue, 17 Jun 2008 00:23:01 +0000 Subject: Move Tuscany from Incubator to top level. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68 --- .../webapp/TuscanyContextListenerTestCase.java | 113 ++++++++++++++++++ .../runtime/webapp/TuscanyFilterTestCase.java | 112 ++++++++++++++++++ .../runtime/webapp/TuscanyServletTestCase.java | 80 +++++++++++++ .../webapp/TuscanySessionListenerTestCase.java | 78 ++++++++++++ .../tuscany/runtime/webapp/WebappUtilTestCase.java | 131 +++++++++++++++++++++ 5 files changed, 514 insertions(+) create mode 100644 sandbox/old/contrib/runtime-webapp/webapp-api/src/test/java/org/apache/tuscany/runtime/webapp/TuscanyContextListenerTestCase.java create mode 100644 sandbox/old/contrib/runtime-webapp/webapp-api/src/test/java/org/apache/tuscany/runtime/webapp/TuscanyFilterTestCase.java create mode 100644 sandbox/old/contrib/runtime-webapp/webapp-api/src/test/java/org/apache/tuscany/runtime/webapp/TuscanyServletTestCase.java create mode 100644 sandbox/old/contrib/runtime-webapp/webapp-api/src/test/java/org/apache/tuscany/runtime/webapp/TuscanySessionListenerTestCase.java create mode 100644 sandbox/old/contrib/runtime-webapp/webapp-api/src/test/java/org/apache/tuscany/runtime/webapp/WebappUtilTestCase.java (limited to 'sandbox/old/contrib/runtime-webapp/webapp-api/src/test/java/org') diff --git a/sandbox/old/contrib/runtime-webapp/webapp-api/src/test/java/org/apache/tuscany/runtime/webapp/TuscanyContextListenerTestCase.java b/sandbox/old/contrib/runtime-webapp/webapp-api/src/test/java/org/apache/tuscany/runtime/webapp/TuscanyContextListenerTestCase.java new file mode 100644 index 0000000000..aa237da01c --- /dev/null +++ b/sandbox/old/contrib/runtime-webapp/webapp-api/src/test/java/org/apache/tuscany/runtime/webapp/TuscanyContextListenerTestCase.java @@ -0,0 +1,113 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.runtime.webapp; + +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URI; +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; + +import junit.framework.TestCase; +import static org.easymock.classextension.EasyMock.createMock; +import static org.easymock.classextension.EasyMock.eq; +import static org.easymock.classextension.EasyMock.expect; +import static org.easymock.classextension.EasyMock.isA; +import static org.easymock.classextension.EasyMock.replay; +import static org.easymock.classextension.EasyMock.verify; + +import static org.apache.tuscany.runtime.webapp.Constants.APPLICATION_SCDL_PATH_PARAM; +import static org.apache.tuscany.runtime.webapp.Constants.APPLICATION_SCDL_PATH_DEFAULT; + +/** + * @version $Rev$ $Date$ + */ +public class TuscanyContextListenerTestCase extends TestCase { + private String contextName; + private ServletContext context; + private TuscanyContextListener listener; + private ClassLoader cl; + private ClassLoader bootClassLoader; + private URL systemUrl; + private URL scdl; + private WebappUtil utils; + private String compositeId; + + public void testInitializationUsingDefaults() throws Exception { + ServletContextEvent event = createMock(ServletContextEvent.class); + expect(event.getServletContext()).andReturn(context); + replay(event); + + WebappRuntime runtime = createMock(WebappRuntime.class); + expect(utils.getBootClassLoader(cl)).andReturn(bootClassLoader); + expect(utils.getInitParameter("tuscany.composite", compositeId)).andReturn(compositeId); + expect(utils.getInitParameter("tuscany.component", contextName)).andReturn(contextName); + expect(utils.getInitParameter("tuscany.online", "true")).andReturn("true"); + expect(utils.getInitParameter(APPLICATION_SCDL_PATH_PARAM, APPLICATION_SCDL_PATH_DEFAULT)) + .andReturn(APPLICATION_SCDL_PATH_DEFAULT); + expect(utils.getRuntime(bootClassLoader)).andReturn(runtime); + expect(utils.getSystemScdl(bootClassLoader)).andReturn(systemUrl); + replay(utils); + + expect(context.getResource("/WEB-INF/tuscany/")).andReturn(null); + expect(context.getResource(APPLICATION_SCDL_PATH_DEFAULT)).andReturn(scdl); + context.setAttribute(eq(Constants.RUNTIME_ATTRIBUTE), isA(WebappRuntime.class)); + replay(context); + replay(cl); + replay(bootClassLoader); + expect(listener.getUtils(context)).andReturn(utils); + replay(listener); + runtime.setServletContext(context); + runtime.setRuntimeInfo(isA(WebappRuntimeInfo.class)); + runtime.setHostClassLoader(cl); + runtime.setSystemScdl(systemUrl); + runtime.initialize(); + runtime.deploy(URI.create(compositeId), scdl, URI.create(contextName)); + replay(runtime); + + ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); + try { + Thread.currentThread().setContextClassLoader(cl); + listener.contextInitialized(event); + } finally { + Thread.currentThread().setContextClassLoader(oldCl); + } + verify(event); + verify(utils); + verify(context); + verify(listener); + verify(cl); + verify(bootClassLoader); + verify(runtime); + } + + protected void setUp() throws Exception { + super.setUp(); + Method getUtilsMethod = TuscanyContextListener.class.getDeclaredMethod("getUtils", ServletContext.class); + utils = createMock(WebappUtil.class); + listener = createMock(TuscanyContextListener.class, new Method[]{getUtilsMethod}); + context = createMock(ServletContext.class); + cl = createMock(ClassLoader.class); + bootClassLoader = createMock(ClassLoader.class); + systemUrl = new URL("file:/system.scdl"); + scdl = new URL("file:/app.scdl"); + contextName = "webapp"; + compositeId = "http://locahost/sca"; + } +} diff --git a/sandbox/old/contrib/runtime-webapp/webapp-api/src/test/java/org/apache/tuscany/runtime/webapp/TuscanyFilterTestCase.java b/sandbox/old/contrib/runtime-webapp/webapp-api/src/test/java/org/apache/tuscany/runtime/webapp/TuscanyFilterTestCase.java new file mode 100644 index 0000000000..9d811df46c --- /dev/null +++ b/sandbox/old/contrib/runtime-webapp/webapp-api/src/test/java/org/apache/tuscany/runtime/webapp/TuscanyFilterTestCase.java @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.runtime.webapp; + +import java.io.IOException; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; + +import junit.framework.TestCase; +import org.easymock.EasyMock; + +import static org.apache.tuscany.runtime.webapp.Constants.RUNTIME_ATTRIBUTE; + +/** + * @version $Rev$ $Date$ + */ +public class TuscanyFilterTestCase extends TestCase { + private TuscanyFilter filter; + private FilterConfig config; + private ServletContext servletContext; + private WebappRuntime runtime; + private ServletRequest request; + private ServletResponse response; + private FilterChain chain; + + public void testFilterInit() { + EasyMock.expect(config.getServletContext()).andReturn(servletContext); + EasyMock.expect(servletContext.getAttribute(RUNTIME_ATTRIBUTE)).andReturn(runtime); + + EasyMock.replay(servletContext); + EasyMock.replay(config); + EasyMock.replay(runtime); + try { + filter.init(config); + } catch (ServletException e) { + fail(e.getMessage()); + } + EasyMock.verify(servletContext); + EasyMock.verify(config); + EasyMock.verify(runtime); + } + + public void testFilterInitWithNoRuntimeConfigured() { + EasyMock.expect(config.getServletContext()).andReturn(servletContext); + EasyMock.expect(servletContext.getAttribute(RUNTIME_ATTRIBUTE)).andReturn(null); + + EasyMock.replay(servletContext); + EasyMock.replay(config); + EasyMock.replay(runtime); + try { + filter.init(config); + fail("Expected a ServletException"); + } catch (ServletException e) { + // OK + } + EasyMock.verify(servletContext); + EasyMock.verify(config); + EasyMock.verify(runtime); + } + + public void testContextIsAssociatedWithThread() throws ServletException, IOException { + EasyMock.expect(config.getServletContext()).andReturn(servletContext); + EasyMock.expect(servletContext.getAttribute(RUNTIME_ATTRIBUTE)).andReturn(runtime); + EasyMock.replay(servletContext); + EasyMock.replay(config); + filter.init(config); + + chain.doFilter(EasyMock.same(request), EasyMock.same(response)); + EasyMock.replay(chain); + runtime.startRequest(); + runtime.stopRequest(); + EasyMock.replay(runtime); + try { + filter.doFilter(request, response, chain); + } catch (IOException e) { + fail(e.getMessage()); + } + EasyMock.verify(chain); + EasyMock.verify(runtime); + } + + protected void setUp() throws Exception { + super.setUp(); + filter = new TuscanyFilter(); + config = EasyMock.createMock(FilterConfig.class); + servletContext = EasyMock.createMock(ServletContext.class); + runtime = EasyMock.createMock(WebappRuntime.class); + request = EasyMock.createMock(ServletRequest.class); + response = EasyMock.createMock(ServletResponse.class); + chain = EasyMock.createMock(FilterChain.class); + } +} diff --git a/sandbox/old/contrib/runtime-webapp/webapp-api/src/test/java/org/apache/tuscany/runtime/webapp/TuscanyServletTestCase.java b/sandbox/old/contrib/runtime-webapp/webapp-api/src/test/java/org/apache/tuscany/runtime/webapp/TuscanyServletTestCase.java new file mode 100644 index 0000000000..5213ae9cb1 --- /dev/null +++ b/sandbox/old/contrib/runtime-webapp/webapp-api/src/test/java/org/apache/tuscany/runtime/webapp/TuscanyServletTestCase.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.runtime.webapp; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; + +import junit.framework.TestCase; +import org.apache.tuscany.host.servlet.ServletRequestInjector; +import static org.apache.tuscany.runtime.webapp.Constants.RUNTIME_ATTRIBUTE; +import org.easymock.EasyMock; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.createNiceMock; +import static org.easymock.EasyMock.eq; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; + +/** + * Verifies {@link TuscanyServlet} properly services a request + * + * @version $Rev$ $Date$ + */ +public class TuscanyServletTestCase extends TestCase { + + public void testRequestInjection() throws Exception { + ServletRequest req = createNiceMock(ServletRequest.class); + ServletResponse resp = createNiceMock(ServletResponse.class); + ServletRequestInjector injector = createMock(ServletRequestInjector.class); + injector.service(eq(req), eq(resp)); + EasyMock.replay(injector); + WebappRuntime runtime = createMock(WebappRuntime.class); + expect(runtime.getRequestInjector()).andReturn(injector); + replay(runtime); + ServletContext context = createNiceMock(ServletContext.class); + EasyMock.expect(context.getAttribute(RUNTIME_ATTRIBUTE)).andReturn(runtime); + EasyMock.replay(context); + TuscanyServlet servlet = new TuscanyServlet(); + ServletConfig config = createMock(ServletConfig.class); + expect(config.getServletContext()).andReturn(context); + replay(config); + servlet.init(config); + servlet.service(req, resp); + verify(context); + verify(injector); + } + + public void testRuntimeNotConfigured() throws Exception { + ServletContext context = createNiceMock(ServletContext.class); + TuscanyServlet servlet = new TuscanyServlet(); + ServletConfig config = createMock(ServletConfig.class); + expect(config.getServletContext()).andReturn(context); + replay(config); + try { + servlet.init(config); + fail(); + } catch (ServletException e) { + //expected + } + } +} diff --git a/sandbox/old/contrib/runtime-webapp/webapp-api/src/test/java/org/apache/tuscany/runtime/webapp/TuscanySessionListenerTestCase.java b/sandbox/old/contrib/runtime-webapp/webapp-api/src/test/java/org/apache/tuscany/runtime/webapp/TuscanySessionListenerTestCase.java new file mode 100644 index 0000000000..cdce50cc2b --- /dev/null +++ b/sandbox/old/contrib/runtime-webapp/webapp-api/src/test/java/org/apache/tuscany/runtime/webapp/TuscanySessionListenerTestCase.java @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.runtime.webapp; + +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.http.HttpSession; +import javax.servlet.http.HttpSessionEvent; + +import junit.framework.TestCase; +import static org.apache.tuscany.runtime.webapp.Constants.RUNTIME_ATTRIBUTE; +import org.easymock.EasyMock; + +/** + * Verifies {@link org.apache.tuscany.runtime.webapp.TuscanySessionListener} notifies the runtime of session events + * + * @version $Rev$ $Date$ + */ +public class TuscanySessionListenerTestCase extends TestCase { + + public void testSessionPropagated() throws Exception { + WebappRuntime runtime = EasyMock.createNiceMock(WebappRuntime.class); + runtime.sessionCreated(EasyMock.isA(HttpSessionEvent.class)); + runtime.sessionDestroyed(EasyMock.isA(HttpSessionEvent.class)); + EasyMock.replay(runtime); + ServletContext context = EasyMock.createNiceMock(ServletContext.class); + EasyMock.expect(context.getAttribute(RUNTIME_ATTRIBUTE)).andReturn(runtime); + EasyMock.replay(context); + HttpSession session = EasyMock.createNiceMock(HttpSession.class); + EasyMock.expect(session.getServletContext()).andReturn(context); + EasyMock.replay(session); + HttpSessionEvent event = new HttpSessionEvent(session); + TuscanySessionListener listener = new TuscanySessionListener(); + listener.sessionCreated(event); + listener.sessionDestroyed(event); + EasyMock.verify(context); + EasyMock.verify(runtime); + } + + /** + * Verifies an error is logged when no runtime is configured + * + * @throws Exception + */ + public void testRuntimeNotConfigured() throws Exception { + ServletContext context = EasyMock.createNiceMock(ServletContext.class); + context.log(EasyMock.isA(String.class), EasyMock.isA(ServletException.class)); + EasyMock.replay(context); + TuscanySessionListener listener = new TuscanySessionListener(); + HttpSession session = EasyMock.createNiceMock(HttpSession.class); + EasyMock.expect(session.getServletContext()).andReturn(context); + EasyMock.replay(session); + HttpSessionEvent event = new HttpSessionEvent(session); + listener.sessionCreated(event); + EasyMock.verify(context); + } + + public void testSessionDestroyedBeforeCreated() throws Exception { + TuscanySessionListener listener = new TuscanySessionListener(); + listener.sessionDestroyed(null); + } +} diff --git a/sandbox/old/contrib/runtime-webapp/webapp-api/src/test/java/org/apache/tuscany/runtime/webapp/WebappUtilTestCase.java b/sandbox/old/contrib/runtime-webapp/webapp-api/src/test/java/org/apache/tuscany/runtime/webapp/WebappUtilTestCase.java new file mode 100644 index 0000000000..f865a4e740 --- /dev/null +++ b/sandbox/old/contrib/runtime-webapp/webapp-api/src/test/java/org/apache/tuscany/runtime/webapp/WebappUtilTestCase.java @@ -0,0 +1,131 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.runtime.webapp; + +import java.net.MalformedURLException; +import java.net.URL; +import javax.servlet.ServletContext; + +import junit.framework.TestCase; +import static org.easymock.classextension.EasyMock.*; + +/** + * @version $Rev$ $Date$ + */ +public class WebappUtilTestCase extends TestCase { + private ServletContext context; + private WebappUtilImpl listener; + private ClassLoader cl; + private URL systemUrl; + + + public void testGetInitParameterWhenSpecified() { + String name = "name"; + String value = "default"; + expect(context.getInitParameter(name)).andReturn(value); + replay(context); + + assertEquals(value, listener.getInitParameter(name, "default")); + verify(context); + } + + public void testGetInitParameterUsingDefault() { + String name = "name"; + String value = "default"; + expect(context.getInitParameter(name)).andReturn(null); + replay(context); + + assertEquals(value, listener.getInitParameter(name, value)); + verify(context); + } + + public void testGetInitParameterWithZeroLength() { + String name = "name"; + String value = "default"; + expect(context.getInitParameter(name)).andReturn(""); + replay(context); + + assertEquals(value, listener.getInitParameter(name, value)); + verify(context); + } + + public void testGetScdlFromWebapp() throws MalformedURLException { + String path = "/WEB-INF/test"; + expect(context.getResource(path)).andReturn(systemUrl); + replay(context); + replay(cl); + assertSame(systemUrl, listener.getScdlURL(path, cl)); + verify(context); + verify(cl); + } + + public void testGetScdlFromWebappMissing() throws MalformedURLException { + String path = "/WEB-INF/test"; + expect(context.getResource(path)).andReturn(null); + replay(context); + expect(cl.getResource(path)).andReturn(null); + replay(cl); + assertNull(listener.getScdlURL(path, cl)); + verify(context); + verify(cl); + } + + public void testGetScdlFromWebappMalformed() throws MalformedURLException { + String path = "/WEB-INF/test"; + expect(context.getResource(path)).andThrow(new MalformedURLException()); + replay(context); + replay(cl); + try { + listener.getScdlURL(path, cl); + fail(); + } catch (MalformedURLException e) { + // OK + } + verify(context); + verify(cl); + } + + public void testGetScdlFromClasspath() throws MalformedURLException { + String path = "META-INF/test"; + replay(context); + expect(cl.getResource(path)).andReturn(systemUrl); + replay(cl); + assertSame(systemUrl, listener.getScdlURL(path, cl)); + verify(context); + verify(cl); + } + + public void testGetScdlFromClasspathMissing() throws MalformedURLException { + String path = "META-INF/test"; + replay(context); + expect(cl.getResource(path)).andReturn(null); + replay(cl); + assertNull(listener.getScdlURL(path, cl)); + verify(context); + verify(cl); + } + + protected void setUp() throws Exception { + super.setUp(); + context = createMock(ServletContext.class); + listener = new WebappUtilImpl(context); + cl = createMock(ClassLoader.class); + systemUrl = new URL("file:/system.scdl"); + } +} -- cgit v1.2.3