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