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

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 554
9d9f26857129
child 809
e63b1f8341ce
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

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

mercurial