Skip to content

Commit 9df200f

Browse files
committedDec 23, 2021
8278795: Create test library and tests for langtools snippets
Reviewed-by: hannesw
1 parent 7aff03a commit 9df200f

File tree

3 files changed

+804
-0
lines changed

3 files changed

+804
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* Copyright (c) 2021, 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 8272944
27+
* @summary Use snippets in jdk.javadoc documentation
28+
* @library /tools/lib ../../lib
29+
* @modules jdk.compiler/com.sun.tools.javac.api
30+
* jdk.compiler/com.sun.tools.javac.main
31+
* jdk.javadoc/jdk.javadoc.internal.tool
32+
* @build snippets.SnippetUtils toolbox.JavacTask toolbox.ToolBox javadoc.tester.*
33+
* @run main TestDocletExample
34+
*/
35+
36+
import java.nio.file.Files;
37+
import java.nio.file.Path;
38+
import java.util.spi.ToolProvider;
39+
import java.util.stream.Stream;
40+
41+
import snippets.SnippetUtils;
42+
import toolbox.Task;
43+
import toolbox.TestRunner;
44+
import toolbox.ToolBox;
45+
46+
import javax.tools.DiagnosticCollector;
47+
import javax.tools.JavaFileObject;
48+
49+
50+
public class TestDocletExample extends TestRunner {
51+
public static void main(String... args) throws Exception {
52+
var t = new TestDocletExample();
53+
t.runTests(m -> new Object[] { Path.of(m.getName()) });
54+
}
55+
56+
SnippetUtils snippets = new SnippetUtils("jdk.javadoc");
57+
ToolBox tb = new ToolBox();
58+
59+
TestDocletExample() {
60+
super(System.out);
61+
}
62+
63+
@Test
64+
public void testEntryPoint(Path base) throws Exception {
65+
var docletPkg = snippets.getElements().getPackageElement("jdk.javadoc.doclet");
66+
var dc = snippets.getDocTrees().getDocCommentTree(docletPkg);
67+
var entryPointSnippet = snippets.getSnippetById(dc, "entry-point");
68+
var entryPointCode = entryPointSnippet.getBody().getBody();
69+
var code = """
70+
class C {
71+
%s { }
72+
}
73+
""".formatted(entryPointCode);
74+
DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<>();
75+
snippets.parse(code, null, collector);
76+
var diags = collector.getDiagnostics();
77+
if (diags.isEmpty()) {
78+
out.println("parsed entry point snippet");
79+
} else {
80+
diags.forEach(out::println);
81+
throw new Exception("parse failed");
82+
}
83+
}
84+
85+
@Test
86+
public void testDocletExample(Path base) throws Exception {
87+
88+
// get source code
89+
var docletPkg = snippets.getElements().getPackageElement("jdk.javadoc.doclet");
90+
var dc = snippets.getDocTrees().getDocCommentTree(docletPkg);
91+
var exampleSnippet = snippets.getSnippetById(dc, "Example.java");
92+
var exampleCode = exampleSnippet.getBody().getBody();
93+
94+
// compile it
95+
Path src = base.resolve("src");
96+
Path classes = base.resolve("classes");
97+
Files.createDirectories(classes);
98+
99+
tb.writeJavaFiles(src, exampleCode);
100+
new toolbox.JavacTask(tb)
101+
.outdir(classes)
102+
.files(tb.findJavaFiles(src))
103+
.run(Task.Expect.SUCCESS)
104+
.writeAll();
105+
106+
// get demo command
107+
var cmdSnippet = snippets.getSnippetById(dc, "run-doclet");
108+
var cmd = cmdSnippet.getBody().getBody()
109+
.replaceAll("\\s+//.*", "") // remove markup
110+
.replaceAll("\\\\\n", " ") // join lines
111+
.trim();
112+
out.println(cmd);
113+
114+
tb.writeFile(src.resolve("overview.html"),
115+
"""
116+
<!doctype html>
117+
<html><title>Overview</title>
118+
<body>
119+
Overview
120+
</body>
121+
</html>
122+
""");
123+
124+
var cmdWords = Stream.of(cmd.split("\\s+"))
125+
.map(s -> s.replace("source-location", src.toString()))
126+
.map(s -> s.replace("doclet-classes", classes.toString()))
127+
.toList();
128+
var toolName = cmdWords.get(0);
129+
var toolArgs = cmdWords.subList(1, cmdWords.size());
130+
131+
ToolProvider tool = ToolProvider.findFirst(toolName)
132+
.orElseThrow(() -> new Exception("tool not found: " + toolName));
133+
int rc = tool.run(System.out, System.err, toolArgs.toArray(new String[0]));
134+
if (rc != 0) {
135+
throw new Exception("ecommand return code: " + rc);
136+
}
137+
}
138+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright (c) 2021, 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 8272944
27+
* @summary Use snippets in java.compiler documentation
28+
* @library /tools/lib ../../lib
29+
* @modules jdk.compiler/com.sun.tools.javac.api
30+
* jdk.compiler/com.sun.tools.javac.main
31+
* @build snippets.SnippetUtils toolbox.JavacTask toolbox.TestRunner toolbox.ToolBox
32+
* @run main TestJavaxToolsSnippets
33+
*/
34+
35+
import java.io.File;
36+
import java.io.IOException;
37+
import java.io.UncheckedIOException;
38+
import java.nio.file.Files;
39+
import java.nio.file.Path;
40+
import java.util.Objects;
41+
42+
import javax.lang.model.element.Element;
43+
import javax.lang.model.element.TypeElement;
44+
import javax.tools.DiagnosticCollector;
45+
import javax.tools.JavaFileObject;
46+
47+
import com.sun.source.doctree.SnippetTree;
48+
49+
import snippets.SnippetUtils;
50+
import toolbox.JavacTask;
51+
import toolbox.Task;
52+
import toolbox.TestRunner;
53+
import toolbox.ToolBox;
54+
55+
/**
56+
* Tests the snippets in the {@code javax.tools} package, by compiling the
57+
* external snippets and parsing the internal Java snippets.
58+
*/
59+
public class TestJavaxToolsSnippets extends TestRunner {
60+
public static void main(String... args) throws Exception {
61+
new TestJavaxToolsSnippets().runTests(m -> new Object[] { Path.of(m.getName()) });
62+
}
63+
64+
SnippetUtils snippets = new SnippetUtils("java.compiler");
65+
ToolBox tb = new ToolBox();
66+
67+
TestJavaxToolsSnippets() {
68+
super(System.err);
69+
}
70+
71+
@Test
72+
public void testExternalSnippets(Path base) throws Exception {
73+
Path snippetFilesDir = snippets.getSourceDir()
74+
.resolve("java.compiler") // module
75+
.resolve("share").resolve("classes")
76+
.resolve("javax.tools".replace(".", File.separator)) // package
77+
.resolve("snippet-files");
78+
new JavacTask(tb)
79+
.files(tb.findJavaFiles(snippetFilesDir))
80+
.outdir(Files.createDirectories(base.resolve("classes")))
81+
.run(Task.Expect.SUCCESS)
82+
.writeAll();
83+
out.println("Compilation succeeded");
84+
}
85+
86+
@Test
87+
public void testJavaCompilerSnippets(Path base) {
88+
TypeElement te = snippets.getElements().getTypeElement("javax.tools.JavaCompiler");
89+
snippets.scan(te, this::handleSnippet);
90+
}
91+
92+
@Test
93+
public void testJavaFileManagerSnippets(Path base) {
94+
TypeElement te = snippets.getElements().getTypeElement("javax.tools.JavaFileManager");
95+
snippets.scan(te, this::handleSnippet);
96+
}
97+
98+
@Test
99+
public void testStandardJavaFileManagerSnippets(Path base) {
100+
TypeElement te = snippets.getElements().getTypeElement("javax.tools.StandardJavaFileManager");
101+
snippets.scan(te, this::handleSnippet);
102+
}
103+
104+
void handleSnippet(Element e, SnippetTree tree) {
105+
String lang = snippets.getAttr(tree, "lang");
106+
if (Objects.equals(lang, "java")) {
107+
String body = snippets.getBody(tree);
108+
if (body != null) {
109+
String id = snippets.getAttr(tree, "id");
110+
try {
111+
out.println("parsing snippet " + e + ":" + id);
112+
if (snippets.parse(body, out::println)) {
113+
out.println("parsed snippet");
114+
} else {
115+
error("parse failed");
116+
}
117+
} catch (IOException ex) {
118+
throw new UncheckedIOException(ex);
119+
}
120+
}
121+
}
122+
}
123+
}

0 commit comments

Comments
 (0)
Please sign in to comment.