Revision: 1048
          http://stripes.svn.sourceforge.net/stripes/?rev=1048&view=rev
Author:   bengunter
Date:     2009-02-25 20:29:45 +0000 (Wed, 25 Feb 2009)

Log Message:
-----------
STS-614: The interfaceImplementations map is no longer static. Added 
addInterfaceImplementation(..) to allow subclasses to add new implementations 
more easily. Added a unit test for this new feature.

Modified Paths:
--------------
    
trunk/stripes/src/net/sourceforge/stripes/controller/DefaultObjectFactory.java
    trunk/tests/src/net/sourceforge/stripes/controller/ObjectFactoryTests.java

Modified: 
trunk/stripes/src/net/sourceforge/stripes/controller/DefaultObjectFactory.java
===================================================================
--- 
trunk/stripes/src/net/sourceforge/stripes/controller/DefaultObjectFactory.java  
    2009-02-25 19:43:05 UTC (rev 1047)
+++ 
trunk/stripes/src/net/sourceforge/stripes/controller/DefaultObjectFactory.java  
    2009-02-25 20:29:45 UTC (rev 1048)
@@ -80,9 +80,8 @@
      * the interface and will, by default, be instantiated when an instance of 
the interface is
      * needed.
      */
-    protected static final Map<Class<?>, Class<?>> interfaceImplementations = 
new HashMap<Class<?>, Class<?>>();
-
-    static {
+    protected final Map<Class<?>, Class<?>> interfaceImplementations = new 
HashMap<Class<?>, Class<?>>();
+    {
         interfaceImplementations.put(Collection.class, ArrayList.class);
         interfaceImplementations.put(List.class,       ArrayList.class);
         interfaceImplementations.put(Set.class,        HashSet.class);
@@ -168,6 +167,21 @@
     }
 
     /**
+     * Register a class as the default implementation of an interface. The 
implementation class will
+     * be returned from future calls to {...@link 
#getImplementingClass(Class)} when the argument is
+     * {...@code iface}.
+     * 
+     * @param iface The interface class
+     * @param impl The implementation class
+     */
+    public <T> void addImplementingClass(Class<T> iface, Class<? extends T> 
impl) {
+        if (!iface.isInterface())
+            throw new IllegalArgumentException("Class " + iface.getName() + " 
is not an interface");
+        else
+            interfaceImplementations.put(iface, impl);
+    }
+
+    /**
      * Create a new instance of {...@code clazz} by looking up the specified 
constructor and passing it
      * and its parameters to {...@link #newInstance(Constructor, Object...)}.
      * 

Modified: 
trunk/tests/src/net/sourceforge/stripes/controller/ObjectFactoryTests.java
===================================================================
--- trunk/tests/src/net/sourceforge/stripes/controller/ObjectFactoryTests.java  
2009-02-25 19:43:05 UTC (rev 1047)
+++ trunk/tests/src/net/sourceforge/stripes/controller/ObjectFactoryTests.java  
2009-02-25 20:29:45 UTC (rev 1048)
@@ -50,6 +50,11 @@
         }
     }
 
+    public static class MyRunnable implements Runnable {
+        public void run() {
+        }
+    }
+
     private static final Log log = Log.getInstance(ObjectFactoryTests.class);
 
     public void instantiateClasses(ObjectFactory factory, Class<?>... classes) 
{
@@ -65,6 +70,7 @@
         for (Class<?> clazz : classes) {
             log.debug("Instantiating ", clazz);
             Object o = factory.newInstance(clazz);
+            log.debug("Implementation class is ", o.getClass().getName());
             Assert.assertNotNull(o);
             Assert.assertTrue(clazz.isAssignableFrom(o.getClass()));
         }
@@ -112,6 +118,13 @@
         }
     }
 
+    @Test(groups = "fast")
+    public void customInterfaceImpl() {
+        DefaultObjectFactory factory = new DefaultObjectFactory();
+        factory.addImplementingClass(Runnable.class, MyRunnable.class);
+        instantiateInterfaces(factory, Runnable.class);
+    }
+
     /** Attempt to instantiate a class that does not have a no-arg 
constructor. */
     @Test(groups = "fast", expectedExceptions = InstantiationException.class)
     public void missingNoArgsConstructor() throws Throwable {


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development

Reply via email to