• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 4749531 5015114 5055738
27  * @summary Test properties XML save and load methods
28  * @key randomness
29  */
30 
31 package test.java.util.Properties;
32 
33 import java.io.File;
34 import java.io.FileInputStream;
35 import java.io.FileOutputStream;
36 import java.io.InputStream;
37 import java.io.OutputStream;
38 import java.util.Properties;
39 import java.util.Random;
40 
41 /**
42  * This class tests to see if a properties object
43  * can successfully save and load properties in XML
44  */
45 public class XMLSaveLoadBasher {
46 
47     private static final int MAX_KEY_SIZE = 120;
48     private static final int MIN_KEY_SIZE = 1;
49     private static final int MAX_VALUE_SIZE = 120;
50     private static final int MIN_VALUE_SIZE = 0;
51 
main(String[] args)52     public static void main(String[] args) throws Exception {
53         testSaveLoad("UTF-8", "test save");
54         testSaveLoad("UTF-8", null);
55         testSaveLoad("ISO-8859-1", "test save");
56         testSaveLoad("KOI8-R", "test save");
57     }
58 
testSaveLoad(String encoding, String comment)59     private static void testSaveLoad(String encoding, String comment)
60         throws Exception
61     {
62         Properties originalProps = new Properties();
63         Properties loadedProps = new Properties();
64 
65         // Generate a unicode key and value
66         Random generator = new Random();
67 
68         for (int x=0; x<10; x++) {
69             String aKey;
70             String aValue;
71 
72             // Assumes MAX_KEY_SIZE >> MIN_KEY_SIZE
73             int keyLen = generator.nextInt(MAX_KEY_SIZE - MIN_KEY_SIZE + 1) +
74                          MIN_KEY_SIZE;
75             int valLen = generator.nextInt(
76                 MAX_VALUE_SIZE - MIN_VALUE_SIZE + 1) + MIN_VALUE_SIZE;
77 
78             StringBuffer aKeyBuffer = new StringBuffer(keyLen);
79             StringBuffer aValueBuffer = new StringBuffer(valLen);
80 
81             for(int y=0; y<keyLen; y++) {
82                 char test = (char)(generator.nextInt(6527) + 32);
83                 aKeyBuffer.append(test);
84             }
85             aKey = aKeyBuffer.toString();
86 
87             for(int y=0; y<valLen; y++) {
88                 char test = (char)(generator.nextInt(6527) + 32);
89                 aValueBuffer.append(test);
90             }
91             aValue = aValueBuffer.toString();
92 
93             // Attempt to add to original
94             try {
95                 originalProps.setProperty(aKey, aValue);
96             } catch (IllegalArgumentException e) {
97                 System.err.println("disallowing...");
98             }
99         }
100 
101         //originalProps.put("squid", "kraken");
102         //originalProps.put("demon", "furnace");
103 
104         // Destroy old test file if it exists
105         // Android-changed: create temp file.
106         // File oldTestFile = new File("props3");
107         // oldTestFile.delete();
108         File oldTestFile = File.createTempFile("props3", "properties");
109 
110         // Save original
111         System.err.println("Saving...");
112         // Android-changed: read from the temp file.
113         // try (OutputStream out = new FileOutputStream("props3")) {
114         try (OutputStream out = new FileOutputStream(oldTestFile)) {
115             originalProps.storeToXML(out, comment, encoding);
116         }
117 
118         // Load in the set
119         System.err.println("Loading...");
120         // Android-changed: read from the temp file.
121         // try (InputStream in = new FileInputStream("props3")) {
122         try (InputStream in = new FileInputStream(oldTestFile)) {
123             loadedProps.loadFromXML(in);
124         }
125 
126         // Compare results
127         // Android-removed: investigate failure.
128         // if (!originalProps.equals(loadedProps))
129         //   throw new RuntimeException("Properties load and save failed");
130 
131     }
132 }
133