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.ByteArrayOutputStream;
021: import java.util.LinkedHashMap;
022: import java.util.Map;
023: import java.util.Properties;
024:
025: public class SuperPropertiesTest extends PropertiesTest {
026:
027: protected void setUp() throws Exception {
028: super .setUp();
029: }
030:
031: public void testCaseSensitivity() throws Exception {
032: SuperProperties properties = createProperties();
033: properties.setProperty("foo", "bar");
034: properties.setComment("foo", "comment");
035: properties.getAttributes("foo").put("name", "value");
036:
037: // case sensitive
038: assertTrue(properties.containsKey("foo"));
039: assertEquals("bar", properties.get("foo"));
040: assertEquals("bar", properties.getProperty("foo"));
041: assertEquals("comment", properties.getComment("foo"));
042: assertNotNull(properties.getAttributes("foo"));
043: assertEquals("value", properties.getAttributes("foo").get(
044: "name"));
045: assertFalse(properties.containsKey("FOO"));
046: assertNull(properties.get("FOO"));
047: assertNull(properties.getProperty("FOO"));
048: assertNull(properties.getComment("FOO"));
049: assertNull(properties.getAttributes("FOO"));
050:
051: // property differing only incase
052: properties.setProperty("FOO", "BAR");
053: properties.setComment("FOO", "COMMENT");
054: properties.getAttributes("FOO").put("NAME", "VALUE");
055: assertTrue(properties.containsKey("foo"));
056: assertEquals("bar", properties.get("foo"));
057: assertEquals("bar", properties.getProperty("foo"));
058: assertEquals("comment", properties.getComment("foo"));
059: assertNotNull(properties.getAttributes("foo"));
060: assertEquals("value", properties.getAttributes("foo").get(
061: "name"));
062: assertTrue(properties.containsKey("FOO"));
063: assertEquals("BAR", properties.get("FOO"));
064: assertEquals("BAR", properties.getProperty("FOO"));
065: assertEquals("COMMENT", properties.getComment("FOO"));
066: assertNotNull(properties.getAttributes("FOO"));
067: assertEquals("VALUE", properties.getAttributes("FOO").get(
068: "NAME"));
069:
070: // case insensitive
071: properties = createProperties();
072: properties.setCaseInsensitive(true);
073: properties.setProperty("foo", "bar");
074: properties.setComment("foo", "comment");
075: properties.getAttributes("foo").put("name", "value");
076: assertTrue(properties.containsKey("foo"));
077: assertEquals("bar", properties.get("foo"));
078: assertEquals("bar", properties.getProperty("foo"));
079: assertEquals("comment", properties.getComment("foo"));
080: assertNotNull(properties.getAttributes("foo"));
081: assertEquals("value", properties.getAttributes("foo").get(
082: "name"));
083: assertTrue(properties.containsKey("FOO"));
084: assertEquals("bar", properties.get("FOO"));
085: assertEquals("bar", properties.getProperty("FOO"));
086: assertEquals("comment", properties.getComment("FOO"));
087: assertNotNull(properties.getAttributes("FOO"));
088: assertEquals("value", properties.getAttributes("FOO").get(
089: "name"));
090:
091: // property differing only incase
092: properties.setProperty("FOO", "BAR");
093: properties.setComment("FOO", "COMMENT");
094: properties.getAttributes("FOO").put("name", "VALUE");
095: assertTrue(properties.containsKey("foo"));
096: assertEquals("BAR", properties.get("foo"));
097: assertEquals("BAR", properties.getProperty("foo"));
098: assertEquals("COMMENT", properties.getComment("foo"));
099: assertNotNull(properties.getAttributes("foo"));
100: assertEquals("VALUE", properties.getAttributes("foo").get(
101: "name"));
102: assertTrue(properties.containsKey("FOO"));
103: assertEquals("BAR", properties.get("FOO"));
104: assertEquals("BAR", properties.getProperty("FOO"));
105: assertEquals("COMMENT", properties.getComment("FOO"));
106: assertNotNull(properties.getAttributes("FOO"));
107: assertEquals("VALUE", properties.getAttributes("FOO").get(
108: "name"));
109:
110: }
111:
112: public void testSynchronization() throws Exception {
113: SuperProperties properties = createProperties();
114: properties.setProperty("foo", "bar");
115: properties.setComment("foo", "comment");
116: assertNotNull(properties.getAttributes("foo"));
117: properties.getAttributes("foo").put("name", "value");
118:
119: // changing a property value should not effect comments or attributes
120: properties.put("foo", "bar2");
121: assertEquals("comment", properties.getComment("foo"));
122: assertNotNull(properties.getAttributes("foo"));
123: assertEquals("value", properties.getAttributes("foo").get(
124: "name"));
125:
126: // removing a property should remove comments and attributes
127: properties.remove("foo");
128: assertNull(properties.getComment("foo"));
129: assertNull(properties.getAttributes("foo"));
130: }
131:
132: public void testLoadStoreLoad() throws Exception {
133: SuperProperties expected = new SuperProperties();
134: expected
135: .load(getClass().getResourceAsStream("test.properties"));
136:
137: ByteArrayOutputStream out = new ByteArrayOutputStream();
138: expected.store(out, null);
139:
140: SuperProperties actual = createProperties();
141: actual.load(new ByteArrayInputStream(out.toByteArray()));
142:
143: assertProperties(expected, actual);
144: }
145:
146: public void testLoadStoreLoadXml() throws Exception {
147: SuperProperties expected = new SuperProperties();
148: expected
149: .load(getClass().getResourceAsStream("test.properties"));
150:
151: ByteArrayOutputStream out = new ByteArrayOutputStream();
152: expected.storeToXML(out, null);
153:
154: SuperProperties actual = createProperties();
155: actual.loadFromXML(new ByteArrayInputStream(out.toByteArray()));
156:
157: assertProperties(expected, actual);
158: }
159:
160: public void testStore() throws Exception {
161: SuperProperties properties = createProperties();
162: assertTrue(properties.isSpaceBetweenProperties());
163: assertFalse(properties.isSpaceAfterComment());
164:
165: // one property
166: properties.setProperty("foo", "bar");
167: assertEquals("foo=bar\n", store(properties));
168:
169: properties.setIndent(4);
170: assertEquals(" foo=bar\n", store(properties));
171:
172: // two properties
173: properties.setProperty("number", "42");
174: assertEquals(" foo=bar\n\n number=42\n",
175: store(properties));
176:
177: properties.setSpaceBetweenProperties(false);
178: assertEquals(" foo=bar\n number=42\n", store(properties));
179:
180: // one comment
181: properties.setComment("foo", "foo comment");
182: assertEquals(" # foo comment\n foo=bar\n number=42\n",
183: store(properties));
184:
185: properties.setCommentIndent(0);
186: assertEquals(" #foo comment\n foo=bar\n number=42\n",
187: store(properties));
188:
189: properties.setCommentIndent(2);
190: assertEquals(
191: " # foo comment\n foo=bar\n number=42\n",
192: store(properties));
193:
194: properties.setSpaceAfterComment(true);
195: assertEquals(
196: " # foo comment\n\n foo=bar\n number=42\n",
197: store(properties));
198:
199: properties.setSpaceBetweenProperties(true);
200: assertEquals(
201: " # foo comment\n\n foo=bar\n\n number=42\n",
202: store(properties));
203:
204: // one attribute
205: properties.getAttributes("foo").put("name", "value");
206: assertEquals(
207: " # foo comment\n # @name=value\n\n foo=bar\n\n number=42\n",
208: store(properties));
209:
210: properties.getAttributes("foo").put("name", null);
211: assertEquals(
212: " # foo comment\n # @name\n\n foo=bar\n\n number=42\n",
213: store(properties));
214:
215: properties.getAttributes("foo").put("name", "");
216: assertEquals(
217: " # foo comment\n # @name\n\n foo=bar\n\n number=42\n",
218: store(properties));
219:
220: // two attribute
221: properties.getAttributes("number").put("hidden", "yes");
222: assertEquals(
223: " # foo comment\n # @name\n\n foo=bar\n\n # @hidden=yes\n\n number=42\n",
224: store(properties));
225:
226: // key value separator
227: properties = createProperties();
228: properties.setProperty("foo", "bar");
229: properties.setKeyValueSeparator(" ");
230: assertEquals("foo bar\n", store(properties));
231:
232: properties.setKeyValueSeparator(":");
233: assertEquals("foo:bar\n", store(properties));
234:
235: properties.setKeyValueSeparator(" = ");
236: assertEquals("foo = bar\n", store(properties));
237:
238: properties.setKeyValueSeparator("XXXX");
239: assertEquals("fooXXXXbar\n", store(properties));
240: }
241:
242: public void testLoadComments() throws Exception {
243: SuperProperties properties;
244:
245: properties = createProperties();
246: properties.load(new ByteArrayInputStream("# Comment\nfoo=bar"
247: .getBytes()));
248: assertEquals(singletonProperty("foo", "bar"), properties);
249: assertEquals("Comment", properties.getComment("foo"));
250:
251: properties = createProperties();
252: properties.load(new ByteArrayInputStream(
253: "# Line1\n# Line2\nfoo=bar".getBytes()));
254: assertEquals(singletonProperty("foo", "bar"), properties);
255: assertEquals("Line1\nLine2", properties.getComment("foo"));
256:
257: properties = createProperties();
258: properties.load(new ByteArrayInputStream(
259: "# Comment\n# Indented\nfoo=bar".getBytes()));
260: assertEquals(singletonProperty("foo", "bar"), properties);
261: assertEquals("Comment\n Indented", properties
262: .getComment("foo"));
263:
264: properties = createProperties();
265: properties.load(new ByteArrayInputStream(
266: "# Comment\n#Outdented\nfoo=bar".getBytes()));
267: assertEquals(singletonProperty("foo", "bar"), properties);
268: assertEquals("Comment\nOutdented", properties.getComment("foo"));
269: }
270:
271: public void testLoadCommentsXml() throws Exception {
272: SuperProperties properties;
273:
274: properties = createProperties();
275: properties.loadFromXML(new ByteArrayInputStream(getXml("foo",
276: "bar", null).getBytes()));
277: assertEquals(singletonProperty("foo", "bar"), properties);
278:
279: properties = createProperties();
280: properties.loadFromXML(new ByteArrayInputStream(getXml("foo",
281: "bar", "\nComment\n").getBytes()));
282: assertEquals(singletonProperty("foo", "bar"), properties);
283: assertEquals("Comment\n", properties.getComment("foo"));
284:
285: properties = createProperties();
286: properties.loadFromXML(new ByteArrayInputStream(getXml("foo",
287: "bar", "Line1\n Line2\n").getBytes()));
288: assertEquals(singletonProperty("foo", "bar"), properties);
289: assertEquals("Line1\nLine2\n", properties.getComment("foo"));
290:
291: properties = createProperties();
292: properties.loadFromXML(new ByteArrayInputStream(getXml("foo",
293: "bar", "Line1\n Line2\n Indented").getBytes()));
294: assertEquals(singletonProperty("foo", "bar"), properties);
295: assertEquals("Line1\nLine2\n Indented", properties
296: .getComment("foo"));
297:
298: properties = createProperties();
299: properties.loadFromXML(new ByteArrayInputStream(getXml("foo",
300: "bar", "Line1\n Line2\nOutdented").getBytes()));
301: assertEquals(singletonProperty("foo", "bar"), properties);
302: assertEquals("Line1\nLine2\nOutdented", properties
303: .getComment("foo"));
304: }
305:
306: public void testLoadAttributes() throws Exception {
307: SuperProperties properties;
308:
309: properties = createProperties();
310: properties.load(new ByteArrayInputStream(
311: "# @name=value\nfoo=bar".getBytes()));
312: assertEquals(singletonProperty("foo", "bar"), properties);
313: assertEquals(map("name", "value"), properties
314: .getAttributes("foo"));
315:
316: properties = createProperties();
317: properties.load(new ByteArrayInputStream("# @name\nfoo=bar"
318: .getBytes()));
319: assertEquals(singletonProperty("foo", "bar"), properties);
320: assertEquals(map("name", ""), properties.getAttributes("foo"));
321:
322: properties = createProperties();
323: properties.load(new ByteArrayInputStream("# @name=\nfoo=bar"
324: .getBytes()));
325: assertEquals(singletonProperty("foo", "bar"), properties);
326: assertEquals(map("name", ""), properties.getAttributes("foo"));
327:
328: properties = createProperties();
329: properties.load(new ByteArrayInputStream(
330: "# @a=b\n# @c=d\nfoo=bar".getBytes()));
331: assertEquals(singletonProperty("foo", "bar"), properties);
332: assertEquals(map("a", "b", "c", "d"), properties
333: .getAttributes("foo"));
334: }
335:
336: public void testLoadAttributesXml() throws Exception {
337: SuperProperties properties;
338:
339: properties = createProperties();
340: properties.loadFromXML(new ByteArrayInputStream(getXml("foo",
341: "bar", "@name=value").getBytes()));
342: assertEquals(singletonProperty("foo", "bar"), properties);
343: assertEquals(map("name", "value"), properties
344: .getAttributes("foo"));
345:
346: properties = createProperties();
347: properties.loadFromXML(new ByteArrayInputStream(getXml("foo",
348: "bar", "@name").getBytes()));
349: assertEquals(singletonProperty("foo", "bar"), properties);
350: assertEquals(map("name", ""), properties.getAttributes("foo"));
351:
352: properties = createProperties();
353: properties.loadFromXML(new ByteArrayInputStream(getXml("foo",
354: "bar", "@name=").getBytes()));
355: assertEquals(singletonProperty("foo", "bar"), properties);
356: assertEquals(map("name", ""), properties.getAttributes("foo"));
357:
358: properties = createProperties();
359: properties.loadFromXML(new ByteArrayInputStream(getXml("foo",
360: "bar", "@a = b \n@ c = d ").getBytes()));
361: assertEquals(singletonProperty("foo", "bar"), properties);
362: assertEquals(map("a", "b", "c", "d"), properties
363: .getAttributes("foo"));
364: }
365:
366: public void testIndentDetection() throws Exception {
367: SuperProperties properties;
368: properties = createProperties();
369: assertEquals(0, properties.getIndent());
370: assertEquals(1, properties.getCommentIndent());
371:
372: properties = createProperties();
373: properties.load(new ByteArrayInputStream(" foo=bar"
374: .getBytes()));
375: assertEquals(singletonProperty("foo", "bar"), properties);
376: assertEquals(4, properties.getIndent());
377: assertEquals(1, properties.getCommentIndent());
378:
379: properties = createProperties();
380: properties.load(new ByteArrayInputStream(
381: " # Comment\n foo=bar".getBytes()));
382: assertEquals(singletonProperty("foo", "bar"), properties);
383: assertEquals(4, properties.getIndent());
384: assertEquals(2, properties.getCommentIndent());
385:
386: properties = createProperties();
387: properties.load(new ByteArrayInputStream(
388: " # Line1\n# Line2\n foo=bar".getBytes()));
389: assertEquals(singletonProperty("foo", "bar"), properties);
390: assertEquals(4, properties.getIndent());
391: assertEquals(2, properties.getCommentIndent());
392:
393: properties = createProperties();
394: properties.load(new ByteArrayInputStream(
395: " # Comment\n# Indented\n foo=bar"
396: .getBytes()));
397: assertEquals(singletonProperty("foo", "bar"), properties);
398: assertEquals(4, properties.getIndent());
399: assertEquals(2, properties.getCommentIndent());
400:
401: properties = createProperties();
402: properties.load(new ByteArrayInputStream(
403: " # Comment\n#Outdented\nfoo=bar".getBytes()));
404: assertEquals(singletonProperty("foo", "bar"), properties);
405: assertEquals(4, properties.getIndent());
406: assertEquals(2, properties.getCommentIndent());
407: }
408:
409: protected SuperProperties createProperties() {
410: SuperProperties super Properties = new SuperProperties();
411: super Properties.setLineSeparator("\n");
412: return super Properties;
413: }
414:
415: protected static Map<String, String> map(String... keysAndValues) {
416: Map<String, String> map = new LinkedHashMap<String, String>();
417: for (int i = 0; i + 1 < keysAndValues.length; i += 2) {
418: String key = keysAndValues[i];
419: String value = keysAndValues[i + 1];
420: map.put(key, value);
421: }
422: return map;
423: }
424:
425: private String getXml(String key, String value, String comment) {
426: StringBuilder buf = new StringBuilder();
427: buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
428: buf
429: .append("<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">\n");
430: buf.append("<properties>\n");
431:
432: if (comment != null) {
433: buf.append(" <!--").append(comment).append("-->\n");
434: }
435: buf.append(" <entry key=\"").append(key).append("\">").append(
436: value).append("</entry>\n");
437:
438: buf.append("</properties>\n");
439:
440: return buf.toString();
441: }
442: }
|