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