1 /*
   2  * Copyright (c) 2010, 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 /* @test
  25  * @key headful
  26  * @summary Test J2Ddemo.jar
  27  * @run main/timeout=200 J2DdemoTest
  28  */
  29 
  30 import java.io.InputStream;
  31 import java.io.IOException;
  32 import java.io.File;
  33 import java.io.BufferedInputStream;
  34 import java.io.PrintStream;
  35 
  36 public class J2DdemoTest {
  37 
  38     public static void main(String args[]) throws Exception {
  39         DemoRun test;
  40 
  41         /* Run the J2Ddemo.jar with the -runs=1 option */
  42         test = new DemoRun("jfc/J2Ddemo/J2Ddemo.jar", "-runs=1");
  43         test.runit();
  44 
  45         /* Make sure patterns in output look ok */
  46         if (test.output_contains("ERROR")) {
  47             throw new RuntimeException("Test failed - ERROR seen in output");
  48         }
  49         if (test.output_contains("Exception")) {
  50             throw new RuntimeException("Test failed - Exception seen in output");
  51         }
  52 
  53         /* Must be a pass. */
  54         System.out.println("Test passed - cleanly terminated");
  55     }
  56 
  57     /*
  58      * Helper class to direct process output to a StringBuffer
  59      */
  60     static class MyInputStream implements Runnable {
  61         private String              name;
  62         private BufferedInputStream in;
  63         private StringBuffer        buffer;
  64 
  65         /* Create MyInputStream that saves all output to a StringBuffer */
  66         MyInputStream(String name, InputStream in) {
  67             this.name = name;
  68             this.in = new BufferedInputStream(in);
  69             buffer = new StringBuffer(4096);
  70             Thread thr = new Thread(this);
  71             thr.setDaemon(true);
  72             thr.start();
  73         }
  74 
  75         /* Dump the buffer */
  76         void dump(PrintStream x) {
  77             String str = buffer.toString();
  78             x.println("<beginning of " + name + " buffer>");
  79             x.println(str);
  80             x.println("<end of buffer>");
  81         }
  82 
  83         /* Check to see if a pattern is inside the output. */
  84         boolean contains(String pattern) {
  85             String str = buffer.toString();
  86             return str.contains(pattern);
  87         }
  88 
  89         /* Runs as a separate thread capturing all output in a StringBuffer */
  90         public void run() {
  91             try {
  92                 byte b[] = new byte[100];
  93                 for (;;) {
  94                     int n = in.read(b);
  95                     String str;
  96                     if (n < 0) {
  97                         break;
  98                     }
  99                     str = new String(b, 0, n);
 100                     buffer.append(str);
 101                     System.out.print(str);
 102                 }
 103             } catch (IOException ioe) { /* skip */ }
 104         }
 105     }
 106 
 107     /*
 108      * Generic run of a demo jar file.
 109      */
 110     static class DemoRun {
 111 
 112         private String        demo_name;
 113         private String        demo_options;
 114         private MyInputStream output;
 115         private MyInputStream error;
 116 
 117         /* Create a Demo run process */
 118         public DemoRun(String name, String options)
 119         {
 120             demo_name    = name;
 121             demo_options = options;
 122         }
 123 
 124         /*
 125          * Execute the demo
 126          */
 127         public void runit()
 128         {
 129             String jre_home  = System.getProperty("java.home");
 130             String sdk_home  = (jre_home.endsWith("jre") ?
 131                                 (jre_home + File.separator + "..") :
 132                                 jre_home );
 133             String java      = sdk_home
 134                                  + File.separator + "bin"
 135                                  + File.separator + "java";
 136 
 137             /* VM options */
 138             String vm_opts[] = new String[0];
 139             String vopts = System.getProperty("test.vm.opts");
 140             if ( vopts != null && vopts.length()>0 ) {
 141                 vm_opts = vopts.split("\\p{Space}+");
 142             } else {
 143                 vm_opts = new String[0];
 144             }
 145 
 146             /* Command line */
 147             String cmd[] = new String[1 + vm_opts.length + 3];
 148             String cmdLine;
 149             int i;
 150 
 151             i = 0;
 152             cmdLine = "";
 153             cmdLine += (cmd[i++] = java);
 154             cmdLine += " ";
 155             for ( String vopt : vm_opts ) {
 156                 cmdLine += (cmd[i++] = vopt);
 157                 cmdLine += " ";
 158             }
 159             cmdLine += (cmd[i++] = "-jar");
 160             cmdLine += " ";
 161             String demo_path;
 162             String test_dir = System.getenv("TEST_IMAGE_DIR");
 163             System.out.println("TEST_IMAGE_DIR="+test_dir);
 164             if (test_dir != null) {
 165                 demo_path = test_dir + File.separator + "jdk" + File.separator +
 166                             "demos" + File.separator + demo_name;
 167             } else {
 168                 demo_path = sdk_home + File.separator +
 169                             "demo" + File.separator + demo_name;
 170             }
 171             System.out.println("demo_path="+demo_path);
 172             cmdLine += cmd[i++] = demo_path;
 173             cmdLine += " ";
 174             cmdLine += (cmd[i++] = demo_options);
 175 
 176             /* Begin process */
 177             Process p;
 178 
 179             System.out.println("Starting: " + cmdLine);
 180             try {
 181                 p = Runtime.getRuntime().exec(cmd);
 182             } catch ( IOException e ) {
 183                 throw new RuntimeException("Test failed - exec got IO exception");
 184             }
 185 
 186             /* Save process output in StringBuffers */
 187             output = new MyInputStream("Input Stream", p.getInputStream());
 188             error  = new MyInputStream("Error Stream", p.getErrorStream());
 189 
 190             /* Wait for process to complete, and if exit code is non-zero we fail */
 191             int exitStatus;
 192             try {
 193                 exitStatus = p.waitFor();
 194                 if ( exitStatus != 0) {
 195                     System.out.println("Exit code is " + exitStatus);
 196                     error.dump(System.out);
 197                     output.dump(System.out);
 198                     throw new RuntimeException("Test failed - " +
 199                                         "exit return code non-zero " +
 200                                         "(exitStatus==" + exitStatus + ")");
 201                 }
 202             } catch ( InterruptedException e ) {
 203                 throw new RuntimeException("Test failed - process interrupted");
 204             }
 205             System.out.println("Completed: " + cmdLine);
 206         }
 207 
 208         /* Does the pattern appear in the output of this process */
 209         public boolean output_contains(String pattern)
 210         {
 211             return output.contains(pattern) || error.contains(pattern);
 212         }
 213     }
 214 
 215 }
 216