1 /*
   2  * Copyright (c) 2015, 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 import java.io.File;
  25 import java.io.IOException;
  26 import java.util.ArrayList;
  27 import java.util.List;
  28 
  29 /**
  30  * @test
  31  * @bug 8076264
  32  * @summary Launching app shouldn't require enclosing class for the main class.
  33  * @compile TestMainWithoutEnclosing.java
  34  * @run main TestMainWithoutEnclosing
  35  */
  36 public final class TestMainWithoutEnclosing extends TestHelper {
  37 
  38     static final String EnclosingName = "Enclosing";
  39 
  40     static void createJarFile(File testJar) throws IOException {
  41         List<String> scratch = new ArrayList<>();
  42         scratch.add("public class Enclosing {");
  43         scratch.add("    public static final class Main {");
  44         scratch.add("        public static void main(String... args) {");
  45         scratch.add("            System.out.println(\"Hello World\");");
  46         scratch.add("        }");
  47         scratch.add("    }");
  48         scratch.add("}");
  49         File enclosingFile = new File(EnclosingName + ".java");
  50         createFile(enclosingFile, scratch);
  51         compile(enclosingFile.getName());
  52         // avoid side effects remove the Enclosing class
  53         getClassFile(enclosingFile).delete();
  54         createJar("cvfe", testJar.getName(), EnclosingName + "$Main",
  55                 EnclosingName + "$Main" + ".class");
  56         // remove extraneous files in the current directory
  57         new File(EnclosingName + "$Main" + ".class").delete();
  58     }
  59 
  60     public static void main(String... args) throws IOException {
  61         File testJarFile = new File("test.jar");
  62         createJarFile(testJarFile);
  63         TestResult tr = doExec(javaCmd, "-jar", testJarFile.getName());
  64         if (!tr.isOK()) {
  65             System.out.println(tr);
  66             throw new RuntimeException("test returned non-positive value");
  67         }
  68         if (!tr.contains("Hello World")) {
  69             System.out.println(tr);
  70             throw new RuntimeException("expected output not found");
  71         }
  72     }
  73 }