test/tools/javac/Paths/TestCompileJARInClassPath.java

changeset 1375
ea2616a6bd01
parent 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/Paths/TestCompileJARInClassPath.java	Thu Oct 25 13:33:27 2012 -0700
     1.3 @@ -0,0 +1,132 @@
     1.4 +/*
     1.5 + * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 6725230
    1.30 + * @summary Test to make sure that java Compilation with JSR199 does not ignore
    1.31 + * Class-Path in manifest
    1.32 + * @author vicente.romero
    1.33 + * @build TestCompileJARInClassPath
    1.34 + * @run main TestCompileJARInClassPath
    1.35 + */
    1.36 +
    1.37 +import java.io.File;
    1.38 +import java.io.FileOutputStream;
    1.39 +import java.io.IOException;
    1.40 +import java.io.PrintStream;
    1.41 +import java.util.ArrayList;
    1.42 +import java.util.List;
    1.43 +import javax.tools.DiagnosticCollector;
    1.44 +import javax.tools.JavaFileObject;
    1.45 +import javax.tools.StandardJavaFileManager;
    1.46 +import javax.tools.StandardLocation;
    1.47 +import javax.tools.ToolProvider;
    1.48 +
    1.49 +public class TestCompileJARInClassPath {
    1.50 +
    1.51 +    public static void main(String args[]) throws Exception {
    1.52 +        TestCompileJARInClassPath theTest = new TestCompileJARInClassPath();
    1.53 +        theTest.run();
    1.54 +    }
    1.55 +
    1.56 +    void run() throws Exception {
    1.57 +        try {
    1.58 +            clean();
    1.59 +            generateFilesNeeded();
    1.60 +            compileWithJSR199();
    1.61 +        } finally {
    1.62 +            clean();
    1.63 +        }
    1.64 +    }
    1.65 +
    1.66 +    void writeFile(String f, String contents) throws IOException {
    1.67 +        PrintStream s = new PrintStream(new FileOutputStream(f));
    1.68 +        s.println(contents);
    1.69 +        s.close();
    1.70 +    }
    1.71 +
    1.72 +    void rm(String filename) throws Exception {
    1.73 +        File f = new File(filename);
    1.74 +        f.delete();
    1.75 +        if (f.exists())
    1.76 +            throw new Exception(filename + ": couldn't remove");
    1.77 +    }
    1.78 +
    1.79 +    void clean() throws Exception {
    1.80 +        rm("C1.java");
    1.81 +        rm("C1.class");
    1.82 +        rm("C1.jar");
    1.83 +
    1.84 +        rm("C2.java");
    1.85 +        rm("C2.class");
    1.86 +        rm("C2.jar");
    1.87 +        rm("MANIFEST.MF");
    1.88 +
    1.89 +        rm("C3.java");
    1.90 +        rm("C3.class");
    1.91 +    }
    1.92 +
    1.93 +    void generateFilesNeeded() throws Exception {
    1.94 +        sun.tools.jar.Main jarGenerator = new sun.tools.jar.Main(System.out, System.err, "jar");
    1.95 +
    1.96 +        writeFile("C1.java",
    1.97 +                  "public class C1 {public static void f() {}}");
    1.98 +        com.sun.tools.javac.Main.compile(new String[]{"C1.java"});
    1.99 +        jarGenerator.run(new String[] {"cf", "C1.jar", "C1.class"});
   1.100 +
   1.101 +        writeFile("C2.java",
   1.102 +                  "public class C2 {public static void g() {}}");
   1.103 +        writeFile("MANIFEST.MF",
   1.104 +                  "Manifest-Version: 1.0\n" +
   1.105 +                  "Class-Path: C1.jar\n" +
   1.106 +                  "Main-Class: C2");
   1.107 +        com.sun.tools.javac.Main.compile(new String[]{"C2.java"});
   1.108 +        jarGenerator.run(new String[] {"cfm", "C2.jar", "MANIFEST.MF", "C2.class"});
   1.109 +
   1.110 +        writeFile("C3.java",
   1.111 +                  "public class C3 {public static void h() {C2.g(); C1.f();}}");
   1.112 +    }
   1.113 +
   1.114 +    void compileWithJSR199() throws IOException {
   1.115 +        String cpath = "C2.jar";
   1.116 +        File clientJarFile = new File(cpath);
   1.117 +        File sourceFileToCompile = new File("C3.java");
   1.118 +
   1.119 +
   1.120 +        javax.tools.JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
   1.121 +        DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
   1.122 +        StandardJavaFileManager stdFileManager = javac.getStandardFileManager(diagnostics, null, null);
   1.123 +
   1.124 +        List<File> files = new ArrayList<>();
   1.125 +        files.add(clientJarFile);
   1.126 +
   1.127 +        stdFileManager.setLocation(StandardLocation.CLASS_PATH, files);
   1.128 +
   1.129 +        Iterable<? extends JavaFileObject> sourceFiles = stdFileManager.getJavaFileObjects(sourceFileToCompile);
   1.130 +
   1.131 +        if (!javac.getTask(null, stdFileManager, diagnostics, null, null, sourceFiles).call()) {
   1.132 +            throw new AssertionError("compilation failed");
   1.133 +        }
   1.134 +    }
   1.135 +}

mercurial