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

Conversation

@mahendrachhipa
Copy link
Contributor

@mahendrachhipa mahendrachhipa commented Jan 20, 2021

https://bugs.openjdk.java.net/browse/JDK-8183372


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

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

Reviewers

Download

$ git fetch https://git.openjdk.java.net/jdk pull/2170/head:pull/2170
$ git checkout pull/2170

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented Jan 20, 2021

👋 Welcome back mahendrachhipa! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr label Jan 20, 2021
@openjdk
Copy link

@openjdk openjdk bot commented Jan 20, 2021

@mahendrachhipa The following label will be automatically applied to this pull request:

  • core-libs

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the core-libs label Jan 20, 2021
@bchristi-git
Copy link
Member

@bchristi-git bchristi-git commented Jan 21, 2021

Can this be done all in EnclosingClassTest.java, without a new RunEnclosingClassTest.java?

Adding the @BeforeClass and @AfterClass methods to what's there, you may just need to
change the test() calls to use reflection - something along the lines of:

test(Class.forName("EnclosingClass").getDeclaredConstructor().newInstance());

Used newInstance() method to create the different EnclosingClasses at runtime
@mahendrachhipa
Copy link
Contributor Author

@mahendrachhipa mahendrachhipa commented Jan 22, 2021

Review comments implemented.

* @build NonJavaNames
* @run testng/othervm NonJavaNameTest
* @summary Verify names that aren't legal Java names are accepted by forName.
* @author Joseph D. Darcy

This comment has been minimized.

@mlchung

mlchung Jan 22, 2021
Member

we can drop this @author in particular this is a new file.

This comment has been minimized.

@mahendrachhipa

mahendrachhipa Jan 25, 2021
Author Contributor

Now this file is deleted.

public class NonJavaNameTest {
private static final String SRC_DIR = System.getProperty("test.src");
private static final String NONJAVA_NAMES_SRC = SRC_DIR + "/classes/";
private static final String NONJAVA_NAMES_CLASSES = System.getProperty("test.classes", ".");

This comment has been minimized.

@mlchung

mlchung Jan 22, 2021
Member

I was at first confused with NON_NAMES_CLASSES of what it means. For simplicity and clarity, better to rename these variables:
s/NONJAVA_NAMES_SRC/TEST_SRC/
s/NONJAVA_NAMES_CLASSES/TEST_CLASSES/

This comment has been minimized.

@mahendrachhipa

mahendrachhipa Jan 25, 2021
Author Contributor

Implemented in next patch

}

