NPEDuplicateClassNamesTest.java revision 3969:7729c633b9f1
1/* 2 * Copyright (c) 2017, 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 8174073 27 * @summary NPE caused by link reference to class 28 * @modules jdk.compiler/com.sun.tools.javac.api 29 * jdk.compiler/com.sun.tools.javac.main 30 * @library /tools/lib 31 * @build toolbox.JavacTask toolbox.TestRunner toolbox.ToolBox 32 * @run main NPEDuplicateClassNamesTest 33 */ 34 35import java.io.IOException; 36import java.nio.file.Files; 37import java.nio.file.Path; 38import java.nio.file.Paths; 39import java.util.Arrays; 40import java.util.List; 41import java.util.Objects; 42 43import toolbox.JavacTask; 44import toolbox.Task; 45import toolbox.TestRunner; 46import toolbox.ToolBox; 47 48public class NPEDuplicateClassNamesTest extends TestRunner { 49 50 public static void main(String... args) throws Exception { 51 NPEDuplicateClassNamesTest t = new NPEDuplicateClassNamesTest(); 52 t.runTests(); 53 } 54 55 private final ToolBox tb = new ToolBox(); 56 private final String class1 = 57 "package com;\n" + 58 "/***/\n" + 59 "public class MyClass {}"; 60 private final String class2 = 61 "package com;\n" + 62 "/**\n" + 63 " * The following link tag causes a NullPointerException: {@link Requirements}. \n" + 64 " */\n" + 65 "public class MyClass {}"; 66 67 NPEDuplicateClassNamesTest() throws IOException { 68 super(System.err); 69 } 70 71 @Test 72 public void testDuplicateClassNames() throws IOException { 73 Path src = Paths.get("src"); 74 Path one = src.resolve("one"); 75 Path two = src.resolve("two"); 76 Path classes = Paths.get("classes"); 77 Files.createDirectories(classes); 78 tb.writeJavaFiles(one, class1); 79 tb.writeJavaFiles(two, class2); 80 81 List<String> expected = Arrays.asList( 82 "MyClass.java:5:8: compiler.err.duplicate.class: com.MyClass", 83 "MyClass.java:3:65: compiler.err.proc.messager: reference not found", 84 "2 errors"); 85 List<String> output = new JavacTask(tb) 86 .outdir(classes) 87 .options("-XDrawDiagnostics", "-Xdoclint:all", "-XDdev") 88 .files(tb.findJavaFiles(src)) 89 .run(Task.Expect.FAIL) 90 .writeAll() 91 .getOutputLines(Task.OutputKind.DIRECT); 92 93 if (!Objects.equals(output, expected)) { 94 throw new IllegalStateException("incorrect output; actual=" + output + "; expected=" + expected); 95 } 96 } 97} 98