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