Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JDK-8183372 : Refactor java/lang/Class shell tests to java #2170

Closed
wants to merge 12 commits into from
91 changes: 72 additions & 19 deletions test/jdk/java/lang/Class/forName/NonJavaNames.java
Expand Up @@ -21,9 +21,22 @@
* questions.
*/

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

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import org.testng.annotations.BeforeClass;
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
* @build NonJavaNames
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This @build can be dropped as this is done implicitly.

* @run testng/othervm NonJavaNames
* @summary Verify names that aren't legal Java names are accepted by forName.
*/

public class NonJavaNames {
Expand All @@ -44,9 +57,50 @@ 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 String TEST_SRC = SRC_DIR + "/classes/";
private static final String TEST_CLASSES = System.getProperty("test.classes", ".");
Path dhyphenPath, dcommaPath, dperiodPath, dleftsquarePath, drightsquarePath, dplusPath, dsemicolonPath, dzeroPath, dthreePath, dzadePath;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These variables should belong to the createInvalidNameClasses method. You can simply add Path type declaration in line 78-87.


@BeforeClass
public void createInvalidNameClasses() throws IOException {
Path hyphenPath = Paths.get(TEST_SRC + "hyphen.class");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Building a Path with string concatenation is an anti-pattern.
Also Path.of is probably preferred instead of Paths.get.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now not doing the concatenation of the strings to create Path.

Path commaPath = Paths.get(TEST_SRC + "comma.class");
Path periodPath = Paths.get(TEST_SRC + "period.class");
Path leftsquarePath = Paths.get(TEST_SRC + "left-square.class");
Path rightsquarePath = Paths.get(TEST_SRC + "right-square.class");
Path plusPath = Paths.get(TEST_SRC + "plus.class");
Path semicolonPath = Paths.get(TEST_SRC + "semicolon.class");
Path zeroPath = Paths.get(TEST_SRC + "0.class");
Path threePath = Paths.get(TEST_SRC + "3.class");
Path zadePath = Paths.get(TEST_SRC + "Z.class");

dhyphenPath = Paths.get(TEST_CLASSES + "/-.class");
dcommaPath = Paths.get(TEST_CLASSES + "/,.class");
dperiodPath = Paths.get(TEST_CLASSES + "/..class");
dleftsquarePath = Paths.get(TEST_CLASSES + "/[.class");
drightsquarePath = Paths.get(TEST_CLASSES + "/].class");
dplusPath = Paths.get(TEST_CLASSES + "/+.class");
dsemicolonPath = Paths.get(TEST_CLASSES + "/;.class");
dzeroPath = Paths.get(TEST_CLASSES + "/0.class");
dthreePath = Paths.get(TEST_CLASSES + "/3.class");
dzadePath = Paths.get(TEST_CLASSES + "/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 testGoodNonJavaClassNames() throws Throwable {
NonJavaNames.Baz bz = new NonJavaNames.Baz();
String name;

if (Class.forName(name=bz.getClass().getName()) != NonJavaNames.Baz.class) {
Expand All @@ -63,28 +117,27 @@ public static void main(String[] args) throws Exception {
}

String goodNonJavaClassNames [] = {
",",
"+",
"-",
"0",
"3",
// ":", These names won't work under windows.
// "<",
// ">",
"Z",
"]"
",",
"+",
"-",
"0",
"3",
// ":", These names won't work under windows.
// "<",
// ">",
"Z",
"]"
};

for(String s : goodNonJavaClassNames) {
System.out.println("Testing good class name ``" + s + "''");
Class.forName(s);
}
}

String badNonJavaClassNames [] = {
";",
"[",
"."
};
@Test
public void testBadNonJavaClassNames() {
String badNonJavaClassNames [] = {";", "[", "."};

for(String s : badNonJavaClassNames) {
System.out.println("Testing bad class name ``" + s + "''");
Expand Down
108 changes: 0 additions & 108 deletions test/jdk/java/lang/Class/forName/NonJavaNames.sh

This file was deleted.