@AfterClass
public void deleteInvalidNameClasses() throws IOException {

This comment has been minimized.

@mlchung

mlchung Jan 22, 2021
Member

jtreg will do the clean up and this is not necessary.

This comment has been minimized.

@mahendrachhipa

mahendrachhipa Jan 25, 2021
Author Contributor

Removed in next patch.

Files.deleteIfExists(pkg2File);
Files.deleteIfExists(pkg2Dir);
Files.deleteIfExists(pkg1File);
Files.deleteIfExists(pkg1Dir);

This comment has been minimized.

@mlchung

mlchung Jan 22, 2021
Member

You can consider using jdk.test.lib.util.FileUtils test library to remove the entire directory.

This comment has been minimized.

@mahendrachhipa

mahendrachhipa Jan 25, 2021
Author Contributor

Thanks. Using FileUtils in next patch

Files.deleteIfExists(pkg1File);
Files.deleteIfExists(pkg1Dir);
Files.createDirectories(pkg2Dir);
} catch (IOException ex) {

This comment has been minimized.

@mlchung

mlchung Jan 22, 2021
Member

The test should fail when running into any error. Using the test library will do the retry

This comment has been minimized.

@mahendrachhipa

mahendrachhipa Jan 25, 2021
Author Contributor

Thanks, Corrected in next patch.

createAndWriteEnclosingClasses(enclosingPath, pkg1File, "pkg1");
createAndWriteEnclosingClasses(enclosingPath, pkg2File, "pkg1.pkg2");

String javacPath = JDKToolFinder.getJDKTool("javac");

This comment has been minimized.

@mlchung

mlchung Jan 22, 2021
Member

You can use jdk.test.lib.compiler.CompilerUtils test library to compile the file in process.

This comment has been minimized.

@mahendrachhipa

mahendrachhipa Jan 25, 2021
Author Contributor

I tried to use the CopmilerUtils to compile the file, But this utility is not able to the dependencies of the class. Example it is not able to find the common.TestMe class while try to compile the EnclosingClass.java.

This comment has been minimized.

@mlchung

mlchung Jan 26, 2021
Member

You need to add --source-path where it can find the dependent source files.


/*
* @test
* @bug 4992173 4992170

This comment has been minimized.

@mlchung

mlchung Jan 22, 2021
Member

this needs @modules jdk.compiler

This comment has been minimized.

@mahendrachhipa

mahendrachhipa Jan 25, 2021
Author Contributor

Thanks. Added in next patch

}
}

private void createAndWriteEnclosingClasses(final Path source, final Path target, final String packagePath) {

This comment has been minimized.

@mlchung

mlchung Jan 22, 2021
Member

s/packagePath/packageName/

no need to declare parameters as final

This comment has been minimized.

@mahendrachhipa

mahendrachhipa Jan 25, 2021
Author Contributor

Renamed the packagePath to packageName in next patch. To avoid the Checkstyle warning added the final key word with parameters.

String line;
while ((line = br.readLine()) != null) {
if (line.contains("canonical=\"EnclosingClass")) {
line = line.replaceAll("canonical=\"EnclosingClass", "canonical=\"" + packagePath + ".EnclosingClass");

This comment has been minimized.

@mlchung

mlchung Jan 22, 2021
Member

suggestion: declare var className = packageName + ".EnclosingClass"; and replace those occurrance.

This comment has been minimized.

@mahendrachhipa

mahendrachhipa Jan 25, 2021
Author Contributor

Thanks. Implemented the comment in next patch.

}
bw.println(line);
}
} catch (IOException ex) {

This comment has been minimized.

@mlchung

mlchung Jan 22, 2021
Member

Should not swallow the error and instead, let it propagate and the test should fail.

This comment has been minimized.

@mahendrachhipa

mahendrachhipa Jan 25, 2021
Author Contributor

Thanks. Corrected this in next patch.

@bchristi-git
Copy link
Member

@bchristi-git bchristi-git commented Jan 22, 2021

Can the new code be added to the existing NonJavaNames.java instead of adding a new NonJavaNameTest.java file?

* @test
* @bug 4952558
* @library /test/lib
* @build NonJavaNames

This comment has been minimized.

@mlchung

mlchung Jan 26, 2021
Member

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

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;

This comment has been minimized.

@mlchung

mlchung Jan 26, 2021
Member

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

public void createEnclosingClasses() throws Throwable {
Path enclosingPath = Paths.get(ENCLOSING_CLASS_SRC);
Path pkg1Dir = Paths.get(SRC_DIR + "/pkg1");
Path pkg2Dir = Paths.get(SRC_DIR+"/pkg1/pkg2");

This comment has been minimized.

@mlchung

mlchung Jan 26, 2021
Member

nit: space before and after + is missing

createAndWriteEnclosingClasses(enclosingPath, pkg1File, "pkg1");
createAndWriteEnclosingClasses(enclosingPath, pkg2File, "pkg1.pkg2");

String javacPath = JDKToolFinder.getJDKTool("javac");

This comment has been minimized.

@mlchung

mlchung Jan 26, 2021
Member

You need to add --source-path where it can find the dependent source files.

@BeforeClass
public void createEnclosingClasses() throws Throwable {
Path enclosingPath = Paths.get(ENCLOSING_CLASS_SRC);
Path pkg1Dir = Paths.get(SRC_DIR + "/pkg1");

This comment has been minimized.

@mlchung

mlchung Jan 26, 2021
Member

Alternatively:

    Path pkg1Dir = Paths.get(SRC_DIR, pkg1);
    Path pkg2Dir = Paths.get(SRC_DIR, pkg1, pkg2);
    Path pkg1File = pkg1Dir.resolve("EnclosingClass.java");
    Path pkg2File = pkg2Dir.resolve("EnclosingClass.java");

String javacPath = JDKToolFinder.getJDKTool("javac");
OutputAnalyzer outputAnalyzer = ProcessTools.executeCommand(javacPath, "-d", System.getProperty("test.classes", "."),
SRC_DIR + "/EnclosingClass.java", SRC_DIR + "/pkg1/EnclosingClass.java", SRC_DIR + "/pkg1/pkg2/EnclosingClass.java");

This comment has been minimized.

@mlchung

mlchung Jan 26, 2021
Member

File.separator is different on Unix and Windows. Either replace / with File.separator. Or you can use java.nio.file.Path and call Path::toString to get the string representation.

See above suggestion of declaring static final Path ENCLOSING_CLASS_SRC. So the above should use ENCLOSING_CLASS_SRC and pkg2File instead (calling toString)

String encName, String encNameExpected,
String simpleName, String simpleNameExpected,
String canonicalName, String canonicalNameExpected) {
private void check(final Class<?> c, final Class<?> enc,

This comment has been minimized.

@mlchung

mlchung Jan 26, 2021
Member

Is it from your IDE configurations? You can turn off Checkstyle warnings. This just adds noise.

}

@Test
public void testEnclosingClasses() throws Throwable {

This comment has been minimized.

@mlchung

mlchung Jan 26, 2021
Member

better to throw specific exceptions for example ReflectiveOperationException. Same for the @Test below.

String encName, String encNameExpected,
String simpleName, String simpleNameExpected,
String canonicalName, String canonicalNameExpected) {
private void check(final Class<?> c, final Class<?> enc,

This comment has been minimized.

@mlchung

mlchung Jan 26, 2021
Member

I also assume some of the formatting changes are changed by your IDE suggestion. Can you please revert the formatting changes (there are quite a few of them). This will help the reviewers.


@AfterClass
public void deleteEnclosingClasses() throws IOException {
Path pkg1Dir = Paths.get(SRC_DIR + "/pkg1");

This comment has been minimized.

@mlchung

mlchung Jan 26, 2021
Member

Suggested change
Path pkg1Dir = Paths.get(SRC_DIR + "/pkg1");
Path pkg1Dir = Paths.get(SRC_DIR, "pkg1");
Copy link
Member

@bchristi-git bchristi-git left a comment

I like keeping the changes within the existing .java files, thanks. I have a few more suggestions.

* @library /test/lib
* @modules jdk.compiler
* @build jdk.test.lib.process.* EnclosingClassTest
* @run testng/othervm EnclosingClassTest

This comment has been minimized.

@bchristi-git

bchristi-git Jan 26, 2021
Member

The test should still be run with -esa -ea

This comment has been minimized.

@mahendrachhipa

mahendrachhipa Jan 27, 2021
Author Contributor

Yes, while running from command line providing the -esa and -a option to run the tests.


@BeforeClass
public void createEnclosingClasses() throws Throwable {
Path enclosingPath = Paths.get(ENCLOSING_CLASS_SRC);

This comment has been minimized.

@bchristi-git

bchristi-git Jan 26, 2021
Member

The 'enclosingPath' Path could be the constant. Then ENCLOSING_CLASS_SRC is not needed.

@AfterClass
public void deleteEnclosingClasses() throws IOException {
Path pkg1Dir = Paths.get(SRC_DIR + "/pkg1");
FileUtils.deleteFileTreeWithRetry(pkg1Dir);
}
Comment on lines 122 to 126

This comment has been minimized.

@bchristi-git

bchristi-git Jan 26, 2021
Member

I'm not convinced the @AfterClass method is necessary. Pre-cleanup is already done on LL94-95. And occasionally, test-generated artifacts are helpful in post-mortem analysis.

This comment has been minimized.

@mahendrachhipa

mahendrachhipa Jan 27, 2021
Author Contributor

To keep the source code repo clean from temporary generated files, these files are removed after test. Test logs have the information about the generated files for the Postmortem analysis.

This comment has been minimized.

@bchristi-git

bchristi-git Feb 2, 2021
Member

Test files shouldn't be written into the repository (repos are sometimes tested on read-only filesystems). Files should be written under JTwork/scratch/.

expected + "'");
private void match(final String actual, final String expected) {
System.out.println("actual:" + actual + "expected:" + expected);
assert ((actual == null && expected == null) || actual.trim().equals(expected.trim()));

This comment has been minimized.

@bchristi-git

bchristi-git Jan 26, 2021
Member

Out of curiousity, why is the trim() now needed ?

This comment has been minimized.

@mahendrachhipa

mahendrachhipa Jan 27, 2021
Author Contributor

expected string having one space as prefixed in some class names incase of pkg1 and pkg2 packages. So trimmig before comparing with actual name:

Current package no space

nested class within top level class:
class EnclosingClass$Nested
is enclosed by: class EnclosingClass
has simple name: Nested' has canonical name: EnclosingClass.Nested'
actual:class EnclosingClassexpected:class EnclosingClass

pkg1.pkg2 package have expected class name with one space prefixed

nested class within top level class:
class pkg1.pkg2.EnclosingClass$Nested
is enclosed by: class pkg1.pkg2.EnclosingClass
has simple name: Nested' has canonical name: pkg1.pkg2.EnclosingClass.Nested'
actual:class pkg1.pkg2.EnclosingClassexpected: class pkg1.pkg2.EnclosingClass

pkg1 package have expected class name with one space prefixed

nested class within top level class:
class pkg1.EnclosingClass$Nested
is enclosed by: class pkg1.EnclosingClass
has simple name: Nested' has canonical name: pkg1.EnclosingClass.Nested'
actual:class pkg1.EnclosingClassexpected: class pkg1.EnclosingClass

check(c, encClass, "" + encClass, annotation.encl(), c.getSimpleName(), annotation.simple(),
c.getCanonicalName(), annotation.hasCanonical() ? annotation.canonical() : null);
Comment on lines 179 to 180

This comment has been minimized.

@bchristi-git

bchristi-git Jan 26, 2021
Member

This looks like an unneeded formatting change

check(array, array.getEnclosingClass(), "", "", array.getSimpleName(),
annotation.simple() + "[]", array.getCanonicalName(), annotation.hasCanonical()
? annotation.canonical() + "[]" : null);
Comment on lines 185 to 187

This comment has been minimized.

@bchristi-git

bchristi-git Jan 26, 2021
Member

This looks like an unneeded formatting change

System.err.println("Error in " +
tests.getClass().getName() +
"." + f.getName());
System.err.println("Error in " + tests.getClass().getName() + "." + f.getName());

This comment has been minimized.

@bchristi-git

bchristi-git Jan 26, 2021
Member

This looks like an unneeded formatting change


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

This comment has been minimized.

@efge

efge Jan 26, 2021

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

This comment has been minimized.

@mahendrachhipa

mahendrachhipa Jan 27, 2021
Author Contributor

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

@openjdk openjdk bot removed the rfr label Feb 1, 2021
@mlchung
mlchung approved these changes Feb 1, 2021
Copy link
Member

@mlchung mlchung left a comment

Thanks for the update. This is much cleaner.

}
@DataProvider(name = "badNonJavaClassNames")
Object[][] getBadNonJavaClassNames() {
return new Object[][] {{";"}, {"["}, {"."}};

This comment has been minimized.

@mlchung

mlchung Feb 1, 2021
Member

Nit: one line per test (i.e. make it multiple lines)

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

/*
* @test

This comment has been minimized.

@mlchung

mlchung Feb 1, 2021
Member

Nit: suggest to move this comment block to the top before all imports.

@openjdk
Copy link

@openjdk openjdk bot commented Feb 1, 2021

@mahendrachhipa This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8183372: Refactor java/lang/Class shell tests to java

Reviewed-by: bchristi, mchung

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 692 new commits pushed to the master branch:

  • 105d3e8: 8260861: TrustStoreDescriptor log the same value
  • 69189f8: 8256421: Add 2 HARICA roots to cacerts truststore
  • f546fd0: 8260902: CDS mapping errors should not lead to unconditional output
  • d7b1fc5: 8260707: java/lang/instrument/PremainClass/InheritAgent0100.java times out
  • 0093183: 8260368: [PPC64] GC interface needs enhancement to support GCs with load barriers
  • defcb04: 8260867: ProblemList java/awt/FullScreen/TranslucentWindow/TranslucentWindow.java on linux
  • a421bfa: 8259839: SystemDictionary exports too much implementation
  • 189b65b: 8260264: Move common os_ inline methods to a common posix source file
  • 288a4fe: 8260643: Remove parallel version handling in CardTableRS::younger_refs_in_space_iterate()
  • ddd2951: 8260571: Add PrintMetaspaceStatistics to print metaspace statistics upon VM exit
  • ... and 682 more: https://git.openjdk.java.net/jdk/compare/0201a33dd681dc731637fa3f9bf1d197610a9da4...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@bchristi-git, @mlchung) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@mlchung
mlchung approved these changes Feb 1, 2021
"]"
};
@Test(dataProvider = "goodNonJavaClassNames")
public void testGoodNonJavaClassNames(String name) throws Throwable {

This comment has been minimized.

@mlchung

mlchung Feb 1, 2021
Member

s/Throwable/ClassFileNotFoundException/

System.out.println("Testing bad class name ``" + name + "''");
try {
Class.forName(name);
} catch (Exception e) {

This comment has been minimized.

@mlchung

mlchung Feb 1, 2021
Member

s/Exception/ClassNotFoundException/

@mahendrachhipa
Copy link
Contributor Author

@mahendrachhipa mahendrachhipa commented Feb 1, 2021

/integrate

@openjdk openjdk bot added the sponsor label Feb 1, 2021
@openjdk
Copy link

@openjdk openjdk bot commented Feb 1, 2021

@mahendrachhipa
Your change (at version 320b305) is now ready to be sponsored by a Committer.

@mlchung
Copy link
Member

@mlchung mlchung commented Feb 1, 2021

I think Brent wants to review it. I will let Brent to sponsor you.

actual + "' matches expected `" +
expected + "'");
private void match(final String actual, final String expected) {
System.out.println("actual:" + actual + "expected:" + expected);

This comment has been minimized.

@bchristi-git

bchristi-git Feb 1, 2021
Member

It would be good to insert a space before expected:

expected + "'");
private void match(final String actual, final String expected) {
System.out.println("actual:" + actual + "expected:" + expected);
assert ((actual == null && expected == null) || actual.equals(expected.trim()));

This comment has been minimized.

@bchristi-git

bchristi-git Feb 1, 2021
Member

I think we need to figure out where the extra space is coming from, if the test worked before, but now fails without adding the trim().

Also, I think it would be better to throw an exception instead of using assert, so the test works with or without assertions being enabled.

This comment has been minimized.

@mlchung

mlchung Feb 1, 2021
Member

Good catch. I missed your comment about this change.

assert((actual == null && expected == null) || actual.equals(expected));
private void match(String actual, String expected) {
System.out.println("actual:" + actual + "expected:" + expected);
assert((actual == null && expected == null) || actual.equals(expected.trim()));

This comment has been minimized.

@bchristi-git

bchristi-git Feb 1, 2021
Member

I don't think this check is done, because assertions aren't enabled. This should be changed to explicitly throw an exception.

@bchristi-git
Copy link
Member

@bchristi-git bchristi-git commented Feb 1, 2021

(I think my comments may no have been visible because I didn't "submit" my review. Hopefully they are now.)

@openjdk openjdk bot removed the sponsor label Feb 2, 2021
@mahendrachhipa
Copy link
Contributor Author

@mahendrachhipa mahendrachhipa commented Feb 2, 2021

/integrate

@openjdk openjdk bot added the sponsor label Feb 2, 2021
@openjdk
Copy link

@openjdk openjdk bot commented Feb 2, 2021

@mahendrachhipa
Your change (at version 224a104) is now ready to be sponsored by a Committer.

@openjdk openjdk bot removed the sponsor label Feb 2, 2021
Copy link
Member

@bchristi-git bchristi-git left a comment

Looks good. Thanks for the changes.

@bchristi-git
Copy link
Member

@bchristi-git bchristi-git commented Feb 2, 2021

/sponsor

@openjdk
Copy link

@openjdk openjdk bot commented Feb 2, 2021

@bchristi-git The PR has been updated since the change author (@mahendrachhipa) issued the integrate command - the author must perform this command again.

@mahendrachhipa
Copy link
Contributor Author

@mahendrachhipa mahendrachhipa commented Feb 2, 2021

/integrate

@openjdk openjdk bot added the sponsor label Feb 2, 2021
@openjdk
Copy link

@openjdk openjdk bot commented Feb 2, 2021

@mahendrachhipa
Your change (at version 3c235ab) is now ready to be sponsored by a Committer.

@bchristi-git
Copy link
Member

@bchristi-git bchristi-git commented Feb 2, 2021

/sponsor

@openjdk openjdk bot closed this Feb 2, 2021
@openjdk
Copy link

@openjdk openjdk bot commented Feb 2, 2021

@bchristi-git @mahendrachhipa Since your change was applied there have been 692 commits pushed to the master branch:

  • 105d3e8: 8260861: TrustStoreDescriptor log the same value
  • 69189f8: 8256421: Add 2 HARICA roots to cacerts truststore
  • f546fd0: 8260902: CDS mapping errors should not lead to unconditional output
  • d7b1fc5: 8260707: java/lang/instrument/PremainClass/InheritAgent0100.java times out
  • 0093183: 8260368: [PPC64] GC interface needs enhancement to support GCs with load barriers
  • defcb04: 8260867: ProblemList java/awt/FullScreen/TranslucentWindow/TranslucentWindow.java on linux
  • a421bfa: 8259839: SystemDictionary exports too much implementation
  • 189b65b: 8260264: Move common os_ inline methods to a common posix source file
  • 288a4fe: 8260643: Remove parallel version handling in CardTableRS::younger_refs_in_space_iterate()
  • ddd2951: 8260571: Add PrintMetaspaceStatistics to print metaspace statistics upon VM exit
  • ... and 682 more: https://git.openjdk.java.net/jdk/compare/0201a33dd681dc731637fa3f9bf1d197610a9da4...master

Your commit was automatically rebased without conflicts.

Pushed as commit 6dc3c6d.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
4 participants