langtools/test/tools/javac/6400872/T6400872.java
author jjg
Wed, 29 Oct 2014 17:25:23 -0700
changeset 27319 030080f03e4f
parent 5520 86e4b9a9da40
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:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     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
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    21
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
 * @test
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
 * @bug 6400872
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
 * @summary REGRESSION:  Java Compiler cannot find jar files referenced by other
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
 * @run main T6400872
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
// ${TESTJAVA}/bin/javac -d ${TESTCLASSES} ${TESTSRC}/A.java ${TESTSRC}/B.java
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
// ${TESTJAVA}/bin/jar -cfm A.jar ${TESTSRC}/A/META-INF/MANIFEST.MF -C ${TESTCLASSES} A.class
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
// ${TESTJAVA}/bin/jar -cfm B.jar ${TESTSRC}/B/META-INF/MANIFEST.MF -C ${TESTCLASSES} B.class
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
// ${TESTJAVA}/bin/javac -cp A.jar ${TESTSRC}/C.java
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import java.nio.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import java.util.jar.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import javax.tools.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import javax.tools.StandardJavaFileManager.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
public class T6400872 {
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
    static File testSrc = new File(System.getProperty("test.src", "."));
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
    static File testClasses = new File(System.getProperty("test.classes", "."));
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    public static void main(String... args) throws Exception {
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
        // compile A.java and B.java
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
        compile(testClasses, null, new File(testSrc, "A.java"), new File(testSrc, "B.java"));
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
        // put them in mutually referential class files
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
        jar(new File("A.jar"), iterable(new File(".", "B.jar")), testClasses, new File("A.class"));
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
        jar(new File("B.jar"), iterable(new File(".", "A.jar")), testClasses, new File("B.class"));
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
        // verify we can successfully use the class path entries in the jar files
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
        compile(new File("."), iterable(new File("A.jar")), new File(testSrc, "C.java"));
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    static void compile(File classOutDir, Iterable<File> classPath, File... files)
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
                throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        System.err.println("compile...");
06bc494ca11e Initial load
duke
parents:
diff changeset
    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
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
            Iterable<? extends JavaFileObject> fileObjects =
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
                fm.getJavaFileObjectsFromFiles(Arrays.asList(files));
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
            List<String> options = new ArrayList<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
            if (classOutDir != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
                options.add("-d");
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
                options.add(classOutDir.getPath());
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
            if (classPath != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
                options.add("-classpath");
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
                options.add(join(classPath, File.pathSeparator));
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
            options.add("-verbose");
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
            JavaCompiler.CompilationTask task =
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
                compiler.getTask(null, fm, null, options, null, fileObjects);
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
            if (!task.call())
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
                throw new AssertionError("compilation failed");
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    static void jar(File jar, Iterable<File> classPath, File base, File... files)
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
            throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        System.err.println("jar...");
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        Manifest m = new Manifest();
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        if (classPath != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
            Attributes mainAttrs = m.getMainAttributes();
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
            mainAttrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
            mainAttrs.put(Attributes.Name.CLASS_PATH, join(classPath, " "));
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        OutputStream out = new BufferedOutputStream(new FileOutputStream(jar));
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        JarOutputStream j = new JarOutputStream(out, m);
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        add(j, base, files);
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        j.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    static void add(JarOutputStream j, File base, File... files) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        if (files == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        for (File f: files)
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
            add(j, base, f);
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    static void add(JarOutputStream j, File base, File file) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        File f = new File(base, file.getPath());
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        if (f.isDirectory()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
            String[] children = f.list();
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
            if (children != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
                for (String c: children)
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
                    add(j, base, new File(file, c));
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
            JarEntry e = new JarEntry(file.getPath());
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            e.setSize(f.length());
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
            j.putNextEntry(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
            j.write(read(f));
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
            j.closeEntry();
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    static byte[] read(File f) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        byte[] buf = new byte[(int) f.length()];
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        int offset = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        while (offset < buf.length) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
            int n = in.read(buf, offset, buf.length - offset);
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
            if (n < 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
                throw new EOFException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
            offset += n;
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        return buf;
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    static <T> Iterable<T> iterable(T single) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
        return Collections.singleton(single);
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
    static <T> String join(Iterable<T> iter, String sep) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        StringBuilder p = new StringBuilder();
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        for (T t: iter) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
            if (p.length() > 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
                p.append(' ');
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            p.append(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        return p.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
}