001: /*
002: * Copyright (C) 2004, 2005 Joe Walnes.
003: * Copyright (C) 2006, 2007, 2008 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 23. February 2004 by Joe Walnes
011: */
012: package com.thoughtworks.xstream.converters.collections;
013:
014: import com.thoughtworks.xstream.XStream;
015:
016: import junit.framework.TestCase;
017:
018: import java.util.Properties;
019:
020: public class PropertiesConverterTest extends TestCase {
021:
022: public void testConvertsPropertiesObjectToShortKeyValueElements() {
023: Properties in = new Properties();
024: in.setProperty("hello", "world");
025: in.setProperty("foo", "cheese");
026:
027: String expectedXML = "" + "<properties>\n"
028: + " <property name=\"hello\" value=\"world\"/>\n"
029: + " <property name=\"foo\" value=\"cheese\"/>\n"
030: + "</properties>";
031: XStream xstream = new XStream();
032: String actualXML = xstream.toXML(in);
033: assertEquals(expectedXML, actualXML);
034:
035: Properties expectedOut = new Properties();
036: expectedOut.setProperty("hello", "world");
037: expectedOut.setProperty("foo", "cheese");
038: Properties actualOut = (Properties) xstream.fromXML(actualXML);
039: assertEquals(in, actualOut);
040: assertEquals(in.toString(), actualOut.toString());
041:
042: }
043:
044: public void testIncludesDefaultProperties() {
045: Properties defaults = new Properties();
046: defaults.setProperty("host", "localhost");
047: defaults.setProperty("port", "80");
048:
049: Properties override = new Properties(defaults);
050: override.setProperty("port", "999");
051:
052: // sanity check
053: assertEquals("Unexpected overriden property", "999", override
054: .getProperty("port"));
055: assertEquals("Unexpected default property", "localhost",
056: override.getProperty("host"));
057:
058: String expectedXML = "" + "<properties>\n"
059: + " <property name=\"port\" value=\"999\"/>\n"
060: + " <defaults>\n"
061: + " <property name=\"port\" value=\"80\"/>\n"
062: + " <property name=\"host\" value=\"localhost\"/>\n"
063: + " </defaults>\n" + "</properties>";
064:
065: XStream xstream = new XStream();
066: String actualXML = xstream.toXML(override);
067: assertEquals(expectedXML, actualXML);
068:
069: Properties out = (Properties) xstream.fromXML(actualXML);
070: assertEquals("Unexpected overriden property", "999", out
071: .getProperty("port"));
072: assertEquals("Unexpected default property", "localhost", out
073: .getProperty("host"));
074: assertEquals(override, out);
075: }
076:
077: public void testCanSortElements() {
078: Properties defaults = new Properties();
079: defaults.setProperty("host", "localhost");
080: defaults.setProperty("port", "80");
081:
082: Properties override = new Properties(defaults);
083: override.setProperty("port", "999");
084: override.setProperty("domain", "codehaus.org");
085:
086: String expectedXML = ""
087: + "<properties>\n"
088: + " <property name=\"domain\" value=\"codehaus.org\"/>\n"
089: + " <property name=\"port\" value=\"999\"/>\n"
090: + " <defaults>\n"
091: + " <property name=\"host\" value=\"localhost\"/>\n"
092: + " <property name=\"port\" value=\"80\"/>\n"
093: + " </defaults>\n" + "</properties>";
094:
095: XStream xstream = new XStream();
096: xstream.registerConverter(new PropertiesConverter(true));
097: String actualXML = xstream.toXML(override);
098: assertEquals(expectedXML, actualXML);
099:
100: Properties out = (Properties) xstream.fromXML(actualXML);
101: assertEquals("Unexpected overriden property", "999", out
102: .getProperty("port"));
103: assertEquals("Unexpected default property", "localhost", out
104: .getProperty("host"));
105: assertEquals(override, out);
106: }
107: }
|