duke@1: /* ohair@554: * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as duke@1: * published by the Free Software Foundation. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: /* duke@1: * A utility used to invoke and test the javadoc tool. duke@1: * duke@1: * @author Scott Seligman duke@1: */ duke@1: duke@1: duke@1: import java.io.*; duke@1: import java.util.*; duke@1: import com.sun.javadoc.*; duke@1: duke@1: duke@1: public class Tester { duke@1: duke@1: protected final String TEST_SRC = System.getProperty("test.src", "."); duke@1: protected final String TEST_CLASSES = System.getProperty("test.classes", duke@1: "."); duke@1: private final String DEFAULT_ARGS[] = { duke@1: "-sourcepath", TEST_SRC, duke@1: }; duke@1: duke@1: private final File outputFile = new File(TEST_CLASSES, "testrun.out"); duke@1: private final File expectedOutputFile = new File(TEST_SRC, "expected.out"); duke@1: // private final File bootstrapMarkerFile = new File("bootstrap"); duke@1: duke@1: // True if we should "set expectations" by writing the expected output file duke@1: // rather than reading it and comparing. duke@1: // private final boolean bootstrap = bootstrapMarkerFile.isFile(); duke@1: duke@1: private String docletName; duke@1: private String[] args; duke@1: private Writer out = null; duke@1: duke@1: duke@1: /* duke@1: * Individual tests can extend this to create generics-aware doclets. duke@1: */ duke@1: public static abstract class Doclet extends com.sun.javadoc.Doclet { duke@1: public static LanguageVersion languageVersion() { duke@1: return LanguageVersion.JAVA_1_5; duke@1: } duke@1: } duke@1: duke@1: duke@1: public Tester(String docletName) { duke@1: this(docletName, new String[0]); duke@1: } duke@1: duke@1: public Tester(String docletName, String... additionalArgs) { duke@1: this.docletName = docletName; duke@1: duke@1: int len = DEFAULT_ARGS.length + additionalArgs.length; duke@1: args = new String[len]; duke@1: System.arraycopy(DEFAULT_ARGS, 0, args, 0, DEFAULT_ARGS.length); duke@1: System.arraycopy(additionalArgs, 0, args, DEFAULT_ARGS.length, duke@1: additionalArgs.length); duke@1: duke@1: try { duke@1: out = new BufferedWriter(new FileWriter(outputFile)); duke@1: } catch (IOException e) { duke@1: throw new Error("Could not open output file " + outputFile); duke@1: } duke@1: } duke@1: duke@1: public void run() throws IOException { duke@1: try { duke@1: if (com.sun.tools.javadoc.Main.execute("javadoc", jjg@140: docletName, jjg@140: getClass().getClassLoader(), jjg@140: args) != 0) { duke@1: throw new Error("Javadoc errors encountered."); duke@1: } duke@1: System.out.println("--> Output written to " + outputFile); duke@1: } finally { duke@1: out.close(); duke@1: } duke@1: } duke@1: duke@1: /* duke@1: * Compare output of test run to expected output. duke@1: * Throw an Error if they don't match. duke@1: */ duke@1: public void verify() throws IOException { duke@1: BufferedReader thisRun = duke@1: new BufferedReader(new FileReader(outputFile)); duke@1: BufferedReader expected = duke@1: new BufferedReader(new FileReader(expectedOutputFile)); duke@1: duke@1: for (int lineNum = 1; true; lineNum++) { duke@1: String line1 = thisRun.readLine(); duke@1: String line2 = expected.readLine(); duke@1: if (line1 == null && line2 == null) { duke@1: return; // EOF with all lines matching duke@1: } duke@1: if (line1 == null || !line1.equals(line2)) { duke@1: throw new Error(outputFile + ":" + lineNum + duke@1: ": output doesn't match"); duke@1: } duke@1: } duke@1: } duke@1: duke@1: duke@1: public void println(Object o) throws IOException { duke@1: prln(0, o); duke@1: } duke@1: duke@1: public void println() throws IOException { duke@1: prln(); duke@1: } duke@1: duke@1: public void printPackage(PackageDoc p) throws IOException { duke@1: prPackage(0, p); duke@1: } duke@1: duke@1: public void printClass(ClassDoc cd) throws IOException { duke@1: if (cd.isAnnotationType()) duke@1: printAnnotationType((AnnotationTypeDoc)cd); duke@1: else duke@1: prClass(0, cd); duke@1: } duke@1: duke@1: public void printAnnotationType(AnnotationTypeDoc at) throws IOException { duke@1: prAnnotationType(0, at); duke@1: } duke@1: duke@1: public void printField(FieldDoc f) throws IOException { duke@1: prField(0, f); duke@1: } duke@1: duke@1: public void printParameter(Parameter p) throws IOException { duke@1: prParameter(0, p); duke@1: } duke@1: duke@1: public void printMethod(MethodDoc m) throws IOException { duke@1: prln(0, "method " + m); duke@1: prMethod(0, m); duke@1: } duke@1: duke@1: public void printAnnotationTypeElement(AnnotationTypeElementDoc e) duke@1: throws IOException { duke@1: prln(0, "element " + e); duke@1: prMethod(0, e); duke@1: } duke@1: duke@1: public void printConstructor(ConstructorDoc c) throws IOException { duke@1: prln(0, "constructor " + c); duke@1: prExecutable(0, c); duke@1: } duke@1: duke@1: duke@1: private void prPackage(int off, PackageDoc p) throws IOException { duke@1: prln(off, "package " + p); duke@1: prAnnotations(off + 2, p.annotations()); duke@1: } duke@1: duke@1: private void prClass(int off, ClassDoc cd) throws IOException { duke@1: prln(off, duke@1: (cd.isInterface() ? "interface" : cd.isEnum() ? "enum" : "class") duke@1: + " " + cd); duke@1: prln(off + 2, "name: " + cd.simpleTypeName() + " / " + duke@1: cd.typeName() + " / " + cd.qualifiedTypeName()); duke@1: prAnnotations(off + 2, cd.annotations()); duke@1: prLabel(off + 2, "type parameters"); duke@1: for (Type t : cd.typeParameters()) duke@1: prln(off + 4, t); duke@1: prParamTags(off + 2, cd.typeParamTags()); duke@1: prLabel(off + 2, "nested in"); duke@1: prln(off + 4, cd.containingClass()); duke@1: prLabel(off + 2, "superclass"); duke@1: prln(off + 4, cd.superclassType()); duke@1: prLabel(off + 2, "interfaces"); duke@1: Type[] ts = cd.interfaceTypes(); duke@1: Arrays.sort(ts); duke@1: for (Type t : ts) duke@1: prln(off + 4, t); duke@1: prLabel(off + 2, "enum constants"); duke@1: for (FieldDoc f : cd.enumConstants()) duke@1: prln(off + 4, f.name()); duke@1: prLabel(off + 2, "fields"); duke@1: for (FieldDoc f : cd.fields()) duke@1: prln(off + 4, f.type() + " " + f.name()); duke@1: prLabel(off + 2, "constructors"); duke@1: for (ConstructorDoc c : cd.constructors()) duke@1: prln(off + 4, c.name() + c.flatSignature()); duke@1: prLabel(off + 2, "methods"); duke@1: for (MethodDoc m : cd.methods()) duke@1: prln(off + 4, typeUseString(m.returnType()) + " " + duke@1: m.name() + m.flatSignature()); duke@1: } duke@1: duke@1: private void prAnnotationType(int off, AnnotationTypeDoc at) duke@1: throws IOException { duke@1: prln(off, "@interface " + at); duke@1: prAnnotations(off + 2, at.annotations()); duke@1: prLabel(off + 2, "elements"); duke@1: for (AnnotationTypeElementDoc e : at.elements()) { duke@1: String def = (e.defaultValue() == null) duke@1: ? "" duke@1: : " default " + e.defaultValue(); duke@1: prln(off + 4, typeUseString(e.returnType()) + " " + e.name() + duke@1: e.flatSignature() + def); duke@1: } duke@1: } duke@1: duke@1: private void prField(int off, FieldDoc f) throws IOException { duke@1: prln(off, "field " + typeUseString(f.type()) + " " + f.name()); duke@1: prAnnotations(off + 2, f.annotations()); duke@1: } duke@1: duke@1: private void prParameter(int off, Parameter p) throws IOException { duke@1: prln(off, "parameter " + p); duke@1: prAnnotations(off + 2, p.annotations()); duke@1: } duke@1: duke@1: private void prMethod(int off, MethodDoc m) throws IOException { duke@1: prExecutable(off, m); duke@1: prLabel(off + 2, "returns"); duke@1: prln(off + 4, typeUseString(m.returnType())); duke@1: prLabel(off + 2, "overridden type"); duke@1: prln(off + 4, m.overriddenType()); duke@1: } duke@1: duke@1: private void prExecutable(int off, ExecutableMemberDoc m) duke@1: throws IOException { duke@1: if (!m.isAnnotationTypeElement()) { duke@1: prln(off + 2, "signature: " + m.flatSignature()); duke@1: prln(off + 2, " " + m.signature()); duke@1: } duke@1: prAnnotations(off + 2, m.annotations()); duke@1: prParamTags(off + 2, m.typeParamTags()); duke@1: prParamTags(off + 2, m.paramTags()); duke@1: prLabel(off + 2, "type parameters"); duke@1: for (Type t : m.typeParameters()) duke@1: prln(off + 4, t); duke@1: prLabel(off + 2, "throws"); duke@1: Type[] ts = m.thrownExceptionTypes(); duke@1: Arrays.sort(ts); duke@1: for (Type t : ts) duke@1: prln(off + 4, t); duke@1: } duke@1: duke@1: private void prAnnotations(int off, AnnotationDesc[] as) duke@1: throws IOException { duke@1: prLabel(off, "annotations"); duke@1: for (AnnotationDesc a : as) duke@1: prln(off + 2, a.toString()); duke@1: } duke@1: duke@1: private void prParamTags(int off, ParamTag tags[]) throws IOException { duke@1: for (ParamTag tag : tags) duke@1: prParamTag(off, tag); duke@1: } duke@1: duke@1: private void prParamTag(int off, ParamTag tag) throws IOException { duke@1: String name = tag.parameterName(); duke@1: if (tag.isTypeParameter()) name = "<" + name + ">"; duke@1: prln(off, "@param " + name + " " + tag.parameterComment()); duke@1: } duke@1: duke@1: duke@1: private String typeUseString(Type t) { duke@1: return (t instanceof ClassDoc || t instanceof TypeVariable) duke@1: ? t.typeName() duke@1: : t.toString(); duke@1: } duke@1: duke@1: duke@1: // Labels queued for possible printing. Innermost is first in list. duke@1: List labels = new ArrayList(); duke@1: duke@1: // Print label if its section is nonempty. duke@1: void prLabel(int off, String s) { duke@1: while (!labels.isEmpty() && labels.get(0).off >= off) duke@1: labels.remove(0); duke@1: labels.add(0, new Line(off, s)); duke@1: } duke@1: duke@1: // Print queued labels with offsets less than "off". duke@1: void popLabels(int off) throws IOException { duke@1: while (!labels.isEmpty()) { duke@1: Line label = labels.remove(0); duke@1: if (label.off < off) duke@1: prln(label.off, label.o + ":"); duke@1: } duke@1: } duke@1: duke@1: // Print "o" at given offset. duke@1: void pr(int off, Object o) throws IOException { duke@1: popLabels(off); duke@1: for (int i = 0; i < off; i++) duke@1: out.write(' '); duke@1: if (o != null) duke@1: out.write(o.toString()); duke@1: } duke@1: duke@1: // Print "o" (if non-null) at given offset, then newline. duke@1: void prln(int off, Object o) throws IOException { duke@1: if (o != null) { duke@1: pr(off, o); duke@1: prln(); duke@1: } duke@1: } duke@1: duke@1: // Print newline. duke@1: void prln() throws IOException { duke@1: out.write('\n'); // don't want platform-dependent separator duke@1: } duke@1: duke@1: duke@1: static class Line { duke@1: int off; duke@1: Object o; duke@1: Line(int off, Object o) { this.off = off; this.o = o; } duke@1: } duke@1: }