Title: [waffle-scm] [739] trunk/examples: WAFFLE-87: Added servlet context stub for component registry retrieval.

Diff

Modified: trunk/examples/freemarker-example/src/main/java/org/codehaus/waffle/example/freemarker/FreemarkerRegistrar.java (738 => 739)

--- trunk/examples/freemarker-example/src/main/java/org/codehaus/waffle/example/freemarker/FreemarkerRegistrar.java	2008-06-18 23:18:46 UTC (rev 738)
+++ trunk/examples/freemarker-example/src/main/java/org/codehaus/waffle/example/freemarker/FreemarkerRegistrar.java	2008-06-19 10:05:51 UTC (rev 739)
@@ -39,10 +39,7 @@
         register(PersonListValueConverter.class);
         ValueConverterFinder finder = registry.locateByType(ValueConverterFinder.class);
         finder.registerConverter((ValueConverter) getRegistered(PersonListValueConverter.class));
-    }
-
-    @Override
-    public void session() {
         register("people/manage", PersonController.class);
     }
+
 }

Modified: trunk/examples/freemarker-example/src/test/java/org/codehaus/waffle/example/freemarker/FreemarkerRegistrarTest.java (738 => 739)

--- trunk/examples/freemarker-example/src/test/java/org/codehaus/waffle/example/freemarker/FreemarkerRegistrarTest.java	2008-06-18 23:18:46 UTC (rev 738)
+++ trunk/examples/freemarker-example/src/test/java/org/codehaus/waffle/example/freemarker/FreemarkerRegistrarTest.java	2008-06-19 10:05:51 UTC (rev 739)
@@ -1,44 +1,28 @@
 package org.codehaus.waffle.example.freemarker;
 
-import static java.util.Arrays.asList;
+import static org.codehaus.waffle.context.ContextLevel.APPLICATION;
+import static org.codehaus.waffle.context.ContextLevel.REQUEST;
+import static org.codehaus.waffle.context.ContextLevel.SESSION;
+import static org.junit.Assert.assertNotNull;
 
-import java.util.Collections;
-
-import javax.servlet.ServletContext;
-
-import org.codehaus.waffle.ComponentRegistry;
-import org.codehaus.waffle.bind.converters.DateValueConverter;
-import org.codehaus.waffle.context.pico.PicoComponentRegistry;
-import org.codehaus.waffle.mock.PicoRegistrarMockery;
-import org.hamcrest.Matcher;
-import org.hamcrest.core.IsEqual;
-import org.hamcrest.core.IsNot;
-import org.jmock.Expectations;
+import org.codehaus.waffle.testing.registrar.RegistrarHelper;
 import org.junit.Test;
 
 public class FreemarkerRegistrarTest {
 
-    @SuppressWarnings("unchecked")
+    private static final Class<FreemarkerRegistrar> CLASS = FreemarkerRegistrar.class;
+
     @Test
-    public void canAssertConfiguration() {
-        PicoRegistrarMockery mockery = new PicoRegistrarMockery();
-        final ServletContext context = mockery.mockServletContext();
-        mockery.checking(new Expectations(){{
-            atLeast(1).of(context).getInitParameterNames();
-            will(returnValue(Collections.enumeration(asList("register:DateValueConverter"))));
-            atLeast(1).of(context).getInitParameter(with(notEqual("register:DateValueConverter")));
-            will(returnValue(null));
-            atLeast(1).of(context).getInitParameter(with(equal("register:DateValueConverter")));
-            will(returnValue(DateValueConverter.class.getName()));
-        }});
-        mockery.checking(new Expectations(){{
-            atLeast(1).of(context).getAttribute(ComponentRegistry.class.getName());
-            will(returnValue(new PicoComponentRegistry(context)));
-        }});
-        mockery.assertConfiguration(FreemarkerRegistrar.class);
+    public void canRegisterComponentsAtDifferentLevels() {
+        RegistrarHelper helper = new RegistrarHelper();
+        helper.componentsFor(CLASS, APPLICATION);
+        helper.componentsFor(CLASS, SESSION);
+        helper.componentsFor(CLASS, REQUEST);
     }
 
-    public static <T> Matcher<T> notEqual(T value) {
-        return new IsNot<T>(new IsEqual<T>(value));
+    @Test
+    public void canRetrieveControllers() {
+        RegistrarHelper helper = new RegistrarHelper();
+        assertNotNull(helper.controllerFor(CLASS, APPLICATION, "people/manage"));
     }
 }

Modified: trunk/examples/paranamer-example/src/main/java/org/codehaus/waffle/example/paranamer/ParanamerRegistrar.java (738 => 739)

--- trunk/examples/paranamer-example/src/main/java/org/codehaus/waffle/example/paranamer/ParanamerRegistrar.java	2008-06-18 23:18:46 UTC (rev 738)
+++ trunk/examples/paranamer-example/src/main/java/org/codehaus/waffle/example/paranamer/ParanamerRegistrar.java	2008-06-19 10:05:51 UTC (rev 739)
@@ -18,11 +18,11 @@
         register(SimplePersonDAO.class);
         register("helloworld", HelloWorldController.class);
         register("ajaxexample", AjaxExample.class);
+        register("people/person", PersonController.class);
     }
 
     @Override
     public void session() {
         register("calculator", CalculatorController.class);
-        register("people/person", PersonController.class);
     }
 }

