Tue, 25 Jan 2011 17:02:56 -0800
7013420: Project Coin: remove general expression support from try-with-resources statement
Reviewed-by: mcimadamore, jjg
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 */
24 /**
25 * @test
26 * @bug 6968063
27 * @summary provide examples of code that generate diagnostics
28 * @build Example HTMLWriter RunExamples
29 * @run main RunExamples
30 */
32 import java.io.*;
33 import java.text.SimpleDateFormat;
34 import java.util.*;
35 import java.util.regex.Matcher;
36 import java.util.regex.Pattern;
38 /**
39 * Utility to run selected or all examples, writing results to
40 * stdout, a plain text file or an HTML file. This program can be
41 * run standalone, or as a jtreg test.
42 *
43 * Options:
44 * -examples dir directory of examples. Defaults to ${test.src}/examples
45 * -raw run examples with -XDrawDiagnostics
46 * -showFiles include text of source files in the output
47 * -verbose verbose output
48 * -o file write output to file: format will be HTML if
49 * file has .html extension; otherwise it will be plain text.
50 * default is to stdout
51 * -title string specify a title, only applies to HTML output
52 */
53 public class RunExamples {
54 public static void main(String... args) throws Exception {
55 jtreg = (System.getProperty("test.src") != null);
56 File tmpDir;
57 if (jtreg) {
58 // use standard jtreg scratch directory: the current directory
59 tmpDir = new File(System.getProperty("user.dir"));
60 } else {
61 tmpDir = new File(System.getProperty("java.io.tmpdir"),
62 RunExamples.class.getName()
63 + (new SimpleDateFormat("yyMMddHHmmss")).format(new Date()));
64 }
65 Example.setTempDir(tmpDir);
67 RunExamples r = new RunExamples();
69 try {
70 if (r.run(args))
71 return;
72 } finally {
73 /* VERY IMPORTANT NOTE. In jtreg mode, tmpDir is set to the
74 * jtreg scratch directory, which is the current directory.
75 * In case someone is faking jtreg mode, make sure to only
76 * clean tmpDir when it is reasonable to do so.
77 */
78 if (tmpDir.isDirectory() &&
79 tmpDir.getName().startsWith(RunExamples.class.getName())) {
80 if (clean(tmpDir))
81 tmpDir.delete();
82 }
83 }
85 if (jtreg)
86 throw new Exception(r.errors + " errors occurred");
87 else
88 System.exit(1);
89 }
91 boolean run(String... args) {
92 Set<String> selectedKeys = new TreeSet<String>();
93 Set<Example> selectedExamples = new TreeSet<Example>();
94 File testSrc = new File(System.getProperty("test.src", "."));
95 File examplesDir = new File(testSrc, "examples");
96 File outFile = null;
97 boolean raw = false;
98 boolean showFiles = false;
99 boolean verbose = false;
100 String title = null;
102 for (int i = 0; i < args.length; i++) {
103 String arg = args[i];
104 if (arg.equals("-k") && (i + 1) < args.length)
105 selectedKeys.add(args[++i]);
106 else if (arg.equals("-examples") && (i + 1) < args.length)
107 examplesDir = new File(args[++i]);
108 else if (arg.equals("-raw"))
109 raw = true;
110 else if (arg.equals("-showFiles"))
111 showFiles = true;
112 else if (arg.equals("-verbose"))
113 verbose = true;
114 else if (arg.equals("-o") && (i + 1) < args.length)
115 outFile = new File(args[++i]);
116 else if (arg.equals("-title") && (i + 1) < args.length)
117 title = args[++i];
118 else if (arg.startsWith("-")) {
119 error("unknown option: " + arg);
120 return false;
121 } else {
122 while (i < args.length) {
123 File f = new File(examplesDir, args[i]);
124 selectedExamples.add(new Example(f));
125 i++;
126 }
127 }
128 }
130 if (selectedKeys.size() > 0) {
131 Set<Example> examples = getExamples(examplesDir);
132 nextKey:
133 for (String k: selectedKeys) {
134 for (Example e: examples) {
135 if (e.getDeclaredKeys().contains(k))
136 continue nextKey;
137 }
138 error("Key " + k + ": no examples found");
139 }
140 } else {
141 if (selectedExamples.size() == 0)
142 selectedExamples = getExamples(examplesDir);
143 }
145 try {
146 Runner r;
147 if (outFile == null) {
148 PrintWriter out = new PrintWriter(System.out);
149 r = new TextRunner(out, showFiles, raw, verbose);
150 } else if (outFile.getName().endsWith(".html"))
151 r = new HTMLRunner(outFile, showFiles, raw, verbose, title);
152 else
153 r = new TextRunner(outFile, showFiles, raw, verbose);
154 r.run(selectedExamples);
155 r.close();
156 } catch (IOException e) {
157 error("Error writing output: " + e);
158 }
160 return (errors == 0);
161 }
163 /**
164 * Get the complete set of examples to be checked.
165 */
166 Set<Example> getExamples(File examplesDir) {
167 Set<Example> results = new TreeSet<Example>();
168 for (File f: examplesDir.listFiles()) {
169 if (isValidExample(f))
170 results.add(new Example(f));
171 }
172 return results;
173 }
175 boolean isValidExample(File f) {
176 return (f.isDirectory() && (!jtreg || f.list().length > 0)) ||
177 (f.isFile() && f.getName().endsWith(".java"));
178 }
180 /**
181 * Report an error.
182 */
183 void error(String msg) {
184 System.err.println("Error: " + msg);
185 errors++;
186 }
188 static boolean jtreg;
190 int errors;
192 /**
193 * Clean the contents of a directory.
194 */
195 static boolean clean(File dir) {
196 boolean ok = true;
197 for (File f: dir.listFiles()) {
198 if (f.isDirectory())
199 ok &= clean(f);
200 ok &= f.delete();
201 }
202 return ok;
203 }
205 static abstract class Runner {
206 Runner(boolean showFiles, boolean raw, boolean verbose) {
207 this.showFiles = showFiles;
208 this.raw = raw;
209 this.verbose = verbose;
210 }
212 void close() throws IOException { }
214 void run(Collection<Example> examples) throws IOException {
215 for (Example e: examples) {
216 startExample(e);
217 if (showFiles) {
218 showFile(e, e.infoFile);
219 Set<File> srcFiles = new TreeSet<File>(e.srcFiles);
220 srcFiles.remove(e.infoFile);
221 showFiles(e, srcFiles);
222 showFiles(e, e.srcPathFiles);
223 showFiles(e, e.procFiles);
224 showFiles(e, e.supportFiles);
225 }
226 run(e);
227 }
228 }
230 void showFiles(Example e, Collection<File> files) throws IOException {
231 for (File f: files)
232 showFile(e, f);
233 }
235 abstract void startExample(Example e) throws IOException;
237 abstract void showFile(Example e, File f) throws IOException;
239 abstract void run(Example e) throws IOException;
241 protected String read(File f) throws IOException {
242 byte[] bytes = new byte[(int) f.length()];
243 DataInputStream in = new DataInputStream(new FileInputStream(f));
244 try {
245 in.readFully(bytes);
246 } finally {
247 in.close();
248 }
249 return new String(bytes);
250 }
252 protected Pattern copyrightHeaderPat =
253 Pattern.compile("(?s)(/\\*.*?Copyright.*?\\*/\n)\\s*(.*)");
254 protected Pattern infoHeaderPat =
255 Pattern.compile("(?s)((?://\\s*[a-z]+:[^\n]*\n)+)\\s*(.*)");
257 protected boolean showFiles;
258 protected boolean raw;
259 protected boolean verbose;
260 }
262 static class TextRunner extends Runner {
263 TextRunner(File file, boolean showFiles, boolean raw, boolean verbose)
264 throws IOException {
265 super(showFiles, raw, verbose);
266 this.file = file;
267 out = new PrintWriter(new FileWriter(file));
268 }
270 TextRunner(PrintWriter out, boolean showFiles, boolean raw, boolean verbose)
271 throws IOException {
272 super(showFiles, raw, verbose);
273 this.out = out;
274 }
276 @Override
277 void close() {
278 if (file != null)
279 out.close();
280 }
282 @Override
283 void startExample(Example e) {
284 out.println("----- " + e.getName() + " --------------------");
285 out.println();
286 }
288 @Override
289 void showFile(Example e, File f) {
290 out.println("--- " + f);
291 String text;
292 try {
293 text = read(f);
294 } catch (IOException ex) {
295 text = "Error reading " + f + "; " + ex;
296 }
297 Matcher m = copyrightHeaderPat.matcher(text);
298 if (m.matches()) {
299 out.println("(Copyright)");
300 writeLines(m.group(2));
301 } else {
302 writeLines(text);
303 }
304 out.println();
305 }
307 @Override
308 void run(Example e) {
309 // only show Output: header if also showing files
310 if (showFiles)
311 out.println("--- Output:");
312 e.run(out, raw, verbose);
313 out.println();
314 }
316 void writeLines(String text) {
317 for (String line: text.split("\n"))
318 out.println(line);
319 }
321 File file;
322 PrintWriter out;
323 }
325 static class HTMLRunner extends Runner {
326 HTMLRunner(File file, boolean showFiles, boolean raw, boolean verbose, String title)
327 throws IOException {
328 super(showFiles, raw, verbose);
329 this.file = file;
330 PrintWriter out = new PrintWriter(new FileWriter(file));
331 html = new HTMLWriter(out);
332 html.startTag(HTMLWriter.HEAD);
333 if (title != null) {
334 html.startTag(HTMLWriter.TITLE);
335 html.write(title);
336 html.endTag(HTMLWriter.TITLE);
337 }
338 html.startTag(HTMLWriter.STYLE);
339 html.newLine();
340 html.writeLine("div.file { background-color:#e0ffe0; margin-left:30px; margin-right:30px;\n"
341 + " padding: 3px; border: thin solid silver; }");
342 html.writeLine("p.file { white-space: pre-wrap; font-family:monospace; margin: 0; }");
343 html.writeLine("div.output { background-color:#e0e0ff; margin-left:30px; margin-right:30px;\n"
344 + " padding: 3px; border: thin solid silver; }");
345 html.writeLine("p.output { white-space: pre-wrap; font-family:monospace; margin: 0; }");
346 html.writeLine("table.index { border: thin solid silver; }");
347 html.writeLine(".copyright { font-size: x-small }");
348 html.writeLine(".hidden { display:none }");
349 html.writeLine(".unhidden { display:block }");
350 html.writeLine(".odd { background-color: #e0e0e0 }");
351 html.writeLine(".even { background-color: white }");
352 html.endTag(HTMLWriter.STYLE);
353 html.startTag(HTMLWriter.SCRIPT);
354 html.writeAttr(HTMLWriter.TYPE, HTMLWriter.TEXT_JAVASCRIPT);
355 html.writeLine("\nfunction unhide(id) {\n"
356 + " var item = document.getElementById(id);\n"
357 + " if (item) {\n"
358 + " item.className=(item.className=='hidden')?'unhidden':'hidden';\n"
359 + " }\n"
360 + "}");
361 html.endTag(HTMLWriter.SCRIPT);
362 html.endTag(HTMLWriter.HEAD);
363 html.startTag(HTMLWriter.BODY);
364 if (title != null) {
365 html.startTag(TITLE_HEADER);
366 html.write(title);
367 html.endTag(TITLE_HEADER);
368 }
369 }
371 @Override
372 void close() throws IOException {
373 html.endTag(HTMLWriter.BODY);
374 html.newLine();
375 html.flush();
376 }
378 @Override
379 void run(Collection<Example> examples) throws IOException {
380 if (examples.size() > 1)
381 writeIndex(examples);
382 super.run(examples);
383 }
385 void writeIndex(Collection<Example> examples) throws IOException {
386 Map<String, Set<Example>> index = new TreeMap<String, Set<Example>>();
387 Set<String> initials = new HashSet<String>();
388 for (Example e: examples) {
389 for (String k: e.getDeclaredKeys()) {
390 Set<Example> s = index.get(k);
391 if (s == null)
392 index.put(k, s = new TreeSet<Example>());
393 s.add(e);
394 }
395 initials.add(e.getName().substring(0, 1).toUpperCase());
396 }
399 if (INDEX_HEADER != null) {
400 html.startTag(INDEX_HEADER);
401 html.write("Index");
402 html.endTag(INDEX_HEADER);
403 }
405 html.startTag(HTMLWriter.P);
406 html.writeLine("Examples: ");
407 for (char initial = 'A'; initial <= 'Z'; initial++) {
408 String s = String.valueOf(initial);
409 if (initials.contains(s)) {
410 html.writeLink("#" + s, s);
411 } else {
412 html.write(s);
413 }
414 html.newLine();
415 }
416 html.endTag(HTMLWriter.P);
418 html.startTag(HTMLWriter.TABLE);
419 html.writeAttr(HTMLWriter.CLASS, "index");
420 html.newLine();
421 int row = 0;
422 for (Map.Entry<String, Set<Example>> entry: index.entrySet()) {
423 html.startTag(HTMLWriter.TR);
424 html.writeAttr(HTMLWriter.CLASS,
425 (row++ % 2 == 0 ? "even" : "odd"));
426 html.startTag(HTMLWriter.TD);
427 html.writeAttr("valign", "top");
428 html.write(entry.getKey());
429 html.endTag(HTMLWriter.TD);
430 html.newLine();
431 html.startTag(HTMLWriter.TD);
432 html.writeAttr(HTMLWriter.ALIGN, "top");
433 String sep = "";
434 for (Example e: entry.getValue()) {
435 html.write(sep);
436 html.writeLink('#' + e.getName(), e.getName());
437 sep = ", ";
438 }
439 html.endTag(HTMLWriter.TD);
440 html.endTag(HTMLWriter.TR);
441 html.newLine();
442 }
443 html.endTag(HTMLWriter.TABLE);
444 }
446 @Override
447 void startExample(Example e) throws IOException {
448 String name = e.getName();
449 String initial = name.substring(0, 1).toUpperCase();
450 if (!initial.equals(currInitial)) {
451 html.writeLinkDestination(initial, "");
452 currInitial = initial;
453 }
454 html.writeLinkDestination(name, "");
455 html.startTag(EXAMPLE_HEADER);
456 html.write(e.getName());
457 html.endTag(EXAMPLE_HEADER);
458 }
460 @Override
461 void showFile(Example e, File f) throws IOException {
462 String text;
463 try {
464 text = read(f);
465 } catch (IOException ex) {
466 text = "Error reading " + f + ": " + ex;
467 }
468 if (!f.equals(e.file)) {
469 html.startTag(FILE_HEADER);
470 html.write(e.file.toURI().relativize(f.toURI()).toString());
471 html.endTag(FILE_HEADER);
472 }
473 html.startTag(HTMLWriter.DIV);
474 html.writeAttr(CLASS, FILE);
476 String legalHeader;
477 Matcher m1 = copyrightHeaderPat.matcher(text);
478 if (m1.matches()) {
479 legalHeader = m1.group(1);
480 text = m1.group(2);
481 } else
482 legalHeader = null;
484 String infoHeader;
485 Matcher m2 = infoHeaderPat.matcher(text);
486 if (m2.matches()) {
487 infoHeader = m2.group(1);
488 text = m2.group(2);
489 } else
490 infoHeader = null;
492 String legalId = null, infoId = null;
493 if (legalHeader != null || infoHeader != null) {
494 String sep = "";
495 html.startTag(HTMLWriter.SPAN);
496 html.writeStyleAttr("float: right");
497 if (legalHeader != null) {
498 legalId = nextId();
499 html.startTag(HTMLWriter.A);
500 html.writeAttr(HTMLWriter.HREF, "javascript:unhide('" + legalId + "');");
501 //html.writeEntity("©");
502 html.write("Copyright");
503 html.endTag(HTMLWriter.A);
504 sep = ", ";
505 }
506 if (infoHeader != null) {
507 html.write(sep);
508 infoId = nextId();
509 html.startTag(HTMLWriter.A);
510 html.writeAttr(HTMLWriter.HREF, "javascript:unhide('" + infoId + "');");
511 html.write("Info");
512 html.endTag(HTMLWriter.A);
513 sep = ", ";
514 }
515 html.endTag(HTMLWriter.SPAN);
516 }
518 html.startTag(HTMLWriter.P);
519 html.writeAttr(CLASS, FILE);
520 if (legalHeader != null) {
521 html.startTag(HTMLWriter.SPAN);
522 html.writeAttr(HTMLWriter.CLASS, "hidden");
523 html.writeAttr(HTMLWriter.ID, legalId);
524 html.write(legalHeader);
525 html.newLine();
526 html.endTag(HTMLWriter.SPAN);
527 }
528 if (infoHeader != null) {
529 html.startTag(HTMLWriter.SPAN);
530 html.writeAttr(HTMLWriter.CLASS, "hidden");
531 html.writeAttr(HTMLWriter.ID, infoId);
532 html.write(infoHeader);
533 html.newLine();
534 html.endTag(HTMLWriter.SPAN);
535 }
536 html.write(text);
537 html.endTag(HTMLWriter.P);
539 html.endTag(HTMLWriter.DIV);
540 }
542 @Override
543 void run(Example e) throws IOException {
544 StringWriter sw = new StringWriter();
545 PrintWriter pw = new PrintWriter(sw);
546 e.run(pw, raw, verbose);
547 pw.flush();
549 // only show Output: header if also showing files
550 if (showFiles) {
551 html.startTag(OUTPUT_HEADER);
552 html.write("Output:");
553 html.endTag(OUTPUT_HEADER);
554 }
556 html.startTag(HTMLWriter.DIV);
557 html.writeAttr(CLASS, OUTPUT);
558 html.startTag(HTMLWriter.P);
559 html.writeAttr(CLASS, OUTPUT);
560 String[] lines = sw.toString().split("\n");
561 for (String line: lines) {
562 html.write(line);
563 html.newLine();
564 }
565 html.endTag(HTMLWriter.P);
566 html.endTag(HTMLWriter.DIV);
567 }
569 String nextId() {
570 return "id" + (nextId++);
571 }
573 File file;
574 HTMLWriter html;
575 int nextId;
576 String currInitial = "";
578 static final String TITLE_HEADER = HTMLWriter.H3;
579 static final String INDEX_HEADER = HTMLWriter.H4;
580 static final String EXAMPLE_HEADER = HTMLWriter.H4;
581 static final String FILE_HEADER = HTMLWriter.H5;
582 static final String OUTPUT_HEADER = HTMLWriter.H5;
583 static final String CLASS = "class";
584 static final String FILE = "file";
585 static final String OUTPUT = "output";
586 }
587 }