void test(String[] opts, String className) throws Exception { count++; System.err.println("Test " + count + " " + Arrays.asList(opts) + " " + className); Path testSrcDir = Paths.get(System.getProperty("test.src")); Path testClassesDir = Paths.get(System.getProperty("test.classes")); Path classes = Paths.get("classes." + count); classes.createDirectory(); Context ctx = new Context(); PathFileManager fm = new JavacPathFileManager(ctx, true, null); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); List<String> options = new ArrayList<String>(); options.addAll(Arrays.asList(opts)); options.addAll(Arrays.asList("-verbose", "-XDverboseCompilePolicy", "-d", classes.toString())); Iterable<? extends JavaFileObject> compilationUnits = fm.getJavaFileObjects(testSrcDir.resolve(className + ".java")); StringWriter sw = new StringWriter(); PrintWriter out = new PrintWriter(sw); JavaCompiler.CompilationTask t = compiler.getTask(out, fm, null, options, null, compilationUnits); boolean ok = t.call(); System.err.println(sw.toString()); if (!ok) { throw new Exception("compilation failed"); } File expect = new File("classes." + count + "/" + className + ".class"); if (!expect.exists()) throw new Exception("expected file not found: " + expect); long expectedSize = new File(testClassesDir.toString(), className + ".class").length(); long actualSize = expect.length(); if (expectedSize != actualSize) throw new Exception("wrong size found: " + actualSize + "; expected: " + expectedSize); }
public void run() throws Exception { File rtDir = new File("rt.dir"); File javaHome = new File(System.getProperty("java.home")); if (javaHome.getName().equals("jre")) javaHome = javaHome.getParentFile(); File rtJar = new File(new File(new File(javaHome, "jre"), "lib"), "rt.jar"); expand(rtJar, rtDir); String[] rtDir_opts = { "-bootclasspath", rtDir.toString(), "-classpath", "", "-sourcepath", "", "-extdirs", "" }; test(rtDir_opts, "HelloPathWorld"); if (isJarFileSystemAvailable()) { String[] rtJar_opts = { "-bootclasspath", rtJar.toString(), "-classpath", "", "-sourcepath", "", "-extdirs", "" }; test(rtJar_opts, "HelloPathWorld"); String[] default_opts = {}; test(default_opts, "HelloPathWorld"); // finally, a non-trivial program test(default_opts, "CompileTest"); } else System.err.println("jar file system not available: test skipped"); }