PropertiesTest.java revision 5d709784bbf5001012d7f25172927d46f6c1abe1
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
18package tests.api.java.util;
19
20import dalvik.annotation.KnownFailure;
21import dalvik.annotation.TestTargetNew;
22import dalvik.annotation.TestLevel;
23import dalvik.annotation.TestTargetClass;
24
25import java.io.ByteArrayInputStream;
26import java.io.ByteArrayOutputStream;
27import java.io.IOException;
28import java.io.InputStream;
29import java.io.PrintStream;
30import java.io.PrintWriter;
31import java.util.Enumeration;
32import java.util.InvalidPropertiesFormatException;
33import java.util.Properties;
34
35import tests.support.resource.Support_Resources;
36
37@TestTargetClass(Properties.class)
38public class PropertiesTest extends junit.framework.TestCase {
39
40    Properties tProps;
41
42    byte[] propsFile;
43
44    /**
45     * @tests java.util.Properties#Properties()
46     */
47    @TestTargetNew(
48        level = TestLevel.COMPLETE,
49        notes = "",
50        method = "Properties",
51        args = {}
52    )
53    public void test_Constructor() {
54        Properties p = new Properties();
55        // do something to avoid getting a variable unused warning
56        p.clear();
57    }
58
59    /**
60     * @tests java.util.Properties#Properties(java.util.Properties)
61     */
62    @TestTargetNew(
63        level = TestLevel.COMPLETE,
64        notes = "",
65        method = "Properties",
66        args = {java.util.Properties.class}
67    )
68    public void test_ConstructorLjava_util_Properties() {
69        if (System.getProperty("java.vendor") != null) {
70            Properties p = new Properties(System.getProperties());
71            assertNotNull("failed to construct correct properties", p
72                    .getProperty("java.vendor"));
73        }
74    }
75
76    /**
77     * @tests java.util.Properties#getProperty(java.lang.String)
78     */
79    @TestTargetNew(
80        level = TestLevel.COMPLETE,
81        notes = "Verifies positive case.",
82        method = "getProperty",
83        args = {java.lang.String.class}
84    )
85    public void test_getPropertyLjava_lang_String() {
86        assertEquals("Did not retrieve property", "this is a test property",
87                tProps.getProperty("test.prop"));
88    }
89
90    /**
91     * @tests java.util.Properties#getProperty(java.lang.String,
92     *        java.lang.String)
93     */
94    @TestTargetNew(
95        level = TestLevel.COMPLETE,
96        notes = "",
97        method = "getProperty",
98        args = {java.lang.String.class, java.lang.String.class}
99    )
100    public void test_getPropertyLjava_lang_StringLjava_lang_String() {
101        assertEquals("Did not retrieve property", "this is a test property",
102                tProps.getProperty("test.prop", "Blarg"));
103        assertEquals("Did not return default value", "Gabba", tProps
104                .getProperty("notInThere.prop", "Gabba"));
105        assertNull(tProps.getProperty("", null));
106    }
107
108    /**
109     * @tests java.util.Properties#getProperty(java.lang.String)
110     */
111    @TestTargetNew(
112        level = TestLevel.PARTIAL_COMPLETE,
113        notes = "Regression test.",
114        method = "getProperty",
115        args = {java.lang.String.class}
116    )
117    public void test_getPropertyLjava_lang_String2() {
118        // regression test for HARMONY-3518
119        MyProperties props = new MyProperties();
120        assertNull(props.getProperty("key"));
121    }
122
123    /**
124     * @tests java.util.Properties#getProperty(java.lang.String,
125     *        java.lang.String)
126     */
127    @TestTargetNew(
128        level = TestLevel.PARTIAL_COMPLETE,
129        notes = "Regression test.",
130        method = "getProperty",
131        args = {java.lang.String.class, java.lang.String.class}
132    )
133    public void test_getPropertyLjava_lang_StringLjava_lang_String2() {
134        // regression test for HARMONY-3518
135        MyProperties props = new MyProperties();
136        assertEquals(props.getProperty("key", "defaultValue"), "defaultValue");
137    }
138
139    // regression testing for HARMONY-3518
140    static class MyProperties extends Properties {
141        public synchronized Object get(Object key) {
142            return getProperty((String) key); // assume String
143        }
144    }
145
146    /**
147     * @tests java.util.Properties#list(java.io.PrintStream)
148     */
149    @TestTargetNew(
150        level = TestLevel.COMPLETE,
151        notes = "",
152        method = "list",
153        args = {java.io.PrintStream.class}
154    )
155    public void test_listLjava_io_PrintStream() {
156        ByteArrayOutputStream baos = new ByteArrayOutputStream();
157        PrintStream ps = new PrintStream(baos);
158        Properties myProps = new Properties();
159        String propList;
160        myProps.setProperty("Abba", "Cadabra");
161        myProps.setProperty("Open", "Sesame");
162        myProps.list(ps);
163        ps.flush();
164        propList = baos.toString();
165        assertTrue("Property list innacurate", (propList
166                .indexOf("Abba=Cadabra") >= 0)
167                && (propList.indexOf("Open=Sesame") >= 0));
168    }
169
170    /**
171     * @tests java.util.Properties#list(java.io.PrintWriter)
172     */
173    @TestTargetNew(
174        level = TestLevel.COMPLETE,
175        notes = "",
176        method = "list",
177        args = {java.io.PrintWriter.class}
178    )
179    public void test_listLjava_io_PrintWriter() {
180        ByteArrayOutputStream baos = new ByteArrayOutputStream();
181        PrintWriter pw = new PrintWriter(baos);
182        Properties myProps = new Properties();
183        String propList;
184        myProps.setProperty("Abba", "Cadabra");
185        myProps.setProperty("Open", "Sesame");
186        myProps.list(pw);
187        pw.flush();
188        propList = baos.toString();
189        assertTrue("Property list innacurate", (propList
190                .indexOf("Abba=Cadabra") >= 0)
191                && (propList.indexOf("Open=Sesame") >= 0));
192    }
193
194    /**
195     * @throws IOException
196     * @tests java.util.Properties#load(java.io.InputStream)
197     */
198    @TestTargetNew(
199        level = TestLevel.COMPLETE,
200        notes = "",
201        method = "load",
202        args = {java.io.InputStream.class}
203    )
204    public void test_loadLjava_io_InputStream() throws IOException {
205        Properties prop = new Properties();
206        InputStream is = new ByteArrayInputStream(writeProperties());
207        prop.load(is);
208        is.close();
209
210        assertEquals("Failed to load correct properties", "harmony.tests", prop
211                .getProperty("test.pkg"));
212        assertNull("Load failed to parse incorrectly", prop
213                .getProperty("commented.entry"));
214
215        prop = new Properties();
216        prop.load(new ByteArrayInputStream("=".getBytes()));
217        assertTrue("Failed to add empty key", prop.get("").equals(""));
218
219        prop = new Properties();
220        prop.load(new ByteArrayInputStream(" = ".getBytes()));
221        assertTrue("Failed to add empty key2", prop.get("").equals(""));
222
223        prop = new Properties();
224        prop.load(new ByteArrayInputStream(" a= b".getBytes()));
225        assertEquals("Failed to ignore whitespace", "b", prop.get("a"));
226
227        prop = new Properties();
228        prop.load(new ByteArrayInputStream(" a b".getBytes()));
229        assertEquals("Failed to interpret whitespace as =", "b", prop.get("a"));
230
231        prop = new Properties();
232        prop.load(new ByteArrayInputStream("#\u008d\u00d2\na=\u008d\u00d3"
233                .getBytes("ISO8859_1")));
234        assertEquals("Failed to parse chars >= 0x80", "\u008d\u00d3", prop
235                .get("a"));
236
237        prop = new Properties();
238        prop.load(new ByteArrayInputStream(
239                "#properties file\r\nfred=1\r\n#last comment"
240                        .getBytes("ISO8859_1")));
241        assertEquals("Failed to load when last line contains a comment", "1",
242                prop.get("fred"));
243
244        ByteArrayInputStream bais = new ByteArrayInputStream(new byte[]{'\\', 'u', 'x', 'x', 'x', 'x'});
245        try {
246            prop.load(bais);
247            fail("IllegalArgumentException expected");
248        } catch (IllegalArgumentException e) {
249            //expected
250        }
251    }
252
253    /**
254     * @throws IOException
255     * @tests java.util.Properties#load(java.io.InputStream)
256     */
257    @TestTargetNew(
258        level = TestLevel.PARTIAL_COMPLETE,
259        notes = "Doesn't verify IOException, IllegalArgumentException.",
260        method = "load",
261        args = {java.io.InputStream.class}
262    )
263    public void test_loadLjava_io_InputStream_subtest0() throws IOException {
264        InputStream is = Support_Resources
265                .getStream("hyts_PropertiesTest.properties");
266        Properties props = new Properties();
267        props.load(is);
268        is.close();
269        assertEquals("1", "\n \t \f", props.getProperty(" \r"));
270        assertEquals("2", "a", props.getProperty("a"));
271        assertEquals("3", "bb as,dn   ", props.getProperty("b"));
272        assertEquals("4", ":: cu", props.getProperty("c\r \t\nu"));
273        assertEquals("5", "bu", props.getProperty("bu"));
274        assertEquals("6", "d\r\ne=e", props.getProperty("d"));
275        assertEquals("7", "fff", props.getProperty("f"));
276        assertEquals("8", "g", props.getProperty("g"));
277        assertEquals("9", "", props.getProperty("h h"));
278        assertEquals("10", "i=i", props.getProperty(" "));
279        assertEquals("11", "   j", props.getProperty("j"));
280        assertEquals("12", "   c", props.getProperty("space"));
281        assertEquals("13", "\\", props.getProperty("dblbackslash"));
282    }
283
284    /**
285     * @throws IOException
286     * @tests java.util.Properties#save(java.io.OutputStream, java.lang.String)
287     */
288    @TestTargetNew(
289        level = TestLevel.COMPLETE,
290        notes = "",
291        method = "save",
292        args = {java.io.OutputStream.class, java.lang.String.class}
293    )
294    public void test_saveLjava_io_OutputStreamLjava_lang_String()
295            throws IOException {
296        Properties myProps = new Properties();
297        myProps.setProperty("Property A", "aye");
298        myProps.setProperty("Property B", "bee");
299        myProps.setProperty("Property C", "see");
300
301        ByteArrayOutputStream out = new ByteArrayOutputStream();
302        myProps.save(out, "A Header");
303        out.close();
304
305        Properties myProps2 = new Properties();
306        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
307        myProps2.load(in);
308        in.close();
309
310        Enumeration e = myProps.propertyNames();
311        while (e.hasMoreElements()) {
312            String nextKey = (String) e.nextElement();
313            assertTrue("Stored property list not equal to original", myProps2
314                    .getProperty(nextKey).equals(myProps.getProperty(nextKey)));
315        }
316    }
317
318    /**
319     * @tests java.util.Properties#setProperty(java.lang.String,
320     *        java.lang.String)
321     */
322    @TestTargetNew(
323        level = TestLevel.COMPLETE,
324        notes = "",
325        method = "setProperty",
326        args = {java.lang.String.class, java.lang.String.class}
327    )
328    public void test_setPropertyLjava_lang_StringLjava_lang_String() {
329        Properties myProps = new Properties();
330        myProps.setProperty("Yoink", "Yabba");
331        assertEquals("Failed to set property", "Yabba", myProps
332                .getProperty("Yoink"));
333        myProps.setProperty("Yoink", "Gab");
334        assertEquals("Failed to reset property", "Gab", myProps
335                .getProperty("Yoink"));
336    }
337
338    /**
339     * @throws IOException
340     * @tests java.util.Properties#store(java.io.OutputStream, java.lang.String)
341     */
342    @TestTargetNew(
343        level = TestLevel.COMPLETE,
344        notes = "",
345        method = "store",
346        args = {java.io.OutputStream.class, java.lang.String.class}
347    )
348    public void test_storeLjava_io_OutputStreamLjava_lang_String()
349            throws IOException {
350        Properties myProps = new Properties();
351        myProps.put("Property A", " aye\\\f\t\n\r\b");
352        myProps.put("Property B", "b ee#!=:");
353        myProps.put("Property C", "see");
354
355        Properties myProps2 = new Properties();
356        ByteArrayOutputStream out = new ByteArrayOutputStream();
357        myProps.store(out, "A Header");
358        myProps.store(out, null);
359        out.close();
360
361        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
362        myProps2.load(in);
363        in.close();
364
365        Enumeration e = myProps.propertyNames();
366        while (e.hasMoreElements()) {
367            String nextKey = (String) e.nextElement();
368            assertTrue("Stored property list not equal to original", myProps2
369                    .getProperty(nextKey).equals(myProps.getProperty(nextKey)));
370        }
371
372        try {
373            myProps.store(null, "String");
374            fail("NullPointerException expected");
375        } catch (NullPointerException ee){
376            //expected
377        }
378    }
379
380    /**
381     * @throws IOException
382     * @tests java.util.Properties#loadFromXML(java.io.InputStream)
383     */
384    @TestTargetNew(
385        level = TestLevel.COMPLETE,
386        notes = "",
387        method = "loadFromXML",
388        args = {java.io.InputStream.class}
389    )
390    @KnownFailure("ToT fixed?")
391    public void test_loadFromXMLLjava_io_InputStream() throws IOException {
392        Properties myProps = new Properties();
393        myProps.put("Property A", " aye\\\f\t\n\r\b");
394        myProps.put("Property B", "b ee#!=:");
395        myProps.put("Property C", "see");
396
397        Properties myProps2 = new Properties();
398        ByteArrayOutputStream out = new ByteArrayOutputStream();
399        myProps.storeToXML(out, "A Header");
400        out.close();
401
402        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
403        try {
404            myProps2.loadFromXML(in);
405            fail("InvalidPropertiesFormatException expected");
406        } catch (InvalidPropertiesFormatException e) {
407            //expected
408        }
409        in.close();
410
411
412        Properties prop = new Properties();
413        InputStream is = new ByteArrayInputStream(writePropertiesXML("UTF-8"));
414        prop.loadFromXML(is);
415        is.close();
416
417        assertEquals("Failed to load correct properties", "value3", prop
418                .getProperty("key3"));
419        assertEquals("Failed to load correct properties", "value1", prop
420                .getProperty("key1"));
421
422        prop = new Properties();
423        is = new ByteArrayInputStream(writePropertiesXML("ISO-8859-1"));
424        prop.loadFromXML(is);
425        is.close();
426
427        assertEquals("Failed to load correct properties", "value2", prop
428                .getProperty("key2"));
429        assertEquals("Failed to load correct properties", "value1", prop
430                .getProperty("key1"));
431
432        try {
433            prop.loadFromXML(null);
434            fail("NullPointerException expected");
435        } catch (NullPointerException e) {
436            //expected
437        }
438    }
439
440    /**
441     * @throws IOException
442     * @tests java.util.Properties#storeToXML(java.io.OutputStream,
443     *        java.lang.String, java.lang.String)
444     */
445    @TestTargetNew(
446        level = TestLevel.COMPLETE,
447        notes = "",
448        method = "storeToXML",
449        args = {java.io.OutputStream.class, java.lang.String.class, java.lang.String.class}
450    )
451    public void test_storeToXMLLjava_io_OutputStreamLjava_lang_StringLjava_lang_String()
452            throws IOException {
453        Properties myProps = new Properties();
454        myProps.setProperty("key1", "value1");
455        myProps.setProperty("key2", "value2");
456        myProps.setProperty("key3", "value3");
457        myProps.setProperty("<a>key4</a>", "\"value4");
458        myProps.setProperty("key5   ", "<h>value5</h>");
459        myProps.setProperty("<a>key6</a>", "   <h>value6</h>   ");
460        myProps.setProperty("<comment>key7</comment>", "value7");
461        myProps.setProperty("  key8   ", "<comment>value8</comment>");
462        myProps.setProperty("&lt;key9&gt;", "\u0027value9");
463        myProps.setProperty("key10\"", "&lt;value10&gt;");
464        myProps.setProperty("&amp;key11&amp;", "value11");
465        myProps.setProperty("key12", "&amp;value12&amp;");
466        myProps.setProperty("<a>&amp;key13&lt;</a>",
467                "&amp;&value13<b>&amp;</b>");
468
469        // store in UTF-8 encoding
470        ByteArrayOutputStream out = new ByteArrayOutputStream();
471        myProps.storeToXML(out, "comment");
472        out.close();
473
474        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
475        Properties myProps2 = new Properties();
476        myProps2.loadFromXML(in);
477        in.close();
478
479        Enumeration e = myProps.propertyNames();
480        while (e.hasMoreElements()) {
481            String nextKey = (String) e.nextElement();
482            assertTrue("Stored property list not equal to original", myProps2
483                    .getProperty(nextKey).equals(myProps.getProperty(nextKey)));
484        }
485
486        // store in ISO-8859-1 encoding
487        out = new ByteArrayOutputStream();
488        myProps.storeToXML(out, "comment", "ISO-8859-1");
489        out.close();
490
491        in = new ByteArrayInputStream(out.toByteArray());
492        myProps2 = new Properties();
493        myProps2.loadFromXML(in);
494        in.close();
495
496        e = myProps.propertyNames();
497        while (e.hasMoreElements()) {
498            String nextKey = (String) e.nextElement();
499            assertTrue("Stored property list not equal to original", myProps2
500                    .getProperty(nextKey).equals(myProps.getProperty(nextKey)));
501        }
502
503        out = new ByteArrayOutputStream();
504        myProps.storeToXML(out, "comment", "ISO-8859-1");
505        myProps.storeToXML(out, null, "ISO-8859-1");
506        out.close();
507
508        try {
509            myProps.storeToXML(out, "comment", null);
510            fail("NulPointerException expected");
511        } catch (NullPointerException ee) {
512            //expected
513        }
514
515        try {
516            myProps.storeToXML(null, "comment", "ISO-8859-1");
517            fail("NulPointerException expected");
518        } catch (NullPointerException ee) {
519            //expected
520        }
521    }
522
523    @TestTargetNew(
524        level = TestLevel.COMPLETE,
525        notes = "",
526        method = "storeToXML",
527        args = {java.io.OutputStream.class, java.lang.String.class}
528    )
529    public void test_storeToXMLLjava_io_OutputStreamLjava_lang_String()
530    throws IOException {
531        Properties myProps = new Properties();
532        myProps.put("Property A", "value 1");
533        myProps.put("Property B", "value 2");
534        myProps.put("Property C", "value 3");
535
536        Properties myProps2 = new Properties();
537        ByteArrayOutputStream out = new ByteArrayOutputStream();
538        myProps.storeToXML(out, "A Header");
539        out.close();
540
541        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
542        myProps2.loadFromXML(in);
543        in.close();
544
545        Enumeration e = myProps.propertyNames();
546        while (e.hasMoreElements()) {
547            String nextKey = (String) e.nextElement();
548            assertTrue("Stored property list not equal to original", myProps2
549                    .getProperty(nextKey).equals(myProps.getProperty(nextKey)));
550        }
551
552        try {
553            myProps.storeToXML(null, "String");
554            fail("NullPointerException expected");
555        } catch (NullPointerException ee){
556            //expected
557        }
558    }
559
560    @TestTargetNew(
561        level = TestLevel.COMPLETE,
562        notes = "",
563        method = "propertyNames",
564        args = {}
565    )
566    public void test_propertyNames() {
567        Properties myProps = new Properties();
568        myProps.put("Property A", "value 1");
569        myProps.put("Property B", "value 2");
570        myProps.put("Property C", "value 3");
571
572        Enumeration e = myProps.propertyNames();
573
574        int count = 0;
575        while (e.hasMoreElements()) {
576            count++;
577            assertTrue(myProps.containsKey(e.nextElement()));
578        }
579
580        assertTrue(myProps.size() == count);
581    }
582
583    /**
584     * Sets up the fixture, for example, open a network connection. This method
585     * is called before a test is executed.
586     */
587    protected void setUp() throws Exception {
588        super.setUp();
589        tProps = new Properties();
590        tProps.put("test.prop", "this is a test property");
591        tProps.put("bogus.prop", "bogus");
592    }
593
594    /**
595     * Tears down the fixture, for example, close a network connection. This
596     * method is called after a test is executed.
597     */
598    protected void tearDown() throws Exception {
599        tProps = null;
600        super.tearDown();
601    }
602
603    protected byte[] writeProperties() throws IOException {
604        ByteArrayOutputStream bout = new ByteArrayOutputStream();
605        PrintStream ps = new PrintStream(bout);
606        ps.println("#commented.entry=Bogus");
607        ps.println("test.pkg=harmony.tests");
608        ps.println("test.proj=Automated Tests");
609        ps.close();
610        return bout.toByteArray();
611    }
612
613    protected byte[] writePropertiesXML(String encoding) throws IOException {
614        ByteArrayOutputStream bout = new ByteArrayOutputStream();
615        PrintStream ps = new PrintStream(bout, true, encoding);
616        ps.println("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>");
617        ps
618                .println("<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">");
619        ps.println("<properties>");
620        ps.println("<comment>comment</comment>");
621        ps.println("<entry key=\"key4\">value4</entry>");
622        ps.println("<entry key=\"key3\">value3</entry>");
623        ps.println("<entry key=\"key2\">value2</entry>");
624        ps.println("<entry key=\"key1\"><!-- xml comment -->value1</entry>");
625        ps.println("</properties>");
626        ps.close();
627        return bout.toByteArray();
628    }
629}
630