KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > TestExtendedProperties


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections;
17
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21
22 import junit.framework.Test;
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25
26 /**
27  * Tests some basic functions of the ExtendedProperties class.
28  *
29  * @version $Revision: 1.13 $ $Date: 2004/06/21 23:39:25 $
30  *
31  * @author Geir Magnusson Jr.
32  * @author Mohan Kishore
33  * @author Stephen Colebourne
34  */

35 public class TestExtendedProperties extends TestCase {
36     
37     protected ExtendedProperties eprop = new ExtendedProperties();
38
39     public TestExtendedProperties(String JavaDoc testName) {
40         super(testName);
41     }
42
43     public static Test suite() {
44         return new TestSuite(TestExtendedProperties.class);
45     }
46
47     public static void main(String JavaDoc args[]) {
48         String JavaDoc[] testCaseName = { TestExtendedProperties.class.getName()};
49         junit.textui.TestRunner.main(testCaseName);
50     }
51
52     public void testRetrieve() {
53         /*
54          * should be empty and return null
55          */

56         assertEquals("This returns null", eprop.getProperty("foo"), null);
57
58         /*
59          * add a real value, and get it two different ways
60          */

61         eprop.setProperty("number", "1");
62         assertEquals("This returns '1'", eprop.getProperty("number"), "1");
63         assertEquals("This returns '1'", eprop.getString("number"), "1");
64
65         /*
66          * now add another and get a Vector
67          */

68         eprop.addProperty("number", "2");
69         assertTrue("This returns array", (eprop.getVector("number") instanceof java.util.Vector JavaDoc));
70
71         /*
72          * now test dan's new fix where we get the first scalar
73          * when we access a vector valued
74          * property
75          */

76         assertTrue("This returns scalar", (eprop.getString("number") instanceof String JavaDoc));
77
78         /*
79          * test comma separated string properties
80          */

81         String JavaDoc prop = "hey, that's a test";
82         eprop.setProperty("prop.string", prop);
83         assertTrue("This returns vector", (eprop.getVector("prop.string") instanceof java.util.Vector JavaDoc));
84
85         String JavaDoc prop2 = "hey\\, that's a test";
86         eprop.remove("prop.string");
87         eprop.setProperty("prop.string", prop2);
88         assertTrue("This returns array", (eprop.getString("prop.string") instanceof java.lang.String JavaDoc));
89
90         /*
91          * test subset : we want to make sure that the EP doesn't reprocess the data
92          * elements when generating the subset
93          */

94
95         ExtendedProperties subEprop = eprop.subset("prop");
96
97         assertTrue("Returns the full string", subEprop.getString("string").equals(prop));
98         assertTrue("This returns string for subset", (subEprop.getString("string") instanceof java.lang.String JavaDoc));
99         assertTrue("This returns array for subset", (subEprop.getVector("string") instanceof java.util.Vector JavaDoc));
100
101     }
102
103     public void testInterpolation() {
104         eprop.setProperty("applicationRoot", "/home/applicationRoot");
105         eprop.setProperty("db", "${applicationRoot}/db/hypersonic");
106         String JavaDoc dbProp = "/home/applicationRoot/db/hypersonic";
107         assertTrue("Checking interpolated variable", eprop.getString("db").equals(dbProp));
108     }
109
110     public void testSaveAndLoad() {
111         ExtendedProperties ep1 = new ExtendedProperties();
112         ExtendedProperties ep2 = new ExtendedProperties();
113
114         try {
115             /* initialize value:
116             one=Hello\World
117             two=Hello\,World
118             three=Hello,World
119             */

120             String JavaDoc s1 = "one=Hello\\World\ntwo=Hello\\,World\nthree=Hello,World";
121             byte[] bytes = s1.getBytes();
122             ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(bytes);
123             ep1.load(bais);
124             assertEquals("Back-slashes not interpreted properly",
125                     "Hello\\World", ep1.getString("one"));
126             assertEquals("Escaped commas not interpreted properly",
127                     "Hello,World", ep1.getString("two"));
128             assertEquals("Commas not interpreted properly",
129                     2, ep1.getVector("three").size());
130             assertEquals("Commas not interpreted properly",
131                     "Hello", ep1.getVector("three").get(0));
132             assertEquals("Commas not interpreted properly",
133                     "World", ep1.getVector("three").get(1));
134                     
135             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
136             ep1.save(baos, null);
137             bytes = baos.toByteArray();
138             bais = new ByteArrayInputStream JavaDoc(bytes);
139             ep2.load(bais);
140             assertEquals("Back-slash not same after being saved and loaded",
141                     ep1.getString("one"), ep2.getString("one"));
142             assertEquals("Escaped comma not same after being saved and loaded",
143                     ep1.getString("two"), ep2.getString("two"));
144             assertEquals("Comma not same after being saved and loaded",
145                     ep1.getString("three"), ep2.getString("three"));
146         } catch (IOException JavaDoc ioe) {
147             fail("There was an exception saving and loading the EP");
148         }
149     }
150
151     public void testTrailingBackSlash() {
152         ExtendedProperties ep1 = new ExtendedProperties();
153
154         try {
155             /*
156             initialize using:
157             one=ONE
158             two=TWO \\
159             three=THREE
160             */

161             String JavaDoc s1 = "one=ONE\ntwo=TWO \\\\\nthree=THREE";
162             byte[] bytes = s1.getBytes();
163             ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(bytes);
164             ep1.load(bais);
165             assertEquals("Trailing back-slashes not interpreted properly",
166                     3, ep1.size());
167             assertEquals("Back-slash not escaped properly",
168                     "TWO \\", ep1.getString("two"));
169         } catch (IOException JavaDoc ioe) {
170             fail("There was an exception loading the EP");
171         }
172     }
173     
174     public void testMultipleSameKey1() throws Exception JavaDoc {
175         ExtendedProperties ep1 = new ExtendedProperties();
176
177         /*
178         initialize using:
179         one=a
180         one=b,c
181         */

182         String JavaDoc s1 = "one=a\none=b,c\n";
183         byte[] bytes = s1.getBytes();
184         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(bytes);
185         ep1.load(bais);
186         assertEquals(1, ep1.size());
187         assertEquals(3, ep1.getVector("one").size());
188         assertEquals("a", ep1.getVector("one").get(0));
189         assertEquals("b", ep1.getVector("one").get(1));
190         assertEquals("c", ep1.getVector("one").get(2));
191     }
192     
193     public void testMultipleSameKey2() throws Exception JavaDoc {
194         ExtendedProperties ep1 = new ExtendedProperties();
195
196         /*
197         initialize using:
198         one=a,b
199         one=c,d
200         */

201         String JavaDoc s1 = "one=a,b\none=c,d\n";
202         byte[] bytes = s1.getBytes();
203         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(bytes);
204         ep1.load(bais);
205         assertEquals(1, ep1.size());
206         assertEquals(4, ep1.getVector("one").size());
207         assertEquals("a", ep1.getVector("one").get(0));
208         assertEquals("b", ep1.getVector("one").get(1));
209         assertEquals("c", ep1.getVector("one").get(2));
210         assertEquals("d", ep1.getVector("one").get(3));
211     }
212     
213     public void testMultipleSameKey3() throws Exception JavaDoc {
214         ExtendedProperties ep1 = new ExtendedProperties();
215
216         /*
217         initialize using:
218         one=a,b
219         one=c
220         */

221         String JavaDoc s1 = "one=a,b\none=c\n";
222         byte[] bytes = s1.getBytes();
223         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(bytes);
224         ep1.load(bais);
225         assertEquals(1, ep1.size());
226         assertEquals(3, ep1.getVector("one").size());
227         assertEquals("a", ep1.getVector("one").get(0));
228         assertEquals("b", ep1.getVector("one").get(1));
229         assertEquals("c", ep1.getVector("one").get(2));
230     }
231     
232     public void testMultipleSameKeyByCode() throws Exception JavaDoc {
233         ExtendedProperties ep1 = new ExtendedProperties();
234
235         ep1.addProperty("one", "a");
236         assertEquals(1, ep1.size());
237         assertEquals(1, ep1.getVector("one").size());
238         assertEquals("a", ep1.getVector("one").get(0));
239         
240         ep1.addProperty("one", Boolean.TRUE);
241         assertEquals(1, ep1.size());
242         assertEquals(2, ep1.getVector("one").size());
243         assertEquals("a", ep1.getVector("one").get(0));
244         assertEquals(Boolean.TRUE, ep1.getVector("one").get(1));
245         
246         ep1.addProperty("one", "c,d");
247         assertEquals(1, ep1.size());
248         assertEquals(4, ep1.getVector("one").size());
249         assertEquals("a", ep1.getVector("one").get(0));
250         assertEquals(Boolean.TRUE, ep1.getVector("one").get(1));
251         assertEquals("c", ep1.getVector("one").get(2));
252         assertEquals("d", ep1.getVector("one").get(3));
253     }
254     
255 }
256
Popular Tags