Index: api2/test/java/javax/jdo/JDOHelperConfigTest.java
===================================================================
--- api2/test/java/javax/jdo/JDOHelperConfigTest.java (revision 635377)
+++ api2/test/java/javax/jdo/JDOHelperConfigTest.java (working copy)
@@ -1,568 +1,583 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package javax.jdo;
-
-import junit.framework.TestSuite;
-
-import javax.jdo.util.AbstractTest;
-import javax.jdo.util.BatchTestRunner;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.URLClassLoader;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Random;
-
-/**
- * Tests class javax.jdo.JDOHelper for META-INF/jdoconfig.xml compliance.
- */
-public class JDOHelperConfigTest extends AbstractTest implements Constants {
-
- public static void main(String args[]) {
- BatchTestRunner.run(JDOHelperConfigTest.class);
- }
-
- public static TestSuite suite() {
- return new TestSuite(JDOHelperConfigTest.class);
- }
-
- protected static String JDOCONFIG_CLASSPATH_PREFIX
- = initJDOConfigClasspathPrefix();
-
- protected static String initJDOConfigClasspathPrefix() {
- String basedir = System.getProperty("basedir");
- if (basedir != null) {
- if (!basedir.endsWith("/")) {
- basedir += "/";
- }
- } else {
- basedir = "";
- }
- return basedir + "test/schema/jdoconfig";
- }
-
- protected static Random RANDOM = new Random(System.currentTimeMillis());
-
- protected Map prepareInitialExpectedMap(
- String testVariant,
- int listenerCount,
- int vendorSpecificPropertyCount,
- boolean excludeName,
- boolean excludePUName
- ) {
- Map expected = new HashMap();
-
- if (!excludeName) {
- expected.put(
- PROPERTY_NAME,
- PMF_ATTRIBUTE_NAME + "." + testVariant);
- }
- if (!excludePUName) {
- expected.put(
- PROPERTY_PERSISTENCE_UNIT_NAME,
- PMF_ATTRIBUTE_PERSISTENCE_UNIT_NAME + "." + testVariant);
- }
-
- expected.put(PROPERTY_PERSISTENCE_MANAGER_FACTORY_CLASS,
- PMF_ATTRIBUTE_CLASS + "." + testVariant);
- expected.put(
- PROPERTY_CONNECTION_DRIVER_NAME,
- PMF_ATTRIBUTE_CONNECTION_DRIVER_NAME + "." + testVariant);
- expected.put(
- PROPERTY_CONNECTION_FACTORY_NAME,
- PMF_ATTRIBUTE_CONNECTION_FACTORY_NAME + "." + testVariant);
- expected.put(
- PROPERTY_CONNECTION_FACTORY2_NAME,
- PMF_ATTRIBUTE_CONNECTION_FACTORY2_NAME + "." + testVariant);
- expected.put(
- PROPERTY_CONNECTION_PASSWORD,
- PMF_ATTRIBUTE_CONNECTION_PASSWORD + "." + testVariant);
- expected.put(
- PROPERTY_CONNECTION_URL,
- PMF_ATTRIBUTE_CONNECTION_URL + "." + testVariant);
- expected.put(
- PROPERTY_CONNECTION_USER_NAME,
- PMF_ATTRIBUTE_CONNECTION_USER_NAME + "." + testVariant);
- expected.put(
- PROPERTY_IGNORE_CACHE,
- PMF_ATTRIBUTE_IGNORE_CACHE + "." + testVariant);
- expected.put(
- PROPERTY_MAPPING,
- PMF_ATTRIBUTE_MAPPING + "." + testVariant);
- expected.put(
- PROPERTY_MULTITHREADED,
- PMF_ATTRIBUTE_MULTITHREADED + "." + testVariant);
- expected.put(
- PROPERTY_NONTRANSACTIONAL_READ,
- PMF_ATTRIBUTE_NONTRANSACTIONAL_READ + "." + testVariant);
- expected.put(
- PROPERTY_NONTRANSACTIONAL_WRITE,
- PMF_ATTRIBUTE_NONTRANSACTIONAL_WRITE + "." + testVariant);
- expected.put(
- PROPERTY_OPTIMISTIC,
- PMF_ATTRIBUTE_OPTIMISTIC + "." + testVariant);
- expected.put(
- PROPERTY_RESTORE_VALUES,
- PMF_ATTRIBUTE_RESTORE_VALUES + "." + testVariant);
- expected.put(
- PROPERTY_RETAIN_VALUES,
- PMF_ATTRIBUTE_RETAIN_VALUES + "." + testVariant);
- expected.put(
- PROPERTY_DETACH_ALL_ON_COMMIT,
- PMF_ATTRIBUTE_DETACH_ALL_ON_COMMIT + "." + testVariant);
- expected.put(
- PROPERTY_SERVER_TIME_ZONE_ID,
- PMF_ATTRIBUTE_SERVER_TIME_ZONE_ID + "." + testVariant);
-
- // listeners
- for (int i = 0; i < listenerCount; i++) {
- expected.put(
- PROPERTY_PREFIX_INSTANCE_LIFECYCLE_LISTENER +
- "listener." + testVariant + ".listener" + i,
- "classes." + testVariant + ".classes" + i
- );
- }
-
- // vendor-specific properties
- for (int i = 0; i < vendorSpecificPropertyCount; i++) {
- expected.put(
- "property." + testVariant + ".name" + i,
- "property." + testVariant + ".value" + i
- );
- }
-
- return expected;
- }
-
- static void assertEqualProperties(Map expected, Map actual) {
- Iterator i = expected.entrySet().iterator();
- while (i.hasNext()) {
- Map.Entry entry = (Map.Entry) i.next();
- String key = (String) entry.getKey();
- String expectedValue = (String) entry.getValue();
- String actualValue = (String) actual.get(key);
-
- assertEquals(
- "Actual property at key [" + key + "] with value [" +
- actualValue + "] not equal to expected value [" +
- expectedValue + "]",
- expectedValue,
- actualValue);
- }
- }
-
- protected void doPositiveTest(
- String[] classpaths,
- String testVariantName,
- int listenerCount,
- int vendorSpecificPropertyCount,
- boolean checkEqualProperties)
- throws IOException {
-
- doPositiveTest(
- classpaths,
- testVariantName,
- listenerCount,
- vendorSpecificPropertyCount,
- checkEqualProperties,
- false,
- false);
- }
-
- protected void doPositiveTest(
- String[] classpaths,
- String testVariantName,
- int listenerCount,
- int vendorSpecificPropertyCount,
- boolean checkEqualProperties,
- boolean excludeName,
- boolean excludePUName)
- throws IOException {
-
- URLClassLoader loader = new JDOConfigTestClassLoader(
- JDOCONFIG_CLASSPATH_PREFIX,
- getClass().getClassLoader());
-
- for (int i = 0; i < classpaths.length; i++) {
- ClasspathHelper.addFile(classpaths[i], loader);
- }
-
- Map expected = prepareInitialExpectedMap(
- testVariantName,
- listenerCount,
- vendorSpecificPropertyCount,
- excludeName,
- excludePUName);
-
- String name = testVariantName == null
- ? null
- : (String) expected.get(PROPERTY_NAME);
-
- Map actual = JDOHelper.getNamedPMFProperties(name, loader);
-
- assertNotNull("No properties found", actual);
- if (checkEqualProperties) {
- assertEqualProperties(expected, actual);
- }
- }
-
- public void testPositive00_PMF0_BasicPMFConfigUsingOnlyStandardAttributesAndListeners()
- throws IOException {
- doPositiveTest(
- new String[]{JDOCONFIG_CLASSPATH_PREFIX + "/Positive00"},
- "positive00.pmf0",
- 2,
- 0,
- true);
- }
-
- public void testPositive00_PMF1_BasicPMFConfigUsingOnlyPropertyElementsWithStandardJavaxDotJDOProperties()
- throws IOException {
- doPositiveTest(
- new String[]{JDOCONFIG_CLASSPATH_PREFIX + "/Positive00"},
- "positive00.pmf1",
- 2,
- 0,
- true);
- }
-
- public void testPositive00_PMF2_NestedPropertyElementsWithOnlyStandardAttributeNames()
- throws IOException {
- doPositiveTest(
- new String[]{JDOCONFIG_CLASSPATH_PREFIX + "/Positive00"},
- "positive00.pmf2",
- 2,
- 0,
- true);
- }
-
- public void testPositive00_PMF3_StandardAttributesPlusNonstandardPropertiesInPropertyElements()
- throws IOException {
- doPositiveTest(
- new String[]{JDOCONFIG_CLASSPATH_PREFIX + "/Positive00"},
- "positive00.pmf3",
- 2,
- 2,
- true);
- }
-
- public void testPositive00_PMF4_StandardAttributesPlusNonstandardAttributes()
- throws IOException {
- doPositiveTest(
- new String[]{JDOCONFIG_CLASSPATH_PREFIX + "/Positive00"},
- "positive00.pmf4",
- 0,
- 2,
- true);
- }
-
- public void testPositive01_DuplicatePUsInDifferentConfigFilesButNotRequested()
- throws IOException {
-
- URLClassLoader loader = new JDOConfigTestClassLoader(
- JDOCONFIG_CLASSPATH_PREFIX,
- getClass().getClassLoader());
-
- String[] classpaths = new String[]{
- JDOCONFIG_CLASSPATH_PREFIX + "/Positive01/1a",
- JDOCONFIG_CLASSPATH_PREFIX + "/Positive01/1b"
- };
- for (int i = 0; i < classpaths.length; i++) {
- ClasspathHelper.addFile(classpaths[i], loader);
- }
-
- Map actual = JDOHelper.getNamedPMFProperties(
- ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME, loader);
- }
-
- public void testPositive02_GetAnonymousPMFWithNoProperties()
- throws IOException {
-
- URLClassLoader loader = new JDOConfigTestClassLoader(
- JDOCONFIG_CLASSPATH_PREFIX,
- getClass().getClassLoader());
-
- ClasspathHelper.addFile(
- JDOCONFIG_CLASSPATH_PREFIX + "/Positive02", loader);
-
- Map properties = JDOHelper.getNamedPMFProperties(
- ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME, loader);
- assertNotNull(
- "Anonymous PMF with no properties returned null", properties);
- assertTrue(
- "Anonymous PMF with no properties had properties",
- properties.size() == 0);
- }
-
- public void testPositive03_PMF0_PMFClassNameViaServicesLookup()
- throws IOException {
-
- URLClassLoader loader = new JDOConfigTestClassLoader(
- JDOCONFIG_CLASSPATH_PREFIX,
- getClass().getClassLoader());
- ClasspathHelper.addFile(JDOCONFIG_CLASSPATH_PREFIX + "/Positive03", loader);
-
- String expected = "class.positive03.pmf0";
- String actual = JDOHelper.getPMFClassNameViaServiceLookup(loader);
-
- assertNotNull("No PMF name found via services lookup", actual);
- assertEquals(expected, actual);
- }
-
- public void testPositive04_PMF0_PMFClassNameViaServicesLookup()
- throws IOException {
-
- URLClassLoader loader = new JDOConfigTestClassLoader(
- JDOCONFIG_CLASSPATH_PREFIX,
- getClass().getClassLoader());
- ClasspathHelper.addFile(JDOCONFIG_CLASSPATH_PREFIX + "/Positive04", loader);
-
- String expected = "class.positive04.pmf0";
- String actual = JDOHelper.getPMFClassNameViaServiceLookup(loader);
-
- assertNotNull("No PMF name found via services lookup", actual);
- assertEquals(expected, actual);
- }
-
- public void testPositive05_PMF0_PMFClassNameViaServicesLookup()
- throws IOException {
-
- URLClassLoader loader = new JDOConfigTestClassLoader(
- JDOCONFIG_CLASSPATH_PREFIX,
- getClass().getClassLoader());
- ClasspathHelper.addFile(
- JDOCONFIG_CLASSPATH_PREFIX + "/Positive05", loader);
-
- String expected = "class.positive05.pmf0";
- String actual = JDOHelper.getPMFClassNameViaServiceLookup(loader);
-
- assertNotNull("No PMF name found via services lookup", actual);
- assertEquals(expected, actual);
- }
-
- public void testPositive06_PMF0_GetAnonymousPMFProperties()
- throws IOException {
-
- URLClassLoader loader = new JDOConfigTestClassLoader(
- JDOCONFIG_CLASSPATH_PREFIX,
- getClass().getClassLoader());
-
- ClasspathHelper.addFile(
- JDOCONFIG_CLASSPATH_PREFIX + "/Positive06", loader);
-
- Map expected = prepareInitialExpectedMap(
- "positive06.pmf0", 2, 0, true, true);
-
- Map actual = JDOHelper.getNamedPMFProperties(
- ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME, loader);
-
- assertNotNull("No properties found", actual);
- assertEqualProperties(expected, actual);
- }
-
- public void testPositive07_PMF0_GetAnonymousPMFPropertiesWithPUName()
- throws IOException {
-
- URLClassLoader loader = new JDOConfigTestClassLoader(
- JDOCONFIG_CLASSPATH_PREFIX,
- getClass().getClassLoader());
-
- ClasspathHelper.addFile(
- JDOCONFIG_CLASSPATH_PREFIX + "/Positive07", loader);
-
- Map expected = prepareInitialExpectedMap(
- "positive07.pmf0", 2, 0, true, false);
-
- Map actual = JDOHelper.getNamedPMFProperties(
- ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME, loader);
-
- assertNotNull("No properties found", actual);
- assertEqualProperties(expected, actual);
- }
-
- public void testNegative00_EmptyJDOConfigXML() throws IOException {
- try {
- URLClassLoader loader = new JDOConfigTestClassLoader(
- JDOCONFIG_CLASSPATH_PREFIX,
- getClass().getClassLoader());
- ClasspathHelper.addFile(
- JDOCONFIG_CLASSPATH_PREFIX + "/Negative0", loader);
-
- JDOHelper.getPersistenceManagerFactory(loader);
- fail("JDOHelper failed to throw JDOFatalUserException");
- }
- catch (JDOFatalUserException x) {
- // sunny day
- }
- }
-
- public void testNegative01_NoPersistenceUnitsDefined() throws IOException {
- try {
- URLClassLoader loader = new JDOConfigTestClassLoader(
- JDOCONFIG_CLASSPATH_PREFIX,
- getClass().getClassLoader());
- ClasspathHelper.addFile(
- JDOCONFIG_CLASSPATH_PREFIX + "/Negative01", loader);
-
- JDOHelper.getPersistenceManagerFactory(loader);
- fail("JDOHelper failed to throw JDOFatalUserException");
- }
- catch (JDOFatalUserException x) {
- // joy, sweet joy
- }
- }
-
- public void testNegative02_DuplicateAnonymousPersistenceUnitsInSameConfig()
- throws IOException {
- try {
- URLClassLoader loader = new JDOConfigTestClassLoader(
- JDOCONFIG_CLASSPATH_PREFIX,
- getClass().getClassLoader());
- ClasspathHelper.addFile(
- JDOCONFIG_CLASSPATH_PREFIX + "/Negative02", loader);
-
- JDOHelper.getPersistenceManagerFactory(loader);
- fail("JDOHelper failed to throw JDOFatalUserException");
- }
- catch (JDOFatalUserException x) {
- // the cockles of my heart warmeth
- }
- }
-
- public void testNegative03_DuplicateNamedPersistenceUnitsInSameConfig()
- throws IOException {
- try {
- URLClassLoader loader = new JDOConfigTestClassLoader(
- JDOCONFIG_CLASSPATH_PREFIX,
- getClass().getClassLoader());
- ClasspathHelper.addFile(
- JDOCONFIG_CLASSPATH_PREFIX + "/Negative03", loader);
-
- JDOHelper.getPersistenceManagerFactory(
- "name.negative03",
- loader);
-
- fail("JDOHelper failed to throw JDOFatalUserException");
- }
- catch (JDOFatalUserException x) {
- // warm fuzzies
- }
- }
-
- public void testNegative04_DuplicatePUNamePropertyInAttributeAndElement()
- throws IOException {
- try {
- URLClassLoader loader = new JDOConfigTestClassLoader(
- JDOCONFIG_CLASSPATH_PREFIX,
- getClass().getClassLoader());
- ClasspathHelper.addFile(
- JDOCONFIG_CLASSPATH_PREFIX + "/Negative04", loader);
-
- JDOHelper.getPersistenceManagerFactory(
- "name.negative04.value0",
- loader);
-
- fail("JDOHelper failed to throw JDOFatalUserException");
- }
- catch (JDOFatalUserException x) {
- // no cold pricklies
- }
- }
-
- public void testNegative05_DuplicatePropertyInAttributeAndElement()
- throws IOException {
- try {
- URLClassLoader loader = new JDOConfigTestClassLoader(
- JDOCONFIG_CLASSPATH_PREFIX,
- getClass().getClassLoader());
- ClasspathHelper.addFile(
- JDOCONFIG_CLASSPATH_PREFIX + "/Negative05", loader);
-
- JDOHelper.getPersistenceManagerFactory(loader);
-
- fail("JDOHelper failed to throw JDOFatalUserException");
- }
- catch (JDOFatalUserException x) {
- // party!
- }
- }
-
- public void testNegative06_DuplicatePUInDifferentConfigFiles()
- throws IOException {
- try {
- URLClassLoader loader = new JDOConfigTestClassLoader(
- JDOCONFIG_CLASSPATH_PREFIX,
- getClass().getClassLoader());
- ClasspathHelper.addFile(
- JDOCONFIG_CLASSPATH_PREFIX + "/Negative06/6a", loader);
- ClasspathHelper.addFile(
- JDOCONFIG_CLASSPATH_PREFIX + "/Negative06/6b", loader);
-
- JDOHelper.getPersistenceManagerFactory(
- "name.negative06",
- loader);
-
- fail("JDOHelper failed to throw JDOFatalUserException");
- }
- catch (JDOFatalUserException x) {
- // clear blue sky
- }
- }
-
- public void testNegative07_EmptyServicesFile()
- throws IOException {
- JDOConfigTestClassLoader testLoader = new JDOConfigTestClassLoader(
- new String[]{JDOCONFIG_CLASSPATH_PREFIX},
- getClass().getClassLoader());
- ClasspathHelper.addFile(
- JDOCONFIG_CLASSPATH_PREFIX + "/Negative07", testLoader);
- String shouldBeNull =
- JDOHelper.getPMFClassNameViaServiceLookup(testLoader);
- assertNull(shouldBeNull);
- }
-
- public void testNegative08_NoResourcesFound() {
- String resource = "" + RANDOM.nextLong();
-
- InputStream in =
- getClass().getClassLoader().getResourceAsStream(resource);
- assertNull(in);
-
- // resource pretty much guaranteed not to exist
- try {
- JDOHelper.getPersistenceManagerFactory(resource);
- fail("JDOHelper failed to throw JDOFatalUserException");
- }
- catch (JDOFatalUserException x) {
- // happy path
- }
- }
-
- public void testNegative08_ServicesFileWithOnlyComments()
- throws IOException {
- JDOConfigTestClassLoader testLoader = new JDOConfigTestClassLoader(
- new String[]{JDOCONFIG_CLASSPATH_PREFIX},
- getClass().getClassLoader());
- ClasspathHelper.addFile(
- JDOCONFIG_CLASSPATH_PREFIX + "/Negative08", testLoader);
- String shouldBeNull =
- JDOHelper.getPMFClassNameViaServiceLookup(testLoader);
- assertNull(shouldBeNull);
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package javax.jdo;
+
+import junit.framework.TestSuite;
+
+import javax.jdo.util.AbstractTest;
+import javax.jdo.util.BatchTestRunner;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Random;
+
+/**
+ * Tests class javax.jdo.JDOHelper for META-INF/jdoconfig.xml compliance.
+ */
+public class JDOHelperConfigTest extends AbstractTest implements Constants {
+
+ public static void main(String args[]) {
+ BatchTestRunner.run(JDOHelperConfigTest.class);
+ }
+
+ public static TestSuite suite() {
+ return new TestSuite(JDOHelperConfigTest.class);
+ }
+
+ protected static String JDOCONFIG_CLASSPATH_PREFIX
+ = initJDOConfigClasspathPrefix();
+
+ protected static String initJDOConfigClasspathPrefix() {
+ String basedir = System.getProperty("basedir");
+ if (basedir != null) {
+ if (!basedir.endsWith("/")) {
+ basedir += "/";
+ }
+ } else {
+ basedir = "";
+ }
+ return basedir + "test/schema/jdoconfig";
+ }
+
+ protected static Random RANDOM = new Random(System.currentTimeMillis());
+
+ protected Map prepareInitialExpectedMap(
+ String testVariant,
+ int listenerCount,
+ int vendorSpecificPropertyCount,
+ boolean excludeName,
+ boolean excludePUName
+ ) {
+ Map expected = new HashMap();
+
+ if (!excludeName) {
+ expected.put(
+ PROPERTY_NAME,
+ PMF_ATTRIBUTE_NAME + "." + testVariant);
+ }
+ if (!excludePUName) {
+ expected.put(
+ PROPERTY_PERSISTENCE_UNIT_NAME,
+ PMF_ATTRIBUTE_PERSISTENCE_UNIT_NAME + "." + testVariant);
+ }
+
+ expected.put(PROPERTY_PERSISTENCE_MANAGER_FACTORY_CLASS,
+ PMF_ATTRIBUTE_CLASS + "." + testVariant);
+ expected.put(
+ PROPERTY_CONNECTION_DRIVER_NAME,
+ PMF_ATTRIBUTE_CONNECTION_DRIVER_NAME + "." + testVariant);
+ expected.put(
+ PROPERTY_CONNECTION_FACTORY_NAME,
+ PMF_ATTRIBUTE_CONNECTION_FACTORY_NAME + "." + testVariant);
+ expected.put(
+ PROPERTY_CONNECTION_FACTORY2_NAME,
+ PMF_ATTRIBUTE_CONNECTION_FACTORY2_NAME + "." + testVariant);
+ expected.put(
+ PROPERTY_CONNECTION_PASSWORD,
+ PMF_ATTRIBUTE_CONNECTION_PASSWORD + "." + testVariant);
+ expected.put(
+ PROPERTY_CONNECTION_URL,
+ PMF_ATTRIBUTE_CONNECTION_URL + "." + testVariant);
+ expected.put(
+ PROPERTY_CONNECTION_USER_NAME,
+ PMF_ATTRIBUTE_CONNECTION_USER_NAME + "." + testVariant);
+ expected.put(
+ PROPERTY_IGNORE_CACHE,
+ PMF_ATTRIBUTE_IGNORE_CACHE + "." + testVariant);
+ expected.put(
+ PROPERTY_MAPPING,
+ PMF_ATTRIBUTE_MAPPING + "." + testVariant);
+ expected.put(
+ PROPERTY_MULTITHREADED,
+ PMF_ATTRIBUTE_MULTITHREADED + "." + testVariant);
+ expected.put(
+ PROPERTY_NONTRANSACTIONAL_READ,
+ PMF_ATTRIBUTE_NONTRANSACTIONAL_READ + "." + testVariant);
+ expected.put(
+ PROPERTY_NONTRANSACTIONAL_WRITE,
+ PMF_ATTRIBUTE_NONTRANSACTIONAL_WRITE + "." + testVariant);
+ expected.put(
+ PROPERTY_OPTIMISTIC,
+ PMF_ATTRIBUTE_OPTIMISTIC + "." + testVariant);
+ expected.put(
+ PROPERTY_RESTORE_VALUES,
+ PMF_ATTRIBUTE_RESTORE_VALUES + "." + testVariant);
+ expected.put(
+ PROPERTY_RETAIN_VALUES,
+ PMF_ATTRIBUTE_RETAIN_VALUES + "." + testVariant);
+ expected.put(
+ PROPERTY_DETACH_ALL_ON_COMMIT,
+ PMF_ATTRIBUTE_DETACH_ALL_ON_COMMIT + "." + testVariant);
+ expected.put(
+ PROPERTY_SERVER_TIME_ZONE_ID,
+ PMF_ATTRIBUTE_SERVER_TIME_ZONE_ID + "." + testVariant);
+
+ // listeners
+ for (int i = 0; i < listenerCount; i++) {
+ expected.put(
+ PROPERTY_PREFIX_INSTANCE_LIFECYCLE_LISTENER +
+ "listener." + testVariant + ".listener" + i,
+ "classes." + testVariant + ".classes" + i
+ );
+ }
+
+ // vendor-specific properties
+ for (int i = 0; i < vendorSpecificPropertyCount; i++) {
+ expected.put(
+ "property." + testVariant + ".name" + i,
+ "property." + testVariant + ".value" + i
+ );
+ }
+
+ return expected;
+ }
+
+ static void assertEqualProperties(Map expected, Map actual) {
+ Iterator i = expected.entrySet().iterator();
+ while (i.hasNext()) {
+ Map.Entry entry = (Map.Entry) i.next();
+ String key = (String) entry.getKey();
+ String expectedValue = (String) entry.getValue();
+ String actualValue = (String) actual.get(key);
+
+ assertEquals(
+ "Actual property at key [" + key + "] with value [" +
+ actualValue + "] not equal to expected value [" +
+ expectedValue + "]",
+ expectedValue,
+ actualValue);
+ }
+ }
+
+ protected void doPositiveTest(
+ String[] classpaths,
+ String testVariantName,
+ int listenerCount,
+ int vendorSpecificPropertyCount,
+ boolean checkEqualProperties)
+ throws IOException {
+
+ doPositiveTest(
+ classpaths,
+ testVariantName,
+ listenerCount,
+ vendorSpecificPropertyCount,
+ checkEqualProperties,
+ false,
+ false);
+ }
+
+ protected void doPositiveTest(
+ String[] classpaths,
+ String testVariantName,
+ int listenerCount,
+ int vendorSpecificPropertyCount,
+ boolean checkEqualProperties,
+ boolean excludeName,
+ boolean excludePUName)
+ throws IOException {
+
+ URLClassLoader loader = new JDOConfigTestClassLoader(
+ JDOCONFIG_CLASSPATH_PREFIX,
+ getClass().getClassLoader());
+
+ for (int i = 0; i < classpaths.length; i++) {
+ ClasspathHelper.addFile(classpaths[i], loader);
+ }
+
+ Map expected = prepareInitialExpectedMap(
+ testVariantName,
+ listenerCount,
+ vendorSpecificPropertyCount,
+ excludeName,
+ excludePUName);
+
+ String name = testVariantName == null
+ ? null
+ : (String) expected.get(PROPERTY_NAME);
+
+ Map actual = JDOHelper.getPropertiesFromJdoconfig(name, loader);
+
+ assertNotNull("No properties found", actual);
+ if (checkEqualProperties) {
+ assertEqualProperties(expected, actual);
+ }
+ }
+
+ public void testPositive00_PMF0_BasicPMFConfigUsingOnlyStandardAttributesAndListeners()
+ throws IOException {
+ doPositiveTest(
+ new String[]{JDOCONFIG_CLASSPATH_PREFIX + "/Positive00"},
+ "positive00.pmf0",
+ 2,
+ 0,
+ true);
+ }
+
+ public void testPositive00_PMF1_BasicPMFConfigUsingOnlyPropertyElementsWithStandardJavaxDotJDOProperties()
+ throws IOException {
+ doPositiveTest(
+ new String[]{JDOCONFIG_CLASSPATH_PREFIX + "/Positive00"},
+ "positive00.pmf1",
+ 2,
+ 0,
+ true);
+ }
+
+ public void testPositive00_PMF2_NestedPropertyElementsWithOnlyStandardAttributeNames()
+ throws IOException {
+ doPositiveTest(
+ new String[]{JDOCONFIG_CLASSPATH_PREFIX + "/Positive00"},
+ "positive00.pmf2",
+ 2,
+ 0,
+ true);
+ }
+
+ public void testPositive00_PMF3_StandardAttributesPlusNonstandardPropertiesInPropertyElements()
+ throws IOException {
+ doPositiveTest(
+ new String[]{JDOCONFIG_CLASSPATH_PREFIX + "/Positive00"},
+ "positive00.pmf3",
+ 2,
+ 2,
+ true);
+ }
+
+ public void testPositive00_PMF4_StandardAttributesPlusNonstandardAttributes()
+ throws IOException {
+ doPositiveTest(
+ new String[]{JDOCONFIG_CLASSPATH_PREFIX + "/Positive00"},
+ "positive00.pmf4",
+ 0,
+ 2,
+ true);
+ }
+
+ public void testPositive01_DuplicatePUsInDifferentConfigFilesButNotRequested()
+ throws IOException {
+
+ URLClassLoader loader = new JDOConfigTestClassLoader(
+ JDOCONFIG_CLASSPATH_PREFIX,
+ getClass().getClassLoader());
+
+ String[] classpaths = new String[]{
+ JDOCONFIG_CLASSPATH_PREFIX + "/Positive01/1a",
+ JDOCONFIG_CLASSPATH_PREFIX + "/Positive01/1b"
+ };
+ for (int i = 0; i < classpaths.length; i++) {
+ ClasspathHelper.addFile(classpaths[i], loader);
+ }
+
+ Map actual = JDOHelper.getPropertiesFromJdoconfig(
+ ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME, loader);
+ }
+
+ public void testPositive02_GetAnonymousPMFWithNoProperties()
+ throws IOException {
+
+ URLClassLoader loader = new JDOConfigTestClassLoader(
+ JDOCONFIG_CLASSPATH_PREFIX,
+ getClass().getClassLoader());
+
+ ClasspathHelper.addFile(
+ JDOCONFIG_CLASSPATH_PREFIX + "/Positive02", loader);
+
+ Map properties = JDOHelper.getPropertiesFromJdoconfig(
+ ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME, loader);
+ assertNotNull(
+ "Anonymous PMF with no properties returned null", properties);
+ assertTrue(
+ "Anonymous PMF with no properties had properties",
+ properties.size() == 0);
+ }
+
+ public void testPositive03_PMF0_PMFClassNameViaServicesLookup()
+ throws IOException {
+
+ URLClassLoader loader = new JDOConfigTestClassLoader(
+ JDOCONFIG_CLASSPATH_PREFIX,
+ getClass().getClassLoader());
+ ClasspathHelper.addFile(JDOCONFIG_CLASSPATH_PREFIX + "/Positive03", loader);
+
+ String expected = "class.positive03.pmf0";
+ String actual = getPMFClassNameViaServiceLookup(loader);
+
+ assertNotNull("No PMF name found via services lookup", actual);
+ assertEquals(expected, actual);
+ }
+
+ public void testPositive04_PMF0_PMFClassNameViaServicesLookup()
+ throws IOException {
+
+ URLClassLoader loader = new JDOConfigTestClassLoader(
+ JDOCONFIG_CLASSPATH_PREFIX,
+ getClass().getClassLoader());
+ ClasspathHelper.addFile(JDOCONFIG_CLASSPATH_PREFIX + "/Positive04", loader);
+
+ String expected = "class.positive04.pmf0";
+ String actual = getPMFClassNameViaServiceLookup(loader);
+
+ assertNotNull("No PMF name found via services lookup", actual);
+ assertEquals(expected, actual);
+ }
+
+ public void testPositive05_PMF0_PMFClassNameViaServicesLookup()
+ throws IOException {
+
+ URLClassLoader loader = new JDOConfigTestClassLoader(
+ JDOCONFIG_CLASSPATH_PREFIX,
+ getClass().getClassLoader());
+ ClasspathHelper.addFile(
+ JDOCONFIG_CLASSPATH_PREFIX + "/Positive05", loader);
+
+ String expected = "class.positive05.pmf0";
+ String actual = getPMFClassNameViaServiceLookup(loader);
+
+ assertNotNull("No PMF name found via services lookup", actual);
+ assertEquals(expected, actual);
+ }
+
+ public void testPositive06_PMF0_GetAnonymousPMFProperties()
+ throws IOException {
+
+ URLClassLoader loader = new JDOConfigTestClassLoader(
+ JDOCONFIG_CLASSPATH_PREFIX,
+ getClass().getClassLoader());
+
+ ClasspathHelper.addFile(
+ JDOCONFIG_CLASSPATH_PREFIX + "/Positive06", loader);
+
+ Map expected = prepareInitialExpectedMap(
+ "positive06.pmf0", 2, 0, true, true);
+
+ Map actual = JDOHelper.getPropertiesFromJdoconfig(
+ ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME, loader);
+
+ assertNotNull("No properties found", actual);
+ assertEqualProperties(expected, actual);
+ }
+
+ public void testPositive07_PMF0_GetAnonymousPMFPropertiesWithPUName()
+ throws IOException {
+
+ URLClassLoader loader = new JDOConfigTestClassLoader(
+ JDOCONFIG_CLASSPATH_PREFIX,
+ getClass().getClassLoader());
+
+ ClasspathHelper.addFile(
+ JDOCONFIG_CLASSPATH_PREFIX + "/Positive07", loader);
+
+ Map expected = prepareInitialExpectedMap(
+ "positive07.pmf0", 2, 0, true, false);
+
+ Map actual = JDOHelper.getPropertiesFromJdoconfig(
+ ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME, loader);
+
+ assertNotNull("No properties found", actual);
+ assertEqualProperties(expected, actual);
+ }
+
+ public void testNegative00_EmptyJDOConfigXML() throws IOException {
+ try {
+ URLClassLoader loader = new JDOConfigTestClassLoader(
+ JDOCONFIG_CLASSPATH_PREFIX,
+ getClass().getClassLoader());
+ ClasspathHelper.addFile(
+ JDOCONFIG_CLASSPATH_PREFIX + "/Negative0", loader);
+
+ JDOHelper.getPersistenceManagerFactory(loader);
+ fail("JDOHelper failed to throw JDOFatalUserException");
+ }
+ catch (JDOFatalUserException x) {
+ // sunny day
+ }
+ }
+
+ public void testNegative01_NoPersistenceUnitsDefined() throws IOException {
+ try {
+ URLClassLoader loader = new JDOConfigTestClassLoader(
+ JDOCONFIG_CLASSPATH_PREFIX,
+ getClass().getClassLoader());
+ ClasspathHelper.addFile(
+ JDOCONFIG_CLASSPATH_PREFIX + "/Negative01", loader);
+
+ JDOHelper.getPersistenceManagerFactory(loader);
+ fail("JDOHelper failed to throw JDOFatalUserException");
+ }
+ catch (JDOFatalUserException x) {
+ // joy, sweet joy
+ }
+ }
+
+ public void testNegative02_DuplicateAnonymousPersistenceUnitsInSameConfig()
+ throws IOException {
+ try {
+ URLClassLoader loader = new JDOConfigTestClassLoader(
+ JDOCONFIG_CLASSPATH_PREFIX,
+ getClass().getClassLoader());
+ ClasspathHelper.addFile(
+ JDOCONFIG_CLASSPATH_PREFIX + "/Negative02", loader);
+
+ JDOHelper.getPersistenceManagerFactory(loader);
+ fail("JDOHelper failed to throw JDOFatalUserException");
+ }
+ catch (JDOFatalUserException x) {
+ // the cockles of my heart warmeth
+ }
+ }
+
+ public void testNegative03_DuplicateNamedPersistenceUnitsInSameConfig()
+ throws IOException {
+ try {
+ URLClassLoader loader = new JDOConfigTestClassLoader(
+ JDOCONFIG_CLASSPATH_PREFIX,
+ getClass().getClassLoader());
+ ClasspathHelper.addFile(
+ JDOCONFIG_CLASSPATH_PREFIX + "/Negative03", loader);
+
+ JDOHelper.getPersistenceManagerFactory(
+ "name.negative03",
+ loader);
+
+ fail("JDOHelper failed to throw JDOFatalUserException");
+ }
+ catch (JDOFatalUserException x) {
+ // warm fuzzies
+ }
+ }
+
+ public void testNegative04_DuplicatePUNamePropertyInAttributeAndElement()
+ throws IOException {
+ try {
+ URLClassLoader loader = new JDOConfigTestClassLoader(
+ JDOCONFIG_CLASSPATH_PREFIX,
+ getClass().getClassLoader());
+ ClasspathHelper.addFile(
+ JDOCONFIG_CLASSPATH_PREFIX + "/Negative04", loader);
+
+ JDOHelper.getPersistenceManagerFactory(
+ "name.negative04.value0",
+ loader);
+
+ fail("JDOHelper failed to throw JDOFatalUserException");
+ }
+ catch (JDOFatalUserException x) {
+ // no cold pricklies
+ }
+ }
+
+ public void testNegative05_DuplicatePropertyInAttributeAndElement()
+ throws IOException {
+ try {
+ URLClassLoader loader = new JDOConfigTestClassLoader(
+ JDOCONFIG_CLASSPATH_PREFIX,
+ getClass().getClassLoader());
+ ClasspathHelper.addFile(
+ JDOCONFIG_CLASSPATH_PREFIX + "/Negative05", loader);
+
+ JDOHelper.getPersistenceManagerFactory(loader);
+
+ fail("JDOHelper failed to throw JDOFatalUserException");
+ }
+ catch (JDOFatalUserException x) {
+ // party!
+ }
+ }
+
+ public void testNegative06_DuplicatePUInDifferentConfigFiles()
+ throws IOException {
+ try {
+ URLClassLoader loader = new JDOConfigTestClassLoader(
+ JDOCONFIG_CLASSPATH_PREFIX,
+ getClass().getClassLoader());
+ ClasspathHelper.addFile(
+ JDOCONFIG_CLASSPATH_PREFIX + "/Negative06/6a", loader);
+ ClasspathHelper.addFile(
+ JDOCONFIG_CLASSPATH_PREFIX + "/Negative06/6b", loader);
+
+ JDOHelper.getPersistenceManagerFactory(
+ "name.negative06",
+ loader);
+
+ fail("JDOHelper failed to throw JDOFatalUserException");
+ }
+ catch (JDOFatalUserException x) {
+ // clear blue sky
+ }
+ }
+
+ public void testNegative07_EmptyServicesFile()
+ throws IOException {
+ JDOConfigTestClassLoader testLoader = new JDOConfigTestClassLoader(
+ new String[]{JDOCONFIG_CLASSPATH_PREFIX},
+ getClass().getClassLoader());
+ ClasspathHelper.addFile(
+ JDOCONFIG_CLASSPATH_PREFIX + "/Negative07", testLoader);
+ String shouldBeNull =
+ getPMFClassNameViaServiceLookup(testLoader);
+ assertNull(shouldBeNull);
+ }
+
+ public void testNegative08_NoResourcesFound() {
+ String resource = "" + RANDOM.nextLong();
+
+ InputStream in =
+ getClass().getClassLoader().getResourceAsStream(resource);
+ assertNull(in);
+
+ // resource pretty much guaranteed not to exist
+ try {
+ JDOHelper.getPersistenceManagerFactory(resource);
+ fail("JDOHelper failed to throw JDOFatalUserException");
+ }
+ catch (JDOFatalUserException x) {
+ // happy path
+ }
+ }
+
+ public void testNegative08_ServicesFileWithOnlyComments()
+ throws IOException {
+ JDOConfigTestClassLoader testLoader = new JDOConfigTestClassLoader(
+ new String[]{JDOCONFIG_CLASSPATH_PREFIX},
+ getClass().getClassLoader());
+ ClasspathHelper.addFile(
+ JDOCONFIG_CLASSPATH_PREFIX + "/Negative08", testLoader);
+ String shouldBeNull =
+ getPMFClassNameViaServiceLookup(testLoader);
+ assertNull(shouldBeNull);
+ }
+
+ private String getPMFClassNameViaServiceLookup(ClassLoader loader) {
+ try {
+ Enumeration urls = JDOHelper.getResources(loader,
+ SERVICE_LOOKUP_PMF_RESOURCE_NAME);
+ while (urls.hasMoreElements()) {
+ return JDOHelper.getClassNameFromURL((URL)urls.nextElement());
+ }
+ } catch (Exception ex) {
+ // ignore exceptions from i/o errors
+ }
+ return null;
+ }
+}
Property changes on: api2/test/java/javax/jdo/JDOHelperConfigTest.java
___________________________________________________________________
Name: svn:eol-style
+ LF
Index: api2/src/java/javax/jdo/JDOHelper.java
===================================================================
--- api2/src/java/javax/jdo/JDOHelper.java (revision 635377)
+++ api2/src/java/javax/jdo/JDOHelper.java (working copy)
@@ -67,6 +67,7 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
+import java.util.List;
/**
@@ -660,9 +661,9 @@
* @see #getPersistenceManagerFactory(Map,String,ClassLoader,ClassLoader)
*/
public static PersistenceManagerFactory getPersistenceManagerFactory() {
- ClassLoader cl = getContextClassLoader();
- return getPersistenceManagerFactory
- (null, ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME, cl, cl);
+ ClassLoader cl = getContextClassLoader();
+ return getPersistenceManagerFactory(
+ null, ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME, cl, cl);
}
/** Get the anonymous PersistenceManagerFactory configured via
@@ -674,12 +675,11 @@
* @see #getPersistenceManagerFactory(Map,String,ClassLoader,ClassLoader)
*/
public static PersistenceManagerFactory getPersistenceManagerFactory(
- ClassLoader pmfClassLoader
- ) {
+ ClassLoader pmfClassLoader) {
return getPersistenceManagerFactory(
null,
ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME,
- getContextClassLoader(),
+ pmfClassLoader,
pmfClassLoader);
}
@@ -692,14 +692,34 @@
* @see #getPersistenceManagerFactory(java.util.Map,ClassLoader)
*/
public static PersistenceManagerFactory getPersistenceManagerFactory
- (Map props)
- {
- return getPersistenceManagerFactory(props, getContextClassLoader());
+ (Map props) {
+ return getPersistenceManagerFactory(
+ null, props, getContextClassLoader());
}
+
+ /** Get a PersistenceManagerFactory based on a
+ * Map and a class loader.
+ * This method delegates to the public getPersistenceManagerFactory
+ * method that takes a Map of overrides and a Map of properties,
+ * passing null as the overrides parameter.
+ * @return the PersistenceManagerFactory.
+ * @param props a Map with properties of the
+ * PersistenceManagerFactory.
+ * @param pmfClassLoader the class loader used to load the
+ * PersistenceManagerFactory class
+ * @since 1.0
+ */
+ public static PersistenceManagerFactory getPersistenceManagerFactory
+ (Map props, ClassLoader pmfClassLoader) {
+ return getPersistenceManagerFactory(
+ null, props, pmfClassLoader);
+ }
+
/**
* Get a PersistenceManagerFactory based on a
- * Map instance and a class loader.
+ * Map of overrides, a Map of
+ * properties, and a class loader.
* The following are standard key values:
* "javax.jdo.PersistenceManagerFactoryClass"
*
* and properties of the form
@@ -749,10 +773,11 @@
*
"javax.jdo.option.Optimistic",
@@ -718,6 +738,10 @@
*
"javax.jdo.mapping.Catalog",
*
"javax.jdo.mapping.Schema",
*
"javax.jdo.option.PersistenceUnitName".
+ *
"javax.jdo.option.DetachAllOnCommit".
+ *
"javax.jdo.option.CopyOnAttach".
+ *
"javax.jdo.option.TransactionType".
+ *
"javax.jdo.option.ServerTimeZoneID".
*
"javax.jdo.option.Name".
* PersistenceManagerFactory.
* @param pmfClassLoader the class loader to use to load the
* PersistenceManagerFactory class
+ * @since 2.1
*/
- public static PersistenceManagerFactory getPersistenceManagerFactory
- (Map props, ClassLoader pmfClassLoader)
- {
+ protected static PersistenceManagerFactory getPersistenceManagerFactory
+ (Map overrides, Map props, ClassLoader pmfClassLoader) {
+ List exceptions = new ArrayList();
if (pmfClassLoader == null)
throw new JDOFatalUserException (msg.msg (
"EXC_GetPMFNullLoader")); //NOI18N
@@ -760,104 +785,49 @@
String pmfClassName = (String) props.get (
PROPERTY_PERSISTENCE_MANAGER_FACTORY_CLASS);
- if (pmfClassName == null) {
- // no PMF class name property -- try to find via services lookup
- try {
- pmfClassName = getPMFClassNameViaServiceLookup(pmfClassLoader);
- }
- catch (IOException e) {
- throw new JDOFatalInternalException(msg.msg(
- "EXC_IOExceptionDuringServiceLookup"), e); // NOI18N
- }
- }
+ if (!isNullOrBlank(pmfClassName)) {
+ return invokeGetPersistenceManagerFactoryOnImplementation(
+ pmfClassName, overrides, props, pmfClassLoader);
- if (pmfClassName != null) {
+ } else {
+ // PMF class name null or blank -- try services
+ // for each file META-INF/services/javax.jdo.PersistenceManagerFactory
+ // try to invoke the getPersistenceManagerFactory method of the
+ // implementation class
try {
- Class pmfClass = forName(pmfClassName, true, pmfClassLoader);
- Method pmfMethod = getMethod(pmfClass,
- "getPersistenceManagerFactory", //NOI18N
- new Class[] {Map.class});
- return (PersistenceManagerFactory) invoke(pmfMethod,
- null, new Object[] {props});
- } catch (ClassNotFoundException cnfe) {
- throw new JDOFatalUserException (msg.msg(
- "EXC_GetPMFClassNotFound", pmfClassName), cnfe); //NOI18N
- } catch (IllegalAccessException iae) {
- throw new JDOFatalUserException (msg.msg(
- "EXC_GetPMFIllegalAccess", pmfClassName), iae); //NOI18N
- } catch (NoSuchMethodException nsme) {
- throw new JDOFatalInternalException (msg.msg(
- "EXC_GetPMFNoSuchMethod"), nsme); //NOI18N
- } catch (InvocationTargetException ite) {
- Throwable nested = ite.getTargetException();
- if (nested instanceof JDOException) {
- throw (JDOException)nested;
- } else throw new JDOFatalInternalException (msg.msg(
- "EXC_GetPMFUnexpectedException"), ite); //NOI18N
- } catch (NullPointerException e) {
- throw new JDOFatalInternalException (msg.msg(
- "EXC_GetPMFNullPointerException", pmfClassName), e); //NOI18N
- } catch (ClassCastException e) {
- throw new JDOFatalInternalException (msg.msg(
- "EXC_GetPMFClassCastException", pmfClassName), e); //NOI18N
- } catch (Exception e) {
- throw new JDOFatalInternalException (msg.msg(
- "EXC_GetPMFUnexpectedException"), e); //NOI18N
+ Enumeration urls = getResources(pmfClassLoader,
+ SERVICE_LOOKUP_PMF_RESOURCE_NAME);
+ while (urls.hasMoreElements()) {
+ pmfClassName = getClassNameFromURL((URL)urls.nextElement());
+ return invokeGetPersistenceManagerFactoryOnImplementation(
+ pmfClassName, overrides, props, pmfClassLoader);
+ }
+ } catch (Throwable ex) {
+ // remember exceptions from failed pmf invocations
+ exceptions.add(ex);
}
}
-
- // else no PMF class name given; check PU name
- String puName = (String) props.get(PROPERTY_PERSISTENCE_UNIT_NAME);
- if (puName != null && !"".equals(puName.trim())) {
- PersistenceManagerFactory pmf =
- getPMFFromEMF(puName, props, pmfClassLoader);
-
- if (pmf != null) {
- return pmf;
- }
- // else pmf was null, which means that
- // either javax.persistence.Persistence wasn't on classpath
- // or there was no PU found with the given name
- throw new JDOFatalUserException(msg.msg(
- "EXC_JavaxPersistencePersistenceAbsentOrNoPUFound"),
- puName);
- }
- // else no PMF class name or PU name
+ // no PMF class name and no services
+
throw new JDOFatalUserException(msg.msg(
- "EXC_GetPMFNoPMFClassNamePropertyOrPUNameProperty"));
+ "EXC_GetPMFNoPMFClassNamePropertyOrPUNameProperty"),
+ exceptions.toArray(new Throwable[exceptions.size()]));
}
- /**
- * Looks up a (@link PersistenceManagerFactory} implementation class name
- * from the resource
- * META-INF/services/javax.jdo.PersistenceManagerFactory,
- * which should be a text file whose
- * only contents are the fully qualified name of a class that implements
- * {@link PersistenceManagerFactory}.
- * Note that the method
- * {@link java.lang.ClassLoader#getResourceAsStream(String)} is used to
- * find the resource.
- * Only the first implementation class name found in the resource is used.
- * @param cl The ClassLoader used to find the resource.
- * @return The first non-blank, trimmed line found in the file; should be
- * a PMF implementation class name, or null
- * if no content was found.
- * @throws IOException
+ /** Get a class name from a URL. The URL is from getResources with
+ * e.g. META-INF/services/javax.jdo.PersistenceManagerFactory as the
+ * parameter. Parse the file, removing blank lines, comment lines,
+ * and comments.
+ * @param url the URL of the services file
+ * @return the name of the class contained in the file
+ * @throws java.io.IOException
+ * @since 2.1
*/
- protected static String getPMFClassNameViaServiceLookup(ClassLoader cl)
- throws IOException
- {
- /*
- Note: not using sun.misc.Service because it's not a standard part of
- the JDK (yet).
- */
- InputStream is = getResourceAsStream(cl,
- SERVICE_LOOKUP_PMF_RESOURCE_NAME);
- if (is == null) {
- return null;
- }
+ protected static String getClassNameFromURL (URL url)
+ throws IOException {
+ InputStream is = openStream(url);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = null;
try {
@@ -995,191 +965,172 @@
Map overrides,
String name,
ClassLoader resourceLoader,
- ClassLoader pmfLoader
- ) {
+ ClassLoader pmfLoader) {
if (pmfLoader == null)
throw new JDOFatalUserException (msg.msg (
"EXC_GetPMFNullPMFLoader")); //NOI18N
-
- Properties props = null;
- InputStream in = null;
- if (name != null
- && !ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME.equals(
- (name = name.trim())))
- {
- // then try to load resources from properties file
- if (resourceLoader == null)
- throw new JDOFatalUserException (msg.msg (
- "EXC_GetPMFNullPropsLoader")); //NOI18N
- try {
- in = getResourceAsStream(resourceLoader, name);
- if (in != null) {
- // then some kind of resource was found by the given name;
- // assume that it's a properties file and proceed as usual
- props = new Properties();
- props.load(in);
- // JDO 2.1:
- // add the SPI property to inform the implementation that
- // the PMF was configured by the given resource name
- // and not via named PMF for proper deserialization
- props.put(PROPERTY_SPI_RESOURCE_NAME, name);
- props.remove(PROPERTY_NAME);
- }
- }
- catch (IOException ioe) {
- throw new JDOFatalUserException (msg.msg (
- "EXC_GetPMFIOExceptionRsrc", name), ioe); //NOI18N
- }
- finally {
- if (in != null)
- try {
- in.close ();
- } catch (IOException ioe) { }
- }
+ if (resourceLoader == null) {
+ throw new JDOFatalUserException(msg.msg(
+ "EXC_GetPMFNullPropsLoader")); //NOI18N
}
- // JDO 2.1: else name was null, empty string, or no resource found by
- // given name; first see if name represents name of named PMF
-
- // as a convenience in public usage, convert null to anonymous PMF name
- if (name == null) {
- name = ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME;
+ Map props = null;
+ // trim spaces from name and ensure non-null
+ name = (name == null?ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME:name.trim());
+ if (!ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME.equals(name)) {
+ props = loadPropertiesFromResource(resourceLoader, name);
}
- Map properties = props;
- if (properties == null) {
- properties = getNamedPMFProperties(name, pmfLoader);
- if (properties != null) {
- // inform the impl that the config came from a jdoconfig.xml
- // element with the given name
- properties.put(PROPERTY_NAME, name);
- properties.remove(PROPERTY_SPI_RESOURCE_NAME);
- }
+ if (props != null) {
+ // add the SPI property to inform the implementation that
+ // the PMF was configured by the given resource name
+ // and not via named PMF for proper deserialization
+ props.put(PROPERTY_SPI_RESOURCE_NAME, name);
+ props.remove(PROPERTY_NAME);
+ return getPersistenceManagerFactory(overrides, props, pmfLoader);
}
+ // props were null; try getting from jdoconfig.xml
+ props = getPropertiesFromJdoconfig(name, pmfLoader);
+ if (props != null) {
+ // inform the impl that the config came from a jdoconfig.xml
+ // element with the given name
+ props.put(PROPERTY_NAME, name);
+ props.remove(PROPERTY_SPI_RESOURCE_NAME);
+ // we have loaded a Properties, delegate to implementation
+ return getPersistenceManagerFactory(overrides, props, pmfLoader);
+ }
+ // no properties found; last try to see if name is a JPA PU name
+ if (!ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME.equals(name)) {
+ props = new Properties();
+ props.put(PROPERTY_PERSISTENCE_UNIT_NAME, name);
+ return getPersistenceManagerFactory(overrides, props, pmfLoader);
+ }
+
+ // no PMF found; give up
+ throw new JDOFatalUserException (msg.msg (
+ "EXC_NoPMFConfigurableViaPropertiesOrXML", name)); //NOI18N
+ }
- if (properties != null) {
- // try to resolve PMF class name
- String pmfClassName = null;
+ /** Invoke the getPersistenceManagerFactory method on the implementation.
+ * If the overrides parameter to this method is not null, the static method
+ * with Map overrides, Map properties parameters will be invoked.
+ * If the overrides parameter to this method is null, the static method
+ * with Map properties parameter will be invoked.
+ * @param pmfClassName the name of the implementation factory class
+ * @param overrides a Map of overrides
+ * @param properties a Map of properties
+ * @param cl the class loader to use to load the implementation class
+ * @return the PersistenceManagerFactory
+ */
+ protected static PersistenceManagerFactory
+ invokeGetPersistenceManagerFactoryOnImplementation(
+ String pmfClassName, Map overrides, Map properties, ClassLoader cl) {
+ if (overrides != null) {
+ // overrides is not null; use getPersistenceManagerFactory(Map overrides, Map props)
try {
- pmfClassName = resolvePMFClassName(
- overrides, properties, pmfLoader);
- } catch (IOException ioe) {
- throw new JDOFatalUserException(msg.msg(
- "EXC_IOExceptionDuringServiceLookup"), ioe); //NOI18N
- }
+ Class implClass = forName(pmfClassName, true, cl);
+ Method m = getMethod(implClass,
+ "getPersistenceManagerFactory",
+ new Class[]{Map.class, Map.class});
+ return (PersistenceManagerFactory) invoke(m,
+ null, new Object[]{overrides, properties});
- if (pmfClassName == null) {
+ } catch (ClassNotFoundException e) {
throw new JDOFatalUserException(msg.msg(
- "EXC_GetPMFNoPMFClassNamePropertyOrPUNameProperty"));
+ "EXC_GetPMFClassNotFound", pmfClassName), e);
+ } catch (NoSuchMethodException e) {
+ throw new JDOFatalInternalException(msg.msg(
+ "EXC_GetPMFNoSuchMethod2", pmfClassName), e);
+ } catch (NullPointerException e) {
+ throw new JDOFatalInternalException (msg.msg(
+ "EXC_GetPMFNullPointerException", pmfClassName), e); //NOI18N
+ } catch (IllegalAccessException e) {
+ throw new JDOFatalUserException(msg.msg(
+ "EXC_GetPMFIllegalAccess", pmfClassName), e);
+ } catch (ClassCastException e) {
+ throw new JDOFatalInternalException (msg.msg(
+ "EXC_GetPMFClassCastException", pmfClassName), e); //NOI18N
+ } catch (InvocationTargetException ite) {
+ Throwable nested = ite.getTargetException();
+ if (nested instanceof JDOException) {
+ throw (JDOException)nested;
+ } else throw new JDOFatalInternalException (msg.msg(
+ "EXC_GetPMFUnexpectedException"), ite); //NOI18N
}
-
+ } else {
+ // overrides is null; use getPersistenceManagerFactory(Map props)
try {
- return invokeGetPersistenceManagerFactoryOnImplementation(
- pmfClassName, overrides, properties, pmfLoader);
+ Class implClass = forName(pmfClassName, true, cl);
+ Method m = getMethod(implClass,
+ "getPersistenceManagerFactory",
+ new Class[]{Map.class});
+ return (PersistenceManagerFactory) invoke(m,
+ null, new Object[]{properties});
} catch (ClassNotFoundException e) {
throw new JDOFatalUserException(msg.msg(
"EXC_GetPMFClassNotFound", pmfClassName), e);
} catch (NoSuchMethodException e) {
- throw new JDOFatalUserException(msg.msg(
+ throw new JDOFatalInternalException(msg.msg(
"EXC_GetPMFNoSuchMethod2", pmfClassName), e);
+ } catch (NullPointerException e) {
+ throw new JDOFatalInternalException (msg.msg(
+ "EXC_GetPMFNullPointerException", pmfClassName), e); //NOI18N
} catch (IllegalAccessException e) {
throw new JDOFatalUserException(msg.msg(
"EXC_GetPMFIllegalAccess", pmfClassName), e);
- } catch (InvocationTargetException e) {
- throw new JDOFatalUserException(msg.msg(
- "EXC_GetPMFInvocationTargetException", pmfClassName), e);
+ } catch (ClassCastException e) {
+ throw new JDOFatalInternalException (msg.msg(
+ "EXC_GetPMFClassCastException", pmfClassName), e); //NOI18N
+ } catch (InvocationTargetException ite) {
+ Throwable nested = ite.getTargetException();
+ if (nested instanceof JDOException) {
+ throw (JDOException)nested;
+ } else throw new JDOFatalInternalException (msg.msg(
+ "EXC_GetPMFUnexpectedException"), ite); //NOI18N
}
}
-
- // else no properties found; next, assume name is a JPA PU name
- if (!ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME.equals(name)) {
- PersistenceManagerFactory pmf =
- getPMFFromEMF(name, overrides, pmfLoader);
- if (pmf != null) {
- return pmf;
- }
- }
-
- // else no PMF found
- throw new JDOFatalUserException (msg.msg (
- "EXC_NoPMFConfigurableViaPropertiesOrXML",
- name)); //NOI18N
}
- protected static PersistenceManagerFactory
- invokeGetPersistenceManagerFactoryOnImplementation(
- String pmfClassName, Map overrides, Map properties, ClassLoader cl)
- throws
- ClassNotFoundException,
- NoSuchMethodException,
- IllegalAccessException,
- InvocationTargetException
- {
- Class implClass = forName(pmfClassName, true, cl);
- Method m = getMethod(implClass,
- "getPersistenceManagerFactory",
- new Class[] { Map.class, Map.class });
-
- return (PersistenceManagerFactory) invoke(m,
- null, new Object[] {overrides, properties});
- }
-
-
- /**
- * Tries to get the PMF class name property in the following locations
- * in this order:
PROPERY_PERSISTENCE_MANAGER_FACTORY_CLASS in
- * parameter "overrides"PROPERY_PERSISTENCE_MANAGER_FACTORY_CLASS in
- * parameter "properties"META-INF/services/javax.jdo.PeristenceManagerFactoryjavax.persistence.EntityManagerFactory
- * via the javax.persistence.Persistence method
- * createEntityManagerFactory(String)
- * and casts it to a {@link PersistenceManagerFactory}. It is a user error
- * if his chosen JPA vendor's
- * javax.persistence.EntityManagerFactory
- * implementation class does not also implement
- * {@link PersistenceManagerFactory}.
- * @param name The persistence unit name.
- * @param overrides The overriding properties of the persistence unit.
- * @param loader The classloader used to attempt loading of the class
- * javax.persistence.Persistence
- * and javax.persistence.PersistenceException.
- * @return The EntityManagerFactory, cast as a
- * PersistenceManagerFactory for the given
- * persistence unit name, or null if any of the following
- * conditions are true.
- * javax.persistence.Persistence or
- * javax.persistence.Persistence fails for any
- * reason.javax.persistence.Persistence method
- * createEntityManagerFactory(String)
- * cannot be invoked.javax.persistence.EntityManagerFactory cannot be
- * cast to a
- * {@link PersistenceManagerFactory}.