oheger 2004/12/23 10:42:25
Modified: configuration/src/test/org/apache/commons/configuration
TestHierarchicalConfigurationXMLReader.java
TestXMLConfiguration.java
TestHierarchicalConfiguration.java
TestHierarchicalXMLConfiguration.java
configuration/conf test.xml
configuration/src/java/org/apache/commons/configuration
HierarchicalConfiguration.java
ConfigurationFactory.java ConfigurationKey.java
Log:
Replaced HierarchicalXMLConfiguration with XMLConfiguration and adapted
affected classes
Revision Changes Path
1.5 +3 -3
jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestHierarchicalConfigurationXMLReader.java
Index: TestHierarchicalConfigurationXMLReader.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestHierarchicalConfigurationXMLReader.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- TestHierarchicalConfigurationXMLReader.java 12 Jul 2004 12:14:38
-0000 1.4
+++ TestHierarchicalConfigurationXMLReader.java 23 Dec 2004 18:42:25
-0000 1.5
@@ -39,8 +39,8 @@
protected void setUp() throws Exception
{
- HierarchicalXMLConfiguration config =
- new HierarchicalXMLConfiguration();
+ XMLConfiguration config =
+ new XMLConfiguration();
config.setFileName(TEST_FILE);
config.load();
parser = new HierarchicalConfigurationXMLReader(config);
1.16 +32 -64
jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestXMLConfiguration.java
Index: TestXMLConfiguration.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestXMLConfiguration.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- TestXMLConfiguration.java 18 Oct 2004 21:38:45 -0000 1.15
+++ TestXMLConfiguration.java 23 Dec 2004 18:42:25 -0000 1.16
@@ -22,7 +22,6 @@
import java.util.List;
import junit.framework.TestCase;
-import junitx.framework.ArrayAssert;
/**
* test for loading and saving xml properties files
@@ -40,7 +39,9 @@
protected void setUp() throws Exception
{
- conf = new XMLConfiguration(new File(testProperties));
+ conf = new XMLConfiguration();
+ conf.setFile(new File(testProperties));
+ conf.load();
}
public void testGetProperty()
@@ -64,94 +65,94 @@
String key = "clearly";
conf.clearProperty(key);
assertNull(key, conf.getProperty(key));
- assertNull(key, conf.getXmlProperty(key));
+ assertNull(key, conf.getProperty(key));
// test single element
conf.load();
key = "clear.element";
conf.clearProperty(key);
assertNull(key, conf.getProperty(key));
- assertNull(key, conf.getXmlProperty(key));
+ assertNull(key, conf.getProperty(key));
// test single element with attribute
conf.load();
key = "clear.element2";
conf.clearProperty(key);
assertNull(key, conf.getProperty(key));
- assertNull(key, conf.getXmlProperty(key));
+ assertNull(key, conf.getProperty(key));
key = "[EMAIL PROTECTED]";
assertNotNull(key, conf.getProperty(key));
- assertNotNull(key, conf.getXmlProperty(key));
+ assertNotNull(key, conf.getProperty(key));
// test non-text/cdata element
conf.load();
key = "clear.comment";
conf.clearProperty(key);
assertNull(key, conf.getProperty(key));
- assertNull(key, conf.getXmlProperty(key));
+ assertNull(key, conf.getProperty(key));
// test cdata element
conf.load();
key = "clear.cdata";
conf.clearProperty(key);
assertNull(key, conf.getProperty(key));
- assertNull(key, conf.getXmlProperty(key));
+ assertNull(key, conf.getProperty(key));
// test multiple sibling elements
conf.load();
key = "clear.list.item";
conf.clearProperty(key);
assertNull(key, conf.getProperty(key));
- assertNull(key, conf.getXmlProperty(key));
+ assertNull(key, conf.getProperty(key));
key = "[EMAIL PROTECTED]";
assertNotNull(key, conf.getProperty(key));
- assertNotNull(key, conf.getXmlProperty(key));
+ assertNotNull(key, conf.getProperty(key));
// test multiple, disjoined elements
conf.load();
key = "list.item";
conf.clearProperty(key);
assertNull(key, conf.getProperty(key));
- assertNull(key, conf.getXmlProperty(key));
+ assertNull(key, conf.getProperty(key));
}
- public void testGetXmlProperty() {
+ public void testgetProperty() {
// test non-leaf element
- Object property = conf.getXmlProperty("clear");
+ Object property = conf.getProperty("clear");
assertNull(property);
// test non-existent element
- property = conf.getXmlProperty("e");
+ property = conf.getProperty("e");
assertNull(property);
// test non-existent element
- property = conf.getXmlProperty("[EMAIL PROTECTED]");
+ property = conf.getProperty("[EMAIL PROTECTED]");
assertNull(property);
// test single element
- property = conf.getXmlProperty("element");
+ property = conf.getProperty("element");
assertNotNull(property);
assertTrue(property instanceof String);
assertEquals("value", property);
// test single attribute
- property = conf.getXmlProperty("[EMAIL PROTECTED]");
+ property = conf.getProperty("[EMAIL PROTECTED]");
assertNotNull(property);
assertTrue(property instanceof String);
assertEquals("foo", property);
// test non-text/cdata element
- property = conf.getXmlProperty("test.comment");
+ property = conf.getProperty("test.comment");
assertNull(property);
// test cdata element
- property = conf.getXmlProperty("test.cdata");
+ property = conf.getProperty("test.cdata");
assertNotNull(property);
assertTrue(property instanceof String);
assertEquals("<cdata value>", property);
// test multiple sibling elements
- property = conf.getXmlProperty("list.sublist.item");
+ property = conf.getProperty("list.sublist.item");
assertNotNull(property);
assertTrue(property instanceof List);
List list = (List)property;
@@ -160,7 +161,7 @@
assertEquals("six", list.get(1));
// test multiple, disjoined elements
- property = conf.getXmlProperty("list.item");
+ property = conf.getProperty("list.item");
assertNotNull(property);
assertTrue(property instanceof List);
list = (List)property;
@@ -171,7 +172,7 @@
assertEquals("four", list.get(3));
// test multiple, disjoined attributes
- property = conf.getXmlProperty("[EMAIL PROTECTED]");
+ property = conf.getProperty("[EMAIL PROTECTED]");
assertNotNull(property);
assertTrue(property instanceof List);
list = (List)property;
@@ -191,29 +192,29 @@
String key = "[EMAIL PROTECTED]";
conf.clearProperty(key);
assertNull(key, conf.getProperty(key));
- assertNull(key, conf.getXmlProperty(key));
+ assertNull(key, conf.getProperty(key));
// test single attribute
conf.load();
key = "[EMAIL PROTECTED]";
- Object p = conf.getXmlProperty(key);
- p = conf.getXmlProperty("clear.element2");
+ Object p = conf.getProperty(key);
+ p = conf.getProperty("clear.element2");
conf.clearProperty(key);
assertNull(key, conf.getProperty(key));
- assertNull(key, conf.getXmlProperty(key));
+ assertNull(key, conf.getProperty(key));
key = "clear.element2";
assertNotNull(key, conf.getProperty(key));
- assertNotNull(key, conf.getXmlProperty(key));
+ assertNotNull(key, conf.getProperty(key));
// test multiple, disjoined attributes
conf.load();
key = "[EMAIL PROTECTED]";
conf.clearProperty(key);
assertNull(key, conf.getProperty(key));
- assertNull(key, conf.getXmlProperty(key));
+ assertNull(key, conf.getProperty(key));
key = "clear.list.item";
assertNotNull(key, conf.getProperty(key));
- assertNotNull(key, conf.getXmlProperty(key));
+ assertNotNull(key, conf.getProperty(key));
}
public void testSetAttribute()
@@ -317,7 +318,7 @@
conf.setProperty("element.string", "hello");
assertEquals("'element.string'", "hello",
conf.getString("element.string"));
- assertEquals("XML value of element.string", "hello",
conf.getXmlProperty("element.string"));
+ assertEquals("XML value of element.string", "hello",
conf.getProperty("element.string"));
}
public void testAddProperty()
@@ -381,38 +382,5 @@
// reload the configuration
XMLConfiguration conf2 = new XMLConfiguration(conf.getFile());
assertEquals("'autosave' property", "ok",
conf2.getString("autosave"));
- }
-
- public void testParseElementsNames()
- {
- // without attribute
- String key = "x.y.z";
- String[] array = new String[] {"x", "y", "z"};
- ArrayAssert.assertEquals("key without attribute", array,
XMLConfiguration.parseElementNames(key));
-
- // with attribute
- key = "[EMAIL PROTECTED]";
- ArrayAssert.assertEquals("key with attribute", array,
XMLConfiguration.parseElementNames(key));
-
- // null key
- ArrayAssert.assertEquals("null key", new String[] {},
XMLConfiguration.parseElementNames(null));
- }
-
- public void testParseAttributeName()
- {
- // no attribute
- String key = "x.y.z";
- assertEquals("no attribute", null,
XMLConfiguration.parseAttributeName(key));
-
- // simple attribute
- key = "[EMAIL PROTECTED]";
- assertEquals("simple attribute", "name",
XMLConfiguration.parseAttributeName(key));
-
- // missing end marker
- key = "[EMAIL PROTECTED]";
- assertEquals("missing end marker", "name",
XMLConfiguration.parseAttributeName(key));
-
- // null key
- assertEquals("null key", null,
XMLConfiguration.parseAttributeName(null));
}
}
1.9 +3 -3
jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java
Index: TestHierarchicalConfiguration.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- TestHierarchicalConfiguration.java 13 Dec 2004 16:40:14 -0000
1.8
+++ TestHierarchicalConfiguration.java 23 Dec 2004 18:42:25 -0000
1.9
@@ -216,7 +216,7 @@
checkKeys("tables.table(1)", new String[] { "name",
"fields.field.name" });
checkKeys("tables.table(0)",
- new String[] { "name", "fields.field.name", "[EMAIL
PROTECTED]", "size", "fields.field.type", "fields.field.size" });
+ new String[] { "name", "fields.field.name",
"tables.table(0)[EMAIL PROTECTED]", "size", "fields.field.type",
"fields.field.size" });
checkKeys("connections.connection(0).param",
new String[] {"url", "user", "pwd" });
checkKeys("connections.connection(1).param",
@@ -322,7 +322,7 @@
Set values = new HashSet();
for(int i = 0; i < expected.length; i++)
{
- values.add(prefix + "." + expected[i]);
+ values.add((expected[i].startsWith(prefix)) ? expected[i] :
prefix + "." + expected[i]);
}
Iterator itKeys = config.getKeys(prefix);
1.6 +36 -8
jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestHierarchicalXMLConfiguration.java
Index: TestHierarchicalXMLConfiguration.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestHierarchicalXMLConfiguration.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- TestHierarchicalXMLConfiguration.java 18 Dec 2004 16:33:03 -0000
1.5
+++ TestHierarchicalXMLConfiguration.java 23 Dec 2004 18:42:25 -0000
1.6
@@ -23,10 +23,13 @@
import java.util.Iterator;
/**
- * Test class for HierarchicalXMLConfiguration,
+ * Test class for XMLConfiguration. In addition to TestXMLConfiguration this
+ * class especially tests the hierarchical nature of this class and
structured
+ * data access.
*
* @author Emmanuel Bourg
* @author Mark Woodman
+ * @author Oliver Heger
* @version $Id$
*/
public class TestHierarchicalXMLConfiguration extends TestCase
@@ -56,15 +59,15 @@
private static final String TEST_SAVE = "target" + File.separator +
TEST_SAVENAME;
/** Instance config used for tests. */
- private HierarchicalXMLConfiguration config;
+ private XMLConfiguration config;
/** Fixture setup. */
protected void setUp() throws Exception
{
- config = new HierarchicalXMLConfiguration();
+ config = new XMLConfiguration();
}
- private void configTest(HierarchicalXMLConfiguration config)
+ private void configTest(XMLConfiguration config)
{
assertEquals(1, config.getMaxIndex("tables.table"));
assertEquals("system", config.getProperty("tables.table(0)[EMAIL
PROTECTED]"));
@@ -169,7 +172,7 @@
File saveFile = new File(TEST_SAVE);
config.save(saveFile);
- config = new HierarchicalXMLConfiguration();
+ config = new XMLConfiguration();
config.load(saveFile.toURL());
assertEquals("value", config.getProperty("element"));
assertEquals("I'm complex!",
config.getProperty("element2.subelement.subsubelement"));
@@ -213,7 +216,7 @@
config.setRootElementName("myconfig");
config.save();
- config = new HierarchicalXMLConfiguration();
+ config = new XMLConfiguration();
config.load(saveFile);
assertEquals(1, config.getMaxIndex("tables.table.name"));
assertEquals("tests", config.getString("tables.table(0).name"));
@@ -222,6 +225,7 @@
assertTrue(config.getBoolean("tables.table(1).fields.field(1)[EMAIL
PROTECTED]"));
assertEquals("tiger", config.getString("connection.passwd"));
assertEquals("system", config.getProperty("[EMAIL PROTECTED]"));
+ assertEquals("myconfig", config.getRootElementName());
}
finally
{
@@ -254,7 +258,7 @@
try
{
config.save(new File(TEST_SAVE));
- config = new HierarchicalXMLConfiguration();
+ config = new XMLConfiguration();
config.load(TEST_SAVE);
assertFalse(config.containsKey("[EMAIL PROTECTED]"));
assertEquals("1<2", config.getProperty("test.entity"));
@@ -272,6 +276,30 @@
finally
{
removeTestSaveFile();
+ }
+ }
+
+ /**
+ * Tests manipulation of the root element's name.
+ *
+ */
+ public void testRootElement() throws Exception
+ {
+ assertEquals("configuration", config.getRootElementName());
+ config.setRootElementName("newRootName");
+ assertEquals("newRootName", config.getRootElementName());
+
+ config.setFile(new File(TEST_FILE3));
+ config.load();
+ assertEquals("testconfig", config.getRootElementName());
+ try
+ {
+ config.setRootElementName("anotherRootElement");
+ fail("Setting root element name when loaded from file!");
+ }
+ catch(UnsupportedOperationException uex)
+ {
+ //fine
}
}
1.7 +2 -2 jakarta-commons/configuration/conf/test.xml
Index: test.xml
===================================================================
RCS file: /home/cvs/jakarta-commons/configuration/conf/test.xml,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- test.xml 13 Dec 2004 16:40:14 -0000 1.6
+++ test.xml 23 Dec 2004 18:42:25 -0000 1.7
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<!-- Test file for XMLConfiguration -->
-<configuration>
+<testconfig>
<element>value</element>
<element2>
<subelement>
@@ -58,4 +58,4 @@
<item id="4">four</item>
</list>
</clear>
-</configuration>
+</testconfig>
1.18 +5 -1
jakarta-commons/configuration/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java
Index: HierarchicalConfiguration.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/configuration/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- HierarchicalConfiguration.java 18 Dec 2004 16:33:03 -0000 1.17
+++ HierarchicalConfiguration.java 23 Dec 2004 18:42:25 -0000 1.18
@@ -1193,7 +1193,11 @@
else
{
StringBuffer buf = new StringBuffer(prefix);
- buf.append('.').append(key);
+ if (!key.isAttributeKey())
+ {
+ buf.append(ConfigurationKey.PROPERTY_DELIMITER);
+ }
+ buf.append(key);
keyList.add(buf.toString());
}
}
1.20 +2 -2
jakarta-commons/configuration/src/java/org/apache/commons/configuration/ConfigurationFactory.java
Index: ConfigurationFactory.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/configuration/src/java/org/apache/commons/configuration/ConfigurationFactory.java,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -r1.19 -r1.20
--- ConfigurationFactory.java 19 Nov 2004 19:25:47 -0000 1.19
+++ ConfigurationFactory.java 23 Dec 2004 18:42:25 -0000 1.20
@@ -291,7 +291,7 @@
setupDigesterInstance(
digester,
matchString + "hierarchicalXml",
- new FileConfigurationFactory(HierarchicalXMLConfiguration.class),
+ new FileConfigurationFactory(XMLConfiguration.class),
null,
additional);
1.7 +14 -4
jakarta-commons/configuration/src/java/org/apache/commons/configuration/ConfigurationKey.java
Index: ConfigurationKey.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/configuration/src/java/org/apache/commons/configuration/ConfigurationKey.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- ConfigurationKey.java 19 Nov 2004 19:26:48 -0000 1.6
+++ ConfigurationKey.java 23 Dec 2004 18:42:25 -0000 1.7
@@ -39,15 +39,15 @@
*/
public class ConfigurationKey implements Serializable
{
+ /** Constant for a property delimiter.*/
+ public static final char PROPERTY_DELIMITER = '.';
+
/** Constant for an attribute start marker.*/
private static final String ATTRIBUTE_START = "[@";
/** Constant for an attribute end marker.*/
private static final String ATTRIBUTE_END = "]";
- /** Constant for a property delimiter.*/
- private static final char PROPERTY_DELIMITER = '.';
-
/** Constant for an index start marker.*/
private static final char INDEX_START = '(';
@@ -122,6 +122,16 @@
{
keyBuffer.append(constructAttributeKey(attr));
return this;
+ }
+
+ /**
+ * Checks if this key is an attribute key.
+ *
+ * @return a flag if this key is an attribute key
+ */
+ public boolean isAttributeKey()
+ {
+ return isAttributeKey(keyBuffer.toString());
}
/**
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]