PathsTest.java revision 2721:f7ce2cfa4cdb
1139743Simp/* 239212Sgibbs * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. 339212Sgibbs * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 439212Sgibbs * 539212Sgibbs * This code is free software; you can redistribute it and/or modify it 639212Sgibbs * under the terms of the GNU General Public License version 2 only, as 739212Sgibbs * published by the Free Software Foundation. 839212Sgibbs * 939212Sgibbs * This code is distributed in the hope that it will be useful, but WITHOUT 1039212Sgibbs * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1139212Sgibbs * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1239212Sgibbs * version 2 for more details (a copy is included in the LICENSE file that 1339212Sgibbs * accompanied this code). 1439212Sgibbs * 1539212Sgibbs * You should have received a copy of the GNU General Public License version 1639212Sgibbs * 2 along with this work; if not, write to the Free Software Foundation, 1739212Sgibbs * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1839212Sgibbs * 1939212Sgibbs * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2039212Sgibbs * or visit www.oracle.com if you need additional information or have any 2139212Sgibbs * questions. 2239212Sgibbs */ 2339212Sgibbs 2439212Sgibbs/* 2539212Sgibbs * @test 2639212Sgibbs * @bug 8006263 2739212Sgibbs * @summary Supplementary test cases needed for doclint 28116161Sobrien * @library /tools/lib 29116161Sobrien * @build ToolBox 30116161Sobrien * @run main PathsTest 31116161Sobrien */ 3239212Sgibbs 3374840Skenimport com.sun.tools.javac.util.Context; 3474840Skenimport com.sun.tools.javac.file.JavacFileManager; 3589114Smsmithimport com.sun.tools.doclint.DocLint; 3689114Smsmithimport com.sun.tools.doclint.DocLint.BadArgs; 3774840Skenimport java.io.File; 3874840Skenimport java.io.FileWriter; 3974840Skenimport java.io.IOException; 40216088Skenimport java.io.PrintWriter; 41194189Sedimport java.io.StringWriter; 4274840Skenimport java.util.regex.Pattern; 4374840Skenimport javax.tools.StandardLocation; 4439212Sgibbsimport javax.tools.JavaFileManager; 4574840Sken 4674840Skenpublic class PathsTest { 47216088Sken public static void main(String... args) throws Exception { 4874840Sken new PathsTest().run(); 4939212Sgibbs } 5074840Sken 5174840Sken void run() throws Exception { 52195534Sscottl String PS = File.pathSeparator; 5374840Sken writeFile("src1/p/A.java", 5474840Sken "package p; public class A { }"); 5574840Sken compile("-d", "classes1", "src1/p/A.java"); 5674840Sken 5774840Sken writeFile("src2/q/B.java", 5874840Sken "package q; public class B extends p.A { }"); 5974840Sken compile("-d", "classes2", "-classpath", "classes1", "src2/q/B.java"); 6074840Sken 6174840Sken writeFile("src/Test.java", 6274840Sken "/** &0; */ class Test extends q.B { }"); 6374840Sken 64210779Sbcr test("src/Test.java", "-sourcepath", "src1" + PS + "src2"); 6574840Sken test("src/Test.java", "-classpath", "classes1" + PS + "classes2"); 6674840Sken 6774840Sken File testJar = createJar(); 6874840Sken String sysBootClassPath = System.getProperty("sun.boot.class.path"); 6974840Sken test("src/Test.java", "-bootclasspath", 7074840Sken testJar + PS + "classes1" + PS + "classes2"); 7174840Sken 7274840Sken if (errors > 0) 7374840Sken throw new Exception(errors + " errors found"); 7474840Sken } 7574840Sken 7674840Sken Pattern pkgNotFound = Pattern.compile("package [a-z]+ does not exist"); 7774840Sken Pattern badHtmlEntity = Pattern.compile("bad HTML entity"); 7874840Sken 7974840Sken void test(String file, String pathOpt, String path) throws BadArgs, IOException { 8074840Sken System.err.println("test " + pathOpt); 8174840Sken String out1 = doclint("-Xmsgs", file); 8274840Sken if (!pkgNotFound.matcher(out1).find()) 8374840Sken error("message not found: " + pkgNotFound); 8474840Sken 8574840Sken String out2 = doclint("-Xmsgs", pathOpt, path, file); 8674840Sken if (pkgNotFound.matcher(out2).find()) 87195534Sscottl error("unexpected message found: " + pkgNotFound); 88216088Sken if (!badHtmlEntity.matcher(out1).find()) 89216088Sken error("message not found: " + badHtmlEntity); 9074840Sken 9174840Sken try { 9274840Sken doclint("-Xmsgs", pathOpt); 9374840Sken error("expected exception not thrown"); 9474840Sken } catch (BadArgs e) { 9574840Sken System.err.println(e); 9674840Sken } 9774840Sken } 9874840Sken 9974840Sken File createJar() throws IOException { 10074840Sken File f = new File("test.jar"); 10174840Sken try (JavaFileManager fm = new JavacFileManager(new Context(), false, null)) { 10274840Sken ToolBox tb = new ToolBox(); 10374840Sken tb.new JarTask(f.getPath()) 10474840Sken .files(fm, StandardLocation.PLATFORM_CLASS_PATH, "java.lang.*") 10574840Sken .run(); 10674840Sken } 10774840Sken return f; 10889114Smsmith } 10989114Smsmith 11089114Smsmith void compile(String... args) { 11189114Smsmith for (int i = 0; i < args.length; i++) { 11239212Sgibbs if (args[i].equals("-d")) { 11339212Sgibbs new File(args[++i]).mkdirs(); 11439212Sgibbs break; 11539212Sgibbs } 11639552Sgibbs } 11739212Sgibbs 11839212Sgibbs StringWriter sw = new StringWriter(); 11939552Sgibbs PrintWriter pw = new PrintWriter(sw); 12039552Sgibbs int rc = com.sun.tools.javac.Main.compile(args, pw); 12139212Sgibbs pw.close(); 12239212Sgibbs String out = sw.toString(); 12339212Sgibbs if (!out.isEmpty()) 12439212Sgibbs System.err.println(out); 12539212Sgibbs if (rc != 0) 12639212Sgibbs error("compilation failed: rc=" + rc); 12739212Sgibbs } 12839212Sgibbs 12939212Sgibbs String doclint(String... args) throws BadArgs, IOException { 13039212Sgibbs StringWriter sw = new StringWriter(); 13139212Sgibbs PrintWriter pw = new PrintWriter(sw); 13239212Sgibbs DocLint dl = new DocLint(); 13339212Sgibbs dl.run(pw, args); 13439212Sgibbs pw.close(); 13539212Sgibbs String out = sw.toString(); 13639212Sgibbs if (!out.isEmpty()) 13739212Sgibbs System.err.println(out); 13839212Sgibbs return out; 13939212Sgibbs } 14039212Sgibbs 14139212Sgibbs File writeFile(String path, String body) throws IOException { 14239212Sgibbs File f = new File(path); 14339212Sgibbs f.getParentFile().mkdirs(); 14439212Sgibbs try (FileWriter fw = new FileWriter(path)) { 14539212Sgibbs fw.write(body); 14639212Sgibbs } 14739212Sgibbs return f; 14839212Sgibbs } 14939212Sgibbs 15039212Sgibbs void error(String msg) { 15139212Sgibbs System.err.println("Error: " + msg); 15239212Sgibbs errors++; 15339212Sgibbs } 15439212Sgibbs 15539212Sgibbs int errors; 15639212Sgibbs} 15739212Sgibbs