Path: blob/master/test/langtools/jdk/javadoc/tool/EnsureNewOldDoclet.java
40957 views
/*1* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8035473 8154482 8154399 8159096 8176131 8176331 817751126* @summary make sure the javadoc tool responds correctly,27* to old and new doclet implementations.28* @library /tools/lib29* @build toolbox.ToolBox toolbox.TestRunner30* @run main EnsureNewOldDoclet31*/3233import java.io.*;34import java.nio.file.Path;35import java.util.Arrays;36import java.util.Collections;37import java.util.List;38import java.util.Set;39import java.util.regex.Pattern;40import java.util.stream.Collectors;41import javax.lang.model.element.Element;4243import com.sun.source.doctree.DocTree;4445import toolbox.*;464748/**49* This test ensures the doclet responds correctly when given50* various conditions that force a fall back to the old javadoc51* tool.52*/53public class EnsureNewOldDoclet extends TestRunner {5455final ToolBox tb;56final File testSrc;57final Path javadocPath;58final ExecTask task;59final String testClasses;60final PrintStream ostream;6162final static String CLASS_NAME = "EnsureNewOldDoclet";63final static String OLD_DOCLET_CLASS_NAME = CLASS_NAME + "$OldDoclet";64final static String NEW_DOCLET_CLASS_NAME = CLASS_NAME + "$NewDoclet"; //unused65final static String NEW_TAGLET_CLASS_NAME = CLASS_NAME + "$NewTaglet";6667final static Pattern OLD_HEADER = Pattern.compile(".*This is not the Standard Doclet.*");68final static Pattern NEW_HEADER = Pattern.compile("^Standard Doclet version.*");697071final static String OLD_DOCLET_MARKER = "OLD_DOCLET_MARKER";7273final static String NEW_DOCLET_MARKER = "NEW_DOCLET_MARKER";74final static String NEW_TAGLET_MARKER = "Registered Taglet " + CLASS_NAME + "\\$NewTaglet";7576final static String NEW_STDDOCLET = "jdk.javadoc.doclet.StandardDoclet";777879public EnsureNewOldDoclet() throws Exception {80super(System.err);81ostream = System.err;82testClasses = System.getProperty("test.classes");83tb = new ToolBox();84javadocPath = tb.getJDKTool("javadoc");85task = new ExecTask(tb, javadocPath);86testSrc = new File("Foo.java");87generateSample(testSrc);88}8990void generateSample(File testSrc) throws Exception {91String nl = System.getProperty("line.separator");92String src = Arrays.asList(93"/**",94" * A test class to test javadoc. Nothing more nothing less.",95" */",96" public class Foo{}").stream().collect(Collectors.joining(nl));97tb.writeFile(testSrc.getPath(), src);98}99100public static void main(String... args) throws Exception {101new EnsureNewOldDoclet().runTests();102}103104// input: nothing, default mode105// outcome: new tool and new doclet106@Test107public void testDefault() throws Exception {108setArgs("-classpath", ".", // insulates us from ambient classpath109testSrc.toString());110Task.Result tr = task.run(Task.Expect.SUCCESS);111List<String> err = tr.getOutputLines(Task.OutputKind.STDERR);112checkOutput(testName, err, NEW_HEADER);113}114115// input: new doclet and new taglet116// outcome: new doclet and new taglet should register117@Test118public void testNewDocletNewTaglet() throws Exception {119setArgs("-classpath", ".", // ambient classpath insulation120"-doclet",121NEW_STDDOCLET,122"-taglet",123NEW_TAGLET_CLASS_NAME,124"-tagletpath",125testClasses,126testSrc.toString());127Task.Result tr = task.run(Task.Expect.SUCCESS);128List<String> out = tr.getOutputLines(Task.OutputKind.STDOUT);129List<String> err = tr.getOutputLines(Task.OutputKind.STDERR);130checkOutput(testName, err, NEW_HEADER);131checkOutput(testName, err, NEW_TAGLET_MARKER);132}133134void setArgs(String... args) {135ostream.println("cmds: " + Arrays.asList(args));136task.args(args);137}138139void checkOutput(String testCase, List<String> content, String toFind) throws Exception {140checkOutput(testCase, content, Pattern.compile(".*" + toFind + ".*"));141}142143void checkOutput(String testCase, List<String> content, Pattern toFind) throws Exception {144ostream.println("---" + testCase + "---");145content.stream().forEach(x -> System.out.println(x));146for (String x : content) {147ostream.println(x);148if (toFind.matcher(x).matches()) {149return;150}151}152throw new Exception(testCase + ": Expected string not found: " + toFind);153}154155public static class NewTaglet implements jdk.javadoc.doclet.Taglet {156157@Override158public Set<Location> getAllowedLocations() {159return Collections.emptySet();160}161162@Override163public boolean isInlineTag() {164return true;165}166167@Override168public String getName() {169return "NewTaglet";170}171172@Override173public String toString(List<? extends DocTree> tags, Element element) {174return tags.toString();175}176177}178}179180181