001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.runtime.java.util;
032:
033: import java.io.*;
034: import java.util.*;
035: import junit.framework.TestCase;
036:
037: /**
038: * @author Taras Puchko
039: */
040: public class _PropertiesTestCase extends TestCase {
041:
042: public void testLoadFromXML() throws Exception {
043: String xml = "<?xml version='1.0' encoding='UTF-8' standalone='no'?>\n"
044: + "<!DOCTYPE properties SYSTEM 'http://java.sun.com/dtd/properties.dtd'>\n"
045: + "<properties>\n"
046: + "<comment>test</comment>\n"
047: + "<entry key='keyB'>valueB</entry>\n"
048: + "<entry key='keyA'>valueA</entry>\n"
049: + "</properties>";
050: Properties properties = new Properties();
051: properties.loadFromXML(new ByteArrayInputStream(xml
052: .getBytes("UTF-8")));
053: assertEquals(2, properties.size());
054: assertEquals("valueA", properties.getProperty("keyA"));
055: assertEquals("valueB", properties.getProperty("keyB"));
056: }
057:
058: public void testLoadFromXML_Ok() throws Exception {
059: String xml = "<?xml version='1.0'?>\n"
060: + "<!DOCTYPE properties SYSTEM 'http://java.sun.com/dtd/properties.dtd'>\n"
061: + "<properties version='1.0'>\n"
062: + "<entry key='keyA'>valueA</entry>\n"
063: + "<entry key='keyB'>valueB</entry>\n"
064: + "</properties>";
065: Properties properties = new Properties();
066: properties.loadFromXML(new ByteArrayInputStream(xml
067: .getBytes("UTF-8")));
068: assertEquals(2, properties.size());
069: assertEquals("valueA", properties.getProperty("keyA"));
070: assertEquals("valueB", properties.getProperty("keyB"));
071: }
072:
073: public void testLoadFromXML_ErrorVersion() throws Exception {
074: String xml = "<?xml version='1.0' encoding='UTF-8' standalone='no'?>\n"
075: + "<!DOCTYPE properties SYSTEM 'http://java.sun.com/dtd/properties.dtd'>\n"
076: + "<properties version='1.1'>\n"
077: + "<comment>test</comment>\n"
078: + "<entry key='keyB'>valueB</entry>\n"
079: + "<entry key='keyA'>valueA</entry>\n"
080: + "</properties>";
081: try {
082: new Properties().loadFromXML(new ByteArrayInputStream(xml
083: .getBytes("UTF-8")));
084: fail();
085: } catch (InvalidPropertiesFormatException e) {
086: //ok
087: }
088: }
089:
090: public void testLoadFromXML_ErrorXML() throws Exception {
091: String xml = "<?xml version='1.0' encoding='UTF-8' standalone='no'?>\n"
092: + "<!DOCTYPE properties SYSTEM 'http://java.sun.com/dtd/properties.dtd'>\n"
093: + "<properties>\n"
094: + "<comment>test</comment>\n"
095: + "<item key='keyB'>valueB</item>\n"
096: + "<entry key='keyA'>valueA</entry>\n"
097: + "</properties>";
098: try {
099: new Properties().loadFromXML(new ByteArrayInputStream(xml
100: .getBytes("UTF-8")));
101: fail();
102: } catch (InvalidPropertiesFormatException e) {
103: //ok
104: }
105: }
106:
107: public void testStoreToXML() throws Exception {
108: ByteArrayOutputStream stream = new ByteArrayOutputStream();
109: Properties properties = new Properties();
110: properties.setProperty("keyA", "valueA");
111: properties.setProperty("keyB", "valueB");
112: properties.storeToXML(stream, "test");
113: byte[] bytes = stream.toByteArray();
114: String content = new String(bytes, "UTF-8");
115: assertTrue(content.contains("UTF-8"));
116: assertTrue(content.contains("comment"));
117: assertTrue(content.contains("test"));
118: properties.clear();
119: assertTrue(properties.isEmpty());
120: properties.loadFromXML(new ByteArrayInputStream(bytes));
121: assertEquals(2, properties.size());
122: assertEquals("valueA", properties.getProperty("keyA"));
123: assertEquals("valueB", properties.getProperty("keyB"));
124: }
125:
126: public void testStoreToXML_ASCII() throws Exception {
127: ByteArrayOutputStream stream = new ByteArrayOutputStream();
128: Properties properties = new Properties();
129: properties.setProperty("keyA", "valueA");
130: properties.setProperty("keyB", "valueB");
131: properties.storeToXML(stream, null, "US-ASCII");
132: byte[] bytes = stream.toByteArray();
133: String content = new String(bytes, "US-ASCII");
134: assertTrue(content.contains("US-ASCII"));
135: assertFalse(content.contains("comment"));
136: properties.clear();
137: assertTrue(properties.isEmpty());
138: properties.loadFromXML(new ByteArrayInputStream(bytes));
139: assertEquals(2, properties.size());
140: assertEquals("valueA", properties.getProperty("keyA"));
141: assertEquals("valueB", properties.getProperty("keyB"));
142: }
143:
144: }
|