1 /*
   2  * Copyright (c) 2012, 2016, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package test.util;
  27 
  28 import java.io.BufferedReader;
  29 import java.io.BufferedWriter;
  30 import java.io.File;
  31 import java.io.FileReader;
  32 import java.io.FileWriter;
  33 import java.io.IOException;
  34 import java.net.URL;
  35 import java.util.ArrayList;
  36 import java.util.Iterator;
  37 import java.util.List;
  38 import java.util.concurrent.CountDownLatch;
  39 import java.util.concurrent.TimeUnit;
  40 import javafx.application.Platform;
  41 import junit.framework.AssertionFailedError;
  42 import org.junit.Assert;
  43 
  44 /**
  45  * Utility methods for life-cycle testing
  46  */
  47 public class Util {
  48 
  49     // Test timeout value in milliseconds
  50     public static final int TIMEOUT = 5000;
  51 
  52     private static interface Future {
  53         public abstract boolean await(long timeout, TimeUnit unit);
  54     }
  55 
  56     public static void throwError(Throwable testError) {
  57         if (testError != null) {
  58             if (testError instanceof Error) {
  59                 throw (Error)testError;
  60             } else if (testError instanceof RuntimeException) {
  61                 throw (RuntimeException)testError;
  62             } else {
  63                 AssertionFailedError err = new AssertionFailedError("Unknown exception");
  64                 err.initCause(testError.getCause());
  65                 throw err;
  66             }
  67         } else {
  68             AssertionFailedError err = new AssertionFailedError("Unexpected exception");
  69             throw err;
  70         }
  71     }
  72 
  73     public static void sleep(long msec) {
  74         try {
  75             Thread.sleep(msec);
  76         } catch (InterruptedException ex) {}
  77     }
  78 
  79     private static Future submit(final Runnable r, final CountDownLatch delayLatch) {
  80         final Throwable[] testError = new Throwable[1];
  81         final CountDownLatch latch = new CountDownLatch(1);
  82 
  83         Platform.runLater(() -> {
  84             try {
  85                 if (delayLatch != null) {
  86                     delayLatch.await();
  87                 }
  88                 r.run();
  89             } catch (Throwable th) {
  90                 testError[0] = th;
  91             } finally {
  92                 latch.countDown();
  93             }
  94         });
  95 
  96         Future future = (timeout, unit) -> {
  97             try {
  98                 if (!latch.await(timeout, unit)) {
  99                     return false;
 100                 }
 101             } catch (InterruptedException ex) {
 102                 AssertionFailedError err = new AssertionFailedError("Unexpected exception");
 103                 err.initCause(ex);
 104                 throw err;
 105             }
 106 
 107             if (testError[0] != null) {
 108                 if (testError[0] instanceof Error) {
 109                     throw (Error)testError[0];
 110                 } else if (testError[0] instanceof RuntimeException) {
 111                     throw (RuntimeException)testError[0];
 112                 } else {
 113                     AssertionFailedError err = new AssertionFailedError("Unknown execution exception");
 114                     err.initCause(testError[0].getCause());
 115                     throw err;
 116                 }
 117             }
 118 
 119             return true;
 120         };
 121 
 122         return future;
 123     }
 124 
 125     public static void runAndWait(Runnable... runnables) {
 126         runAndWait(false, runnables);
 127     }
 128 
 129     public static void runAndWait(boolean delay, Runnable... runnables) {
 130         List<Future> futures = new ArrayList(runnables.length);
 131         int i = 0;
 132         CountDownLatch delayLatch = delay ? new CountDownLatch(1) : null;
 133         for (Runnable r : runnables) {
 134             futures.add(submit(r, delayLatch));
 135         }
 136         if (delayLatch != null) {
 137             delayLatch.countDown();
 138         }
 139 
 140         int count = TIMEOUT / 100;
 141         while (!futures.isEmpty() && count-- > 0) {
 142             Iterator<Future> it = futures.iterator();
 143             while (it.hasNext()) {
 144                 Future future = it.next();
 145                 if (future.await(0, TimeUnit.MILLISECONDS)) {
 146                     it.remove();
 147                 }
 148             }
 149             if (!futures.isEmpty()) {
 150                 Util.sleep(100);
 151             }
 152         }
 153 
 154         if (!futures.isEmpty()) {
 155             throw new AssertionFailedError("Exceeded timeout limit of " + TIMEOUT + " msec");
 156         }
 157     }
 158 
 159     private static String getJfxrtDir(final String classpath) {
 160         final String jfxrt = "jfxrt.jar";
 161         String cp = classpath;
 162         int idx = cp.replace('\\', '/').indexOf("/" + jfxrt);
 163         if (idx >= 0) {
 164             cp = cp.substring(0, idx);
 165             idx = cp.lastIndexOf(File.pathSeparator);
 166             if (idx >= 0) {
 167                 cp = cp.substring(idx, cp.length());
 168             }
 169             return cp;
 170         }
 171         return null;
 172     }
 173         
 174     public static ArrayList<String> createApplicationLaunchCommand(
 175             String testAppName,
 176             String testPldrName,
 177             String testPolicy) throws IOException {
 178         
 179         final String classpath = System.getProperty("java.class.path");
 180         final String libraryPath = System.getProperty("java.library.path");
 181 
 182         final String workerJavaCmd = System.getProperty("worker.java.cmd");
 183         final Boolean workerDebug = Boolean.getBoolean("worker.debug");
 184 
 185         final ArrayList<String> cmd = new ArrayList<>(30);
 186 
 187         if (workerJavaCmd != null) {
 188             cmd.add(workerJavaCmd);
 189         } else {
 190             cmd.add("java");
 191         }
 192 
 193         String jfxdir = getJfxrtDir(classpath);
 194         Assert.assertNotNull("failed to find jfxdir",jfxdir);
 195         cmd.add("-Djava.ext.dirs=" + jfxdir);
 196 
 197         cmd.add("-cp");
 198         cmd.add(classpath);
 199 
 200         if (testPldrName != null) {
 201             cmd.add("-Djavafx.preloader=" + testPldrName);
 202         }
 203 
 204         if (testPolicy != null) {
 205             cmd.add("-Djava.security.manager");
 206             cmd.add("-Djava.security.policy=" + testPolicy);
 207         }
 208 
 209         cmd.add(testAppName);
 210 
 211         if (workerDebug) {
 212             System.err.println("Child cmd is");
 213             cmd.stream().forEach((s) -> {
 214                 System.err.println(" " + s);
 215             });
 216             System.err.println("Child cmd: end");
 217         }
 218 
 219         return cmd;
 220     }
 221 }