langtools/test/tools/javac/Paths/TestCompileJARInClassPath.java
author jjg
Wed, 29 Oct 2014 17:25:23 -0700
changeset 27319 030080f03e4f
parent 14360 2a8304e421ed
child 30730 d3ce7619db2c
permissions -rw-r--r--
8062348: langtools tests should close file manager (group 1) Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14360
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
     1
/*
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 14360
diff changeset
     2
 * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
14360
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
     4
 *
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
     8
 *
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    13
 * accompanied this code).
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    14
 *
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    18
 *
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    21
 * questions.
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    22
 */
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    23
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    24
/*
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    25
 * @test
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    26
 * @bug 6725230
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    27
 * @summary Test to make sure that java Compilation with JSR199 does not ignore
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    28
 * Class-Path in manifest
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    29
 * @author vicente.romero
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    30
 * @build TestCompileJARInClassPath
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    31
 * @run main TestCompileJARInClassPath
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    32
 */
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    33
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    34
import java.io.File;
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    35
import java.io.FileOutputStream;
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    36
import java.io.IOException;
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    37
import java.io.PrintStream;
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    38
import java.util.ArrayList;
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    39
import java.util.List;
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    40
import javax.tools.DiagnosticCollector;
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    41
import javax.tools.JavaFileObject;
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    42
import javax.tools.StandardJavaFileManager;
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    43
import javax.tools.StandardLocation;
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    44
import javax.tools.ToolProvider;
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    45
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    46
public class TestCompileJARInClassPath {
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    47
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    48
    public static void main(String args[]) throws Exception {
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    49
        TestCompileJARInClassPath theTest = new TestCompileJARInClassPath();
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    50
        theTest.run();
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    51
    }
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    52
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    53
    void run() throws Exception {
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    54
        try {
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    55
            clean();
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    56
            generateFilesNeeded();
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    57
            compileWithJSR199();
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    58
        } finally {
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    59
            clean();
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    60
        }
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    61
    }
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    62
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    63
    void writeFile(String f, String contents) throws IOException {
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    64
        PrintStream s = new PrintStream(new FileOutputStream(f));
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    65
        s.println(contents);
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    66
        s.close();
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    67
    }
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    68
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    69
    void rm(String filename) throws Exception {
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    70
        File f = new File(filename);
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    71
        f.delete();
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    72
        if (f.exists())
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    73
            throw new Exception(filename + ": couldn't remove");
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    74
    }
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    75
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    76
    void clean() throws Exception {
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    77
        rm("C1.java");
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    78
        rm("C1.class");
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    79
        rm("C1.jar");
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    80
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    81
        rm("C2.java");
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    82
        rm("C2.class");
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    83
        rm("C2.jar");
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    84
        rm("MANIFEST.MF");
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    85
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    86
        rm("C3.java");
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    87
        rm("C3.class");
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    88
    }
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    89
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    90
    void generateFilesNeeded() throws Exception {
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    91
        sun.tools.jar.Main jarGenerator = new sun.tools.jar.Main(System.out, System.err, "jar");
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    92
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    93
        writeFile("C1.java",
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    94
                  "public class C1 {public static void f() {}}");
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    95
        com.sun.tools.javac.Main.compile(new String[]{"C1.java"});
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    96
        jarGenerator.run(new String[] {"cf", "C1.jar", "C1.class"});
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    97
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    98
        writeFile("C2.java",
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
    99
                  "public class C2 {public static void g() {}}");
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
   100
        writeFile("MANIFEST.MF",
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
   101
                  "Manifest-Version: 1.0\n" +
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
   102
                  "Class-Path: C1.jar\n" +
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
   103
                  "Main-Class: C2");
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
   104
        com.sun.tools.javac.Main.compile(new String[]{"C2.java"});
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
   105
        jarGenerator.run(new String[] {"cfm", "C2.jar", "MANIFEST.MF", "C2.class"});
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
   106
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
   107
        writeFile("C3.java",
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
   108
                  "public class C3 {public static void h() {C2.g(); C1.f();}}");
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
   109
    }
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
   110
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
   111
    void compileWithJSR199() throws IOException {
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
   112
        String cpath = "C2.jar";
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
   113
        File clientJarFile = new File(cpath);
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
   114
        File sourceFileToCompile = new File("C3.java");
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
   115
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
   116
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
   117
        javax.tools.JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
   118
        DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 14360
diff changeset
   119
        try (StandardJavaFileManager stdFileManager = javac.getStandardFileManager(diagnostics, null, null)) {
14360
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
   120
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 14360
diff changeset
   121
            List<File> files = new ArrayList<>();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 14360
diff changeset
   122
            files.add(clientJarFile);
14360
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
   123
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 14360
diff changeset
   124
            stdFileManager.setLocation(StandardLocation.CLASS_PATH, files);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 14360
diff changeset
   125
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 14360
diff changeset
   126
            Iterable<? extends JavaFileObject> sourceFiles = stdFileManager.getJavaFileObjects(sourceFileToCompile);
14360
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
   127
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 14360
diff changeset
   128
            if (!javac.getTask(null, stdFileManager, diagnostics, null, null, sourceFiles).call()) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 14360
diff changeset
   129
                throw new AssertionError("compilation failed");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 14360
diff changeset
   130
            }
14360
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
   131
        }
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
   132
    }
2a8304e421ed 6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
diff changeset
   133
}