Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Properties/XMLSaveLoadBasher.java
41149 views
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
import java.io.File;
32
import java.io.FileInputStream;
33
import java.io.FileOutputStream;
34
import java.io.InputStream;
35
import java.io.OutputStream;
36
import java.util.Properties;
37
import java.util.Random;
38
39
/**
40
* This class tests to see if a properties object
41
* can successfully save and load properties in XML
42
*/
43
public class XMLSaveLoadBasher {
44
45
private static final int MAX_KEY_SIZE = 120;
46
private static final int MIN_KEY_SIZE = 1;
47
private static final int MAX_VALUE_SIZE = 120;
48
private static final int MIN_VALUE_SIZE = 0;
49
50
public static void main(String[] args) throws Exception {
51
testSaveLoad("UTF-8", "test save");
52
testSaveLoad("UTF-8", null);
53
testSaveLoad("ISO-8859-1", "test save");
54
testSaveLoad("KOI8-R", "test save");
55
}
56
57
private static void testSaveLoad(String encoding, String comment)
58
throws Exception
59
{
60
Properties originalProps = new Properties();
61
Properties loadedProps = new Properties();
62
63
// Generate a unicode key and value
64
Random generator = new Random();
65
66
for (int x=0; x<10; x++) {
67
String aKey;
68
String aValue;
69
70
// Assumes MAX_KEY_SIZE >> MIN_KEY_SIZE
71
int keyLen = generator.nextInt(MAX_KEY_SIZE - MIN_KEY_SIZE + 1) +
72
MIN_KEY_SIZE;
73
int valLen = generator.nextInt(
74
MAX_VALUE_SIZE - MIN_VALUE_SIZE + 1) + MIN_VALUE_SIZE;
75
76
StringBuffer aKeyBuffer = new StringBuffer(keyLen);
77
StringBuffer aValueBuffer = new StringBuffer(valLen);
78
79
for(int y=0; y<keyLen; y++) {
80
char test = (char)(generator.nextInt(6527) + 32);
81
aKeyBuffer.append(test);
82
}
83
aKey = aKeyBuffer.toString();
84
85
for(int y=0; y<valLen; y++) {
86
char test = (char)(generator.nextInt(6527) + 32);
87
aValueBuffer.append(test);
88
}
89
aValue = aValueBuffer.toString();
90
91
// Attempt to add to original
92
try {
93
originalProps.setProperty(aKey, aValue);
94
} catch (IllegalArgumentException e) {
95
System.err.println("disallowing...");
96
}
97
}
98
99
//originalProps.put("squid", "kraken");
100
//originalProps.put("demon", "furnace");
101
102
// Destroy old test file if it exists
103
File oldTestFile = new File("props3");
104
oldTestFile.delete();
105
106
// Save original
107
System.err.println("Saving...");
108
try (OutputStream out = new FileOutputStream("props3")) {
109
originalProps.storeToXML(out, comment, encoding);
110
}
111
112
// Load in the set
113
System.err.println("Loading...");
114
try (InputStream in = new FileInputStream("props3")) {
115
loadedProps.loadFromXML(in);
116
}
117
118
// Compare results
119
if (!originalProps.equals(loadedProps))
120
throw new RuntimeException("Properties load and save failed");
121
122
}
123
}
124
125