cvs commit: jakarta-commons/configuration/src/test/org/apache/commons/configuration TestPropertiesConfiguration.java
ebourg 2005/01/03 03:58:46 Modified:configuration/src/java/org/apache/commons/configuration PropertiesConfiguration.java configuration/src/test/org/apache/commons/configuration TestPropertiesConfiguration.java Log: Changed PropertyWriter and unescapeJava to make them independant from the static delimiter. This will allow instance specific delimiters. Revision ChangesPath 1.18 +16 -13 jakarta-commons/configuration/src/java/org/apache/commons/configuration/PropertiesConfiguration.java Index: PropertiesConfiguration.java === RCS file: /home/cvs/jakarta-commons/configuration/src/java/org/apache/commons/configuration/PropertiesConfiguration.java,v retrieving revision 1.17 retrieving revision 1.18 diff -u -r1.17 -r1.18 --- PropertiesConfiguration.java 4 Dec 2004 15:45:40 - 1.17 +++ PropertiesConfiguration.java 3 Jan 2005 11:58:46 - 1.18 @@ -34,7 +34,7 @@ * This is the "classic" Properties loader which loads the values from * a single or multiple files (which can be chained with "include =". * All given path references are either absolute or relative to the - * file name supplied in the Constructor. + * file name supplied in the constructor. * * In this class, empty PropertyConfigurations can be built, properties * added and later saved. include statements are (obviously) not supported @@ -72,16 +72,16 @@ * If a property is named "include" (or whatever is defined by * setInclude() and getInclude() and the value of that property is * the full path to a file on disk, that file will be included into - * the ConfigurationsRepository. You can also pull in files relative - * to the parent configuration file. So if you have something - * like the following: + * the configuration. You can also pull in files relative to the parent + * configuration file. So if you have something like the following: * * include = additional.properties * * Then "additional.properties" is expected to be in the same * directory as the parent configuration file. * - * Duplicate name values will be replaced, so be careful. + * The properties in the included file are added to the parent configuration, + * they do not replace existing properties with the same key. * * * @@ -284,7 +284,7 @@ } else { -addProperty(key, unescapeJava(value)); +addProperty(key, unescapeJava(value, getDelimiter())); } } } @@ -304,7 +304,7 @@ { try { -PropertiesWriter out = new PropertiesWriter(writer); +PropertiesWriter out = new PropertiesWriter(writer, getDelimiter()); out.writeComment("written by PropertiesConfiguration"); out.writeComment(new Date().toString()); @@ -413,14 +413,17 @@ */ public static class PropertiesWriter extends FilterWriter { +private char delimiter; + /** * Constructor. * * @param writer a Writer object providing the underlying stream */ -public PropertiesWriter(Writer writer) throws IOException +public PropertiesWriter(Writer writer, char delimiter) { super(writer); +this.delimiter = delimiter; } /** @@ -437,7 +440,7 @@ if (value != null) { String v = StringEscapeUtils.escapeJava(String.valueOf(value)); -v = StringUtils.replace(v, String.valueOf(getDelimiter()), "\\" + getDelimiter()); +v = StringUtils.replace(v, String.valueOf(delimiter), "\\" + delimiter); write(v); } @@ -480,7 +483,7 @@ * * @throws IllegalArgumentException if the Writer is null */ -protected static String unescapeJava(String str) +protected static String unescapeJava(String str, char delimiter) { if (str == null) { @@ -548,9 +551,9 @@ else if (ch=='b'){ out.append('\b'); } -else if (ch==getDelimiter()){ +else if (ch==delimiter){ out.append('\\'); -out.append(getDelimiter()); +out.append(delimiter); } else if (ch=='u'){ // uh-oh, we're in unicode country 1.18 +2 -2 jakarta-commons/configuration/src/test/org/apache/commons/configuration
cvs commit: jakarta-commons/configuration/src/test/org/apache/commons/configuration TestPropertiesConfiguration.java
ebourg 2004/09/23 04:47:57 Modified:configuration/src/test/org/apache/commons/configuration TestPropertiesConfiguration.java Log: More tests: - for the new constructor PropertiesConfiguration(File) - for a file loaded from a jar - for setInclude() Revision ChangesPath 1.14 +35 -1 jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java Index: TestPropertiesConfiguration.java === RCS file: /home/cvs/jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java,v retrieving revision 1.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- TestPropertiesConfiguration.java 22 Sep 2004 17:17:30 - 1.13 +++ TestPropertiesConfiguration.java 23 Sep 2004 11:47:57 - 1.14 @@ -79,6 +79,21 @@ assertEquals("true", loaded); } +public void testSetInclude() throws Exception +{ +// change the include key +PropertiesConfiguration.setInclude("import"); + +// load the configuration +PropertiesConfiguration conf = new PropertiesConfiguration(); +conf.load("conf/test.properties"); + +// restore the previous value for the other tests +PropertiesConfiguration.setInclude("include"); + +assertNull(conf.getString("include.loaded")); +} + /** * Tests List parsing. */ @@ -183,6 +198,25 @@ pc.load(); assertTrue("Make sure we have multiple keys", pc.getBoolean("test.boolean")); +} + +public void testLoadFromJAR() throws Exception +{ +conf = new PropertiesConfiguration(); +conf.setIncludesAllowed(true); +conf.setFileName("test-jar.properties"); +conf.load(); + +assertEquals("jar", conf.getProperty("configuration.location")); +assertEquals("property in an included file", "jar", conf.getProperty("include.location")); +} + +public void testLoadFromFile() throws Exception +{ +File file = new File("conf/test.properties"); +conf = new PropertiesConfiguration(file); + +assertEquals("true", conf.getString("configuration.loaded")); } public void testGetStringWithEscapedChars() - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
cvs commit: jakarta-commons/configuration/src/test/org/apache/commons/configuration TestPropertiesConfiguration.java
ebourg 2004/06/22 04:23:47 Modified:configuration/conf test.properties configuration/src/test/org/apache/commons/configuration TestPropertiesConfiguration.java Log: Test for properties spread out across several adjacent lines Revision ChangesPath 1.8 +5 -0 jakarta-commons/configuration/conf/test.properties Index: test.properties === RCS file: /home/cvs/jakarta-commons/configuration/conf/test.properties,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- test.properties 22 Jun 2004 10:51:22 - 1.7 +++ test.properties 22 Jun 2004 11:23:46 - 1.8 @@ -19,9 +19,14 @@ test.mixed.array = a test.mixed.array = b, c, d +test.multilines = This is a value spread out across several adjacent \ + natural lines by escaping the line terminator with \ + a backslash character. + # # Test a property that uses a previous property # + base = base base.reference = ${base}extra base.reference.array = ${base}extra 1.10 +11 -1 jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java Index: TestPropertiesConfiguration.java === RCS file: /home/cvs/jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- TestPropertiesConfiguration.java 17 Jun 2004 15:30:29 - 1.9 +++ TestPropertiesConfiguration.java 22 Jun 2004 11:23:47 - 1.10 @@ -131,4 +131,14 @@ assertEquals("3rd element", "c", array[2]); assertEquals("4th element", "d", array[3]); } + +public void testMultilines() +{ +String property = "This is a value spread out across several adjacent " ++ "natural lines by escaping the line terminator with " ++ "a backslash character."; + +assertEquals("'test.multilines' property", property, conf.getString("test.multilines")); +} + } - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
cvs commit: jakarta-commons/configuration/src/test/org/apache/commons/configuration TestPropertiesConfiguration.java
ebourg 2004/06/17 08:30:29 Modified:configuration/src/test/org/apache/commons/configuration TestPropertiesConfiguration.java Log: copy/paste fix :) Revision ChangesPath 1.9 +4 -4 jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java Index: TestPropertiesConfiguration.java === RCS file: /home/cvs/jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- TestPropertiesConfiguration.java 17 Jun 2004 14:39:54 - 1.8 +++ TestPropertiesConfiguration.java 17 Jun 2004 15:30:29 - 1.9 @@ -127,8 +127,8 @@ assertEquals("array length", 4, array.length); assertEquals("1st element", "a", array[0]); -assertEquals("1st element", "b", array[1]); -assertEquals("1st element", "c", array[2]); -assertEquals("1st element", "d", array[3]); +assertEquals("2nd element", "b", array[1]); +assertEquals("3rd element", "c", array[2]); +assertEquals("4th element", "d", array[3]); } } - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
cvs commit: jakarta-commons/configuration/src/test/org/apache/commons/configuration TestPropertiesConfiguration.java
ebourg 2004/06/17 07:39:54 Modified:configuration/conf test.properties configuration/src/test/org/apache/commons/configuration TestPropertiesConfiguration.java Log: Added a test for list properties specified as a comma separated values and with multiple keys Revision ChangesPath 1.6 +3 -0 jakarta-commons/configuration/conf/test.properties Index: test.properties === RCS file: /home/cvs/jakarta-commons/configuration/conf/test.properties,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- test.properties 15 Jun 2004 10:12:29 - 1.5 +++ test.properties 17 Jun 2004 14:39:54 - 1.6 @@ -16,6 +16,9 @@ test.empty = +test.mixed.array = a +test.mixed.array = b, c, d + # # Test a property that uses a previous property # 1.8 +12 -1 jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java Index: TestPropertiesConfiguration.java === RCS file: /home/cvs/jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- TestPropertiesConfiguration.java 15 Jun 2004 10:12:29 - 1.7 +++ TestPropertiesConfiguration.java 17 Jun 2004 14:39:54 - 1.8 @@ -120,4 +120,15 @@ { assertEquals("test\\,test", PropertiesConfiguration.unescapeJava("test\\,test")); } + +public void testMixedArray() +{ +String[] array = conf.getStringArray("test.mixed.array"); + +assertEquals("array length", 4, array.length); +assertEquals("1st element", "a", array[0]); +assertEquals("1st element", "b", array[1]); +assertEquals("1st element", "c", array[2]); +assertEquals("1st element", "d", array[3]); +} } - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
cvs commit: jakarta-commons/configuration/src/test/org/apache/commons/configuration TestPropertiesConfiguration.java
ebourg 2004/06/15 03:12:29 Modified:configuration/src/java/org/apache/commons/configuration BasePropertiesConfiguration.java configuration/xdocs changes.xml configuration/conf test.properties configuration/src/test/org/apache/commons/configuration TestPropertiesConfiguration.java Log: Interpolation tokens in a PropertyConfiguration are now preserved on saving Revision ChangesPath 1.9 +18 -9 jakarta-commons/configuration/src/java/org/apache/commons/configuration/BasePropertiesConfiguration.java Index: BasePropertiesConfiguration.java === RCS file: /home/cvs/jakarta-commons/configuration/src/java/org/apache/commons/configuration/BasePropertiesConfiguration.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- BasePropertiesConfiguration.java 3 Jun 2004 15:32:46 - 1.8 +++ BasePropertiesConfiguration.java 15 Jun 2004 10:12:29 - 1.9 @@ -27,6 +27,7 @@ import java.util.Date; import java.util.Iterator; +import java.util.List; import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.lang.StringUtils; @@ -111,8 +112,7 @@ * * @version $Id$ */ -public abstract class BasePropertiesConfiguration -extends BasePathConfiguration +public abstract class BasePropertiesConfiguration extends BasePathConfiguration { /** Allow file inclusion or not */ private boolean includesAllowed = false; @@ -239,7 +239,16 @@ for (Iterator i = this.getKeys(); i.hasNext();) { String key = (String) i.next(); - out.writeProperty(key, this.getStringArray(key)); +Object value = getProperty(key); + +if (value instanceof List) +{ +out.writeProperty(key, (List) value); +} +else +{ +out.writeProperty(key, value); +} } out.flush(); out.close(); @@ -388,13 +397,13 @@ * @param value * @exception IOException */ -public void writeProperty(String key, String value) throws IOException +public void writeProperty(String key, Object value) throws IOException { write(key); write(" = "); if (value != null) { -String v = StringEscapeUtils.escapeJava(value); +String v = StringEscapeUtils.escapeJava(String.valueOf(value)); v = StringUtils.replace(v, String.valueOf(DELIMITER), "\\" + DELIMITER); write(v); } @@ -408,11 +417,11 @@ * @param key The key of the property * @param values The array of values of the property */ -public void writeProperty(String key, String[] values) throws IOException +public void writeProperty(String key, List values) throws IOException { -for (int i = 0; i < values.length; i++) +for (int i = 0; i < values.size(); i++) { -writeProperty(key, values[i]); +writeProperty(key, values.get(i)); } } 1.20 +4 -1 jakarta-commons/configuration/xdocs/changes.xml Index: changes.xml === RCS file: /home/cvs/jakarta-commons/configuration/xdocs/changes.xml,v retrieving revision 1.19 retrieving revision 1.20 diff -u -r1.19 -r1.20 --- changes.xml 4 May 2004 22:27:10 - 1.19 +++ changes.xml 15 Jun 2004 10:12:29 - 1.20 @@ -6,7 +6,10 @@ - + + +Tokens like ${ref} in a PropertyConfiguration are now properly saved (Bugzilla 29366). + SubsetConfiguration returns a List on getList(). AbstractConfiguration wouldn't properly deal with a List, only with a Container for getList()! Thanks to jschaible for the unit test. 1.5 +7 -5 jakarta-commons/configuration/conf/test.properties Index: test.properties === RCS file: /home/cvs/jakarta-commons/configuration/conf/test.properties,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- test.properties 3 Jun 2004 15:32:46 - 1.4 +++ test.properties 15 Jun 2004 10:12:29 - 1.5 @@ -1,7 +1,7 @@ configuration.loaded = true packages = packagea -propertyInOrder=test.properties +propertyInOrder = test.properties include = include
cvs commit: jakarta-commons/configuration/src/test/org/apache/commons/configuration TestPropertiesConfiguration.java
ebourg 2004/06/03 08:32:46 Modified:configuration/src/java/org/apache/commons/configuration AbstractConfiguration.java BasePropertiesConfiguration.java configuration/conf test.properties configuration/src/test/org/apache/commons/configuration TestPropertiesConfiguration.java Log: Fixed Bug 27775, the list separator (comma) can be escaped with the \ character Revision ChangesPath 1.9 +7 -16 jakarta-commons/configuration/src/java/org/apache/commons/configuration/AbstractConfiguration.java Index: AbstractConfiguration.java === RCS file: /home/cvs/jakarta-commons/configuration/src/java/org/apache/commons/configuration/AbstractConfiguration.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- AbstractConfiguration.java4 May 2004 22:27:10 - 1.8 +++ AbstractConfiguration.java3 Jun 2004 15:32:46 - 1.9 @@ -42,18 +42,13 @@ /** how big the initial arraylist for splitting up name value pairs */ private static final int INITIAL_LIST_SIZE = 2; - /** start token */ protected static final String START_TOKEN = "${"; /** end token */ protected static final String END_TOKEN = "}"; -/** - * Empty constructor. - */ -public AbstractConfiguration() -{ -} +/** The property delimiter used while parsing (a comma). */ +protected static final char DELIMITER = ','; /** * Add a property to the configuration. If it already exists then the value @@ -76,8 +71,7 @@ { if (token instanceof String) { -for(Iterator it = processString((String) token).iterator(); -it.hasNext();) +for(Iterator it = processString((String) token).iterator(); it.hasNext();) { addPropertyDirect(key, it.next()); } @@ -238,7 +232,7 @@ { List retList = new ArrayList(INITIAL_LIST_SIZE); -if (token.indexOf(PropertiesTokenizer.DELIMITER) > 0) +if (token.indexOf(DELIMITER) > 0) { PropertiesTokenizer tokenizer = new PropertiesTokenizer(token); @@ -1300,11 +1294,8 @@ * separator is "," but commas into the property value are escaped * using the backslash in front. */ -class PropertiesTokenizer extends StringTokenizer +static class PropertiesTokenizer extends StringTokenizer { -/** The property delimiter used while parsing (a comma). */ -static final String DELIMITER = ","; - /** * Constructor. * @@ -1312,7 +1303,7 @@ */ public PropertiesTokenizer(String string) { -super(string, DELIMITER); +super(string, String.valueOf(DELIMITER)); } /** 1.8 +168 -56 jakarta-commons/configuration/src/java/org/apache/commons/configuration/BasePropertiesConfiguration.java Index: BasePropertiesConfiguration.java === RCS file: /home/cvs/jakarta-commons/configuration/src/java/org/apache/commons/configuration/BasePropertiesConfiguration.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- BasePropertiesConfiguration.java 2 Jun 2004 16:42:24 - 1.7 +++ BasePropertiesConfiguration.java 3 Jun 2004 15:32:46 - 1.8 @@ -30,6 +30,7 @@ import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.exception.NestableRuntimeException; /** * loads the configuration from a properties file. @@ -101,7 +102,7 @@ * * # commas may be escaped in tokens * commas.excaped = Hi\, what'up? - * + * * # properties can reference other properties * base.prop = /base * first.prop = ${base.prop}/first @@ -150,19 +151,17 @@ * encoding. * * @param input An InputStream. - * @param enc An encoding. + * @param encoding An encoding. * @exception ConfigurationException */ -public synchronized void load(InputStream input, String enc) -throws ConfigurationException +public synchronized void load(InputStream input, String encoding) throws ConfigurationException { PropertiesReader reader = null; -if (enc != null) +if (encoding != null) { try { -reader = - new PropertiesReader(new InputStreamReader(input, enc)); +reader = new PropertiesReader(new InputStreamRead