Path: blob/master/test/jdk/demo/jfc/J2Ddemo/J2DdemoTest.java
41149 views
/*1* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @key headful25* @summary Test J2Ddemo.jar26* @run main/timeout=200 J2DdemoTest27*/2829import java.io.InputStream;30import java.io.IOException;31import java.io.File;32import java.io.BufferedInputStream;33import java.io.PrintStream;3435public class J2DdemoTest {3637public static void main(String args[]) throws Exception {38DemoRun test;3940/* Run the J2Ddemo.jar with the -runs=1 option */41test = new DemoRun("jfc/J2Ddemo/J2Ddemo.jar", "-runs=1");42test.runit();4344/* Make sure patterns in output look ok */45if (test.output_contains("ERROR")) {46throw new RuntimeException("Test failed - ERROR seen in output");47}48if (test.output_contains("Exception")) {49throw new RuntimeException("Test failed - Exception seen in output");50}5152/* Must be a pass. */53System.out.println("Test passed - cleanly terminated");54}5556/*57* Helper class to direct process output to a StringBuffer58*/59static class MyInputStream implements Runnable {60private String name;61private BufferedInputStream in;62private StringBuffer buffer;6364/* Create MyInputStream that saves all output to a StringBuffer */65MyInputStream(String name, InputStream in) {66this.name = name;67this.in = new BufferedInputStream(in);68buffer = new StringBuffer(4096);69Thread thr = new Thread(this);70thr.setDaemon(true);71thr.start();72}7374/* Dump the buffer */75void dump(PrintStream x) {76String str = buffer.toString();77x.println("<beginning of " + name + " buffer>");78x.println(str);79x.println("<end of buffer>");80}8182/* Check to see if a pattern is inside the output. */83boolean contains(String pattern) {84String str = buffer.toString();85return str.contains(pattern);86}8788/* Runs as a separate thread capturing all output in a StringBuffer */89public void run() {90try {91byte b[] = new byte[100];92for (;;) {93int n = in.read(b);94String str;95if (n < 0) {96break;97}98str = new String(b, 0, n);99buffer.append(str);100System.out.print(str);101}102} catch (IOException ioe) { /* skip */ }103}104}105106/*107* Generic run of a demo jar file.108*/109static class DemoRun {110111private String demo_name;112private String demo_options;113private MyInputStream output;114private MyInputStream error;115116/* Create a Demo run process */117public DemoRun(String name, String options)118{119demo_name = name;120demo_options = options;121}122123/*124* Execute the demo125*/126public void runit()127{128String jre_home = System.getProperty("java.home");129String sdk_home = (jre_home.endsWith("jre") ?130(jre_home + File.separator + "..") :131jre_home );132String java = sdk_home133+ File.separator + "bin"134+ File.separator + "java";135136/* VM options */137String vm_opts[] = new String[0];138String vopts = System.getProperty("test.vm.opts");139if ( vopts != null && vopts.length()>0 ) {140vm_opts = vopts.split("\\p{Space}+");141} else {142vm_opts = new String[0];143}144145/* Command line */146String cmd[] = new String[1 + vm_opts.length + 3];147String cmdLine;148int i;149150i = 0;151cmdLine = "";152cmdLine += (cmd[i++] = java);153cmdLine += " ";154for ( String vopt : vm_opts ) {155cmdLine += (cmd[i++] = vopt);156cmdLine += " ";157}158cmdLine += (cmd[i++] = "-jar");159cmdLine += " ";160String demo_path;161String test_dir = System.getenv("TEST_IMAGE_DIR");162System.out.println("TEST_IMAGE_DIR="+test_dir);163if (test_dir != null) {164demo_path = test_dir + File.separator + "jdk" + File.separator +165"demos" + File.separator + demo_name;166} else {167demo_path = sdk_home + File.separator +168"demo" + File.separator + demo_name;169}170System.out.println("demo_path="+demo_path);171cmdLine += cmd[i++] = demo_path;172cmdLine += " ";173cmdLine += (cmd[i++] = demo_options);174175/* Begin process */176Process p;177178System.out.println("Starting: " + cmdLine);179try {180p = Runtime.getRuntime().exec(cmd);181} catch ( IOException e ) {182throw new RuntimeException("Test failed - exec got IO exception");183}184185/* Save process output in StringBuffers */186output = new MyInputStream("Input Stream", p.getInputStream());187error = new MyInputStream("Error Stream", p.getErrorStream());188189/* Wait for process to complete, and if exit code is non-zero we fail */190int exitStatus;191try {192exitStatus = p.waitFor();193if ( exitStatus != 0) {194System.out.println("Exit code is " + exitStatus);195error.dump(System.out);196output.dump(System.out);197throw new RuntimeException("Test failed - " +198"exit return code non-zero " +199"(exitStatus==" + exitStatus + ")");200}201} catch ( InterruptedException e ) {202throw new RuntimeException("Test failed - process interrupted");203}204System.out.println("Completed: " + cmdLine);205}206207/* Does the pattern appear in the output of this process */208public boolean output_contains(String pattern)209{210return output.contains(pattern) || error.contains(pattern);211}212}213214}215216217218