AbstractOrInnerClassServiceImplTest.java revision 3294:9adfb22ff08f
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 8145016 26 * @summary Javac doesn't report errors on service implementation which cannot be initialized 27 * @library /tools/lib 28 * @modules 29 * jdk.compiler/com.sun.tools.javac.api 30 * jdk.compiler/com.sun.tools.javac.main 31 * jdk.jdeps/com.sun.tools.javap 32 * @build ToolBox ModuleTestBase 33 * @run main AbstractOrInnerClassServiceImplTest 34 */ 35 36import java.nio.file.Files; 37import java.nio.file.Path; 38 39public class AbstractOrInnerClassServiceImplTest extends ModuleTestBase { 40 public static void main(String... args) throws Exception { 41 AbstractOrInnerClassServiceImplTest t = new AbstractOrInnerClassServiceImplTest(); 42 t.runTests(); 43 } 44 45 @Test 46 void testAbstractServiceImpl(Path base) throws Exception { 47 Path src = base.resolve("src"); 48 tb.writeJavaFiles(src, 49 "module m { provides p1.Service with p2.Impl; }", 50 "package p1; public interface Service { }", 51 "package p2; public interface Impl extends p1.Service { }"); 52 Path classes = base.resolve("classes"); 53 Files.createDirectories(classes); 54 55 String log = tb.new JavacTask() 56 .options("-XDrawDiagnostics") 57 .outdir(classes) 58 .files(findJavaFiles(src)) 59 .run(ToolBox.Expect.FAIL) 60 .writeAll() 61 .getOutput(ToolBox.OutputKind.DIRECT); 62 if (!log.contains("module-info.java:1:39: compiler.err.service.implementation.is.abstract: p2.Impl")) 63 throw new Exception("expected output not found"); 64 } 65 66 @Test 67 void testInnerClassServiceImpl(Path base) throws Exception { 68 Path src = base.resolve("src"); 69 tb.writeJavaFiles(src, 70 "module m { provides p1.Service with p2.Outer.Inner; }", 71 "package p1; public interface Service { }", 72 "package p2; public class Outer { public class Inner implements p1.Service {} }"); 73 Path classes = base.resolve("classes"); 74 Files.createDirectories(classes); 75 76 String log = tb.new JavacTask() 77 .options("-XDrawDiagnostics") 78 .outdir(classes) 79 .files(findJavaFiles(src)) 80 .run(ToolBox.Expect.FAIL) 81 .writeAll() 82 .getOutput(ToolBox.OutputKind.DIRECT); 83 if (!log.contains("module-info.java:1:45: compiler.err.service.implementation.is.inner: p2.Outer.Inner")) 84 throw new Exception("expected output not found"); 85 } 86 87 @Test 88 void testInnerInterfaceServiceImpl(Path base) throws Exception { 89 Path src = base.resolve("src"); 90 tb.writeJavaFiles(src, 91 "module m { provides p1.Service with p2.Outer.Inner; }", 92 "package p1; public interface Service { }", 93 "package p2; public class Outer { public interface Inner extends p1.Service {} }"); 94 Path classes = base.resolve("classes"); 95 Files.createDirectories(classes); 96 97 String log = tb.new JavacTask() 98 .options("-XDrawDiagnostics") 99 .outdir(classes) 100 .files(findJavaFiles(src)) 101 .run(ToolBox.Expect.FAIL) 102 .writeAll() 103 .getOutput(ToolBox.OutputKind.DIRECT); 104 if (!log.contains("module-info.java:1:45: compiler.err.service.implementation.is.abstract: p2.Outer.Inner")) 105 throw new Exception("expected output not found"); 106 } 107} 108