Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/Class/forName/NonJavaNames.java
41153 views
1
/*
2
* Copyright (c) 2003, 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
import java.io.IOException;
25
import java.nio.file.Files;
26
import java.nio.file.Path;
27
28
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
29
import org.testng.annotations.BeforeClass;
30
import org.testng.annotations.DataProvider;
31
import org.testng.annotations.Test;
32
33
/*
34
* @test
35
* @bug 4952558
36
* @library /test/lib
37
* @run testng/othervm NonJavaNames
38
* @summary Verify names that aren't legal Java names are accepted by forName.
39
*/
40
41
public class NonJavaNames {
42
public static class Baz {
43
public Baz(){}
44
}
45
46
public static interface myInterface {
47
}
48
49
NonJavaNames.myInterface create() {
50
// With target 1.5, this class's name will include a '+'
51
// instead of a '$'.
52
class Baz2 implements NonJavaNames.myInterface {
53
public Baz2() { }
54
}
55
56
return new Baz2();
57
}
58
59
private static final String SRC_DIR = System.getProperty("test.src");
60
private static final Path TEST_SRC = Path.of(SRC_DIR, "classes");
61
private static final Path TEST_CLASSES = Path.of(System.getProperty("test.classes", "."));
62
63
@BeforeClass
64
public void createInvalidNameClasses() throws IOException {
65
Path hyphenPath = TEST_SRC.resolve("hyphen.class");
66
Path commaPath = TEST_SRC.resolve("comma.class");
67
Path periodPath = TEST_SRC.resolve("period.class");
68
Path leftsquarePath = TEST_SRC.resolve("left-square.class");
69
Path rightsquarePath = TEST_SRC.resolve("right-square.class");
70
Path plusPath = TEST_SRC.resolve("plus.class");
71
Path semicolonPath = TEST_SRC.resolve("semicolon.class");
72
Path zeroPath = TEST_SRC.resolve("0.class");
73
Path threePath = TEST_SRC.resolve("3.class");
74
Path zadePath = TEST_SRC.resolve("Z.class");
75
76
Path dhyphenPath = TEST_CLASSES.resolve("-.class");
77
Path dcommaPath = TEST_CLASSES.resolve(",.class");
78
Path dperiodPath = TEST_CLASSES.resolve("..class");
79
Path dleftsquarePath = TEST_CLASSES.resolve("[.class");
80
Path drightsquarePath = TEST_CLASSES.resolve("].class");
81
Path dplusPath = TEST_CLASSES.resolve("+.class");
82
Path dsemicolonPath = TEST_CLASSES.resolve(";.class");
83
Path dzeroPath = TEST_CLASSES.resolve("0.class");
84
Path dthreePath = TEST_CLASSES.resolve("3.class");
85
Path dzadePath = TEST_CLASSES.resolve("Z.class");
86
87
Files.copy(hyphenPath, dhyphenPath, REPLACE_EXISTING);
88
Files.copy(commaPath, dcommaPath, REPLACE_EXISTING);
89
Files.copy(periodPath, dperiodPath, REPLACE_EXISTING);
90
Files.copy(leftsquarePath, dleftsquarePath, REPLACE_EXISTING);
91
Files.copy(rightsquarePath, drightsquarePath, REPLACE_EXISTING);
92
Files.copy(plusPath, dplusPath, REPLACE_EXISTING);
93
Files.copy(semicolonPath, dsemicolonPath, REPLACE_EXISTING);
94
Files.copy(zeroPath, dzeroPath, REPLACE_EXISTING);
95
Files.copy(threePath, dthreePath, REPLACE_EXISTING);
96
Files.copy(zadePath, dzadePath, REPLACE_EXISTING);
97
}
98
99
@Test
100
public void testForNameReturnsSameClass() throws ClassNotFoundException {
101
NonJavaNames.Baz bz = new NonJavaNames.Baz();
102
String name;
103
104
if (Class.forName(name=bz.getClass().getName()) != NonJavaNames.Baz.class) {
105
System.err.println("Class object from forName does not match object.class.");
106
System.err.println("Failures for class ``" + name + "''.");
107
throw new RuntimeException();
108
}
109
110
NonJavaNames.myInterface bz2 = (new NonJavaNames()).create();
111
if (Class.forName(name=bz2.getClass().getName()) != bz2.getClass()) {
112
System.err.println("Class object from forName does not match getClass.");
113
System.err.println("Failures for class ``" + name + "''.");
114
throw new RuntimeException();
115
}
116
}
117
118
@Test(dataProvider = "goodNonJavaClassNames")
119
public void testGoodNonJavaClassNames(String name) throws ClassNotFoundException {
120
System.out.println("Testing good class name ``" + name + "''");
121
Class.forName(name);
122
}
123
124
@Test(dataProvider = "badNonJavaClassNames")
125
public void testBadNonJavaClassNames(String name) {
126
System.out.println("Testing bad class name ``" + name + "''");
127
try {
128
Class.forName(name);
129
} catch (ClassNotFoundException e) {
130
// Expected behavior
131
return;
132
}
133
throw new RuntimeException("Bad class name ``" + name + "'' accepted.");
134
}
135
136
@DataProvider(name = "goodNonJavaClassNames")
137
Object[][] getGoodNonJavaClassNames() {
138
return new Object[][] {
139
{","},
140
{"+"},
141
{"-"},
142
{"0"},
143
{"3"},
144
// ":", These names won't work under windows.
145
// "<",
146
// ">",
147
{"Z"},
148
{"]"}
149
};
150
}
151
152
@DataProvider(name = "badNonJavaClassNames")
153
Object[][] getBadNonJavaClassNames() {
154
return new Object[][] {
155
{";"},
156
{"["},
157
{"."}
158
};
159
}
160
}
161
162