test/langtools/tools/javac/file/FSInfoTest.java
author jjg
Fri, 22 Nov 2019 10:38:35 -0800
changeset 59229 fba8fa613d1a
permissions -rw-r--r--
8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause Reviewed-by: jjg Contributed-by: jai.forums2013@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
59229
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
     1
/*
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
     4
 *
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
     8
 *
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    13
 * accompanied this code).
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    14
 *
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    18
 *
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    21
 * questions.
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    22
 */
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    23
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    24
import com.sun.tools.javac.file.FSInfo;
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    25
import com.sun.tools.javac.util.Context;
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    26
import org.testng.annotations.Test;
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    27
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    28
import java.io.IOException;
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    29
import java.nio.file.Files;
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    30
import java.nio.file.Path;
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    31
import java.util.Locale;
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    32
import java.util.jar.JarOutputStream;
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    33
import java.util.jar.Manifest;
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    34
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    35
/*
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    36
 * @test
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    37
 * @bug 8232170
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    38
 * @summary Test com.sun.tools.javac.file.FSInfo
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    39
 * @modules jdk.compiler/com.sun.tools.javac.util
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    40
 *          jdk.compiler/com.sun.tools.javac.file
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    41
 * @run testng FSInfoTest
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    42
 */
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    43
public class FSInfoTest {
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    44
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    45
    /**
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    46
     * Tests that if a jar file has a manifest with a invalid path value for {@code Class-Path} attribute,
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    47
     * then parsing such a jar file through {@link FSInfo#getJarClassPath(Path)} doesn't throw any other
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    48
     * exception other than {@link IOException}
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    49
     *
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    50
     * @throws Exception
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    51
     */
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    52
    @Test
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    53
    public void testInvalidClassPath() throws Exception {
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    54
        final String invalidOSPath = System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows")
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    55
                ? "C:\\*" : "foo\u0000bar";
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    56
        final Path jarFile = Files.createTempFile(null, ".jar");
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    57
        jarFile.toFile().deleteOnExit();
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    58
        final Manifest mf = new Manifest();
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    59
        mf.getMainAttributes().putValue("Manifest-Version", "1.0");
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    60
        // add Class-Path which points to an invalid path
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    61
        System.out.println("Intentionally using an invalid Class-Path entry " + invalidOSPath + " in manifest");
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    62
        mf.getMainAttributes().putValue("Class-Path", invalidOSPath + " " + "/some/other-random/path");
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    63
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    64
        // create a jar file with the manifest
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    65
        try (final JarOutputStream jar = new JarOutputStream(Files.newOutputStream(jarFile), mf)) {
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    66
        }
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    67
        final FSInfo fsInfo = FSInfo.instance(new Context());
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    68
        try {
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    69
            fsInfo.getJarClassPath(jarFile);
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    70
            // we don't rely on fsInfo.getJarClassPath to throw an exception for invalid
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    71
            // paths. Hence no Assert.fail(...) call here. But if it does throw some exception,
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    72
            // then that exception should always be a IOException.
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    73
        } catch (IOException ioe) {
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    74
            // expected
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    75
            System.out.println("(As expected) FSInfo.getJarClassPath threw an IOException - " + ioe.getMessage());
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    76
        }
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    77
    }
fba8fa613d1a 8232170: FSInfo#getJarClassPath throws an exception not declared in its throws clause
jjg
parents:
diff changeset
    78
}