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 | 104 ++++++++++++++++ .../runtime/webapp/TuscanyFilterTestCase.java | 118 +++++++++++++++++++ .../runtime/webapp/TuscanyServletTestCase.java | 80 +++++++++++++ .../webapp/TuscanySessionListenerTestCase.java | 73 ++++++++++++ .../tuscany/runtime/webapp/WebappUtilTestCase.java | 131 +++++++++++++++++++++ 5 files changed, 506 insertions(+) create mode 100644 branches/sca-java-M2/sca/runtime/webapp/src/test/java/org/apache/tuscany/runtime/webapp/TuscanyContextListenerTestCase.java create mode 100644 branches/sca-java-M2/sca/runtime/webapp/src/test/java/org/apache/tuscany/runtime/webapp/TuscanyFilterTestCase.java create mode 100644 branches/sca-java-M2/sca/runtime/webapp/src/test/java/org/apache/tuscany/runtime/webapp/TuscanyServletTestCase.java create mode 100644 branches/sca-java-M2/sca/runtime/webapp/src/test/java/org/apache/tuscany/runtime/webapp/TuscanySessionListenerTestCase.java create mode 100644 branches/sca-java-M2/sca/runtime/webapp/src/test/java/org/apache/tuscany/runtime/webapp/WebappUtilTestCase.java (limited to 'branches/sca-java-M2/sca/runtime/webapp/src/test') diff --git a/branches/sca-java-M2/sca/runtime/webapp/src/test/java/org/apache/tuscany/runtime/webapp/TuscanyContextListenerTestCase.java b/branches/sca-java-M2/sca/runtime/webapp/src/test/java/org/apache/tuscany/runtime/webapp/TuscanyContextListenerTestCase.java new file mode 100644 index 0000000000..b4f8f4c3db --- /dev/null +++ b/branches/sca-java-M2/sca/runtime/webapp/src/test/java/org/apache/tuscany/runtime/webapp/TuscanyContextListenerTestCase.java @@ -0,0 +1,104 @@ +/* + * 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.URL; +import java.lang.reflect.Method; +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; + +import junit.framework.TestCase; +import static org.easymock.classextension.EasyMock.*; + +import org.apache.tuscany.host.MonitorFactory; + +/** + * @version $Rev$ $Date$ + */ +public class TuscanyContextListenerTestCase extends TestCase { + private ServletContext context; + private TuscanyContextListener listener; + private ClassLoader cl; + private ClassLoader bootClassLoader; + private URL systemUrl; + private URL applicationUrl; + private Method getUtilsMethod; + private MonitorFactory monitorFactory; + private WebappUtil utils; + + 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.online", "true")).andReturn("true"); + expect(utils.getRuntime(bootClassLoader)).andReturn(runtime); + expect(utils.getSystemScdl(bootClassLoader)).andReturn(systemUrl); + expect(utils.getApplicationScdl(cl)).andReturn(applicationUrl); + expect(utils.getApplicationName()).andReturn("application"); + replay(utils); + + expect(context.getResource("/WEB-INF/tuscany/")).andReturn(null); + 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); + expect(runtime.createDefaultMonitorFactory()).andReturn(monitorFactory); + runtime.setMonitorFactory(monitorFactory); + runtime.setRuntimeInfo(isA(WebappRuntimeInfo.class)); + runtime.setHostClassLoader(cl); + runtime.setSystemScdl(systemUrl); + runtime.setApplicationName("application"); + runtime.setApplicationScdl(applicationUrl); + runtime.initialize(); + replay(runtime); + + ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); + try { + Thread.currentThread().setContextClassLoader(cl); + listener.contextInitialized(event); + } finally { + Thread.currentThread().setContextClassLoader(oldCl); + } + verify(event); + verify(context); + verify(listener); + verify(cl); + verify(bootClassLoader); + verify(runtime); + } + + protected void setUp() throws Exception { + super.setUp(); + 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"); + applicationUrl = new URL("file:/application.scdl"); + monitorFactory = createMock(MonitorFactory.class); + } +} diff --git a/branches/sca-java-M2/sca/runtime/webapp/src/test/java/org/apache/tuscany/runtime/webapp/TuscanyFilterTestCase.java b/branches/sca-java-M2/sca/runtime/webapp/src/test/java/org/apache/tuscany/runtime/webapp/TuscanyFilterTestCase.java new file mode 100644 index 0000000000..b649c22178 --- /dev/null +++ b/branches/sca-java-M2/sca/runtime/webapp/src/test/java/org/apache/tuscany/runtime/webapp/TuscanyFilterTestCase.java @@ -0,0 +1,118 @@ +/* + * 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.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; + +import org.osoa.sca.SCA; + +import junit.framework.TestCase; +import static org.apache.tuscany.runtime.webapp.Constants.RUNTIME_ATTRIBUTE; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.createNiceMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.isA; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import org.easymock.classextension.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class TuscanyFilterTestCase extends TestCase { + + public void testStartStopFilter() throws Exception { + SCA sca = EasyMock.createNiceMock(SCA.class); + sca.start(); + sca.stop(); + EasyMock.replay(sca); + WebappRuntime runtime = createMock(WebappRuntime.class); + expect(runtime.getContext()).andReturn(sca); + runtime.startRequest(); + runtime.stopRequest(); + replay(runtime); + ServletContext context = createNiceMock(ServletContext.class); + EasyMock.expect(context.getAttribute(RUNTIME_ATTRIBUTE)).andReturn(runtime); + replay(context); + TuscanyFilter filter = new TuscanyFilter(); + FilterConfig config = createMock(FilterConfig.class); + expect(config.getServletContext()).andReturn(context); + replay(config); + filter.init(config); + ServletRequest req = createNiceMock(ServletRequest.class); + ServletResponse resp = createNiceMock(ServletResponse.class); + FilterChain chain = createNiceMock(FilterChain.class); + filter.doFilter(req, resp, chain); + verify(runtime); + EasyMock.verify(sca); + } + + public void testExceptionCleanupFilter() throws Exception { + SCA sca = EasyMock.createNiceMock(SCA.class); + sca.start(); + sca.stop(); + EasyMock.replay(sca); + WebappRuntime runtime = createMock(WebappRuntime.class); + runtime.startRequest(); + runtime.stopRequest(); + expect(runtime.getContext()).andReturn(sca); + replay(runtime); + ServletContext context = createNiceMock(ServletContext.class); + EasyMock.expect(context.getAttribute(RUNTIME_ATTRIBUTE)).andReturn(runtime); + replay(context); + TuscanyFilter filter = new TuscanyFilter(); + FilterConfig config = createMock(FilterConfig.class); + expect(config.getServletContext()).andReturn(context); + replay(config); + filter.init(config); + ServletRequest req = createNiceMock(ServletRequest.class); + ServletResponse resp = createNiceMock(ServletResponse.class); + FilterChain chain = createNiceMock(FilterChain.class); + chain.doFilter(isA(ServletRequest.class), isA(ServletResponse.class)); + EasyMock.expectLastCall().andThrow(new TestException()); + filter.doFilter(req, resp, chain); + verify(runtime); + EasyMock.verify(sca); + } + + public void testRuntimeNotConfigured() throws Exception { + ServletContext context = createNiceMock(ServletContext.class); + TuscanyFilter filter = new TuscanyFilter(); + FilterConfig config = createMock(FilterConfig.class); + expect(config.getServletContext()).andReturn(context); + replay(config); + try { + filter.init(config); + fail(); + } catch (ServletException e) { + //expected + } + } + + private class TestException extends RuntimeException { + + } + + +} diff --git a/branches/sca-java-M2/sca/runtime/webapp/src/test/java/org/apache/tuscany/runtime/webapp/TuscanyServletTestCase.java b/branches/sca-java-M2/sca/runtime/webapp/src/test/java/org/apache/tuscany/runtime/webapp/TuscanyServletTestCase.java new file mode 100644 index 0000000000..5213ae9cb1 --- /dev/null +++ b/branches/sca-java-M2/sca/runtime/webapp/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/branches/sca-java-M2/sca/runtime/webapp/src/test/java/org/apache/tuscany/runtime/webapp/TuscanySessionListenerTestCase.java b/branches/sca-java-M2/sca/runtime/webapp/src/test/java/org/apache/tuscany/runtime/webapp/TuscanySessionListenerTestCase.java new file mode 100644 index 0000000000..3bac14fd8a --- /dev/null +++ b/branches/sca-java-M2/sca/runtime/webapp/src/test/java/org/apache/tuscany/runtime/webapp/TuscanySessionListenerTestCase.java @@ -0,0 +1,73 @@ +/* + * 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); + } +} diff --git a/branches/sca-java-M2/sca/runtime/webapp/src/test/java/org/apache/tuscany/runtime/webapp/WebappUtilTestCase.java b/branches/sca-java-M2/sca/runtime/webapp/src/test/java/org/apache/tuscany/runtime/webapp/WebappUtilTestCase.java new file mode 100644 index 0000000000..f865a4e740 --- /dev/null +++ b/branches/sca-java-M2/sca/runtime/webapp/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