test/tools/javac/Paths/TestCompileJARInClassPath.java

changeset 1375
ea2616a6bd01
parent 0
959103a6100f
equal deleted inserted replaced
1374:c002fdee76fd 1375:ea2616a6bd01
1 /*
2 * Copyright (c) 2002, 2012, Oracle and/or its affiliates. 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 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.
22 */
23
24 /*
25 * @test
26 * @bug 6725230
27 * @summary Test to make sure that java Compilation with JSR199 does not ignore
28 * Class-Path in manifest
29 * @author vicente.romero
30 * @build TestCompileJARInClassPath
31 * @run main TestCompileJARInClassPath
32 */
33
34 import java.io.File;
35 import java.io.FileOutputStream;
36 import java.io.IOException;
37 import java.io.PrintStream;
38 import java.util.ArrayList;
39 import java.util.List;
40 import javax.tools.DiagnosticCollector;
41 import javax.tools.JavaFileObject;
42 import javax.tools.StandardJavaFileManager;
43 import javax.tools.StandardLocation;
44 import javax.tools.ToolProvider;
45
46 public class TestCompileJARInClassPath {
47
48 public static void main(String args[]) throws Exception {
49 TestCompileJARInClassPath theTest = new TestCompileJARInClassPath();
50 theTest.run();
51 }
52
53 void run() throws Exception {
54 try {
55 clean();
56 generateFilesNeeded();
57 compileWithJSR199();
58 } finally {
59 clean();
60 }
61 }
62
63 void writeFile(String f, String contents) throws IOException {
64 PrintStream s = new PrintStream(new FileOutputStream(f));
65 s.println(contents);
66 s.close();
67 }
68
69 void rm(String filename) throws Exception {
70 File f = new File(filename);
71 f.delete();
72 if (f.exists())
73 throw new Exception(filename + ": couldn't remove");
74 }
75
76 void clean() throws Exception {
77 rm("C1.java");
78 rm("C1.class");
79 rm("C1.jar");
80
81 rm("C2.java");
82 rm("C2.class");
83 rm("C2.jar");
84 rm("MANIFEST.MF");
85
86 rm("C3.java");
87 rm("C3.class");
88 }
89
90 void generateFilesNeeded() throws Exception {
91 sun.tools.jar.Main jarGenerator = new sun.tools.jar.Main(System.out, System.err, "jar");
92
93 writeFile("C1.java",
94 "public class C1 {public static void f() {}}");
95 com.sun.tools.javac.Main.compile(new String[]{"C1.java"});
96 jarGenerator.run(new String[] {"cf", "C1.jar", "C1.class"});
97
98 writeFile("C2.java",
99 "public class C2 {public static void g() {}}");
100 writeFile("MANIFEST.MF",
101 "Manifest-Version: 1.0\n" +
102 "Class-Path: C1.jar\n" +
103 "Main-Class: C2");
104 com.sun.tools.javac.Main.compile(new String[]{"C2.java"});
105 jarGenerator.run(new String[] {"cfm", "C2.jar", "MANIFEST.MF", "C2.class"});
106
107 writeFile("C3.java",
108 "public class C3 {public static void h() {C2.g(); C1.f();}}");
109 }
110
111 void compileWithJSR199() throws IOException {
112 String cpath = "C2.jar";
113 File clientJarFile = new File(cpath);
114 File sourceFileToCompile = new File("C3.java");
115
116
117 javax.tools.JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
118 DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
119 StandardJavaFileManager stdFileManager = javac.getStandardFileManager(diagnostics, null, null);
120
121 List<File> files = new ArrayList<>();
122 files.add(clientJarFile);
123
124 stdFileManager.setLocation(StandardLocation.CLASS_PATH, files);
125
126 Iterable<? extends JavaFileObject> sourceFiles = stdFileManager.getJavaFileObjects(sourceFileToCompile);
127
128 if (!javac.getTask(null, stdFileManager, diagnostics, null, null, sourceFiles).call()) {
129 throw new AssertionError("compilation failed");
130 }
131 }
132 }

mercurial