Path: blob/master/test/jdk/tools/launcher/MainClassAttributeTest.java
41145 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* @modules jdk.compiler29* jdk.zipfs30* @build MainClassAttributeTest31* @run main MainClassAttributeTest32*/3334import java.io.File;35import java.io.FileNotFoundException;36import java.io.IOException;37import java.util.ArrayList;38import java.util.List;3940/*41* This tests negative scenarios for Main class entry in a jar file.42* An error should be thrown for each of the test cases when such a43* jar is executed.44*/4546public class MainClassAttributeTest extends TestHelper {4748/*49* These tests compare messages which could be localized, therefore50* these tests compare messages only with English locales, and51* for all other locales, the exit values are checked.52*/53static void runTest(File jarFile, String expectedErrorMessage) {54TestResult tr = doExec(TestHelper.javaCmd,55"-jar", jarFile.getAbsolutePath());56if (isEnglishLocale() && !tr.contains(expectedErrorMessage)) {57System.out.println(tr);58throw new RuntimeException("expected string not found");59}60if (tr.isOK()) {61System.out.println(tr);62throw new RuntimeException("test exit with status 0");63}64}6566// Missing manifest entry67static void test1() throws IOException {68File jarFile = new File("missingmainentry.jar");69createJar("cvf", jarFile.getName(), ".");70runTest(jarFile, "no main manifest attribute");71}7273// Entry point in manifest file has .class extension74static void test2() throws IOException {75File jarFile = new File("extensionmainentry.jar");76createJar("Foo.class", jarFile, new File("Foo"), (String[])null);77runTest(jarFile, "Error: Could not find or load main class");78}7980// Entry point in manifest file is misspelled81static void test3() throws IOException {82File jarFile = new File("misspelledmainentry.jar");83createJar("FooMIS", jarFile, new File("Foo"), (String[])null);84runTest(jarFile, "Error: Could not find or load main class");85}8687// Main-Class attribute is misspelled in manifest file88static void test4() throws IOException {89File jarFile = new File("misspelledMainAttribute.jar");90File manifestFile = new File("manifest.txt");91List<String> contents = new ArrayList<>();92contents.add("MainClassName: Foo");93createFile(manifestFile, contents);94createJar("-cmf", manifestFile.getName(), jarFile.getName());95runTest(jarFile, "no main manifest attribute");96}9798public static void main(String[] args) throws IOException {99test1();100test2();101test3();102test4();103}104}105106107