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