author | jjg |
Wed, 29 Oct 2014 17:25:23 -0700 | |
changeset 27319 | 030080f03e4f |
parent 5520 | 86e4b9a9da40 |
child 30730 | d3ce7619db2c |
permissions | -rw-r--r-- |
10 | 1 |
/* |
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
5520
diff
changeset
|
2 |
* Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
* |
|
5520 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
10 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 6400872 |
|
27 |
* @summary REGRESSION: Java Compiler cannot find jar files referenced by other |
|
28 |
* @run main T6400872 |
|
29 |
*/ |
|
30 |
||
31 |
// ${TESTJAVA}/bin/javac -d ${TESTCLASSES} ${TESTSRC}/A.java ${TESTSRC}/B.java |
|
32 |
// ${TESTJAVA}/bin/jar -cfm A.jar ${TESTSRC}/A/META-INF/MANIFEST.MF -C ${TESTCLASSES} A.class |
|
33 |
// ${TESTJAVA}/bin/jar -cfm B.jar ${TESTSRC}/B/META-INF/MANIFEST.MF -C ${TESTCLASSES} B.class |
|
34 |
// ${TESTJAVA}/bin/javac -cp A.jar ${TESTSRC}/C.java |
|
35 |
||
36 |
import java.io.*; |
|
37 |
import java.nio.*; |
|
38 |
import java.util.*; |
|
39 |
import java.util.jar.*; |
|
40 |
import javax.tools.*; |
|
41 |
import javax.tools.StandardJavaFileManager.*; |
|
42 |
||
43 |
public class T6400872 { |
|
44 |
static File testSrc = new File(System.getProperty("test.src", ".")); |
|
45 |
static File testClasses = new File(System.getProperty("test.classes", ".")); |
|
46 |
||
47 |
public static void main(String... args) throws Exception { |
|
48 |
// compile A.java and B.java |
|
49 |
compile(testClasses, null, new File(testSrc, "A.java"), new File(testSrc, "B.java")); |
|
50 |
// put them in mutually referential class files |
|
51 |
jar(new File("A.jar"), iterable(new File(".", "B.jar")), testClasses, new File("A.class")); |
|
52 |
jar(new File("B.jar"), iterable(new File(".", "A.jar")), testClasses, new File("B.class")); |
|
53 |
// verify we can successfully use the class path entries in the jar files |
|
54 |
compile(new File("."), iterable(new File("A.jar")), new File(testSrc, "C.java")); |
|
55 |
} |
|
56 |
||
57 |
static void compile(File classOutDir, Iterable<File> classPath, File... files) |
|
58 |
throws IOException { |
|
59 |
System.err.println("compile..."); |
|
60 |
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
5520
diff
changeset
|
61 |
try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) { |
10 | 62 |
Iterable<? extends JavaFileObject> fileObjects = |
63 |
fm.getJavaFileObjectsFromFiles(Arrays.asList(files)); |
|
64 |
||
65 |
List<String> options = new ArrayList<String>(); |
|
66 |
if (classOutDir != null) { |
|
67 |
options.add("-d"); |
|
68 |
options.add(classOutDir.getPath()); |
|
69 |
} |
|
70 |
if (classPath != null) { |
|
71 |
options.add("-classpath"); |
|
72 |
options.add(join(classPath, File.pathSeparator)); |
|
73 |
} |
|
74 |
options.add("-verbose"); |
|
75 |
||
76 |
JavaCompiler.CompilationTask task = |
|
77 |
compiler.getTask(null, fm, null, options, null, fileObjects); |
|
78 |
if (!task.call()) |
|
79 |
throw new AssertionError("compilation failed"); |
|
80 |
} |
|
81 |
} |
|
82 |
||
83 |
static void jar(File jar, Iterable<File> classPath, File base, File... files) |
|
84 |
throws IOException { |
|
85 |
System.err.println("jar..."); |
|
86 |
Manifest m = new Manifest(); |
|
87 |
if (classPath != null) { |
|
88 |
Attributes mainAttrs = m.getMainAttributes(); |
|
89 |
mainAttrs.put(Attributes.Name.MANIFEST_VERSION, "1.0"); |
|
90 |
mainAttrs.put(Attributes.Name.CLASS_PATH, join(classPath, " ")); |
|
91 |
} |
|
92 |
OutputStream out = new BufferedOutputStream(new FileOutputStream(jar)); |
|
93 |
JarOutputStream j = new JarOutputStream(out, m); |
|
94 |
add(j, base, files); |
|
95 |
j.close(); |
|
96 |
} |
|
97 |
||
98 |
static void add(JarOutputStream j, File base, File... files) throws IOException { |
|
99 |
if (files == null) |
|
100 |
return; |
|
101 |
||
102 |
for (File f: files) |
|
103 |
add(j, base, f); |
|
104 |
} |
|
105 |
||
106 |
static void add(JarOutputStream j, File base, File file) throws IOException { |
|
107 |
File f = new File(base, file.getPath()); |
|
108 |
if (f.isDirectory()) { |
|
109 |
String[] children = f.list(); |
|
110 |
if (children != null) |
|
111 |
for (String c: children) |
|
112 |
add(j, base, new File(file, c)); |
|
113 |
} else { |
|
114 |
JarEntry e = new JarEntry(file.getPath()); |
|
115 |
e.setSize(f.length()); |
|
116 |
j.putNextEntry(e); |
|
117 |
j.write(read(f)); |
|
118 |
j.closeEntry(); |
|
119 |
} |
|
120 |
||
121 |
} |
|
122 |
||
123 |
static byte[] read(File f) throws IOException { |
|
124 |
byte[] buf = new byte[(int) f.length()]; |
|
125 |
BufferedInputStream in = new BufferedInputStream(new FileInputStream(f)); |
|
126 |
int offset = 0; |
|
127 |
while (offset < buf.length) { |
|
128 |
int n = in.read(buf, offset, buf.length - offset); |
|
129 |
if (n < 0) |
|
130 |
throw new EOFException(); |
|
131 |
offset += n; |
|
132 |
} |
|
133 |
return buf; |
|
134 |
} |
|
135 |
||
136 |
static <T> Iterable<T> iterable(T single) { |
|
137 |
return Collections.singleton(single); |
|
138 |
} |
|
139 |
||
140 |
static <T> String join(Iterable<T> iter, String sep) { |
|
141 |
StringBuilder p = new StringBuilder(); |
|
142 |
for (T t: iter) { |
|
143 |
if (p.length() > 0) |
|
144 |
p.append(' '); |
|
145 |
p.append(t); |
|
146 |
} |
|
147 |
return p.toString(); |
|
148 |
} |
|
149 |
} |