ModifiersTest.java revision 3731:6bb6785c2329
1/* 2 * Copyright (c) 2015, 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 8167643 26 * @summary Tests for modifiers 27 * @build KullaTesting TestingInputStream ExpectedDiagnostic 28 * @run testng ModifiersTest 29 */ 30 31import java.util.ArrayList; 32import java.util.List; 33 34import javax.tools.Diagnostic; 35 36import org.testng.annotations.DataProvider; 37import org.testng.annotations.Test; 38 39@Test 40public class ModifiersTest extends KullaTesting { 41 42 @DataProvider(name = "ignoredModifiers") 43 public Object[][] getTestCases() { 44 List<Object[]> testCases = new ArrayList<>(); 45 String[] ignoredModifiers = new String[] { 46 "static", "final" 47 }; 48 for (String ignoredModifier : ignoredModifiers) { 49 for (ClassType classType : ClassType.values()) { 50 testCases.add(new Object[] { ignoredModifier, classType }); 51 } 52 } 53 return testCases.toArray(new Object[testCases.size()][]); 54 } 55 56 @Test(dataProvider = "ignoredModifiers") 57 public void ignoredModifiers(String modifier, ClassType classType) { 58 assertDeclareWarn1( 59 String.format("%s %s A {}", modifier, classType), "jdk.eval.warn.illegal.modifiers"); 60 assertNumberOfActiveClasses(1); 61 assertClasses(clazz(classType, "A")); 62 assertActiveKeys(); 63 } 64 65 @DataProvider(name = "silentlyIgnoredModifiers") 66 public Object[][] getSilentTestCases() { 67 List<Object[]> testCases = new ArrayList<>(); 68 String[] ignoredModifiers = new String[] { 69 "public", "protected", "private" 70 }; 71 for (String ignoredModifier : ignoredModifiers) { 72 for (ClassType classType : ClassType.values()) { 73 testCases.add(new Object[] { ignoredModifier, classType }); 74 } 75 } 76 return testCases.toArray(new Object[testCases.size()][]); 77 } 78 79 @Test(dataProvider = "silentlyIgnoredModifiers") 80 public void silentlyIgnoredModifiers(String modifier, ClassType classType) { 81 assertEval(String.format("%s %s A {}", modifier, classType)); 82 assertNumberOfActiveClasses(1); 83 assertClasses(clazz(classType, "A")); 84 assertActiveKeys(); 85 } 86 87 public void accessToStaticFieldsOfClass() { 88 assertEval("class A {" + 89 "int x = 14;" + 90 "static int y = 18;" + 91 " }"); 92 assertDeclareFail("A.x;", 93 new ExpectedDiagnostic("compiler.err.non-static.cant.be.ref", 0, 3, 1, -1, -1, Diagnostic.Kind.ERROR)); 94 assertEval("A.y;", "18"); 95 assertEval("new A().x;", "14"); 96 assertEval("A.y = 88;", "88"); 97 assertActiveKeys(); 98 } 99 100 public void accessToStaticMethodsOfClass() { 101 assertEval("class A {" + 102 "void x() {}" + 103 "static void y() {}" + 104 " }"); 105 assertDeclareFail("A.x();", 106 new ExpectedDiagnostic("compiler.err.non-static.cant.be.ref", 0, 3, 1, -1, -1, Diagnostic.Kind.ERROR)); 107 assertEval("A.y();"); 108 assertActiveKeys(); 109 } 110 111 public void accessToStaticFieldsOfInterface() { 112 assertEval("interface A {" + 113 "int x = 14;" + 114 "static int y = 18;" + 115 " }"); 116 assertEval("A.x;", "14"); 117 assertEval("A.y;", "18"); 118 119 assertDeclareFail("A.x = 18;", 120 new ExpectedDiagnostic("compiler.err.cant.assign.val.to.final.var", 0, 3, 1, -1, -1, Diagnostic.Kind.ERROR)); 121 assertDeclareFail("A.y = 88;", 122 new ExpectedDiagnostic("compiler.err.cant.assign.val.to.final.var", 0, 3, 1, -1, -1, Diagnostic.Kind.ERROR)); 123 assertActiveKeys(); 124 } 125 126 public void accessToStaticMethodsOfInterface() { 127 assertEval("interface A { static void x() {} }"); 128 assertEval("A.x();"); 129 assertActiveKeys(); 130 } 131 132 public void finalMethod() { 133 assertEval("class A { final void f() {} }"); 134 assertDeclareFail("class B extends A { void f() {} }", 135 new ExpectedDiagnostic("compiler.err.override.meth", 20, 31, 25, -1, -1, Diagnostic.Kind.ERROR)); 136 assertActiveKeys(); 137 } 138 139 //TODO: is this the right semantics? 140 public void finalConstructor() { 141 assertDeclareFail("class A { final A() {} }", 142 new ExpectedDiagnostic("compiler.err.mod.not.allowed.here", 10, 22, 16, -1, -1, Diagnostic.Kind.ERROR)); 143 assertActiveKeys(); 144 } 145 146 //TODO: is this the right semantics? 147 public void finalDefaultMethod() { 148 assertDeclareFail("interface A { final default void a() {} }", 149 new ExpectedDiagnostic("compiler.err.mod.not.allowed.here", 14, 39, 33, -1, -1, Diagnostic.Kind.ERROR)); 150 assertActiveKeys(); 151 } 152} 153