1/*
2 * Copyright (c) 2006, 2016, 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     6507179
27 * @summary Ensure that "-source" option isn't ignored.
28 * @author  Scott Seligman
29 * @modules jdk.javadoc/jdk.javadoc.internal.tool
30 * @run main/fail SourceOption 7
31 * @run main      SourceOption 9
32 * @run main      SourceOption
33 */
34
35/*
36 * TEST NOTE
37 * With JDK9, this test has been transformed into a NEGATIVE test.
38 *
39 * Generally speaking, this test should check a feature not in at least
40 * one of the currently supported previous versions.  In this manner,
41 * a failure of the -source option to be honored would mean a pass of
42 * the test, and therefore a failure of the -source option.
43 *
44 * For JDK9 and JDK10, both support 1.7, which did not support javac's
45 * lambda construct.  So we set "-source 1.7" to compile a .java file
46 * containing the lambda construct.  javac should fail, thus showing
47 * -source to be working.  Thus the test passes.
48 *
49 * The second jtreg @run command checks to make sure that the source
50 * provided is valid for the current release of the JDK.
51 *
52 *  fixVersion: JDK11
53 *      replace ./p/LambdaConstructTest.java with a missing from
54 *      JDK8, JDK9, or JDK10.  Set -source below appropriately.
55 */
56
57import java.util.ArrayList;
58import java.util.Collections;
59import java.util.List;
60import java.util.Locale;
61import java.util.Set;
62
63import javax.lang.model.SourceVersion;
64import javax.lang.model.util.ElementFilter;
65import javax.tools.Diagnostic.Kind;
66
67import jdk.javadoc.doclet.Doclet;
68import jdk.javadoc.doclet.Doclet.Option;
69import jdk.javadoc.doclet.DocletEnvironment;
70import jdk.javadoc.doclet.Reporter;
71
72public class SourceOption implements Doclet {
73
74    public static void main(String[] args) {
75        List<String> params = new ArrayList<>();
76        params.add("-sourcepath");
77        params.add(System.getProperty("test.src"));
78        params.add("-docletpath");
79        params.add(System.getProperty("test.classes"));
80        params.add("-doclet");
81        params.add("SourceOption");
82        if ((args == null) || (args.length==0)) {
83            System.out.println("NOTE : -source not provided, default taken");
84        } else {
85            params.add("-source");
86            params.add(args[0]);
87            System.out.println("NOTE : -source will be: " + args[0]);
88        }
89        params.add("p");
90        System.out.println("arguments: " + params);
91        if (jdk.javadoc.internal.tool.Main.execute(params.toArray(new String[params.size()])) != 0)
92            throw new Error("Javadoc encountered warnings or errors.");
93    }
94
95    public boolean run(DocletEnvironment root) {
96        ElementFilter.typesIn(root.getIncludedElements());
97        return true;
98    }
99
100    @Override
101    public String getName() {
102        return "Test";
103    }
104
105    @Override
106    public Set<Option> getSupportedOptions() {
107        return Collections.emptySet();
108    }
109
110    @Override
111    public SourceVersion getSupportedSourceVersion() {
112        return SourceVersion.latest();
113    }
114
115    @Override
116    public void init(Locale locale, Reporter reporter) {
117        reporter.print(Kind.NOTE, "init");
118    }
119}
120