From 604703c6d1e9c9920eaa09932d027007acd37617 Mon Sep 17 00:00:00 2001 From: "A. Abram White" Date: Fri, 28 Jul 2006 19:58:44 +0000 Subject: [PATCH] Got all tests passing. git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@426658 13f79535-47bb-0310-9956-ffa450edef68 --- ...onfigurationTestConfigurationProvider.java | 88 +++++++++++++++++++ .../lib/conf/test/TestConfigurationImpl.java | 31 +++---- .../openjpa/lib/xml/TestDocTypeReader.java | 2 +- ...che.openjpa.lib.conf.ConfigurationProvider | 1 + .../openjpa/lib/xml/formatted-result.xml | 1 - .../src/test/resources/test.properties | 1 + 6 files changed, 102 insertions(+), 22 deletions(-) create mode 100644 openjpa-lib/src/test/java/org/apache/openjpa/lib/conf/test/ConfigurationTestConfigurationProvider.java create mode 100644 openjpa-lib/src/test/resources/META-INF/services/org.apache.openjpa.lib.conf.ConfigurationProvider create mode 100755 openjpa-lib/src/test/resources/test.properties diff --git a/openjpa-lib/src/test/java/org/apache/openjpa/lib/conf/test/ConfigurationTestConfigurationProvider.java b/openjpa-lib/src/test/java/org/apache/openjpa/lib/conf/test/ConfigurationTestConfigurationProvider.java new file mode 100644 index 000000000..5770b12c7 --- /dev/null +++ b/openjpa-lib/src/test/java/org/apache/openjpa/lib/conf/test/ConfigurationTestConfigurationProvider.java @@ -0,0 +1,88 @@ +package org.apache.openjpa.lib.conf.test; + +import java.io.File; +import java.io.InputStream; +import java.io.IOException; +import java.net.URL; +import java.util.MissingResourceException; +import java.util.Properties; + +import org.apache.openjpa.lib.conf.MapConfigurationProvider; + +/** + * Configuration provider used in testing. + * + * @author Abe White + */ +public class ConfigurationTestConfigurationProvider + extends MapConfigurationProvider { + + public ConfigurationTestConfigurationProvider() { + super(null); + } + + public boolean loadDefaults(ClassLoader loader) + throws IOException { + return load(null, loader); + } + + public boolean load(String rsrc, ClassLoader loader) + throws IOException { + if (rsrc == null) + rsrc = System.getProperty("openjpatest.properties"); + if (rsrc == null || !rsrc.endsWith(".properties")) + return false; + + URL url = findResource(rsrc, loader); + if (url == null) + throw new MissingResourceException(rsrc, getClass().getName(), + rsrc); + + InputStream in = url.openStream(); + Properties props = new Properties(); + if (in != null) { + try { + props.load(in); + addProperties(props); + return true; + } finally { + try { + in.close(); + } catch (Exception e) { + } + } + } + return false; + } + + /** + * Locate the given resource. + */ + private URL findResource(String rsrc, ClassLoader loader) + throws IOException { + if (loader != null) + return loader.getResource(rsrc); + + // in jbuilder the classloader can be null + URL url = null; + loader = getClass().getClassLoader(); + if (loader != null) + url = loader.getResource(rsrc); + if (url == null) { + loader = Thread.currentThread().getContextClassLoader(); + if (loader != null) + url = loader.getResource(rsrc); + } + if (url == null) { + loader = ClassLoader.getSystemClassLoader(); + if (loader != null) + url = loader.getResource(rsrc); + } + return url; + } + + public boolean load(File file) + throws IOException { + return false; + } +} diff --git a/openjpa-lib/src/test/java/org/apache/openjpa/lib/conf/test/TestConfigurationImpl.java b/openjpa-lib/src/test/java/org/apache/openjpa/lib/conf/test/TestConfigurationImpl.java index 88dcf12c9..5033413f4 100644 --- a/openjpa-lib/src/test/java/org/apache/openjpa/lib/conf/test/TestConfigurationImpl.java +++ b/openjpa-lib/src/test/java/org/apache/openjpa/lib/conf/test/TestConfigurationImpl.java @@ -43,24 +43,19 @@ public class TestConfigurationImpl extends AbstractTestCase { } public void setUp() { - _def = System.getProperty("openjpa.properties"); - System.setProperty("openjpa.properties", "test.properties"); + System.setProperty("openjpatest.properties", "test.properties"); } public void tearDown() throws Exception { - if (_def != null) - System.setProperty("openjpa.properties", _def); - + System.setProperty("openjpatest.properties", ""); super.tearDown(); } /** * Test that default properties are found and loaded. - * ### This test method requires some sort of ConfigurationProvider - * ### to be available in the openjpa-lib module, which is not the case. */ public void testDefaults() { - System.setProperty("sysKey", "sysvalue"); + System.setProperty("openjpa.sysKey", "sysvalue"); assertNull(_conf.getTestKey()); assertNull(_conf.getSysKey()); assertNull(_conf.getPluginKey()); @@ -74,7 +69,7 @@ public class TestConfigurationImpl extends AbstractTestCase { // override the properties location to a non-existant value _conf.setTestKey(null); _conf.setSysKey(null); - System.setProperty("openjpa.properties", "foo.properties"); + System.setProperty("openjpatest.properties", "foo.properties"); try { assertTrue(!_conf.loadDefaults()); fail("Should have thrown exception for missing resource."); @@ -82,8 +77,8 @@ public class TestConfigurationImpl extends AbstractTestCase { } // set back for remainder of tests - System.setProperty("openjpa.properties", "test.properties"); - System.setProperty("pluginKey", "java.lang.Object"); + System.setProperty("openjpatest.properties", "test.properties"); + System.setProperty("openjpa.pluginKey", "java.lang.Object"); assertTrue(_conf.loadDefaults()); assertEquals("testvalue", _conf.getTestKey()); assertEquals("sysvalue", _conf.getSysKey()); @@ -99,15 +94,15 @@ public class TestConfigurationImpl extends AbstractTestCase { assertTrue(_conf.loadDefaults()); assertEquals("testvalue", _conf.getTestKey()); Map props = _conf.toProperties(false); - assertEquals("testvalue", props.get("testKey")); - assertFalse(props.containsKey("objectKey")); + assertEquals("testvalue", props.get("openjpa.testKey")); + assertFalse(props.containsKey("openjpa.objectKey")); _conf.setTestKey("foo"); _conf.setPluginKey(new Object()); _conf.setObjectKey(new Object()); props = _conf.toProperties(false); - assertEquals("foo", props.get("testKey")); - assertEquals("java.lang.Object", props.get("pluginKey")); - assertFalse(props.containsKey("objectKey")); + assertEquals("foo", props.get("openjpa.testKey")); + assertEquals("java.lang.Object", props.get("openjpa.pluginKey")); + assertFalse(props.containsKey("openjpa.objectKey")); } /** @@ -263,10 +258,6 @@ public class TestConfigurationImpl extends AbstractTestCase { _objectKey = addObject("objectKey"); } - public String getProductName() { - return "test"; - } - public String getTestKey() { return _testKey.get(); } diff --git a/openjpa-lib/src/test/java/org/apache/openjpa/lib/xml/TestDocTypeReader.java b/openjpa-lib/src/test/java/org/apache/openjpa/lib/xml/TestDocTypeReader.java index b9ab07518..21fff2124 100644 --- a/openjpa-lib/src/test/java/org/apache/openjpa/lib/xml/TestDocTypeReader.java +++ b/openjpa-lib/src/test/java/org/apache/openjpa/lib/xml/TestDocTypeReader.java @@ -51,7 +51,7 @@ public class TestDocTypeReader extends TestCase { public void setUp() { StringBuffer docType = new StringBuffer(); docType.append("\n"); + docType.append("\t\n"); docType.append("\t\n"); docType.append("\t\n"); docType.append("\t\n"); diff --git a/openjpa-lib/src/test/resources/META-INF/services/org.apache.openjpa.lib.conf.ConfigurationProvider b/openjpa-lib/src/test/resources/META-INF/services/org.apache.openjpa.lib.conf.ConfigurationProvider new file mode 100644 index 000000000..902d0f8c4 --- /dev/null +++ b/openjpa-lib/src/test/resources/META-INF/services/org.apache.openjpa.lib.conf.ConfigurationProvider @@ -0,0 +1 @@ +org.apache.openjpa.lib.conf.test.ConfigurationTestConfigurationProvider diff --git a/openjpa-lib/src/test/resources/org/apache/openjpa/lib/xml/formatted-result.xml b/openjpa-lib/src/test/resources/org/apache/openjpa/lib/xml/formatted-result.xml index 74f3837e8..aeadb30e2 100644 --- a/openjpa-lib/src/test/resources/org/apache/openjpa/lib/xml/formatted-result.xml +++ b/openjpa-lib/src/test/resources/org/apache/openjpa/lib/xml/formatted-result.xml @@ -1,5 +1,4 @@ - diff --git a/openjpa-lib/src/test/resources/test.properties b/openjpa-lib/src/test/resources/test.properties new file mode 100755 index 000000000..910218599 --- /dev/null +++ b/openjpa-lib/src/test/resources/test.properties @@ -0,0 +1 @@ +openjpa.testKey=testvalue