author | jjg |
Mon, 11 Jan 2010 14:12:10 -0800 | |
changeset 4701 | 39e13c0d8d75 |
parent 4548 | bc0d5b3c3b2d |
child 4702 | bb3925c7b58a |
permissions | -rw-r--r-- |
4548 | 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 |
|
4701
39e13c0d8d75
6915476: java.util.regex.PatternSyntaxException in com.sun.tools.javac.nio.PathFileObject
jjg
parents:
4548
diff
changeset
|
26 |
* @bug 6906175 6915476 |
39e13c0d8d75
6915476: java.util.regex.PatternSyntaxException in com.sun.tools.javac.nio.PathFileObject
jjg
parents:
4548
diff
changeset
|
27 |
* @summary Path-based JavaFileManager |
4548 | 28 |
* @compile HelloPathWorld.java |
29 |
* @run main CompileTest |
|
30 |
*/ |
|
31 |
||
32 |
import java.io.*; |
|
33 |
import java.nio.file.*; |
|
34 |
import java.util.*; |
|
35 |
import java.util.jar.*; |
|
36 |
import javax.tools.*; |
|
37 |
||
38 |
import com.sun.tools.javac.nio.*; |
|
39 |
import com.sun.tools.javac.util.Context; |
|
40 |
import java.nio.file.spi.FileSystemProvider; |
|
41 |
||
42 |
||
43 |
public class CompileTest { |
|
44 |
public static void main(String[] args) throws Exception { |
|
45 |
new CompileTest().run(); |
|
46 |
} |
|
47 |
||
48 |
public void run() throws Exception { |
|
49 |
File rtDir = new File("rt.dir"); |
|
50 |
File javaHome = new File(System.getProperty("java.home")); |
|
51 |
if (javaHome.getName().equals("jre")) |
|
52 |
javaHome = javaHome.getParentFile(); |
|
53 |
File rtJar = new File(new File(new File(javaHome, "jre"), "lib"), "rt.jar"); |
|
54 |
expand(rtJar, rtDir); |
|
55 |
||
56 |
String[] rtDir_opts = { |
|
57 |
"-bootclasspath", rtDir.toString(), |
|
58 |
"-classpath", "", |
|
59 |
"-sourcepath", "", |
|
60 |
"-extdirs", "" |
|
61 |
}; |
|
62 |
test(rtDir_opts, "HelloPathWorld"); |
|
63 |
||
64 |
if (isJarFileSystemAvailable()) { |
|
65 |
String[] rtJar_opts = { |
|
66 |
"-bootclasspath", rtJar.toString(), |
|
67 |
"-classpath", "", |
|
68 |
"-sourcepath", "", |
|
69 |
"-extdirs", "" |
|
70 |
}; |
|
71 |
test(rtJar_opts, "HelloPathWorld"); |
|
72 |
||
73 |
String[] default_opts = { }; |
|
74 |
test(default_opts, "HelloPathWorld"); |
|
75 |
||
76 |
// finally, a non-trivial program |
|
77 |
test(default_opts, "CompileTest"); |
|
78 |
} else |
|
79 |
System.err.println("jar file system not available: test skipped"); |
|
80 |
} |
|
81 |
||
82 |
void test(String[] opts, String className) throws Exception { |
|
83 |
count++; |
|
84 |
System.err.println("Test " + count + " " + Arrays.asList(opts) + " " + className); |
|
85 |
Path testSrcDir = Paths.get(System.getProperty("test.src")); |
|
86 |
Path testClassesDir = Paths.get(System.getProperty("test.classes")); |
|
87 |
Path classes = Paths.get("classes." + count); |
|
88 |
classes.createDirectory(); |
|
89 |
||
90 |
Context ctx = new Context(); |
|
91 |
PathFileManager fm = new JavacPathFileManager(ctx, true, null); |
|
92 |
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
|
93 |
List<String> options = new ArrayList<String>(); |
|
94 |
options.addAll(Arrays.asList(opts)); |
|
95 |
options.addAll(Arrays.asList( |
|
96 |
"-verbose", "-XDverboseCompilePolicy", |
|
97 |
"-d", classes.toString() |
|
98 |
)); |
|
99 |
Iterable<? extends JavaFileObject> compilationUnits = |
|
100 |
fm.getJavaFileObjects(testSrcDir.resolve(className + ".java")); |
|
101 |
StringWriter sw = new StringWriter(); |
|
102 |
PrintWriter out = new PrintWriter(sw); |
|
103 |
JavaCompiler.CompilationTask t = |
|
104 |
compiler.getTask(out, fm, null, options, null, compilationUnits); |
|
105 |
boolean ok = t.call(); |
|
106 |
System.err.println(sw.toString()); |
|
107 |
if (!ok) { |
|
108 |
throw new Exception("compilation failed"); |
|
109 |
} |
|
110 |
||
111 |
File expect = new File("classes." + count + "/" + className + ".class"); |
|
112 |
if (!expect.exists()) |
|
113 |
throw new Exception("expected file not found: " + expect); |
|
114 |
long expectedSize = new File(testClassesDir.toString(), className + ".class").length(); |
|
115 |
long actualSize = expect.length(); |
|
116 |
if (expectedSize != actualSize) |
|
117 |
throw new Exception("wrong size found: " + actualSize + "; expected: " + expectedSize); |
|
118 |
} |
|
119 |
||
120 |
boolean isJarFileSystemAvailable() { |
|
121 |
boolean result = false; |
|
122 |
for (FileSystemProvider fsp: FileSystemProvider.installedProviders()) { |
|
123 |
String scheme = fsp.getScheme(); |
|
124 |
System.err.println("Provider: " + scheme + " " + fsp); |
|
125 |
if (scheme.equalsIgnoreCase("jar") || scheme.equalsIgnoreCase("zip")) |
|
126 |
result = true; |
|
127 |
} |
|
128 |
return result; |
|
129 |
} |
|
130 |
||
131 |
void expand(File jar, File dir) throws IOException { |
|
132 |
JarFile jarFile = new JarFile(jar); |
|
133 |
try { |
|
134 |
Enumeration<JarEntry> entries = jarFile.entries(); |
|
135 |
while (entries.hasMoreElements()) { |
|
136 |
JarEntry je = entries.nextElement(); |
|
137 |
if (!je.isDirectory()) { |
|
138 |
copy(jarFile.getInputStream(je), new File(dir, je.getName())); |
|
139 |
} |
|
140 |
} |
|
141 |
} finally { |
|
142 |
jarFile.close(); |
|
143 |
} |
|
144 |
} |
|
145 |
||
146 |
void copy(InputStream in, File dest) throws IOException { |
|
147 |
dest.getParentFile().mkdirs(); |
|
148 |
OutputStream out = new BufferedOutputStream(new FileOutputStream(dest)); |
|
149 |
try { |
|
150 |
byte[] data = new byte[8192]; |
|
151 |
int n; |
|
152 |
while ((n = in.read(data, 0, data.length)) > 0) |
|
153 |
out.write(data, 0, n); |
|
154 |
} finally { |
|
155 |
out.close(); |
|
156 |
in.close(); |
|
157 |
} |
|
158 |
} |
|
159 |
||
160 |
void error(String message) { |
|
161 |
System.err.println("Error: " + message); |
|
162 |
errors++; |
|
163 |
} |
|
164 |
||
165 |
int errors; |
|
166 |
int count; |
|
167 |
} |