8081472: Add a mode to the tests for class-file attributes which dumps in-memory sources to disk
authoraeremeev
Mon, 28 Sep 2015 13:23:35 +0300
changeset 32908 bef9744cf56f
parent 32803 51b2db2fa04c
child 32909 989fef2663d2
8081472: Add a mode to the tests for class-file attributes which dumps in-memory sources to disk Reviewed-by: ksrini
langtools/test/tools/javac/classfiles/attributes/LineNumberTable/LineNumberTestBase.java
langtools/test/tools/javac/classfiles/attributes/annotations/AnnotationsTestBase.java
langtools/test/tools/javac/classfiles/attributes/innerclasses/InnerClassesTestBase.java
langtools/test/tools/javac/classfiles/attributes/lib/TestBase.java
--- a/langtools/test/tools/javac/classfiles/attributes/LineNumberTable/LineNumberTestBase.java	Wed Jul 05 20:51:37 2017 +0200
+++ b/langtools/test/tools/javac/classfiles/attributes/LineNumberTable/LineNumberTestBase.java	Mon Sep 28 13:23:35 2015 +0300
@@ -23,6 +23,7 @@
 
 import com.sun.tools.classfile.*;
 
+import java.nio.file.Paths;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -62,6 +63,7 @@
         boolean failed = false;
         for (TestCase testCase : testCases) {
             try {
+                writeToFileIfEnabled(Paths.get(testCase.getName() + ".java"), testCase.src);
                 Set<Integer> coveredLines = new HashSet<>();
                 for (JavaFileObject file : compile(testCase.src).getClasses().values()) {
                     ClassFile classFile = ClassFile.read(file.openInputStream());
--- a/langtools/test/tools/javac/classfiles/attributes/annotations/AnnotationsTestBase.java	Wed Jul 05 20:51:37 2017 +0200
+++ b/langtools/test/tools/javac/classfiles/attributes/annotations/AnnotationsTestBase.java	Mon Sep 28 13:23:35 2015 +0300
@@ -218,7 +218,7 @@
                 String source = testCase.generateSource();
                 Path sourceFile = Paths.get(getClass().getSimpleName() + i + ".java");
                 addTestCase(sourceFile.toAbsolutePath().toString());
-                writeToFile(sourceFile, source);
+                writeToFileIfEnabled(sourceFile, source);
                 echo("Testing: " + sourceFile.toString());
                 try {
                     test(testCase, compile(source).getClasses());
--- a/langtools/test/tools/javac/classfiles/attributes/innerclasses/InnerClassesTestBase.java	Wed Jul 05 20:51:37 2017 +0200
+++ b/langtools/test/tools/javac/classfiles/attributes/innerclasses/InnerClassesTestBase.java	Mon Sep 28 13:23:35 2015 +0300
@@ -26,8 +26,10 @@
 import com.sun.tools.classfile.InnerClasses_attribute;
 import com.sun.tools.classfile.InnerClasses_attribute.Info;
 
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -87,8 +89,13 @@
      */
     public void test(String classToTest, String...skipClasses) throws TestFailedException {
         try {
-            for (TestCase test : generateTestCases()) {
-                addTestCase(test.getSource());
+            String testName = getClass().getName();
+            List<TestCase> testCases = generateTestCases();
+            for (int i = 0; i < testCases.size(); ++i) {
+                TestCase test = testCases.get(i);
+                String testCaseName = testName + i + ".java";
+                addTestCase(testCaseName);
+                writeToFileIfEnabled(Paths.get(testCaseName), test.getSource());
                 test(classToTest, test, skipClasses);
             }
         } catch (Exception e) {
@@ -330,7 +337,7 @@
                     list.add(Arrays.asList(access, mod1, mod2));
                 }
                 if (mod1 == Modifier.EMPTY) {
-                    list.add(Arrays.asList(access));
+                    list.add(Collections.singletonList(access));
                 }
             }
         }
@@ -413,7 +420,7 @@
 
         private final String classType;
 
-        private ClassType(String clazz) {
+        ClassType(String clazz) {
             this.classType = clazz;
         }
 
@@ -435,7 +442,7 @@
 
         private final String str;
 
-        private Modifier(String str) {
+        Modifier(String str) {
             this.str = str;
         }
 
--- a/langtools/test/tools/javac/classfiles/attributes/lib/TestBase.java	Wed Jul 05 20:51:37 2017 +0200
+++ b/langtools/test/tools/javac/classfiles/attributes/lib/TestBase.java	Mon Sep 28 13:23:35 2015 +0300
@@ -44,6 +44,7 @@
 public class TestBase {
 
     public static final String LINE_SEPARATOR = System.lineSeparator();
+    public static final boolean isDumpOfSourceEnabled = Boolean.getBoolean("dump.src");
 
     private <S> InMemoryFileManager compile(
             List<String> options,
@@ -176,7 +177,9 @@
      * @throws ConstantPoolException if constant pool error occurs
      */
     public ClassFile readClassFile(File file) throws IOException, ConstantPoolException {
-        return readClassFile(new FileInputStream(file));
+        try (InputStream is = new FileInputStream(file)) {
+            return readClassFile(is);
+        }
     }
 
     public void assertEquals(Object actual, Object expected, String message) {
@@ -215,6 +218,14 @@
         }
     }
 
+    public void writeToFileIfEnabled(Path path, String source) throws IOException {
+        if (isDumpOfSourceEnabled) {
+            writeToFile(path, source);
+        } else {
+            System.err.println("Source dumping disabled. To enable, run the test with '-Ddump.src=true'");
+        }
+    }
+
     public File getSourceDir() {
         return new File(System.getProperty("test.src", "."));
     }