1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.commons.collections;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.ByteArrayOutputStream;
21  import java.io.IOException;
22  import java.util.Properties;
23  
24  import junit.framework.Test;
25  import junit.framework.TestCase;
26  import junit.framework.TestSuite;
27  
28  /**
29   * Tests some basic functions of the ExtendedProperties class.
30   * 
31   * @version $Revision: 646780 $ $Date: 2008-04-10 13:48:07 +0100 (Thu, 10 Apr 2008) $
32   * 
33   * @author Geir Magnusson Jr.
34   * @author Mohan Kishore
35   * @author Stephen Colebourne
36   * @author Shinobu Kawai
37   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
38   */
39  public class TestExtendedProperties extends TestCase {
40      
41      protected ExtendedProperties eprop = new ExtendedProperties();
42  
43      public TestExtendedProperties(String testName) {
44          super(testName);
45      }
46  
47      public static Test suite() {
48          return new TestSuite(TestExtendedProperties.class);
49      }
50  
51      public static void main(String args[]) {
52          String[] testCaseName = { TestExtendedProperties.class.getName()};
53          junit.textui.TestRunner.main(testCaseName);
54      }
55  
56      public void testRetrieve() {
57          /*
58           * should be empty and return null
59           */
60          assertEquals("This returns null", eprop.getProperty("foo"), null);
61  
62          /*
63           *  add a real value, and get it two different ways
64           */
65          eprop.setProperty("number", "1");
66          assertEquals("This returns '1'", eprop.getProperty("number"), "1");
67          assertEquals("This returns '1'", eprop.getString("number"), "1");
68  
69          /*
70           * now add another and get a Vector/list
71           */
72          eprop.addProperty("number", "2");
73          assertTrue("This returns array", (eprop.getVector("number") instanceof java.util.Vector));
74          assertTrue("This returns array", (eprop.getList("number") instanceof java.util.List));
75  
76          /*
77           *  now test dan's new fix where we get the first scalar 
78           *  when we access a vector/list valued
79           *  property
80           */
81          assertTrue("This returns scalar", (eprop.getString("number") instanceof String));
82  
83          /*
84           * test comma separated string properties
85           */
86          String prop = "hey, that's a test";
87          eprop.setProperty("prop.string", prop);
88          assertTrue("This returns vector", (eprop.getVector("prop.string") instanceof java.util.Vector));
89          assertTrue("This returns list", (eprop.getList("prop.string") instanceof java.util.List));
90  
91          String prop2 = "hey\\, that's a test";
92          eprop.remove("prop.string");
93          eprop.setProperty("prop.string", prop2);
94          assertTrue("This returns array", (eprop.getString("prop.string") instanceof java.lang.String));
95  
96          /*
97           * test subset : we want to make sure that the EP doesn't reprocess the data 
98           *  elements when generating the subset
99           */
100 
101         ExtendedProperties subEprop = eprop.subset("prop");
102 
103         assertTrue("Returns the full string", subEprop.getString("string").equals(prop));
104         assertTrue("This returns string for subset", (subEprop.getString("string") instanceof java.lang.String));
105         assertTrue("This returns array for subset", (subEprop.getVector("string") instanceof java.util.Vector));
106         assertTrue("This returns array for subset", (subEprop.getList("string") instanceof java.util.List));
107 
108     }
109 
110     public void testInterpolation() {
111         eprop.setProperty("applicationRoot", "/home/applicationRoot");
112         eprop.setProperty("db", "${applicationRoot}/db/hypersonic");
113         String dbProp = "/home/applicationRoot/db/hypersonic";
114         assertTrue("Checking interpolated variable", eprop.getString("db").equals(dbProp));
115     }
116 
117     public void testSaveAndLoad() {
118         ExtendedProperties ep1 = new ExtendedProperties();
119         ExtendedProperties ep2 = new ExtendedProperties();
120 
121         try {
122             /* initialize value:
123             one=Hello\World
124             two=Hello\,World
125             three=Hello,World
126             */
127             String s1 = "one=Hello\\World\ntwo=Hello\\,World\nthree=Hello,World";
128             byte[] bytes = s1.getBytes();
129             ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
130             ep1.load(bais);
131             assertEquals("Back-slashes not interpreted properly", 
132                     "Hello\\World", ep1.getString("one"));
133             assertEquals("Escaped commas not interpreted properly", 
134                     "Hello,World", ep1.getString("two"));
135             assertEquals("Commas not interpreted properly", 
136                     2, ep1.getVector("three").size());
137             assertEquals("Commas not interpreted properly", 
138                     "Hello", ep1.getVector("three").get(0));
139             assertEquals("Commas not interpreted properly", 
140                     "World", ep1.getVector("three").get(1));
141 
142             assertEquals("Commas not interpreted properly", 
143                     2, ep1.getList("three").size());
144             assertEquals("Commas not interpreted properly", 
145                     "Hello", ep1.getList("three").get(0));
146             assertEquals("Commas not interpreted properly", 
147                     "World", ep1.getList("three").get(1));
148                     
149             ByteArrayOutputStream baos = new ByteArrayOutputStream();
150             ep1.save(baos, null);
151             bytes = baos.toByteArray();
152             bais = new ByteArrayInputStream(bytes);
153             ep2.load(bais);
154             assertEquals("Back-slash not same after being saved and loaded",
155                     ep1.getString("one"), ep2.getString("one"));
156             assertEquals("Escaped comma not same after being saved and loaded",
157                     ep1.getString("two"), ep2.getString("two"));
158             assertEquals("Comma not same after being saved and loaded",
159                     ep1.getString("three"), ep2.getString("three"));
160         } catch (IOException ioe) {
161             fail("There was an exception saving and loading the EP");
162         }
163     }
164 
165     public void testTrailingBackSlash() {
166         ExtendedProperties ep1 = new ExtendedProperties();
167 
168         try {
169             /*
170             initialize using:
171             one=ONE
172             two=TWO \\
173             three=THREE
174             */
175             String s1 = "one=ONE\ntwo=TWO \\\\\nthree=THREE";
176             byte[] bytes = s1.getBytes();
177             ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
178             ep1.load(bais);
179             assertEquals("Trailing back-slashes not interpreted properly", 
180                     3, ep1.size());
181             assertEquals("Back-slash not escaped properly", 
182                     "TWO \\", ep1.getString("two"));
183         } catch (IOException ioe) {
184             fail("There was an exception loading the EP");
185         }
186     }
187     
188     public void testMultipleSameKey1() throws Exception {
189         ExtendedProperties ep1 = new ExtendedProperties();
190 
191         /*
192         initialize using:
193         one=a
194         one=b,c
195         */
196         String s1 = "one=a\none=b,c\n";
197         byte[] bytes = s1.getBytes();
198         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
199         ep1.load(bais);
200         assertEquals(1, ep1.size());
201 
202         assertEquals(3, ep1.getVector("one").size());
203         assertEquals("a", ep1.getVector("one").get(0));
204         assertEquals("b", ep1.getVector("one").get(1));
205         assertEquals("c", ep1.getVector("one").get(2));
206 
207         assertEquals(3, ep1.getList("one").size());
208         assertEquals("a", ep1.getList("one").get(0));
209         assertEquals("b", ep1.getList("one").get(1));
210         assertEquals("c", ep1.getList("one").get(2));
211     }
212     
213     public void testMultipleSameKey2() throws Exception {
214         ExtendedProperties ep1 = new ExtendedProperties();
215 
216         /*
217         initialize using:
218         one=a,b
219         one=c,d
220         */
221         String s1 = "one=a,b\none=c,d\n";
222         byte[] bytes = s1.getBytes();
223         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
224         ep1.load(bais);
225         assertEquals(1, ep1.size());
226 
227         assertEquals(4, ep1.getVector("one").size());
228         assertEquals("a", ep1.getVector("one").get(0));
229         assertEquals("b", ep1.getVector("one").get(1));
230         assertEquals("c", ep1.getVector("one").get(2));
231         assertEquals("d", ep1.getVector("one").get(3));
232 
233         assertEquals(4, ep1.getList("one").size());
234         assertEquals("a", ep1.getList("one").get(0));
235         assertEquals("b", ep1.getList("one").get(1));
236         assertEquals("c", ep1.getList("one").get(2));
237         assertEquals("d", ep1.getList("one").get(3));
238     }
239     
240     public void testMultipleSameKey3() throws Exception {
241         ExtendedProperties ep1 = new ExtendedProperties();
242 
243         /*
244         initialize using:
245         one=a,b
246         one=c
247         */
248         String s1 = "one=a,b\none=c\n";
249         byte[] bytes = s1.getBytes();
250         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
251         ep1.load(bais);
252         assertEquals(1, ep1.size());
253 
254         assertEquals(3, ep1.getVector("one").size());
255         assertEquals("a", ep1.getVector("one").get(0));
256         assertEquals("b", ep1.getVector("one").get(1));
257         assertEquals("c", ep1.getVector("one").get(2));
258 
259         assertEquals(3, ep1.getList("one").size());
260         assertEquals("a", ep1.getList("one").get(0));
261         assertEquals("b", ep1.getList("one").get(1));
262         assertEquals("c", ep1.getList("one").get(2));
263     }
264     
265     public void testMultipleSameKeyByCode() throws Exception {
266         ExtendedProperties ep1 = new ExtendedProperties();
267 
268         ep1.addProperty("one", "a");
269         assertEquals(1, ep1.size());
270 
271         assertEquals(1, ep1.getVector("one").size());
272         assertEquals("a", ep1.getVector("one").get(0));
273 
274         assertEquals(1, ep1.getList("one").size());
275         assertEquals("a", ep1.getList("one").get(0));
276         
277         ep1.addProperty("one", Boolean.TRUE);
278         assertEquals(1, ep1.size());
279 
280         assertEquals(2, ep1.getVector("one").size());
281         assertEquals("a", ep1.getVector("one").get(0));
282         assertEquals(Boolean.TRUE, ep1.getVector("one").get(1));
283 
284         assertEquals(2, ep1.getList("one").size());
285         assertEquals("a", ep1.getList("one").get(0));
286         assertEquals(Boolean.TRUE, ep1.getList("one").get(1));
287         
288         ep1.addProperty("one", "c,d");
289         assertEquals(1, ep1.size());
290 
291         assertEquals(4, ep1.getVector("one").size());
292         assertEquals("a", ep1.getVector("one").get(0));
293         assertEquals(Boolean.TRUE, ep1.getVector("one").get(1));
294         assertEquals("c", ep1.getVector("one").get(2));
295         assertEquals("d", ep1.getVector("one").get(3));
296 
297         assertEquals(4, ep1.getList("one").size());
298         assertEquals("a", ep1.getList("one").get(0));
299         assertEquals(Boolean.TRUE, ep1.getList("one").get(1));
300         assertEquals("c", ep1.getList("one").get(2));
301         assertEquals("d", ep1.getList("one").get(3));
302     }
303 
304     public void testInheritDefaultProperties() {
305         Properties defaults = new Properties();
306         defaults.setProperty("resource.loader", "class");
307 
308         Properties properties = new Properties(defaults);
309         properties.setProperty("test", "foo");
310 
311         ExtendedProperties extended = ExtendedProperties.convertProperties(properties);
312 
313         assertEquals("foo", extended.getString("test"));
314         assertEquals("class", extended.getString("resource.loader"));
315     }
316 
317 }