Path: blob/master/test/langtools/jdk/javadoc/tool/8224612/OptionsTest.java
40971 views
/*1* Copyright (c) 2020, 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 822461226* @key randomness27* @library /tools/lib ../../lib28* @modules jdk.javadoc/jdk.javadoc.internal.tool29* @build javadoc.tester.*30* @run main/othervm OptionsTest31*/3233import javadoc.tester.JavadocTester;34import jdk.javadoc.doclet.Doclet;35import jdk.javadoc.doclet.DocletEnvironment;36import jdk.javadoc.doclet.Reporter;3738import javax.lang.model.SourceVersion;39import java.nio.file.Path;40import java.nio.file.Paths;41import java.util.Collections;42import java.util.HashSet;43import java.util.LinkedHashSet;44import java.util.List;45import java.util.Locale;46import java.util.Random;47import java.util.Set;48import java.util.TreeSet;49import java.util.function.Supplier;5051public class OptionsTest extends JavadocTester {5253public static void main(String... args) throws Exception {54new OptionsTest().runTests(m -> new Object[]{Paths.get(m.getName())});55}5657@Test58public void testEmptySupportedOptionsDoclet(Path base) {59test(EmptySupportedOptionsDoclet.class);60}6162private void test(Class<? extends Doclet> _class) {63javadoc("-doclet", _class.getName(),64"-docletpath", System.getProperty("test.classes", "."),65"--help");66checkExit(Exit.OK);67checkOutput(Output.OUT, false, "javadoc: error - fatal error encountered: java.lang.NullPointerException");68checkOutput(Output.OUT, false, "Provided by the %s doclet:".formatted(_class.getSimpleName()));69}7071@Test72public void testNullSupportedOptionsDoclet(Path base) {73test(NullSupportedOptionsDoclet.class);74}7576public static final class EmptySupportedOptionsDoclet implements Doclet {7778private final Random random;7980public EmptySupportedOptionsDoclet() {81long seed = Long.getLong("jdk.test.lib.random.seed", System.currentTimeMillis());82System.out.println("new java.util.Random(" + seed + ")");83this.random = new Random(seed);84}8586@Override87public void init(Locale locale, Reporter reporter) {88}8990@Override91public String getName() {92return getClass().getSimpleName();93}9495@Override96public Set<? extends Option> getSupportedOptions() {97return randomEmptySet();98}99100/*101* This method is used to check that emptiness of a set is determined102* by value (or in this case, by behavior), rather than by reference103* (i.e. there's no code like `s == Collections.EMPTY_SET`, etc.)104*/105private Set<? extends Option> randomEmptySet() {106List<Supplier<Set<? extends Option>>> emptySets = List.of(107Set::of,108Collections::emptySet,109HashSet::new,110TreeSet::new,111LinkedHashSet::new112);113int idx = random.nextInt(emptySets.size());114return emptySets.get(idx).get();115}116117@Override118public SourceVersion getSupportedSourceVersion() {119return SourceVersion.latestSupported();120}121122@Override123public boolean run(DocletEnvironment environment) {124return true;125}126}127128/**129* An implementation of an otherwise well-behaving Doclet, that returns130* {@code null} from {@link #getSupportedOptions}.131*/132public static final class NullSupportedOptionsDoclet implements Doclet {133134@Override135public void init(Locale locale, Reporter reporter) {136}137138@Override139public String getName() {140return getClass().getSimpleName();141}142143@Override144public Set<? extends Option> getSupportedOptions() {145return null;146}147148@Override149public SourceVersion getSupportedSourceVersion() {150return SourceVersion.latestSupported();151}152153@Override154public boolean run(DocletEnvironment environment) {155return true;156}157}158}159160161