< prev index next >

test/jdk/tools/launcher/FXLauncherTest.java

Print this page
rev 49939 : 8202553: Update FXLauncherTest as part of removing JavaFX from JDK
Reviewed-by:

@@ -21,33 +21,46 @@
  * questions.
  */
 
 /*
  * @test
- * @bug 8001533 8004547 8035782
+ * @library /test/lib
+ * @build FXLauncherTest jdk.test.lib.compiler.CompilerUtils
+ * @bug 8001533 8004547 8035782 8202553
  * @summary Test launching FX application with java -jar
- * Test uses main method and blank main method, a jfx app class and an incorrest
+ * Test uses main method and blank main method, a jfx app class and an incorrect
  * jfx app class, a main-class for the manifest, a bogus one and none.
+ * Now that FX is no longer bundled with the JDK, this test uses a
+ * "mock" javafx.graphics module to test the FX launcher. It also verifies
+ * that FX is, in fact, not included with the JDK.
  * All should execute except the incorrect fx app class entries.
  * @run main/othervm FXLauncherTest
- * @key intermittent headful
  */
 import java.io.File;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
+import jdk.test.lib.compiler.CompilerUtils;
+
 public class FXLauncherTest extends TestHelper {
     private static final String FX_MARKER_CLASS = "javafx.application.Application";
     private static void line() {
         System.out.println("_____________________________________________");
     }
     private static File MainJavaFile = null;
     private static final File FXtestJar =  new File("fxtest.jar");
     private static final File ManifestFile = new File("manifest.txt");
     private static final File ScratchDir = new File(".");
 
+    private static final String TEST_SRC = System.getProperty("test.src");
+    private static final Path SRC_DIR = Paths.get(TEST_SRC, "mockfx/src");
+    private static final Path MODS_DIR = Paths.get("mods");
+    private static String moduleDir = MODS_DIR.toString();
+
     /* standard main class can be used as java main for fx app class */
     static final String StdMainClass = "helloworld.HelloWorld";
     static final String ExtMainClass = "helloworld.ExtHello";
     static final String NonFXMainClass = "helloworld.HelloJava";
     static int testcount = 0;

@@ -66,32 +79,15 @@
         try {
             String mainClass = "HelloWorld";
             List<String> contents = new ArrayList<>();
             contents.add("package helloworld;");
             contents.add("import javafx.application.Application;");
-            contents.add("import javafx.event.ActionEvent;");
-            contents.add("import javafx.event.EventHandler;");
-            contents.add("import javafx.scene.Scene;");
-            contents.add("import javafx.scene.control.Button;");
-            contents.add("import javafx.scene.layout.StackPane;");
             contents.add("import javafx.stage.Stage;");
             contents.add("public class HelloWorld extends Application {");
             contents.add(mainmethod);
             contents.add("@Override");
             contents.add("public void start(Stage primaryStage) {");
-            contents.add("    primaryStage.setTitle(\"Hello World!\");");
-            contents.add("    Button btn = new Button();");
-            contents.add("    btn.setText(\"Say 'Hello World'\");");
-            contents.add("    btn.setOnAction(new EventHandler<ActionEvent>() {");
-            contents.add("        @Override");
-            contents.add("        public void handle(ActionEvent event) {");
-            contents.add("            System.out.println(\"Hello World!\");");
-            contents.add("        }");
-            contents.add("    });");
-            contents.add("    StackPane root = new StackPane();");
-            contents.add("    root.getChildren().add(btn);");
-            contents.add("    primaryStage.setScene(new Scene(root, 300, 250));");
             contents.add("//    primaryStage.show(); no GUI for auto tests. ");
             contents.add("    System.out.println(\"HelloWorld.primaryStage.show();\");");
             contents.add("    System.out.println(\"Parameters:\");" );
             contents.add("    for(String p : getParameters().getUnnamed())");
             contents.add("        System.out.println(\"parameter: \" + p );" );

@@ -100,11 +96,11 @@
             contents.add("}");
 
             // Create and compile java source.
             MainJavaFile = new File(mainClass + JAVA_FILE_EXT);
             createFile(MainJavaFile, contents);
-            compile("-d", ".", mainClass + JAVA_FILE_EXT);
+            doFxCompile("-d", ".", mainClass + JAVA_FILE_EXT);
         } catch (java.io.IOException ioe) {
             ioe.printStackTrace();
             throw new RuntimeException("Failed creating HelloWorld.");
         }
     }

