Skip to content

Commit 6dc3c6d

Browse files
mahendrachhipaBrent Christian
authored and
Brent Christian
committedFeb 2, 2021
8183372: Refactor java/lang/Class shell tests to java
Reviewed-by: bchristi, mchung
1 parent 105d3e8 commit 6dc3c6d

File tree

5 files changed

+190
-251
lines changed

5 files changed

+190
-251
lines changed
 

‎test/jdk/java/lang/Class/forName/NonJavaNames.java

+95-35
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,21 @@
2121
* questions.
2222
*/
2323

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+
2433
/*
25-
* Used by NonJavaNames.sh; needs to be run with a classpath including
26-
* test/java/lang/Class/forName/classes
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.
2739
*/
2840

2941
public class NonJavaNames {
@@ -34,19 +46,59 @@ public Baz(){}
3446
public static interface myInterface {
3547
}
3648

37-
NonJavaNames.myInterface create(){
49+
NonJavaNames.myInterface create() {
3850
// With target 1.5, this class's name will include a '+'
3951
// instead of a '$'.
4052
class Baz2 implements NonJavaNames.myInterface {
41-
public Baz2(){}
53+
public Baz2() { }
4254
}
4355

4456
return new Baz2();
4557
}
4658

47-
public static void main(String[] args) throws Exception {
48-
NonJavaNames.Baz bz = new NonJavaNames.Baz();
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");
4975

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();
50102
String name;
51103

52104
if (Class.forName(name=bz.getClass().getName()) != NonJavaNames.Baz.class) {
@@ -61,40 +113,48 @@ public static void main(String[] args) throws Exception {
61113
System.err.println("Failures for class ``" + name + "''.");
62114
throw new RuntimeException();
63115
}
116+
}
64117

65-
String goodNonJavaClassNames [] = {
66-
",",
67-
"+",
68-
"-",
69-
"0",
70-
"3",
71-
// ":", These names won't work under windows.
72-
// "<",
73-
// ">",
74-
"Z",
75-
"]"
76-
};
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+
}
77123

78-
for(String s : goodNonJavaClassNames) {
79-
System.out.println("Testing good class name ``" + s + "''");
80-
Class.forName(s);
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;
81132
}
133+
throw new RuntimeException("Bad class name ``" + name + "'' accepted.");
134+
}
82135

83-
String badNonJavaClassNames [] = {
84-
";",
85-
"[",
86-
"."
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+
{"]"}
87149
};
150+
}
88151

89-
for(String s : badNonJavaClassNames) {
90-
System.out.println("Testing bad class name ``" + s + "''");
91-
try {
92-
Class.forName(s);
93-
} catch (Exception e) {
94-
// Expected behavior
95-
continue;
96-
}
97-
throw new RuntimeException("Bad class name ``" + s + "'' accepted.");
98-
}
152+
@DataProvider(name = "badNonJavaClassNames")
153+
Object[][] getBadNonJavaClassNames() {
154+
return new Object[][] {
155+
{";"},
156+
{"["},
157+
{"."}
158+
};
99159
}
100160
}

‎test/jdk/java/lang/Class/forName/NonJavaNames.sh

-108
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.