001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.util;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.PrintStream;
025: import java.io.PrintWriter;
026: import java.util.Enumeration;
027: import java.util.Properties;
028:
029: import tests.support.resource.Support_Resources;
030:
031: public class PropertiesTest extends junit.framework.TestCase {
032:
033: Properties tProps;
034:
035: byte[] propsFile;
036:
037: /**
038: * @tests java.util.Properties#Properties()
039: */
040: public void test_Constructor() {
041: Properties p = new Properties();
042: // do something to avoid getting a variable unused warning
043: p.clear();
044: }
045:
046: /**
047: * @tests java.util.Properties#Properties(java.util.Properties)
048: */
049: public void test_ConstructorLjava_util_Properties() {
050: if (System.getProperty("java.vendor") != null) {
051: Properties p = new Properties(System.getProperties());
052: assertNotNull("failed to construct correct properties", p
053: .getProperty("java.vendor"));
054: }
055: }
056:
057: /**
058: * @tests java.util.Properties#getProperty(java.lang.String)
059: */
060: public void test_getPropertyLjava_lang_String() {
061: assertEquals("Did not retrieve property",
062: "this is a test property", tProps
063: .getProperty("test.prop"));
064: }
065:
066: /**
067: * @tests java.util.Properties#getProperty(java.lang.String,
068: * java.lang.String)
069: */
070: public void test_getPropertyLjava_lang_StringLjava_lang_String() {
071: assertEquals("Did not retrieve property",
072: "this is a test property", tProps.getProperty(
073: "test.prop", "Blarg"));
074: assertEquals("Did not return default value", "Gabba", tProps
075: .getProperty("notInThere.prop", "Gabba"));
076: }
077:
078: /**
079: * @tests java.util.Properties#getProperty(java.lang.String)
080: */
081: public void test_getPropertyLjava_lang_String2() {
082: // regression test for HARMONY-3518
083: MyProperties props = new MyProperties();
084: assertNull(props.getProperty("key"));
085: }
086:
087: /**
088: * @tests java.util.Properties#getProperty(java.lang.String,
089: * java.lang.String)
090: */
091: public void test_getPropertyLjava_lang_StringLjava_lang_String2() {
092: // regression test for HARMONY-3518
093: MyProperties props = new MyProperties();
094: assertEquals("defaultValue", props.getProperty("key",
095: "defaultValue"));
096: }
097:
098: // regression testing for HARMONY-3518
099: static class MyProperties extends Properties {
100: public synchronized Object get(Object key) {
101: return getProperty((String) key); // assume String
102: }
103: }
104:
105: /**
106: * @tests java.util.Properties#list(java.io.PrintStream)
107: */
108: public void test_listLjava_io_PrintStream() {
109: ByteArrayOutputStream baos = new ByteArrayOutputStream();
110: PrintStream ps = new PrintStream(baos);
111: Properties myProps = new Properties();
112: String propList;
113: myProps.setProperty("Abba", "Cadabra");
114: myProps.setProperty("Open", "Sesame");
115: myProps.list(ps);
116: ps.flush();
117: propList = baos.toString();
118: assertTrue("Property list innacurate", (propList
119: .indexOf("Abba=Cadabra") >= 0)
120: && (propList.indexOf("Open=Sesame") >= 0));
121: }
122:
123: /**
124: * @tests java.util.Properties#list(java.io.PrintWriter)
125: */
126: public void test_listLjava_io_PrintWriter() {
127: ByteArrayOutputStream baos = new ByteArrayOutputStream();
128: PrintWriter pw = new PrintWriter(baos);
129: Properties myProps = new Properties();
130: String propList;
131: myProps.setProperty("Abba", "Cadabra");
132: myProps.setProperty("Open", "Sesame");
133: myProps.list(pw);
134: pw.flush();
135: propList = baos.toString();
136: assertTrue("Property list innacurate", (propList
137: .indexOf("Abba=Cadabra") >= 0)
138: && (propList.indexOf("Open=Sesame") >= 0));
139: }
140:
141: /**
142: * @throws IOException
143: * @tests java.util.Properties#load(java.io.InputStream)
144: */
145: public void test_loadLjava_io_InputStream() throws IOException {
146: Properties prop = new Properties();
147: InputStream is = new ByteArrayInputStream(writeProperties());
148: prop.load(is);
149: is.close();
150: assertEquals("Failed to load correct properties",
151: "harmony.tests", prop.getProperty("test.pkg"));
152: assertNull("Load failed to parse incorrectly", prop
153: .getProperty("commented.entry"));
154:
155: prop = new Properties();
156: prop.load(new ByteArrayInputStream("=".getBytes()));
157: assertTrue("Failed to add empty key", prop.get("").equals(""));
158:
159: prop = new Properties();
160: prop.load(new ByteArrayInputStream(" = ".getBytes()));
161: assertTrue("Failed to add empty key2", prop.get("").equals(""));
162:
163: prop = new Properties();
164: prop.load(new ByteArrayInputStream(" a= b".getBytes()));
165: assertEquals("Failed to ignore whitespace", "b", prop.get("a"));
166:
167: prop = new Properties();
168: prop.load(new ByteArrayInputStream(" a b".getBytes()));
169: assertEquals("Failed to interpret whitespace as =", "b", prop
170: .get("a"));
171:
172: prop = new Properties();
173: prop.load(new ByteArrayInputStream(
174: "#\u008d\u00d2\na=\u008d\u00d3".getBytes("ISO8859_1")));
175: assertEquals("Failed to parse chars >= 0x80", "\u008d\u00d3",
176: prop.get("a"));
177:
178: prop = new Properties();
179: prop.load(new ByteArrayInputStream(
180: "#properties file\r\nfred=1\r\n#last comment"
181: .getBytes("ISO8859_1")));
182: assertEquals(
183: "Failed to load when last line contains a comment",
184: "1", prop.get("fred"));
185:
186: // Regression tests for HARMONY-5414
187: prop = new Properties();
188: prop.load(new ByteArrayInputStream("a=\\u1234z".getBytes()));
189:
190: prop = new Properties();
191: try {
192: prop.load(new ByteArrayInputStream("a=\\u123".getBytes()));
193: fail("Expected IllegalArgumentException due to invalid Unicode sequence");
194: } catch (IllegalArgumentException e) {
195: // Expected
196: }
197:
198: prop = new Properties();
199: try {
200: prop.load(new ByteArrayInputStream("a=\\u123z".getBytes()));
201: fail("Expected IllegalArgumentException due to invalid unicode sequence");
202: } catch (IllegalArgumentException expected) {
203: // Expected
204: }
205:
206: prop = new Properties();
207: Properties expected = new Properties();
208: expected.put("a", "\u0000");
209: prop.load(new ByteArrayInputStream("a=\\".getBytes()));
210: assertEquals("Failed to read trailing slash value", expected,
211: prop);
212:
213: prop = new Properties();
214: expected = new Properties();
215: expected.put("a", "\u1234\u0000");
216: prop.load(new ByteArrayInputStream("a=\\u1234\\".getBytes()));
217: assertEquals("Failed to read trailing slash value #2",
218: expected, prop);
219:
220: prop = new Properties();
221: expected = new Properties();
222: expected.put("a", "q");
223: prop.load(new ByteArrayInputStream("a=\\q".getBytes()));
224: assertEquals("Failed to read slash value #3", expected, prop);
225: }
226:
227: /**
228: * @throws IOException
229: * @tests java.util.Properties#load(java.io.InputStream)
230: */
231: public void test_loadLjava_io_InputStream_subtest0()
232: throws IOException {
233: InputStream is = Support_Resources
234: .getStream("hyts_PropertiesTest.properties");
235: Properties props = new Properties();
236: props.load(is);
237: is.close();
238: assertEquals("1", "\n \t \f", props.getProperty(" \r"));
239: assertEquals("2", "a", props.getProperty("a"));
240: assertEquals("3", "bb as,dn ", props.getProperty("b"));
241: assertEquals("4", ":: cu", props.getProperty("c\r \t\nu"));
242: assertEquals("5", "bu", props.getProperty("bu"));
243: assertEquals("6", "d\r\ne=e", props.getProperty("d"));
244: assertEquals("7", "fff", props.getProperty("f"));
245: assertEquals("8", "g", props.getProperty("g"));
246: assertEquals("9", "", props.getProperty("h h"));
247: assertEquals("10", "i=i", props.getProperty(" "));
248: assertEquals("11", " j", props.getProperty("j"));
249: assertEquals("12", " c", props.getProperty("space"));
250: assertEquals("13", "\\", props.getProperty("dblbackslash"));
251: }
252:
253: /**
254: * @tests java.util.Properties#propertyNames()
255: */
256: public void test_propertyNames() {
257: Enumeration names = tProps.propertyNames();
258: while (names.hasMoreElements()) {
259: String p = (String) names.nextElement();
260: assertTrue("Incorrect names returned", p
261: .equals("test.prop")
262: || p.equals("bogus.prop"));
263: }
264: }
265:
266: /**
267: * @throws IOException
268: * @tests java.util.Properties#save(java.io.OutputStream, java.lang.String)
269: */
270: public void test_saveLjava_io_OutputStreamLjava_lang_String()
271: throws IOException {
272: Properties myProps = new Properties();
273: myProps.setProperty("Property A", "aye");
274: myProps.setProperty("Property B", "bee");
275: myProps.setProperty("Property C", "see");
276:
277: ByteArrayOutputStream out = new ByteArrayOutputStream();
278: myProps.save(out, "A Header");
279: out.close();
280:
281: Properties myProps2 = new Properties();
282: ByteArrayInputStream in = new ByteArrayInputStream(out
283: .toByteArray());
284: myProps2.load(in);
285: in.close();
286:
287: Enumeration e = myProps.propertyNames();
288: while (e.hasMoreElements()) {
289: String nextKey = (String) e.nextElement();
290: assertTrue("Stored property list not equal to original",
291: myProps2.getProperty(nextKey).equals(
292: myProps.getProperty(nextKey)));
293: }
294: }
295:
296: /**
297: * @tests java.util.Properties#setProperty(java.lang.String,
298: * java.lang.String)
299: */
300: public void test_setPropertyLjava_lang_StringLjava_lang_String() {
301: Properties myProps = new Properties();
302: myProps.setProperty("Yoink", "Yabba");
303: assertEquals("Failed to set property", "Yabba", myProps
304: .getProperty("Yoink"));
305: myProps.setProperty("Yoink", "Gab");
306: assertEquals("Failed to reset property", "Gab", myProps
307: .getProperty("Yoink"));
308: }
309:
310: /**
311: * @throws IOException
312: * @tests java.util.Properties#store(java.io.OutputStream, java.lang.String)
313: */
314: public void test_storeLjava_io_OutputStreamLjava_lang_String()
315: throws IOException {
316: Properties myProps = new Properties();
317: myProps.put("Property A", " aye\\\f\t\n\r\b");
318: myProps.put("Property B", "b ee#!=:");
319: myProps.put("Property C", "see");
320:
321: Properties myProps2 = new Properties();
322: ByteArrayOutputStream out = new ByteArrayOutputStream();
323: myProps.store(out, "A Header");
324: out.close();
325:
326: ByteArrayInputStream in = new ByteArrayInputStream(out
327: .toByteArray());
328: myProps2.load(in);
329: in.close();
330:
331: Enumeration e = myProps.propertyNames();
332: while (e.hasMoreElements()) {
333: String nextKey = (String) e.nextElement();
334: assertTrue("Stored property list not equal to original",
335: myProps2.getProperty(nextKey).equals(
336: myProps.getProperty(nextKey)));
337: }
338: }
339:
340: /**
341: * @throws IOException
342: * @tests java.util.Properties#loadFromXML(java.io.InputStream)
343: */
344: public void test_loadFromXMLLjava_io_InputStream()
345: throws IOException {
346: Properties prop = new Properties();
347: InputStream is = new ByteArrayInputStream(
348: writePropertiesXML("UTF-8"));
349: prop.loadFromXML(is);
350: is.close();
351:
352: assertEquals("Failed to load correct properties", "value3",
353: prop.getProperty("key3"));
354: assertEquals("Failed to load correct properties", "value1",
355: prop.getProperty("key1"));
356:
357: prop = new Properties();
358: is = new ByteArrayInputStream(writePropertiesXML("ISO-8859-1"));
359: prop.loadFromXML(is);
360: is.close();
361:
362: assertEquals("Failed to load correct properties", "value2",
363: prop.getProperty("key2"));
364: assertEquals("Failed to load correct properties", "value1",
365: prop.getProperty("key1"));
366: }
367:
368: /**
369: * @throws IOException
370: * @tests java.util.Properties#storeToXML(java.io.OutputStream,
371: * java.lang.String, java.lang.String)
372: */
373: public void test_storeToXMLLjava_io_OutputStreamLjava_lang_StringLjava_lang_String()
374: throws IOException {
375: Properties myProps = new Properties();
376: myProps.setProperty("key1", "value1");
377: myProps.setProperty("key2", "value2");
378: myProps.setProperty("key3", "value3");
379: myProps.setProperty("<a>key4</a>", "\"value4");
380: myProps.setProperty("key5 ", "<h>value5</h>");
381: myProps.setProperty("<a>key6</a>", " <h>value6</h> ");
382: myProps.setProperty("<comment>key7</comment>", "value7");
383: myProps.setProperty(" key8 ", "<comment>value8</comment>");
384: myProps.setProperty("<key9>", "\u0027value9");
385: myProps.setProperty("key10\"", "<value10>");
386: myProps.setProperty("&key11&", "value11");
387: myProps.setProperty("key12", "&value12&");
388: myProps.setProperty("<a>&key13<</a>",
389: "&&value13<b>&</b>");
390:
391: // store in UTF-8 encoding
392: ByteArrayOutputStream out = new ByteArrayOutputStream();
393: myProps.storeToXML(out, "comment");
394: out.close();
395:
396: ByteArrayInputStream in = new ByteArrayInputStream(out
397: .toByteArray());
398: Properties myProps2 = new Properties();
399: myProps2.loadFromXML(in);
400: in.close();
401:
402: Enumeration e = myProps.propertyNames();
403: while (e.hasMoreElements()) {
404: String nextKey = (String) e.nextElement();
405: assertTrue("Stored property list not equal to original",
406: myProps2.getProperty(nextKey).equals(
407: myProps.getProperty(nextKey)));
408: }
409:
410: // store in ISO-8859-1 encoding
411: out = new ByteArrayOutputStream();
412: myProps.storeToXML(out, "comment", "ISO-8859-1");
413: out.close();
414:
415: in = new ByteArrayInputStream(out.toByteArray());
416: myProps2 = new Properties();
417: myProps2.loadFromXML(in);
418: in.close();
419:
420: e = myProps.propertyNames();
421: while (e.hasMoreElements()) {
422: String nextKey = (String) e.nextElement();
423: assertTrue("Stored property list not equal to original",
424: myProps2.getProperty(nextKey).equals(
425: myProps.getProperty(nextKey)));
426: }
427: }
428:
429: /**
430: * if loading from single line like "hello" without "\n\r" neither "=", it should be same
431: * as loading from "hello="
432: */
433: public void testLoadSingleLine() throws Exception {
434: Properties props = new Properties();
435: InputStream sr = new ByteArrayInputStream("hello".getBytes());
436: props.load(sr);
437: assertEquals(1, props.size());
438: }
439:
440: /**
441: * Sets up the fixture, for example, open a network connection. This method
442: * is called before a test is executed.
443: */
444: protected void setUp() throws Exception {
445: super .setUp();
446: tProps = new Properties();
447: tProps.put("test.prop", "this is a test property");
448: tProps.put("bogus.prop", "bogus");
449: }
450:
451: /**
452: * Tears down the fixture, for example, close a network connection. This
453: * method is called after a test is executed.
454: */
455: protected void tearDown() throws Exception {
456: tProps = null;
457: super .tearDown();
458: }
459:
460: protected byte[] writeProperties() throws IOException {
461: ByteArrayOutputStream bout = new ByteArrayOutputStream();
462: PrintStream ps = new PrintStream(bout);
463: ps.println("#commented.entry=Bogus");
464: ps.println("test.pkg=harmony.tests");
465: ps.println("test.proj=Automated Tests");
466: ps.close();
467: return bout.toByteArray();
468: }
469:
470: protected byte[] writePropertiesXML(String encoding)
471: throws IOException {
472: ByteArrayOutputStream bout = new ByteArrayOutputStream();
473: PrintStream ps = new PrintStream(bout, true, encoding);
474: ps.println("<?xml version=\"1.0\" encoding=\"" + encoding
475: + "\"?>");
476: ps
477: .println("<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">");
478: ps.println("<properties>");
479: ps.println("<comment>comment</comment>");
480: ps.println("<entry key=\"key4\">value4</entry>");
481: ps.println("<entry key=\"key3\">value3</entry>");
482: ps.println("<entry key=\"key2\">value2</entry>");
483: ps
484: .println("<entry key=\"key1\"><!-- xml comment -->value1</entry>");
485: ps.println("</properties>");
486: ps.close();
487: return bout.toByteArray();
488: }
489: }
|