1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package javax.jdo;
19
20 import java.io.File;
21 import java.io.InputStream;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Properties;
26
27 import javax.jdo.pc.PCPoint;
28 import javax.jdo.util.AbstractTest;
29 import javax.jdo.util.BatchTestRunner;
30
31 import javax.naming.Context;
32 import javax.naming.InitialContext;
33 import javax.naming.NamingException;
34
35 /**
36 * Tests class javax.jdo.JDOHelper.
37 * <p>
38 * TBD: implementation of testMakeDirty,
39 * TBD: testing interrogative methods for persistent instances
40 * TBD: getPMF for valid PMF class
41 */
42 public class JDOHelperTest extends AbstractTest {
43
44 /** */
45 public static void main(String args[]) {
46 BatchTestRunner.run(JDOHelperTest.class);
47 }
48
49 /** The purpose of this test is simply to call some of the
50 * methods on a constructed instance of JDOHelper and verify that
51 * they do not throw exceptions. It is not a functional test.
52 * @since 2.1
53 */
54 public void testConstructor() {
55 JDOHelper helper = new JDOHelper();
56 assertNull("getObjectId(null) returned non-null",
57 helper.getObjectId(null));
58 assertNull("getPersistenceManager(null) returned non-null",
59 helper.getPersistenceManager(null));
60 assertNull("getTransactionalObjectId(null) returned non-null",
61 helper.getTransactionalObjectId(null));
62 assertNull("getVersion(null) returned non-null",
63 helper.getVersion(null));
64 assertFalse("isDeleted(null) returned non-null",
65 helper.isDeleted(null));
66 assertFalse("isDetached(null) returned non-null",
67 helper.isDetached(null));
68 assertFalse("isDirty(null) returned non-null",
69 helper.isDirty(null));
70 assertFalse("isNew(null) returned non-null",
71 helper.isNew(null));
72 assertFalse("isPersistent(null) returned non-null",
73 helper.isPersistent(null));
74 assertFalse("isTransactional(null) returned non-null",
75 helper.isTransactional(null));
76 }
77
78 /** The purpose of this test is simply to call some of the
79 * methods on the static instance of JDOHelper and verify that
80 * they do not throw exceptions. It is not a functional test.
81 * @since 2.1
82 */
83 public void testGetInstance() {
84 JDOHelper helper = JDOHelper.getInstance();
85 assertNull("getObjectId(null) returned non-null",
86 helper.getObjectId(null));
87 assertNull("getPersistenceManager(null) returned non-null",
88 helper.getPersistenceManager(null));
89 assertNull("getTransactionalObjectId(null) returned non-null",
90 helper.getTransactionalObjectId(null));
91 assertNull("getVersion(null) returned non-null",
92 helper.getVersion(null));
93 assertFalse("isDeleted(null) returned non-null",
94 helper.isDeleted(null));
95 assertFalse("isDetached(null) returned non-null",
96 helper.isDetached(null));
97 assertFalse("isDirty(null) returned non-null",
98 helper.isDirty(null));
99 assertFalse("isNew(null) returned non-null",
100 helper.isNew(null));
101 assertFalse("isPersistent(null) returned non-null",
102 helper.isPersistent(null));
103 assertFalse("isTransactional(null) returned non-null",
104 helper.isTransactional(null));
105 }
106
107 /** */
108 public void testGetPM() {
109 PCPoint p = new PCPoint(1, new Integer(1));
110 if (JDOHelper.getPersistenceManager(p) != null) {
111 fail("JDOHelper.getPersistenceManager should return null pm for non-persistent instance");
112 }
113
114
115 }
116
117 /** */
118 public void testMakeDirty() {
119
120 }
121
122 /** */
123 public void testGetObjectId() {
124 PCPoint p = new PCPoint(1, new Integer(1));
125 if (JDOHelper.getObjectId(p) != null) {
126 fail("JDOHelper.getObjectId should return null ObjectId for non-persistent instance");
127 }
128
129
130 }
131
132 /** */
133 public void testGetTransactionObjectId() {
134 PCPoint p = new PCPoint(1, new Integer(1));
135 if (JDOHelper.getObjectId(p) != null) {
136 fail("JDOHelper.getTransactionalObjectId should return null ObjectId for non-persistent instance");
137 }
138
139
140 }
141
142 /** */
143 public void testIsDirty() {
144 PCPoint p = new PCPoint(1, new Integer(1));
145 if (JDOHelper.isDirty(p)) {
146 fail("JDOHelper.isDirty should return false for non-persistent instance");
147 }
148
149
150 }
151
152 /** */
153 public void testIsTransactional() {
154 PCPoint p = new PCPoint(1, new Integer(1));
155 if (JDOHelper.isTransactional(p)) {
156 fail("JDOHelper.isTransactional should return false for non-persistent instance");
157 }
158
159
160 }
161
162 /** */
163 public void testIsPersistent() {
164 PCPoint p = new PCPoint(1, new Integer(1));
165 if (JDOHelper.isPersistent(p)) {
166 fail("JDOHelper.isPersistent should return false for non-persistent instance");
167 }
168
169
170 }
171
172 /** */
173 public void testIsNew() {
174 PCPoint p = new PCPoint(1, new Integer(1));
175 if (JDOHelper.isNew(p)) {
176 fail("JDOHelper.isNew should return false for non-persistent instance");
177 }
178
179
180 }
181
182
183 /** */
184 public void testIsDeleted() {
185 PCPoint p = new PCPoint(1, new Integer(1));
186 if (JDOHelper.isDeleted(p)) {
187 fail("JDOHelper.isDeleted should return false for non-persistent instance");
188 }
189
190
191 }
192
193 /** Test null String resource with no class loader.
194 */
195 public void testGetPMFNullResource() {
196 PersistenceManagerFactory pmf = null;
197 try {
198 pmf = JDOHelper.getPersistenceManagerFactory((String)null);
199 fail("Null resource name should result in JDOFatalUserException");
200 }
201 catch (JDOFatalUserException ex) {
202 if (verbose)
203 println("Caught expected exception " + ex);
204 }
205 }
206
207 /** Test null String resource with good class loader.
208 */
209 public void testGetPMFNullResourceGoodClassLoader() {
210 PersistenceManagerFactory pmf = null;
211 try {
212 pmf = JDOHelper.getPersistenceManagerFactory((String)null, this.getClass().getClassLoader());
213 fail("Null resource name should result in JDOFatalUserException");
214 }
215 catch (JDOFatalUserException ex) {
216 if (verbose)
217 println("Caught expected exception " + ex);
218 }
219 }
220
221 /** Test bad String resource with no class loader.
222 */
223 public void testGetPMFBadResource() {
224 PersistenceManagerFactory pmf = null;
225 try {
226 pmf = JDOHelper.getPersistenceManagerFactory("Whatever");
227 fail("Null resource name should result in JDOFatalUserException");
228 }
229 catch (JDOFatalUserException ex) {
230 if (verbose)
231 println("Caught expected exception " + ex);
232 }
233 }
234
235 /** Test null String resource with good class loader.
236 */
237 public void testGetPMFBadResourceGoodClassLoader() {
238 PersistenceManagerFactory pmf = null;
239 try {
240 pmf = JDOHelper.getPersistenceManagerFactory("Whatever", this.getClass().getClassLoader());
241 fail("Null resource name should result in JDOFatalUserException");
242 }
243 catch (JDOFatalUserException ex) {
244 if (verbose)
245 println("Caught expected exception " + ex);
246 }
247 }
248
249 /** Test null File resource with no class loader.
250 */
251 public void testGetPMFNullFile() {
252 PersistenceManagerFactory pmf = null;
253 try {
254 pmf = JDOHelper.getPersistenceManagerFactory((File)null);
255 fail("Null file should result in JDOFatalUserException");
256 }
257 catch (JDOFatalUserException ex) {
258 if (verbose)
259 println("Caught expected exception " + ex);
260 }
261 }
262
263 /** Test null File resource with good class loader.
264 */
265 public void testGetPMFNullFileGoodClassLoader() {
266 PersistenceManagerFactory pmf = null;
267 try {
268 pmf = JDOHelper.getPersistenceManagerFactory((File)null, this.getClass().getClassLoader());
269 fail("Null file should result in JDOFatalUserException");
270 }
271 catch (JDOFatalUserException ex) {
272 if (verbose)
273 println("Caught expected exception " + ex);
274 }
275 }
276
277 /** Test bad File resource with no class loader.
278 */
279 public void testGetPMFBadFile() {
280 PersistenceManagerFactory pmf = null;
281 try {
282 pmf = JDOHelper.getPersistenceManagerFactory(new File("Whatever"));
283 fail("Null file should result in JDOFatalUserException");
284 }
285 catch (JDOFatalUserException ex) {
286 if (verbose)
287 println("Caught expected exception " + ex);
288 }
289 }
290
291 /** Test bad File resource with good class loader.
292 */
293 public void testGetPMFBadFileGoodClassLoader() {
294 PersistenceManagerFactory pmf = null;
295 try {
296 pmf = JDOHelper.getPersistenceManagerFactory(new File("Whatever"), this.getClass().getClassLoader());
297 fail("Null file should result in JDOFatalUserException");
298 }
299 catch (JDOFatalUserException ex) {
300 if (verbose)
301 println("Caught expected exception " + ex);
302 }
303 }
304
305 /** Test null JNDI resource name with no class loader.
306 */
307 public void testGetPMFNullJNDI() {
308 PersistenceManagerFactory pmf = null;
309 try {
310 pmf = JDOHelper.getPersistenceManagerFactory((String)null, getInitialContext());
311 fail("Null JNDI resource name should result in JDOFatalUserException");
312 }
313 catch (JDOFatalUserException ex) {
314 if (verbose)
315 println("Caught expected exception " + ex);
316 }
317 }
318
319 /** Test null JNDI resource name with good class loader.
320 */
321 public void testGetPMFNullJNDIGoodClassLoader() {
322 PersistenceManagerFactory pmf = null;
323 try {
324 pmf = JDOHelper.getPersistenceManagerFactory((String)null, getInitialContext(), this.getClass().getClassLoader());
325 fail("Null JNDI resource name should result in JDOFatalUserException");
326 }
327 catch (JDOFatalUserException ex) {
328 if (verbose)
329 println("Caught expected exception " + ex);
330 }
331 }
332
333 /** Test bad JNDI resource name with no class loader.
334 */
335 public void testGetPMFBadJNDI() {
336 PersistenceManagerFactory pmf = null;
337 try {
338 pmf = JDOHelper.getPersistenceManagerFactory("Whatever", getInitialContext());
339 fail("Bad JNDI resource name should result in JDOFatalUserException");
340 }
341 catch (JDOFatalUserException ex) {
342 if (verbose)
343 println("Caught expected exception " + ex);
344 }
345 }
346
347 /** Test bad JNDI resource name with good class loader.
348 */
349 public void testGetPMFBadJNDIGoodClassLoader() {
350 PersistenceManagerFactory pmf = null;
351 try {
352 pmf = JDOHelper.getPersistenceManagerFactory("Whatever", getInitialContext(), this.getClass().getClassLoader());
353 fail("Bad JNDI resource name should result in JDOFatalUserException");
354 }
355 catch (JDOFatalUserException ex) {
356 if (verbose)
357 println("Caught expected exception " + ex);
358 }
359 }
360
361 /** Test null stream with no class loader.
362 */
363 public void testGetPMFNullStream() {
364 PersistenceManagerFactory pmf = null;
365 try {
366 pmf = JDOHelper.getPersistenceManagerFactory((InputStream)null);
367 fail("Null JNDI resource name should result in JDOFatalUserException");
368 }
369 catch (JDOFatalUserException ex) {
370 if (verbose)
371 println("Caught expected exception " + ex);
372 }
373 }
374
375 /** Test null stream with good class loader.
376 */
377 public void testGetPMFNullStreamGoodClassLoader() {
378 PersistenceManagerFactory pmf = null;
379 try {
380 pmf = JDOHelper.getPersistenceManagerFactory((InputStream)null, this.getClass().getClassLoader());
381 fail("Null JNDI resource name should result in JDOFatalUserException");
382 }
383 catch (JDOFatalUserException ex) {
384 if (verbose)
385 println("Caught expected exception " + ex);
386 }
387 }
388
389 /** Test null ClassLoader.
390 */
391 public void testGetPMFNullClassLoader() {
392 PersistenceManagerFactory pmf = null;
393 try {
394 pmf = JDOHelper.getPersistenceManagerFactory
395 ("Whatever", (ClassLoader)null);
396 fail("Null ClassLoader should result in JDOFatalUserException");
397 }
398 catch (JDOFatalUserException ex) {
399 if (verbose)
400 println("Caught expected exception " + ex);
401 }
402 }
403
404 /** Test both null ClassLoaders.
405 */
406 public void testGetPMFBothNullClassLoader() {
407 PersistenceManagerFactory pmf = null;
408 try {
409 pmf = JDOHelper.getPersistenceManagerFactory
410 ("Whatever", (ClassLoader)null, (ClassLoader)null);
411 fail("Null ClassLoader should result in JDOFatalUserException");
412 }
413 catch (JDOFatalUserException ex) {
414 if (verbose)
415 println("Caught expected exception " + ex);
416 }
417 }
418
419 /** Test missing property javax.jdo.PersistenceManagerFactoryClass.
420 */
421 public void testGetPMFNoClassNameProperty() {
422 PersistenceManagerFactory pmf = null;
423 try {
424 pmf = JDOHelper.getPersistenceManagerFactory(new Properties());
425 fail("Missing property PersistenceManagerFactoryClass should result in JDOFatalUserException ");
426 }
427 catch (JDOFatalUserException ex) {
428 if (verbose)
429 println("Caught expected exception " + ex);
430 }
431 }
432
433 /** Test bad PMF class does not exist.
434 */
435 public void testBadPMFClassNotFound() {
436 PersistenceManagerFactory pmf = null;
437 Properties props = new Properties();
438 props.put("javax.jdo.PersistenceManagerFactoryClass",
439 "ThisClassDoesNotExist");
440 try {
441 pmf = JDOHelper.getPersistenceManagerFactory(props);
442 fail("Bad PersistenceManagerFactoryClass should result in JDOFatalUserException ");
443 }
444 catch (JDOFatalUserException ex) {
445 if (verbose)
446 println("Caught expected exception " + ex);
447 }
448 }
449
450 /** Test bad PMF class no method getPersistenceManagerFactory(Properties).
451 */
452 public void testBadPMFNoGetPMFPropertiesMethod() {
453 PersistenceManagerFactory pmf = null;
454 Properties props = new Properties();
455 props.put("javax.jdo.PersistenceManagerFactoryClass",
456 "javax.jdo.JDOHelperTest$BadPMFNoGetPMFMethod");
457 try {
458 pmf = JDOHelper.getPersistenceManagerFactory(props);
459 fail("Bad PersistenceManagerFactory should result in JDOFatalInternalException ");
460 }
461 catch (JDOFatalInternalException ex) {
462 if (ex.getCause() instanceof NoSuchMethodException) {
463 if (verbose)
464 println("Caught expected exception " + ex);
465 } else {
466 fail("Bad PersistenceManagerFactory should result in " +
467 "JDOFatalInternalException with nested " +
468 "NoSuchMethodException. " +
469 "Actual nested exception was " + ex);
470 }
471 }
472 }
473
474 /** Test bad PMF class no method getPersistenceManagerFactory(Map).
475 */
476 public void testBadPMFNoGetPMFMapMethod() {
477 PersistenceManagerFactory pmf = null;
478 Map<String,String> props = new HashMap<String,String>();
479 props.put("javax.jdo.PersistenceManagerFactoryClass",
480 "javax.jdo.JDOHelperTest$BadPMFNoGetPMFMethod");
481 try {
482 pmf = JDOHelper.getPersistenceManagerFactory(props);
483 fail("Bad PersistenceManagerFactory should result in JDOFatalInternalException ");
484 }
485 catch (JDOFatalInternalException ex) {
486 if (ex.getCause() instanceof NoSuchMethodException) {
487 if (verbose)
488 println("Caught expected exception " + ex);
489 } else {
490 fail("Bad PersistenceManagerFactory should result in " +
491 "JDOFatalInternalException with nested " +
492 "NoSuchMethodException. " +
493 "Actual nested exception was " + ex);
494 }
495 }
496 }
497
498 /** Test bad PMF class non-static getPMF method.
499 */
500 public void testBadPMFNonStaticGetPMFMethod() {
501 PersistenceManagerFactory pmf = null;
502 Properties props = new Properties();
503 props.put("javax.jdo.PersistenceManagerFactoryClass",
504 "javax.jdo.JDOHelperTest$BadPMFNonStaticGetPMFMethod");
505 try {
506 pmf = JDOHelper.getPersistenceManagerFactory(props);
507 fail("Bad PersistenceManagerFactoryClass should result in JDOFatalInternalException ");
508 }
509 catch (JDOFatalInternalException ex) {
510 if (verbose)
511 println("Caught expected exception " + ex);
512 }
513 }
514
515 /** Test bad PMF class doesn't implement PMF.
516 */
517 public void testBadPMFWrongReturnType() {
518 PersistenceManagerFactory pmf = null;
519 Properties props = new Properties();
520 props.put("javax.jdo.PersistenceManagerFactoryClass",
521 "javax.jdo.JDOHelperTest$BadPMFWrongReturnType");
522 try {
523 pmf = JDOHelper.getPersistenceManagerFactory(props);
524 fail("Bad PersistenceManagerFactoryClass should result in JDOFatalInternalException ");
525 }
526 catch (JDOFatalInternalException ex) {
527 if (verbose)
528 println("Caught expected exception " + ex);
529 }
530 }
531
532 /** Test bad PMF class getPersistenceManagerFactory throws Exception.
533 */
534 public void testBadPMFGetPMFMethodThrowsJDOException() {
535 PersistenceManagerFactory pmf = null;
536 Properties props = new Properties();
537 props.put("javax.jdo.PersistenceManagerFactoryClass",
538 "javax.jdo.JDOHelperTest$BadPMFGetPMFMethodThrowsJDOException");
539 try {
540 pmf = JDOHelper.getPersistenceManagerFactory(props);
541 fail("BadPMFGetPMFMethodThrowsJDOException.GetPersistenceManagerFactory " +
542 "should result in JDOUnsupportedOptionException. " +
543 "No exception was thrown.");
544 }
545 catch (JDOUnsupportedOptionException ex) {
546 if (verbose)
547 println("Caught expected exception " + ex);
548 }
549 }
550
551 /** Test bad PMF class getPersistenceManagerFactory returns null.
552 */
553 public void testBadPMFGetPMFMethodReturnsNull() {
554 PersistenceManagerFactory pmf = null;
555 Properties props = new Properties();
556 props.put("javax.jdo.PersistenceManagerFactoryClass",
557 "javax.jdo.JDOHelperTest$BadPMFGetPMFMethodReturnsNull");
558 try {
559 pmf = JDOHelper.getPersistenceManagerFactory(props);
560 fail("BadPMFGetPMFMethodReturnsNull.GetPersistenceManagerFactory " +
561 "should result in JDOFatalInternalException. " +
562 "No exception was thrown.");
563 }
564 catch (JDOFatalInternalException ex) {
565 if (verbose)
566 println("Caught expected exception " + ex);
567 }
568 }
569
570 private Context getInitialContext() {
571 try {
572 return new InitialContext();
573 } catch (NamingException ne) {
574 fail("Could not get Initial Context");
575 return null;
576 }
577 }
578
579 public static class BadPMFNoGetPMFMethod {
580 }
581
582 public static class BadPMFNonStaticGetPMFMethod {
583 public PersistenceManagerFactory
584 getPersistenceManagerFactory(Map props) {
585 return null;
586 }
587 }
588
589 public static class BadPMFWrongReturnType {
590 public static BadPMFWrongReturnType
591 getPersistenceManagerFactory(Map props) {
592 return new BadPMFWrongReturnType();
593 }
594 }
595
596 public static class BadPMFGetPMFMethodThrowsJDOException {
597 public static PersistenceManagerFactory
598 getPersistenceManagerFactory(Map props) {
599 throw new JDOUnsupportedOptionException(
600 "GetPMF method throws JDOUnsupportedOptionException");
601 }
602 }
603
604 public static class BadPMFGetPMFMethodThrowsJDOFatalInternalException {
605 public static PersistenceManagerFactory
606 getPersistenceManagerFactory(Map props) {
607 throw new JDOFatalInternalException(
608 "GetPMF method throws JDOFatalInternalException");
609 }
610 }
611
612 public static class BadPMFGetPMFMethodReturnsNull {
613 public static PersistenceManagerFactory
614 getPersistenceManagerFactory(Map props) {
615 return null;
616 }
617 }
618 }