1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/nio/compileTest/CompileTest.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,169 @@ 1.4 +/* 1.5 + * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/** 1.28 + * @test 1.29 + * @bug 6906175 6915476 6915497 7006564 1.30 + * @summary Path-based JavaFileManager 1.31 + * @compile -g CompileTest.java HelloPathWorld.java 1.32 + * @run main CompileTest 1.33 + */ 1.34 + 1.35 +import java.io.*; 1.36 +import java.nio.file.*; 1.37 +import java.util.*; 1.38 +import java.util.jar.*; 1.39 +import javax.tools.*; 1.40 + 1.41 +import com.sun.tools.javac.nio.*; 1.42 +import com.sun.tools.javac.util.Context; 1.43 +import java.nio.file.spi.FileSystemProvider; 1.44 + 1.45 + 1.46 +public class CompileTest { 1.47 + public static void main(String[] args) throws Exception { 1.48 + new CompileTest().run(); 1.49 + } 1.50 + 1.51 + public void run() throws Exception { 1.52 + File rtDir = new File("rt.dir"); 1.53 + File javaHome = new File(System.getProperty("java.home")); 1.54 + if (javaHome.getName().equals("jre")) 1.55 + javaHome = javaHome.getParentFile(); 1.56 + File rtJar = new File(new File(new File(javaHome, "jre"), "lib"), "rt.jar"); 1.57 + expand(rtJar, rtDir); 1.58 + 1.59 + String[] rtDir_opts = { 1.60 + "-bootclasspath", rtDir.toString(), 1.61 + "-classpath", "", 1.62 + "-sourcepath", "", 1.63 + "-extdirs", "" 1.64 + }; 1.65 + test(rtDir_opts, "HelloPathWorld"); 1.66 + 1.67 + if (isJarFileSystemAvailable()) { 1.68 + String[] rtJar_opts = { 1.69 + "-bootclasspath", rtJar.toString(), 1.70 + "-classpath", "", 1.71 + "-sourcepath", "", 1.72 + "-extdirs", "" 1.73 + }; 1.74 + test(rtJar_opts, "HelloPathWorld"); 1.75 + 1.76 + String[] default_opts = { }; 1.77 + test(default_opts, "HelloPathWorld"); 1.78 + 1.79 + // finally, a non-trivial program 1.80 + test(default_opts, "CompileTest"); 1.81 + } else 1.82 + System.err.println("jar file system not available: test skipped"); 1.83 + } 1.84 + 1.85 + void test(String[] opts, String className) throws Exception { 1.86 + count++; 1.87 + System.err.println("Test " + count + " " + Arrays.asList(opts) + " " + className); 1.88 + Path testSrcDir = Paths.get(System.getProperty("test.src")); 1.89 + Path testClassesDir = Paths.get(System.getProperty("test.classes")); 1.90 + Path classes = Files.createDirectory(Paths.get("classes." + count)); 1.91 + 1.92 + Context ctx = new Context(); 1.93 + PathFileManager fm = new JavacPathFileManager(ctx, true, null); 1.94 + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 1.95 + List<String> options = new ArrayList<String>(); 1.96 + options.addAll(Arrays.asList(opts)); 1.97 + options.addAll(Arrays.asList( 1.98 + "-verbose", "-XDverboseCompilePolicy", 1.99 + "-d", classes.toString(), 1.100 + "-g" 1.101 + )); 1.102 + Iterable<? extends JavaFileObject> compilationUnits = 1.103 + fm.getJavaFileObjects(testSrcDir.resolve(className + ".java")); 1.104 + StringWriter sw = new StringWriter(); 1.105 + PrintWriter out = new PrintWriter(sw); 1.106 + JavaCompiler.CompilationTask t = 1.107 + compiler.getTask(out, fm, null, options, null, compilationUnits); 1.108 + boolean ok = t.call(); 1.109 + System.err.println(sw.toString()); 1.110 + if (!ok) { 1.111 + throw new Exception("compilation failed"); 1.112 + } 1.113 + 1.114 + File expect = new File("classes." + count + "/" + className + ".class"); 1.115 + if (!expect.exists()) 1.116 + throw new Exception("expected file not found: " + expect); 1.117 + // Note that we explicitly specify -g for compiling both the actual class and the expected class. 1.118 + // This isolates the expected class from javac options that might be given to jtreg. 1.119 + long expectedSize = new File(testClassesDir.toString(), className + ".class").length(); 1.120 + long actualSize = expect.length(); 1.121 + if (expectedSize != actualSize) 1.122 + throw new Exception("wrong size found: " + actualSize + "; expected: " + expectedSize); 1.123 + } 1.124 + 1.125 + boolean isJarFileSystemAvailable() { 1.126 + boolean result = false; 1.127 + for (FileSystemProvider fsp: FileSystemProvider.installedProviders()) { 1.128 + String scheme = fsp.getScheme(); 1.129 + System.err.println("Provider: " + scheme + " " + fsp); 1.130 + if (scheme.equalsIgnoreCase("jar") || scheme.equalsIgnoreCase("zip")) 1.131 + result = true; 1.132 + } 1.133 + return result; 1.134 + } 1.135 + 1.136 + void expand(File jar, File dir) throws IOException { 1.137 + JarFile jarFile = new JarFile(jar); 1.138 + try { 1.139 + Enumeration<JarEntry> entries = jarFile.entries(); 1.140 + while (entries.hasMoreElements()) { 1.141 + JarEntry je = entries.nextElement(); 1.142 + if (!je.isDirectory()) { 1.143 + copy(jarFile.getInputStream(je), new File(dir, je.getName())); 1.144 + } 1.145 + } 1.146 + } finally { 1.147 + jarFile.close(); 1.148 + } 1.149 + } 1.150 + 1.151 + void copy(InputStream in, File dest) throws IOException { 1.152 + dest.getParentFile().mkdirs(); 1.153 + OutputStream out = new BufferedOutputStream(new FileOutputStream(dest)); 1.154 + try { 1.155 + byte[] data = new byte[8192]; 1.156 + int n; 1.157 + while ((n = in.read(data, 0, data.length)) > 0) 1.158 + out.write(data, 0, n); 1.159 + } finally { 1.160 + out.close(); 1.161 + in.close(); 1.162 + } 1.163 + } 1.164 + 1.165 + void error(String message) { 1.166 + System.err.println("Error: " + message); 1.167 + errors++; 1.168 + } 1.169 + 1.170 + int errors; 1.171 + int count; 1.172 +}