1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
/*
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8224613
* @library /tools/lib ../../lib
* @modules jdk.javadoc/jdk.javadoc.internal.tool
* @build javadoc.tester.* toolbox.ToolBox builder.ClassBuilder
* @run main/othervm OptionProcessingFailureTest
*/
import builder.ClassBuilder;
import javadoc.tester.JavadocTester;
import jdk.javadoc.doclet.Doclet;
import jdk.javadoc.doclet.DocletEnvironment;
import jdk.javadoc.doclet.Reporter;
import toolbox.ToolBox;
import javax.lang.model.SourceVersion;
import javax.tools.Diagnostic;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
/*
* Tests that a particular error is raised only if a doclet has not reported any
* errors (to Reporter), and yet at least one of that Doclet's options returned
* `false` from the `Doclet.Option.process` method.
*
* This test explores scenarios generated by combining a few independent factors
* involved in failing a doclet run. These factors are:
*
* 1. Reporting errors in Doclet.init(...)
* 2. Reporting errors in Doclet.Option.process(...)
* 3. Returning `false` from Doclet.Option.process(...)
*
* A doclet that behaves according to a scenario is run by the javadoc tool. An
* output of that run is then examined for presence of a particular error. That
* error must be in the output if and only if one or more options returned
* `false` from Doclet.Option.process(...) and no other errors were reported.
*
* Configuration of the doclet is performed out-of-band, using System Properties
* (when running the javadoc tool from the command line this could be achieved
* using -J-Dsystem.property.name=value syntax). The "puppet" doclet is
* instructed on which options is has, how many errors it should report,
* and how each individual option should be processed.
*/
public class OptionProcessingFailureTest extends JavadocTester {
private final ToolBox tb = new ToolBox();
public static void main(String... args) throws Exception {
new OptionProcessingFailureTest().runTests();
}
@Test
public void test(Path base) throws IOException {
Path srcDir = base.resolve("src");
// Since the minimal successful run of the javadoc tool with a custom
// doclet involves source files, a package with a class in it is generated:
new ClassBuilder(tb, "pkg.A")
.setModifiers("public", "class")
.write(srcDir);
generateScenarios(base, this::testScenario);
}
private static void generateScenarios(Path base, ScenarioConsumer consumer) {
for (int nInitErrors : List.of(0, 1)) { // the number of errors the Doclet.init method should report
for (int nOptions : List.of(0, 1, 2)) { // the number of options a doclet should have
generateOptionsCombinations(base, nInitErrors, nOptions, consumer);
}
}
}
private static void generateOptionsCombinations(Path base,
int nInitErrors,
int nOptions,
ScenarioConsumer consumer) {
var emptyDescriptions = new PuppetOption.Description[nOptions];
generateOptionsCombinations(base, nInitErrors, 0, emptyDescriptions, consumer);
}
private static void generateOptionsCombinations(Path base,
int nInitErrors,
int descriptionsIndex,
PuppetOption.Description[] descriptions,
ScenarioConsumer consumer) {
if (descriptionsIndex >= descriptions.length) {
consumer.consume(base, nInitErrors, List.of(descriptions));
return;
}
for (boolean success : List.of(true, false)) { // return values of Doclet.Options.process
for (int nErrors : List.of(0, 1)) { // the number of errors Doclet.Options.process should report
String optionName = "--doclet-option-" + descriptionsIndex;
descriptions[descriptionsIndex] = new PuppetOption.Description(optionName, success, nErrors);
generateOptionsCombinations(base, nInitErrors, descriptionsIndex + 1, descriptions, consumer);
}
}
}
private void testScenario(Path base,
int nInitErrors,
List<PuppetOption.Description> optionDescriptions) {
System.out.printf("nInitErrors=%s, optionDescriptions=%s%n", nInitErrors, optionDescriptions);
List<String> args = new ArrayList<>(
List.of("-doclet", PuppetDoclet.class.getName(),
"-docletpath", System.getProperty("test.classes", "."),
"-sourcepath", base.resolve("src").toString(),
"pkg"));
optionDescriptions.forEach(d -> args.add(d.name)); // options passed to the doclet
/* The puppet doclet does not create any output directories, so there is
no need for any related checks; other checks are disabled to speed up
the processing and reduce the size of the output. */
setOutputDirectoryCheck(DirectoryCheck.NONE);
setAutomaticCheckAccessibility(false);
setAutomaticCheckLinks(false);
/* Ideally, the system properties should've been passed to the `javadoc`
method below. However, since there's no such option, the system
properties are manipulated in an external fashion. */
String descriptions = System.getProperty("puppet.descriptions");
if (descriptions != null) {
throw new IllegalStateException(descriptions);
}
String errors = System.getProperty("puppet.errors");
if (errors != null) {
throw new IllegalStateException(errors);
}
try {
System.setProperty("puppet.descriptions", PuppetDoclet.descriptionsToString(optionDescriptions));
System.setProperty("puppet.errors", String.valueOf(nInitErrors));
javadoc(args.toArray(new String[0]));
} finally {
System.clearProperty("puppet.descriptions");
System.clearProperty("puppet.errors");
}
long sumErrors = optionDescriptions.stream().mapToLong(d -> d.nProcessErrors).sum() + nInitErrors;
boolean success = optionDescriptions.stream().allMatch(d -> d.success);
checkOutput(Output.OUT, sumErrors != 0 || !success, "error: ");
}
/* Creating a specialized consumer is even more lightweight than creating a POJO */
@FunctionalInterface
public interface ScenarioConsumer {
void consume(Path src, int nInitErrors, List<PuppetOption.Description> optionDescriptions);
}
public static final class PuppetDoclet implements Doclet {
private final int nErrorsInInit;
private final Set<PuppetOption.Description> descriptions;
private Set<Option> options;
private Reporter reporter;
public PuppetDoclet() {
this(nInitErrorsFromString(System.getProperty("puppet.errors")),
descriptionsFromString(System.getProperty("puppet.descriptions")));
}
private static Collection<PuppetOption.Description> descriptionsFromString(String value) {
if (value.isEmpty())
return List.of(); // special case of description of zero-length
String[] split = value.split(",");
List<PuppetOption.Description> descriptions = new ArrayList<>();
for (int i = 0; i < split.length; i += 3) {
String name = split[i];
boolean success = Boolean.parseBoolean(split[i + 1]);
int nErrors = Integer.parseInt(split[i + 2]);
descriptions.add(new PuppetOption.Description(name, success, nErrors));
}
return descriptions;
}
public static String descriptionsToString(Collection<? extends PuppetOption.Description> descriptions) {
return descriptions.stream()
.map(d -> d.name + "," + d.success + "," + d.nProcessErrors)
.collect(Collectors.joining(","));
}
private static int nInitErrorsFromString(String value) {
return Integer.parseInt(Objects.requireNonNull(value));
}
public PuppetDoclet(int nErrorsInInit, Collection<PuppetOption.Description> descriptions) {
if (nErrorsInInit < 0) {
throw new IllegalArgumentException();
}
this.nErrorsInInit = nErrorsInInit;
this.descriptions = Set.copyOf(descriptions);
}
@Override
public void init(Locale locale, Reporter reporter) {
this.reporter = reporter;
for (int i = 0; i < nErrorsInInit; i++) {
reporter.print(Diagnostic.Kind.ERROR, "init error #%s".formatted(i));
}
}
@Override
public String getName() {
return getClass().getSimpleName();
}
@Override
public Set<? extends Option> getSupportedOptions() {
if (options == null) {
options = new HashSet<>();
for (PuppetOption.Description d : descriptions) {
options.add(new PuppetOption(d, reporter));
}
}
return options;
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
@Override
public boolean run(DocletEnvironment environment) {
return true;
}
}
private static final class PuppetOption implements Doclet.Option {
private final Reporter reporter;
private final Description description;
public PuppetOption(Description description, Reporter reporter) {
this.description = description;
this.reporter = reporter;
}
@Override
public int getArgumentCount() {
return 0;
}
@Override
public String getDescription() {
return description.name
+ ": success=" + description.success
+ ", nProcessErrors=" + description.nProcessErrors;
}
@Override
public Kind getKind() {
return Kind.STANDARD;
}
@Override
public List<String> getNames() {
return List.of(description.name);
}
@Override
public String getParameters() {
return "";
}
@Override
public boolean process(String option, List<String> arguments) {
for (int i = 0; i < description.nProcessErrors; i++) {
reporter.print(Diagnostic.Kind.ERROR,
"option: '%s', process error #%s".formatted(description.name, i));
}
return description.success;
}
public final static class Description {
public final String name;
public final boolean success;
public final int nProcessErrors;
Description(String name, boolean success, int nErrors) {
this.name = name;
this.success = success;
this.nProcessErrors = nErrors;
}
@Override
public String toString() {
return "Description{" +
"name='" + name + '\'' +
", success=" + success +
", nProcessErrors=" + nProcessErrors +
'}';
}
}
}
}
|