RepeatedUsesAndProvidesTest.java revision 3792:d516975e8110
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 8145012 26 * @summary Javac doesn't report errors on duplicate uses or provides 27 * @library /tools/lib 28 * @modules 29 * jdk.compiler/com.sun.tools.javac.api 30 * jdk.compiler/com.sun.tools.javac.main 31 * @build toolbox.ToolBox toolbox.JavacTask ModuleTestBase 32 * @run main RepeatedUsesAndProvidesTest 33 */ 34 35import java.nio.file.Files; 36import java.nio.file.Path; 37 38import toolbox.JavacTask; 39import toolbox.Task; 40import toolbox.ToolBox; 41 42public class RepeatedUsesAndProvidesTest extends ModuleTestBase { 43 public static void main(String... args) throws Exception { 44 RepeatedUsesAndProvidesTest t = new RepeatedUsesAndProvidesTest(); 45 t.runTests(); 46 } 47 48 @Test 49 public void testDuplicateUses(Path base) throws Exception { 50 Path src = base.resolve("src"); 51 tb.writeJavaFiles(src, 52 "module m { uses p1.C1; uses p1.C1; }", 53 "package p1; public class C1 {}"); 54 Path classes = base.resolve("classes"); 55 Files.createDirectories(classes); 56 57 String log = new JavacTask(tb) 58 .options("-XDrawDiagnostics") 59 .outdir(classes) 60 .files(findJavaFiles(src)) 61 .run(Task.Expect.FAIL) 62 .writeAll() 63 .getOutput(Task.OutputKind.DIRECT); 64 if (!log.contains("module-info.java:1:24: compiler.err.duplicate.uses: p1.C1")) 65 throw new Exception("expected output not found"); 66 } 67 68 @Test 69 public void testDuplicateProvides(Path base) throws Exception { 70 Path src = base.resolve("src"); 71 tb.writeJavaFiles(src, 72 "module m { provides p1.C1 with p2.C2; provides p1.C1 with p2.C2; }", 73 "package p1; public class C1 {}", 74 "package p2; public class C2 extends p1.C1 {}"); 75 Path classes = base.resolve("classes"); 76 Files.createDirectories(classes); 77 78 String log = new JavacTask(tb) 79 .options("-XDrawDiagnostics") 80 .outdir(classes) 81 .files(findJavaFiles(src)) 82 .run(Task.Expect.FAIL) 83 .writeAll() 84 .getOutput(Task.OutputKind.DIRECT); 85 if (!log.contains("module-info.java:1:61: compiler.err.duplicate.provides")) 86 throw new Exception("expected output not found"); 87 } 88} 89