Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/launcher/FXLauncherTest.java
38833 views
/*1* Copyright (c) 2012, 2013, 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 8001533 800454726* @summary Test launching FX application with java -jar27* Test uses main method and blank main method, a jfx app class and an incorrest28* jfx app class, a main-class for the manifest, a bogus one and none.29* All should execute except the incorrect fx app class entries.30* @run main FXLauncherTest31*/32import java.io.File;33import java.io.IOException;34import java.util.ArrayList;35import java.util.List;3637public class FXLauncherTest extends TestHelper {38private static final String FX_MARKER_CLASS = "javafx.application.Application";39private static void line() {40System.out.println("_____________________________________________");41}42private static File MainJavaFile = null;43private static final File FXtestJar = new File("fxtest.jar");44private static final File ManifestFile = new File("manifest.txt");45private static final File ScratchDir = new File(".");4647/* standard main class can be used as java main for fx app class */48static final String StdMainClass = "helloworld.HelloWorld";49static final String ExtMainClass = "helloworld.ExtHello";50static final String NonFXMainClass = "helloworld.HelloJava";51static int testcount = 0;5253/* a main method and a blank. */54static final String[] MAIN_METHODS = {55"public static void main(String[] args) { launch(args); }",56" "57};5859// Array of parameters to pass to fx application.60static final String[] APP_PARMS = { "one", "two" };6162// Create fx java file for test application63static void createJavaFile(String mainmethod) {64try {65String mainClass = "HelloWorld";66List<String> contents = new ArrayList<>();67contents.add("package helloworld;");68contents.add("import javafx.application.Application;");69contents.add("import javafx.event.ActionEvent;");70contents.add("import javafx.event.EventHandler;");71contents.add("import javafx.scene.Scene;");72contents.add("import javafx.scene.control.Button;");73contents.add("import javafx.scene.layout.StackPane;");74contents.add("import javafx.stage.Stage;");75contents.add("public class HelloWorld extends Application {");76contents.add(mainmethod);77contents.add("@Override");78contents.add("public void start(Stage primaryStage) {");79contents.add(" primaryStage.setTitle(\"Hello World!\");");80contents.add(" Button btn = new Button();");81contents.add(" btn.setText(\"Say 'Hello World'\");");82contents.add(" btn.setOnAction(new EventHandler<ActionEvent>() {");83contents.add(" @Override");84contents.add(" public void handle(ActionEvent event) {");85contents.add(" System.out.println(\"Hello World!\");");86contents.add(" }");87contents.add(" });");88contents.add(" StackPane root = new StackPane();");89contents.add(" root.getChildren().add(btn);");90contents.add(" primaryStage.setScene(new Scene(root, 300, 250));");91contents.add("// primaryStage.show(); no GUI for auto tests. ");92contents.add(" System.out.println(\"HelloWorld.primaryStage.show();\");");93contents.add(" System.out.println(\"Parameters:\");" );94contents.add(" for(String p : getParameters().getUnnamed())");95contents.add(" System.out.println(\"parameter: \" + p );" );96contents.add(" System.exit(0);");97contents.add("}");98contents.add("}");99100// Create and compile java source.101MainJavaFile = new File(mainClass + JAVA_FILE_EXT);102createFile(MainJavaFile, contents);103compile("-d", ".", mainClass + JAVA_FILE_EXT);104} catch (java.io.IOException ioe) {105ioe.printStackTrace();106throw new RuntimeException("Failed creating HelloWorld.");107}108}109110/*111* Create class that extends HelloWorld instead of Application112*/113static void createExtJavaFile(String mainmethod) {114try {115String mainClass = "ExtHello";116List<String> contents = new ArrayList<>();117contents.add("package helloworld;");118contents.add("public class ExtHello extends HelloWorld {");119contents.add(mainmethod);120contents.add("}");121// Create and compile java source.122MainJavaFile = new File(mainClass + JAVA_FILE_EXT);123createFile(MainJavaFile, contents);124compile("-cp", ".", "-d", ".", mainClass + JAVA_FILE_EXT);125} catch (java.io.IOException ioe) {126ioe.printStackTrace();127throw new RuntimeException("Failed creating ExtHello.");128}129}130131/*132* Create non-JavaFX class for testing if jfxrt.jar is being loaded133* when it shouldn't be134*/135static void createNonFXJavaFile() {136try {137String mainClass = "HelloJava";138List<String> contents = new ArrayList<>();139contents.add("package helloworld;");140contents.add("public class HelloJava {");141contents.add(" public static void main(String[] args) {");142contents.add(" for(String aa : args)");143contents.add(" System.out.println(\"arg: \" + aa);" );144contents.add(" }");145contents.add("}");146// Create and compile java source.147MainJavaFile = new File(mainClass + JAVA_FILE_EXT);148createFile(MainJavaFile, contents);149compile("-cp", ".", "-d", ".", mainClass + JAVA_FILE_EXT);150} catch (java.io.IOException ioe) {151ioe.printStackTrace();152throw new RuntimeException("Failed creating HelloJava.");153}154}155156// Create manifest for test fx application157static List<String> createManifestContents(String mainClassEntry, String fxMainEntry) {158List<String> mcontents = new ArrayList<>();159mcontents.add("Manifest-Version: 1.0");160mcontents.add("Created-By: FXLauncherTest");161if (mainClassEntry != null) {162mcontents.add("Main-Class: " + mainClassEntry);163System.out.println("Main-Class: " + mainClassEntry);164}165if (fxMainEntry != null) {166mcontents.add("JavaFX-Application-Class: " + fxMainEntry);167System.out.println("JavaFX-Application-Class: " + fxMainEntry);168}169return mcontents;170}171172// Method to marshal createJar to TestHelper.createJar()173static void createJar(File theJar, File manifestFile) {174createJar("cvmf", manifestFile.getName(),175theJar.getAbsolutePath(), "helloworld");176}177178static void saveFile(String tname, int testcount, File srcFile) {179File newFile = new File(tname + "-" + testcount + "-" + srcFile.getName());180System.out.println("renaming " + srcFile.getName() +181" to " + newFile.getName());182srcFile.renameTo(newFile);183}184185static void cleanupFiles() throws IOException {186for(File f : ScratchDir.listFiles()) {187recursiveDelete(f);188}189}190191static void checkStatus(TestResult tr, String testName, int testCount,192String mainclass) throws Exception {193if (tr.testStatus) {194System.out.println("PASS: " + testName + ":" + testCount +195" : test with " + mainclass);196cleanupFiles();197} else {198saveFile(testName, testcount, FXtestJar);199System.out.println("FAIL: " + testName + ":" + testCount +200" : test with " + mainclass);201cleanupFiles();202System.err.println(tr);203throw new Exception("Failed: " + testName + ":" + testCount);204}205}206207/*208* Set Main-Class and iterate main_methods.209* Try launching with both -jar and -cp methods, with and without FX main210* class manifest entry.211* All cases should run.212*213* See sun.launcher.LauncherHelper$FXHelper for more details on how JavaFX214* applications are launched.215*/216@Test217static void testBasicFXApp() throws Exception {218testBasicFXApp(true, false); // -cp, no JAC219testBasicFXApp(false, true); // -jar, with JAC220testBasicFXApp(false, false); // -jar, no JAC221}222223static void testBasicFXApp(boolean useCP, boolean setFXMainClass) throws Exception {224String testname = "testBasicFXApp";225if (useCP) {226testname = testname.concat("_useCP");227}228String fxMC = StdMainClass;229if (!setFXMainClass) {230testname = testname.concat("_noJAC");231fxMC = null;232}233for (String mm : MAIN_METHODS) {234testcount++;235line();236System.out.println("test# " + testcount + "- Main method: " + mm);237createJavaFile(mm);238createFile(ManifestFile, createManifestContents(StdMainClass, fxMC));239createJar(FXtestJar, ManifestFile);240String sTestJar = FXtestJar.getAbsolutePath();241TestResult tr;242if (useCP) {243tr = doExec(javaCmd, "-cp", sTestJar, StdMainClass, APP_PARMS[0], APP_PARMS[1]);244} else {245tr = doExec(javaCmd, "-jar", sTestJar, APP_PARMS[0], APP_PARMS[1]);246}247tr.checkPositive();248if (tr.testStatus && tr.contains("HelloWorld.primaryStage.show()")) {249for (String p : APP_PARMS) {250if (!tr.contains(p)) {251System.err.println("ERROR: Did not find "252+ p + " in output!");253}254}255}256checkStatus(tr, testname, testcount, StdMainClass);257}258}259260/*261* Set Main-Class and iterate main methods.262* Main class extends another class that extends Application.263* Try launching with both -jar and -cp methods.264* All cases should run.265*/266@Test267static void testExtendFXApp() throws Exception {268testExtendFXApp(true, false); // -cp, no JAC269testExtendFXApp(false, true); // -jar, with JAC270testExtendFXApp(false, false); // -jar, no JAC271}272273static void testExtendFXApp(boolean useCP, boolean setFXMainClass) throws Exception {274String testname = "testExtendFXApp";275if (useCP) {276testname = testname.concat("_useCP");277}278String fxMC = ExtMainClass;279if (!setFXMainClass) {280testname = testname.concat("_noJAC");281fxMC = null;282}283for (String mm : MAIN_METHODS) {284testcount++;285line();286System.out.println("test# " + testcount + "- Main method: " + mm);287createJavaFile(mm);288createExtJavaFile(mm);289createFile(ManifestFile, createManifestContents(ExtMainClass, fxMC));290createJar(FXtestJar, ManifestFile);291String sTestJar = FXtestJar.getAbsolutePath();292TestResult tr;293if (useCP) {294tr = doExec(javaCmd, "-cp", sTestJar, ExtMainClass, APP_PARMS[0], APP_PARMS[1]);295} else {296tr = doExec(javaCmd, "-jar", sTestJar, APP_PARMS[0], APP_PARMS[1]);297}298tr.checkPositive();299if (tr.testStatus && tr.contains("HelloWorld.primaryStage.show()")) {300for (String p : APP_PARMS) {301if (!tr.contains(p)) {302System.err.println("ERROR: Did not find "303+ p + " in output!");304}305}306}307checkStatus(tr, testname, testcount, ExtMainClass);308}309}310311/*312* Ensure we can NOT launch a FX app jar with no Main-Class manifest entry313*/314@Test315static void testMissingMC() throws Exception {316String testname = "testMissingMC";317testcount++;318line();319System.out.println("test# " + testcount + ": abort on missing Main-Class");320createJavaFile(" "); // no main() needed321createFile(ManifestFile, createManifestContents(null, StdMainClass)); // No MC, but supply JAC322createJar(FXtestJar, ManifestFile);323String sTestJar = FXtestJar.getAbsolutePath();324TestResult tr = doExec(javaCmd, "-jar", sTestJar, APP_PARMS[0], APP_PARMS[1]);325tr.checkNegative(); // should abort if no Main-Class326if (tr.testStatus) {327if (!tr.contains("no main manifest attribute")) {328System.err.println("ERROR: launcher did not abort properly");329}330} else {331System.err.println("ERROR: jar executed with no Main-Class!");332}333checkStatus(tr, testname, testcount, StdMainClass);334}335336/*337* test to ensure that we don't load any extraneous fx jars when338* launching a standard java application339* Test both -cp and -jar methods since they use different code paths.340* Neither case should cause jfxrt.jar to be loaded.341*/342@Test343static void testExtraneousJars() throws Exception {344testExtraneousJars(true);345testExtraneousJars(false);346}347348static void testExtraneousJars(boolean useCP) throws Exception {349String testname = "testExtraneousJars";350if (useCP) {351testname = testname.concat("_useCP");352}353testcount++;354line();355System.out.println("test# " + testcount356+ ": test for erroneous jfxrt.jar loading");357createNonFXJavaFile();358createFile(ManifestFile, createManifestContents(NonFXMainClass, null));359createJar(FXtestJar, ManifestFile);360String sTestJar = FXtestJar.getAbsolutePath();361TestResult tr;362363if (useCP) {364tr = doExec(javaCmd, "-verbose:class", "-cp", sTestJar, NonFXMainClass, APP_PARMS[0], APP_PARMS[1]);365} else {366tr = doExec(javaCmd, "-verbose:class", "-jar", sTestJar, APP_PARMS[0], APP_PARMS[1]);367}368tr.checkPositive();369if (tr.testStatus) {370if (!tr.notContains("jfxrt.jar")) {371System.out.println("testing for extraneous jfxrt jar");372System.out.println(tr);373throw new Exception("jfxrt.jar is being loaded, it should not be!");374}375for (String p : APP_PARMS) {376if (!tr.contains(p)) {377System.err.println("ERROR: Did not find "378+ p + " in output!");379}380}381}382checkStatus(tr, testname, testcount, NonFXMainClass);383}384385public static void main(String... args) throws Exception {386//check if fx is part of jdk387Class<?> fxClass = null;388try {389fxClass = Class.forName(FX_MARKER_CLASS);390} catch (ClassNotFoundException ex) {391// do nothing392}393if (fxClass != null) {394FXLauncherTest fxt = new FXLauncherTest();395fxt.run(args);396if (testExitValue > 0) {397System.out.println("Total of " + testExitValue398+ " failed. Test cases covered: "399+ FXLauncherTest.testcount);400System.exit(1);401} else {402System.out.println("All tests pass. Test cases covered: "403+ FXLauncherTest.testcount);404}405} else {406System.err.println("Warning: JavaFX components missing or not supported");407System.err.println(" test passes vacuously.");408}409}410}411412413