test/tools/javac/nio/compileTest/CompileTest.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /**
aoqi@0 25 * @test
aoqi@0 26 * @bug 6906175 6915476 6915497 7006564
aoqi@0 27 * @summary Path-based JavaFileManager
aoqi@0 28 * @compile -g CompileTest.java HelloPathWorld.java
aoqi@0 29 * @run main CompileTest
aoqi@0 30 */
aoqi@0 31
aoqi@0 32 import java.io.*;
aoqi@0 33 import java.nio.file.*;
aoqi@0 34 import java.util.*;
aoqi@0 35 import java.util.jar.*;
aoqi@0 36 import javax.tools.*;
aoqi@0 37
aoqi@0 38 import com.sun.tools.javac.nio.*;
aoqi@0 39 import com.sun.tools.javac.util.Context;
aoqi@0 40 import java.nio.file.spi.FileSystemProvider;
aoqi@0 41
aoqi@0 42
aoqi@0 43 public class CompileTest {
aoqi@0 44 public static void main(String[] args) throws Exception {
aoqi@0 45 new CompileTest().run();
aoqi@0 46 }
aoqi@0 47
aoqi@0 48 public void run() throws Exception {
aoqi@0 49 File rtDir = new File("rt.dir");
aoqi@0 50 File javaHome = new File(System.getProperty("java.home"));
aoqi@0 51 if (javaHome.getName().equals("jre"))
aoqi@0 52 javaHome = javaHome.getParentFile();
aoqi@0 53 File rtJar = new File(new File(new File(javaHome, "jre"), "lib"), "rt.jar");
aoqi@0 54 expand(rtJar, rtDir);
aoqi@0 55
aoqi@0 56 String[] rtDir_opts = {
aoqi@0 57 "-bootclasspath", rtDir.toString(),
aoqi@0 58 "-classpath", "",
aoqi@0 59 "-sourcepath", "",
aoqi@0 60 "-extdirs", ""
aoqi@0 61 };
aoqi@0 62 test(rtDir_opts, "HelloPathWorld");
aoqi@0 63
aoqi@0 64 if (isJarFileSystemAvailable()) {
aoqi@0 65 String[] rtJar_opts = {
aoqi@0 66 "-bootclasspath", rtJar.toString(),
aoqi@0 67 "-classpath", "",
aoqi@0 68 "-sourcepath", "",
aoqi@0 69 "-extdirs", ""
aoqi@0 70 };
aoqi@0 71 test(rtJar_opts, "HelloPathWorld");
aoqi@0 72
aoqi@0 73 String[] default_opts = { };
aoqi@0 74 test(default_opts, "HelloPathWorld");
aoqi@0 75
aoqi@0 76 // finally, a non-trivial program
aoqi@0 77 test(default_opts, "CompileTest");
aoqi@0 78 } else
aoqi@0 79 System.err.println("jar file system not available: test skipped");
aoqi@0 80 }
aoqi@0 81
aoqi@0 82 void test(String[] opts, String className) throws Exception {
aoqi@0 83 count++;
aoqi@0 84 System.err.println("Test " + count + " " + Arrays.asList(opts) + " " + className);
aoqi@0 85 Path testSrcDir = Paths.get(System.getProperty("test.src"));
aoqi@0 86 Path testClassesDir = Paths.get(System.getProperty("test.classes"));
aoqi@0 87 Path classes = Files.createDirectory(Paths.get("classes." + count));
aoqi@0 88
aoqi@0 89 Context ctx = new Context();
aoqi@0 90 PathFileManager fm = new JavacPathFileManager(ctx, true, null);
aoqi@0 91 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
aoqi@0 92 List<String> options = new ArrayList<String>();
aoqi@0 93 options.addAll(Arrays.asList(opts));
aoqi@0 94 options.addAll(Arrays.asList(
aoqi@0 95 "-verbose", "-XDverboseCompilePolicy",
aoqi@0 96 "-d", classes.toString(),
aoqi@0 97 "-g"
aoqi@0 98 ));
aoqi@0 99 Iterable<? extends JavaFileObject> compilationUnits =
aoqi@0 100 fm.getJavaFileObjects(testSrcDir.resolve(className + ".java"));
aoqi@0 101 StringWriter sw = new StringWriter();
aoqi@0 102 PrintWriter out = new PrintWriter(sw);
aoqi@0 103 JavaCompiler.CompilationTask t =
aoqi@0 104 compiler.getTask(out, fm, null, options, null, compilationUnits);
aoqi@0 105 boolean ok = t.call();
aoqi@0 106 System.err.println(sw.toString());
aoqi@0 107 if (!ok) {
aoqi@0 108 throw new Exception("compilation failed");
aoqi@0 109 }
aoqi@0 110
aoqi@0 111 File expect = new File("classes." + count + "/" + className + ".class");
aoqi@0 112 if (!expect.exists())
aoqi@0 113 throw new Exception("expected file not found: " + expect);
aoqi@0 114 // Note that we explicitly specify -g for compiling both the actual class and the expected class.
aoqi@0 115 // This isolates the expected class from javac options that might be given to jtreg.
aoqi@0 116 long expectedSize = new File(testClassesDir.toString(), className + ".class").length();
aoqi@0 117 long actualSize = expect.length();
aoqi@0 118 if (expectedSize != actualSize)
aoqi@0 119 throw new Exception("wrong size found: " + actualSize + "; expected: " + expectedSize);
aoqi@0 120 }
aoqi@0 121
aoqi@0 122 boolean isJarFileSystemAvailable() {
aoqi@0 123 boolean result = false;
aoqi@0 124 for (FileSystemProvider fsp: FileSystemProvider.installedProviders()) {
aoqi@0 125 String scheme = fsp.getScheme();
aoqi@0 126 System.err.println("Provider: " + scheme + " " + fsp);
aoqi@0 127 if (scheme.equalsIgnoreCase("jar") || scheme.equalsIgnoreCase("zip"))
aoqi@0 128 result = true;
aoqi@0 129 }
aoqi@0 130 return result;
aoqi@0 131 }
aoqi@0 132
aoqi@0 133 void expand(File jar, File dir) throws IOException {
aoqi@0 134 JarFile jarFile = new JarFile(jar);
aoqi@0 135 try {
aoqi@0 136 Enumeration<JarEntry> entries = jarFile.entries();
aoqi@0 137 while (entries.hasMoreElements()) {
aoqi@0 138 JarEntry je = entries.nextElement();
aoqi@0 139 if (!je.isDirectory()) {
aoqi@0 140 copy(jarFile.getInputStream(je), new File(dir, je.getName()));
aoqi@0 141 }
aoqi@0 142 }
aoqi@0 143 } finally {
aoqi@0 144 jarFile.close();
aoqi@0 145 }
aoqi@0 146 }
aoqi@0 147
aoqi@0 148 void copy(InputStream in, File dest) throws IOException {
aoqi@0 149 dest.getParentFile().mkdirs();
aoqi@0 150 OutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
aoqi@0 151 try {
aoqi@0 152 byte[] data = new byte[8192];
aoqi@0 153 int n;
aoqi@0 154 while ((n = in.read(data, 0, data.length)) > 0)
aoqi@0 155 out.write(data, 0, n);
aoqi@0 156 } finally {
aoqi@0 157 out.close();
aoqi@0 158 in.close();
aoqi@0 159 }
aoqi@0 160 }
aoqi@0 161
aoqi@0 162 void error(String message) {
aoqi@0 163 System.err.println("Error: " + message);
aoqi@0 164 errors++;
aoqi@0 165 }
aoqi@0 166
aoqi@0 167 int errors;
aoqi@0 168 int count;
aoqi@0 169 }

mercurial