test/jdk/tools/launcher/MainClassAttributeTest.java
author mr
Wed, 16 Jan 2019 16:27:21 -0800
changeset 53372 4003935e6e5f
parent 47216 71c04702a3d5
permissions -rw-r--r--
8216532: tools/launcher/Test7029048.java fails (Solaris) Reviewed-by: rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11822
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
     1
/*
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
     4
 *
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
     7
 * published by the Free Software Foundation.
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
     8
 *
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    13
 * accompanied this code).
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    14
 *
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    18
 *
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    21
 * questions.
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    22
 */
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    23
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    24
/**
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    25
 * @test
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    26
 * @bug 7067922
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    27
 * @author sogoel
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    28
 * @summary Test negative scenarios for main class attribute
45944
882cea808912 8179292: a number of launcher tests fail when run with --limit-modules due to CNFE: javax.tools.ToolProvider
anazarov
parents: 11822
diff changeset
    29
 * @modules jdk.compiler
882cea808912 8179292: a number of launcher tests fail when run with --limit-modules due to CNFE: javax.tools.ToolProvider
anazarov
parents: 11822
diff changeset
    30
 *          jdk.zipfs
11822
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    31
 * @build MainClassAttributeTest
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    32
 * @run main MainClassAttributeTest
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    33
 */
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    34
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    35
import java.io.File;
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    36
import java.io.FileNotFoundException;
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    37
import java.io.IOException;
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    38
import java.util.ArrayList;
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    39
import java.util.List;
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    40
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    41
/*
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    42
 * This tests negative scenarios for Main class entry in a jar file.
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    43
 * An error should be thrown for each of the test cases when such a
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    44
 * jar is executed.
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    45
 */
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    46
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    47
public class MainClassAttributeTest extends TestHelper {
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    48
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    49
    /*
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    50
     * These tests compare messages which could be localized, therefore
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    51
     * these tests compare messages only with English locales, and
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    52
     * for all other locales,  the exit values are checked.
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    53
     */
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    54
    static void runTest(File jarFile, String expectedErrorMessage) {
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    55
        TestResult tr = doExec(TestHelper.javaCmd,
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    56
                "-jar", jarFile.getAbsolutePath());
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    57
        if (isEnglishLocale() && !tr.contains(expectedErrorMessage)) {
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    58
            System.out.println(tr);
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    59
            throw new RuntimeException("expected string not found");
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    60
        }
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    61
        if (tr.isOK()) {
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    62
            System.out.println(tr);
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    63
            throw new RuntimeException("test exit with status 0");
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    64
        }
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    65
    }
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    66
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    67
    // Missing manifest entry
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    68
    static void test1() throws IOException {
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    69
        File jarFile = new File("missingmainentry.jar");
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    70
        createJar("cvf", jarFile.getName(), ".");
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    71
        runTest(jarFile, "no main manifest attribute");
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    72
    }
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    73
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    74
    // Entry point in manifest file has .class extension
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    75
    static void test2() throws IOException {
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    76
        File jarFile = new File("extensionmainentry.jar");
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    77
        createJar("Foo.class", jarFile, new File("Foo"), (String[])null);
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    78
        runTest(jarFile, "Error: Could not find or load main class");
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    79
    }
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    80
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    81
    // Entry point in manifest file is misspelled
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    82
    static void test3() throws IOException {
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    83
        File jarFile = new File("misspelledmainentry.jar");
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    84
        createJar("FooMIS", jarFile, new File("Foo"), (String[])null);
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    85
        runTest(jarFile, "Error: Could not find or load main class");
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    86
    }
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    87
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    88
    // Main-Class attribute is misspelled in manifest file
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    89
    static void test4() throws IOException {
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    90
        File jarFile = new File("misspelledMainAttribute.jar");
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    91
        File manifestFile = new File("manifest.txt");
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    92
        List<String> contents = new ArrayList<>();
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    93
        contents.add("MainClassName: Foo");
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    94
        createFile(manifestFile, contents);
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    95
        createJar("-cmf", manifestFile.getName(), jarFile.getName());
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    96
        runTest(jarFile, "no main manifest attribute");
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    97
    }
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    98
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    99
    public static void main(String[] args) throws IOException {
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
   100
        test1();
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
   101
        test2();
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
   102
        test3();
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
   103
        test4();
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
   104
    }
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
   105
}