langtools/test/tools/doclint/RunTest.java
changeset 14952 d0022ae20516
child 14962 19ffdfafbcd2
equal deleted inserted replaced
14951:8d9ea42e4aba 14952:d0022ae20516
       
     1 /*
       
     2  * Copyright (c) 2012, 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  * @bug 8000103
       
    26  * @summary Create doclint utility
       
    27  */
       
    28 
       
    29 import com.sun.tools.doclint.DocLint;
       
    30 import com.sun.tools.doclint.DocLint.BadArgs;
       
    31 import java.io.ByteArrayOutputStream;
       
    32 import java.io.File;
       
    33 import java.io.FilterOutputStream;
       
    34 import java.io.IOException;
       
    35 import java.io.OutputStream;
       
    36 import java.io.PrintStream;
       
    37 import java.io.PrintWriter;
       
    38 import java.io.StringWriter;
       
    39 import java.lang.annotation.Annotation;
       
    40 import java.lang.annotation.Retention;
       
    41 import java.lang.annotation.RetentionPolicy;
       
    42 import java.lang.reflect.InvocationTargetException;
       
    43 import java.lang.reflect.Method;
       
    44 
       
    45 /** javadoc error on toplevel:  a & b. */
       
    46 public class RunTest {
       
    47     /** javadoc error on member: a < b */
       
    48     public static void main(String... args) throws Exception {
       
    49         new RunTest().run();
       
    50     }
       
    51 
       
    52 
       
    53     File testSrc = new File(System.getProperty("test.src"));
       
    54     File thisFile = new File(testSrc, RunTest.class.getSimpleName() + ".java");
       
    55 
       
    56     void run() throws Exception {
       
    57         for (Method m: getClass().getDeclaredMethods()) {
       
    58             Annotation a = m.getAnnotation(Test.class);
       
    59             if (a != null) {
       
    60                 System.err.println("test: " + m.getName());
       
    61                 try {
       
    62                     StringWriter sw = new StringWriter();
       
    63                     PrintWriter pw = new PrintWriter(sw);;
       
    64                     m.invoke(this, new Object[] { pw });
       
    65                     String out = sw.toString();
       
    66                     System.err.println(">>> " + out.replace("\n", "\n>>> "));
       
    67                     if (!out.contains("a < b"))
       
    68                         error("\"a < b\" not found");
       
    69                     if (!out.contains("a & b"))
       
    70                         error("\"a & b\" not found");
       
    71                 } catch (InvocationTargetException e) {
       
    72                     Throwable cause = e.getCause();
       
    73                     throw (cause instanceof Exception) ? ((Exception) cause) : e;
       
    74                 }
       
    75                 System.err.println();
       
    76             }
       
    77         }
       
    78 
       
    79         if (errors > 0)
       
    80             throw new Exception(errors + " errors occurred");
       
    81     }
       
    82 
       
    83 
       
    84     void error(String msg) {
       
    85         System.err.println("Error: " + msg);
       
    86         errors++;
       
    87     }
       
    88 
       
    89     int errors;
       
    90 
       
    91     /** Marker annotation for test cases. */
       
    92     @Retention(RetentionPolicy.RUNTIME)
       
    93     @interface Test { }
       
    94 
       
    95     @Test
       
    96     void testMain(PrintWriter pw) throws BadArgs, IOException {
       
    97         String[] args = { "-Xmsgs", thisFile.getPath() };
       
    98         DocLint d = new DocLint();
       
    99         d.run(pw, args);
       
   100     }
       
   101 }
       
   102 
       
   103