Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/launcher/MainClassAttributeTest.java
38833 views
/*1* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 706792226* @author sogoel27* @summary Test negative scenarios for main class attribute28* @build MainClassAttributeTest29* @run main MainClassAttributeTest30*/3132import java.io.File;33import java.io.FileNotFoundException;34import java.io.IOException;35import java.util.ArrayList;36import java.util.List;3738/*39* This tests negative scenarios for Main class entry in a jar file.40* An error should be thrown for each of the test cases when such a41* jar is executed.42*/4344public class MainClassAttributeTest extends TestHelper {4546/*47* These tests compare messages which could be localized, therefore48* these tests compare messages only with English locales, and49* for all other locales, the exit values are checked.50*/51static void runTest(File jarFile, String expectedErrorMessage) {52TestResult tr = doExec(TestHelper.javaCmd,53"-jar", jarFile.getAbsolutePath());54if (isEnglishLocale() && !tr.contains(expectedErrorMessage)) {55System.out.println(tr);56throw new RuntimeException("expected string not found");57}58if (tr.isOK()) {59System.out.println(tr);60throw new RuntimeException("test exit with status 0");61}62}6364// Missing manifest entry65static void test1() throws IOException {66File jarFile = new File("missingmainentry.jar");67createJar("cvf", jarFile.getName(), ".");68runTest(jarFile, "no main manifest attribute");69}7071// Entry point in manifest file has .class extension72static void test2() throws IOException {73File jarFile = new File("extensionmainentry.jar");74createJar("Foo.class", jarFile, new File("Foo"), (String[])null);75runTest(jarFile, "Error: Could not find or load main class");76}7778// Entry point in manifest file is misspelled79static void test3() throws IOException {80File jarFile = new File("misspelledmainentry.jar");81createJar("FooMIS", jarFile, new File("Foo"), (String[])null);82runTest(jarFile, "Error: Could not find or load main class");83}8485// Main-Class attribute is misspelled in manifest file86static void test4() throws IOException {87File jarFile = new File("misspelledMainAttribute.jar");88File manifestFile = new File("manifest.txt");89List<String> contents = new ArrayList<>();90contents.add("MainClassName: Foo");91createFile(manifestFile, contents);92createJar("-cmf", manifestFile.getName(), jarFile.getName());93runTest(jarFile, "no main manifest attribute");94}9596public static void main(String[] args) throws IOException {97test1();98test2();99test3();100test4();101}102}103104105