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