001: /*
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.ByteArrayOutputStream;
020: import java.io.IOException;
021: import java.util.Properties;
022:
023: import junit.framework.Test;
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026:
027: /**
028: * Tests some basic functions of the ExtendedProperties class.
029: *
030: * @version $Revision: 333060 $ $Date: 2005-11-13 16:59:51 +0000 (Sun, 13 Nov 2005) $
031: *
032: * @author Geir Magnusson Jr.
033: * @author Mohan Kishore
034: * @author Stephen Colebourne
035: * @author Shinobu Kawai
036: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
037: */
038: public class TestExtendedProperties extends TestCase {
039:
040: protected ExtendedProperties eprop = new ExtendedProperties();
041:
042: public TestExtendedProperties(String testName) {
043: super (testName);
044: }
045:
046: public static Test suite() {
047: return new TestSuite(TestExtendedProperties.class);
048: }
049:
050: public static void main(String args[]) {
051: String[] testCaseName = { TestExtendedProperties.class
052: .getName() };
053: junit.textui.TestRunner.main(testCaseName);
054: }
055:
056: public void testRetrieve() {
057: /*
058: * should be empty and return null
059: */
060: assertEquals("This returns null", eprop.getProperty("foo"),
061: null);
062:
063: /*
064: * add a real value, and get it two different ways
065: */
066: eprop.setProperty("number", "1");
067: assertEquals("This returns '1'", eprop.getProperty("number"),
068: "1");
069: assertEquals("This returns '1'", eprop.getString("number"), "1");
070:
071: /*
072: * now add another and get a Vector/list
073: */
074: eprop.addProperty("number", "2");
075: assertTrue("This returns array",
076: (eprop.getVector("number") instanceof java.util.Vector));
077: assertTrue("This returns array",
078: (eprop.getList("number") instanceof java.util.List));
079:
080: /*
081: * now test dan's new fix where we get the first scalar
082: * when we access a vector/list valued
083: * property
084: */
085: assertTrue("This returns scalar",
086: (eprop.getString("number") instanceof String));
087:
088: /*
089: * test comma separated string properties
090: */
091: String prop = "hey, that's a test";
092: eprop.setProperty("prop.string", prop);
093: assertTrue("This returns vector", (eprop
094: .getVector("prop.string") instanceof java.util.Vector));
095: assertTrue(
096: "This returns list",
097: (eprop.getList("prop.string") instanceof java.util.List));
098:
099: String prop2 = "hey\\, that's a test";
100: eprop.remove("prop.string");
101: eprop.setProperty("prop.string", prop2);
102: assertTrue("This returns array", (eprop
103: .getString("prop.string") instanceof java.lang.String));
104:
105: /*
106: * test subset : we want to make sure that the EP doesn't reprocess the data
107: * elements when generating the subset
108: */
109:
110: ExtendedProperties subEprop = eprop.subset("prop");
111:
112: assertTrue("Returns the full string", subEprop.getString(
113: "string").equals(prop));
114: assertTrue("This returns string for subset", (subEprop
115: .getString("string") instanceof java.lang.String));
116: assertTrue("This returns array for subset", (subEprop
117: .getVector("string") instanceof java.util.Vector));
118: assertTrue("This returns array for subset", (subEprop
119: .getList("string") instanceof java.util.List));
120:
121: }
122:
123: public void testInterpolation() {
124: eprop.setProperty("applicationRoot", "/home/applicationRoot");
125: eprop.setProperty("db", "${applicationRoot}/db/hypersonic");
126: String dbProp = "/home/applicationRoot/db/hypersonic";
127: assertTrue("Checking interpolated variable", eprop.getString(
128: "db").equals(dbProp));
129: }
130:
131: public void testSaveAndLoad() {
132: ExtendedProperties ep1 = new ExtendedProperties();
133: ExtendedProperties ep2 = new ExtendedProperties();
134:
135: try {
136: /* initialize value:
137: one=Hello\World
138: two=Hello\,World
139: three=Hello,World
140: */
141: String s1 = "one=Hello\\World\ntwo=Hello\\,World\nthree=Hello,World";
142: byte[] bytes = s1.getBytes();
143: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
144: ep1.load(bais);
145: assertEquals("Back-slashes not interpreted properly",
146: "Hello\\World", ep1.getString("one"));
147: assertEquals("Escaped commas not interpreted properly",
148: "Hello,World", ep1.getString("two"));
149: assertEquals("Commas not interpreted properly", 2, ep1
150: .getVector("three").size());
151: assertEquals("Commas not interpreted properly", "Hello",
152: ep1.getVector("three").get(0));
153: assertEquals("Commas not interpreted properly", "World",
154: ep1.getVector("three").get(1));
155:
156: assertEquals("Commas not interpreted properly", 2, ep1
157: .getList("three").size());
158: assertEquals("Commas not interpreted properly", "Hello",
159: ep1.getList("three").get(0));
160: assertEquals("Commas not interpreted properly", "World",
161: ep1.getList("three").get(1));
162:
163: ByteArrayOutputStream baos = new ByteArrayOutputStream();
164: ep1.save(baos, null);
165: bytes = baos.toByteArray();
166: bais = new ByteArrayInputStream(bytes);
167: ep2.load(bais);
168: assertEquals(
169: "Back-slash not same after being saved and loaded",
170: ep1.getString("one"), ep2.getString("one"));
171: assertEquals(
172: "Escaped comma not same after being saved and loaded",
173: ep1.getString("two"), ep2.getString("two"));
174: assertEquals("Comma not same after being saved and loaded",
175: ep1.getString("three"), ep2.getString("three"));
176: } catch (IOException ioe) {
177: fail("There was an exception saving and loading the EP");
178: }
179: }
180:
181: public void testTrailingBackSlash() {
182: ExtendedProperties ep1 = new ExtendedProperties();
183:
184: try {
185: /*
186: initialize using:
187: one=ONE
188: two=TWO \\
189: three=THREE
190: */
191: String s1 = "one=ONE\ntwo=TWO \\\\\nthree=THREE";
192: byte[] bytes = s1.getBytes();
193: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
194: ep1.load(bais);
195: assertEquals(
196: "Trailing back-slashes not interpreted properly",
197: 3, ep1.size());
198: assertEquals("Back-slash not escaped properly", "TWO \\",
199: ep1.getString("two"));
200: } catch (IOException ioe) {
201: fail("There was an exception loading the EP");
202: }
203: }
204:
205: public void testMultipleSameKey1() throws Exception {
206: ExtendedProperties ep1 = new ExtendedProperties();
207:
208: /*
209: initialize using:
210: one=a
211: one=b,c
212: */
213: String s1 = "one=a\none=b,c\n";
214: byte[] bytes = s1.getBytes();
215: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
216: ep1.load(bais);
217: assertEquals(1, ep1.size());
218:
219: assertEquals(3, ep1.getVector("one").size());
220: assertEquals("a", ep1.getVector("one").get(0));
221: assertEquals("b", ep1.getVector("one").get(1));
222: assertEquals("c", ep1.getVector("one").get(2));
223:
224: assertEquals(3, ep1.getList("one").size());
225: assertEquals("a", ep1.getList("one").get(0));
226: assertEquals("b", ep1.getList("one").get(1));
227: assertEquals("c", ep1.getList("one").get(2));
228: }
229:
230: public void testMultipleSameKey2() throws Exception {
231: ExtendedProperties ep1 = new ExtendedProperties();
232:
233: /*
234: initialize using:
235: one=a,b
236: one=c,d
237: */
238: String s1 = "one=a,b\none=c,d\n";
239: byte[] bytes = s1.getBytes();
240: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
241: ep1.load(bais);
242: assertEquals(1, ep1.size());
243:
244: assertEquals(4, ep1.getVector("one").size());
245: assertEquals("a", ep1.getVector("one").get(0));
246: assertEquals("b", ep1.getVector("one").get(1));
247: assertEquals("c", ep1.getVector("one").get(2));
248: assertEquals("d", ep1.getVector("one").get(3));
249:
250: assertEquals(4, ep1.getList("one").size());
251: assertEquals("a", ep1.getList("one").get(0));
252: assertEquals("b", ep1.getList("one").get(1));
253: assertEquals("c", ep1.getList("one").get(2));
254: assertEquals("d", ep1.getList("one").get(3));
255: }
256:
257: public void testMultipleSameKey3() throws Exception {
258: ExtendedProperties ep1 = new ExtendedProperties();
259:
260: /*
261: initialize using:
262: one=a,b
263: one=c
264: */
265: String s1 = "one=a,b\none=c\n";
266: byte[] bytes = s1.getBytes();
267: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
268: ep1.load(bais);
269: assertEquals(1, ep1.size());
270:
271: assertEquals(3, ep1.getVector("one").size());
272: assertEquals("a", ep1.getVector("one").get(0));
273: assertEquals("b", ep1.getVector("one").get(1));
274: assertEquals("c", ep1.getVector("one").get(2));
275:
276: assertEquals(3, ep1.getList("one").size());
277: assertEquals("a", ep1.getList("one").get(0));
278: assertEquals("b", ep1.getList("one").get(1));
279: assertEquals("c", ep1.getList("one").get(2));
280: }
281:
282: public void testMultipleSameKeyByCode() throws Exception {
283: ExtendedProperties ep1 = new ExtendedProperties();
284:
285: ep1.addProperty("one", "a");
286: assertEquals(1, ep1.size());
287:
288: assertEquals(1, ep1.getVector("one").size());
289: assertEquals("a", ep1.getVector("one").get(0));
290:
291: assertEquals(1, ep1.getList("one").size());
292: assertEquals("a", ep1.getList("one").get(0));
293:
294: ep1.addProperty("one", Boolean.TRUE);
295: assertEquals(1, ep1.size());
296:
297: assertEquals(2, ep1.getVector("one").size());
298: assertEquals("a", ep1.getVector("one").get(0));
299: assertEquals(Boolean.TRUE, ep1.getVector("one").get(1));
300:
301: assertEquals(2, ep1.getList("one").size());
302: assertEquals("a", ep1.getList("one").get(0));
303: assertEquals(Boolean.TRUE, ep1.getList("one").get(1));
304:
305: ep1.addProperty("one", "c,d");
306: assertEquals(1, ep1.size());
307:
308: assertEquals(4, ep1.getVector("one").size());
309: assertEquals("a", ep1.getVector("one").get(0));
310: assertEquals(Boolean.TRUE, ep1.getVector("one").get(1));
311: assertEquals("c", ep1.getVector("one").get(2));
312: assertEquals("d", ep1.getVector("one").get(3));
313:
314: assertEquals(4, ep1.getList("one").size());
315: assertEquals("a", ep1.getList("one").get(0));
316: assertEquals(Boolean.TRUE, ep1.getList("one").get(1));
317: assertEquals("c", ep1.getList("one").get(2));
318: assertEquals("d", ep1.getList("one").get(3));
319: }
320:
321: public void testInheritDefaultProperties() {
322: Properties defaults = new Properties();
323: defaults.setProperty("resource.loader", "class");
324:
325: Properties properties = new Properties(defaults);
326: properties.setProperty("test", "foo");
327:
328: ExtendedProperties extended = ExtendedProperties
329: .convertProperties(properties);
330:
331: assertEquals("foo", extended.getString("test"));
332: assertEquals("class", extended.getString("resource.loader"));
333: }
334:
335: }
|