1 /*
2  * Copyright (c) 2003, 2018, 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 8149565
27  * @summary Verify that init method works correctly.
28  * @modules jdk.javadoc/jdk.javadoc.internal.tool
29  */
30 
31 import java.nio.file.Path;
32 import java.nio.file.Paths;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.Locale;
36 import java.util.Set;
37 
38 import javax.lang.model.SourceVersion;
39 import javax.tools.Diagnostic.Kind;
40 import javax.tools.DocumentationTool;
41 import javax.tools.DocumentationTool.DocumentationTask;
42 import javax.tools.JavaFileObject;
43 import javax.tools.ToolProvider;
44 
45 import jdk.javadoc.doclet.Doclet;
46 import jdk.javadoc.doclet.Reporter;
47 import jdk.javadoc.doclet.DocletEnvironment;
48 
49 public class VerifyLocale implements Doclet {
50     static String language;
51     static String country;
52     static String variant;
53 
54     Locale locale;
55     Reporter reporter;
56 
main(String[] args)57     public static void main(String[] args) {
58         DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
59         Path thisFile =
60             Paths.get(System.getProperty("test.src", ".")).resolve("VerifyLocale.java");
61         JavaFileObject fo = tool.getStandardFileManager(null, null, null)
62                 .getJavaFileObjects(thisFile).iterator().next();
63 
64         int skipCount = 0;
65         int testCount = 0;
66 
67         for (Locale loc : Locale.getAvailableLocales()) {
68 
69             language = loc.getLanguage();
70             country = loc.getCountry();
71             variant = loc.getVariant();
72             System.err.printf("test locale: %s [%s,%s,%s] %s%n",
73                 loc, language, country, variant, loc.toLanguageTag());
74 
75             // skip locales for which the round-trip fails
76             if (!loc.equals(Locale.forLanguageTag(loc.toLanguageTag()))) {
77                 System.err.println("skipped " + loc + "!");
78                 System.err.println();
79                 skipCount++;
80                 continue;
81             }
82 
83             if (!language.equals("")) {
84                 List<String> options = List.of("-locale", loc.toLanguageTag());
85                 System.err.println("test options: " + options);
86                 DocumentationTask t = tool.getTask(null, null, null,
87                         VerifyLocale.class, options, List.of(fo));
88                 if (!t.call())
89                     throw new Error("Javadoc encountered warnings or errors.");
90                 testCount++;
91             }
92             System.err.println();
93         }
94         System.err.println("Skipped " + skipCount + " locales");
95         System.err.println("Tested " + testCount + " locales");
96     }
97 
run(DocletEnvironment root)98     public boolean run(DocletEnvironment root) {
99         reporter.print(Kind.NOTE, String.format("doclet locale is: %s [%s,%s,%s] %s (%s)",
100                 locale,
101                 locale.getLanguage(),
102                 locale.getCountry(),
103                 locale.getVariant(),
104                 locale.toLanguageTag(),
105                 locale.getDisplayName()));
106         return language.equals(locale.getLanguage())
107                && country.equals(locale.getCountry())
108                && variant.equals(locale.getVariant());
109     }
110 
111     @Override
getName()112     public String getName() {
113         return "Test";
114     }
115 
116     @Override
getSupportedOptions()117     public Set<Option> getSupportedOptions() {
118         return Collections.emptySet();
119     }
120 
121     @Override
getSupportedSourceVersion()122     public SourceVersion getSupportedSourceVersion() {
123         return SourceVersion.latest();
124     }
125 
init(Locale locale, Reporter reporter)126     public void init(Locale locale, Reporter reporter) {
127         this.locale = locale;
128         this.reporter = reporter;
129     }
130 }
131