Skip to content
Permalink
Browse files
8183372: Refactor java/lang/Class shell tests to java
Reviewed-by: bchristi, mchung
  • Loading branch information
mahendrachhipa authored and Brent Christian committed Feb 2, 2021
1 parent 105d3e8 commit 6dc3c6dcddcbcb3a1a93415d66f1d9595b217519
@@ -21,9 +21,21 @@
* questions.
*/

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

/*
* Used by NonJavaNames.sh; needs to be run with a classpath including
* test/java/lang/Class/forName/classes
* @test
* @bug 4952558
* @library /test/lib
* @run testng/othervm NonJavaNames
* @summary Verify names that aren't legal Java names are accepted by forName.
*/

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

NonJavaNames.myInterface create(){
NonJavaNames.myInterface create() {
// With target 1.5, this class's name will include a '+'
// instead of a '$'.
class Baz2 implements NonJavaNames.myInterface {
public Baz2(){}
public Baz2() { }
}

return new Baz2();
}

public static void main(String[] args) throws Exception {
NonJavaNames.Baz bz = new NonJavaNames.Baz();
private static final String SRC_DIR = System.getProperty("test.src");
private static final Path TEST_SRC = Path.of(SRC_DIR, "classes");
private static final Path TEST_CLASSES = Path.of(System.getProperty("test.classes", "."));

@BeforeClass
public void createInvalidNameClasses() throws IOException {
Path hyphenPath = TEST_SRC.resolve("hyphen.class");
Path commaPath = TEST_SRC.resolve("comma.class");
Path periodPath = TEST_SRC.resolve("period.class");
Path leftsquarePath = TEST_SRC.resolve("left-square.class");
Path rightsquarePath = TEST_SRC.resolve("right-square.class");
Path plusPath = TEST_SRC.resolve("plus.class");
Path semicolonPath = TEST_SRC.resolve("semicolon.class");
Path zeroPath = TEST_SRC.resolve("0.class");
Path threePath = TEST_SRC.resolve("3.class");
Path zadePath = TEST_SRC.resolve("Z.class");

Path dhyphenPath = TEST_CLASSES.resolve("-.class");
Path dcommaPath = TEST_CLASSES.resolve(",.class");
Path dperiodPath = TEST_CLASSES.resolve("..class");
Path dleftsquarePath = TEST_CLASSES.resolve("[.class");
Path drightsquarePath = TEST_CLASSES.resolve("].class");
Path dplusPath = TEST_CLASSES.resolve("+.class");
Path dsemicolonPath = TEST_CLASSES.resolve(";.class");
Path dzeroPath = TEST_CLASSES.resolve("0.class");
Path dthreePath = TEST_CLASSES.resolve("3.class");
Path dzadePath = TEST_CLASSES.resolve("Z.class");

Files.copy(hyphenPath, dhyphenPath, REPLACE_EXISTING);
Files.copy(commaPath, dcommaPath, REPLACE_EXISTING);
Files.copy(periodPath, dperiodPath, REPLACE_EXISTING);
Files.copy(leftsquarePath, dleftsquarePath, REPLACE_EXISTING);
Files.copy(rightsquarePath, drightsquarePath, REPLACE_EXISTING);
Files.copy(plusPath, dplusPath, REPLACE_EXISTING);
Files.copy(semicolonPath, dsemicolonPath, REPLACE_EXISTING);
Files.copy(zeroPath, dzeroPath, REPLACE_EXISTING);
Files.copy(threePath, dthreePath, REPLACE_EXISTING);
Files.copy(zadePath, dzadePath, REPLACE_EXISTING);
}

@Test
public void testForNameReturnsSameClass() throws ClassNotFoundException {
NonJavaNames.Baz bz = new NonJavaNames.Baz();
String name;

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

String goodNonJavaClassNames [] = {
",",
"+",
"-",
"0",
"3",
// ":", These names won't work under windows.
// "<",
// ">",
"Z",
"]"
};
@Test(dataProvider = "goodNonJavaClassNames")
public void testGoodNonJavaClassNames(String name) throws ClassNotFoundException {
System.out.println("Testing good class name ``" + name + "''");
Class.forName(name);
}

for(String s : goodNonJavaClassNames) {
System.out.println("Testing good class name ``" + s + "''");
Class.forName(s);
@Test(dataProvider = "badNonJavaClassNames")
public void testBadNonJavaClassNames(String name) {
System.out.println("Testing bad class name ``" + name + "''");
try {
Class.forName(name);
} catch (ClassNotFoundException e) {
// Expected behavior
return;
}
throw new RuntimeException("Bad class name ``" + name + "'' accepted.");
}

String badNonJavaClassNames [] = {
";",
"[",
"."
@DataProvider(name = "goodNonJavaClassNames")
Object[][] getGoodNonJavaClassNames() {
return new Object[][] {
{","},
{"+"},
{"-"},
{"0"},
{"3"},
// ":", These names won't work under windows.
// "<",
// ">",
{"Z"},
{"]"}
};
}

for(String s : badNonJavaClassNames) {
System.out.println("Testing bad class name ``" + s + "''");
try {
Class.forName(s);
} catch (Exception e) {
// Expected behavior
continue;
}
throw new RuntimeException("Bad class name ``" + s + "'' accepted.");
}
@DataProvider(name = "badNonJavaClassNames")
Object[][] getBadNonJavaClassNames() {
return new Object[][] {
{";"},
{"["},
{"."}
};
}
}

This file was deleted.

Loading

0 comments on commit 6dc3c6d

Please sign in to comment.