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.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     /* static PersistenceManagerFactory getPersistenceManagerFactory()
124     Get the anonymous PersistenceManagerFactory configured via the
125     standard configuration file resource "META-INF/jdoconfig.xml",
126     using the current thread's context class loader to locate the
127     configuration file resource(s).
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     static PersistenceManagerFactory getPersistenceManagerFactory
149     (java.lang.ClassLoader pmfClassLoader)
150     Get the anonymous PersistenceManagerFactory configured via the
151     standard configuration file resource "META-INF/jdoconfig.xml",
152     using the given class loader.
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     static PersistenceManagerFactory getPersistenceManagerFactory(
173     java.io.File propsFile)
174     Returns a PersistenceManagerFactory configured based on the properties
175     stored in the file at propsFile.
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     static PersistenceManagerFactory getPersistenceManagerFactory(
198     java.io.File propsFile, java.lang.ClassLoader loader)
199     Returns a PersistenceManagerFactory configured based on the properties
200     stored in the file at propsFile.
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     static PersistenceManagerFactory getPersistenceManagerFactory(
222     java.io.InputStream stream)
223     Returns a PersistenceManagerFactory configured based on the Properties
224     stored in the input stream at stream.
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     static PersistenceManagerFactory getPersistenceManagerFactory(
260     java.io.InputStream stream, java.lang.ClassLoader loader)
261     Returns a PersistenceManagerFactory configured based on the Properties
262     stored in the input stream at stream.
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     static PersistenceManagerFactory getPersistenceManagerFactory(
298     java.util.Map<?,?> props)
299     Get a PersistenceManagerFactory based on a Properties instance,
300     using the current thread's context class loader to locate
301     the PersistenceManagerFactory class.
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     static PersistenceManagerFactory getPersistenceManagerFactory(
327     java.util.Map<?,?> props, java.lang.ClassLoader pmfClassLoader)
328     Get a PersistenceManagerFactory based on a Map and a class loader.
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     static PersistenceManagerFactory getPersistenceManagerFactory
354     (java.util.Map<?,?> overrides, java.lang.String name)
355     Returns a named PersistenceManagerFactory or persistence unit.
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     static PersistenceManagerFactory getPersistenceManagerFactory(
380     java.util.Map<?,?> overrides, java.lang.String name,
381     java.lang.ClassLoader resourceLoader)
382     Returns a named PersistenceManagerFactory or persistence unit.
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     static PersistenceManagerFactory getPersistenceManagerFactory(
407     java.util.Map<?,?> overrides, java.lang.String name,
408     java.lang.ClassLoader resourceLoader, java.lang.ClassLoader pmfLoader)
409     Returns a PersistenceManagerFactory configured based on the properties
410     stored in the resource at name, or, if not found,
411     returns a PersistenceManagerFactory with the given name or,
412     if not found, returns a javax.persistence.EntityManagerFactory
413     cast to a PersistenceManagerFactory.
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     static PersistenceManagerFactory getPersistenceManagerFactory(
439     java.lang.String name)
440     Returns a named PersistenceManagerFactory or persistence unit.
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     static PersistenceManagerFactory getPersistenceManagerFactory
462     (java.lang.String name, java.lang.ClassLoader loader)
463     Returns a named PersistenceManagerFactory or persistence unit.
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     static PersistenceManagerFactory getPersistenceManagerFactory(
485     java.lang.String name, java.lang.ClassLoader resourceLoader,
486     java.lang.ClassLoader pmfLoader)
487     Returns a named PersistenceManagerFactory or persistence unit.
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 }