EnsureNewOldDoclet.java revision 3233:b5d08bc0d224
1/* 2 * Copyright (c) 2015, 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 8035473 27 * @summary make sure the new doclet is invoked by default, and -Xold 28 */ 29 30import java.io.*; 31import java.util.ArrayList; 32import java.util.List; 33import java.util.regex.Pattern; 34 35/** 36 * Dummy javadoc comment. 37 */ 38public class EnsureNewOldDoclet { 39 40 final File javadoc; 41 final File testSrc; 42 final String thisClassName; 43 44 final static Pattern Expected1 = Pattern.compile("^Standard Doclet \\(Next\\) version.*"); 45 final static Pattern Expected2 = Pattern.compile("^Standard Doclet version.*"); 46 47 public EnsureNewOldDoclet() { 48 File javaHome = new File(System.getProperty("java.home")); 49 if (javaHome.getName().endsWith("jre")) 50 javaHome = javaHome.getParentFile(); 51 javadoc = new File(new File(javaHome, "bin"), "javadoc"); 52 testSrc = new File(System.getProperty("test.src")); 53 thisClassName = EnsureNewOldDoclet.class.getName(); 54 } 55 56 public static void main(String... args) throws Exception { 57 EnsureNewOldDoclet test = new EnsureNewOldDoclet(); 58 test.run1(); 59 test.run2(); 60 } 61 62 // make sure new doclet is invoked by default 63 void run1() throws Exception { 64 List<String> output = doTest(javadoc.getPath(), 65 "-J-Xbootclasspath:" + System.getProperty("sun.boot.class.path"), 66 "-classpath", ".", // insulates us from ambient classpath 67 "-Xdoclint:none", 68 "-package", 69 new File(testSrc, thisClassName + ".java").getPath()); 70 System.out.println(output); 71 for (String x : output) { 72 if (Expected1.matcher(x).matches()) { 73 return; 74 } 75 } 76 throw new Exception("run1: Expected string not found:"); 77 } 78 79 // make sure the old doclet is invoked with -Xold 80 void run2() throws Exception { 81 List<String> output = doTest(javadoc.getPath(), 82 "-Xold", 83 "-J-Xbootclasspath:" + System.getProperty("sun.boot.class.path"), 84 "-classpath", ".", // insulates us from ambient classpath 85 "-Xdoclint:none", 86 "-package", 87 new File(testSrc, thisClassName + ".java").getPath()); 88 89 for (String x : output) { 90 if (Expected2.matcher(x).matches()) { 91 throw new Exception("run2: Expected string not found"); 92 } 93 return; 94 } 95 } 96 97 /** 98 * More dummy comments. 99 */ 100 List<String> doTest(String... args) throws Exception { 101 List<String> output = new ArrayList<>(); 102 // run javadoc in separate process to ensure doclet executed under 103 // normal user conditions w.r.t. classloader 104 Process p = new ProcessBuilder() 105 .command(args) 106 .redirectErrorStream(true) 107 .start(); 108 try (BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()))) { 109 String line = in.readLine(); 110 while (line != null) { 111 output.add(line.trim()); 112 line = in.readLine(); 113 } 114 } 115 int rc = p.waitFor(); 116 if (rc != 0) 117 throw new Exception("javadoc failed, rc:" + rc); 118 return output; 119 } 120} 121