aoqi@0: /* aoqi@0: * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /** aoqi@0: * @test aoqi@0: * @bug 6906175 6915476 6915497 7006564 aoqi@0: * @summary Path-based JavaFileManager aoqi@0: * @compile -g CompileTest.java HelloPathWorld.java aoqi@0: * @run main CompileTest aoqi@0: */ aoqi@0: aoqi@0: import java.io.*; aoqi@0: import java.nio.file.*; aoqi@0: import java.util.*; aoqi@0: import java.util.jar.*; aoqi@0: import javax.tools.*; aoqi@0: aoqi@0: import com.sun.tools.javac.nio.*; aoqi@0: import com.sun.tools.javac.util.Context; aoqi@0: import java.nio.file.spi.FileSystemProvider; aoqi@0: aoqi@0: aoqi@0: public class CompileTest { aoqi@0: public static void main(String[] args) throws Exception { aoqi@0: new CompileTest().run(); aoqi@0: } aoqi@0: aoqi@0: public void run() throws Exception { aoqi@0: File rtDir = new File("rt.dir"); aoqi@0: File javaHome = new File(System.getProperty("java.home")); aoqi@0: if (javaHome.getName().equals("jre")) aoqi@0: javaHome = javaHome.getParentFile(); aoqi@0: File rtJar = new File(new File(new File(javaHome, "jre"), "lib"), "rt.jar"); aoqi@0: expand(rtJar, rtDir); aoqi@0: aoqi@0: String[] rtDir_opts = { aoqi@0: "-bootclasspath", rtDir.toString(), aoqi@0: "-classpath", "", aoqi@0: "-sourcepath", "", aoqi@0: "-extdirs", "" aoqi@0: }; aoqi@0: test(rtDir_opts, "HelloPathWorld"); aoqi@0: aoqi@0: if (isJarFileSystemAvailable()) { aoqi@0: String[] rtJar_opts = { aoqi@0: "-bootclasspath", rtJar.toString(), aoqi@0: "-classpath", "", aoqi@0: "-sourcepath", "", aoqi@0: "-extdirs", "" aoqi@0: }; aoqi@0: test(rtJar_opts, "HelloPathWorld"); aoqi@0: aoqi@0: String[] default_opts = { }; aoqi@0: test(default_opts, "HelloPathWorld"); aoqi@0: aoqi@0: // finally, a non-trivial program aoqi@0: test(default_opts, "CompileTest"); aoqi@0: } else aoqi@0: System.err.println("jar file system not available: test skipped"); aoqi@0: } aoqi@0: aoqi@0: void test(String[] opts, String className) throws Exception { aoqi@0: count++; aoqi@0: System.err.println("Test " + count + " " + Arrays.asList(opts) + " " + className); aoqi@0: Path testSrcDir = Paths.get(System.getProperty("test.src")); aoqi@0: Path testClassesDir = Paths.get(System.getProperty("test.classes")); aoqi@0: Path classes = Files.createDirectory(Paths.get("classes." + count)); aoqi@0: aoqi@0: Context ctx = new Context(); aoqi@0: PathFileManager fm = new JavacPathFileManager(ctx, true, null); aoqi@0: JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); aoqi@0: List options = new ArrayList(); aoqi@0: options.addAll(Arrays.asList(opts)); aoqi@0: options.addAll(Arrays.asList( aoqi@0: "-verbose", "-XDverboseCompilePolicy", aoqi@0: "-d", classes.toString(), aoqi@0: "-g" aoqi@0: )); aoqi@0: Iterable compilationUnits = aoqi@0: fm.getJavaFileObjects(testSrcDir.resolve(className + ".java")); aoqi@0: StringWriter sw = new StringWriter(); aoqi@0: PrintWriter out = new PrintWriter(sw); aoqi@0: JavaCompiler.CompilationTask t = aoqi@0: compiler.getTask(out, fm, null, options, null, compilationUnits); aoqi@0: boolean ok = t.call(); aoqi@0: System.err.println(sw.toString()); aoqi@0: if (!ok) { aoqi@0: throw new Exception("compilation failed"); aoqi@0: } aoqi@0: aoqi@0: File expect = new File("classes." + count + "/" + className + ".class"); aoqi@0: if (!expect.exists()) aoqi@0: throw new Exception("expected file not found: " + expect); aoqi@0: // Note that we explicitly specify -g for compiling both the actual class and the expected class. aoqi@0: // This isolates the expected class from javac options that might be given to jtreg. aoqi@0: long expectedSize = new File(testClassesDir.toString(), className + ".class").length(); aoqi@0: long actualSize = expect.length(); aoqi@0: if (expectedSize != actualSize) aoqi@0: throw new Exception("wrong size found: " + actualSize + "; expected: " + expectedSize); aoqi@0: } aoqi@0: aoqi@0: boolean isJarFileSystemAvailable() { aoqi@0: boolean result = false; aoqi@0: for (FileSystemProvider fsp: FileSystemProvider.installedProviders()) { aoqi@0: String scheme = fsp.getScheme(); aoqi@0: System.err.println("Provider: " + scheme + " " + fsp); aoqi@0: if (scheme.equalsIgnoreCase("jar") || scheme.equalsIgnoreCase("zip")) aoqi@0: result = true; aoqi@0: } aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: void expand(File jar, File dir) throws IOException { aoqi@0: JarFile jarFile = new JarFile(jar); aoqi@0: try { aoqi@0: Enumeration entries = jarFile.entries(); aoqi@0: while (entries.hasMoreElements()) { aoqi@0: JarEntry je = entries.nextElement(); aoqi@0: if (!je.isDirectory()) { aoqi@0: copy(jarFile.getInputStream(je), new File(dir, je.getName())); aoqi@0: } aoqi@0: } aoqi@0: } finally { aoqi@0: jarFile.close(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void copy(InputStream in, File dest) throws IOException { aoqi@0: dest.getParentFile().mkdirs(); aoqi@0: OutputStream out = new BufferedOutputStream(new FileOutputStream(dest)); aoqi@0: try { aoqi@0: byte[] data = new byte[8192]; aoqi@0: int n; aoqi@0: while ((n = in.read(data, 0, data.length)) > 0) aoqi@0: out.write(data, 0, n); aoqi@0: } finally { aoqi@0: out.close(); aoqi@0: in.close(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void error(String message) { aoqi@0: System.err.println("Error: " + message); aoqi@0: errors++; aoqi@0: } aoqi@0: aoqi@0: int errors; aoqi@0: int count; aoqi@0: }