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