summaryrefslogtreecommitdiffstats
path: root/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/PropertiesTest.java
blob: 27cae4e3656ed5cfe83dbf1acc040b05cac4186f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.apache.harmony.tests.java.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.InvalidPropertiesFormatException;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;
import java.util.Set;
import tests.support.resource.Support_Resources;

public class PropertiesTest extends junit.framework.TestCase {

    Properties tProps;

    byte[] propsFile;

    /**
     * java.util.Properties#Properties()
     */
    public void test_Constructor() {
        // Test for method java.util.Properties()
        Properties p = new Properties();
        // do something to avoid getting a variable unused warning
        p.clear();
        assertTrue("Created incorrect Properties", true);
    }

    public void test_loadLjava_io_InputStream_NPE() throws Exception {
        Properties p = new Properties();
        try {
            p.load((InputStream) null);
            fail("should throw NullPointerException");
        } catch (NullPointerException e) {
            // Expected
        }
    }

    public void test_loadsave() throws Exception {
        Properties p = new Properties();
        try {
            p.load((InputStream) null);
            fail("should throw NPE");
        } catch (NullPointerException npe) {
            // expected
        }
    }

    /**
     * java.util.Properties#Properties(java.util.Properties)
     */
    public void test_ConstructorLjava_util_Properties() {
        Properties systemProperties = System.getProperties();
        Properties properties = new Properties(systemProperties);
        Enumeration<?> propertyNames = systemProperties.propertyNames();
        String propertyName = null;
        while (propertyNames.hasMoreElements()) {
            propertyName = (String) propertyNames.nextElement();
            assertEquals("failed to construct correct properties",
                    systemProperties.getProperty(propertyName),
                    properties.getProperty(propertyName));
        }
    }

    /**
     * java.util.Properties#getProperty(java.lang.String)
     */
    public void test_getPropertyLjava_lang_String() {
        // Test for method java.lang.String
        // java.util.Properties.getProperty(java.lang.String)
        assertEquals("Did not retrieve property", "this is a test property",
                ((String) tProps.getProperty("test.prop")));
    }

    /**
     * java.util.Properties#getProperty(java.lang.String,
     *java.lang.String)
     */
    public void test_getPropertyLjava_lang_StringLjava_lang_String() {
        // Test for method java.lang.String
        // java.util.Properties.getProperty(java.lang.String, java.lang.String)
        assertEquals("Did not retrieve property", "this is a test property",
                ((String) tProps.getProperty("test.prop", "Blarg")));
        assertEquals("Did not return default value", "Gabba", ((String) tProps
                .getProperty("notInThere.prop", "Gabba")));
    }

    /**
     * java.util.Properties#getProperty(java.lang.String)
     */
    public void test_getPropertyLjava_lang_String2() {
        // regression test for HARMONY-3518
        MyProperties props = new MyProperties();
        assertNull(props.getProperty("key"));
    }

    /**
     * java.util.Properties#getProperty(java.lang.String,
     *java.lang.String)
     */
    public void test_getPropertyLjava_lang_StringLjava_lang_String2() {
        // regression test for HARMONY-3518
        MyProperties props = new MyProperties();
        assertEquals("defaultValue", props.getProperty("key", "defaultValue"));
    }

    // regression testing for HARMONY-3518
    static class MyProperties extends Properties {
        public synchronized Object get(Object key) {
            return getProperty((String) key); // assume String
        }
    }

    /**
     * java.util.Properties#list(java.io.PrintStream)
     */
    public void test_listLjava_io_PrintStream() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(baos);
        Properties myProps = new Properties();
        myProps.setProperty("Abba", "Cadabra");
        myProps.setProperty("Open", "Sesame");
        myProps.setProperty("LongProperty",
                "a long long long long long long long property");
        myProps.list(ps);
        ps.flush();
        String propList = baos.toString();
        assertTrue("Property list innacurate",
                propList.indexOf("Abba=Cadabra") >= 0);
        assertTrue("Property list innacurate",
                propList.indexOf("Open=Sesame") >= 0);
        assertTrue("property list do not conatins \"...\"", propList
                .indexOf("...") != -1);

