Home | History | Annotate | Download | only in util
      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 
     18 package org.apache.harmony.tests.java.util;
     19 
     20 import java.io.BufferedReader;
     21 import java.io.ByteArrayInputStream;
     22 import java.io.ByteArrayOutputStream;
     23 import java.io.CharArrayReader;
     24 import java.io.IOException;
     25 import java.io.InputStream;
     26 import java.io.InputStreamReader;
     27 import java.io.OutputStreamWriter;
     28 import java.io.PrintStream;
     29 import java.io.PrintWriter;
     30 import java.io.Reader;
     31 import java.io.Writer;
     32 import java.util.ArrayList;
     33 import java.util.Arrays;
     34 import java.util.Collections;
     35 import java.util.Enumeration;
     36 import java.util.HashSet;
     37 import java.util.InvalidPropertiesFormatException;
     38 import java.util.Iterator;
     39 import java.util.List;
     40 import java.util.Properties;
     41 import java.util.Scanner;
     42 import java.util.Set;
     43 import tests.support.resource.Support_Resources;
     44 
     45 public class PropertiesTest extends junit.framework.TestCase {
     46 
     47     Properties tProps;
     48 
     49     byte[] propsFile;
     50 
     51     /**
     52      * java.util.Properties#Properties()
     53      */
     54     public void test_Constructor() {
     55         // Test for method java.util.Properties()
     56         Properties p = new Properties();
     57         // do something to avoid getting a variable unused warning
     58         p.clear();
     59         assertTrue("Created incorrect Properties", true);
     60     }
     61 
     62     public void test_loadLjava_io_InputStream_NPE() throws Exception {
     63         Properties p = new Properties();
     64         try {
     65             p.load((InputStream) null);
     66             fail("should throw NullPointerException");
     67         } catch (NullPointerException e) {
     68             // Expected
     69         }
     70     }
     71 
     72     public void test_loadsave() throws Exception {
     73         Properties p = new Properties();
     74         try {
     75             p.load((InputStream) null);
     76             fail("should throw NPE");
     77         } catch (NullPointerException npe) {
     78             // expected
     79         }
     80     }
     81 
     82     /**
     83      * java.util.Properties#Properties(java.util.Properties)
     84      */
     85     public void test_ConstructorLjava_util_Properties() {
     86         Properties systemProperties = System.getProperties();
     87         Properties properties = new Properties(systemProperties);
     88         Enumeration<?> propertyNames = systemProperties.propertyNames();
     89         String propertyName = null;
     90         while (propertyNames.hasMoreElements()) {
     91             propertyName = (String) propertyNames.nextElement();
     92             assertEquals("failed to construct correct properties",
     93                     systemProperties.getProperty(propertyName),
     94                     properties.getProperty(propertyName));
     95         }
     96     }
     97 
     98     /**
     99      * java.util.Properties#getProperty(java.lang.String)
    100      */
    101     public void test_getPropertyLjava_lang_String() {
    102         // Test for method java.lang.String
    103         // java.util.Properties.getProperty(java.lang.String)
    104         assertEquals("Did not retrieve property", "this is a test property",
    105                 ((String) tProps.getProperty("test.prop")));
    106     }
    107 
    108     /**
    109      * java.util.Properties#getProperty(java.lang.String,
    110      *java.lang.String)
    111      */
    112     public void test_getPropertyLjava_lang_StringLjava_lang_String() {
    113         // Test for method java.lang.String
    114         // java.util.Properties.getProperty(java.lang.String, java.lang.String)
    115         assertEquals("Did not retrieve property", "this is a test property",
    116                 ((String) tProps.getProperty("test.prop", "Blarg")));
    117         assertEquals("Did not return default value", "Gabba", ((String) tProps
    118                 .getProperty("notInThere.prop", "Gabba")));
    119     }
    120 
    121     /**
    122      * java.util.Properties#getProperty(java.lang.String)
    123      */
    124     public void test_getPropertyLjava_lang_String2() {
    125         // regression test for HARMONY-3518
    126         MyProperties props = new MyProperties();
    127         assertNull(props.getProperty("key"));
    128     }
    129 
    130     /**
    131      * java.util.Properties#getProperty(java.lang.String,
    132      *java.lang.String)
    133      */
    134     public void test_getPropertyLjava_lang_StringLjava_lang_String2() {
    135         // regression test for HARMONY-3518
    136         MyProperties props = new MyProperties();
    137         assertEquals("defaultValue", props.getProperty("key", "defaultValue"));
    138     }
    139 
    140     // regression testing for HARMONY-3518
    141     static class MyProperties extends Properties {
    142         public synchronized Object get(Object key) {
    143             return getProperty((String) key); // assume String
    144         }
    145     }
    146 
    147     /**
    148      * java.util.Properties#list(java.io.PrintStream)
    149      */
    150     public void test_listLjava_io_PrintStream() {
    151         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    152         PrintStream ps = new PrintStream(baos);
    153         Properties myProps = new Properties();
    154         myProps.setProperty("Abba", "Cadabra");
    155         myProps.setProperty("Open", "Sesame");
    156         myProps.setProperty("LongProperty",
    157                 "a long long long long long long long property");
    158         myProps.list(ps);
    159         ps.flush();
    160         String propList = baos.toString();
    161         assertTrue("Property list innacurate",
    162                 propList.indexOf("Abba=Cadabra") >= 0);
    163         assertTrue("Property list innacurate",
    164                 propList.indexOf("Open=Sesame") >= 0);
    165         assertTrue("property list do not conatins \"...\"", propList
    166                 .indexOf("...") != -1);
    167 
    168         ps = null;
    169         try {
    170             myProps.list(ps);
    171             fail("should throw NullPointerException");
    172         } catch (NullPointerException e) {
    173             // expected
    174         }
    175     }
    176 
    177     /**
    178      * java.util.Properties#list(java.io.PrintWriter)
    179      */
    180     public void test_listLjava_io_PrintWriter() {
    181         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    182         PrintWriter pw = new PrintWriter(baos);
    183         Properties myProps = new Properties();
    184         myProps.setProperty("Abba", "Cadabra");
    185         myProps.setProperty("Open", "Sesame");
    186         myProps.setProperty("LongProperty",
    187                 "a long long long long long long long property");
    188         myProps.list(pw);
    189         pw.flush();
    190         String propList = baos.toString();
    191         assertTrue("Property list innacurate",
    192                 propList.indexOf("Abba=Cadabra") >= 0);
    193         assertTrue("Property list innacurate",
    194                 propList.indexOf("Open=Sesame") >= 0);
    195         pw = null;
    196         try {
    197             myProps.list(pw);
    198             fail("should throw NullPointerException");
    199         } catch (NullPointerException e) {
    200             // expected
    201         }
    202     }
    203 
    204     /**
    205      * java.util.Properties#load(java.io.InputStream)
    206      */
    207     public void test_loadLjava_io_InputStream() {
    208         // Test for method void java.util.Properties.load(java.io.InputStream)
    209         Properties prop = null;
    210         try {
    211             InputStream is;
    212             prop = new Properties();
    213             prop.load(is = new ByteArrayInputStream(writeProperties()));
    214             is.close();
    215         } catch (Exception e) {
    216             fail("Exception during load test : " + e.getMessage());
    217         }
    218         assertEquals("Failed to load correct properties", "harmony.tests", prop
    219                 .getProperty("test.pkg"));
    220         assertNull("Load failed to parse incorrectly", prop
    221                 .getProperty("commented.entry"));
    222 
    223         prop = new Properties();
    224         try {
    225             prop.load(new ByteArrayInputStream("=".getBytes()));
    226         } catch (IOException e) {
    227             // expected
    228         }
    229         assertTrue("Failed to add empty key", prop.get("").equals(""));
    230 
    231         prop = new Properties();
    232         try {
    233             prop.load(new ByteArrayInputStream(" = ".getBytes()));
    234         } catch (IOException e) {
    235             // expected
    236         }
    237         assertTrue("Failed to add empty key2", prop.get("").equals(""));
    238 
    239         prop = new Properties();
    240         try {
    241             prop.load(new ByteArrayInputStream(" a= b".getBytes()));
    242         } catch (IOException e) {
    243             // expected
    244         }
    245         assertEquals("Failed to ignore whitespace", "b", prop.get("a"));
    246 
    247         prop = new Properties();
    248         try {
    249             prop.load(new ByteArrayInputStream(" a b".getBytes()));
    250         } catch (IOException e) {
    251             // expected
    252         }
    253         assertEquals("Failed to interpret whitespace as =", "b", prop.get("a"));
    254 
    255         prop = new Properties();
    256         try {
    257             prop.load(new ByteArrayInputStream("#\u008d\u00d2\na=\u008d\u00d3"
    258                     .getBytes("ISO8859_1")));
    259         } catch (IOException e) {
    260             // expected
    261         }
    262         assertEquals("Failed to parse chars >= 0x80", "\u008d\u00d3", prop
    263                 .get("a"));
    264 
    265         prop = new Properties();
    266         try {
    267             prop.load(new ByteArrayInputStream(
    268                     "#properties file\r\nfred=1\r\n#last comment"
    269                             .getBytes("ISO8859_1")));
    270         } catch (IOException e) {
    271             // expected
    272         } catch (IndexOutOfBoundsException e) {
    273             fail("IndexOutOfBoundsException when last line is a comment with no line terminator");
    274         }
    275         assertEquals("Failed to load when last line contains a comment", "1",
    276                 prop.get("fred"));
    277     }
    278 
    279     /**
    280      * java.util.Properties#load(java.io.InputStream)
    281      */
    282     public void test_loadLjava_io_InputStream_subtest0() {
    283         try {
    284             InputStream is = Support_Resources
    285                     .getStream("hyts_PropertiesTest.properties");
    286             Properties props = new Properties();
    287             props.load(is);
    288             is.close();
    289             assertEquals("1", "\n \t \f", props.getProperty(" \r"));
    290             assertEquals("2", "a", props.getProperty("a"));
    291             assertEquals("3", "bb as,dn   ", props.getProperty("b"));
    292             assertEquals("4", ":: cu", props.getProperty("c\r \t\nu"));
    293             assertEquals("5", "bu", props.getProperty("bu"));
    294             assertEquals("6", "d\r\ne=e", props.getProperty("d"));
    295             assertEquals("7", "fff", props.getProperty("f"));
    296             assertEquals("8", "g", props.getProperty("g"));
    297             assertEquals("9", "", props.getProperty("h h"));
    298             assertEquals("10", "i=i", props.getProperty(" "));
    299             assertEquals("11", "   j", props.getProperty("j"));
    300             assertEquals("12", "   c", props.getProperty("space"));
    301             assertEquals("13", "\\", props.getProperty("dblbackslash"));
    302         } catch (IOException e) {
    303             fail("Unexpected: " + e);
    304         }
    305     }
    306 
    307     /**
    308      * @throws IOException
    309      * java.util.Properties#load(java.io.Reader)
    310      * @since 1.6
    311      */
    312     public void test_loadLjava_io_Reader() throws IOException {
    313         Properties prop = null;
    314         try {
    315             InputStream is;
    316             prop = new Properties();
    317             is = new ByteArrayInputStream(writeProperties());
    318             prop.load(new InputStreamReader(is));
    319             is.close();
    320         } catch (Exception e) {
    321             fail("Exception during load test : " + e.getMessage());
    322         }
    323         assertEquals("Failed to load correct properties", "harmony.tests", prop
    324                 .getProperty("test.pkg"));
    325         assertNull("Load failed to parse incorrectly", prop
    326                 .getProperty("commented.entry"));
    327 
    328         prop = new Properties();
    329         prop.load(new ByteArrayInputStream("=".getBytes()));
    330         assertEquals("Failed to add empty key", "", prop.get(""));
    331 
    332         prop = new Properties();
    333         prop.load(new ByteArrayInputStream(" = ".getBytes()));
    334         assertEquals("Failed to add empty key2", "", prop.get(""));
    335 
    336         prop = new Properties();
    337         prop.load(new ByteArrayInputStream(" a= b".getBytes()));
    338         assertEquals("Failed to ignore whitespace", "b", prop.get("a"));
    339 
    340         prop = new Properties();
    341         prop.load(new ByteArrayInputStream(" a b".getBytes()));
    342         assertEquals("Failed to interpret whitespace as =", "b", prop.get("a"));
    343 
    344         prop = new Properties();
    345         prop.load(new ByteArrayInputStream("#comment\na=value"
    346                 .getBytes("UTF-8")));
    347         assertEquals("value", prop.getProperty("a"));
    348 
    349         prop = new Properties();
    350         prop.load(new InputStreamReader(new ByteArrayInputStream(
    351                 "#\u008d\u00d2\na=\u008d\u00d3".getBytes("UTF-8"))));
    352         try {
    353             prop
    354                     .load(new InputStreamReader(new ByteArrayInputStream(
    355                             "#\u008d\u00d2\na=\\\\u008d\\\\\\uu00d3"
    356                                     .getBytes("UTF-8"))));
    357             fail("Should throw IllegalArgumentException");
    358         } catch (IllegalArgumentException e) {
    359             // expected
    360         }
    361 
    362         prop = new Properties();
    363         try {
    364             prop.load(new InputStreamReader(new ByteArrayInputStream(
    365                     "#properties file\r\nfred=1\r\n#last comment"
    366                             .getBytes("ISO8859_1"))));
    367         } catch (IndexOutOfBoundsException e) {
    368             fail("IndexOutOfBoundsException when last line is a comment with no line terminator");
    369         }
    370         assertEquals("Failed to load when last line contains a comment", "1",
    371                 prop.get("fred"));
    372 
    373         // Regression tests for HARMONY-5414
    374         prop = new Properties();
    375         prop.load(new ByteArrayInputStream("a=\\u1234z".getBytes()));
    376 
    377         prop = new Properties();
    378         try {
    379             prop.load(new ByteArrayInputStream("a=\\u123".getBytes()));
    380             fail("should throw IllegalArgumentException");
    381         } catch (IllegalArgumentException e) {
    382             // Expected
    383         }
    384 
    385         prop = new Properties();
    386         try {
    387             prop.load(new ByteArrayInputStream("a=\\u123z".getBytes()));
    388             fail("should throw IllegalArgumentException");
    389         } catch (IllegalArgumentException expected) {
    390             // Expected
    391         }
    392 
    393         prop = new Properties();
    394         Properties expected = new Properties();
    395         expected.put("a", "");
    396         prop.load(new ByteArrayInputStream("a=\\".getBytes()));
    397         assertEquals("Failed to trim trailing slash value", expected, prop);
    398 
    399         prop = new Properties();
    400         expected = new Properties();
    401         expected.put("a", "\u1234");
    402         prop.load(new ByteArrayInputStream("a=\\u1234\\".getBytes()));
    403         assertEquals("Failed to trim trailing slash value #2", expected, prop);
    404 
    405         prop = new Properties();
    406         expected = new Properties();
    407         expected.put("a", "q");
    408         prop.load(new ByteArrayInputStream("a=\\q".getBytes()));
    409         assertEquals("Failed to skip slash value #3", expected, prop);
    410     }
    411 
    412     /**
    413      * java.util.Properties#load(java.io.InputStream)
    414      */
    415     public void test_loadLjava_io_InputStream_Special() throws IOException {
    416         // Test for method void java.util.Properties.load(java.io.InputStream)
    417         Properties prop = null;
    418         prop = new Properties();
    419         prop.load(new ByteArrayInputStream("=".getBytes()));
    420         assertTrue("Failed to add empty key", prop.get("").equals(""));
    421 
    422         prop = new Properties();
    423         prop.load(new ByteArrayInputStream("=\r\n".getBytes()));
    424         assertTrue("Failed to add empty key", prop.get("").equals(""));
    425 
    426         prop = new Properties();
    427         prop.load(new ByteArrayInputStream("=\n\r".getBytes()));
    428         assertTrue("Failed to add empty key", prop.get("").equals(""));
    429     }
    430 
    431     /**
    432      * @throws IOException
    433      * java.util.Properties#load(java.io.Reader)
    434      * @since 1.6
    435      */
    436     public void test_loadLjava_io_Reader_subtest0() throws IOException {
    437         InputStream is = Support_Resources
    438                 .getStream("hyts_PropertiesTest.properties");
    439         Properties props = new Properties();
    440         props.load(new InputStreamReader(is));
    441         is.close();
    442         assertEquals("1", "\n \t \f", props.getProperty(" \r"));
    443         assertEquals("2", "a", props.getProperty("a"));
    444         assertEquals("3", "bb as,dn   ", props.getProperty("b"));
    445         assertEquals("4", ":: cu", props.getProperty("c\r \t\nu"));
    446         assertEquals("5", "bu", props.getProperty("bu"));
    447         assertEquals("6", "d\r\ne=e", props.getProperty("d"));
    448         assertEquals("7", "fff", props.getProperty("f"));
    449         assertEquals("8", "g", props.getProperty("g"));
    450         assertEquals("9", "", props.getProperty("h h"));
    451         assertEquals("10", "i=i", props.getProperty(" "));
    452         assertEquals("11", "   j", props.getProperty("j"));
    453         assertEquals("12", "   c", props.getProperty("space"));
    454         assertEquals("13", "\\", props.getProperty("dblbackslash"));
    455     }
    456 
    457     /**
    458      * {@link java.util.Properties#stringPropertyNames()}
    459      * @since 1.6
    460      */
    461     public void test_stringPropertyNames() {
    462         Set<String> set = tProps.stringPropertyNames();
    463         assertEquals(2, set.size());
    464         assertTrue(set.contains("test.prop"));
    465         assertTrue(set.contains("bogus.prop"));
    466         assertNotSame(set, tProps.stringPropertyNames());
    467 
    468         set = new Properties().stringPropertyNames();
    469         assertEquals(0, set.size());
    470 
    471         set = new Properties(System.getProperties()).stringPropertyNames();
    472         assertTrue(set.size() > 0);
    473 
    474         tProps = new Properties(tProps);
    475         tProps.put("test.prop", "anotherValue");
    476         tProps.put("3rdKey", "3rdValue");
    477         set = tProps.stringPropertyNames();
    478         assertEquals(3, set.size());
    479         assertTrue(set.contains("test.prop"));
    480         assertTrue(set.contains("bogus.prop"));
    481         assertTrue(set.contains("3rdKey"));
    482 
    483         tProps.put(String.class, "valueOfNonStringKey");
    484         set = tProps.stringPropertyNames();
    485         assertEquals(3, set.size());
    486         assertTrue(set.contains("test.prop"));
    487         assertTrue(set.contains("bogus.prop"));
    488         assertTrue(set.contains("3rdKey"));
    489 
    490         tProps.put("4thKey", "4thValue");
    491         assertEquals(4, tProps.size());
    492         assertEquals(3, set.size());
    493 
    494         try {
    495             set.add("another");
    496             fail("Should throw UnsupportedOperationException");
    497         } catch (UnsupportedOperationException e) {
    498             // expected
    499         }
    500     }
    501 
    502     /**
    503      * {@link java.util.Properties#stringPropertyNames()}
    504      * @since 1.6
    505      */
    506     public void test_stringPropertyNames_scenario1() {
    507         String[] keys = new String[] { "key1", "key2", "key3" };
    508         String[] values = new String[] { "value1", "value2", "value3" };
    509         List<String> keyList = Arrays.asList(keys);
    510 
    511         Properties properties = new Properties();
    512         for (int index = 0; index < keys.length; index++) {
    513             properties.setProperty(keys[index], values[index]);
    514         }
    515 
    516         properties = new Properties(properties);
    517         Set<String> nameSet = properties.stringPropertyNames();
    518         assertEquals(keys.length, nameSet.size());
    519         Iterator<String> iterator = nameSet.iterator();
    520         while (iterator.hasNext()) {
    521             assertTrue(keyList.contains(iterator.next()));
    522         }
    523 
    524         Enumeration<?> nameEnum = properties.propertyNames();
    525         int count = 0;
    526         while (nameEnum.hasMoreElements()) {
    527             count++;
    528             assertTrue(keyList.contains(nameEnum.nextElement()));
    529         }
    530         assertEquals(keys.length, count);
    531 
    532         properties = new Properties(properties);
    533         nameSet = properties.stringPropertyNames();
    534         assertEquals(keys.length, nameSet.size());
    535         iterator = nameSet.iterator();
    536         while (iterator.hasNext()) {
    537             assertTrue(keyList.contains(iterator.next()));
    538         }
    539 
    540         nameEnum = properties.propertyNames();
    541         count = 0;
    542         while (nameEnum.hasMoreElements()) {
    543             count++;
    544             assertTrue(keyList.contains(nameEnum.nextElement()));
    545         }
    546         assertEquals(keys.length, count);
    547     }
    548 
    549     /**
    550      * {@link java.util.Properties#stringPropertyNames()}
    551      * @since 1.6
    552      */
    553     public void test_stringPropertyNames_scenario2() {
    554         String[] defaultKeys = new String[] { "defaultKey1", "defaultKey2",
    555                 "defaultKey3", "defaultKey4", "defaultKey5", "defaultKey6" };
    556         String[] defaultValues = new String[] { "defaultValue1",
    557                 "defaultValue2", "defaultValue3", "defaultValue4",
    558                 "defaultValue5", "defaultValue6" };
    559         List<String> keyList = new ArrayList<String>();
    560         Properties defaults = new Properties();
    561         for (int index = 0; index < 3; index++) {
    562             defaults.setProperty(defaultKeys[index], defaultValues[index]);
    563             keyList.add(defaultKeys[index]);
    564         }
    565 
    566         String[] keys = new String[] { "key1", "key2", "key3" };
    567         String[] values = new String[] { "value1", "value2", "value3" };
    568         Properties properties = new Properties(defaults);
    569         for (int index = 0; index < keys.length; index++) {
    570             properties.setProperty(keys[index], values[index]);
    571             keyList.add(keys[index]);
    572         }
    573 
    574         Set<String> nameSet = properties.stringPropertyNames();
    575         assertEquals(keyList.size(), nameSet.size());
    576         Iterator<String> iterator = nameSet.iterator();
    577         while (iterator.hasNext()) {
    578             assertTrue(keyList.contains(iterator.next()));
    579         }
    580 
    581         Enumeration<?> nameEnum = properties.propertyNames();
    582         int count = 0;
    583         while (nameEnum.hasMoreElements()) {
    584             count++;
    585             assertTrue(keyList.contains(nameEnum.nextElement()));
    586         }
    587         assertEquals(keyList.size(), count);
    588 
    589         for (int index = 3; index < defaultKeys.length; index++) {
    590             defaults.setProperty(defaultKeys[index], defaultValues[index]);
    591             keyList.add(defaultKeys[index]);
    592         }
    593 
    594         nameSet = properties.stringPropertyNames();
    595         assertEquals(keyList.size(), nameSet.size());
    596         iterator = nameSet.iterator();
    597         while (iterator.hasNext()) {
    598             assertTrue(keyList.contains(iterator.next()));
    599         }
    600 
    601         nameEnum = properties.propertyNames();
    602         count = 0;
    603         while (nameEnum.hasMoreElements()) {
    604             count++;
    605             assertTrue(keyList.contains(nameEnum.nextElement()));
    606         }
    607         assertEquals(keyList.size(), count);
    608     }
    609 
    610     /**
    611      * java.util.Properties#save(java.io.OutputStream, java.lang.String)
    612      */
    613     public void test_saveLjava_io_OutputStreamLjava_lang_String() {
    614         // Test for method void java.util.Properties.save(java.io.OutputStream,
    615         // java.lang.String)
    616         Properties myProps = new Properties();
    617         Properties myProps2 = new Properties();
    618 
    619         myProps.setProperty("Property A", "aye");
    620         myProps.setProperty("Property B", "bee");
    621         myProps.setProperty("Property C", "see");
    622 
    623         try {
    624             ByteArrayOutputStream out = new ByteArrayOutputStream();
    625             myProps.save(out, "A Header");
    626             out.close();
    627             ByteArrayInputStream in = new ByteArrayInputStream(out
    628                     .toByteArray());
    629             myProps2.load(in);
    630             in.close();
    631         } catch (IOException ioe) {
    632             fail("IOException occurred reading/writing file : "
    633                     + ioe.getMessage());
    634         }
    635 
    636         Enumeration e = myProps.propertyNames();
    637         String nextKey;
    638         while (e.hasMoreElements()) {
    639             nextKey = (String) e.nextElement();
    640             assertEquals("Stored property list not equal to original", myProps
    641                     .getProperty(nextKey), myProps2.getProperty(nextKey));
    642         }
    643     }
    644 
    645     /**
    646      * java.util.Properties#setProperty(java.lang.String,
    647      *java.lang.String)
    648      */
    649     public void test_setPropertyLjava_lang_StringLjava_lang_String() {
    650         // Test for method java.lang.Object
    651         // java.util.Properties.setProperty(java.lang.String, java.lang.String)
    652         Properties myProps = new Properties();
    653         myProps.setProperty("Yoink", "Yabba");
    654         assertEquals("Failed to set property", "Yabba", myProps
    655                 .getProperty("Yoink"));
    656         myProps.setProperty("Yoink", "Gab");
    657         assertEquals("Failed to reset property", "Gab", myProps
    658                 .getProperty("Yoink"));
    659     }
    660 
    661     /**
    662      * java.util.Properties#store(java.io.OutputStream, java.lang.String)
    663      */
    664     public void test_storeLjava_io_OutputStreamLjava_lang_String() {
    665         // Test for method void java.util.Properties.store(java.io.OutputStream,
    666         // java.lang.String)
    667         Properties myProps = new Properties();
    668         Properties myProps2 = new Properties();
    669         Enumeration e;
    670         String nextKey;
    671 
    672         myProps.put("Property A", " aye\\\f\t\n\r\b");
    673         myProps.put("Property B", "b ee#!=:");
    674         myProps.put("Property C", "see");
    675 
    676         try {
    677             ByteArrayOutputStream out = new ByteArrayOutputStream();
    678             myProps.store(out, "A Header");
    679             out.close();
    680             ByteArrayInputStream in = new ByteArrayInputStream(out
    681                     .toByteArray());
    682             myProps2.load(in);
    683             in.close();
    684         } catch (IOException ioe) {
    685             fail("IOException occurred reading/writing file : "
    686                     + ioe.getMessage());
    687         }
    688 
    689         e = myProps.propertyNames();
    690         while (e.hasMoreElements()) {
    691             nextKey = (String) e.nextElement();
    692             assertTrue("Stored property list not equal to original", myProps2
    693                     .getProperty(nextKey).equals(myProps.getProperty(nextKey)));
    694         }
    695 
    696     }
    697 
    698     /**
    699      * @throws IOException
    700      * java.util.Properties#store(java.io.Writer, java.lang.String)
    701      * @since 1.6
    702      */
    703     public void test_storeLjava_io_WriterLjava_lang_String() throws IOException {
    704         Properties myProps = new Properties();
    705         Properties myProps2 = new Properties();
    706 
    707         myProps.put("Property A", " aye\\\f\t\n\r\b");
    708         myProps.put("Property B", "b ee#!=:");
    709         myProps.put("Property C", "see");
    710 
    711         ByteArrayOutputStream out = new ByteArrayOutputStream();
    712         myProps.store(new OutputStreamWriter(out), "A Header");
    713         Scanner scanner = new Scanner(out.toString());
    714         assertTrue(scanner.nextLine().startsWith("#A Header"));
    715         assertTrue(scanner.nextLine().startsWith("#"));
    716         out.close();
    717         ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    718         myProps2.load(in);
    719         in.close();
    720 
    721         Enumeration e = myProps.propertyNames();
    722         String nextKey;
    723         while (e.hasMoreElements()) {
    724             nextKey = (String) e.nextElement();
    725             assertTrue("Stored property list not equal to original", myProps2
    726                     .getProperty(nextKey).equals(myProps.getProperty(nextKey)));
    727         }
    728 
    729         try {
    730             myProps.store((Writer) null, "some comments");
    731             fail("Should throw NullPointerException");
    732         } catch (NullPointerException e1) {
    733             // expected
    734         }
    735 
    736         myProps.put(String.class, "wrong type");
    737         try {
    738             myProps.store(new OutputStreamWriter(new ByteArrayOutputStream()),
    739                     "some comments");
    740             fail("Should throw ClassCastException");
    741         } catch (ClassCastException e1) {
    742             // expected
    743         }
    744         myProps.remove(String.class);
    745         myProps.store(new OutputStreamWriter(new ByteArrayOutputStream()),
    746                 "some comments");
    747         // it is OK
    748         myProps.put("wrong type", String.class);
    749         try {
    750             myProps.store(new OutputStreamWriter(new ByteArrayOutputStream()),
    751                     "some comments");
    752             fail("Should throw ClassCastException");
    753         } catch (ClassCastException e1) {
    754             // expected
    755         }
    756     }
    757 
    758     /**
    759      * java.util.Properties#loadFromXML(java.io.InputStream)
    760      */
    761     public void test_loadFromXMLLjava_io_InputStream() throws Exception {
    762         // Test for method void
    763         // java.util.Properties.loadFromXML(java.io.InputStream)
    764         Properties prop = null;
    765         try {
    766             InputStream is;
    767             prop = new Properties();
    768             prop.loadFromXML(is = new ByteArrayInputStream(
    769                     writePropertiesXMLUTF_8()));
    770             is.close();
    771         } catch (Exception e) {
    772             fail("Exception during load test : " + e.getMessage());
    773         }
    774         assertEquals("Failed to load correct properties", "value3", prop
    775                 .getProperty("key3"));
    776         assertEquals("Failed to load correct properties", "value1", prop
    777                 .getProperty("key1"));
    778 
    779         try {
    780             InputStream is;
    781             prop = new Properties();
    782             prop.loadFromXML(is = new ByteArrayInputStream(
    783                     writePropertiesXMLISO_8859_1()));
    784             is.close();
    785         } catch (Exception e) {
    786             fail("Exception during load test : " + e.getMessage());
    787         }
    788         assertEquals("Failed to load correct properties", "value2", prop
    789                 .getProperty("key2"));
    790         assertEquals("Failed to load correct properties", "value1", prop
    791                 .getProperty("key1"));
    792 
    793         try {
    794             prop.loadFromXML(null);
    795             fail("should throw NullPointerException");
    796         } catch (NullPointerException e) {
    797             // expected
    798         }
    799     }
    800 
    801     /**
    802      * java.util.Properties#storeToXML(java.io.OutputStream,
    803      *java.lang.String, java.lang.String)
    804      */
    805     public void test_storeToXMLLjava_io_OutputStreamLjava_lang_StringLjava_lang_String()
    806             throws Exception {
    807         // Test for method void
    808         // java.util.Properties.storeToXML(java.io.OutputStream,
    809         // java.lang.String, java.lang.String)
    810         Properties myProps = new Properties();
    811         Properties myProps2 = new Properties();
    812         Enumeration e;
    813         String nextKey;
    814 
    815         myProps.setProperty("key1", "value1");
    816         myProps.setProperty("key2", "value2");
    817         myProps.setProperty("key3", "value3");
    818         myProps.setProperty("<a>key4</a>", "\"value4");
    819         myProps.setProperty("key5   ", "<h>value5</h>");
    820         myProps.setProperty("<a>key6</a>", "   <h>value6</h>   ");
    821         myProps.setProperty("<comment>key7</comment>", "value7");
    822         myProps.setProperty("  key8   ", "<comment>value8</comment>");
    823         myProps.setProperty("&lt;key9&gt;", "\u0027value9");
    824         myProps.setProperty("key10\"", "&lt;value10&gt;");
    825         myProps.setProperty("&amp;key11&amp;", "value11");
    826         myProps.setProperty("key12", "&amp;value12&amp;");
    827         myProps.setProperty("<a>&amp;key13&lt;</a>",
    828                 "&amp;&value13<b>&amp;</b>");
    829 
    830         try {
    831             ByteArrayOutputStream out = new ByteArrayOutputStream();
    832 
    833             // store in UTF-8 encoding
    834             myProps.storeToXML(out, "comment");
    835             out.close();
    836             ByteArrayInputStream in = new ByteArrayInputStream(out
    837                     .toByteArray());
    838             myProps2.loadFromXML(in);
    839             in.close();
    840         } catch (InvalidPropertiesFormatException ipfe) {
    841             fail("InvalidPropertiesFormatException occurred reading file: "
    842                     + ipfe.getMessage());
    843         } catch (IOException ioe) {
    844             fail("IOException occurred reading/writing file : "
    845                     + ioe.getMessage());
    846         }
    847 
    848         e = myProps.propertyNames();
    849         while (e.hasMoreElements()) {
    850             nextKey = (String) e.nextElement();
    851             assertTrue("Stored property list not equal to original", myProps2
    852                     .getProperty(nextKey).equals(myProps.getProperty(nextKey)));
    853         }
    854 
    855         try {
    856             ByteArrayOutputStream out = new ByteArrayOutputStream();
    857 
    858             // store in ISO-8859-1 encoding
    859             myProps.storeToXML(out, "comment", "ISO-8859-1");
    860             out.close();
    861             ByteArrayInputStream in = new ByteArrayInputStream(out
    862                     .toByteArray());
    863             myProps2 = new Properties();
    864             myProps2.loadFromXML(in);
    865             in.close();
    866         } catch (InvalidPropertiesFormatException ipfe) {
    867             fail("InvalidPropertiesFormatException occurred reading file: "
    868                     + ipfe.getMessage());
    869         } catch (IOException ioe) {
    870             fail("IOException occurred reading/writing file : "
    871                     + ioe.getMessage());
    872         }
    873 
    874         e = myProps.propertyNames();
    875         while (e.hasMoreElements()) {
    876             nextKey = (String) e.nextElement();
    877             assertTrue("Stored property list not equal to original", myProps2
    878                     .getProperty(nextKey).equals(myProps.getProperty(nextKey)));
    879         }
    880 
    881         try {
    882             ByteArrayOutputStream out = new ByteArrayOutputStream();
    883             myProps.storeToXML(out, null, null);
    884             fail("should throw nullPointerException");
    885         } catch (NullPointerException ne) {
    886             // expected
    887         }
    888     }
    889 
    890     /**
    891      * if loading from single line like "hello" without "\n\r" neither "=", it
    892      * should be same as loading from "hello="
    893      */
    894     public void testLoadSingleLine() throws Exception {
    895         Properties props = new Properties();
    896         InputStream sr = new ByteArrayInputStream("hello".getBytes());
    897         props.load(sr);
    898         assertEquals(1, props.size());
    899     }
    900 
    901     public void test_propertyNames() {
    902         Properties parent = new Properties();
    903         parent.setProperty("parent.a.key", "parent.a.value");
    904         parent.setProperty("parent.b.key", "parent.b.value");
    905 
    906         Enumeration<?> names = parent.propertyNames();
    907         assertPropertyEnumeration(names, "parent.a.key", "parent.b.key");
    908 
    909         Properties current = new Properties(parent);
    910         current.setProperty("current.a.key", "current.a.value");
    911         current.setProperty("current.b.key", "current.b.value");
    912 
    913         names = current.propertyNames();
    914         assertPropertyEnumeration(names,
    915                 "parent.a.key",
    916                 "parent.b.key",
    917                 "current.a.key",
    918                 "current.b.key");
    919 
    920         Properties child = new Properties(current);
    921         child.setProperty("child.a.key", "child.a.value");
    922         child.setProperty("child.b.key", "child.b.value");
    923 
    924         names = child.propertyNames();
    925         assertPropertyEnumeration(names,
    926                 "parent.a.key",
    927                 "parent.b.key",
    928                 "current.a.key",
    929                 "current.b.key",
    930                 "child.a.key",
    931                 "child.b.key");
    932     }
    933 
    934     public void assertPropertyEnumeration(Enumeration<?> propNames,
    935             String... expected) {
    936         Set<String> propsSet = new HashSet<String>();
    937         while (propNames.hasMoreElements()) {
    938             String next = (String) propNames.nextElement();
    939             assertFalse(propsSet.contains(next));
    940             propsSet.add(next);
    941         }
    942 
    943         assertEquals(expected.length, propsSet.size());
    944         assertTrue(propsSet.containsAll(Arrays.asList(expected)));
    945     }
    946 
    947     private String comment1 = "comment1";
    948 
    949     private String comment2 = "comment2";
    950 
    951     private void validateOutput(String[] expectStrings, byte[] output)
    952             throws IOException {
    953         ByteArrayInputStream bais = new ByteArrayInputStream(output);
    954         BufferedReader br = new BufferedReader(new InputStreamReader(bais,
    955                 "ISO8859_1"));
    956         for (String expectString : expectStrings) {
    957             assertEquals(expectString, br.readLine());
    958         }
    959         br.readLine();
    960         assertNull(br.readLine());
    961         br.close();
    962     }
    963 
    964     public void testStore_scenario0() throws IOException {
    965         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    966         Properties props = new Properties();
    967         props.store(baos, comment1 + '\r' + comment2);
    968         validateOutput(new String[] { "#comment1", "#comment2" },
    969                 baos.toByteArray());
    970         baos.close();
    971     }
    972 
    973     public void testStore_scenario1() throws IOException {
    974         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    975         Properties props = new Properties();
    976         props.store(baos, comment1 + '\n' + comment2);
    977         validateOutput(new String[] { "#comment1", "#comment2" },
    978                 baos.toByteArray());
    979         baos.close();
    980     }
    981 
    982     public void testStore_scenario2() throws IOException {
    983         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    984         Properties props = new Properties();
    985         props.store(baos, comment1 + '\r' + '\n' + comment2);
    986         validateOutput(new String[] { "#comment1", "#comment2" },
    987                 baos.toByteArray());
    988         baos.close();
    989     }
    990 
    991     public void testStore_scenario3() throws IOException {
    992         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    993         Properties props = new Properties();
    994         props.store(baos, comment1 + '\n' + '\r' + comment2);
    995         validateOutput(new String[] { "#comment1", "#", "#comment2" },
    996                 baos.toByteArray());
    997         baos.close();
    998     }
    999 
   1000     public void testStore_scenario4() throws IOException {
   1001         ByteArrayOutputStream baos = new ByteArrayOutputStream();
   1002         Properties props = new Properties();
   1003         props.store(baos, comment1 + '\r' + '#' + comment2);
   1004         validateOutput(new String[] { "#comment1", "#comment2" },
   1005                 baos.toByteArray());
   1006         baos.close();
   1007     }
   1008 
   1009     public void testStore_scenario5() throws IOException {
   1010         ByteArrayOutputStream baos = new ByteArrayOutputStream();
   1011         Properties props = new Properties();
   1012         props.store(baos, comment1 + '\r' + '!' + comment2);
   1013         validateOutput(new String[] { "#comment1", "!comment2" },
   1014                 baos.toByteArray());
   1015         baos.close();
   1016     }
   1017 
   1018     public void testStore_scenario6() throws IOException {
   1019         ByteArrayOutputStream baos = new ByteArrayOutputStream();
   1020         Properties props = new Properties();
   1021         props.store(baos, comment1 + '\n' + '#' + comment2);
   1022         validateOutput(new String[] { "#comment1", "#comment2" },
   1023                 baos.toByteArray());
   1024         baos.close();
   1025     }
   1026 
   1027     public void testStore_scenario7() throws IOException {
   1028         ByteArrayOutputStream baos = new ByteArrayOutputStream();
   1029         Properties props = new Properties();
   1030         props.store(baos, comment1 + '\n' + '!' + comment2);
   1031         validateOutput(new String[] { "#comment1", "!comment2" },
   1032                 baos.toByteArray());
   1033         baos.close();
   1034     }
   1035 
   1036     public void testStore_scenario8() throws IOException {
   1037         ByteArrayOutputStream baos = new ByteArrayOutputStream();
   1038         Properties props = new Properties();
   1039         props.store(baos, comment1 + '\r' + '\n' + '#' + comment2);
   1040         validateOutput(new String[] { "#comment1", "#comment2" },
   1041                 baos.toByteArray());
   1042         baos.close();
   1043     }
   1044 
   1045     public void testStore_scenario9() throws IOException {
   1046         ByteArrayOutputStream baos = new ByteArrayOutputStream();
   1047         Properties props = new Properties();
   1048         props.store(baos, comment1 + '\n' + '\r' + '#' + comment2);
   1049         validateOutput(new String[] { "#comment1", "#", "#comment2" },
   1050                 baos.toByteArray());
   1051         baos.close();
   1052     }
   1053 
   1054     public void testStore_scenario10() throws IOException {
   1055         ByteArrayOutputStream baos = new ByteArrayOutputStream();
   1056         Properties props = new Properties();
   1057         props.store(baos, comment1 + '\r' + '\n' + '!' + comment2);
   1058         validateOutput(new String[] { "#comment1", "!comment2" },
   1059                 baos.toByteArray());
   1060         baos.close();
   1061     }
   1062 
   1063     public void testStore_scenario11() throws IOException {
   1064         ByteArrayOutputStream baos = new ByteArrayOutputStream();
   1065         Properties props = new Properties();
   1066         props.store(baos, comment1 + '\n' + '\r' + '!' + comment2);
   1067         validateOutput(new String[] { "#comment1", "#", "!comment2" },
   1068                 baos.toByteArray());
   1069         baos.close();
   1070     }
   1071 
   1072     public void testLoadReader() throws IOException {
   1073         InputStream inputStream = new ByteArrayInputStream(
   1074                 "\u3000key=value".getBytes("UTF-8"));
   1075         Properties props = new Properties();
   1076         props.load(inputStream);
   1077         Enumeration<Object> keyEnum = props.keys();
   1078         assertFalse("\u3000key".equals(keyEnum.nextElement()));
   1079         assertFalse(keyEnum.hasMoreElements());
   1080         inputStream.close();
   1081 
   1082         inputStream = new ByteArrayInputStream(
   1083                 "\u3000key=value".getBytes("UTF-8"));
   1084         props = new Properties();
   1085         props.load(new InputStreamReader(inputStream, "UTF-8"));
   1086         keyEnum = props.keys();
   1087         assertEquals("key", keyEnum.nextElement());
   1088         assertFalse(keyEnum.hasMoreElements());
   1089         inputStream.close();
   1090     }
   1091 
   1092     /**
   1093      * Checks the example given in the documentation of a single property split over
   1094      * multiple lines separated by a backslash and newline character.
   1095      */
   1096     public void testSingleProperty_multipleLinesJoinedByBackslash() throws Exception {
   1097         String propertyString = "fruits                           apple, banana, pear, \\\n"
   1098                 + "                                  cantaloupe, watermelon, \\\n"
   1099                 + "                                  kiwi, mango";
   1100         checkSingleProperty("fruits", "apple, banana, pear, cantaloupe, watermelon, kiwi, mango",
   1101                 propertyString);
   1102     }
   1103 
   1104     /**
   1105      * Checks that a trailing backslash at the end of the single line of input is ignored.
   1106      * This is similar to a check in {@link #test_loadLjava_io_Reader()} that uses an
   1107      * InputStream and {@link Properties#equals(Object)} .
   1108      */
   1109     public void testSingleProperty_oneLineWithTrailingBackslash() throws Exception {
   1110         checkSingleProperty("key", "value", "key=value\\");
   1111     }
   1112 
   1113     /**
   1114      * Checks that a trailing backslash at the end of the single line of input is ignored,
   1115      * even when that line has a newline.
   1116      */
   1117     public void testSingleProperty_oneLineWithTrailingBackslash_newline() throws Exception {
   1118         checkSingleProperty("key", "value", "key=value\\\r");
   1119         checkSingleProperty("key", "value", "key=value\\\n");
   1120         checkSingleProperty("key", "value", "key=value\\\r\n");
   1121     }
   1122 
   1123     private static void checkSingleProperty(String key, String value, String serialized)
   1124             throws IOException {
   1125         Properties properties = new Properties();
   1126         try (Reader reader = new CharArrayReader(serialized.toCharArray())) {
   1127             properties.load(reader);
   1128             assertEquals(Collections.singleton(key), properties.keySet());
   1129             assertEquals(value, properties.getProperty(key));
   1130         }
   1131     }
   1132 
   1133     /**
   1134      * Sets up the fixture, for example, open a network connection. This method
   1135      * is called before a test is executed.
   1136      */
   1137     protected void setUp() {
   1138 
   1139         tProps = new Properties();
   1140         tProps.put("test.prop", "this is a test property");
   1141         tProps.put("bogus.prop", "bogus");
   1142     }
   1143 
   1144     /**
   1145      * Tears down the fixture, for example, close a network connection. This
   1146      * method is called after a test is executed.
   1147      */
   1148     protected void tearDown() {
   1149     }
   1150 
   1151     /**
   1152      * Tears down the fixture, for example, close a network connection. This
   1153      * method is called after a test is executed.
   1154      */
   1155     protected byte[] writeProperties() throws IOException {
   1156         PrintStream ps = null;
   1157         ByteArrayOutputStream bout = new ByteArrayOutputStream();
   1158         ps = new PrintStream(bout);
   1159         ps.println("#commented.entry=Bogus");
   1160         ps.println("test.pkg=harmony.tests");
   1161         ps.println("test.proj=Automated Tests");
   1162         ps.close();
   1163         return bout.toByteArray();
   1164     }
   1165 
   1166     protected byte[] writePropertiesXMLUTF_8() throws IOException {
   1167         PrintStream ps = null;
   1168         ByteArrayOutputStream bout = new ByteArrayOutputStream();
   1169         ps = new PrintStream(bout, true, "UTF-8");
   1170         ps.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
   1171         ps
   1172                 .println("<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">");
   1173         ps.println("<properties>");
   1174         ps.println("<comment>comment</comment>");
   1175         ps.println("<entry key=\"key4\">value4</entry>");
   1176         ps.println("<entry key=\"key3\">value3</entry>");
   1177         ps.println("<entry key=\"key2\">value2</entry>");
   1178         ps.println("<entry key=\"key1\"><!-- xml comment -->value1</entry>");
   1179         ps.println("</properties>");
   1180         ps.close();
   1181         return bout.toByteArray();
   1182     }
   1183 
   1184     protected byte[] writePropertiesXMLISO_8859_1() throws IOException {
   1185         PrintStream ps = null;
   1186         ByteArrayOutputStream bout = new ByteArrayOutputStream();
   1187         ps = new PrintStream(bout, true, "ISO-8859-1");
   1188         ps.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
   1189         ps
   1190                 .println("<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">");
   1191         ps.println("<properties>");
   1192         ps.println("<comment>comment</comment>");
   1193         ps.println("<entry key=\"key4\">value4</entry>");
   1194         ps.println("<entry key=\"key3\">value3</entry>");
   1195         ps.println("<entry key=\"key2\">value2</entry>");
   1196         ps.println("<entry key=\"key1\"><!-- xml comment -->value1</entry>");
   1197         ps.println("</properties>");
   1198         ps.close();
   1199         return bout.toByteArray();
   1200     }
   1201 }
   1202