|
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("demo/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 cmdLine += (cmd[i++] = sdk_home + File.separator + demo_name); |
|
162 cmdLine += " "; |
|
163 cmdLine += (cmd[i++] = demo_options); |
|
164 |
|
165 /* Begin process */ |
|
166 Process p; |
|
167 |
|
168 System.out.println("Starting: " + cmdLine); |
|
169 try { |
|
170 p = Runtime.getRuntime().exec(cmd); |
|
171 } catch ( IOException e ) { |
|
172 throw new RuntimeException("Test failed - exec got IO exception"); |
|
173 } |
|
174 |
|
175 /* Save process output in StringBuffers */ |
|
176 output = new MyInputStream("Input Stream", p.getInputStream()); |
|
177 error = new MyInputStream("Error Stream", p.getErrorStream()); |
|
178 |
|
179 /* Wait for process to complete, and if exit code is non-zero we fail */ |
|
180 int exitStatus; |
|
181 try { |
|
182 exitStatus = p.waitFor(); |
|
183 if ( exitStatus != 0) { |
|
184 System.out.println("Exit code is " + exitStatus); |
|
185 error.dump(System.out); |
|
186 output.dump(System.out); |
|
187 throw new RuntimeException("Test failed - " + |
|
188 "exit return code non-zero " + |
|
189 "(exitStatus==" + exitStatus + ")"); |
|
190 } |
|
191 } catch ( InterruptedException e ) { |
|
192 throw new RuntimeException("Test failed - process interrupted"); |
|
193 } |
|
194 System.out.println("Completed: " + cmdLine); |
|
195 } |
|
196 |
|
197 /* Does the pattern appear in the output of this process */ |
|
198 public boolean output_contains(String pattern) |
|
199 { |
|
200 return output.contains(pattern) || error.contains(pattern); |
|
201 } |
|
202 } |
|
203 |
|
204 } |
|
205 |