@@ -121,11 +117,11 @@
             contents.add(mainmethod);
             contents.add("}");
             // Create and compile java source.
             MainJavaFile = new File(mainClass + JAVA_FILE_EXT);
             createFile(MainJavaFile, contents);
-            compile("-cp", ".", "-d", ".", mainClass + JAVA_FILE_EXT);
+            doFxCompile("-cp", ".", "-d", ".", mainClass + JAVA_FILE_EXT);
         } catch (java.io.IOException ioe) {
             ioe.printStackTrace();
             throw new RuntimeException("Failed creating ExtHello.");
         }
     }

@@ -146,11 +142,11 @@
             contents.add("    }");
             contents.add("}");
             // Create and compile java source.
             MainJavaFile = new File(mainClass + JAVA_FILE_EXT);
             createFile(MainJavaFile, contents);
-            compile("-cp", ".", "-d", ".", mainClass + JAVA_FILE_EXT);
+            doFxCompile("-cp", ".", "-d", ".", mainClass + JAVA_FILE_EXT);
         } catch (java.io.IOException ioe) {
             ioe.printStackTrace();
             throw new RuntimeException("Failed creating HelloJava.");
         }
     }

@@ -204,10 +200,46 @@
             System.err.println(tr);
             throw new Exception("Failed: " + testName + ":" + testCount);
         }
     }
 
