1 package com.github.javaparser.manual; 2 3 import com.github.javaparser.ParserConfiguration; 4 import com.github.javaparser.Problem; 5 import com.github.javaparser.utils.CodeGenerationUtils; 6 import com.github.javaparser.utils.Log; 7 import com.github.javaparser.utils.SourceRoot; 8 import com.github.javaparser.utils.SourceZip; 9 import org.junit.Test; 10 11 import java.io.BufferedWriter; 12 import java.io.IOException; 13 import java.net.URL; 14 import java.nio.file.Files; 15 import java.nio.file.Path; 16 import java.nio.file.Paths; 17 import java.util.List; 18 import java.util.Map; 19 import java.util.TreeMap; 20 21 import static com.github.javaparser.ParserConfiguration.LanguageLevel.JAVA_10; 22 import static com.github.javaparser.ParserConfiguration.LanguageLevel.JAVA_9; 23 import static com.github.javaparser.utils.CodeGenerationUtils.f; 24 import static com.github.javaparser.utils.SourceRoot.Callback.Result.DONT_SAVE; 25 import static com.github.javaparser.utils.TestUtils.download; 26 import static com.github.javaparser.utils.TestUtils.temporaryDirectory; 27 import static java.util.Comparator.comparing; 28 29 public class BulkParseTest { 30 /** 31 * Running this will download a version of the OpenJDK, unzip it, and parse it. If it throws a stack overflow 32 * exception, increase the JVM's stack size. 33 */ 34 public static void main(String[] args) throws IOException { 35 Log.setAdapter(new Log.StandardOutStandardErrorAdapter()); 36 // This contains all kinds of test cases so it will lead to a lot of errors: 37 new BulkParseTest().parseOpenJdkLangToolsRepository(); 38 // This contains the JDK source code, so it should have zero errors: 39 new BulkParseTest().parseJdkSrcZip(); 40 } 41 42 private void parseOpenJdkLangToolsRepository() throws IOException { 43 Path workdir = CodeGenerationUtils.mavenModuleRoot(BulkParseTest.class).resolve(Paths.get(temporaryDirectory(), "javaparser_bulkparsetest")); 44 workdir.toFile().mkdirs(); 45 Path openJdkZipPath = workdir.resolve("langtools.zip"); 46 if (Files.notExists(openJdkZipPath)) { 47 Log.info("Downloading JDK langtools"); 48 /* Found by choosing a tag here: http://hg.openjdk.java.net/jdk9/jdk9/langtools/tags 49 then copying the "zip" link to the line below: */ 50 download(new URL("http://hg.openjdk.java.net/jdk10/jdk10/langtools/archive/19293ea3999f.zip"), openJdkZipPath); 51 } 52 bulkTest(new SourceZip(openJdkZipPath), "openjdk_src_repo_test_results.txt", new ParserConfiguration().setLanguageLevel(JAVA_10)); 53 } 54 55 private void parseJdkSrcZip() throws IOException { 56 // This is where Ubuntu stores the contents of package openjdk-8-src 57 Path path = Paths.get("/usr/lib/jvm/openjdk-9/src.zip"); 58 bulkTest(new SourceZip(path), "openjdk_src_zip_test_results.txt", new ParserConfiguration().setLanguageLevel(JAVA_9)); 59 } 60 61 @Test 62 public void parseOwnSourceCode() throws IOException { 63 bulkTest( 64 new SourceRoot(CodeGenerationUtils.mavenModuleRoot(BulkParseTest.class).resolve("..")), 65 "javaparser_test_results.txt", 66 new ParserConfiguration().setLanguageLevel(JAVA_9)); 67 } 68 69 public void bulkTest(SourceRoot sourceRoot, String testResultsFileName, ParserConfiguration configuration) throws IOException { 70 sourceRoot.setParserConfiguration(configuration); 71 TreeMap<Path, List<Problem>> results = new TreeMap<>(comparing(o -> o.toString().toLowerCase())); 72 sourceRoot.parseParallelized((localPath, absolutePath, result) -> { 73 if (!localPath.toString().contains("target")) { 74 if (!result.isSuccessful()) { 75 results.put(localPath, result.getProblems()); 76 } 77 } 78 return DONT_SAVE; 79 }); 80 writeResults(results, testResultsFileName); 81 } 82 83 public void bulkTest(SourceZip sourceRoot, String testResultsFileName, ParserConfiguration configuration) throws IOException { 84 sourceRoot.setParserConfiguration(configuration); 85 TreeMap<Path, List<Problem>> results = new TreeMap<>(comparing(o -> o.toString().toLowerCase())); 86 sourceRoot.parse((path, result) -> { 87 if (!path.toString().contains("target")) { 88 if (!result.isSuccessful()) { 89 results.put(path, result.getProblems()); 90 } 91 } 92 }); 93 writeResults(results, testResultsFileName); 94 } 95 96 private void writeResults(TreeMap<Path, List<Problem>> results, String testResultsFileName) throws IOException { 97 Log.info("Writing results..."); 98 99 Path testResults = CodeGenerationUtils.mavenModuleRoot(BulkParseTest.class).resolve(Paths.get("..", "javaparser-testing", "src", "test", "resources", "com", "github", "javaparser", "bulk_test_results")).normalize(); 100 testResults.toFile().mkdirs(); 101 testResults = testResults.resolve(testResultsFileName); 102 103 int problemTotal = 0; 104 try (BufferedWriter writer = Files.newBufferedWriter(testResults)) { 105 for (Map.Entry<Path, List<Problem>> file : results.entrySet()) { 106 writer.write(file.getKey().toString().replace("\\", "/")); 107 writer.newLine(); 108 for (Problem problem : file.getValue()) { 109 writer.write(problem.getVerboseMessage()); 110 writer.newLine(); 111 problemTotal++; 112 } 113 writer.newLine(); 114 } 115 writer.write(f("%s problems in %s files", problemTotal, results.size())); 116 } 117 118 Log.info("Results are in %s", testResults); 119 } 120 }