Got all tests passing.

git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@426658 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
A. Abram White 2006-07-28 19:58:44 +00:00
parent 1a6d7d547c
commit 604703c6d1
6 changed files with 102 additions and 22 deletions

View File

@ -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;
}
}

View File

@ -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();
}

View File

@ -51,7 +51,7 @@ public class TestDocTypeReader extends TestCase {
public void setUp() {
StringBuffer docType = new StringBuffer();
docType.append("<!DOCTYPE foo [\n");
docType.append("\t<!ELEMENT foo(bar)>\n");
docType.append("\t<!ELEMENT foo (bar)>\n");
docType.append("\t<!ELEMENT bar EMPTY>\n");
docType.append("\t<!ATTLIST bar attr CDATA \"value\">\n");
docType.append("\t<!ATTLIST bar attr2 CDATA \"value2\">\n");

View File

@ -0,0 +1 @@
org.apache.openjpa.lib.conf.test.ConfigurationTestConfigurationProvider

View File

@ -1,5 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<root attr1="1" attr2="2">
<list/>
<list>

View File

@ -0,0 +1 @@
openjpa.testKey=testvalue