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