21
21
* questions.
22
22
*/
23
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
+
24
33
/*
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.
27
39
*/
28
40
29
41
public class NonJavaNames {
@@ -34,19 +46,59 @@ public Baz(){}
34
46
public static interface myInterface {
35
47
}
36
48
37
- NonJavaNames .myInterface create (){
49
+ NonJavaNames .myInterface create () {
38
50
// With target 1.5, this class's name will include a '+'
39
51
// instead of a '$'.
40
52
class Baz2 implements NonJavaNames .myInterface {
41
- public Baz2 (){ }
53
+ public Baz2 () { }
42
54
}
43
55
44
56
return new Baz2 ();
45
57
}
46
58
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" );
49
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 ();
50
102
String name ;
51
103
52
104
if (Class .forName (name =bz .getClass ().getName ()) != NonJavaNames .Baz .class ) {
@@ -61,40 +113,48 @@ public static void main(String[] args) throws Exception {
61
113
System .err .println ("Failures for class ``" + name + "''." );
62
114
throw new RuntimeException ();
63
115
}
116
+ }
64
117
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
+ }
77
123
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 ;
81
132
}
133
+ throw new RuntimeException ("Bad class name ``" + name + "'' accepted." );
134
+ }
82
135
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
+ {"]" }
87
149
};
150
+ }
88
151
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
+ };
99
159
}
100
160
}
0 commit comments