        ps = null;
        try {
            myProps.list(ps);
            fail("should throw NullPointerException");
        } catch (NullPointerException e) {
            // expected
        }
    }

    /**
     * java.util.Properties#list(java.io.PrintWriter)
     */
    public void test_listLjava_io_PrintWriter() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintWriter pw = new PrintWriter(baos);
        Properties myProps = new Properties();
        myProps.setProperty("Abba", "Cadabra");
        myProps.setProperty("Open", "Sesame");
        myProps.setProperty("LongProperty",
                "a long long long long long long long property");
        myProps.list(pw);
        pw.flush();
        String propList = baos.toString();
        assertTrue("Property list innacurate",
                propList.indexOf("Abba=Cadabra") >= 0);
        assertTrue("Property list innacurate",
                propList.indexOf("Open=Sesame") >= 0);
        pw = null;
        try {
            myProps.list(pw);
            fail("should throw NullPointerException");
        } catch (NullPointerException e) {
            // expected
        }
    }

    /**
     * java.util.Properties#load(java.io.InputStream)
     */
    public void test_loadLjava_io_InputStream() {
        // Test for method void java.util.Properties.load(java.io.InputStream)
        Properties prop = null;
        try {
            InputStream is;
            prop = new Properties();
            prop.load(is = new ByteArrayInputStream(writeProperties()));
            is.close();
        } catch (Exception e) {
            fail("Exception during load test : " + e.getMessage());
        }
        assertEquals("Failed to load correct properties", "harmony.tests", prop
                .getProperty("test.pkg"));
        assertNull("Load failed to parse incorrectly", prop
                .getProperty("commented.entry"));

        prop = new Properties();
        try {
            prop.load(new ByteArrayInputStream("=".getBytes()));
        } catch (IOException e) {
            // expected
        }
        assertTrue("Failed to add empty key", prop.get("").equals(""));

        prop = new Properties();
        try {
            prop.load(new ByteArrayInputStream(" = ".getBytes()));
        } catch (IOException e) {
            // expected
        }
        assertTrue("Failed to add empty key2", prop.get("").equals(""));

        prop = new Properties();
        try {
            prop.load(new ByteArrayInputStream(" a= b".getBytes()));
        } catch (IOException e) {
            // expected
        }
        assertEquals("Failed to ignore whitespace", "b", prop.get("a"));

        prop = new Properties();
        try {
            prop.load(new ByteArrayInputStream(" a b".getBytes()));
        } catch (IOException e) {
            // expected
        }
        assertEquals("Failed to interpret whitespace as =", "b", prop.get("a"));

        prop = new Properties();
        try {
            prop.load(new ByteArrayInputStream("#\u008d\u00d2\na=\u008d\u00d3"
                    .getBytes("ISO8859_1")));
        } catch (IOException e) {
            // expected
        }
        assertEquals("Failed to parse chars >= 0x80", "\u008d\u00d3", prop
                .get("a"));

        prop = new Properties();
        try {
            prop.load(new ByteArrayInputStream(
                    "#properties file\r\nfred=1\r\n#last comment"
                            .getBytes("ISO8859_1")));
        } catch (IOException e) {
            // expected
        } catch (IndexOutOfBoundsException e) {
            fail("IndexOutOfBoundsException when last line is a comment with no line terminator");
        }
        assertEquals("Failed to load when last line contains a comment", "1",
                prop.get("fred"));
    }

    /**
     * java.util.Properties#load(java.io.InputStream)
     */
    public void test_loadLjava_io_InputStream_subtest0() {
        try {
            InputStream is = Support_Resources
                    .getStream("hyts_PropertiesTest.properties");
            Properties props = new Properties();
            props.load(is);
            is.close();
            assertEquals("1", "\n \t \f", props.getProperty(" \r"));
            assertEquals("2", "a", props.getProperty("a"));
            assertEquals("3", "bb as,dn   ", props.getProperty("b"));
            assertEquals("4", ":: cu", props.getProperty("c\r \t\nu"));
            assertEquals("5", "bu", props.getProperty("bu"));
            assertEquals("6", "d\r\ne=e", props.getProperty("d"));
            assertEquals("7", "fff", props.getProperty("f"));
            assertEquals("8", "g", props.getProperty("g"));
            assertEquals("9", "", props.getProperty("h h"));
            assertEquals("10", "i=i", props.getProperty(" "));
            assertEquals("11", "   j", props.getProperty("j"));
            assertEquals("12", "   c", props.getProperty("space"));
            assertEquals("13", "\\", props.getProperty("dblbackslash"));
        } catch (IOException e) {
            fail("Unexpected: " + e);
        }
    }

    /**
     * @throws IOException
     * java.util.Properties#load(java.io.Reader)
     * @since 1.6
     */
    public void test_loadLjava_io_Reader() throws IOException {
        Properties prop = null;
        try {
            InputStream is;
            prop = new Properties();
            is = new ByteArrayInputStream(writeProperties());
            prop.load(new InputStreamReader(is));
            is.close();
        } catch (Exception e) {
            fail("Exception during load test : " + e.getMessage());
        }
        assertEquals("Failed to load correct properties", "harmony.tests", prop
                .getProperty("test.pkg"));
        assertNull("Load failed to parse incorrectly", prop
                .getProperty("commented.entry"));

        prop = new Properties();
        prop.load(new ByteArrayInputStream("=".getBytes()));
        assertEquals("Failed to add empty key", "", prop.get(""));

        prop = new Properties();
        prop.load(new ByteArrayInputStream(" = ".getBytes()));
        assertEquals("Failed to add empty key2", "", prop.get(""));

        prop = new Properties();
        prop.load(new ByteArrayInputStream(" a= b".getBytes()));
        assertEquals("Failed to ignore whitespace", "b", prop.get("a"));

        prop = new Properties();
        prop.load(new ByteArrayInputStream(" a b".getBytes()));
        assertEquals("Failed to interpret whitespace as =", "b", prop.get("a"));

        prop = new Properties();
        prop.load(new ByteArrayInputStream("#comment\na=value"
                .getBytes("UTF-8")));
        assertEquals("value", prop.getProperty("a"));

        prop = new Properties();
        prop.load(new InputStreamReader(new ByteArrayInputStream(
                "#\u008d\u00d2\na=\u008d\u00d3".getBytes("UTF-8"))));
        try {
            prop
                    .load(new InputStreamReader(new ByteArrayInputStream(
                            "#\u008d\u00d2\na=\\\\u008d\\\\\\uu00d3"
                                    .getBytes("UTF-8"))));
            fail("Should throw IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            // expected
        }

        prop = new Properties();
        try {
            prop.load(new InputStreamReader(new ByteArrayInputStream(
                    "#properties file\r\nfred=1\r\n#last comment"
                            .getBytes("ISO8859_1"))));
        } catch (IndexOutOfBoundsException e) {
            fail("IndexOutOfBoundsException when last line is a comment with no line terminator");
        }
        assertEquals("Failed to load when last line contains a comment", "1",
                prop.get("fred"));

        // Regression tests for HARMONY-5414
        prop = new Properties();
        prop.load(new ByteArrayInputStream("a=\\u1234z".getBytes()));

        prop = new Properties();
        try {
            prop.load(new ByteArrayInputStream("a=\\u123".getBytes()));
            fail("should throw IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            // Expected
        }

        prop = new Properties();
        try {
            prop.load(new ByteArrayInputStream("a=\\u123z".getBytes()));
            fail("should throw IllegalArgumentException");
        } catch (IllegalArgumentException expected) {
            // Expected
        }

        prop = new Properties();
        Properties expected = new Properties();
        expected.put("a", "\u0000");
        prop.load(new ByteArrayInputStream("a=\\".getBytes()));
        assertEquals("Failed to read trailing slash value", expected, prop);

        prop = new Properties();
        expected = new Properties();
        expected.put("a", "\u1234\u0000");
        prop.load(new ByteArrayInputStream("a=\\u1234\\".getBytes()));
        assertEquals("Failed to read trailing slash value #2", expected, prop);

        prop = new Properties();
        expected = new Properties();
        expected.put("a", "q");
        prop.load(new ByteArrayInputStream("a=\\q".getBytes()));
        assertEquals("Failed to read slash value #3", expected, prop);
    }

    /**
     * java.util.Properties#load(java.io.InputStream)
     */
    public void test_loadLjava_io_InputStream_Special() throws IOException {
        // Test for method void java.util.Properties.load(java.io.InputStream)
        Properties prop = null;
        prop = new Properties();
        prop.load(new ByteArrayInputStream("=".getBytes()));
        assertTrue("Failed to add empty key", prop.get("").equals(""));

        prop = new Properties();
        prop.load(new ByteArrayInputStream("=\r\n".getBytes()));
        assertTrue("Failed to add empty key", prop.get("").equals(""));

        prop = new Properties();
        prop.load(new ByteArrayInputStream("=\n\r".getBytes()));
        assertTrue("Failed to add empty key", prop.get("").equals(""));
    }

    /**
     * @throws IOException
     * java.util.Properties#load(java.io.Reader)
     * @since 1.6
     */
    public void test_loadLjava_io_Reader_subtest0() throws IOException {
        InputStream is = Support_Resources
                .getStream("hyts_PropertiesTest.properties");
        Properties props = new Properties();
        props.load(new InputStreamReader(is));
        is.close();
        assertEquals("1", "\n \t \f", props.getProperty(" \r"));
        assertEquals("2", "a", props.getProperty("a"));
        assertEquals("3", "bb as,dn   ", props.getProperty("b"));
        assertEquals("4", ":: cu", props.getProperty("c\r \t\nu"));
        assertEquals("5", "bu", props.getProperty("bu"));
        assertEquals("6", "d\r\ne=e", props.getProperty("d"));
        assertEquals("7", "fff", props.getProperty("f"));
        assertEquals("8", "g", props.getProperty("g"));
        assertEquals("9", "", props.getProperty("h h"));
        assertEquals("10", "i=i", props.getProperty(" "));
        assertEquals("11", "   j", props.getProperty("j"));
        assertEquals("12", "   c", props.getProperty("space"));
        assertEquals("13", "\\", props.getProperty("dblbackslash"));
    }

    /**
     * {@link java.util.Properties#stringPropertyNames()}
     * @since 1.6
     */
    public void test_stringPropertyNames() {
        Set<String> set = tProps.stringPropertyNames();
        assertEquals(2, set.size());
        assertTrue(set.contains("test.prop"));
        assertTrue(set.contains("bogus.prop"));
        assertNotSame(set, tProps.stringPropertyNames());

        set = new Properties().stringPropertyNames();
        assertEquals(0, set.size());

        set = new Properties(System.getProperties()).stringPropertyNames();
        assertTrue(set.size() > 0);

        tProps = new Properties(tProps);
        tProps.put("test.prop", "anotherValue");
        tProps.put("3rdKey", "3rdValue");
        set = tProps.stringPropertyNames();
        assertEquals(3, set.size());
        assertTrue(set.contains("test.prop"));
        assertTrue(set.contains("bogus.prop"));
        assertTrue(set.contains("3rdKey"));

        tProps.put(String.class, "valueOfNonStringKey");
        set = tProps.stringPropertyNames();
        assertEquals(3, set.size());
        assertTrue(set.contains("test.prop"));
        assertTrue(set.contains("bogus.prop"));
        assertTrue(set.contains("3rdKey"));

        tProps.put("4thKey", "4thValue");
        assertEquals(4, tProps.size());
        assertEquals(3, set.size());

        try {
            set.add("another");
            fail("Should throw UnsupportedOperationException");
        } catch (UnsupportedOperationException e) {
            // expected
        }
    }

    /**
     * {@link java.util.Properties#stringPropertyNames()}
     * @since 1.6
     */
    public void test_stringPropertyNames_scenario1() {
        String[] keys = new String[] { "key1", "key2", "key3" };
        String[] values = new String[] { "value1", "value2", "value3" };
        List<String> keyList = Arrays.asList(keys);

        Properties properties = new Properties();
        for (int index = 0; index < keys.length; index++) {
            properties.setProperty(keys[index], values[index]);
        }

        properties = new Properties(properties);
        Set<String> nameSet = properties.stringPropertyNames();
        assertEquals(keys.length, nameSet.size());
        Iterator<String> iterator = nameSet.iterator();
        while (iterator.hasNext()) {
            assertTrue(keyList.contains(iterator.next()));
        }

        Enumeration<?> nameEnum = properties.propertyNames();
        int count = 0;
        while (nameEnum.hasMoreElements()) {
            count++;
            assertTrue(keyList.contains(nameEnum.nextElement()));
        }
        assertEquals(keys.length, count);

        properties = new Properties(properties);
        nameSet = properties.stringPropertyNames();
        assertEquals(keys.length, nameSet.size());
        iterator = nameSet.iterator();
        while (iterator.hasNext()) {
            assertTrue(keyList.contains(iterator.next()));
        }

        nameEnum = properties.propertyNames();
        count = 0;
        while (nameEnum.hasMoreElements()) {
            count++;
            assertTrue(keyList.contains(nameEnum.nextElement()));
        }
        assertEquals(keys.length, count);
    }

    /**
     * {@link java.util.Properties#stringPropertyNames()}
     * @since 1.6
     */
    public void test_stringPropertyNames_scenario2() {
        String[] defaultKeys = new String[] { "defaultKey1", "defaultKey2",
                "defaultKey3", "defaultKey4", "defaultKey5", "defaultKey6" };
        String[] defaultValues = new String[] { "defaultValue1",
                "defaultValue2", "defaultValue3", "defaultValue4",
                "defaultValue5", "defaultValue6" };
        List<String> keyList = new ArrayList<String>();
        Properties defaults = new Properties();
        for (int index = 0; index < 3; index++) {
            defaults.setProperty(defaultKeys[index], defaultValues[index]);
            keyList.add(defaultKeys[index]);
        }

        String[] keys = new String[] { "key1", "key2", "key3" };
        String[] values = new String[] { "value1", "value2", "value3" };
        Properties properties = new Properties(defaults);
        for (int index = 0; index < keys.length; index++) {
            properties.setProperty(keys[index], values[index]);
            keyList.add(keys[index]);
        }

        Set<String> nameSet = properties.stringPropertyNames();
        assertEquals(keyList.size(), nameSet.size());
        Iterator<String> iterator = nameSet.iterator();
        while (iterator.hasNext()) {
            assertTrue(keyList.contains(iterator.next()));
        }

        Enumeration<?> nameEnum = properties.propertyNames();
        int count = 0;
        while (nameEnum.hasMoreElements()) {
            count++;
            assertTrue(keyList.contains(nameEnum.nextElement()));
        }
        assertEquals(keyList.size(), count);

        for (int index = 3; index < defaultKeys.length; index++) {
            defaults.setProperty(defaultKeys[index], defaultValues[index]);
            keyList.add(defaultKeys[index]);
        }

        nameSet = properties.stringPropertyNames();
        assertEquals(keyList.size(), nameSet.size());
        iterator = nameSet.iterator();
        while (iterator.hasNext()) {
            assertTrue(keyList.contains(iterator.next()));
        }

        nameEnum = properties.propertyNames();
        count = 0;
        while (nameEnum.hasMoreElements()) {
            count++;
            assertTrue(keyList.contains(nameEnum.nextElement()));
        }
        assertEquals(keyList.size(), count);
    }

    /**
     * java.util.Properties#save(java.io.OutputStream, java.lang.String)
     */
    public void test_saveLjava_io_OutputStreamLjava_lang_String() {
        // Test for method void java.util.Properties.save(java.io.OutputStream,
        // java.lang.String)
        Properties myProps = new Properties();
        Properties myProps2 = new Properties();

        myProps.setProperty("Property A", "aye");
        myProps.setProperty("Property B", "bee");
        myProps.setProperty("Property C", "see");

        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            myProps.save(out, "A Header");
            out.close();
            ByteArrayInputStream in = new ByteArrayInputStream(out
                    .toByteArray());
            myProps2.load(in);
            in.close();
        } catch (IOException ioe) {
            fail("IOException occurred reading/writing file : "
                    + ioe.getMessage());
        }

        Enumeration e = myProps.propertyNames();
        String nextKey;
        while (e.hasMoreElements()) {
            nextKey = (String) e.nextElement();
            assertEquals("Stored property list not equal to original", myProps
                    .getProperty(nextKey), myProps2.getProperty(nextKey));
        }
    }

    /**
     * java.util.Properties#setProperty(java.lang.String,
     *java.lang.String)
     */
    public void test_setPropertyLjava_lang_StringLjava_lang_String() {
        // Test for method java.lang.Object
        // java.util.Properties.setProperty(java.lang.String, java.lang.String)
        Properties myProps = new Properties();
        myProps.setProperty("Yoink", "Yabba");
        assertEquals("Failed to set property", "Yabba", myProps
                .getProperty("Yoink"));
        myProps.setProperty("Yoink", "Gab");
        assertEquals("Failed to reset property", "Gab", myProps
                .getProperty("Yoink"));
    }

    /**
     * java.util.Properties#store(java.io.OutputStream, java.lang.String)
     */
    public void test_storeLjava_io_OutputStreamLjava_lang_String() {
        // Test for method void java.util.Properties.store(java.io.OutputStream,
        // java.lang.String)
        Properties myProps = new Properties();
        Properties myProps2 = new Properties();
        Enumeration e;
        String nextKey;

        myProps.put("Property A", " aye\\\f\t\n\r\b");
        myProps.put("Property B", "b ee#!=:");
        myProps.put("Property C", "see");

        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            myProps.store(out, "A Header");
            out.close();
            ByteArrayInputStream in = new ByteArrayInputStream(out
                    .toByteArray());
            myProps2.load(in);
            in.close();
        } catch (IOException ioe) {
            fail("IOException occurred reading/writing file : "
                    + ioe.getMessage());
        }

        e = myProps.propertyNames();
        while (e.hasMoreElements()) {
            nextKey = (String) e.nextElement();
            assertTrue("Stored property list not equal to original", myProps2
                    .getProperty(nextKey).equals(myProps.getProperty(nextKey)));
        }

    }

    /**
     * @throws IOException
     * java.util.Properties#store(java.io.Writer, java.lang.String)
     * @since 1.6
     */
    public void test_storeLjava_io_WriterLjava_lang_String() throws IOException {
        Properties myProps = new Properties();
        Properties myProps2 = new Properties();

        myProps.put("Property A", " aye\\\f\t\n\r\b");
        myProps.put("Property B", "b ee#!=:");
        myProps.put("Property C", "see");

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        myProps.store(new OutputStreamWriter(out), "A Header");
        Scanner scanner = new Scanner(out.toString());
        assertTrue(scanner.nextLine().startsWith("#A Header"));
        assertTrue(scanner.nextLine().startsWith("#"));
        out.close();
        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
        myProps2.load(in);
        in.close();

        Enumeration e = myProps.propertyNames();
        String nextKey;
        while (e.hasMoreElements()) {
            nextKey = (String) e.nextElement();
            assertTrue("Stored property list not equal to original", myProps2
                    .getProperty(nextKey).equals(myProps.getProperty(nextKey)));
        }

        try {
            myProps.store((Writer) null, "some comments");
            fail("Should throw NullPointerException");
        } catch (NullPointerException e1) {
            // expected
        }

        myProps.put(String.class, "wrong type");
        try {
            myProps.store(new OutputStreamWriter(new ByteArrayOutputStream()),
                    "some comments");
            fail("Should throw ClassCastException");
        } catch (ClassCastException e1) {
            // expected
        }
        myProps.remove(String.class);
        myProps.store(new OutputStreamWriter(new ByteArrayOutputStream()),
                "some comments");
        // it is OK
        myProps.put("wrong type", String.class);
        try {
            myProps.store(new OutputStreamWriter(new ByteArrayOutputStream()),
                    "some comments");
            fail("Should throw ClassCastException");
        } catch (ClassCastException e1) {
            // expected
        }
    }

    /**
     * java.util.Properties#loadFromXML(java.io.InputStream)
     */
    public void test_loadFromXMLLjava_io_InputStream() throws Exception {
        // Test for method void
        // java.util.Properties.loadFromXML(java.io.InputStream)
        Properties prop = null;
        try {
            InputStream is;
            prop = new Properties();
            prop.loadFromXML(is = new ByteArrayInputStream(
                    writePropertiesXMLUTF_8()));
            is.close();
        } catch (Exception e) {
            fail("Exception during load test : " + e.getMessage());
        }
        assertEquals("Failed to load correct properties", "value3", prop
                .getProperty("key3"));
        assertEquals("Failed to load correct properties", "value1", prop
                .getProperty("key1"));

        try {
            InputStream is;
            prop = new Properties();
            prop.loadFromXML(is = new ByteArrayInputStream(
                    writePropertiesXMLISO_8859_1()));
            is.close();
        } catch (Exception e) {
            fail("Exception during load test : " + e.getMessage());
        }
        assertEquals("Failed to load correct properties", "value2", prop
                .getProperty("key2"));
        assertEquals("Failed to load correct properties", "value1", prop
                .getProperty("key1"));

        try {
            prop.loadFromXML(null);
            fail("should throw NullPointerException");
        } catch (NullPointerException e) {
            // expected
        }
    }

    /**
     * java.util.Properties#storeToXML(java.io.OutputStream,
     *java.lang.String, java.lang.String)
     */
    public void test_storeToXMLLjava_io_OutputStreamLjava_lang_StringLjava_lang_String()
            throws Exception {
        // Test for method void
        // java.util.Properties.storeToXML(java.io.OutputStream,
        // java.lang.String, java.lang.String)
        Properties myProps = new Properties();
        Properties myProps2 = new Properties();
        Enumeration e;
        String nextKey;

        myProps.setProperty("key1", "value1");
        myProps.setProperty("key2", "value2");
        myProps.setProperty("key3", "value3");
        myProps.setProperty("<a>key4</a>", "\"value4");
        myProps.setProperty("key5   ", "<h>value5</h>");
        myProps.setProperty("<a>key6</a>", "   <h>value6</h>   ");
        myProps.setProperty("<comment>key7</comment>", "value7");
        myProps.setProperty("  key8   ", "<comment>value8</comment>");
        myProps.setProperty("&lt;key9&gt;", "\u0027value9");
        myProps.setProperty("key10\"", "&lt;value10&gt;");
        myProps.setProperty("&amp;key11&amp;", "value11");
        myProps.setProperty("key12", "&amp;value12&amp;");
        myProps.setProperty("<a>&amp;key13&lt;</a>",
                "&amp;&value13<b>&amp;</b>");

        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();

            // store in UTF-8 encoding
            myProps.storeToXML(out, "comment");
            out.close();
            ByteArrayInputStream in = new ByteArrayInputStream(out
                    .toByteArray());
            myProps2.loadFromXML(in);
            in.close();
        } catch (InvalidPropertiesFormatException ipfe) {
            fail("InvalidPropertiesFormatException occurred reading file: "
                    + ipfe.getMessage());
        } catch (IOException ioe) {
            fail("IOException occurred reading/writing file : "
                    + ioe.getMessage());
        }

        e = myProps.propertyNames();
        while (e.hasMoreElements()) {
            nextKey = (String) e.nextElement();
            assertTrue("Stored property list not equal to original", myProps2
                    .getProperty(nextKey).equals(myProps.getProperty(nextKey)));
        }

        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();

            // store in ISO-8859-1 encoding
            myProps.storeToXML(out, "comment", "ISO-8859-1");
            out.close();
            ByteArrayInputStream in = new ByteArrayInputStream(out
                    .toByteArray());
            myProps2 = new Properties();
            myProps2.loadFromXML(in);
            in.close();
        } catch (InvalidPropertiesFormatException ipfe) {
            fail("InvalidPropertiesFormatException occurred reading file: "
                    + ipfe.getMessage());
        } catch (IOException ioe) {
            fail("IOException occurred reading/writing file : "
                    + ioe.getMessage());
        }

        e = myProps.propertyNames();
        while (e.hasMoreElements()) {
            nextKey = (String) e.nextElement();
            assertTrue("Stored property list not equal to original", myProps2
                    .getProperty(nextKey).equals(myProps.getProperty(nextKey)));
        }

        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            myProps.storeToXML(out, null, null);
            fail("should throw nullPointerException");
        } catch (NullPointerException ne) {
            // expected
        }
    }

    /**
     * if loading from single line like "hello" without "\n\r" neither "=", it
     * should be same as loading from "hello="
     */
    public void testLoadSingleLine() throws Exception {
        Properties props = new Properties();
        InputStream sr = new ByteArrayInputStream("hello".getBytes());
        props.load(sr);
        assertEquals(1, props.size());
    }

    public void test_propertyNames() {
        Properties parent = new Properties();
        parent.setProperty("parent.a.key", "parent.a.value");
        parent.setProperty("parent.b.key", "parent.b.value");

        Enumeration<?> names = parent.propertyNames();
        assertPropertyEnumeration(names, "parent.a.key", "parent.b.key");

        Properties current = new Properties(parent);
        current.setProperty("current.a.key", "current.a.value");
        current.setProperty("current.b.key", "current.b.value");

        names = current.propertyNames();
        assertPropertyEnumeration(names,
                "parent.a.key",
                "parent.b.key",
                "current.a.key",
                "current.b.key");

        Properties child = new Properties(current);
        child.setProperty("child.a.key", "child.a.value");
        child.setProperty("child.b.key", "child.b.value");

        names = child.propertyNames();
        assertPropertyEnumeration(names,
                "parent.a.key",
                "parent.b.key",
                "current.a.key",
                "current.b.key",
                "child.a.key",
                "child.b.key");
    }

    public void assertPropertyEnumeration(Enumeration<?> propNames,
            String... expected) {
        Set<String> propsSet = new HashSet<String>();
        while (propNames.hasMoreElements()) {
            String next = (String) propNames.nextElement();
            assertFalse(propsSet.contains(next));
            propsSet.add(next);
        }

        assertEquals(expected.length, propsSet.size());
        assertTrue(propsSet.containsAll(Arrays.asList(expected)));
    }

    private String comment1 = "comment1";

    private String comment2 = "comment2";

    private void validateOutput(String[] expectStrings, byte[] output)
            throws IOException {
        ByteArrayInputStream bais = new ByteArrayInputStream(output);
        BufferedReader br = new BufferedReader(new InputStreamReader(bais,
                "ISO8859_1"));
        for (String expectString : expectStrings) {
            assertEquals(expectString, br.readLine());
        }
        br.readLine();
        assertNull(br.readLine());
        br.close();
    }

    public void testStore_scenario0() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Properties props = new Properties();
        props.store(baos, comment1 + '\r' + comment2);
        validateOutput(new String[] { "#comment1", "#comment2" },
                baos.toByteArray());
        baos.close();
    }

    public void testStore_scenario1() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Properties props = new Properties();
        props.store(baos, comment1 + '\n' + comment2);
        validateOutput(new String[] { "#comment1", "#comment2" },
                baos.toByteArray());
        baos.close();
    }

    public void testStore_scenario2() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Properties props = new Properties();
        props.store(baos, comment1 + '\r' + '\n' + comment2);
        validateOutput(new String[] { "#comment1", "#comment2" },
                baos.toByteArray());
        baos.close();
    }

    public void testStore_scenario3() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Properties props = new Properties();
        props.store(baos, comment1 + '\n' + '\r' + comment2);
        validateOutput(new String[] { "#comment1", "#", "#comment2" },
                baos.toByteArray());
        baos.close();
    }

    public void testStore_scenario4() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Properties props = new Properties();
        props.store(baos, comment1 + '\r' + '#' + comment2);
        validateOutput(new String[] { "#comment1", "#comment2" },
                baos.toByteArray());
        baos.close();
    }

    public void testStore_scenario5() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Properties props = new Properties();
        props.store(baos, comment1 + '\r' + '!' + comment2);
        validateOutput(new String[] { "#comment1", "!comment2" },
                baos.toByteArray());
        baos.close();
    }

    public void testStore_scenario6() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Properties props = new Properties();
        props.store(baos, comment1 + '\n' + '#' + comment2);
        validateOutput(new String[] { "#comment1", "#comment2" },
                baos.toByteArray());
        baos.close();
    }

    public void testStore_scenario7() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Properties props = new Properties();
        props.store(baos, comment1 + '\n' + '!' + comment2);
        validateOutput(new String[] { "#comment1", "!comment2" },
                baos.toByteArray());
        baos.close();
    }

    public void testStore_scenario8() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Properties props = new Properties();
        props.store(baos, comment1 + '\r' + '\n' + '#' + comment2);
        validateOutput(new String[] { "#comment1", "#comment2" },
                baos.toByteArray());
        baos.close();
    }

    public void testStore_scenario9() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Properties props = new Properties();
        props.store(baos, comment1 + '\n' + '\r' + '#' + comment2);
        validateOutput(new String[] { "#comment1", "#", "#comment2" },
                baos.toByteArray());
        baos.close();
    }

    public void testStore_scenario10() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Properties props = new Properties();
        props.store(baos, comment1 + '\r' + '\n' + '!' + comment2);
        validateOutput(new String[] { "#comment1", "!comment2" },
                baos.toByteArray());
        baos.close();
    }

    public void testStore_scenario11() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Properties props = new Properties();
        props.store(baos, comment1 + '\n' + '\r' + '!' + comment2);
        validateOutput(new String[] { "#comment1", "#", "!comment2" },
                baos.toByteArray());
        baos.close();
    }

    public void testLoadReader() throws IOException {
        InputStream inputStream = new ByteArrayInputStream(
                "\u3000key=value".getBytes("UTF-8"));
        Properties props = new Properties();
        props.load(inputStream);
        Enumeration<Object> keyEnum = props.keys();
        assertFalse("\u3000key".equals(keyEnum.nextElement()));
        assertFalse(keyEnum.hasMoreElements());
        inputStream.close();

        inputStream = new ByteArrayInputStream(
                "\u3000key=value".getBytes("UTF-8"));
        props = new Properties();
        props.load(new InputStreamReader(inputStream, "UTF-8"));
        keyEnum = props.keys();
        assertEquals("key", keyEnum.nextElement());
        assertFalse(keyEnum.hasMoreElements());
        inputStream.close();
    }

    /**
     * Sets up the fixture, for example, open a network connection. This method
     * is called before a test is executed.
     */
    protected void setUp() {

        tProps = new Properties();
        tProps.put("test.prop", "this is a test property");
        tProps.put("bogus.prop", "bogus");
    }

    /**
     * Tears down the fixture, for example, close a network connection. This
     * method is called after a test is executed.
     */
    protected void tearDown() {
    }

    /**
     * Tears down the fixture, for example, close a network connection. This
     * method is called after a test is executed.
     */
    protected byte[] writeProperties() throws IOException {
        PrintStream ps = null;
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ps = new PrintStream(bout);
        ps.println("#commented.entry=Bogus");
        ps.println("test.pkg=harmony.tests");
        ps.println("test.proj=Automated Tests");
        ps.close();
        return bout.toByteArray();
    }

    protected byte[] writePropertiesXMLUTF_8() throws IOException {
        PrintStream ps = null;
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ps = new PrintStream(bout, true, "UTF-8");
        ps.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        ps
                .println("<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">");
        ps.println("<properties>");
        ps.println("<comment>comment</comment>");
        ps.println("<entry key=\"key4\">value4</entry>");
        ps.println("<entry key=\"key3\">value3</entry>");
        ps.println("<entry key=\"key2\">value2</entry>");
        ps.println("<entry key=\"key1\"><!-- xml comment -->value1</entry>");
        ps.println("</properties>");
        ps.close();
        return bout.toByteArray();
    }

    protected byte[] writePropertiesXMLISO_8859_1() throws IOException {
        PrintStream ps = null;
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ps = new PrintStream(bout, true, "ISO-8859-1");
        ps.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
        ps
                .println("<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">");
        ps.println("<properties>");
        ps.println("<comment>comment</comment>");
        ps.println("<entry key=\"key4\">value4</entry>");
        ps.println("<entry key=\"key3\">value3</entry>");
        ps.println("<entry key=\"key2\">value2</entry>");
        ps.println("<entry key=\"key1\"><!-- xml comment -->value1</entry>");
        ps.println("</properties>");
        ps.close();
        return bout.toByteArray();
    }
}