T6942366.java revision 2721:f7ce2cfa4cdb
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 6942366 27 * @summary javadoc no longer inherits doc from sourcepath 28 * @build p.Base Test 29 * @run main T6942366 30 */ 31 32import java.io.*; 33import java.util.*; 34 35public class T6942366 { 36 public static void main(String... args) throws Exception { 37 new T6942366().run(); 38 } 39 40 File testSrc; 41 File testClasses; 42 int count; 43 int errors; 44 45 void run() throws Exception { 46 testSrc = new File(System.getProperty("test.src")); 47 testClasses = new File(System.getProperty("test.classes")); 48 49 test(true, false); 50 test(false, true); 51 test(true, true); 52 53 if (errors > 0) 54 throw new Exception(errors + " errors found"); 55 } 56 57 void test(boolean useSourcePath, boolean useClassPath) throws Exception { 58 System.out.println("test " + (++count) + " sp:" + useSourcePath + " cp:" + useClassPath); 59 File testDir = new File("test" + count); 60 testDir.mkdirs(); 61 62 List<String> args = new ArrayList<String>(); 63 //args.add("-verbose"); 64 args.add("-d"); 65 args.add(testDir.getPath()); 66 if (useSourcePath) { 67 args.add("-sourcepath"); 68 args.add(testSrc.getPath()); 69 } 70 if (useClassPath) { 71 args.add("-classpath"); 72 args.add(testClasses.getPath()); 73 } else { 74 // override classpath to avoid stuff jtreg might have put on papth 75 args.add("-classpath"); 76 args.add("."); 77 } 78 79 args.add(new File(testSrc, "Test.java").getPath()); 80 System.out.println("javadoc: " + args); 81 82 int rc = com.sun.tools.javadoc.Main.execute(args.toArray(new String[args.size()])); 83 if (rc != 0) 84 throw new Exception("unexpected exit from javadoc, rc=" + rc); 85 86 if (useSourcePath && useClassPath) { 87 long srcLastMod = new File(testSrc, "Test.java").lastModified(); 88 long classLastMod = new File(testClasses, "Test.class").lastModified(); 89 System.out.println("Test.java last modified: " + new Date(srcLastMod)); 90 System.out.println("Test.class last modified: " + new Date(classLastMod)); 91 System.out.println((srcLastMod > classLastMod ? "source" : "class") + " is newer"); 92 } 93 94 String s = "javadoc-for-Base.m"; 95 boolean expect = useSourcePath; 96 boolean found = contains(new File(testDir, "Test.html"), s); 97 if (found) { 98 if (expect) 99 System.out.println("javadoc content \"" + s + "\" found, as expected"); 100 else 101 error("javadoc content \"" + s + "\" found unexpectedly"); 102 } else { 103 if (expect) 104 error("javadoc content \"" + s + "\" not found"); 105 else 106 System.out.println("javadoc content \"" + s + "\" not found, as expected"); 107 } 108 109 System.out.println(); 110 } 111 112 boolean contains(File f, String s) throws Exception { 113 byte[] buf = new byte[(int) f.length()]; 114 try (DataInputStream in = new DataInputStream(new FileInputStream(f))) { 115 in.readFully(buf); 116 } 117 return new String(buf).contains(s); 118 } 119 120 void error(String msg) { 121 System.out.println("Error: " + msg); 122 errors++; 123 } 124 125} 126 127