Modified: trunk/examples/paranamer-example/src/test/java/org/codehaus/waffle/example/paranamer/ParanamerRegistrarTest.java (738 => 739)

--- trunk/examples/paranamer-example/src/test/java/org/codehaus/waffle/example/paranamer/ParanamerRegistrarTest.java	2008-06-18 23:18:46 UTC (rev 738)
+++ trunk/examples/paranamer-example/src/test/java/org/codehaus/waffle/example/paranamer/ParanamerRegistrarTest.java	2008-06-19 10:05:51 UTC (rev 739)
@@ -1,13 +1,31 @@
 package org.codehaus.waffle.example.paranamer;
 
-import org.codehaus.waffle.mock.PicoRegistrarMockery;
+import static org.codehaus.waffle.context.ContextLevel.APPLICATION;
+import static org.codehaus.waffle.context.ContextLevel.REQUEST;
+import static org.codehaus.waffle.context.ContextLevel.SESSION;
+import static org.junit.Assert.assertNotNull;
+
+import org.codehaus.waffle.testing.registrar.RegistrarHelper;
 import org.junit.Test;
 
 public class ParanamerRegistrarTest {
 
+    private static final Class<ParanamerRegistrar> CLASS = ParanamerRegistrar.class;
+
     @Test
-    public void canAssertConfiguration() {
-        new PicoRegistrarMockery().assertConfiguration(ParanamerRegistrar.class);
+    public void canRegisterComponentsAtDifferentLevels() {
+        RegistrarHelper helper = new RegistrarHelper();
+        helper.componentsFor(CLASS, APPLICATION);
+        helper.componentsFor(CLASS, SESSION);
+        helper.componentsFor(CLASS, REQUEST);
     }
 
+    @Test
+    public void canRetrieveControllers() {
+        RegistrarHelper helper = new RegistrarHelper();
+        assertNotNull(helper.controllerFor(CLASS, APPLICATION, "helloworld"));
+        assertNotNull(helper.controllerFor(CLASS, APPLICATION, "ajaxexample"));
+        assertNotNull(helper.controllerFor(CLASS, APPLICATION, "people/person"));
+        assertNotNull(helper.controllerFor(CLASS, SESSION, "calculator"));
+    }
 }

Modified: trunk/examples/pom.xml (738 => 739)

--- trunk/examples/pom.xml	2008-06-18 23:18:46 UTC (rev 738)
+++ trunk/examples/pom.xml	2008-06-19 10:05:51 UTC (rev 739)
@@ -36,6 +36,12 @@
       <version>${pom.version}</version>
     </dependency>
     <dependency>
+      <groupId>${pom.groupId}</groupId>
+      <artifactId>waffle-testing</artifactId>
+      <version>${pom.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>servlet-api</artifactId>
       <scope>provided</scope>

Modified: trunk/examples/simple-example/src/main/java/org/codehaus/waffle/example/simple/SimpleRegistrar.java (738 => 739)

--- trunk/examples/simple-example/src/main/java/org/codehaus/waffle/example/simple/SimpleRegistrar.java	2008-06-18 23:18:46 UTC (rev 738)
+++ trunk/examples/simple-example/src/main/java/org/codehaus/waffle/example/simple/SimpleRegistrar.java	2008-06-19 10:05:51 UTC (rev 739)
@@ -1,5 +1,7 @@
 package org.codehaus.waffle.example.simple;
 
+import static org.codehaus.waffle.registrar.RequestParameterReference.requestParameter;
+
 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
 import org.codehaus.waffle.example.simple.action.CalculatorController;
 import org.codehaus.waffle.example.simple.action.HelloWorldController;
@@ -9,7 +11,6 @@
 import org.codehaus.waffle.io.RequestFileUploader;
 import org.codehaus.waffle.registrar.AbstractRegistrar;
 import org.codehaus.waffle.registrar.Registrar;
-import static org.codehaus.waffle.registrar.RequestParameterReference.requestParameter;
 
 public class SimpleRegistrar extends AbstractRegistrar {
 
@@ -19,9 +20,10 @@
 
     @Override
     public void application() {
-        register(SimplePersonDAO.class)
-                .register("helloworld", HelloWorldController.class)
-                .register("ajaxexample", AjaxExample.class);
+        register(SimplePersonDAO.class);
+        register("helloworld", HelloWorldController.class);
+        register("ajaxexample", AjaxExample.class);
+        register("person", PersonController.class);
     }
 
     @Override
@@ -31,14 +33,13 @@
         //validation for automobile controller done in the controller itself
         //uncomment registration of validator if you prefer to override it
         //register("automobileValidator", AutomobileControllerValidator.class);
-        register("person", PersonController.class);
     }
 
     @Override
     public void request() {
-        register("fileItemFactory", DiskFileItemFactory.class)
-                .register("uploader", RequestFileUploader.class)
-                .register("upload", UploadController.class)
-                .register("parameter_example", ParameterExampleController.class, "Mike", requestParameter("age", 30));
+        register("fileItemFactory", DiskFileItemFactory.class);
+        register("uploader", RequestFileUploader.class);
+        register("upload", UploadController.class);
+        register("parameter_example", ParameterExampleController.class, "Mike", requestParameter("age", 30));
     }
 }

Modified: trunk/examples/simple-example/src/test/java/org/codehaus/waffle/example/simple/SimpleRegistrarTest.java (738 => 739)

--- trunk/examples/simple-example/src/test/java/org/codehaus/waffle/example/simple/SimpleRegistrarTest.java	2008-06-18 23:18:46 UTC (rev 738)
+++ trunk/examples/simple-example/src/test/java/org/codehaus/waffle/example/simple/SimpleRegistrarTest.java	2008-06-19 10:05:51 UTC (rev 739)
@@ -1,26 +1,33 @@
 package org.codehaus.waffle.example.simple;
 
-import org.codehaus.waffle.mock.PicoRegistrarMockery;
-import org.jmock.Expectations;
+import static org.codehaus.waffle.context.ContextLevel.APPLICATION;
+import static org.codehaus.waffle.context.ContextLevel.REQUEST;
+import static org.codehaus.waffle.context.ContextLevel.SESSION;
+import static org.junit.Assert.assertNotNull;
+
+import org.codehaus.waffle.testing.registrar.RegistrarHelper;
 import org.junit.Test;
 
-import javax.servlet.http.HttpServletRequest;
-
 public class SimpleRegistrarTest {
 
+    private static final Class<SimpleRegistrar> CLASS = SimpleRegistrar.class;
+
     @Test
-    public void canAssertConfiguration() {
-        PicoRegistrarMockery picoRegistrarMockery = new PicoRegistrarMockery();
-        final HttpServletRequest request = picoRegistrarMockery.mockHttpServletRequest();
+    public void canRegisterComponentsAtDifferentLevels() {
+        RegistrarHelper helper = new RegistrarHelper();
+        helper.componentsFor(CLASS, APPLICATION);
+        helper.componentsFor(CLASS, SESSION);
+        helper.componentsFor(CLASS, REQUEST);
+    }
 
-        picoRegistrarMockery.checking(new Expectations() {
-            {
-                exactly(2).of(request).getParameter("age");
-                will(returnValue("99"));
-            }
-        });
-
-        picoRegistrarMockery.assertConfiguration(SimpleRegistrar.class);
+    @Test
+    public void canRetrieveControllers() {
+        RegistrarHelper helper = new RegistrarHelper();
+        assertNotNull(helper.controllerFor(CLASS, APPLICATION, "helloworld"));
+        assertNotNull(helper.controllerFor(CLASS, APPLICATION, "ajaxexample"));
+        assertNotNull(helper.controllerFor(CLASS, APPLICATION, "person"));
+        assertNotNull(helper.controllerFor(CLASS, SESSION, "calculator"));
+        assertNotNull(helper.controllerFor(CLASS, SESSION, "automobile"));
     }
 
 }

Modified: trunk/waffle-testing/src/main/java/org/codehaus/waffle/testing/registrar/RegistrarHelper.java (738 => 739)

--- trunk/waffle-testing/src/main/java/org/codehaus/waffle/testing/registrar/RegistrarHelper.java	2008-06-18 23:18:46 UTC (rev 738)
+++ trunk/waffle-testing/src/main/java/org/codehaus/waffle/testing/registrar/RegistrarHelper.java	2008-06-19 10:05:51 UTC (rev 739)
@@ -1,15 +1,29 @@
 package org.codehaus.waffle.testing.registrar;
 
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.Set;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.Servlet;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+
+import org.codehaus.waffle.ComponentRegistry;
 import org.codehaus.waffle.bind.DefaultStringTransmuter;
 import org.codehaus.waffle.bind.ognl.OgnlValueConverterFinder;
 import org.codehaus.waffle.context.ContextLevel;
+import org.codehaus.waffle.context.pico.PicoComponentRegistry;
 import org.codehaus.waffle.context.pico.PicoLifecycleStrategy;
 import org.codehaus.waffle.monitor.SilentMonitor;
 import org.codehaus.waffle.registrar.Registrar;
 import org.codehaus.waffle.registrar.pico.DefaultParameterResolver;
 import org.codehaus.waffle.registrar.pico.PicoRegistrar;
+import org.picocontainer.DefaultPicoContainer;
+import org.picocontainer.MutablePicoContainer;
 import org.picocontainer.monitors.NullComponentMonitor;
-import org.picocontainer.DefaultPicoContainer;
 
 /**
  * Registrar helper class. Retrieves controller instances registered in a Registrar and allows the registration of all
@@ -28,7 +42,7 @@
      * @return The controller instance or <code>null</code> if not found
      */
     public Object controllerFor(Class<?> registrarType, ContextLevel level, String path) {
-        DefaultPicoContainer registrarContainer = new DefaultPicoContainer();
+        MutablePicoContainer registrarContainer = new DefaultPicoContainer();
         registerComponentsFor(registrarType, level, registrarContainer);
         return registrarContainer.getComponent(path);
     }
@@ -44,8 +58,8 @@
     }
 
     private void registerComponentsFor(Class<?> registrarType, ContextLevel level,
-            DefaultPicoContainer registrarContainer) {
-        Registrar registrar = createRegistrar(registrarContainer, registrarType);
+            MutablePicoContainer registrarContainer) {
+        Registrar registrar = createRegistrar(registrarType, registrarContainer);
         switch (level) {
             case APPLICATION:
                 registrar.application();
@@ -59,23 +73,143 @@
         }
     }
 
-    private Registrar createRegistrar(DefaultPicoContainer container, Class<?> type) {
-        String name = type.getName();
+    /**
+     * Creates a registrar backed by a PicoRegistrar delegate
+     * 
+     * @param type the Class of the Registrar to be created
+     * @param registrarContainer the container with the registrar component
+     * @return The Registrar of the given type
+     */
+    private Registrar createRegistrar(Class<?> type, MutablePicoContainer registrarContainer) {
+        String registrarName = type.getName();
+        registrarContainer.addComponent(PicoComponentRegistryServletContext.class);
         try {
-            DefaultPicoContainer initContainer = new DefaultPicoContainer();
-            initContainer.addComponent(container);
+            MutablePicoContainer initContainer = new DefaultPicoContainer();
+            initContainer.addComponent(registrarContainer);
             initContainer.addComponent(NullComponentMonitor.class);
             initContainer.addComponent(PicoLifecycleStrategy.class);
             initContainer.addComponent(SilentMonitor.class);
-            initContainer.addComponent(PicoRegistrar.class);
             initContainer.addComponent(DefaultParameterResolver.class);
             initContainer.addComponent(DefaultStringTransmuter.class);
             initContainer.addComponent(OgnlValueConverterFinder.class);
-            initContainer.addComponent(name, Class.forName(name));
-            return (Registrar) initContainer.getComponent(name);
+            initContainer.addComponent(PicoRegistrar.class);
+            initContainer.addComponent(registrarName, Class.forName(registrarName));
+            return (Registrar) initContainer.getComponent(registrarName);
         } catch (Exception e) {
-            throw new RuntimeException("Failed to create Registrar for " + name, e);
+            throw new RuntimeException("Failed to create Registrar for " + registrarName, e);
         }
     }
 
+    /**
+     * ServletContext stub that returns a PicoComponentRegistry backed by the context itself.
+     * 
+     * @author Mauro Talevi
+     */
+    public static class PicoComponentRegistryServletContext implements ServletContext {
+        
+        private ComponentRegistry registry;
+        
+        public PicoComponentRegistryServletContext(){
+            this.registry = new PicoComponentRegistry(this);
+        }
+        
+        public Object getAttribute(String name) {
+            if ( name.equals(ComponentRegistry.class.getName())){
+                return registry;
+            }
+            return null;
+        }
+
+        public Enumeration<String> getAttributeNames() {
+            return null;
+        }
+
+        public ServletContext getContext(String uripath) {
+            return null;
+        }
+
+        public String getContextPath() {
+            return null;
+        }
+
+        public String getInitParameter(String name) {
+            return null;
+        }
+
+        public Enumeration<String> getInitParameterNames() {
+            return null;
+        }
+
+        public int getMajorVersion() {
+            return 0;
+        }
+
+        public String getMimeType(String file) {
+            return null;
+        }
+
+        public int getMinorVersion() {
+            return 0;
+        }
+
+        public RequestDispatcher getNamedDispatcher(String name) {
+            return null;
+        }
+
+        public String getRealPath(String path) {
+            return null;
+        }
+
+        public RequestDispatcher getRequestDispatcher(String path) {
+            return null;
+        }
+
+        public URL getResource(String path) throws MalformedURLException {
+            return null;
+        }
+
+        public InputStream getResourceAsStream(String path) {
+            return null;
+        }
+
+        public Set<String> getResourcePaths(String path) {
+            return null;
+        }
+
+        public String getServerInfo() {
+            return null;
+        }
+
+        public Servlet getServlet(String name) throws ServletException {
+            return null;
+        }
+
+        public String getServletContextName() {
+            return null;
+        }
+
+        public Enumeration<String> getServletNames() {
+            return null;
+        }
+
+        public Enumeration<?> getServlets() {
+            return null;
+        }
+
+        public void log(String msg) {
+        }
+
+        public void log(Exception exception, String msg) {
+        }
+
+        public void log(String message, Throwable throwable) {
+        }
+
+        public void removeAttribute(String name) {
+        }
+
+        public void setAttribute(String name, Object object) {
+        }
+        
+    }
 }


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to