609N/A/*
1132N/A * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
609N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
609N/A *
609N/A * This code is free software; you can redistribute it and/or modify it
609N/A * under the terms of the GNU General Public License version 2 only, as
609N/A * published by the Free Software Foundation.
609N/A *
609N/A * This code is distributed in the hope that it will be useful, but WITHOUT
609N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
609N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
609N/A * version 2 for more details (a copy is included in the LICENSE file that
609N/A * accompanied this code).
609N/A *
609N/A * You should have received a copy of the GNU General Public License version
609N/A * 2 along with this work; if not, write to the Free Software Foundation,
609N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
609N/A *
609N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
609N/A * or visit www.oracle.com if you need additional information or have any
609N/A * questions.
609N/A */
609N/A
609N/A/**
609N/A * @test
1132N/A * @bug 6968063 7127924
609N/A * @summary provide examples of code that generate diagnostics
841N/A * @build ArgTypeCompilerFactory Example HTMLWriter RunExamples
1132N/A * @run main/othervm RunExamples
1132N/A */
1132N/A/*
1132N/A * See CR 7127924 for info on why othervm is used.
609N/A */
609N/A
609N/Aimport java.io.*;
609N/Aimport java.text.SimpleDateFormat;
609N/Aimport java.util.*;
609N/Aimport java.util.regex.Matcher;
609N/Aimport java.util.regex.Pattern;
609N/A
609N/A/**
609N/A * Utility to run selected or all examples, writing results to
609N/A * stdout, a plain text file or an HTML file. This program can be
609N/A * run standalone, or as a jtreg test.
609N/A *
609N/A * Options:
609N/A * -examples dir directory of examples. Defaults to ${test.src}/examples
609N/A * -raw run examples with -XDrawDiagnostics
609N/A * -showFiles include text of source files in the output
609N/A * -verbose verbose output
609N/A * -o file write output to file: format will be HTML if
609N/A * file has .html extension; otherwise it will be plain text.
609N/A * default is to stdout
609N/A * -title string specify a title, only applies to HTML output
609N/A */
609N/Apublic class RunExamples {
609N/A public static void main(String... args) throws Exception {
794N/A jtreg = (System.getProperty("test.src") != null);
609N/A File tmpDir;
609N/A if (jtreg) {
609N/A // use standard jtreg scratch directory: the current directory
609N/A tmpDir = new File(System.getProperty("user.dir"));
609N/A } else {
609N/A tmpDir = new File(System.getProperty("java.io.tmpdir"),
609N/A RunExamples.class.getName()
609N/A + (new SimpleDateFormat("yyMMddHHmmss")).format(new Date()));
609N/A }
609N/A Example.setTempDir(tmpDir);
609N/A
609N/A RunExamples r = new RunExamples();
609N/A
609N/A try {
609N/A if (r.run(args))
609N/A return;
609N/A } finally {
609N/A /* VERY IMPORTANT NOTE. In jtreg mode, tmpDir is set to the
609N/A * jtreg scratch directory, which is the current directory.
609N/A * In case someone is faking jtreg mode, make sure to only
609N/A * clean tmpDir when it is reasonable to do so.
609N/A */
609N/A if (tmpDir.isDirectory() &&
609N/A tmpDir.getName().startsWith(RunExamples.class.getName())) {
609N/A if (clean(tmpDir))
609N/A tmpDir.delete();
609N/A }
609N/A }
609N/A
609N/A if (jtreg)
609N/A throw new Exception(r.errors + " errors occurred");
609N/A else
609N/A System.exit(1);
609N/A }
609N/A
609N/A boolean run(String... args) {
609N/A Set<String> selectedKeys = new TreeSet<String>();
609N/A Set<Example> selectedExamples = new TreeSet<Example>();
609N/A File testSrc = new File(System.getProperty("test.src", "."));
609N/A File examplesDir = new File(testSrc, "examples");
609N/A File outFile = null;
609N/A boolean raw = false;
609N/A boolean showFiles = false;
609N/A boolean verbose = false;
841N/A boolean argTypes = false;
609N/A String title = null;
609N/A
609N/A for (int i = 0; i < args.length; i++) {
609N/A String arg = args[i];
609N/A if (arg.equals("-k") && (i + 1) < args.length)
609N/A selectedKeys.add(args[++i]);
609N/A else if (arg.equals("-examples") && (i + 1) < args.length)
609N/A examplesDir = new File(args[++i]);
609N/A else if (arg.equals("-raw"))
609N/A raw = true;
609N/A else if (arg.equals("-showFiles"))
609N/A showFiles = true;
609N/A else if (arg.equals("-verbose"))
609N/A verbose = true;
609N/A else if (arg.equals("-o") && (i + 1) < args.length)
609N/A outFile = new File(args[++i]);
609N/A else if (arg.equals("-title") && (i + 1) < args.length)
609N/A title = args[++i];
841N/A else if (arg.equals("-argtypes"))
841N/A argTypes = true;
609N/A else if (arg.startsWith("-")) {
609N/A error("unknown option: " + arg);
609N/A return false;
609N/A } else {
609N/A while (i < args.length) {
609N/A File f = new File(examplesDir, args[i]);
609N/A selectedExamples.add(new Example(f));
609N/A i++;
609N/A }
609N/A }
609N/A }
609N/A
841N/A // special mode to show message keys and the types of the args that
841N/A // are used.
841N/A if (argTypes)
841N/A Example.Compiler.factory = new ArgTypeCompilerFactory();
841N/A
609N/A if (selectedKeys.size() > 0) {
609N/A Set<Example> examples = getExamples(examplesDir);
609N/A nextKey:
609N/A for (String k: selectedKeys) {
609N/A for (Example e: examples) {
609N/A if (e.getDeclaredKeys().contains(k))
609N/A continue nextKey;
609N/A }
609N/A error("Key " + k + ": no examples found");
609N/A }
609N/A } else {
841N/A if (selectedExamples.isEmpty())
609N/A selectedExamples = getExamples(examplesDir);
609N/A }
609N/A
609N/A try {
609N/A Runner r;
609N/A if (outFile == null) {
609N/A PrintWriter out = new PrintWriter(System.out);
609N/A r = new TextRunner(out, showFiles, raw, verbose);
609N/A } else if (outFile.getName().endsWith(".html"))
609N/A r = new HTMLRunner(outFile, showFiles, raw, verbose, title);
609N/A else
609N/A r = new TextRunner(outFile, showFiles, raw, verbose);
609N/A r.run(selectedExamples);
609N/A r.close();
609N/A } catch (IOException e) {
609N/A error("Error writing output: " + e);
609N/A }
609N/A
609N/A return (errors == 0);
609N/A }
609N/A
609N/A /**
609N/A * Get the complete set of examples to be checked.
609N/A */
609N/A Set<Example> getExamples(File examplesDir) {
609N/A Set<Example> results = new TreeSet<Example>();
609N/A for (File f: examplesDir.listFiles()) {
794N/A if (isValidExample(f))
609N/A results.add(new Example(f));
609N/A }
609N/A return results;
609N/A }
609N/A
794N/A boolean isValidExample(File f) {
794N/A return (f.isDirectory() && (!jtreg || f.list().length > 0)) ||
794N/A (f.isFile() && f.getName().endsWith(".java"));
794N/A }
794N/A
609N/A /**
609N/A * Report an error.
609N/A */
609N/A void error(String msg) {
609N/A System.err.println("Error: " + msg);
609N/A errors++;
609N/A }
609N/A
794N/A static boolean jtreg;
794N/A
609N/A int errors;
609N/A
609N/A /**
609N/A * Clean the contents of a directory.
609N/A */
609N/A static boolean clean(File dir) {
609N/A boolean ok = true;
609N/A for (File f: dir.listFiles()) {
609N/A if (f.isDirectory())
609N/A ok &= clean(f);
609N/A ok &= f.delete();
609N/A }
609N/A return ok;
609N/A }
609N/A
609N/A static abstract class Runner {
609N/A Runner(boolean showFiles, boolean raw, boolean verbose) {
609N/A this.showFiles = showFiles;
609N/A this.raw = raw;
609N/A this.verbose = verbose;
609N/A }
609N/A
609N/A void close() throws IOException { }
609N/A
609N/A void run(Collection<Example> examples) throws IOException {
609N/A for (Example e: examples) {
609N/A startExample(e);
609N/A if (showFiles) {
609N/A showFile(e, e.infoFile);
609N/A Set<File> srcFiles = new TreeSet<File>(e.srcFiles);
609N/A srcFiles.remove(e.infoFile);
609N/A showFiles(e, srcFiles);
609N/A showFiles(e, e.srcPathFiles);
609N/A showFiles(e, e.procFiles);
609N/A showFiles(e, e.supportFiles);
609N/A }
609N/A run(e);
609N/A }
609N/A }
609N/A
609N/A void showFiles(Example e, Collection<File> files) throws IOException {
609N/A for (File f: files)
609N/A showFile(e, f);
609N/A }
609N/A
609N/A abstract void startExample(Example e) throws IOException;
609N/A
609N/A abstract void showFile(Example e, File f) throws IOException;
609N/A
609N/A abstract void run(Example e) throws IOException;
609N/A
609N/A protected String read(File f) throws IOException {
609N/A byte[] bytes = new byte[(int) f.length()];
609N/A DataInputStream in = new DataInputStream(new FileInputStream(f));
609N/A try {
609N/A in.readFully(bytes);
609N/A } finally {
609N/A in.close();
609N/A }
609N/A return new String(bytes);
609N/A }
609N/A
609N/A protected Pattern copyrightHeaderPat =
609N/A Pattern.compile("(?s)(/\\*.*?Copyright.*?\\*/\n)\\s*(.*)");
609N/A protected Pattern infoHeaderPat =
609N/A Pattern.compile("(?s)((?://\\s*[a-z]+:[^\n]*\n)+)\\s*(.*)");
609N/A
609N/A protected boolean showFiles;
609N/A protected boolean raw;
609N/A protected boolean verbose;
609N/A }
609N/A
609N/A static class TextRunner extends Runner {
609N/A TextRunner(File file, boolean showFiles, boolean raw, boolean verbose)
609N/A throws IOException {
609N/A super(showFiles, raw, verbose);
609N/A this.file = file;
609N/A out = new PrintWriter(new FileWriter(file));
609N/A }
609N/A
609N/A TextRunner(PrintWriter out, boolean showFiles, boolean raw, boolean verbose)
609N/A throws IOException {
609N/A super(showFiles, raw, verbose);
609N/A this.out = out;
609N/A }
609N/A
609N/A @Override
609N/A void close() {
609N/A if (file != null)
609N/A out.close();
609N/A }
609N/A
609N/A @Override
609N/A void startExample(Example e) {
609N/A out.println("----- " + e.getName() + " --------------------");
609N/A out.println();
609N/A }
609N/A
609N/A @Override
609N/A void showFile(Example e, File f) {
609N/A out.println("--- " + f);
609N/A String text;
609N/A try {
609N/A text = read(f);
609N/A } catch (IOException ex) {
609N/A text = "Error reading " + f + "; " + ex;
609N/A }
609N/A Matcher m = copyrightHeaderPat.matcher(text);
609N/A if (m.matches()) {
609N/A out.println("(Copyright)");
609N/A writeLines(m.group(2));
609N/A } else {
609N/A writeLines(text);
609N/A }
609N/A out.println();
609N/A }
609N/A
609N/A @Override
609N/A void run(Example e) {
609N/A // only show Output: header if also showing files
609N/A if (showFiles)
609N/A out.println("--- Output:");
609N/A e.run(out, raw, verbose);
609N/A out.println();
609N/A }
609N/A
609N/A void writeLines(String text) {
609N/A for (String line: text.split("\n"))
609N/A out.println(line);
609N/A }
609N/A
609N/A File file;
609N/A PrintWriter out;
609N/A }
609N/A
609N/A static class HTMLRunner extends Runner {
609N/A HTMLRunner(File file, boolean showFiles, boolean raw, boolean verbose, String title)
609N/A throws IOException {
609N/A super(showFiles, raw, verbose);
609N/A this.file = file;
609N/A PrintWriter out = new PrintWriter(new FileWriter(file));
609N/A html = new HTMLWriter(out);
609N/A html.startTag(HTMLWriter.HEAD);
609N/A if (title != null) {
609N/A html.startTag(HTMLWriter.TITLE);
609N/A html.write(title);
609N/A html.endTag(HTMLWriter.TITLE);
609N/A }
609N/A html.startTag(HTMLWriter.STYLE);
609N/A html.newLine();
609N/A html.writeLine("div.file { background-color:#e0ffe0; margin-left:30px; margin-right:30px;\n"
609N/A + " padding: 3px; border: thin solid silver; }");
609N/A html.writeLine("p.file { white-space: pre-wrap; font-family:monospace; margin: 0; }");
609N/A html.writeLine("div.output { background-color:#e0e0ff; margin-left:30px; margin-right:30px;\n"
609N/A + " padding: 3px; border: thin solid silver; }");
609N/A html.writeLine("p.output { white-space: pre-wrap; font-family:monospace; margin: 0; }");
609N/A html.writeLine("table.index { border: thin solid silver; }");
609N/A html.writeLine(".copyright { font-size: x-small }");
609N/A html.writeLine(".hidden { display:none }");
609N/A html.writeLine(".unhidden { display:block }");
609N/A html.writeLine(".odd { background-color: #e0e0e0 }");
609N/A html.writeLine(".even { background-color: white }");
609N/A html.endTag(HTMLWriter.STYLE);
609N/A html.startTag(HTMLWriter.SCRIPT);
609N/A html.writeAttr(HTMLWriter.TYPE, HTMLWriter.TEXT_JAVASCRIPT);
609N/A html.writeLine("\nfunction unhide(id) {\n"
609N/A + " var item = document.getElementById(id);\n"
609N/A + " if (item) {\n"
609N/A + " item.className=(item.className=='hidden')?'unhidden':'hidden';\n"
609N/A + " }\n"
609N/A + "}");
609N/A html.endTag(HTMLWriter.SCRIPT);
609N/A html.endTag(HTMLWriter.HEAD);
609N/A html.startTag(HTMLWriter.BODY);
609N/A if (title != null) {
609N/A html.startTag(TITLE_HEADER);
609N/A html.write(title);
609N/A html.endTag(TITLE_HEADER);
609N/A }
609N/A }
609N/A
609N/A @Override
609N/A void close() throws IOException {
609N/A html.endTag(HTMLWriter.BODY);
609N/A html.newLine();
609N/A html.flush();
609N/A }
609N/A
609N/A @Override
609N/A void run(Collection<Example> examples) throws IOException {
609N/A if (examples.size() > 1)
609N/A writeIndex(examples);
609N/A super.run(examples);
609N/A }
609N/A
609N/A void writeIndex(Collection<Example> examples) throws IOException {
609N/A Map<String, Set<Example>> index = new TreeMap<String, Set<Example>>();
609N/A Set<String> initials = new HashSet<String>();
609N/A for (Example e: examples) {
609N/A for (String k: e.getDeclaredKeys()) {
609N/A Set<Example> s = index.get(k);
609N/A if (s == null)
609N/A index.put(k, s = new TreeSet<Example>());
609N/A s.add(e);
609N/A }
609N/A initials.add(e.getName().substring(0, 1).toUpperCase());
609N/A }
609N/A
609N/A
609N/A if (INDEX_HEADER != null) {
609N/A html.startTag(INDEX_HEADER);
609N/A html.write("Index");
609N/A html.endTag(INDEX_HEADER);
609N/A }
609N/A
609N/A html.startTag(HTMLWriter.P);
609N/A html.writeLine("Examples: ");
609N/A for (char initial = 'A'; initial <= 'Z'; initial++) {
609N/A String s = String.valueOf(initial);
609N/A if (initials.contains(s)) {
609N/A html.writeLink("#" + s, s);
609N/A } else {
609N/A html.write(s);
609N/A }
609N/A html.newLine();
609N/A }
609N/A html.endTag(HTMLWriter.P);
609N/A
609N/A html.startTag(HTMLWriter.TABLE);
609N/A html.writeAttr(HTMLWriter.CLASS, "index");
609N/A html.newLine();
609N/A int row = 0;
609N/A for (Map.Entry<String, Set<Example>> entry: index.entrySet()) {
609N/A html.startTag(HTMLWriter.TR);
609N/A html.writeAttr(HTMLWriter.CLASS,
609N/A (row++ % 2 == 0 ? "even" : "odd"));
609N/A html.startTag(HTMLWriter.TD);
609N/A html.writeAttr("valign", "top");
609N/A html.write(entry.getKey());
609N/A html.endTag(HTMLWriter.TD);
609N/A html.newLine();
609N/A html.startTag(HTMLWriter.TD);
609N/A html.writeAttr(HTMLWriter.ALIGN, "top");
609N/A String sep = "";
609N/A for (Example e: entry.getValue()) {
609N/A html.write(sep);
609N/A html.writeLink('#' + e.getName(), e.getName());
609N/A sep = ", ";
609N/A }
609N/A html.endTag(HTMLWriter.TD);
609N/A html.endTag(HTMLWriter.TR);
609N/A html.newLine();
609N/A }
609N/A html.endTag(HTMLWriter.TABLE);
609N/A }
609N/A
609N/A @Override
609N/A void startExample(Example e) throws IOException {
609N/A String name = e.getName();
609N/A String initial = name.substring(0, 1).toUpperCase();
609N/A if (!initial.equals(currInitial)) {
609N/A html.writeLinkDestination(initial, "");
609N/A currInitial = initial;
609N/A }
609N/A html.writeLinkDestination(name, "");
609N/A html.startTag(EXAMPLE_HEADER);
609N/A html.write(e.getName());
609N/A html.endTag(EXAMPLE_HEADER);
609N/A }
609N/A
609N/A @Override
609N/A void showFile(Example e, File f) throws IOException {
609N/A String text;
609N/A try {
609N/A text = read(f);
609N/A } catch (IOException ex) {
609N/A text = "Error reading " + f + ": " + ex;
609N/A }
609N/A if (!f.equals(e.file)) {
609N/A html.startTag(FILE_HEADER);
609N/A html.write(e.file.toURI().relativize(f.toURI()).toString());
609N/A html.endTag(FILE_HEADER);
609N/A }
609N/A html.startTag(HTMLWriter.DIV);
609N/A html.writeAttr(CLASS, FILE);
609N/A
609N/A String legalHeader;
609N/A Matcher m1 = copyrightHeaderPat.matcher(text);
609N/A if (m1.matches()) {
609N/A legalHeader = m1.group(1);
609N/A text = m1.group(2);
609N/A } else
609N/A legalHeader = null;
609N/A
609N/A String infoHeader;
609N/A Matcher m2 = infoHeaderPat.matcher(text);
609N/A if (m2.matches()) {
609N/A infoHeader = m2.group(1);
609N/A text = m2.group(2);
609N/A } else
609N/A infoHeader = null;
609N/A
609N/A String legalId = null, infoId = null;
609N/A if (legalHeader != null || infoHeader != null) {
609N/A String sep = "";
609N/A html.startTag(HTMLWriter.SPAN);
609N/A html.writeStyleAttr("float: right");
609N/A if (legalHeader != null) {
609N/A legalId = nextId();
609N/A html.startTag(HTMLWriter.A);
609N/A html.writeAttr(HTMLWriter.HREF, "javascript:unhide('" + legalId + "');");
609N/A //html.writeEntity("&copy;");
609N/A html.write("Copyright");
609N/A html.endTag(HTMLWriter.A);
609N/A sep = ", ";
609N/A }
609N/A if (infoHeader != null) {
609N/A html.write(sep);
609N/A infoId = nextId();
609N/A html.startTag(HTMLWriter.A);
609N/A html.writeAttr(HTMLWriter.HREF, "javascript:unhide('" + infoId + "');");
609N/A html.write("Info");
609N/A html.endTag(HTMLWriter.A);
609N/A sep = ", ";
609N/A }
609N/A html.endTag(HTMLWriter.SPAN);
609N/A }
609N/A
609N/A html.startTag(HTMLWriter.P);
609N/A html.writeAttr(CLASS, FILE);
609N/A if (legalHeader != null) {
609N/A html.startTag(HTMLWriter.SPAN);
609N/A html.writeAttr(HTMLWriter.CLASS, "hidden");
609N/A html.writeAttr(HTMLWriter.ID, legalId);
609N/A html.write(legalHeader);
609N/A html.newLine();
609N/A html.endTag(HTMLWriter.SPAN);
609N/A }
609N/A if (infoHeader != null) {
609N/A html.startTag(HTMLWriter.SPAN);
609N/A html.writeAttr(HTMLWriter.CLASS, "hidden");
609N/A html.writeAttr(HTMLWriter.ID, infoId);
609N/A html.write(infoHeader);
609N/A html.newLine();
609N/A html.endTag(HTMLWriter.SPAN);
609N/A }
609N/A html.write(text);
609N/A html.endTag(HTMLWriter.P);
609N/A
609N/A html.endTag(HTMLWriter.DIV);
609N/A }
609N/A
609N/A @Override
609N/A void run(Example e) throws IOException {
609N/A StringWriter sw = new StringWriter();
609N/A PrintWriter pw = new PrintWriter(sw);
609N/A e.run(pw, raw, verbose);
609N/A pw.flush();
609N/A
609N/A // only show Output: header if also showing files
609N/A if (showFiles) {
609N/A html.startTag(OUTPUT_HEADER);
609N/A html.write("Output:");
609N/A html.endTag(OUTPUT_HEADER);
609N/A }
609N/A
609N/A html.startTag(HTMLWriter.DIV);
609N/A html.writeAttr(CLASS, OUTPUT);
609N/A html.startTag(HTMLWriter.P);
609N/A html.writeAttr(CLASS, OUTPUT);
609N/A String[] lines = sw.toString().split("\n");
609N/A for (String line: lines) {
609N/A html.write(line);
609N/A html.newLine();
609N/A }
609N/A html.endTag(HTMLWriter.P);
609N/A html.endTag(HTMLWriter.DIV);
609N/A }
609N/A
609N/A String nextId() {
609N/A return "id" + (nextId++);
609N/A }
609N/A
609N/A File file;
609N/A HTMLWriter html;
609N/A int nextId;
609N/A String currInitial = "";
609N/A
609N/A static final String TITLE_HEADER = HTMLWriter.H3;
609N/A static final String INDEX_HEADER = HTMLWriter.H4;
609N/A static final String EXAMPLE_HEADER = HTMLWriter.H4;
609N/A static final String FILE_HEADER = HTMLWriter.H5;
609N/A static final String OUTPUT_HEADER = HTMLWriter.H5;
609N/A static final String CLASS = "class";
609N/A static final String FILE = "file";
609N/A static final String OUTPUT = "output";
609N/A }
609N/A}
609N/A
609N/A