1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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       * static PersistenceManagerFactory getPersistenceManagerFactory() Get the
89       * anonymous PersistenceManagerFactory configured via the standard
90       * configuration file resource "META-INF/jdoconfig.xml", using the current
91       * thread's context class loader to locate the configuration file
92       * resource(s).
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      * static PersistenceManagerFactory getPersistenceManagerFactory
115      * (java.lang.ClassLoader pmfClassLoader) Get the anonymous
116      * PersistenceManagerFactory configured via the standard configuration file
117      * resource "META-INF/jdoconfig.xml", using the given class loader.
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      * static PersistenceManagerFactory getPersistenceManagerFactory(
139      * java.io.File propsFile) Returns a PersistenceManagerFactory configured
140      * based on the properties stored in the file at propsFile.
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      * static PersistenceManagerFactory getPersistenceManagerFactory(
163      * java.io.File propsFile, java.lang.ClassLoader loader) Returns a
164      * PersistenceManagerFactory configured based on the properties stored in
165      * the file at propsFile.
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      * static PersistenceManagerFactory getPersistenceManagerFactory(
188      * java.io.InputStream stream) Returns a PersistenceManagerFactory
189      * configured based on the Properties stored in the input stream at stream.
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      * static PersistenceManagerFactory getPersistenceManagerFactory(
225      * java.io.InputStream stream, java.lang.ClassLoader loader) Returns a
226      * PersistenceManagerFactory configured based on the Properties stored in
227      * the input stream at stream.
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      * static PersistenceManagerFactory getPersistenceManagerFactory(
263      * java.util.Map<?,?> props) Get a PersistenceManagerFactory based on a
264      * Properties instance, using the current thread's context class loader to
265      * locate the PersistenceManagerFactory class.
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      * static PersistenceManagerFactory getPersistenceManagerFactory(
291      * java.util.Map<?,?> props, java.lang.ClassLoader pmfClassLoader) Get a
292      * PersistenceManagerFactory based on a Map and a class loader.
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      * static PersistenceManagerFactory getPersistenceManagerFactory
318      * (java.util.Map<?,?> overrides, java.lang.String name) Returns a named
319      * PersistenceManagerFactory or persistence unit.
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      * static PersistenceManagerFactory getPersistenceManagerFactory(
346      * java.util.Map<?,?> overrides, java.lang.String name,
347      * java.lang.ClassLoader resourceLoader) Returns a named
348      * PersistenceManagerFactory or persistence unit.
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      * static PersistenceManagerFactory getPersistenceManagerFactory(
375      * java.util.Map<?,?> overrides, java.lang.String name,
376      * java.lang.ClassLoader resourceLoader, java.lang.ClassLoader pmfLoader)
377      * Returns a PersistenceManagerFactory configured based on the properties
378      * stored in the resource at name, or, if not found, returns a
379      * PersistenceManagerFactory with the given name or, if not found, returns a
380      * javax.persistence.EntityManagerFactory cast to a
381      * PersistenceManagerFactory.
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      * static PersistenceManagerFactory getPersistenceManagerFactory(
409      * java.lang.String name) Returns a named PersistenceManagerFactory or
410      * persistence unit.
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      * static PersistenceManagerFactory getPersistenceManagerFactory
433      * (java.lang.String name, java.lang.ClassLoader loader) Returns a named
434      * PersistenceManagerFactory or persistence unit.
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      * static PersistenceManagerFactory getPersistenceManagerFactory(
457      * java.lang.String name, java.lang.ClassLoader resourceLoader,
458      * java.lang.ClassLoader pmfLoader) Returns a named
459      * PersistenceManagerFactory or persistence unit.
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 }