- Revision
- 880
- Author
- paul
- Date
- 2009-01-15 13:25:34 -0600 (Thu, 15 Jan 2009)
Log Message
more of old waffle killed
Modified Paths
- sandbox/v2experiment/waffle-core/src/main/java/org/codehaus/waffle/context/ContextContainerFactory.java
- sandbox/v2experiment/waffle-core/src/main/java/org/codehaus/waffle/servlet/WaffleServlet.java
- sandbox/v2experiment/waffle-core/src/main/java/org/codehaus/waffle/servlet/WaffleVelocityServlet.java
- sandbox/v2experiment/waffle-core/src/test/java/org/codehaus/waffle/context/pico/PicoContextContainerFactoryTest.java
- sandbox/v2experiment/waffle-core/src/test/java/org/codehaus/waffle/servlet/WaffleServletTest.java
Removed Paths
- sandbox/v2experiment/waffle-core/src/main/java/org/codehaus/waffle/context/WaffleContextListener.java
- sandbox/v2experiment/waffle-core/src/main/java/org/codehaus/waffle/servlet/ServletContextHelper.java
- sandbox/v2experiment/waffle-core/src/test/java/org/codehaus/waffle/context/pico/PicoWaffleContextListenerTest.java
- sandbox/v2experiment/waffle-core/src/test/java/org/codehaus/waffle/servlet/ServletContextHelperTest.java
Diff
Modified: sandbox/v2experiment/waffle-core/src/main/java/org/codehaus/waffle/context/ContextContainerFactory.java (879 => 880)
--- sandbox/v2experiment/waffle-core/src/main/java/org/codehaus/waffle/context/ContextContainerFactory.java 2009-01-15 18:58:22 UTC (rev 879) +++ sandbox/v2experiment/waffle-core/src/main/java/org/codehaus/waffle/context/ContextContainerFactory.java 2009-01-15 19:25:34 UTC (rev 880) @@ -10,7 +10,6 @@ import org.picocontainer.DefaultPicoContainer; import org.picocontainer.monitors.NullComponentMonitor; import org.codehaus.waffle.context.pico.WaffleLifecycleStrategy; -import org.codehaus.waffle.context.pico.HttpSessionComponentAdapter; import org.codehaus.waffle.monitor.RegistrarMonitor; import org.codehaus.waffle.monitor.ContextMonitor; import org.codehaus.waffle.registrar.pico.ParameterResolver; @@ -61,40 +60,6 @@ return new ContextContainer(buildMutablePicoContainer(null), messageResources); } - public MutablePicoContainer buildSessionLevelContainer() { - MutablePicoContainer delegate = buildMutablePicoContainer(applicationContextContainer); - delegate.addComponent(new HttpSessionComponentAdapter()); - - ContextContainer sessionContextContainer = new ContextContainer(delegate, messageResources); - registrarAssistant.executeDelegatingRegistrar(createRegistrar(sessionContextContainer, picoLifecycleStrategy), ContextLevel.SESSION); - getContextMonitor().sessionContextContainerCreated(applicationContextContainer); - return sessionContextContainer; - } - - public MutablePicoContainer buildRequestLevelContainer(HttpServletRequest request) { - try { - HttpSession session = request.getSession(); - ContextContainer sessionContextContainer = (ContextContainer) session.getAttribute(Constants.SESSION_CONTAINER_KEY); - if (sessionContextContainer == null) { - sessionContextContainer = (ContextContainer) buildSessionLevelContainer(); - session.setAttribute(Constants.SESSION_CONTAINER_KEY, sessionContextContainer); - sessionContextContainer.start(); - } - MutablePicoContainer delegate = sessionContextContainer.getDelegate(); - - ContextContainer requestContextContainer = new ContextContainer(buildMutablePicoContainer(delegate), messageResources); - registrarAssistant.executeDelegatingRegistrar(createRegistrar(requestContextContainer, picoLifecycleStrategy), ContextLevel.REQUEST); - getContextMonitor().requestContextContainerCreated(sessionContextContainer); - return requestContextContainer; - } finally { -//TODO: setting the locale from the request (which will by default return the server default locale) -// does not seem necessary given a default locale is already configured out-of-the-box -// but more seriously it overrides any configuration set via the MessageResourcesConfiguration -// hence impeding any real configurability. Need a clearer usecase for enabling this (MT) -// messageResources.useLocale(request.getLocale()); - } - } - public Registrar createRegistrar(MutablePicoContainer contextContainer, LifecycleStrategy lifecycleStrategy) { Registrar registrar = new PicoRegistrar(contextContainer, parameterResolver, picoLifecycleStrategy, registrarMonitor, picoComponentMonitor, messageResources);
Deleted: sandbox/v2experiment/waffle-core/src/main/java/org/codehaus/waffle/context/WaffleContextListener.java (879 => 880)
--- sandbox/v2experiment/waffle-core/src/main/java/org/codehaus/waffle/context/WaffleContextListener.java 2009-01-15 18:58:22 UTC (rev 879) +++ sandbox/v2experiment/waffle-core/src/main/java/org/codehaus/waffle/context/WaffleContextListener.java 2009-01-15 19:25:34 UTC (rev 880) @@ -1,80 +0,0 @@ -/* - * Copyright (c) terms as published in http://waffle.codehaus.org/license.html - */ -package org.codehaus.waffle.context; - -import org.codehaus.waffle.ComponentRegistry; -import org.codehaus.waffle.Constants; -import org.picocontainer.MutablePicoContainer; - -import javax.servlet.ServletContext; -import javax.servlet.ServletContextEvent; -import javax.servlet.ServletContextListener; -import javax.servlet.http.HttpSession; -import javax.servlet.http.HttpSessionEvent; -import javax.servlet.http.HttpSessionListener; - -/** - * Abstract context and session listener that uses a Waffle ComponentRegistry to retrieve the ContextContainerFactory - * used to manage the components registered at each webapp scope. - * - * @author Mike Ward - * @author Mauro Talevi - */ -public class WaffleContextListener implements ServletContextListener, HttpSessionListener { - private ContextContainerFactory contextContainerFactory; - - /** - * As the servlet context is being initialized Waffle needs to instantiate a component registry and add it to the - * context so that it will be available through out. Next the Waffle context container factory is retrieved from the - * registry and initialized with the servlet context. - */ - public void contextInitialized(ServletContextEvent servletContextEvent) { - ServletContext servletContext = servletContextEvent.getServletContext(); - // build component registry instance and add it to the ServletContext - ComponentRegistry componentRegistry = buildComponentRegistry(servletContext); - servletContext.setAttribute(ComponentRegistry.class.getName(), componentRegistry); - contextContainerFactory = componentRegistry.getContextContainerFactory(); - contextContainerFactory.initialize(servletContext); - } - - /** - * As the servlet context is being destroyed, the context container factory is also destroyed. - */ - public void contextDestroyed(ServletContextEvent servletContextEvent) { - contextContainerFactory.destroy(); - } - - /** - * As the session is created a session-level context container is also created and started. - */ - public void sessionCreated(HttpSessionEvent httpSessionEvent) { - HttpSession session = httpSessionEvent.getSession(); - MutablePicoContainer sessionContextContainer = contextContainerFactory.buildSessionLevelContainer(); - sessionContextContainer.addComponent(session); - session.setAttribute(Constants.SESSION_CONTAINER_KEY, sessionContextContainer); - sessionContextContainer.start(); - } - - /** - * As the session is created the session-level context container is also stopped and disposed. - */ - public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { - HttpSession session = httpSessionEvent.getSession(); - MutablePicoContainer sessionContextContainer = (MutablePicoContainer) session - .getAttribute(Constants.SESSION_CONTAINER_KEY); - sessionContextContainer.stop(); - sessionContextContainer.dispose(); - } - - /** - * Concrete subclasses to provide a Waffle ComponentRegistry instance - * - * @param servletContext - * @return A ComponentRegistry - */ - protected ComponentRegistry buildComponentRegistry(ServletContext servletContext) { - return new ComponentRegistry(servletContext); - } - -}
Deleted: sandbox/v2experiment/waffle-core/src/main/java/org/codehaus/waffle/servlet/ServletContextHelper.java (879 => 880)
--- sandbox/v2experiment/waffle-core/src/main/java/org/codehaus/waffle/servlet/ServletContextHelper.java 2009-01-15 18:58:22 UTC (rev 879) +++ sandbox/v2experiment/waffle-core/src/main/java/org/codehaus/waffle/servlet/ServletContextHelper.java 2009-01-15 19:25:34 UTC (rev 880) @@ -1,43 +0,0 @@ -/* - * Copyright (c) terms as published in http://waffle.codehaus.org/license.html - */ -package org.codehaus.waffle.servlet; - -import org.codehaus.waffle.ComponentRegistry; -import org.codehaus.waffle.WaffleException; -import org.codehaus.waffle.context.WaffleContextListener; - -import javax.servlet.ServletContext; -import java.text.MessageFormat; - -/** - * @author Michael Ward - */ -public class ServletContextHelper { - - private ServletContextHelper() { - // should not be instantiated - } - - /** - * Allows access to Waffle core components - * - * @param servletContext - * @return the ComponentRegistry for the running application - */ - public static ComponentRegistry getComponentRegistry(ServletContext servletContext) { - ComponentRegistry componentRegistry = (ComponentRegistry) servletContext - .getAttribute(ComponentRegistry.class.getName()); - - if (componentRegistry == null) { - String error = MessageFormat.format( - "Unable to locate a {0} from the ServletContext, make sure that {1} is registered as a listener in the web.xml", - ComponentRegistry.class.getName(), - WaffleContextListener.class.getName()); - throw new WaffleException(error); - } - - return componentRegistry; - } - -}
Modified: sandbox/v2experiment/waffle-core/src/main/java/org/codehaus/waffle/servlet/WaffleServlet.java (879 => 880)
--- sandbox/v2experiment/waffle-core/src/main/java/org/codehaus/waffle/servlet/WaffleServlet.java 2009-01-15 18:58:22 UTC (rev 879) +++ sandbox/v2experiment/waffle-core/src/main/java/org/codehaus/waffle/servlet/WaffleServlet.java 2009-01-15 19:25:34 UTC (rev 880) @@ -131,17 +131,16 @@ public void init() throws ServletException { if (!componentsRetrieved) { - // Retrieve instance components from the ComponentRegistry - ComponentRegistry registry = getComponentRegistry(); - actionMethodExecutor = registry.getActionMethodExecutor(); - actionMethodResponseHandler = registry.getActionMethodResponseHandler(); - controllerDefinitionFactory = registry.getControllerDefinitionFactory(); - controllerDataBinder = registry.getControllerDataBinder(); - messageResources = registry.getMessageResources(); - viewDataBinder = registry.getViewDataBinder(); - viewResolver = registry.getViewResolver(); - validator = registry.getValidator(); - servletMonitor = registry.getServletMonitor(); + MutablePicoContainer container = currentAppContainer.get(); + actionMethodExecutor = container.getComponent(ActionMethodExecutor.class); + actionMethodResponseHandler = container.getComponent(ActionMethodResponseHandler.class); + controllerDefinitionFactory = container.getComponent(ControllerDefinitionFactory.class); + controllerDataBinder = container.getComponent(ControllerDataBinder.class); + messageResources = container.getComponent(MessageResources.class); + viewDataBinder = container.getComponent(ViewDataBinder.class); + viewResolver = container.getComponent(ViewResolver.class); + validator = container.getComponent(Validator.class); + servletMonitor = container.getComponent(ServletMonitor.class); } configureViewProperties(); @@ -171,10 +170,6 @@ return value; } - private ComponentRegistry getComponentRegistry() { - return ServletContextHelper.getComponentRegistry(getServletContext()); - } - /** * Responsible for servicing the requests from the users. *
Modified: sandbox/v2experiment/waffle-core/src/main/java/org/codehaus/waffle/servlet/WaffleVelocityServlet.java (879 => 880)
--- sandbox/v2experiment/waffle-core/src/main/java/org/codehaus/waffle/servlet/WaffleVelocityServlet.java 2009-01-15 18:58:22 UTC (rev 879) +++ sandbox/v2experiment/waffle-core/src/main/java/org/codehaus/waffle/servlet/WaffleVelocityServlet.java 2009-01-15 19:25:34 UTC (rev 880) @@ -13,7 +13,6 @@ import org.apache.velocity.context.Context; import org.apache.velocity.tools.view.servlet.VelocityViewServlet; import org.picocontainer.MutablePicoContainer; -import org.picocontainer.web.PicoServletContainerFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -53,8 +52,7 @@ } public WaffleVelocityServlet() { - ComponentRegistry componentRegistry = ServletContextHelper.getComponentRegistry(getServletContext()); - controllerDefinitionFactory = componentRegistry.getControllerDefinitionFactory(); + controllerDefinitionFactory = currentAppContainer.get().getComponent(ControllerDefinitionFactory.class); } @Override
Modified: sandbox/v2experiment/waffle-core/src/test/java/org/codehaus/waffle/context/pico/PicoContextContainerFactoryTest.java (879 => 880)
--- sandbox/v2experiment/waffle-core/src/test/java/org/codehaus/waffle/context/pico/PicoContextContainerFactoryTest.java 2009-01-15 18:58:22 UTC (rev 879) +++ sandbox/v2experiment/waffle-core/src/test/java/org/codehaus/waffle/context/pico/PicoContextContainerFactoryTest.java 2009-01-15 19:25:34 UTC (rev 880) @@ -61,76 +61,6 @@ private final MessageResources messageResources = new DefaultMessageResources(); @Test - public void canBuildEachContextLevelContainer() { - final ContextContainerFactory contextContainerFactory - = new ContextContainerFactory(messageResources, new SilentMonitor(), new SilentMonitor(), null); - - // Mock ServletContext - final ServletContext servletContext = mockery.mock(ServletContext.class); - mockery.checking(new Expectations() { - { - one(servletContext).setAttribute(with(same(ContextContainerFactory.class.getName())), with(same(contextContainerFactory))); - one(servletContext).getInitParameter(with(same(Registrar.class.getName()))); - will(returnValue(CustomRegistrar.class.getName())); - } - }); - contextContainerFactory.initialize(servletContext); - - // Application - MutablePicoContainer applicationContainer = contextContainerFactory.getApplicationContextContainer(); - Assert.assertNotNull(applicationContainer.getComponent(ServletContext.class)); - assertSame(messageResources, applicationContainer.getComponent(MessageResources.class)); - ApplicationLevelComponent applicationLevelComponent = - (ApplicationLevelComponent) applicationContainer.getComponent("application"); - assertNotNull(applicationLevelComponent); - assertTrue(applicationLevelComponent.isStarted()); - assertFalse(applicationLevelComponent.isStopped()); - - // Session - final MutablePicoContainer sessionContainer = contextContainerFactory.buildSessionLevelContainer(); - SessionLevelComponent sessionLevelComponent = (SessionLevelComponent) sessionContainer.getComponent("session"); - assertNotNull(sessionLevelComponent); - assertFalse("Start is the responsibility of HttpSessionListener", sessionLevelComponent.isStarted()); - assertFalse(sessionLevelComponent.isStopped()); - assertEquals(applicationLevelComponent, sessionLevelComponent.getApplicationLevelComponent()); - - // Mock HttpSession - final HttpSession httpSession = mockery.mock(HttpSession.class); - mockery.checking(new Expectations() { - { - one(httpSession).getAttribute(Constants.SESSION_CONTAINER_KEY); - will(returnValue(sessionContainer)); - } - }); - - // Mock HttpServletRequest - final HttpServletRequest request = mockery.mock(HttpServletRequest.class); - mockery.checking(new Expectations() { - { - one(request).getSession(); - will(returnValue(httpSession)); - allowing(request).getLocale(); // allow, rather than mandate - will(returnValue(Locale.US)); - } - }); - - // Request - MutablePicoContainer requestContainer = contextContainerFactory.buildRequestLevelContainer(request); - RequestLevelComponent requestLevelComponent = (RequestLevelComponent) requestContainer.getComponent("request"); - assertNotNull(requestLevelComponent); - assertFalse("Start is the responsibility of ServletRequestListener", requestLevelComponent.isStarted()); - assertFalse(requestLevelComponent.isStopped()); - assertEquals(applicationLevelComponent, requestLevelComponent.getApplicationLevelComponent()); - assertEquals(sessionLevelComponent, requestLevelComponent.getSessionLevelComponent()); - - // Destroy Application Context - contextContainerFactory.destroy(); - assertTrue(applicationLevelComponent.isStopped()); - assertFalse("session component should NOT be stopped", sessionLevelComponent.isStopped()); - assertFalse("request component should NOT be stopped", requestLevelComponent.isStopped()); - } - - @Test public void canInitializeContext() { final ContextContainerFactory contextContainerFactory = new ContextContainerFactory(messageResources, new SilentMonitor(), new SilentMonitor(), null); @@ -248,92 +178,8 @@ assertTrue(startable.stopped); } - @Test - public void canBuildSessionLevelContainerWithLifecycle() { - final ContextContainerFactory contextContainerFactory - = new ContextContainerFactory(messageResources, new SilentMonitor(), new SilentMonitor(), null); - // Mock ServletContext - final ServletContext servletContext = mockery.mock(ServletContext.class); - mockery.checking(new Expectations() { - { - one(servletContext).setAttribute(with(same(ContextContainerFactory.class.getName())), with(same(contextContainerFactory))); - one(servletContext).getInitParameter(with(same(Registrar.class.getName()))); - will(returnValue(CustomRegistrar.class.getName())); - } - }); - contextContainerFactory.initialize(servletContext); - MutablePicoContainer container = contextContainerFactory.buildSessionLevelContainer(); - - StubStartable startable = new StubStartable(); - container.addComponent(startable); - - container.start(); - - assertTrue(startable.started); - assertFalse(startable.stopped); - - container.stop(); - assertTrue(startable.stopped); - } - - @Test - public void canBuildRequestLevelContainerWithLifecycle() { - final ContextContainerFactory contextContainerFactory - = new ContextContainerFactory(messageResources, new SilentMonitor(), new SilentMonitor(), null); - - // Mock ServletContext - final ServletContext servletContext = mockery.mock(ServletContext.class); - mockery.checking(new Expectations() { - { - one(servletContext).setAttribute(with(same(ContextContainerFactory.class.getName())), with(same(contextContainerFactory))); - one(servletContext).getInitParameter(with(same(Registrar.class.getName()))); - will(returnValue(CustomRegistrar.class.getName())); - } - }); - contextContainerFactory.initialize(servletContext); - - final MutablePicoContainer sessionContextContainer = contextContainerFactory.buildSessionLevelContainer(); - - - // Mock HttpSession - final HttpSession httpSession = mockery.mock(HttpSession.class); - mockery.checking(new Expectations() { - { - one(httpSession).getAttribute(Constants.SESSION_CONTAINER_KEY); - will(returnValue(sessionContextContainer)); - } - }); - - // Mock HttpServletRequest - final HttpServletRequest request = mockery.mock(HttpServletRequest.class); - mockery.checking(new Expectations() { - { - one(request).getSession(); - will(returnValue(httpSession)); - allowing(request).getLocale(); // allow, rather than mandate - will(returnValue(Locale.US)); - } - }); - - MutablePicoContainer container = contextContainerFactory.buildRequestLevelContainer(request); - - StubStartable startable = new StubStartable(); - container.addComponent(startable); - - // Remove ErrorsContext and MessagesContext prior to starting... (assert they existed) - assertNotNull(container.removeComponent(ErrorsContext.class)); - assertNotNull(container.removeComponent(MessagesContext.class)); - container.start(); - - assertTrue(startable.started); - assertFalse(startable.stopped); - - container.stop(); - assertTrue(startable.stopped); - } - /** * WAFFLE-52 : ensure PicoRegistrar is not constructed without passing ParameterResolver */
Deleted: sandbox/v2experiment/waffle-core/src/test/java/org/codehaus/waffle/context/pico/PicoWaffleContextListenerTest.java (879 => 880)
--- sandbox/v2experiment/waffle-core/src/test/java/org/codehaus/waffle/context/pico/PicoWaffleContextListenerTest.java 2009-01-15 18:58:22 UTC (rev 879) +++ sandbox/v2experiment/waffle-core/src/test/java/org/codehaus/waffle/context/pico/PicoWaffleContextListenerTest.java 2009-01-15 19:25:34 UTC (rev 880) @@ -1,148 +0,0 @@ -package org.codehaus.waffle.context.pico; - -import java.lang.reflect.Field; - -import javax.servlet.ServletContext; -import javax.servlet.ServletContextEvent; -import javax.servlet.http.HttpSession; -import javax.servlet.http.HttpSessionEvent; - -import org.codehaus.waffle.ComponentRegistry; -import org.codehaus.waffle.Constants; -import org.codehaus.waffle.WaffleException; -import org.codehaus.waffle.testmodel.StubParameterResolver; -import org.codehaus.waffle.registrar.pico.ParameterResolver; -import org.codehaus.waffle.monitor.RegistrarMonitor; -import org.codehaus.waffle.monitor.ContextMonitor; -import org.codehaus.waffle.i18n.MessageResources; -import org.codehaus.waffle.context.ContextContainer; -import org.codehaus.waffle.context.ContextContainerFactory; -import org.codehaus.waffle.context.WaffleContextListener; -import org.jmock.Expectations; -import org.jmock.Mockery; -import org.jmock.integration.junit4.JMock; -import org.junit.Test; -import static org.junit.Assert.assertEquals; -import org.junit.runner.RunWith; -import org.picocontainer.MutablePicoContainer; - -/** - * - * @author Michael Ward - * @author Mauro Talevi - */ -...@runwith(JMock.class) -public class PicoWaffleContextListenerTest { - - private Mockery mockery = new Mockery(); - - @Test - public void canInvokeServletContextListenerMethods() { - - final StringBuilder sb = new StringBuilder(); - - final ContextContainerFactory contextContainerFactory = new ContextContainerFactory(mockery.mock(MessageResources.class), mockery.mock(ContextMonitor.class), mockery.mock(RegistrarMonitor.class), new StubParameterResolver()) { - public void initialize(ServletContext servletContext) throws WaffleException { - sb.append("init;"); - } - - public void destroy() { - sb.append("destroy;"); - } - }; - - // Mock ServletContext - final ServletContext servletContext = mockery.mock(ServletContext.class); - - // Mock ComponentRegistry - final ComponentRegistry registry = new ComponentRegistry(servletContext) { - protected void register(Object key, Class<?> defaultClass, ServletContext servletContext) throws WaffleException { - } - - @SuppressWarnings("unchecked") - protected void registerOtherComponents(ServletContext servletContext) { - } - - public ContextContainerFactory getContextContainerFactory() { - return contextContainerFactory; - } - - }; - - WaffleContextListener waffleContextListener = new WaffleContextListener() { - protected ComponentRegistry buildComponentRegistry(ServletContext servletContext) { - return registry; - } - }; - - mockery.checking(new Expectations() { - { - String name = ComponentRegistry.class.getName(); - one(servletContext).setAttribute(with(same(name)), with(same(registry))); - } - }); - - ServletContextEvent event = new ServletContextEvent(servletContext); - - // test the init - waffleContextListener.contextInitialized(event); - - // test the destroy - waffleContextListener.contextDestroyed(event); - - assertEquals("init;destroy;", sb.toString()); - } - - @Test - public void canInvokeHttpSessionListenerMethods() throws Exception { - WaffleContextListener waffleContextListener = new WaffleContextListener(); - - // Mock ContextContainer - final MutablePicoContainer container = mockery.mock(MutablePicoContainer.class); - mockery.checking(new Expectations() { - { - one(container).start(); - one(container).addComponent(with(an(HttpSession.class))); - one(container).stop(); - one(container).dispose(); - } - }); - - final StringBuilder sb = new StringBuilder(); - - final ContextContainerFactory contextContainerFactory = new ContextContainerFactory(mockery.mock(MessageResources.class), mockery.mock(ContextMonitor.class), mockery.mock(RegistrarMonitor.class), new StubParameterResolver()) { - public MutablePicoContainer buildSessionLevelContainer() { - sb.append("bs;"); - return container; - } - }; - setContextManager(waffleContextListener, contextContainerFactory); - - // Mock HttpSession - final HttpSession httpSession = mockery.mock(HttpSession.class); - mockery.checking(new Expectations() { - { - one(httpSession).setAttribute(Constants.SESSION_CONTAINER_KEY, container); - one(httpSession).getAttribute(Constants.SESSION_CONTAINER_KEY); - will(returnValue(container)); - } - }); - - // created - waffleContextListener.sessionCreated(new HttpSessionEvent(httpSession)); - - // destroy - waffleContextListener.sessionDestroyed(new HttpSessionEvent(httpSession)); - assertEquals("bs;", sb.toString()); - } - - /** - * set the private field so we don't need to add an accessors simply for testing. - */ - private void setContextManager(WaffleContextListener listener, ContextContainerFactory containerFactory) - throws Exception { - Field field = WaffleContextListener.class.getDeclaredField("contextContainerFactory"); - field.setAccessible(true); - field.set(listener, containerFactory); - } -}
Deleted: sandbox/v2experiment/waffle-core/src/test/java/org/codehaus/waffle/servlet/ServletContextHelperTest.java (879 => 880)
--- sandbox/v2experiment/waffle-core/src/test/java/org/codehaus/waffle/servlet/ServletContextHelperTest.java 2009-01-15 18:58:22 UTC (rev 879) +++ sandbox/v2experiment/waffle-core/src/test/java/org/codehaus/waffle/servlet/ServletContextHelperTest.java 2009-01-15 19:25:34 UTC (rev 880) @@ -1,57 +0,0 @@ -package org.codehaus.waffle.servlet; - -import javax.servlet.ServletContext; - -import org.codehaus.waffle.ComponentRegistry; -import org.codehaus.waffle.WaffleException; -import org.jmock.Expectations; -import org.jmock.Mockery; -import org.jmock.integration.junit4.JMock; -import org.junit.Test; -import org.junit.runner.RunWith; - -/** - * - * @author Michael Ward - * @author Mauro Talevi - */ -...@runwith(JMock.class) -public class ServletContextHelperTest { - - private Mockery mockery = new Mockery(); - - @Test - public void canGetComponentRegistry() { - - // Mock ServletContext - final ServletContext servletContext = mockery.mock(ServletContext.class); - mockery.checking(new Expectations() { - { - one(servletContext).getInitParameterNames(); - will(returnValue(null)); - atLeast(1).of(servletContext).getInitParameter(with(any(String.class))); - will(returnValue(null)); - } - }); - mockery.checking(new Expectations() { - { - one(servletContext).getAttribute(ComponentRegistry.class.getName()); - will(returnValue(new ComponentRegistry(servletContext))); - } - }); - ServletContextHelper.getComponentRegistry(servletContext); - } - - @Test(expected = WaffleException.class) - public void cannotGetComponentRegistryIfNotRegistered() { - // Mock ServletContext - final ServletContext servletContext = mockery.mock(ServletContext.class); - mockery.checking(new Expectations() { - { - one(servletContext).getAttribute(ComponentRegistry.class.getName()); - will(returnValue(null)); - } - }); - ServletContextHelper.getComponentRegistry(servletContext); - } -}
Modified: sandbox/v2experiment/waffle-core/src/test/java/org/codehaus/waffle/servlet/WaffleServletTest.java (879 => 880)
--- sandbox/v2experiment/waffle-core/src/test/java/org/codehaus/waffle/servlet/WaffleServletTest.java 2009-01-15 18:58:22 UTC (rev 879) +++ sandbox/v2experiment/waffle-core/src/test/java/org/codehaus/waffle/servlet/WaffleServletTest.java 2009-01-15 19:25:34 UTC (rev 880) @@ -29,6 +29,8 @@ import org.codehaus.waffle.ComponentRegistry; import org.codehaus.waffle.Constants; import org.codehaus.waffle.WaffleException; +import org.codehaus.waffle.testmodel.StubActionMethodExecutor; +import org.codehaus.waffle.testmodel.StubViewResolver; import org.codehaus.waffle.action.ActionMethodExecutor; import org.codehaus.waffle.action.ActionMethodInvocationException; import org.codehaus.waffle.action.ActionMethodResponse; @@ -63,6 +65,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.picocontainer.MutablePicoContainer; +import org.picocontainer.PicoContainer; /** * @author Michael Ward @@ -75,109 +78,68 @@ @SuppressWarnings("serial") @Test public void canConfigureComponentsViaInitAttributes() throws ServletException { - // Mock ServletConfig - final ServletConfig servletConfig = mockery.mock(ServletConfig.class); - mockery.checking(new Expectations() { - { - one(servletConfig).getInitParameter(VIEW_PREFIX_KEY); - will(returnValue(null)); - one(servletConfig).getInitParameter(VIEW_SUFFIX_KEY); - will(returnValue(".jsp")); - one(servletConfig).getInitParameter(ERRORS_VIEW_KEY); - will(returnValue("errors")); - } - }); final StringBuilder sb = new StringBuilder(); final ViewResolver viewResolver = mockery.mock(ViewResolver.class); - mockery.checking(new Expectations() { - { - one(viewResolver).configureViews(with(any(Properties.class))); - } - }); - - final ServletContext servletContext = mockery.mock(ServletContext.class); - // Mock ComponentRegistry - final ComponentRegistry componentRegistry = new ComponentRegistry(servletContext) { - protected void register(Object key, Class<?> defaultClass, ServletContext servletContext) throws WaffleException { - } + final MutablePicoContainer mpc = mockery.mock(MutablePicoContainer.class); - @SuppressWarnings("unchecked") - protected void registerOtherComponents(ServletContext servletContext) { - } + final ServletMonitor servletMonitor = mockery.mock(ServletMonitor.class); - public ActionMethodExecutor getActionMethodExecutor() { - sb.append("getAME;"); - return null; + mockery.checking(new Expectations() { + { + one(mpc).getComponent(ActionMethodExecutor.class); + will(returnValue(null)); + one(mpc).getComponent(ActionMethodResponseHandler.class); + will(returnValue(null)); + one(mpc).getComponent(ServletMonitor.class); + will(returnValue(servletMonitor)); + one(mpc).getComponent(ControllerDataBinder.class); + will(returnValue(null)); + one(mpc).getComponent(ControllerDefinitionFactory.class); + will(returnValue(null)); + one(mpc).getComponent(MessageResources.class); + will(returnValue(null)); + one(mpc).getComponent(ViewDataBinder.class); + will(returnValue(null)); + one(mpc).getComponent(ViewResolver.class); + will(returnValue(new StubViewResolver())); + one(mpc).getComponent(Validator.class); + will(returnValue(null)); } + }); - public ActionMethodResponseHandler getActionMethodResponseHandler() { - sb.append("getAMRH;"); - return null; - } +// // Mock ServletContext +// mockery.checking(new Expectations() { +// { +// one(servletContext).getAttribute(ComponentRegistry.class.getName()); +// will(returnValue(componentRegistry)); +// } +// }); - public ServletMonitor getServletMonitor() { - sb.append("getSM;"); - return new SilentMonitor(); - } + final WaffleServlet servlet = new WaffleServlet() { - public ControllerDataBinder getControllerDataBinder() { - sb.append("getCDB;"); - return null; + public String getInitParameter(String s) { + return "x"; } - public ControllerDefinitionFactory getControllerDefinitionFactory() { - sb.append("getCDF;"); - return null; + @Override + public ServletContext getServletContext() { + return servletContext; } - - public MessageResources getMessageResources() { - sb.append("getMessageResources;"); - return null; - } - - public ViewDataBinder getViewDataBinder() { - sb.append("getViewDataBinder;"); - return null; - } - - public ViewResolver getViewResolver() { - sb.append("getViewResolver;"); - return viewResolver; - } - - public Validator getValidator() { - sb.append("getValidator;"); - return null; - } - - }; - // Mock ServletContext mockery.checking(new Expectations() { { - one(servletContext).getAttribute(ComponentRegistry.class.getName()); - will(returnValue(componentRegistry)); + one(servletMonitor).servletInitialized(servlet); } }); - WaffleServlet servlet = new WaffleServlet() { - @Override - public ServletConfig getServletConfig() { - return servletConfig; - } + new WaffleServlet.ServletFilter().setAppContainer(mpc); - @Override - public ServletContext getServletContext() { - return servletContext; - } - }; - servlet.init(); } @@ -776,44 +738,6 @@ assertEquals("prefix-foobar-suffix", viewResolver.resolve(view)); } - @SuppressWarnings("serial") - @Test(expected = WaffleException.class) - public void cannotInitWithoutParameter() throws ServletException { - final ServletContext servletContext = mockery.mock(ServletContext.class); - mockery.checking(new Expectations() { - { - one(servletContext).getAttribute(ComponentRegistry.class.getName()); - will(returnValue(null)); - } - }); - - // Mock ServletConfig - final ServletConfig servletConfig = mockery.mock(ServletConfig.class); - mockery.checking(new Expectations() { - { - one(servletConfig).getInitParameter(Constants.VIEW_PREFIX_KEY); - will(returnValue("/WEB-INF/jsp")); - one(servletConfig).getInitParameter(Constants.VIEW_SUFFIX_KEY); - one(servletConfig).getInitParameter(Constants.ERRORS_VIEW_KEY); - will(returnValue("errors")); - } - }); - - WaffleServlet servlet = new WaffleServlet() { - @Override - public ServletConfig getServletConfig() { - return servletConfig; - } - - @Override - public ServletContext getServletContext() { - return servletContext; - } - }; - - servlet.init(); - } - public class NonDispatchingController { private int count = 0; private MessagesContext messages = new DefaultMessagesContext(null);
To unsubscribe from this list please visit:
