langtools/test/tools/javadoc/6958836/Test.java
changeset 5850 6f095ff5b469
child 14442 6dc10c88c07a
equal deleted inserted replaced
5849:04edd7910585 5850:6f095ff5b469
       
     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  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 6958836
       
    27  * @summary javadoc should support -Xmaxerrs and -Xmaxwarns
       
    28  */
       
    29 
       
    30 import java.io.*;
       
    31 import java.util.*;
       
    32 
       
    33 public class Test {
       
    34     public static void main(String... args) throws Exception {
       
    35         new Test().run();
       
    36     }
       
    37 
       
    38     void run() throws Exception {
       
    39         javadoc("errs",  list(),                   10,  0);
       
    40         javadoc("errs",  list("-Xmaxerrs",   "0"), 10,  0);
       
    41         javadoc("errs",  list("-Xmaxerrs",   "2"),  2,  0);
       
    42         javadoc("errs",  list("-Xmaxerrs",   "4"),  4,  0);
       
    43         javadoc("errs",  list("-Xmaxerrs",  "20"), 10,  0);
       
    44 
       
    45         javadoc("warns", list(),                    0, 10);
       
    46         javadoc("warns", list("-Xmaxwarns",  "0"),  0, 10);
       
    47         javadoc("warns", list("-Xmaxwarns",  "2"),  0,  2);
       
    48         javadoc("warns", list("-Xmaxwarns",  "4"),  0,  4);
       
    49         javadoc("warns", list("-Xmaxwarns", "20"),  0, 10);
       
    50 
       
    51         if (errors > 0)
       
    52             throw new Exception(errors + " errors occurred.");
       
    53     }
       
    54 
       
    55     void javadoc(String pkg, List<String> testOpts,
       
    56                 int expectErrs, int expectWarns) {
       
    57         System.err.println("Test " + (++count) + ": " + pkg + " " + testOpts);
       
    58         File testOutDir = new File("test" + count);
       
    59 
       
    60         List<String> opts = new ArrayList<String>();
       
    61         // Force en_US locale in lieu of something like -XDrawDiagnostics.
       
    62         // For some reason, this must be the first option when used.
       
    63         opts.addAll(list("-locale", "en_US"));
       
    64         opts.addAll(list("-classpath", System.getProperty("test.src")));
       
    65         opts.addAll(list("-d", testOutDir.getPath()));
       
    66         opts.addAll(testOpts);
       
    67         opts.add(pkg);
       
    68 
       
    69         StringWriter errSW = new StringWriter();
       
    70         PrintWriter errPW = new PrintWriter(errSW);
       
    71         StringWriter warnSW = new StringWriter();
       
    72         PrintWriter warnPW = new PrintWriter(warnSW);
       
    73         StringWriter noteSW = new StringWriter();
       
    74         PrintWriter notePW = new PrintWriter(noteSW);
       
    75 
       
    76         int rc = com.sun.tools.javadoc.Main.execute("javadoc",
       
    77                               errPW, warnPW, notePW,
       
    78                               "com.sun.tools.doclets.standard.Standard",
       
    79                               getClass().getClassLoader(),
       
    80                               opts.toArray(new String[opts.size()]));
       
    81         System.err.println("rc: " + rc);
       
    82 
       
    83         errPW.close();
       
    84         String errOut = errSW.toString();
       
    85         System.err.println("Errors:\n" + errOut);
       
    86         warnPW.close();
       
    87         String warnOut = warnSW.toString();
       
    88         System.err.println("Warnings:\n" + warnOut);
       
    89         notePW.close();
       
    90         String noteOut = noteSW.toString();
       
    91         System.err.println("Notes:\n" + noteOut);
       
    92 
       
    93         check(errOut, "Errors.java", expectErrs);
       
    94         check(warnOut, " warning ", expectWarns); // requires -locale en_US
       
    95     }
       
    96 
       
    97     void check(String text, String expectText, int expectCount) {
       
    98         int foundCount = 0;
       
    99         for (String line: text.split("[\r\n]+")) {
       
   100             if (line.contains(expectText))
       
   101                 foundCount++;
       
   102         }
       
   103         if (foundCount != expectCount) {
       
   104             error("incorrect number of matches found: " + foundCount
       
   105                   + ", expected: " + expectCount);
       
   106         }
       
   107     }
       
   108 
       
   109     private List<String> list(String... args) {
       
   110         return Arrays.asList(args);
       
   111     }
       
   112 
       
   113     void error(String msg) {
       
   114         System.err.println(msg);
       
   115         errors++;
       
   116     }
       
   117 
       
   118     int count;
       
   119     int errors;
       
   120 }