KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > test > PersistenceManagerFactoryImplTest


1 /*
2  * $Id: PersistenceManagerFactoryImplTest.java,v 1.5 2003/11/27 00:22:51 jackknifebarber Exp $
3  */

4
5 package com.triactive.jdo.test;
6
7 import java.io.BufferedInputStream JavaDoc;
8 import java.io.BufferedOutputStream JavaDoc;
9 import java.io.ByteArrayInputStream JavaDoc;
10 import java.io.ByteArrayOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.ObjectInputStream JavaDoc;
13 import java.io.ObjectOutputStream JavaDoc;
14 import java.sql.Connection JavaDoc;
15 import java.util.Properties JavaDoc;
16 import javax.jdo.JDOUserException;
17 import javax.jdo.JDOHelper;
18 import javax.jdo.JDOUnsupportedOptionException;
19 import javax.jdo.PersistenceManager;
20 import javax.jdo.PersistenceManagerFactory;
21 import junit.framework.Assert;
22 import com.triactive.jdo.DatabaseProperties;
23 import com.triactive.jdo.PersistenceManagerFactoryImpl;
24
25
26 /**
27  * Tests the basic functionality of creating <code>PersistenceManagerFactory</code>s
28  * and setting properties on them.
29  *
30  * @author <a HREF="mailto:kgrizzle@triactive.com">Kelly Grizzle</a>
31  * @version $Revision: 1.5 $
32  */

