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

Mon, 11 Jan 2010 14:12:10 -0800

author
jjg
date
Mon, 11 Jan 2010 14:12:10 -0800
changeset 466
ca6bc36b2305
parent 450
4011f49b4af8
child 467
14a4c45ef734
permissions
-rw-r--r--

6915476: java.util.regex.PatternSyntaxException in com.sun.tools.javac.nio.PathFileObject
Reviewed-by: darcy

jjg@450 1 /*
jjg@450 2 * Copyright 2009 Sun Microsystems, Inc. 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 *
jjg@450 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
jjg@450 20 * CA 95054 USA or visit www.sun.com if you need additional information or
jjg@450 21 * have any questions.
jjg@450 22 */
jjg@450 23
jjg@450 24 /**
jjg@450 25 * @test
jjg@466 26 * @bug 6906175 6915476
jjg@466 27 * @summary Path-based JavaFileManager
jjg@450 28 * @compile 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@450 97 "-d", classes.toString()
jjg@450 98 ));
jjg@450 99 Iterable<? extends JavaFileObject> compilationUnits =
jjg@450 100 fm.getJavaFileObjects(testSrcDir.resolve(className + ".java"));
jjg@450 101 StringWriter sw = new StringWriter();
jjg@450 102 PrintWriter out = new PrintWriter(sw);
jjg@450 103 JavaCompiler.CompilationTask t =
jjg@450 104 compiler.getTask(out, fm, null, options, null, compilationUnits);
jjg@450 105 boolean ok = t.call();
jjg@450 106 System.err.println(sw.toString());
jjg@450 107 if (!ok) {
jjg@450 108 throw new Exception("compilation failed");
jjg@450 109 }
jjg@450 110
jjg@450 111 File expect = new File("classes." + count + "/" + className + ".class");
jjg@450 112 if (!expect.exists())
jjg@450 113 throw new Exception("expected file not found: " + expect);
jjg@450 114 long expectedSize = new File(testClassesDir.toString(), className + ".class").length();
jjg@450 115 long actualSize = expect.length();
jjg@450 116 if (expectedSize != actualSize)
jjg@450 117 throw new Exception("wrong size found: " + actualSize + "; expected: " + expectedSize);
jjg@450 118 }
jjg@450 119
jjg@450 120 boolean isJarFileSystemAvailable() {
jjg@450 121 boolean result = false;
jjg@450 122 for (FileSystemProvider fsp: FileSystemProvider.installedProviders()) {
jjg@450 123 String scheme = fsp.getScheme();
jjg@450 124 System.err.println("Provider: " + scheme + " " + fsp);
jjg@450 125 if (scheme.equalsIgnoreCase("jar") || scheme.equalsIgnoreCase("zip"))
jjg@450 126 result = true;
jjg@450 127 }
jjg@450 128 return result;
jjg@450 129 }
jjg@450 130
jjg@450 131 void expand(File jar, File dir) throws IOException {
jjg@450 132 JarFile jarFile = new JarFile(jar);
jjg@450 133 try {
jjg@450 134 Enumeration<JarEntry> entries = jarFile.entries();
jjg@450 135 while (entries.hasMoreElements()) {
jjg@450 136 JarEntry je = entries.nextElement();
jjg@450 137 if (!je.isDirectory()) {
jjg@450 138 copy(jarFile.getInputStream(je), new File(dir, je.getName()));
jjg@450 139 }
jjg@450 140 }
jjg@450 141 } finally {
jjg@450 142 jarFile.close();
jjg@450 143 }
jjg@450 144 }
jjg@450 145
jjg@450 146 void copy(InputStream in, File dest) throws IOException {
jjg@450 147 dest.getParentFile().mkdirs();
jjg@450 148 OutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
jjg@450 149 try {
jjg@450 150 byte[] data = new byte[8192];
jjg@450 151 int n;
jjg@450 152 while ((n = in.read(data, 0, data.length)) > 0)
jjg@450 153 out.write(data, 0, n);
jjg@450 154 } finally {
jjg@450 155 out.close();
jjg@450 156 in.close();
jjg@450 157 }
jjg@450 158 }
jjg@450 159
jjg@450 160 void error(String message) {
jjg@450 161 System.err.println("Error: " + message);
jjg@450 162 errors++;
jjg@450 163 }
jjg@450 164
jjg@450 165 int errors;
jjg@450 166 int count;
jjg@450 167 }

mercurial