1 /*
   2  * Copyright (c) 2006, 2018, 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 /**
  25  * @test
  26  * @bug 6358034 6568560 8198613
  27  * @key headful
  28  * @summary Tests that no exception is thrown when display mode is changed
  29  *          externally
  30  * @compile UninitializedDisplayModeChangeTest.java DisplayModeChanger.java
  31  * @run main/othervm UninitializedDisplayModeChangeTest
  32  */
  33 
  34 import java.awt.EventQueue;
  35 import java.awt.Toolkit;
  36 import java.io.BufferedReader;
  37 import java.io.File;
  38 import java.io.IOException;
  39 import java.io.InputStream;
  40 import java.io.InputStreamReader;
  41 import java.lang.reflect.InvocationTargetException;
  42 
  43 public class UninitializedDisplayModeChangeTest {
  44     public static volatile boolean failed = false;
  45     public static void main(String[] args) {
  46         Toolkit.getDefaultToolkit();
  47         try {
  48             EventQueue.invokeAndWait(new Runnable() {
  49                 public void run() {
  50                     Thread.currentThread().setDefaultUncaughtExceptionHandler(
  51                         new Thread.UncaughtExceptionHandler() {
  52                             public void uncaughtException(Thread t,
  53                                                           Throwable e)
  54                             {
  55                                 System.err.println("Exception Detected:");
  56                                 e.printStackTrace();
  57                                 failed = true;
  58                             }
  59                         }
  60                     );
  61                 }
  62             });
  63         } catch (InterruptedException ex) {
  64             ex.printStackTrace();
  65         } catch (InvocationTargetException ex) {
  66             ex.printStackTrace();
  67         }
  68 
  69         Process childProc;
  70         String classPath = System.getProperty("java.class.path" , ".");
  71         String cmd = new String(System.getProperty("java.home") +
  72                 File.separator +
  73                 "bin" +
  74                 File.separator +
  75                 "java -cp " + classPath +
  76                 " DisplayModeChanger");
  77 
  78         System.out.println("Launching the display mode changer process");
  79         System.out.println("cmd="+cmd);
  80         try {
  81             childProc = Runtime.getRuntime().exec(cmd);
  82             StreamProcessor err =
  83                 new StreamProcessor("stderr", childProc.getErrorStream());
  84             StreamProcessor out =
  85                 new StreamProcessor("stdout", childProc.getInputStream());
  86             err.start();
  87             out.start();
  88 
  89             childProc.waitFor();
  90         } catch (Exception e) {
  91             failed = true;
  92             e.printStackTrace();
  93         }
  94 
  95         if (failed) {
  96             throw new RuntimeException("Test Failed: exception detected");
  97         }
  98         System.out.println("Test Passed.");
  99     }
 100 
 101     static class StreamProcessor extends Thread {
 102         InputStream is;
 103         String inputType;
 104         StreamProcessor(String inputType, InputStream is) {
 105             this.inputType = inputType;
 106             this.is = is;
 107         }
 108         public void run() {
 109             try {
 110                 InputStreamReader isr = new InputStreamReader(is);
 111                 BufferedReader br = new BufferedReader(isr);
 112                 String line = null;
 113                 while ( (line = br.readLine()) != null) {
 114                     System.out.println("Display Changer "+inputType+
 115                                        " output > " + line);
 116                 }
 117             } catch (IOException ioe) {
 118                 ioe.printStackTrace();
 119             }
 120         }
 121     }
 122 }