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.ByteArrayInputStream;
20 import java.io.ByteArrayOutputStream;
21 import java.net.MalformedURLException;
22
23 import javax.jdo.util.AbstractTest;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.net.URL;
27 import java.net.URLClassLoader;
28 import java.util.Properties;
29 import junit.framework.TestSuite;
30
31 import javax.jdo.util.BatchTestRunner;
32
33 /**
34 *
35 * Tests class javax.jdo.JDOHelper for calls to the impl's static method
36 * getPersistenceManagerFactory(Map overrides, Map props).
37 *
38 */
39 public class PMFMapMapTest extends AbstractTest implements Constants {
40
41 static String expectedDriverName = "Jane Doe";
42 static String expectedDriverName4NamedPMF = "Larry";
43 static String expectedDriverNameWithOverrides = "Gerard Manley Hopkins";
44 static String PMFName = "BookSearch";
45 static String resourceDir = "Pmfmapmap01";
46 static String propsDir = "Pmfmapmap02";
47 static String pmfServiceClass = "javax.jdo.PMFService";
48 static String propertiesFile = "propsfile.props";
49 PersistenceManagerFactory pmf;
50 Properties props;
51 Properties overrides;
52 URLClassLoader resourceClassLoader;
53 ClassLoader saveContextClassLoader;
54
55 public static void main(String args[]) {
56 BatchTestRunner.run(PMFMapMapTest.class);
57 }
58
59 /**
60 * {@inheritDoc}
61 * @return {@inheritDoc}
62 */
63 public static TestSuite suite() {
64 return new TestSuite(PMFMapMapTest.class);
65 }
66
67 public void setup() {
68 }
69
70 public void teardown() {
71 }
72
73 void setupResourceClassLoader(String dir) {
74 try {
75 URL url = new URL(
76 "file://" + JDOCONFIG_CLASSPATH_PREFIX + "/" + dir + "/");
77 resourceClassLoader = new URLClassLoader(new URL[]{url},
78 getClass().getClassLoader());
79 } catch (MalformedURLException ex) {
80 ex.printStackTrace();
81 }
82 }
83
84 /**
85 * A class path prefix used in the various tests where the class path
86 * needs to be set.
87 */
88 protected static String JDOCONFIG_CLASSPATH_PREFIX = initJDOConfigClasspathPrefix();
89
90 /**
91 * Returns the JDO configuration class path prefix's default value, which is
92 * the project base directory suffixed by the path to the configuration
93 * directory (<tt>test/schema/jdoconfig</tt>).
94 *
95 * @return the default class path prefix used by this test suite.
96 *
97 */
98 protected static String initJDOConfigClasspathPrefix() {
99 return initBasedir() + "test/schema/jdoconfig";
100 }
101
102 /**
103 * Returns the base directory for this project. This base directory
104 * is used to build up the other class paths defined in this test suite.
105 * The value returned is the value returned by
106 * <code>System.getProperty("basedir")</code>.
107 * A trailing slash is appended to the path if it doesn't exist.
108 *
109 * @return the default base directory of the project.
110 */
111 protected static String initBasedir() {
112 String basedir = System.getProperty("basedir");
113 if (basedir != null) {
114 if (!basedir.endsWith("/")) {
115 basedir += "/";
116 }
117 } else {
118 basedir = "";
119 }
120 return basedir;
121 }
122
123
124
125
126
127
128
129 public void testJDOConfigXML() {
130
131 setupResourceClassLoader(resourceDir);
132 Thread.currentThread().setContextClassLoader(resourceClassLoader);
133
134 try {
135 pmf = JDOHelper.getPersistenceManagerFactory();
136 } catch (JDOFatalUserException ex) {
137 fail("Failed to find PersistenceManagerFactoryClass." + ex.getMessage());
138 }
139
140 String driverName = pmf.getConnectionDriverName();
141 if (!expectedDriverName.equals(driverName)) {
142 fail("Bad ConnectionDriverName(): " + driverName + ". Expected: \""
143 + expectedDriverName + "\"");
144 }
145 }
146
147
148
149
150
151
152
153
154 public void testJDOConfigXMLWithLoader() throws IOException {
155
156 setupResourceClassLoader(resourceDir);
157
158 try {
159 pmf = JDOHelper.getPersistenceManagerFactory(resourceClassLoader);
160 } catch (JDOFatalUserException ex) {
161 fail("Failed to find PersistenceManagerFactoryClass." + ex.getMessage());
162 }
163
164 String driverName = pmf.getConnectionDriverName();
165 if (!expectedDriverName.equals(driverName)) {
166 fail("Bad ConnectionDriverName(): " + driverName + ". Expected: \""
167 + expectedDriverName + "\"");
168 }
169 }
170
171
172
173
174
175
176
177 public void testPropsFile() {
178
179 setupResourceClassLoader(propsDir);
180 Thread.currentThread().setContextClassLoader(resourceClassLoader);
181
182 try {
183 pmf = JDOHelper.getPersistenceManagerFactory(propertiesFile);
184 } catch (JDOFatalUserException ex) {
185 fail("Failed to find PersistenceManagerFactoryClass." + ex.getMessage());
186 }
187
188 String driverName = pmf.getConnectionDriverName();
189 if (!expectedDriverName.equals(driverName)) {
190 fail("Bad ConnectionDriverName(): " + driverName + ". Expected: \""
191 + expectedDriverName + "\"");
192 }
193 }
194
195
196
197
198
199
200
201
202 public void testPropsFileAndLoader() {
203
204 setupResourceClassLoader(propsDir);
205
206 try {
207 pmf = JDOHelper.getPersistenceManagerFactory(propertiesFile,
208 resourceClassLoader);
209 } catch (JDOFatalUserException ex) {
210 fail("Failed to find PersistenceManagerFactoryClass." + ex.getMessage());
211 }
212
213 String driverName = pmf.getConnectionDriverName();
214 if (!expectedDriverName.equals(driverName)) {
215 fail("Bad ConnectionDriverName(): " + driverName + ". Expected: \""
216 + expectedDriverName + "\"");
217 }
218 }
219
220
221
222
223
224
225
226 public void testInputStream() {
227 props = new Properties();
228 props.setProperty(PROPERTY_PERSISTENCE_MANAGER_FACTORY_CLASS,
229 pmfServiceClass);
230 props.setProperty(PROPERTY_CONNECTION_DRIVER_NAME,
231 expectedDriverName);
232
233 ByteArrayOutputStream outstream = new ByteArrayOutputStream();
234 try {
235 props.store(outstream, "");
236 } catch (IOException ex) {
237 fail(ex.getMessage());
238 }
239 InputStream byteArrayInputStream =
240 new ByteArrayInputStream(outstream.toByteArray());
241
242 setupResourceClassLoader(resourceDir);
243 Thread.currentThread().setContextClassLoader(resourceClassLoader);
244
245 try {
246 pmf = JDOHelper.getPersistenceManagerFactory(byteArrayInputStream);
247 } catch (JDOFatalUserException ex) {
248 fail("Failed to find PersistenceManagerFactoryClass." + ex.getMessage());
249 }
250
251 String driverName = pmf.getConnectionDriverName();
252 if (!expectedDriverName.equals(driverName)) {
253 fail("Bad ConnectionDriverName(): " + driverName + ". Expected: \""
254 + expectedDriverName + "\"");
255 }
256 }
257
258
259
260
261
262
263
264 public void testInputStreamWithLoader() {
265 props = new Properties();
266 props.setProperty(PROPERTY_PERSISTENCE_MANAGER_FACTORY_CLASS,
267 pmfServiceClass);
268 props.setProperty(PROPERTY_CONNECTION_DRIVER_NAME,
269 expectedDriverName);
270
271 ByteArrayOutputStream outstream = new ByteArrayOutputStream();
272 try {
273 props.store(outstream, "");
274 } catch (IOException ex) {
275 fail(ex.getMessage());
276 }
277 InputStream byteArrayInputStream =
278 new ByteArrayInputStream(outstream.toByteArray());
279
280 setupResourceClassLoader(resourceDir);
281
282 try {
283 pmf = JDOHelper.getPersistenceManagerFactory(byteArrayInputStream,
284 resourceClassLoader);
285 } catch (JDOFatalUserException ex) {
286 fail("Failed to find PersistenceManagerFactoryClass." + ex.getMessage());
287 }
288
289 String driverName = pmf.getConnectionDriverName();
290 if (!expectedDriverName.equals(driverName)) {
291 fail("Bad ConnectionDriverName(): " + driverName + ". Expected: \""
292 + expectedDriverName + "\"");
293 }
294 }
295
296
297
298
299
300
301
302
303 public void testProperties() {
304 props = new Properties();
305 props.setProperty(PROPERTY_PERSISTENCE_MANAGER_FACTORY_CLASS,
306 pmfServiceClass);
307 props.setProperty(PROPERTY_CONNECTION_DRIVER_NAME,
308 expectedDriverName);
309
310 setupResourceClassLoader(resourceDir);
311 Thread.currentThread().setContextClassLoader(resourceClassLoader);
312 try {
313 pmf = JDOHelper.getPersistenceManagerFactory(props);
314 } catch (JDOFatalUserException ex) {
315 fail("Failed to find PersistenceManagerFactoryClass." + ex.getMessage());
316 }
317
318 String driverName = pmf.getConnectionDriverName();
319 if (!expectedDriverName.equals(driverName)) {
320 fail("Bad ConnectionDriverName(): " + driverName + ". Expected: \""
321 + expectedDriverName + "\"");
322 }
323 }
324
325
326
327
328
329
330 public void testPropertiesAndLoader() {
331 props = new Properties();
332 props.setProperty(PROPERTY_PERSISTENCE_MANAGER_FACTORY_CLASS,
333 pmfServiceClass);
334 props.setProperty(PROPERTY_CONNECTION_DRIVER_NAME,
335 expectedDriverName);
336
337 setupResourceClassLoader(resourceDir);
338 try {
339 pmf = JDOHelper.getPersistenceManagerFactory(props,
340 resourceClassLoader);
341 } catch (JDOFatalUserException ex) {
342 fail("Failed to find PersistenceManagerFactoryClass." + ex.getMessage());
343 }
344
345 String driverName = pmf.getConnectionDriverName();
346 if (!expectedDriverName.equals(driverName)) {
347 fail("Bad ConnectionDriverName(): " + driverName + ". Expected: \""
348 + expectedDriverName + "\"");
349 }
350 }
351
352
353
354
355
356
357 public void testNamedPMFWithOverrides() {
358 overrides = new Properties();
359 overrides.setProperty(PROPERTY_CONNECTION_DRIVER_NAME,
360 expectedDriverNameWithOverrides);
361
362 setupResourceClassLoader(resourceDir);
363 Thread.currentThread().setContextClassLoader(resourceClassLoader);
364
365 try {
366 pmf = JDOHelper.getPersistenceManagerFactory(overrides, PMFName);
367 } catch (JDOFatalUserException ex) {
368 fail("Failed to find PersistenceManagerFactoryClass." + ex.getMessage());
369 }
370
371 String driverName = pmf.getConnectionDriverName();
372 if (!expectedDriverNameWithOverrides.equals(driverName)) {
373 fail("Bad ConnectionDriverName(): " + driverName + ". Expected: \""
374 + expectedDriverNameWithOverrides + "\"");
375 }
376 }
377
378
379
380
381
382
383
384 public void testNamedPMFWithOverridesAndLoader() {
385 overrides = new Properties();
386 overrides.setProperty(PROPERTY_CONNECTION_DRIVER_NAME,
387 expectedDriverNameWithOverrides);
388
389 setupResourceClassLoader(resourceDir);
390
391 try {
392 pmf = JDOHelper.getPersistenceManagerFactory(overrides, PMFName,
393 resourceClassLoader);
394 } catch (JDOFatalUserException ex) {
395 fail("Failed to find PersistenceManagerFactoryClass." + ex.getMessage());
396 }
397
398 String driverName = pmf.getConnectionDriverName();
399 if (!expectedDriverNameWithOverrides.equals(driverName)) {
400 fail("Bad ConnectionDriverName(): " + driverName + ". Expected: \""
401 + expectedDriverNameWithOverrides + "\"");
402 }
403 }
404
405
406
407
408
409
410
411
412
413
414
415 public void testNamedPMFWithOverridesAndTwoLoaders() {
416 overrides = new Properties();
417 overrides.setProperty(PROPERTY_CONNECTION_DRIVER_NAME,
418 expectedDriverNameWithOverrides);
419
420 setupResourceClassLoader(resourceDir);
421
422 try {
423 pmf = JDOHelper.getPersistenceManagerFactory(overrides, PMFName,
424 resourceClassLoader,
425 Thread.currentThread().getContextClassLoader());
426 } catch (JDOFatalUserException ex) {
427 fail("Failed to find PersistenceManagerFactoryClass. " + ex.getMessage());
428 }
429
430 String driverName = pmf.getConnectionDriverName();
431 if (!expectedDriverNameWithOverrides.equals(driverName)) {
432 fail("Bad ConnectionDriverName(): " + driverName + ". Expected: \""
433 + expectedDriverNameWithOverrides + "\"");
434 }
435 }
436
437
438
439
440
441
442 public void testNamedPMF() {
443
444 setupResourceClassLoader(resourceDir);
445 Thread.currentThread().setContextClassLoader(resourceClassLoader);
446
447 try {
448 pmf = JDOHelper.getPersistenceManagerFactory(PMFName);
449 } catch (JDOFatalUserException ex) {
450 fail("Failed to find PersistenceManagerFactoryClass. " + ex.getMessage());
451 }
452
453 String driverName = pmf.getConnectionDriverName();
454 if (!expectedDriverName4NamedPMF.equals(driverName)) {
455 fail("Bad ConnectionDriverName(): " + driverName + ". Expected: \""
456 + expectedDriverName4NamedPMF + "\"");
457 }
458 }
459
460
461
462
463
464
465 public void testNamedPMFWithLoader() {
466
467 setupResourceClassLoader(resourceDir);
468
469 try {
470 pmf = JDOHelper.getPersistenceManagerFactory(PMFName,
471 resourceClassLoader);
472 } catch (JDOFatalUserException ex) {
473 fail("Failed to find PersistenceManagerFactoryClass. " + ex.getMessage());
474 }
475
476 String driverName = pmf.getConnectionDriverName();
477 if (!expectedDriverName4NamedPMF.equals(driverName)) {
478 fail("Bad ConnectionDriverName(): " + driverName + ". Expected: \""
479 + expectedDriverName4NamedPMF + "\"");
480 }
481 }
482
483
484
485
486
487
488
489 public void testNamedPMFWithTwoLoaders() {
490
491 setupResourceClassLoader(resourceDir);
492
493 try {
494 pmf = JDOHelper.getPersistenceManagerFactory(PMFName,
495 resourceClassLoader,
496 Thread.currentThread().getContextClassLoader());
497 } catch (JDOFatalUserException ex) {
498 fail("Failed to find PersistenceManagerFactoryClass. " + ex.getMessage());
499 }
500
501 String driverName = pmf.getConnectionDriverName();
502 if (!expectedDriverName4NamedPMF.equals(driverName)) {
503 fail("Bad ConnectionDriverName(): " + driverName + ". Expected: \""
504 + expectedDriverName4NamedPMF + "\"");
505 }
506 }
507 }