001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.util;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.IOException;
021: import java.io.ByteArrayOutputStream;
022: import java.util.Collections;
023: import java.util.HashSet;
024: import java.util.Properties;
025: import java.util.Set;
026: import java.util.TreeSet;
027:
028: import junit.framework.TestCase;
029:
030: public class PropertiesTest extends TestCase {
031:
032: public void testProperties() throws Exception {
033: Properties properties = createProperties();
034:
035: // empty
036: assertTrue(properties.isEmpty());
037: assertEquals(0, properties.size());
038: assertTrue(properties.keySet().isEmpty());
039: assertEquals(0, properties.keySet().size());
040: assertTrue(properties.values().isEmpty());
041: assertEquals(0, properties.values().size());
042: assertTrue(properties.entrySet().isEmpty());
043: assertEquals(0, properties.entrySet().size());
044: assertFalse(properties.keys().hasMoreElements());
045: assertFalse(properties.propertyNames().hasMoreElements());
046: assertFalse(properties.elements().hasMoreElements());
047: assertNull(properties.get("foo"));
048: assertNull(properties.getProperty("foo"));
049: assertEquals("default", properties
050: .getProperty("foo", "default"));
051:
052: // one entry
053: assertNull(properties.put("foo", "bar"));
054: assertFalse(properties.isEmpty());
055: assertEquals(1, properties.size());
056: assertFalse(properties.keySet().isEmpty());
057: assertEquals(1, properties.keySet().size());
058: assertEquals(Collections.singleton("foo"), properties.keySet());
059: assertFalse(properties.values().isEmpty());
060: assertEquals(1, properties.values().size());
061: assertFalse(properties.entrySet().isEmpty());
062: assertEquals(1, properties.entrySet().size());
063: assertTrue(properties.keys().hasMoreElements());
064: assertEquals("foo", properties.keys().nextElement());
065: assertTrue(properties.propertyNames().hasMoreElements());
066: assertEquals("foo", properties.keys().nextElement());
067: assertTrue(properties.elements().hasMoreElements());
068: assertEquals("bar", properties.get("foo"));
069: assertEquals("bar", properties.getProperty("foo"));
070: assertEquals("bar", properties.getProperty("foo", "default"));
071:
072: }
073:
074: public void testSimpleLoad() throws Exception {
075: Properties properties;
076:
077: properties = createProperties();
078: properties.load(new ByteArrayInputStream("foo=bar".getBytes()));
079: assertEquals(singletonProperty("foo", "bar"), properties);
080:
081: properties = createProperties();
082: properties.load(new ByteArrayInputStream("foo bar".getBytes()));
083: assertEquals(singletonProperty("foo", "bar"), properties);
084:
085: properties = createProperties();
086: properties.load(new ByteArrayInputStream("foo:bar".getBytes()));
087: assertEquals(singletonProperty("foo", "bar"), properties);
088:
089: properties = createProperties();
090: properties
091: .load(new ByteArrayInputStream("foo = bar".getBytes()));
092: assertEquals(singletonProperty("foo", "bar"), properties);
093:
094: properties = createProperties();
095: properties
096: .load(new ByteArrayInputStream("foo : bar".getBytes()));
097: assertEquals(singletonProperty("foo", "bar"), properties);
098:
099: properties = createProperties();
100: properties.load(new ByteArrayInputStream("foo\t \t=\t \tbar"
101: .getBytes()));
102: assertEquals(singletonProperty("foo", "bar"), properties);
103:
104: properties = createProperties();
105: properties.load(new ByteArrayInputStream("foo\t \t:\t \tbar"
106: .getBytes()));
107: assertEquals(singletonProperty("foo", "bar"), properties);
108:
109: properties = createProperties();
110: properties.load(new ByteArrayInputStream("foo".getBytes()));
111: assertEquals(singletonProperty("foo", ""), properties);
112:
113: properties = createProperties();
114: properties.load(new ByteArrayInputStream("foo=".getBytes()));
115: assertEquals(singletonProperty("foo", ""), properties);
116:
117: properties = createProperties();
118: properties.load(new ByteArrayInputStream("foo:".getBytes()));
119: assertEquals(singletonProperty("foo", ""), properties);
120:
121: properties = createProperties();
122: properties.load(new ByteArrayInputStream("foo =".getBytes()));
123: assertEquals(singletonProperty("foo", ""), properties);
124:
125: properties = createProperties();
126: properties.load(new ByteArrayInputStream("foo :".getBytes()));
127: assertEquals(singletonProperty("foo", ""), properties);
128:
129: properties = createProperties();
130: properties.load(new ByteArrayInputStream("foo = ".getBytes()));
131: assertEquals(singletonProperty("foo", ""), properties);
132:
133: properties = createProperties();
134: properties.load(new ByteArrayInputStream("foo : ".getBytes()));
135: assertEquals(singletonProperty("foo", ""), properties);
136:
137: //
138: // Invalid key valid separator (results in separator in value)
139: //
140:
141: properties = createProperties();
142: properties.load(new ByteArrayInputStream("foo := bar"
143: .getBytes()));
144: assertEquals(singletonProperty("foo", "= bar"), properties);
145:
146: properties = createProperties();
147: properties.load(new ByteArrayInputStream("foo == bar"
148: .getBytes()));
149: assertEquals(singletonProperty("foo", "= bar"), properties);
150:
151: properties = createProperties();
152: properties.load(new ByteArrayInputStream("foo :: bar"
153: .getBytes()));
154: assertEquals(singletonProperty("foo", ": bar"), properties);
155:
156: }
157:
158: public void testUnicode() throws Exception {
159: Properties properties = createProperties();
160: properties.load(new ByteArrayInputStream("a=\\u1234z"
161: .getBytes()));
162:
163: try {
164: properties = createProperties();
165: properties.load(new ByteArrayInputStream("a=\\u123"
166: .getBytes()));
167: fail("Expected IllegalArgumentException due to invalid unicode sequence");
168: } catch (IllegalArgumentException expected) {
169: }
170:
171: try {
172: properties = createProperties();
173: properties.load(new ByteArrayInputStream("a=\\u123z"
174: .getBytes()));
175: fail("Expected IllegalArgumentException due to invalid unicode sequence");
176: } catch (IllegalArgumentException expected) {
177: }
178:
179: properties = new SuperProperties();
180: properties.load(new ByteArrayInputStream("a=\\".getBytes()));
181: assertEquals(singletonProperty("a", "\u0000"), properties);
182:
183: properties = createProperties();
184: properties.load(new ByteArrayInputStream("a=\\q".getBytes()));
185: assertEquals(singletonProperty("a", "q"), properties);
186: }
187:
188: public void testKeyLineContinuation() throws Exception {
189: Properties properties;
190:
191: // line continuation (\n)
192: properties = createProperties();
193: properties.load(new ByteArrayInputStream("f\\\noo=bar"
194: .getBytes()));
195: assertEquals(singletonProperty("foo", "bar"), properties);
196:
197: // line continuation with white space (\n)
198: properties = createProperties();
199: properties.load(new ByteArrayInputStream("f\\\n oo=bar"
200: .getBytes()));
201: assertEquals(singletonProperty("foo", "bar"), properties);
202: properties = createProperties();
203: properties.load(new ByteArrayInputStream("f\\\n\t\too=bar"
204: .getBytes()));
205: assertEquals(singletonProperty("foo", "bar"), properties);
206: properties = createProperties();
207: properties.load(new ByteArrayInputStream("f\\\n \t oo=bar"
208: .getBytes()));
209: assertEquals(singletonProperty("foo", "bar"), properties);
210:
211: // line continuation (\r)
212: properties = createProperties();
213: properties.load(new ByteArrayInputStream("f\\\roo=bar"
214: .getBytes()));
215: assertEquals(singletonProperty("foo", "bar"), properties);
216:
217: // line continuation (\r) with white space
218: properties = createProperties();
219: properties.load(new ByteArrayInputStream("f\\\r oo=bar"
220: .getBytes()));
221: assertEquals(singletonProperty("foo", "bar"), properties);
222: properties = createProperties();
223: properties.load(new ByteArrayInputStream("f\\\r\t\too=bar"
224: .getBytes()));
225: assertEquals(singletonProperty("foo", "bar"), properties);
226: properties = createProperties();
227: properties.load(new ByteArrayInputStream("f\\\r \t oo=bar"
228: .getBytes()));
229: assertEquals(singletonProperty("foo", "bar"), properties);
230:
231: // line continuation (\r\n)
232: properties = createProperties();
233: properties.load(new ByteArrayInputStream("f\\\r\noo=bar"
234: .getBytes()));
235: assertEquals(singletonProperty("foo", "bar"), properties);
236:
237: //
238: // Invalid contunuations (results in a break)
239: //
240:
241: // line continuation (\n\n)
242: properties = createProperties();
243: properties.load(new ByteArrayInputStream("f\\\n\noo=bar"
244: .getBytes()));
245: assertEquals(properties("f", "", "oo", "bar"), properties);
246:
247: // line continuation (\r\r)
248: properties = createProperties();
249: properties.load(new ByteArrayInputStream("f\\\r\roo=bar"
250: .getBytes()));
251: assertEquals(properties("f", "", "oo", "bar"), properties);
252:
253: // line continuation (\r\n\n)
254: properties = createProperties();
255: properties.load(new ByteArrayInputStream("f\\\r\n\noo=bar"
256: .getBytes()));
257: assertEquals(properties("f", "", "oo", "bar"), properties);
258:
259: // line continuation (\r\n\r)
260: properties = createProperties();
261: properties.load(new ByteArrayInputStream("f\\\r\n\roo=bar"
262: .getBytes()));
263: assertEquals(properties("f", "", "oo", "bar"), properties);
264: }
265:
266: public void testValueLineContinuation() throws Exception {
267: Properties properties;
268:
269: // line continuation (\n)
270: properties = createProperties();
271: properties.load(new ByteArrayInputStream("foo=b\\\nar"
272: .getBytes()));
273: assertEquals(singletonProperty("foo", "bar"), properties);
274:
275: // line continuation with white space (\n)
276: properties = createProperties();
277: properties.load(new ByteArrayInputStream("foo=b\\\n ar"
278: .getBytes()));
279: assertEquals(singletonProperty("foo", "bar"), properties);
280: properties = createProperties();
281: properties.load(new ByteArrayInputStream("foo=b\\\n\t\tar"
282: .getBytes()));
283: assertEquals(singletonProperty("foo", "bar"), properties);
284: properties = createProperties();
285: properties.load(new ByteArrayInputStream("foo=b\\\n \t ar"
286: .getBytes()));
287: assertEquals(singletonProperty("foo", "bar"), properties);
288:
289: // line continuation (\r)
290: properties = createProperties();
291: properties.load(new ByteArrayInputStream("foo=b\\\rar"
292: .getBytes()));
293: assertEquals(singletonProperty("foo", "bar"), properties);
294:
295: // line continuation (\r) with white space
296: properties = createProperties();
297: properties.load(new ByteArrayInputStream("foo=b\\\r ar"
298: .getBytes()));
299: assertEquals(singletonProperty("foo", "bar"), properties);
300: properties = createProperties();
301: properties.load(new ByteArrayInputStream("foo=b\\\r\t\tar"
302: .getBytes()));
303: assertEquals(singletonProperty("foo", "bar"), properties);
304: properties = createProperties();
305: properties.load(new ByteArrayInputStream("foo=b\\\r \t ar"
306: .getBytes()));
307: assertEquals(singletonProperty("foo", "bar"), properties);
308:
309: // line continuation (\r\n)
310: properties = createProperties();
311: properties.load(new ByteArrayInputStream("foo=b\\\r\nar"
312: .getBytes()));
313: assertEquals(singletonProperty("foo", "bar"), properties);
314:
315: //
316: // Invalid contunuations (results in a break)
317: //
318:
319: // line continuation (\n\n)
320: properties = createProperties();
321: properties.load(new ByteArrayInputStream("foo=b\\\n\nar"
322: .getBytes()));
323: assertEquals(properties("foo", "b", "ar", ""), properties);
324:
325: // line continuation (\r\r)
326: properties = createProperties();
327: properties.load(new ByteArrayInputStream("foo=b\\\r\rar"
328: .getBytes()));
329: assertEquals(properties("foo", "b", "ar", ""), properties);
330:
331: // line continuation (\r\n\n)
332: properties = createProperties();
333: properties.load(new ByteArrayInputStream("foo=b\\\r\n\nar"
334: .getBytes()));
335: assertEquals(properties("foo", "b", "ar", ""), properties);
336:
337: // line continuation (\r\n\r)
338: properties = createProperties();
339: properties.load(new ByteArrayInputStream("foo=b\\\r\n\rar"
340: .getBytes()));
341: assertEquals(properties("foo", "b", "ar", ""), properties);
342: }
343:
344: public void testEscapedKeyValueSeparators() throws Exception {
345: Properties properties = createProperties();
346:
347: // test put and get
348: properties.put("\t\r\n my: \t\n\rkey=", "foo");
349: assertEquals("foo", properties.get("\t\r\n my: \t\n\rkey="));
350:
351: // test store
352: String text = store(properties);
353: if (text.startsWith("#"))
354: text = text.split("\\n", 2)[1];
355: text = text.trim();
356: assertEquals("\\t\\r\\n\\ my\\:\\ \\t\\n\\rkey\\==foo", text);
357:
358: // test load
359: properties = createProperties();
360: properties.load(new ByteArrayInputStream(
361: "\\t\\r\\n\\ my\\:\\ \\t\\n\\rkey\\==foo".getBytes()));
362: assertEquals("foo", properties.get("\t\r\n my: \t\n\rkey="));
363: }
364:
365: protected Properties createProperties() {
366: return new Properties();
367: }
368:
369: protected static Properties singletonProperty(String key,
370: String value) {
371: return properties(key, value);
372: }
373:
374: protected static Properties properties(String... keysAndValues) {
375: Properties properties = new Properties();
376: for (int i = 0; i + 1 < keysAndValues.length; i += 2) {
377: String key = keysAndValues[i];
378: String value = keysAndValues[i + 1];
379: properties.put(key, value);
380: }
381: return properties;
382: }
383:
384: @SuppressWarnings({"unchecked"})
385: protected static void assertProperties(Properties expected,
386: Properties actual) {
387: if (expected.equals(actual))
388: return;
389:
390: StringBuilder message = new StringBuilder().append("\n");
391:
392: Set<String> keys = new TreeSet<String>();
393: keys.addAll(new HashSet(expected.keySet()));
394: keys.addAll(new HashSet(actual.keySet()));
395: for (String key : keys) {
396: if (!expected.containsKey(key)) {
397: message.append("A ").append(key).append("=").append(
398: actual.get(key)).append("\n");
399: } else if (!actual.containsKey(key)) {
400: message.append("R ").append(key).append("=").append(
401: expected.get(key)).append("\n");
402: } else {
403: Object expectedValue = expected.get(key);
404: Object actualValue = actual.get(key);
405: if (expectedValue != expectedValue
406: && (expectedValue == null || !expectedValue
407: .equals(actual))) {
408: message.append("C ").append(key).append("=")
409: .append(expectedValue).append("\n");
410: message.append(" ").append(key).append("=")
411: .append(actualValue).append("\n");
412: }
413:
414: }
415: }
416:
417: fail(message.toString());
418: }
419:
420: protected String store(Properties properties) throws IOException {
421: ByteArrayOutputStream out = new ByteArrayOutputStream();
422: properties.store(out, null);
423: return new String(out.toByteArray());
424: }
425: }
|