1/* 2 * Copyright (c) 2016, 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 8160181 8176572 27 * @summary Add lint warning for digits in module names 28 * @library /tools/lib 29 * @modules 30 * jdk.compiler/com.sun.tools.javac.api 31 * jdk.compiler/com.sun.tools.javac.code 32 * jdk.compiler/com.sun.tools.javac.main 33 * @build toolbox.ToolBox toolbox.JavacTask ModuleTestBase 34 * @run main PoorChoiceForModuleNameTest 35 */ 36 37 38import java.nio.file.Path; 39 40import toolbox.JavacTask; 41import toolbox.Task; 42 43public class PoorChoiceForModuleNameTest extends ModuleTestBase { 44 45 public static void main(String... args) throws Exception { 46 new PoorChoiceForModuleNameTest().runTests(); 47 } 48 49 @Test 50 public void testDigitsInModuleNames(Path base) throws Exception { 51 52 Path src = base.resolve("src"); 53 54 // Nitpickable module name 55 Path src_m1 = src.resolve("mango19"); 56 tb.writeJavaFiles(src_m1, "module mango19 { }"); 57 58 // Acceptable module name. 59 Path src_m2 = src.resolve("mango20kg"); 60 tb.writeJavaFiles(src_m2, "module mango20kg { }"); 61 62 // Nitpickable, but should not due to annotation. 63 Path src_m3 = src.resolve("mango100"); 64 tb.writeJavaFiles(src_m3, "@SuppressWarnings(\"module\") module mango100 { }"); 65 66 // Check that there is no warning at use site. 67 Path src_m4 = src.resolve("mangouser"); 68 tb.writeJavaFiles(src_m4, "module mangouser { requires mango19; }"); 69 70 // Check that we warn about component names ending in digit also 71 Path src_m5 = src.resolve("mango1000.mangofruit.mangomodule"); 72 tb.writeJavaFiles(src_m5, "module mango1000.mangofruit.mangomodule { }"); 73 74 // Check that we warn about component names ending in digit also 75 Path src_m6 = src.resolve("mangofruit.mango1000.mangomodule"); 76 tb.writeJavaFiles(src_m6, "module mangofruit.mango1000.mangomodule { }"); 77 78 // Check that we warn about component names ending in digit also 79 Path src_m7 = src.resolve("mangomodule.mangofruit.mango1000"); 80 tb.writeJavaFiles(src_m7, "module mangomodule.mangofruit.mango1000 { }"); 81 82 Path classes = base.resolve("classes"); 83 tb.createDirectories(classes); 84 85 String log = new JavacTask(tb) 86 .options("-XDrawDiagnostics", 87 "-Xlint:module", 88 "-Werror", 89 "--module-source-path", src.toString()) 90 .outdir(classes) 91 .files(findJavaFiles(src)) 92 .run(Task.Expect.FAIL) 93 .writeAll() 94 .getOutput(Task.OutputKind.DIRECT); 95 96 if (!log.contains("module-info.java:1:8: compiler.warn.poor.choice.for.module.name: mango19") || 97 !log.contains("module-info.java:1:8: compiler.warn.poor.choice.for.module.name: mango1000") || 98 !log.contains("module-info.java:1:18: compiler.warn.poor.choice.for.module.name: mango1000") || 99 !log.contains("module-info.java:1:30: compiler.warn.poor.choice.for.module.name: mango1000") || 100 !log.contains("- compiler.err.warnings.and.werror") || 101 !log.contains("1 error") || 102 !log.contains("4 warning")) 103 throw new Exception("expected output not found: " + log); 104 } 105} 106