KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > persist > db > DatabaseFactoryClassFactoryTest


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: DatabaseFactoryClassFactoryTest.java,v 1.6 2007/01/07 06:14:20 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.persist.db;
23
24 import java.util.Properties JavaDoc;
25
26 import junit.framework.TestCase;
27
28 import org.opensubsystems.core.error.OSSDynamicClassException;
29 import org.opensubsystems.core.persist.DatabaseTestFactory;
30 import org.opensubsystems.core.util.ClassFactory;
31 import org.opensubsystems.core.util.Config;
32
33 /**
34  * Tests for factory which can create database factory classes based on other
35  * classes based on name modification.
36  *
37  * @version $Id: DatabaseFactoryClassFactoryTest.java,v 1.6 2007/01/07 06:14:20 bastafidli Exp $
38  * @author Miro Halas
39  * @code.reviewer Miro Halas
40  * @code.reviewed Initial revision
41  */

42 public class DatabaseFactoryClassFactoryTest extends TestCase
43 {
44    // Tests ////////////////////////////////////////////////////////////////////
45

46    /**
47     * Constructor for DatabaseFactoryClassFactoryTest.
48     * @param arg0 - name of the test
49     */

50    public DatabaseFactoryClassFactoryTest(
51       String JavaDoc arg0
52    )
53    {
54       super(arg0);
55    }
56
57    /**
58     * Set up environment for the test case.
59     *
60     * @throws Exception - an error has occured during setting up test
61     */

62    protected void setUp(
63    ) throws Exception JavaDoc
64    {
65       super.setUp();
66    }
67
68    /**
69     * Restore original environment after the test case.
70     *
71     * @throws Exception - an error has occured during tearing down up test
72     */

73    protected void tearDown() throws Exception JavaDoc
74    {
75       super.tearDown();
76    }
77
78    /**
79     * Test createNewInstance method with Class parameter
80     *
81     * @throws Exception - and error has occured
82     */

83    public void testCreateInstanceWithClass(
84    ) throws Exception JavaDoc
85    {
86       Config originalInstance;
87       
88       originalInstance = Config.getInstance();
89       try
90       {
91          Object JavaDoc test;
92          ClassFactory factory = new DatabaseFactoryClassFactory();
93          
94          // First instantiate class which is not database factory
95
try
96          {
97             test = factory.createInstance(String JavaDoc.class);
98             fail("Shouldn't be able to instantiate String class using" +
99                  " DatabaseFactory class factory");
100          }
101          catch (OSSDynamicClassException dceExc)
102          {
103             // This is expected exception, don't do anything
104
}
105          catch (AssertionError JavaDoc aErr)
106          {
107             // This is expected error, don't do anything
108
if (!("The factory class identifier is expected to end with name Factory".equals(
109                      aErr.getMessage())))
110             {
111                // rethrow errror
112
throw aErr;
113   
114             }
115          }
116
117          // Now try to instantiate test database factory to see what we get
118
test = factory.createInstance(DatabaseTestFactory.class);
119          assertNotNull("Cannot instantiate class", test);
120          assertTrue("Must be instance of DatabaseFactory",
121                     test instanceof DatabaseFactory);
122          assertTrue("Must be instance of DatabaseTestFactory",
123                     test instanceof DatabaseTestFactory);
124          assertTrue("Must be instance of DatabaseTestDatabaseFactory",
125                     test instanceof DatabaseTestDatabaseFactory);
126          
127          // Now create new configuration file which will instruct the class
128
// factory to create completely different class when asked for the
129
// default class. This allows us configurable overriding
130
Properties JavaDoc predefinedProperties = new Properties JavaDoc();
131          predefinedProperties.put(DatabaseTestFactory.class.getName(),
132                                   DatabaseTest2DatabaseFactory.class.getName());
133          // Now reset the default configuration file to use our properties
134
Config.setInstance(new Config(predefinedProperties));
135       
136          // And now ask for the class and we should get the configured class
137
test = factory.createInstance(DatabaseTestFactory.class);
138          assertNotNull("Cannot instantiate class", test);
139          assertTrue("Must be instance of DatabaseFactory",
140                     test instanceof DatabaseFactory);
141          assertTrue("Must be instance of DatabaseTestFactory",
142                     test instanceof DatabaseTestFactory);
143          assertTrue("Must be instance of DatabaseTest2DatabaseFactory",
144                   test instanceof DatabaseTest2DatabaseFactory);
145       }
146       finally
147       {
148          // Reset the default config instance since other tests may depend on it
149
Config.setInstance(originalInstance);
150       }
151    }
152
153    /**
154     * Test createNewInstance method with Class parameter and default class
155     *
156     * @throws Exception - and error has occured
157     */

158    public void testCreateInstanceWithClassAndDefault(
159    ) throws Exception JavaDoc
160    {
161       Config originalInstance;
162       
163       originalInstance = Config.getInstance();
164       try
165       {
166          Object JavaDoc test;
167          ClassFactory factory = new DatabaseFactoryClassFactory();
168          
169          // First instantiate class which is not database factory
170
try
171          {
172             test = factory.createInstance(String JavaDoc.class);
173             fail("Shouldn't be able to instantiate String class using" +
174                  " DatabaseFactory class factory");
175          }
176          catch (OSSDynamicClassException dceExc)
177          {
178             // This is expected exception, don't do anything
179
}
180          catch (AssertionError JavaDoc aErr)
181          {
182             // This is expected error, don't do anything
183
if (!("The factory class identifier is expected to end with name Factory".equals(
184                      aErr.getMessage())))
185             {
186                // rethrow errror
187
throw aErr;
188   
189             }
190          }
191
192          // Now try to instantiate test database factory to see what we get
193
test = factory.createInstance(DatabaseTestFactory.class);
194          assertNotNull("Cannot instantiate class", test);
195          assertTrue("Must be instance of DatabaseFactory",
196                     test instanceof DatabaseFactory);
197          assertTrue("Must be instance of DatabaseTestFactory",
198                     test instanceof DatabaseTestFactory);
199          assertTrue("Must be instance of HsqlDBDatabaseTestFactory",
200                     test instanceof DatabaseTestDatabaseFactory);
201          
202          // Now create new configuration file which will instruct the class
203
// factory to create completely different class when asked for the
204
// default class. This allows us configurable overriding
205
Properties JavaDoc predefinedProperties = new Properties JavaDoc();
206          predefinedProperties.put(DatabaseTestFactory.class.getName(),
207                                   DatabaseTest2DatabaseFactory.class.getName());
208          // Now reset the default configuration file to use our properties
209
Config.setInstance(new Config(predefinedProperties));
210       
211          // And now ask for the class and we should get the configured class
212
test = factory.createInstance(DatabaseTestFactory.class);
213          assertNotNull("Cannot instantiate class", test);
214          assertTrue("Must be instance of DatabaseFactory",
215                     test instanceof DatabaseFactory);
216          assertTrue("Must be instance of DatabaseTestFactory",
217                     test instanceof DatabaseTestFactory);
218          assertTrue("Must be instance of SapDBDatabaseTestFactory",
219                   test instanceof DatabaseTest2DatabaseFactory);
220           
221          // The only way how to test the creation of default parameter is to
222
// setup property which instruct to load some bogus class when some
223
// other class is requested
224
predefinedProperties = new Properties JavaDoc();
225          predefinedProperties.put(DatabaseTestFactory.class.getName(),
226                                   "random.text");
227          // Now reset the default configuration file to use our properties
228
Config.setInstance(new Config(predefinedProperties));
229           
230          try
231          {
232             test = factory.createInstance(DatabaseTestFactory.class,
233                                           String JavaDoc.class);
234             fail("Shouldn't be able to instantiate String class even as a default"
235                  + " class using DatabaseFactory class factory");
236          }
237          catch (OSSDynamicClassException dceExc)
238          {
239            // This is expected exception, don't do anything
240
}
241
242          // But we should be able to instantiate valid default class
243
test = factory.createInstance(DatabaseTestFactory.class,
244                                        DatabaseTestDatabaseFactory.class);
245          assertTrue("Must be instance of DatabaseFactory",
246                     test instanceof DatabaseTestDatabaseFactory);
247       }
248       finally
249       {
250          // Reset the default config instance since other tests may depend on it
251
Config.setInstance(originalInstance);
252       }
253    }
254
255    /**
256     * Test createNewInstance method with Strig parameter
257     *
258     * @throws Exception - and error has occured
259     */

260    public void testCreateInstanceWithString(
261    ) throws Exception JavaDoc
262    {
263       Config originalInstance;
264       
265       originalInstance = Config.getInstance();
266       try
267       {
268          Object JavaDoc test;
269          ClassFactory factory = new DatabaseFactoryClassFactory();
270          
271          // First instantiate class which is not database factory
272
try
273          {
274             test = factory.createInstance(String JavaDoc.class.getName());
275             fail("Shouldn't be able to instantiate String class using" +
276                  " DatabaseFactory class factory");
277          }
278          catch (OSSDynamicClassException dceExc)
279          {
280             // This is expected exception, don't do anything
281
}
282          catch (AssertionError JavaDoc aErr)
283          {
284             // This is expected error, don't do anything
285
if (!("The factory class identifier is expected to end with name Factory".equals(
286                      aErr.getMessage())))
287             {
288                // rethrow errror
289
throw aErr;
290   
291             }
292          }
293
294          // Now try to instantiate test database factory to see what we get
295
test = factory.createInstance(DatabaseTestFactory.class.getName());
296          assertNotNull("Cannot instantiate class", test);
297          assertTrue("Must be instance of DatabaseFactory",
298                     test instanceof DatabaseFactory);
299          assertTrue("Must be instance of DatabaseTestFactory",
300                     test instanceof DatabaseTestFactory);
301          assertTrue("Must be instance of HsqlDBDatabaseTestFactory",
302                     test instanceof DatabaseTestDatabaseFactory);
303          
304          // Now create new configuration file which will instruct the class
305
// factory to create completely different class when asked for the
306
// default class. This allows us configurable overriding
307
Properties JavaDoc predefinedProperties = new Properties JavaDoc();
308          predefinedProperties.put(DatabaseTestFactory.class.getName(),
309                                   DatabaseTest2DatabaseFactory.class.getName());
310          // Just to demonstrate that the ControllerClassFactory can still
311
// use the first argument just as an identifier and not as a name,
312
// configure another random property to point to base class
313
predefinedProperties.put("random.text",
314                                   DatabaseTestDatabaseFactory.class.getName());
315          predefinedProperties.put("random.text2", String JavaDoc.class.getName());
316          // Now reset the default configuration file to use our properties
317
Config.setInstance(new Config(predefinedProperties));
318       
319          // And now ask for the class and we should get the misconfigured class
320
test = factory.createInstance(DatabaseTestFactory.class.getName());
321          assertNotNull("Cannot instantiate class", test);
322          assertTrue("Must be instance of DatabaseFactory",
323                     test instanceof DatabaseFactory);
324          assertTrue("Must be instance of DatabaseTestFactory",
325                     test instanceof DatabaseTestFactory);
326          assertTrue("Must be instance of SapDBDatabaseTestFactory",
327                   test instanceof DatabaseTest2DatabaseFactory);
328
329          // Now ask for the text and we should still get our factory
330
test = factory.createInstance("random.text");
331          assertNotNull("Cannot instantiate class", test);
332          assertTrue("Must be instance of base class",
333                     test instanceof DatabaseTestDatabaseFactory);
334          
335          try
336          {
337             test = factory.createInstance("random.text2");
338             fail("Shouldn't be able to instantiate String class even though it"
339                  + " is valid class using DatabaseFactory class factory");
340          }
341          catch (OSSDynamicClassException dceExc)
342          {
343            // This is expected exception, don't do anything
344
}
345       }
346       finally
347       {
348          // Reset the default config instance since other tests may depend on it
349
Config.setInstance(originalInstance);
350       }
351    }
352
353    /**
354     * Test createNewInstance method with String parameter and default class
355     *
356     * @throws Exception - and error has occured
357     */

358    public void testCreateInstanceWithStringAndDefault(
359    ) throws Exception JavaDoc
360    {
361       Config originalInstance;
362       
363       originalInstance = Config.getInstance();
364       try
365       {
366          Object JavaDoc test;
367          ClassFactory factory = new DatabaseFactoryClassFactory();
368          
369          // First instantiate class which is not database factory
370
try
371          {
372             test = factory.createInstance(String JavaDoc.class.getName());
373             fail("Shouldn't be able to instantiate String class using" +
374                  " DatabaseFactory class factory");
375          }
376          catch (OSSDynamicClassException dceExc)
377          {
378             // This is expected exception, don't do anything
379
}
380          catch (AssertionError JavaDoc aErr)
381          {
382             // This is expected error, don't do anything
383
if (!("The factory class identifier is expected to end with name Factory".equals(
384                      aErr.getMessage())))
385             {
386                // rethrow errror
387
throw aErr;
388   
389             }
390          }
391
392          // Now try to instantiate test database factory to see what we get
393
test = factory.createInstance(DatabaseTestFactory.class.getName());
394          assertNotNull("Cannot instantiate class", test);
395          assertTrue("Must be instance of DatabaseFactory",
396                     test instanceof DatabaseFactory);
397          assertTrue("Must be instance of DatabaseTestFactory",
398                     test instanceof DatabaseTestFactory);
399          assertTrue("Must be instance of HsqlDBDatabaseTestFactory",
400                     test instanceof DatabaseTestDatabaseFactory);
401          
402          // Now create new configuration file which will instruct the class
403
// factory to create completely different class when asked for the
404
// default class. THis allows us configurable overriding
405
Properties JavaDoc predefinedProperties = new Properties JavaDoc();
406          predefinedProperties.put(DatabaseTestFactory.class.getName(),
407                                   DatabaseTest2DatabaseFactory.class.getName());
408          // Just to demonstrate that the ControllerClassFactory can still
409
// use the first argument just as an identifier and not as a name,
410
// configure another random property to point to base class
411
predefinedProperties.put("random.text",
412                                   DatabaseTestDatabaseFactory.class.getName());
413          predefinedProperties.put("random.text2", String JavaDoc.class.getName());
414          // Now reset the default configuration file to use our properties
415
Config.setInstance(new Config(predefinedProperties));
416       
417          // And now ask for the class and we should get the misconfigured class
418
test = factory.createInstance(DatabaseTestFactory.class.getName());
419          assertNotNull("Cannot instantiate class", test);
420          assertTrue("Must be instance of DatabaseFactory",
421                     test instanceof DatabaseFactory);
422          assertTrue("Must be instance of DatabaseTestFactory",
423                     test instanceof DatabaseTestFactory);
424          assertTrue("Must be instance of SapDBDatabaseTestFactory",
425                   test instanceof DatabaseTest2DatabaseFactory);
426
427          // Now ask for the text and we should still get our factory
428
test = factory.createInstance("random.text");
429          assertNotNull("Cannot instantiate class", test);
430          assertTrue("Must be instance of base class",
431                     test instanceof DatabaseTestDatabaseFactory);
432          
433          try
434          {
435             test = factory.createInstance("random.text2");
436             fail("Shouldn't be able to instantiate String class even though it"
437                  + " is valid class using DatabaseFactory class factory");
438          }
439          catch (OSSDynamicClassException dceExc)
440          {
441            // This is expected exception, don't do anything
442
}
443
444          // The only way how to test the creation of default parameter is to
445
// setup property which instruct to load some bogus class when some
446
// other class is requested
447
predefinedProperties = new Properties JavaDoc();
448          predefinedProperties.put(DatabaseTestFactory.class.getName(),
449                                   "random.text");
450          // Now reset the default configuration file to use our properties
451
Config.setInstance(new Config(predefinedProperties));
452           
453          try
454          {
455             test = factory.createInstance(DatabaseTestFactory.class.getName(),
456                                           String JavaDoc.class);
457             fail("Shouldn't be able to instantiate String class even as a default"
458                  + " class using DatabaseFactory class factory");
459          }
460          catch (OSSDynamicClassException dceExc)
461          {
462            // This is expected exception, don't do anything
463
}
464
465          // But we should be able to instantiate valid default class
466
test = factory.createInstance(DatabaseTestFactory.class.getName(),
467                                        DatabaseTestDatabaseFactory.class);
468          assertTrue("Must be instance of DatabaseFactory",
469                     test instanceof DatabaseTestDatabaseFactory);
470       }
471       finally
472       {
473          // Reset the default config instance since other tests may depend on it
474
Config.setInstance(originalInstance);
475       }
476    }
477 }
478
Popular Tags