KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > util > ClassFactoryTest


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: ClassFactoryTest.java,v 1.3 2007/01/07 06:14:55 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.util;
23
24 import java.util.Properties JavaDoc;
25
26 import junit.framework.TestCase;
27
28 /**
29  * Tests for factory which can create classes based on other classes based on
30  * name modification.
31  *
32  * @version $Id: ClassFactoryTest.java,v 1.3 2007/01/07 06:14:55 bastafidli Exp $
33  * @author Miro Halas
34  * @code.reviewer Miro Halas
35  * @code.reviewed Initial revision
36  */

37 public class ClassFactoryTest extends TestCase
38 {
39    // Inner class for testing //////////////////////////////////////////////////
40

41    /**
42     * Base test class
43     */

44    public static class ClassFactoryTestSampleBase
45    {
46       /**
47        * Test attribute
48        */

49       protected String JavaDoc m_attribA;
50       
51       /**
52        * Constructor
53        */

54       public ClassFactoryTestSampleBase()
55       {
56          m_attribA = "1";
57       }
58    }
59    
60    /**
61     * Base test class
62     */

63    public static class ClassFactoryTestSampleDerived extends ClassFactoryTestSampleBase
64    {
65       /**
66        * Test attribute
67        */

68       protected String JavaDoc m_attribB;
69       
70       /**
71        * Constructor
72        */

73       public ClassFactoryTestSampleDerived()
74       {
75          m_attribB = "2";
76       }
77    }
78    
79    // Tests ////////////////////////////////////////////////////////////////////
80

81    /**
82     * Constructor for ClassFactoryTest.
83     * @param arg0 - name of the test
84     */

85    public ClassFactoryTest(
86       String JavaDoc arg0
87    )
88    {
89       super(arg0);
90    }
91
92    /**
93     * Set up environment for the test case.
94     *
95     * @throws Exception - an error has occured during setting up test
96     */

97    protected void setUp(
98    ) throws Exception JavaDoc
99    {
100       super.setUp();
101    }
102
103    /**
104     * Restore original environment after the test case.
105     *
106     * @throws Exception - an error has occured during tearing down up test
107     */

108    protected void tearDown() throws Exception JavaDoc
109    {
110       super.tearDown();
111    }
112
113    /**
114     * Test createNewInstance method with Class parameter
115     *
116     * @throws Exception - and error has occured
117     */

118    public void testCreateInstanceWithClass(
119    ) throws Exception JavaDoc
120    {
121       Config originalInstance;
122       
123       originalInstance = Config.getInstance();
124       try
125       {
126          Object JavaDoc test;
127          ClassFactory factory = new ClassFactory();
128          
129          // First instantiate class which should be instantiated when asked
130
// for a base class
131
test = factory.createInstance(ClassFactoryTestSampleBase.class);
132          assertNotNull("Cannot instantiate class", test);
133          assertTrue("Must be instance of base class",
134                     test instanceof ClassFactoryTestSampleBase);
135          assertFalse("Cannot be instance of derived class",
136                      test instanceof ClassFactoryTestSampleDerived);
137    
138          // First instantiate class which should be instantiated when asked
139
// for a derived class
140
test = factory.createInstance(ClassFactoryTestSampleDerived.class);
141          assertNotNull("Cannot instantiate class", test);
142          assertTrue("Must be instance of base class",
143                   test instanceof ClassFactoryTestSampleBase);
144          assertTrue("Must be instance of derived class",
145                     test instanceof ClassFactoryTestSampleDerived);
146          
147          // Now create new configuration file which will instruct the class factory
148
// to create derived class when asked for the base class. THis allows us
149
// configurable overriding
150
Properties JavaDoc predefinedProperties = new Properties JavaDoc();
151          predefinedProperties.put(ClassFactoryTestSampleBase.class.getName(),
152                                   ClassFactoryTestSampleDerived.class.getName());
153          // Now reset the default configuration file to use our properties
154
Config.setInstance(new Config(predefinedProperties));
155       
156          // And now ask for the base class and we should get derived class
157
test = factory.createInstance(ClassFactoryTestSampleBase.class);
158          assertNotNull("Cannot instantiate class", test);
159          assertTrue("Must be instance of base class",
160                     test instanceof ClassFactoryTestSampleBase);
161          assertTrue("Must be instance of derived class",
162                     test instanceof ClassFactoryTestSampleDerived);
163       }
164       finally
165       {
166          // Reset the default config instance since other tests may depend on it
167
Config.setInstance(originalInstance);
168       }
169    }
170
171    /**
172     * Test createNewInstance method with Class parameter and default class
173     *
174     * @throws Exception - and error has occured
175     */

176    public void testCreateInstanceWithClassAndDefault(
177    ) throws Exception JavaDoc
178    {
179       Config originalInstance;
180       
181       originalInstance = Config.getInstance();
182       try
183       {
184          Object JavaDoc test;
185          ClassFactory factory = new ClassFactory();
186          
187          // First instantiate class which should be instantiated when asked
188
// for a base class
189
test = factory.createInstance(ClassFactoryTestSampleBase.class);
190          assertNotNull("Cannot instantiate class", test);
191          assertTrue("Must be instance of base class",
192                     test instanceof ClassFactoryTestSampleBase);
193          assertFalse("Cannot be instance of derived class",
194                      test instanceof ClassFactoryTestSampleDerived);
195    
196          // First instantiate class which should be instantiated when asked
197
// for a derived class
198
test = factory.createInstance(ClassFactoryTestSampleDerived.class);
199          assertNotNull("Cannot instantiate class", test);
200          assertTrue("Must be instance of base class",
201                   test instanceof ClassFactoryTestSampleBase);
202          assertTrue("Must be instance of derived class",
203                     test instanceof ClassFactoryTestSampleDerived);
204          
205          // Now create new configuration file which will instruct the class factory
206
// to create derived class when asked for the base class. THis allows us
207
// configurable overriding
208
Properties JavaDoc predefinedProperties = new Properties JavaDoc();
209          predefinedProperties.put(ClassFactoryTestSampleBase.class.getName(),
210                                   ClassFactoryTestSampleDerived.class.getName());
211          // Now reset the default configuration file to use our properties
212
Config.setInstance(new Config(predefinedProperties));
213       
214          // And now ask for the base class and we should get derived class
215
test = factory.createInstance(ClassFactoryTestSampleBase.class);
216          assertNotNull("Cannot instantiate class", test);
217          assertTrue("Must be instance of base class",
218                     test instanceof ClassFactoryTestSampleBase);
219          assertTrue("Must be instance of derived class",
220                   test instanceof ClassFactoryTestSampleDerived);
221        
222          // The only way how to test the creation of default parameter is to
223
// setup property which instruct to load some bogus class when some
224
// other class is requested
225
predefinedProperties = new Properties JavaDoc();
226          predefinedProperties.put(ClassFactoryTestSampleBase.class.getName(),
227                                   "random.text");
228          // Now reset the default configuration file to use our properties
229
Config.setInstance(new Config(predefinedProperties));
230          
231          test = factory.createInstance(ClassFactoryTestSampleBase.class,
232                                        String JavaDoc.class);
233          assertNotNull("Cannot instantiate class", test);
234          assertTrue("Must be instance of String class", test instanceof String JavaDoc);
235       }
236       finally
237       {
238          // Reset the default config instance since other tests may depend on it
239
Config.setInstance(originalInstance);
240       }
241    }
242
243    /**
244     * Test createNewInstance method with Strig parameter
245     *
246     * @throws Exception - and error has occured
247     */

248    public void testCreateInstanceWithString(
249    ) throws Exception JavaDoc
250    {
251       Config originalInstance;
252       
253       originalInstance = Config.getInstance();
254       try
255       {
256          Object JavaDoc test;
257          ClassFactory factory = new ClassFactory();
258          
259          // First instantiate class which should be instantiated when asked
260
// for a base class
261
test = factory.createInstance(ClassFactoryTestSampleBase.class.getName());
262          assertNotNull("Cannot instantiate class", test);
263          assertTrue("Must be instance of base class",
264                     test instanceof ClassFactoryTestSampleBase);
265          assertFalse("Cannot be instance of derived class",
266                      test instanceof ClassFactoryTestSampleDerived);
267    
268          // First instantiate class which should be instantiated when asked
269
// for a derived class
270
test = factory.createInstance(ClassFactoryTestSampleDerived.class.getName());
271          assertNotNull("Cannot instantiate class", test);
272          assertTrue("Must be instance of base class",
273                   test instanceof ClassFactoryTestSampleBase);
274          assertTrue("Must be instance of derived class",
275                     test instanceof ClassFactoryTestSampleDerived);
276          
277          // Now create new configuration file which will instruct the class factory
278
// to create derived class when asked for the base class. THis allows us
279
// configurable overriding
280
Properties JavaDoc predefinedProperties = new Properties JavaDoc();
281          predefinedProperties.put(ClassFactoryTestSampleBase.class.getName(),
282                                   ClassFactoryTestSampleDerived.class.getName());
283          // Just to demonstrate that the ClassFactory is using the first argument
284
// just as an identifier and not as a name, configure another random
285
// property to point to base class
286
predefinedProperties.put("random.text",
287                                   ClassFactoryTestSampleBase.class.getName());
288          // Now reset the default configuration file to use our properties
289
Config.setInstance(new Config(predefinedProperties));
290       
291          // And now ask for the base class and we should get derived class
292
test = factory.createInstance(ClassFactoryTestSampleBase.class.getName());
293          assertNotNull("Cannot instantiate class", test);
294          assertTrue("Must be instance of base class",
295                     test instanceof ClassFactoryTestSampleBase);
296          assertTrue("Must be instance of derived class",
297                   test instanceof ClassFactoryTestSampleDerived);
298        
299          test = factory.createInstance("random.text");
300          assertNotNull("Cannot instantiate class", test);
301          assertTrue("Must be instance of base class",
302                     test instanceof ClassFactoryTestSampleBase);
303          assertFalse("Cannot be instance of derived class",
304                      test instanceof ClassFactoryTestSampleDerived);
305       }
306       finally
307       {
308          // Reset the default config instance since other tests may depend on it
309
Config.setInstance(originalInstance);
310       }
311    }
312
313    /**
314     * Test createNewInstance method with String parameter and default class
315     *
316     * @throws Exception - and error has occured
317     */

318    public void testCreateInstanceWithStringAndDefault(
319    ) throws Exception JavaDoc
320    {
321       Config originalInstance;
322       
323       originalInstance = Config.getInstance();
324       try
325       {
326          Object JavaDoc test;
327          ClassFactory factory = new ClassFactory();
328          
329          // First instantiate class which should be instantiated when asked
330
// for a base class
331
test = factory.createInstance(ClassFactoryTestSampleBase.class.getName());
332          assertNotNull("Cannot instantiate class", test);
333          assertTrue("Must be instance of base class",
334                     test instanceof ClassFactoryTestSampleBase);
335          assertFalse("Cannot be instance of derived class",
336                      test instanceof ClassFactoryTestSampleDerived);
337    
338          // First instantiate class which should be instantiated when asked
339
// for a derived class
340
test = factory.createInstance(ClassFactoryTestSampleDerived.class.getName());
341          assertNotNull("Cannot instantiate class", test);
342          assertTrue("Must be instance of base class",
343                   test instanceof ClassFactoryTestSampleBase);
344          assertTrue("Must be instance of derived class",
345                     test instanceof ClassFactoryTestSampleDerived);
346          
347          // Now create new configuration file which will instruct the class factory
348
// to create derived class when asked for the base class. THis allows us
349
// configurable overriding
350
Properties JavaDoc predefinedProperties = new Properties JavaDoc();
351          predefinedProperties.put(ClassFactoryTestSampleBase.class.getName(),
352                                   ClassFactoryTestSampleDerived.class.getName());
353          // Just to demonstrate that the ClassFactory is using the first argument
354
// just as an identifier and not as a name, configure another random
355
// property to point to base class
356
predefinedProperties.put("random.text",
357                                   ClassFactoryTestSampleBase.class.getName());
358          // Now reset the default configuration file to use our properties
359
Config.setInstance(new Config(predefinedProperties));
360       
361          // And now ask for the base class and we should get derived class
362
test = factory.createInstance(ClassFactoryTestSampleBase.class.getName());
363          assertNotNull("Cannot instantiate class", test);
364          assertTrue("Must be instance of base class",
365                     test instanceof ClassFactoryTestSampleBase);
366          assertTrue("Must be instance of derived class",
367                   test instanceof ClassFactoryTestSampleDerived);
368        
369          test = factory.createInstance("random.text");
370          assertNotNull("Cannot instantiate class", test);
371          assertTrue("Must be instance of base class",
372                     test instanceof ClassFactoryTestSampleBase);
373          assertFalse("Cannot be instance of derived class",
374                      test instanceof ClassFactoryTestSampleDerived);
375          
376          // The only way how to test the creation of default parameter is to
377
// setup property which instruct to load some bogus class when some
378
// other class is requested
379
predefinedProperties = new Properties JavaDoc();
380          predefinedProperties.put(ClassFactoryTestSampleBase.class.getName(),
381                                   "random.text");
382          // Now reset the default configuration file to use our properties
383
Config.setInstance(new Config(predefinedProperties));
384          
385          test = factory.createInstance(ClassFactoryTestSampleBase.class.getName(),
386                                        String JavaDoc.class);
387          assertNotNull("Cannot instantiate class", test);
388          assertTrue("Must be instance of String class", test instanceof String JavaDoc);
389       }
390       finally
391       {
392          // Reset the default config instance since other tests may depend on it
393
Config.setInstance(originalInstance);
394       }
395    }
396 }
397
Popular Tags