+    public static void compileFXModule() {
+        final String JAVAFX_GRAPHICS_MODULE = "javafx.graphics";
+
+        try {
+            // javac -d mods/javafx.graphics mockfx/src/javafx.graphics/**
+            boolean compiled
+                = CompilerUtils.compile(SRC_DIR.resolve(JAVAFX_GRAPHICS_MODULE),
+                                        MODS_DIR.resolve(JAVAFX_GRAPHICS_MODULE));
+
+            if (!compiled) {
+                throw new RuntimeException("Error compiling mock javafx.graphics module");
+            }
+        } catch (IOException ioe) {
+            throw new RuntimeException(ioe);
+        }
+    }
+
+    static void doFxCompile(String...compilerArgs) {
+        compileFXModule();
+
+        String[] fxCompilerArgs = new String[compilerArgs.length + 2];
+        fxCompilerArgs[0] = "--module-path=" + moduleDir;
+        fxCompilerArgs[1] = "--add-modules=javafx.graphics";
+        System.arraycopy(compilerArgs, 0, fxCompilerArgs, 2, compilerArgs.length);
+        compile(fxCompilerArgs);
+    }
+
+    static TestResult doFxExec(String...cmds) {
+        String[] fxCmds = new String[cmds.length + 2];
+        fxCmds[0] = cmds[0];
+        fxCmds[1] = "--module-path=" + moduleDir;
+        fxCmds[2] = "--add-modules=javafx.graphics";
+        System.arraycopy(cmds, 1, fxCmds, 3, cmds.length - 1);
+        return doExec(fxCmds);
+    }
+
     /*
      * Set Main-Class and iterate main_methods.
      * Try launching with both -jar and -cp methods, with and without FX main
      * class manifest entry.
      * All cases should run.

@@ -240,13 +272,13 @@
             createFile(ManifestFile, createManifestContents(StdMainClass, fxMC));
             createJar(FXtestJar, ManifestFile);
             String sTestJar = FXtestJar.getAbsolutePath();
             final TestResult tr;
             if (useCP) {
-                tr = doExec(javaCmd, "-cp", sTestJar, StdMainClass, APP_PARMS[0], APP_PARMS[1]);
+                tr = doFxExec(javaCmd, "-cp", sTestJar, StdMainClass, APP_PARMS[0], APP_PARMS[1]);
             } else {
-                tr = doExec(javaCmd, "-jar", sTestJar, APP_PARMS[0], APP_PARMS[1]);
+                tr = doFxExec(javaCmd, "-jar", sTestJar, APP_PARMS[0], APP_PARMS[1]);
             }
             tr.checkPositive();
             if (tr.testStatus && tr.contains("HelloWorld.primaryStage.show()")) {
                 for (String p : APP_PARMS) {
                     if (!tr.contains(p)) {

@@ -291,13 +323,13 @@
             createFile(ManifestFile, createManifestContents(ExtMainClass, fxMC));
             createJar(FXtestJar, ManifestFile);
             String sTestJar = FXtestJar.getAbsolutePath();
             final TestResult tr;
             if (useCP) {
-                tr = doExec(javaCmd, "-cp", sTestJar, ExtMainClass, APP_PARMS[0], APP_PARMS[1]);
+                tr = doFxExec(javaCmd, "-cp", sTestJar, ExtMainClass, APP_PARMS[0], APP_PARMS[1]);
             } else {
-                tr = doExec(javaCmd, "-jar", sTestJar, APP_PARMS[0], APP_PARMS[1]);
+                tr = doFxExec(javaCmd, "-jar", sTestJar, APP_PARMS[0], APP_PARMS[1]);
             }
             tr.checkPositive();
             if (tr.testStatus && tr.contains("HelloWorld.primaryStage.show()")) {
                 for (String p : APP_PARMS) {
                     if (!tr.contains(p)) {

@@ -321,11 +353,11 @@
         System.out.println("test# " + testcount + ": abort on missing Main-Class");
         createJavaFile(" "); // no main() needed
         createFile(ManifestFile, createManifestContents(null, StdMainClass)); // No MC, but supply JAC
         createJar(FXtestJar, ManifestFile);
         String sTestJar = FXtestJar.getAbsolutePath();
-        TestResult tr = doExec(javaCmd, "-jar", sTestJar, APP_PARMS[0], APP_PARMS[1]);
+        TestResult tr = doFxExec(javaCmd, "-jar", sTestJar, APP_PARMS[0], APP_PARMS[1]);
         tr.checkNegative(); // should abort if no Main-Class
         if (tr.testStatus) {
             if (!tr.contains("no main manifest attribute")) {
                 System.err.println("ERROR: launcher did not abort properly");
             }

@@ -361,13 +393,13 @@
         createJar(FXtestJar, ManifestFile);
         String sTestJar = FXtestJar.getAbsolutePath();
         final TestResult tr;
 
         if (useCP) {
-            tr = doExec(javaCmd, "-verbose:class", "-cp", sTestJar, NonFXMainClass, APP_PARMS[0], APP_PARMS[1]);
+            tr = doFxExec(javaCmd, "-verbose:class", "-cp", sTestJar, NonFXMainClass, APP_PARMS[0], APP_PARMS[1]);
         } else {
-            tr = doExec(javaCmd, "-verbose:class", "-jar", sTestJar, APP_PARMS[0], APP_PARMS[1]);
+            tr = doFxExec(javaCmd, "-verbose:class", "-jar", sTestJar, APP_PARMS[0], APP_PARMS[1]);
         }
         tr.checkPositive();
         if (tr.testStatus) {
             if (!tr.notContains("jfxrt.jar")) {
                 System.out.println("testing for extraneous jfxrt jar");

@@ -388,18 +420,22 @@
         }
         checkStatus(tr, testname, testcount, NonFXMainClass);
     }
 
     public static void main(String... args) throws Exception {
-        //check if fx is part of jdk
+
+        // Ensure that FX is not part of jdk
         Class<?> fxClass = null;
         try {
             fxClass = Class.forName(FX_MARKER_CLASS);
         } catch (ClassNotFoundException ex) {
             // do nothing
         }
         if (fxClass != null) {
+            throw new RuntimeException("JavaFX modules erroneously included in the JDK");
+        }
+
             FXLauncherTest fxt = new FXLauncherTest();
             fxt.run(args);
             if (testExitValue > 0) {
                 System.out.println("Total of " + testExitValue
                         + " failed. Test cases covered: "

@@ -407,11 +443,8 @@
                 System.exit(1);
             } else {
                 System.out.println("All tests pass. Test cases covered: "
                         + FXLauncherTest.testcount);
             }
-        } else {
-            System.err.println("Warning: JavaFX components missing or not supported");
-            System.err.println("         test passes vacuously.");
-         }
     }
+
 }
< prev index next >