ebourg 2004/07/13 07:10:06
Modified: configuration/src/test/org/apache/commons/configuration
TestXMLConfiguration.java
Log:
tests for save(), list properties and new attributes
Revision Changes Path
1.3 +56 -2
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.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestXMLConfiguration.java 12 Jul 2004 14:38:29 -0000 1.2
+++ TestXMLConfiguration.java 13 Jul 2004 14:10:06 -0000 1.3
@@ -18,6 +18,7 @@
import java.io.File;
import java.util.List;
+import java.util.Iterator;
import junit.framework.TestCase;
@@ -31,6 +32,8 @@
/** The File that we test with */
private String testProperties = new File("conf/test.xml").getAbsolutePath();
private String testBasePath = new File("conf").getAbsolutePath();
+ private File testSaveConf = new File("target/testsave.xml");
+
private XMLConfiguration conf;
protected void setUp() throws Exception
@@ -45,7 +48,7 @@
public void testGetAttribute() throws Exception
{
- assertEquals("element3{name}", "foo", conf.getProperty("[EMAIL
PROTECTED]"));
+ assertEquals("[EMAIL PROTECTED]", "foo", conf.getProperty("[EMAIL
PROTECTED]"));
}
public void testClearAttribute() throws Exception
@@ -56,8 +59,13 @@
public void testSetAttribute() throws Exception
{
+ // replace an existing attribute
conf.setProperty("[EMAIL PROTECTED]", "bar");
assertEquals("[EMAIL PROTECTED]", "bar", conf.getProperty("[EMAIL
PROTECTED]"));
+
+ // set a new attribute
+ conf.setProperty("[EMAIL PROTECTED]", "value");
+ assertEquals("[EMAIL PROTECTED]", "value", conf.getProperty("[EMAIL
PROTECTED]"));
}
public void testAddAttribute() throws Exception
@@ -71,6 +79,18 @@
assertEquals("list size", 2, list.size());
}
+ public void testAddList() throws Exception
+ {
+ conf.addProperty("test.array", "value1");
+ conf.addProperty("test.array", "value2");
+
+ List list = conf.getList("test.array");
+ assertNotNull("null list", list);
+ assertTrue("'value1' element missing", list.contains("value1"));
+ assertTrue("'value2' element missing", list.contains("value2"));
+ assertEquals("list size", 2, list.size());
+ }
+
public void testGetComplexProperty() throws Exception
{
assertEquals("I'm complex!",
conf.getProperty("element2.subelement.subsubelement"));
@@ -122,5 +142,39 @@
config.addProperty("test.string", "hello");
assertEquals("'test.string'", "hello", config.getString("test.string"));
+ }
+
+ public void testSave() throws Exception
+ {
+ // remove the file previously saved if necessary
+ if(testSaveConf.exists()){
+ assertTrue(testSaveConf.delete());
+ }
+
+ // add an array of strings to the configuration
+ conf.addProperty("string", "value1");
+ for (int i = 1; i < 5; i++)
+ {
+ conf.addProperty("test.array", "value" + i);
+ }
+
+ // add an array of strings in an attribute
+ for (int i = 1; i < 5; i++)
+ {
+ //conf.addProperty("[EMAIL PROTECTED]", "value" + i);
+ conf.addProperty("[EMAIL PROTECTED]", "value" + i);
+ }
+
+ // save the configuration
+ conf.save(testSaveConf.getAbsolutePath());
+
+ // read the configuration and compare the properties
+ XMLConfiguration checkConfig = new XMLConfiguration();
+ checkConfig.setFileName(testSaveConf.getAbsolutePath());
+ for (Iterator i = conf.getKeys(); i.hasNext();) {
+ String key = (String) i.next();
+ assertTrue("The saved configuration doesn't contain the key '" + key +
"'", checkConfig.containsKey(key));
+ assertEquals("Value of the '" + key + "' property",
conf.getProperty(key), checkConfig.getProperty(key));
+ }
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]