diff --git a/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ProductDerivations.java b/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ProductDerivations.java index c50a3832e..c72f91b26 100644 --- a/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ProductDerivations.java +++ b/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ProductDerivations.java @@ -268,9 +268,10 @@ public class ProductDerivations { } } reportErrors(errs, resource, err); + String rsrc = resource + "#" + anchor; throw (MissingResourceException) JavaVersions.initCause - (new MissingResourceException(resource, - ProductDerivations.class.getName(), resource), err); + (new MissingResourceException(rsrc, + ProductDerivations.class.getName(), rsrc), err); } /** @@ -304,9 +305,10 @@ public class ProductDerivations { String aPath = (String) AccessController.doPrivileged( J2DoPrivHelper.getAbsolutePathAction(file)); reportErrors(errs, aPath, err); + String rsrc = aPath + "#" + anchor; throw (MissingResourceException) JavaVersions.initCause - (new MissingResourceException(aPath, - ProductDerivations.class.getName(), aPath), err); + (new MissingResourceException(rsrc, + ProductDerivations.class.getName(), rsrc), err); } /** diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/lib/conf/TestAnchorParsing.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/lib/conf/TestAnchorParsing.java index 42d82ffc4..0b97ff2d8 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/lib/conf/TestAnchorParsing.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/lib/conf/TestAnchorParsing.java @@ -19,6 +19,12 @@ package org.apache.openjpa.lib.conf; import java.util.List; +import java.util.MissingResourceException; +import java.io.File; +import java.io.InputStream; +import java.io.IOException; +import java.io.FileOutputStream; +import java.io.OutputStream; import junit.framework.TestCase; import org.apache.openjpa.lib.util.Options; @@ -60,4 +66,90 @@ public class TestAnchorParsing extends TestCase { "META-INF/persistence.xml#third-persistence-unit")); assertTrue(locs.contains("META-INF/persistence.xml#invalid")); } + + public void testProductDerivationsLoadResource() { + ProductDerivations.load( + "org/apache/openjpa/lib/conf/product-derivations-load.xml", + "foo", null); + + ProductDerivations.load( + "org/apache/openjpa/lib/conf/product-derivations-load.xml", + null, null); + + try { + ProductDerivations.load( + "org/apache/openjpa/lib/conf/product-derivations-load.xml", + "nonexistant", null); + fail("pu 'nonexistant' does not exist"); + } catch (MissingResourceException mre) { + // expected + } + + try { + ProductDerivations.load( + "org/apache/openjpa/lib/conf/product-derivations-load.xml", + "", null); + fail("pu '' does not exist"); + } catch (MissingResourceException mre) { + // expected + } + } + + public void testNonexistantResourceLoad() { + try { + ProductDerivations.load("nonexistant-resource", null, null); + fail("resource 'nonexistant-resource' should not exist"); + } catch (MissingResourceException e) { + // expected + } + } + + public void testProductDerivationsLoadFile() throws IOException { + File validFile = resourceToTemporaryFile( + "org/apache/openjpa/lib/conf/product-derivations-load.xml"); + + ProductDerivations.load(validFile, "foo", null); + + ProductDerivations.load(validFile, null, null); + + try { + ProductDerivations.load(validFile, "nonexistant", null); + fail("pu 'nonexistant' does not exist"); + } catch (MissingResourceException mre) { + // expected + } + + try { + ProductDerivations.load(validFile, "", null); + fail("pu '' does not exist"); + } catch (MissingResourceException mre) { + // expected + } + } + + public void testNonexistantFileLoad() { + File f = new File("this-should-not-exist"); + assertFalse(f.exists()); + try { + ProductDerivations.load(f, null, null); + fail(f.getName() + " does not exist"); + } catch (MissingResourceException e) { + // expected + } + } + + private File resourceToTemporaryFile(String s) throws IOException { + InputStream in = getClass().getClassLoader().getResourceAsStream(s); + File f = File.createTempFile("TestAnchorParsing", ".xml"); + OutputStream out = new FileOutputStream(f); + byte[] bytes = new byte[1024]; + while (true) { + int count = in.read(bytes); + if (count < 0) + break; + out.write(bytes, 0, count); + } + f.deleteOnExit(); + return f; + } } diff --git a/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/lib/conf/product-derivations-load.xml b/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/lib/conf/product-derivations-load.xml new file mode 100644 index 000000000..bd8927b04 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/lib/conf/product-derivations-load.xml @@ -0,0 +1,8 @@ + + + + \ No newline at end of file