KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > persist > DataFactoryManagerTest


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: DataFactoryManagerTest.java,v 1.3 2007/01/07 06:15:31 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;
23
24 import java.util.Properties JavaDoc;
25
26 import junit.framework.TestCase;
27
28 import org.opensubsystems.core.persist.db.DatabaseTest2DatabaseFactory;
29 import org.opensubsystems.core.persist.db.DatabaseTestDatabaseFactory;
30 import org.opensubsystems.core.util.Config;
31
32 /**
33  * Tests for manager of database factory classes.
34  *
35  * @version $Id: DataFactoryManagerTest.java,v 1.3 2007/01/07 06:15:31 bastafidli Exp $
36  * @author Miro Halas
37  * @code.reviewer Miro Halas
38  * @code.reviewed Initial revision
39  */

40 public class DataFactoryManagerTest extends TestCase
41 {
42    // Tests ////////////////////////////////////////////////////////////////////
43

44    /**
45     * Constructor for DataFactoryManagerTest.
46     * @param arg0 - name of the test
47     */

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

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

71    protected void tearDown() throws Exception JavaDoc
72    {
73       super.tearDown();
74    }
75
76    /**
77     * Test getFactoryInstance method
78     *
79     * @throws Exception - and error has occured
80     */

81    public void testGetFactoryInstance(
82    ) throws Exception JavaDoc
83    {
84       Config originalInstance;
85       
86       originalInstance = Config.getInstance();
87       try
88       {
89          Object JavaDoc test;
90          Object JavaDoc test2;
91          Object JavaDoc test3;
92          DataFactoryManager factoryManager = new DataFactoryManager();
93          
94          // Create instance of factory for current data
95
test = factoryManager.getFactoryInstance(DatabaseTestFactory.class);
96          assertNotNull("Cannot instantiate class", test);
97          assertTrue("Must be instance of DataFactory",
98                     test instanceof DataFactory);
99          assertTrue("Must be instance of DatabaseTestFactory",
100                     test instanceof DatabaseTestFactory);
101          assertTrue("Must be instance of DatabaseTestDatabaseFactory",
102                     test instanceof DatabaseTestDatabaseFactory);
103          // Now make sure that the instance is cached, ask for it again and we
104
// should get the same
105
test2 = factoryManager.getFactoryInstance(DatabaseTestFactory.class);
106          assertTrue("The data factory is not cached.", test == test2);
107          
108          // Now create new configuration file which will instruct the class
109
// factory to create completely different class when asked for the
110
// default class. This allows us configurable overriding
111
Properties JavaDoc predefinedProperties = new Properties JavaDoc();
112          // Put some other data factory to test This still override
113
// whatever the environment says
114
predefinedProperties.put(DatabaseTestFactory.class.getName(),
115                                   DatabaseTest2DatabaseFactory.class.getName());
116          // Now reset the default configuration file to use our properties
117
Config.setInstance(new Config(predefinedProperties));
118       
119          // And now ask for the class and we should get the original class
120
// since it is cached
121
test = factoryManager.getFactoryInstance(DatabaseTestFactory.class);
122          assertNotNull("Cannot instantiate class", test);
123          assertTrue("Must be instance of DataFactory",
124                     test instanceof DataFactory);
125          assertTrue("Must be instance of DatabaseTestFactory",
126                     test instanceof DatabaseTestFactory);
127                assertTrue("Must be instance of DatabaseTestDatabaseFactory",
128                         test instanceof DatabaseTestDatabaseFactory);
129          assertTrue("The data factory is not cached.", test == test2);
130          
131          // But if we create new instance of factory manager, nothing will be
132
// cached and we should get the misconfigured class
133
factoryManager = new DataFactoryManager();
134          test3 = factoryManager.getFactoryInstance(DatabaseTestFactory.class);
135          assertNotNull("Cannot instantiate class", test);
136          assertTrue("Must be instance of DataFactory",
137                     test instanceof DataFactory);
138          assertTrue("Must be instance of DatabaseTestFactory",
139                     test instanceof DatabaseTestFactory);
140          assertTrue("Must be instance of DatabaseTest2DatabaseFactory",
141                     test3 instanceof DatabaseTest2DatabaseFactory);
142          // And since This is new instance, the cached copy should be different
143
// from the previous ones
144
assertFalse("The data factory is not cached.", test == test3);
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 getInstance method
155     *
156     * @throws Exception - and error has occured
157     */

158    public void testGetInstance(
159    ) throws Exception JavaDoc
160    {
161       Config originalInstance;
162       
163       originalInstance = Config.getInstance();
164       try
165       {
166          Object JavaDoc test;
167          Object JavaDoc test2;
168          
169          // Create instance of factory for current database
170
test = DataFactoryManager.getInstance(DatabaseTestFactory.class);
171          assertNotNull("Cannot instantiate class", test);
172          assertTrue("Must be instance of DataFactory",
173                     test instanceof DataFactory);
174          assertTrue("Must be instance of DatabaseTestFactory",
175                     test instanceof DatabaseTestFactory);
176                assertTrue("Must be instance of DatabaseTestDatabaseFactory",
177                           test instanceof DatabaseTestDatabaseFactory);
178
179          // Now make sure that the instance is cached, ask for it again and we
180
// should get the same
181
test2 = DataFactoryManager.getInstance(DatabaseTestFactory.class);
182          assertTrue("The data factory is not cached.", test == test2);
183          
184          // Now create new configuration file which will instruct the class
185
// factory to create completely different class when asked for the
186
// default class. This allows us configurable overriding
187
Properties JavaDoc predefinedProperties = new Properties JavaDoc();
188          // Put some other data factory to test This still override
189
// whatever the environment says
190
predefinedProperties.put(DatabaseTestFactory.class.getName(),
191                                   DatabaseTest2DatabaseFactory.class.getName());
192          // Now reset the default configuration file to use our properties
193
Config.setInstance(new Config(predefinedProperties));
194       
195          // And now ask for the class and we should get the original class
196
// since it is cached
197
test = DataFactoryManager.getInstance(DatabaseTestFactory.class);
198          assertNotNull("Cannot instantiate class", test);
199          assertTrue("Must be instance of DataFactory",
200                     test instanceof DataFactory);
201          assertTrue("Must be instance of DatabaseTestFactory",
202                     test instanceof DatabaseTestFactory);
203          assertTrue("Must be instance of DatabaseTestDatabaseFactory",
204                   test instanceof DatabaseTestDatabaseFactory);
205       }
206       finally
207       {
208          // Reset the default config instance since other tests may depend on it
209
Config.setInstance(originalInstance);
210       }
211    }
212 }
213
Popular Tags