View Javadoc

1   // $Id: ObjectFactoryTest.java 396 2007-09-11 12:39:05Z pgmjsd $
2   
3   package org.yajul.util;
4   
5   import junit.framework.TestCase;
6   
7   import java.net.URL;
8   import java.util.Properties;
9   
10  /***
11   * TODO: Document this test case here.
12   */
13  public class ObjectFactoryTest extends TestCase
14  {
15      private static final String THING_CLASS_NAME = Thing.class.getName();
16  
17      /***
18       * Standard JUnit test case constructor.
19       *
20       * @param name The name of the test case.
21       */
22      public ObjectFactoryTest(String name)
23      {
24          super(name);
25      }
26  
27      public void testTestObjects()
28      {
29          Thing t = new Thing();
30          t.setValue(128);
31          t.getValue();
32          new NoConstructor(129);
33      }
34  
35      public void testObjectFactory() throws Exception
36      {
37          String s = ObjectFactory.getClasspathRoot(Thing.class);
38          assertNotNull(s);
39  
40          Thing t = (Thing) ObjectFactory.createInstanceFromPropertiesResource("objectfactory-test.properties","thingproperty",
41                  null,true);
42          assertEquals(42,t.getValue());
43  
44          t = (Thing) ObjectFactory.createInstanceFromPropertiesResource("bogus.properties","thingproperty",
45                  THING_CLASS_NAME,false);
46          assertEquals(42,t.getValue());
47  
48          t = (Thing) ObjectFactory.createInstanceFromPropertiesResource("objectfactory-test.properties","bogusproperty",
49                  THING_CLASS_NAME,false);
50          assertEquals(42,t.getValue());
51  
52          Properties properties = new Properties();
53          properties.setProperty("classname",InitializeableThing.class.getName());
54          InitializeableThing it = (InitializeableThing)ObjectFactory.createInstanceFromProperties(properties,"classname",
55                  null,true);
56          assertTrue(it.isInitialized());
57  
58          InitializationException iex = null;
59          try
60          {
61              ObjectFactory.createInstanceFromPropertiesResource("bogus.properties","thingproperty",null,true);
62          }
63          catch (InitializationException e)
64          {
65              iex = e;
66          }
67          assertNotNull(iex);
68  
69          iex = null;
70          try
71          {
72              ObjectFactory.createInstanceFromProperties(null,"thingproperty",null,true);
73          }
74          catch (InitializationException e)
75          {
76              iex = e;
77          }
78          assertNotNull(iex);
79  
80          iex = null;
81          try
82          {
83              ObjectFactory.createInstanceFromPropertiesResource("objectfactory-test.properties","bogusproperty",null,true);
84          }
85          catch (InitializationException e)
86          {
87              iex = e;
88          }
89          assertNotNull(iex);
90  
91          InitializationError ie = null;
92          try
93          {
94              ReflectionUtil.createInstance("org.yajul.util.test.ObjectFactoryTest$DoesNotExist");
95          }
96          catch (InitializationError e)
97          {
98              ie = e;
99          }
100         assertNotNull(ie);
101 
102         ie = null;
103         try
104         {
105             ReflectionUtil.createInstance("java.io.Serializable");
106         }
107         catch (InitializationError e)
108         {
109             ie = e;
110         }
111         assertNotNull(ie);
112     }
113 
114     public static class Thing
115     {
116         private int value = 42;
117 
118         public int getValue()
119         {
120             return value;
121         }
122 
123         public void setValue(int value)
124         {
125             this.value = value;
126         }
127     }
128 
129     public static class InitializeableThing extends Thing implements Initializeable
130     {
131         private boolean initialized = false;
132 
133         public void initialize(Properties properties) throws InitializationException
134         {
135             initialized = true;
136         }
137 
138         public boolean isInitialized()
139         {
140             return initialized;
141         }
142 
143     }
144 
145     public static class NoConstructor extends Thing
146     {
147         @SuppressWarnings({"UnusedDeclaration"})
148         private NoConstructor(int foo)
149         {
150         }
151     }
152 }