Path: blob/master/test/langtools/jdk/javadoc/tool/enum/docComments/EnumCommentTest.java
40983 views
/*1* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 442106626* @summary Verify the comments in an enum type.27* @library ../../../lib28* @modules jdk.javadoc/jdk.javadoc.internal.tool29* @build javadoc.tester.*30* @run main EnumCommentTest31*/3233import javax.lang.model.element.ElementKind;34import javax.lang.model.element.ExecutableElement;35import javax.lang.model.element.TypeElement;36import javax.lang.model.element.VariableElement;37import javax.lang.model.util.ElementFilter;38import javax.lang.model.util.Elements;3940import javadoc.tester.JavadocTester;41import javadoc.tester.TestDoclet;42import jdk.javadoc.doclet.DocletEnvironment;4344public class EnumCommentTest extends JavadocTester {4546public static void main(String[] args) throws Exception {47JavadocTester t = new EnumCommentTest();48t.runTests();49}5051@Test52public void testEnumComments() {53javadoc("-sourcepath", testSrc,54"-docletpath", System.getProperty("test.class.path"),55"-doclet", "EnumCommentTest$ThisDoclet",56"pkg1");57checkExit(Exit.OK);58}5960public static class ThisDoclet extends TestDoclet {61public boolean run(DocletEnvironment env) {62Elements elements = env.getElementUtils();6364System.err.println("incl " + env.getIncludedElements());65TypeElement operation = env.getIncludedElements()66.stream()67.filter(e -> e.getKind() == ElementKind.ENUM)68.map(e -> (TypeElement) e)69.findFirst()70.orElseThrow(() -> new Error("can't find enum Operation"));7172boolean ok = checkComment(elements.getDocComment(operation).trim(),73"Arithmetic operations.");7475for (VariableElement f : ElementFilter.fieldsIn(operation.getEnclosedElements())) {76if (f.getSimpleName().contentEquals("plus")) {77ok = checkComment(elements.getDocComment(f).trim(),78"Addition")79&& ok;80for (ExecutableElement m : ElementFilter.methodsIn(operation.getEnclosedElements())) {81if (m.getSimpleName().contentEquals("eval")) {82ok = checkComment(elements.getDocComment(m).trim(),83"Perform arithmetic operation represented by this constant.")84&& ok;85break;86}87}88break;89}90}91if (!ok) {92throw new Error("Comments don't match expectations.");93} else {94return true;95}96}9798private boolean checkComment(String found, String expected) {99System.out.println("expected: \"" + expected + "\"");100System.out.println("found: \"" + found + "\"");101return expected.equals(found);102}103}104}105106107