JavadocTest.java revision 3767:b265444e51db
1/* 2 * Copyright (c) 2015, 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 8131019 8169561 27 * @summary Test Javadoc 28 * @library /tools/lib 29 * @modules jdk.compiler/com.sun.tools.javac.api 30 * jdk.compiler/com.sun.tools.javac.main 31 * jdk.jshell 32 * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask 33 * @build KullaTesting TestingInputStream Compiler 34 * @run testng JavadocTest 35 */ 36 37import java.io.IOException; 38import java.lang.reflect.Field; 39import java.nio.file.Files; 40import java.nio.file.Path; 41import java.nio.file.Paths; 42import java.util.Arrays; 43import java.util.jar.JarEntry; 44import java.util.jar.JarOutputStream; 45 46import org.testng.annotations.Test; 47 48@Test 49public class JavadocTest extends KullaTesting { 50 51 private final Compiler compiler = new Compiler(); 52 53 public void testJavadoc() { 54 prepareZip(); 55 assertJavadoc("test.Clazz|", "test.Clazz\n" + 56 "Top level. "); 57 assertEval("test.Clazz clz = null;"); 58 assertJavadoc("clz.test(|", "String test.Clazz.test(int p) throws IllegalStateException\n" + 59 " javadoc1A\n" + 60 "\n" + 61 " @param p param\n" + 62 " @throws IllegalStateException exc\n" + 63 " @return value\n"); 64 //undefined handling: 65 assertJavadoc("clz.undef|"); 66 } 67 68 public void testVariableInRepl() { 69 assertEval("Object o;"); 70 assertSignature("o|", "o:java.lang.Object"); 71 } 72 73 private void prepareZip() { 74 String clazz = 75 "package test;\n" + 76 "/**Top level." + 77 " */\n" + 78 "public class Clazz {\n" + 79 " /**\n" + 80 " * javadoc1A\n" + 81 " *\n" + 82 " * @param p param\n" + 83 " * @throws IllegalStateException exc\n" + 84 " * @return value\n" + 85 " */\n" + 86 " public String test(int p) throws IllegalStateException { return null;}\n" + 87 "}\n"; 88 89 Path srcZip = Paths.get("src.zip"); 90 91 try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(srcZip))) { 92 out.putNextEntry(new JarEntry("test/Clazz.java")); 93 out.write(clazz.getBytes()); 94 } catch (IOException ex) { 95 throw new IllegalStateException(ex); 96 } 97 98 compiler.compile(clazz); 99 100 try { 101 Field availableSources = getAnalysis().getClass().getDeclaredField("availableSources"); 102 availableSources.setAccessible(true); 103 availableSources.set(getAnalysis(), Arrays.asList(srcZip)); 104 } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException ex) { 105 throw new IllegalStateException(ex); 106 } 107 addToClasspath(compiler.getClassDir()); 108 } 109 110} 111