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 import java.awt.Frame;
  24 import java.io.File;
  25 import java.io.IOException;
  26 import java.io.InputStream;
  27 import java.util.concurrent.TimeUnit;
  28 
  29 /**
  30  * @test
  31  * @bug 8081019
  32  * @summary Check peer to null in CPlatformWindow.checkZoom() method
  33  * @author Alexandr Scherbatiy
  34  */
  35 public class bug8081019 {
  36 
  37     private static final String RUN_PROCESS = "RUN_PROCESS";
  38     private static final String RUN_TEST = "RUN_TEST";
  39 
  40     public static void main(String[] args) throws Exception {
  41         String command = RUN_PROCESS;
  42 
  43         if (0 < args.length) {
  44             command = args[0];
  45         }
  46 
  47         switch (command) {
  48             case RUN_PROCESS:
  49                 runProcess();
  50                 break;
  51             case RUN_TEST:
  52                 runTest();
  53                 break;
  54             default:
  55                 throw new RuntimeException("Unknown command: " + command);
  56         }
  57     }
  58 
  59     private static void runTest() throws Exception {
  60         System.setSecurityManager(new SecurityManager());
  61         Frame f = new Frame("Test frame");
  62         f.setVisible(true);
  63         f.setVisible(false);
  64         f.dispose();
  65     }
  66 
  67     private static void runProcess() throws Exception {
  68         String javaPath = System.getProperty("java.home", "");
  69         String command = javaPath + File.separator + "bin" + File.separator + "java"
  70                 + " " + bug8081019.class.getName() + " " + RUN_TEST;
  71 
  72         Process process = Runtime.getRuntime().exec(command);
  73         boolean processExit = process.waitFor(20, TimeUnit.SECONDS);
  74 
  75         dumpStream(process.getErrorStream(), "error stream");
  76         dumpStream(process.getInputStream(), "input stream");
  77 
  78         if (!processExit) {
  79             process.destroy();
  80             throw new RuntimeException(""
  81                     + "The sub process has not exited!");
  82         }
  83     }
  84 
  85     public static void dumpStream(InputStream in, String name) throws IOException {
  86         System.out.println("--- dump " + name + " ---");
  87         String tempString;
  88         int count = in.available();
  89         boolean exception = false;
  90         while (count > 0) {
  91             byte[] b = new byte[count];
  92             in.read(b);
  93             tempString = new String(b);
  94             if (!exception) {
  95                 exception = tempString.indexOf("Exception") != -1
  96                         || tempString.indexOf("Error") != -1;
  97             }
  98             System.out.println(tempString);
  99             count = in.available();
 100         }
 101 
 102         if (exception) {
 103             throw new RuntimeException("Exception in the output!");
 104         }
 105     }
 106 }