Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/launcher/MainClassAttributeTest.java
38833 views
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
* @build MainClassAttributeTest
30
* @run main MainClassAttributeTest
31
*/
32
33
import java.io.File;
34
import java.io.FileNotFoundException;
35
import java.io.IOException;
36
import java.util.ArrayList;
37
import java.util.List;
38
39
/*
40
* This tests negative scenarios for Main class entry in a jar file.
41
* An error should be thrown for each of the test cases when such a
42
* jar is executed.
43
*/
44
45
public class MainClassAttributeTest extends TestHelper {
46
47
/*
48
* These tests compare messages which could be localized, therefore
49
* these tests compare messages only with English locales, and
50
* for all other locales, the exit values are checked.
51
*/
52
static void runTest(File jarFile, String expectedErrorMessage) {
53
TestResult tr = doExec(TestHelper.javaCmd,
54
"-jar", jarFile.getAbsolutePath());
55
if (isEnglishLocale() && !tr.contains(expectedErrorMessage)) {
56
System.out.println(tr);
57
throw new RuntimeException("expected string not found");
58
}
59
if (tr.isOK()) {
60
System.out.println(tr);
61
throw new RuntimeException("test exit with status 0");
62
}
63
}
64
65
// Missing manifest entry
66
static void test1() throws IOException {
67
File jarFile = new File("missingmainentry.jar");
68
createJar("cvf", jarFile.getName(), ".");
69
runTest(jarFile, "no main manifest attribute");
70
}
71
72
// Entry point in manifest file has .class extension
73
static void test2() throws IOException {
74
File jarFile = new File("extensionmainentry.jar");
75
createJar("Foo.class", jarFile, new File("Foo"), (String[])null);
76
runTest(jarFile, "Error: Could not find or load main class");
77
}
78
79
// Entry point in manifest file is misspelled
80
static void test3() throws IOException {
81
File jarFile = new File("misspelledmainentry.jar");
82
createJar("FooMIS", jarFile, new File("Foo"), (String[])null);
83
runTest(jarFile, "Error: Could not find or load main class");
84
}
85
86
// Main-Class attribute is misspelled in manifest file
87
static void test4() throws IOException {
88
File jarFile = new File("misspelledMainAttribute.jar");
89
File manifestFile = new File("manifest.txt");
90
List<String> contents = new ArrayList<>();
91
contents.add("MainClassName: Foo");
92
createFile(manifestFile, contents);
93
createJar("-cmf", manifestFile.getName(), jarFile.getName());
94
runTest(jarFile, "no main manifest attribute");
95
}
96
97
public static void main(String[] args) throws IOException {
98
test1();
99
test2();
100
test3();
101
test4();
102
}
103
}
104
105