33
34 public class PersistenceManagerFactoryImplTest
35   extends PersistenceTestCase
36 {
37
38     /**
39      * This class allows us to create a Properties to pass to
40      * JDOHelper.getPersistenceManagerFactory() easily.
41      */

42     public class PMFProperties
43       extends Properties JavaDoc
44     {
45         private boolean optimistic = false;
46         private boolean retainValues = false;
47         private boolean restoreValues = false;
48         private boolean ignoreCache = false;
49         private boolean nontransactionalRead = false;
50         private boolean nontransactionalWrite = false;
51         private boolean multithreaded = false;
52
53         private String JavaDoc driverName = null;
54         private String JavaDoc url = null;
55         private String JavaDoc userName = null;
56         private String JavaDoc password = null;
57         private String JavaDoc factoryName = null;
58         private String JavaDoc factory2Name = null;
59
60         private boolean validateTables;
61         private boolean validateConstraints;
62         private boolean autoCreateTables;
63         private int isolationLevel;
64       
65         /**
66          * Constructor. This sets the "javax.jdo.option.PersistenceManagerFactoryClass"
67          * for us.
68          */

69         public PMFProperties()
70         {
71             super();
72             this.setProperty("javax.jdo.PersistenceManagerFactoryClass",
73                              "com.triactive.jdo.PersistenceManagerFactoryImpl");
74
75             /*
76              * Set expected defaults. These can vary based on system
77              * properties.
78              */

79             PersistenceManagerFactoryImpl pmf = new PersistenceManagerFactoryImpl();
80             validateTables = pmf.getValidateTables();
81             validateConstraints = pmf.getValidateConstraints();
82             autoCreateTables = pmf.getAutoCreateTables();
83             isolationLevel = pmf.getTransactionIsolation();
84         }
85
86         public void setOptimistic(boolean b)
87         {
88             this.optimistic = b;
89             this.setProperty("javax.jdo.option.Optimistic", new Boolean JavaDoc(b).toString());
90         }
91
92         public void setRetainValues(boolean b)
93         {
94             this.retainValues = b;
95             this.setProperty("javax.jdo.option.RetainValues", new Boolean JavaDoc(b).toString());
96         }
97
98         public void setRestoreValues(boolean b)
99         {
100             this.restoreValues = b;
101             this.setProperty("javax.jdo.option.RestoreValues", new Boolean JavaDoc(b).toString());
102         }
103
104         public void setIgnoreCache(boolean b)
105         {
106             this.ignoreCache = b;
107             this.setProperty("javax.jdo.option.IgnoreCache", new Boolean JavaDoc(b).toString());
108         }
109
110         public void setNontransactionalRead(boolean b)
111         {
112             this.nontransactionalRead = b;
113             this.setProperty("javax.jdo.option.NontransactionalRead", new Boolean JavaDoc(b).toString());
114         }
115
116         public void setNontransactionalWrite(boolean b)
117         {
118             this.nontransactionalWrite = b;
119             this.setProperty("javax.jdo.option.NontransactionalWrite", new Boolean JavaDoc(b).toString());
120         }
121
122         public void setMultithreaded(boolean b)
123         {
124             this.multithreaded = b;
125             this.setProperty("javax.jdo.option.Multithreaded", new Boolean JavaDoc(b).toString());
126         }
127
128         public void setDriverName(String JavaDoc s)
129         {
130             this.driverName = s;
131             this.setProperty("javax.jdo.option.ConnectionDriverName", s);
132         }
133
134         public void setUserName(String JavaDoc s)
135         {
136             this.userName = s;
137             this.setProperty("javax.jdo.option.ConnectionUserName", s);
138         }
139
140         public void setPassword(String JavaDoc s)
141         {
142             this.password = s;
143             this.setProperty("javax.jdo.option.ConnectionPassword", s);
144         }
145
146         public void setURL(String JavaDoc s)
147         {
148             this.url = s;
149             this.setProperty("javax.jdo.option.ConnectionURL", s);
150         }
151
152         public void setFactoryName(String JavaDoc s)
153         {
154             this.factoryName = s;
155             this.setProperty("javax.jdo.option.ConnectionFactoryName", s);
156         }
157
158         public void setFactory2Name(String JavaDoc s)
159         {
160             this.factory2Name = s;
161             this.setProperty("javax.jdo.option.ConnectionFactory2Name", s);
162         }
163
164         public void setValidateTables(boolean b)
165         {
166             this.validateTables = b;
167             this.setProperty(PersistenceManagerFactoryImpl.VALIDATE_TABLES_PROPERTY, new Boolean JavaDoc(b).toString());
168         }
169
170         public void setValidateConstraints(boolean b)
171         {
172             this.validateConstraints = b;
173             this.setProperty(PersistenceManagerFactoryImpl.VALIDATE_CONSTRAINTS_PROPERTY, new Boolean JavaDoc(b).toString());
174         }
175
176         public void setAutoCreateTables(boolean b)
177         {
178             this.autoCreateTables = b;
179             this.setProperty(PersistenceManagerFactoryImpl.AUTO_CREATE_TABLES_PROPERTY, new Boolean JavaDoc(b).toString());
180         }
181
182         public void setTransactionIsolation(int i)
183         {
184             this.isolationLevel = i;
185             String JavaDoc name;
186
187             switch (i)
188             {
189                 case Connection.TRANSACTION_READ_UNCOMMITTED:
190                     name = "read uncommitted";
191                     break;
192                 case Connection.TRANSACTION_READ_COMMITTED:
193                     name = "read committed";
194                     break;
195                 case Connection.TRANSACTION_REPEATABLE_READ:
196                     name = "repeatable read";
197                     break;
198                 case Connection.TRANSACTION_SERIALIZABLE:
199                     name = "serializable";
200                     break;
201                 default:
202                     name = "bad level " + Integer.toString(i);
203                     break;
204             }
205
206             this.setProperty(PersistenceManagerFactoryImpl.TRANSACTION_ISOLATION_PROPERTY, name);
207         }
208
209         public void assertMatchesPMF(PersistenceManagerFactory pmf, Assert a)
210         {
211             a.assertEquals(optimistic, pmf.getOptimistic());
212             a.assertEquals(retainValues, pmf.getRetainValues());
213             a.assertEquals(restoreValues, pmf.getRestoreValues());
214             a.assertEquals(ignoreCache, pmf.getIgnoreCache());
215             a.assertEquals(nontransactionalRead, pmf.getNontransactionalRead());
216             a.assertEquals(nontransactionalWrite, pmf.getNontransactionalWrite());
217             a.assertEquals(multithreaded, pmf.getMultithreaded());
218             a.assertEquals(driverName, pmf.getConnectionDriverName());
219             a.assertEquals(url, pmf.getConnectionURL());
220             a.assertEquals(userName, pmf.getConnectionUserName());
221             a.assertEquals(factoryName, pmf.getConnectionFactoryName());
222             a.assertEquals(factory2Name, pmf.getConnectionFactory2Name());
223
224             PersistenceManagerFactoryImpl myPMF = (PersistenceManagerFactoryImpl)pmf;
225             a.assertEquals(validateTables, myPMF.getValidateTables());
226             a.assertEquals(validateConstraints, myPMF.getValidateConstraints());
227             a.assertEquals(autoCreateTables, myPMF.getAutoCreateTables());
228             a.assertEquals(isolationLevel, myPMF.getTransactionIsolation());
229         }
230     }
231
232
233
234     /**
235      * Used by the JUnit framework to construct tests. Normally, programmers
236      * would never explicitly use this constructor.
237      *
238      * @param name Name of the <tt>TestCase</tt>.
239      */

240
241     public PersistenceManagerFactoryImplTest(String JavaDoc name)
242     {
243         super(name);
244     }
245
246
247     /**
248      * Test instantiating a <code>PersistenceManagerFactory</code> via
249      * <code>JDOHelper.getPersistenceManagerFactory(Properties)</code>.
250      */

251     public void testJDOHelperInstantiation()
252     {
253         /*
254          * Things to consider:
255          * - Unknown properties should do nothing.
256          * - Setting Optimistic or RetainValues to true sets NontransactionalRead
257          * to true.
258          * - TransactionIsolation has valid values of Connection.TRANSACTION_*;
259          * anything else will throw an Exception.
260          * - A PersistenceManagerFactory obtained via JDOHelper should be
261          * nonconfigurable.
262          */

263
264         /*
265          * 1) Test setting all propers to valid values.
266          */

267         boolean optimistic = true;
268         boolean retainValues = true;
269         boolean restoreValues = true;
270         boolean ignoreCache = true;
271         boolean nontransactionalRead = true;
272         boolean nontransactionalWrite = true;
273         boolean multithreaded = true;
274         String JavaDoc driverName = DatabaseProperties.dbDriver;
275         String JavaDoc url = DatabaseProperties.dbURL;
276         String JavaDoc userName = DatabaseProperties.dbUser;
277         String JavaDoc password = DatabaseProperties.dbPassword;
278         //String factory1 = "factory1";
279
//String factory2 = "factory2";
280
boolean validateTables = true;
281         boolean validateConstraints = true;
282         boolean autoCreate = true;
283         int transactionIsolation = Connection.TRANSACTION_READ_COMMITTED;
284
285         PMFProperties props = new PMFProperties();
286         props.setOptimistic(optimistic);
287         props.setRetainValues(retainValues);
288         props.setRestoreValues(restoreValues);
289         props.setIgnoreCache(ignoreCache);
290         props.setNontransactionalRead(nontransactionalRead);
291         props.setNontransactionalWrite(nontransactionalWrite);
292         props.setMultithreaded(multithreaded);
293         props.setDriverName(driverName);
294         props.setUserName(userName);
295         props.setPassword(password);
296         props.setURL(url);
297         //props.setFactoryName(factory1); // Setting this uses jndi which blows up w/out
298
//props.setFactory2Name(factory2); // a managed environment (ie - no JNDI).
299
props.setValidateTables(validateTables);
300         props.setValidateConstraints(validateConstraints);
301         props.setAutoCreateTables(autoCreate);
302         props.setTransactionIsolation(transactionIsolation);
303
304         PersistenceManagerFactory pmf =
305             JDOHelper.getPersistenceManagerFactory(props);
306         assertTrue("PMF should be com.triactive.jdo.PersistenceManagerFactoryImpl.",
307                    (pmf instanceof PersistenceManagerFactoryImpl));
308         props.assertMatchesPMF(pmf, this);
309
310         /*
311          * Flip a bunch of flags to assert that the PMF is still configurable.
312          */

313         pmf.setOptimistic(!optimistic);
314         pmf.setRetainValues(!retainValues);
315         pmf.setRestoreValues(!restoreValues);
316         pmf.setIgnoreCache(!ignoreCache);
317         pmf.setNontransactionalRead(!nontransactionalRead);
318         pmf.setNontransactionalWrite(!nontransactionalWrite);
319         pmf.setMultithreaded(!multithreaded);
320
321         /* Scale back to a minimal set of properties for the following tests. */
322         props = new PMFProperties();
323         props.setDriverName(driverName);
324         props.setUserName(userName);
325         props.setPassword(password);
326         props.setURL(url);
327
328         /*
329          * 2) Test that an invalid driver name throws an exception
330          */

331         props.setDriverName("my.test.driver.should.not.exist");
332         pmf = JDOHelper.getPersistenceManagerFactory(props);
333
334         try
335         {
336             pmf.getPersistenceManager();
337             fail("Invalid driver name should have thrown exception");
338         }
339         catch (Exception JavaDoc e) { /* Ignore */ }
340         props.setDriverName(driverName); // restore
341

342
343         /*
344          * 3) Test that setting an invalid transaction isolation throws an exception.
345          */

346         props.setTransactionIsolation(8738);
347
348         try
349         {
350             pmf = JDOHelper.getPersistenceManagerFactory(props);
351             fail("Setting invalid transaction isolation should have thrown exception");
352         }
353         catch (Exception JavaDoc e) { /* Ignore */ }
354         props.setTransactionIsolation(transactionIsolation); // restore
355

356
357         /*
358          * 4) Test that a PMF is not configurable after getting a PM.
359          */

360         pmf = JDOHelper.getPersistenceManagerFactory(props);
361         PersistenceManager pm = pmf.getPersistenceManager();
362
363         try
364         {
365             pmf.setRetainValues(!retainValues);
366             fail("Setting properties after frozen should throw exception");
367         }
368         catch (JDOUserException e) { /* Ignore */ }
369         finally
370         {
371             pm.close();
372         }
373
374
375         /*
376          * 5) Test that setting an unknown property does nothing weird.
377          */

378         props.setProperty("com.triactive.jdo.MyTestUnknownProperty", "unknown");
379         pmf = JDOHelper.getPersistenceManagerFactory(props);
380         props.assertMatchesPMF(pmf, this);
381     }
382
383
384     public void testSerialization() throws Exception JavaDoc
385     {
386         /* Make a PMF to play with. */
387         PersistenceManagerFactory pmf1 = makeNonpooledPMF();
388         /* Clone it via serialization. */
389         PersistenceManagerFactory pmf2 = toPMF(toByteArray(pmf1));
390
391         assertTrue("PMF did not deserialize into a different object", pmf1 != pmf2);
392         assertEquals("PMF did not survive serialization intact", pmf1, pmf2);
393     }
394
395
396     private static byte[] toByteArray(PersistenceManagerFactory pmf) throws IOException JavaDoc
397     {
398         ByteArrayOutputStream JavaDoc bout = new ByteArrayOutputStream JavaDoc();
399         ObjectOutputStream JavaDoc oout = new ObjectOutputStream JavaDoc(new BufferedOutputStream JavaDoc(bout));
400         oout.writeObject(pmf);
401         oout.close();
402
403         return bout.toByteArray();
404     }
405
406
407     private static PersistenceManagerFactory toPMF(byte[] ba) throws IOException JavaDoc, ClassNotFoundException JavaDoc
408     {
409         ByteArrayInputStream JavaDoc bin = new ByteArrayInputStream JavaDoc(ba);
410         ObjectInputStream JavaDoc oin = new ObjectInputStream JavaDoc(new BufferedInputStream JavaDoc(bin));
411
412         return (PersistenceManagerFactory)oin.readObject();
413     }
414
415
416     public void testClose() throws Exception JavaDoc
417     {
418         /* Make a PMF to play with. */
419         PersistenceManagerFactory pmf1 = makeNonpooledPMF();
420         PersistenceManager[] pms = new PersistenceManager[5];
421
422         for (int i = 0; i < pms.length; ++i)
423             pms[i] = pmf1.getPersistenceManager();
424
425         pms[2].currentTransaction().begin();
426
427         try
428         {
429             pmf1.close();
430             fail("PMF.close() should have failed, a TX is active");
431         }
432         catch (JDOUserException e)
433         {
434             Throwable JavaDoc nested[] = e.getNestedExceptions();
435
436             assertNotNull("No nested exceptions: " + e, nested);
437             assertEquals("Wrong number of nested exceptions: " + e, 1, nested.length);
438         }
439
440         pms[2].currentTransaction().rollback();
441
442         for (int i = 0; i < pms.length; ++i)
443             assertTrue("Failed PMF.close() errnoneously closed a PM: " + pms[i], !pms[i].isClosed());
444
445         pmf1.close();
446
447         for (int i = 0; i < pms.length; ++i)
448             assertTrue("PMF.close() did not close PM: " + pms[i], pms[i].isClosed());
449
450         try
451         {
452             pmf1.getPersistenceManager();
453             fail("PMF.getPersistenceManager() should have failed after PMF.close()");
454         }
455         catch (JDOUserException e)
456         {
457             // expected
458
}
459     }
460 }
461
Popular Tags