jdk/test/tools/launcher/MainClassAttributeTest.java
author ksrini
Thu, 02 Feb 2012 15:37:22 -0800
changeset 11822 475ac0b35c06
child 45944 882cea808912
permissions -rw-r--r--
7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file Reviewed-by: ksrini, darcy Contributed-by: sonali.goel@oracle.com
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
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    29
 * @build MainClassAttributeTest
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    30
 * @run main MainClassAttributeTest
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    31
 */
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    32
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    33
import java.io.File;
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    34
import java.io.FileNotFoundException;
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    35
import java.io.IOException;
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    36
import java.util.ArrayList;
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    37
import java.util.List;
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    38
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    39
/*
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    40
 * 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
    41
 * 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
    42
 * jar is executed.
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    43
 */
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    44
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    45
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
    46
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    47
    /*
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    48
     * 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
    49
     * 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
    50
     * 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
    51
     */
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    52
    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
    53
        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
    54
                "-jar", jarFile.getAbsolutePath());
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    55
        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
    56
            System.out.println(tr);
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    57
            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
    58
        }
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    59
        if (tr.isOK()) {
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    60
            System.out.println(tr);
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    61
            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
    62
        }
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    63
    }
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
    // Missing manifest entry
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    66
    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
    67
        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
    68
        createJar("cvf", jarFile.getName(), ".");
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    69
        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
    70
    }
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    71
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    72
    // 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
    73
    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
    74
        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
    75
        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
    76
        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
    77
    }
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    78
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    79
    // 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
    80
    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
    81
        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
    82
        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
    83
        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
    84
    }
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    85
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    86
    // 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
    87
    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
    88
        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
    89
        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
    90
        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
    91
        contents.add("MainClassName: Foo");
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    92
        createFile(manifestFile, contents);
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    93
        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
    94
        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
    95
    }
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    96
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    97
    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
    98
        test1();
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
    99
        test2();
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
   100
        test3();
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
   101
        test4();
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
   102
    }
475ac0b35c06 7141141: Add 3 new test scenarios for testing Main-Class attribute in jar manifest file
ksrini
parents:
diff changeset
   103
}