1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/StringsInSwitch/OneCaseSwitches.java Mon Nov 02 21:36:59 2009 -0800 1.3 @@ -0,0 +1,303 @@ 1.4 +/* 1.5 + * Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 6827009 1.30 + * @summary Positive tests for strings in switch with few alternatives. 1.31 + * @compile/fail -source 6 OneCaseSwitches.java 1.32 + * @compile OneCaseSwitches.java 1.33 + * @run main OneCaseSwitches 1.34 + * @author Joseph D. Darcy 1.35 + */ 1.36 + 1.37 +import java.lang.reflect.*; 1.38 +import java.lang.annotation.*; 1.39 +import java.util.*; 1.40 +import static java.lang.annotation.RetentionPolicy.*; 1.41 + 1.42 +public class OneCaseSwitches { 1.43 + @Retention(RUNTIME) 1.44 + @interface TestMeForNull {} 1.45 + 1.46 + @TestMeForNull 1.47 + public static int zeroCasesNoDefault(String s, Set<String> stringSet, boolean expected) { 1.48 + int failures = 0; 1.49 + switch(s) { 1.50 + } 1.51 + return failures; 1.52 + } 1.53 + 1.54 + @TestMeForNull 1.55 + public static int zeroCasesWithDefault(String s, Set<String> stringSet, boolean expected) { 1.56 + int failures = 2; 1.57 + boolean addResult; 1.58 + 1.59 + switch(s) { 1.60 + default: 1.61 + failures = 0; 1.62 + addResult = stringSet.add(s); 1.63 + if (addResult != expected) { 1.64 + failures++; 1.65 + System.err.println("zeroCaseWithDefault: Expectedly got add result of " + addResult + 1.66 + " on string " + s); 1.67 + } 1.68 + } 1.69 + 1.70 + return failures; 1.71 + } 1.72 + 1.73 + @TestMeForNull 1.74 + public static int zeroCasesWithDefaultBreak(String s, Set<String> stringSet, boolean expected) { 1.75 + int failures = 2; 1.76 + boolean addResult; 1.77 + 1.78 + switch(s) { 1.79 + default: 1.80 + failures = zeroCasesWithDefault(s, stringSet, expected); 1.81 + break; 1.82 + } 1.83 + 1.84 + return failures; 1.85 + } 1.86 + 1.87 + @TestMeForNull 1.88 + public static int oneCaseNoDefault(String s, Set<String> stringSet, boolean expected) { 1.89 + int failures = 2; 1.90 + boolean addResult; 1.91 + 1.92 + switch(s) { 1.93 + case "foo": 1.94 + failures = 0; 1.95 + addResult = stringSet.add(s); 1.96 + if (addResult != expected) { 1.97 + failures++; 1.98 + System.err.println("oneCaseNoDefault: Unexpectedly got add result of " + addResult + 1.99 + " on string " + s); 1.100 + } 1.101 + } 1.102 + 1.103 + return failures; 1.104 + } 1.105 + 1.106 + @TestMeForNull 1.107 + public static int oneCaseNoDefaultBreak(String s, Set<String> stringSet, boolean expected) { 1.108 + int failures = 2; 1.109 + boolean addResult; 1.110 + 1.111 + switch(s) { 1.112 + case "foo": 1.113 + failures = oneCaseNoDefaultBreak(s, stringSet, expected); 1.114 + break; 1.115 + } 1.116 + 1.117 + return failures; 1.118 + } 1.119 + 1.120 + @TestMeForNull 1.121 + public static int oneCaseWithDefault(String s, Set<String> stringSet, boolean expected) { 1.122 + int failures = 2; 1.123 + boolean addResult;; 1.124 + 1.125 + switch(s) { 1.126 + case "foo": 1.127 + failures = 0; 1.128 + addResult = stringSet.add(s); 1.129 + if (addResult != expected) { 1.130 + failures++; 1.131 + System.err.println("oneCaseNoDefault: Expectedly got add result of " + addResult + 1.132 + " on string " + s); 1.133 + } 1.134 + break; 1.135 + default: 1.136 + break; 1.137 + } 1.138 + 1.139 + return failures; 1.140 + } 1.141 + 1.142 + @TestMeForNull 1.143 + public static int oneCaseBreakOnly(String s, Set<String> stringSet, boolean expected) { 1.144 + int failures = 1; 1.145 + switch(s) { 1.146 + case "foo": 1.147 + break; 1.148 + } 1.149 + failures = 0; 1.150 + return failures; 1.151 + } 1.152 + 1.153 + @TestMeForNull 1.154 + public static int oneCaseDefaultBreakOnly(String s, Set<String> stringSet, boolean expected) { 1.155 + int failures = 1; 1.156 + switch(s) { 1.157 + default: 1.158 + break; 1.159 + } 1.160 + failures = 0; 1.161 + return failures; 1.162 + } 1.163 + 1.164 + 1.165 + static int testNullBehavior() { 1.166 + int failures = 0; 1.167 + int count = 0; 1.168 + 1.169 + Method[] methods = OneCaseSwitches.class.getDeclaredMethods(); 1.170 + 1.171 + try { 1.172 + for(Method method : methods) { 1.173 + count++; 1.174 + try { 1.175 + if (method.isAnnotationPresent(TestMeForNull.class)) { 1.176 + System.out.println("Testing method " + method); 1.177 + method.invoke(null, (String)null, emptyStringSet, false); 1.178 + failures++; 1.179 + System.err.println("Didn't get NPE as expected from " + method); 1.180 + } 1.181 + } catch (InvocationTargetException ite) { // Expected 1.182 + Throwable targetException = ite.getTargetException(); 1.183 + if (! (targetException instanceof NullPointerException)) { 1.184 + failures++; // Wrong exception thrown 1.185 + System.err.println("Didn't get expected target exception NPE, got " + 1.186 + ite.getClass().getName()); 1.187 + } 1.188 + } 1.189 + } 1.190 + } catch (Exception e) { 1.191 + throw new RuntimeException(e); 1.192 + } 1.193 + 1.194 + if (count == 0) { 1.195 + failures++; 1.196 + System.err.println("Did not find any annotated methods."); 1.197 + } 1.198 + return failures; 1.199 + } 1.200 + 1.201 + static int testZeroCases() { 1.202 + int failures = 0; 1.203 + Set<String> noDefaultSet = new HashSet<String>(); 1.204 + Set<String> defaultSet = new HashSet<String>(); 1.205 + 1.206 + zeroCasesNoDefault(FOO, noDefaultSet, false); 1.207 + for(String word : words) { 1.208 + zeroCasesNoDefault(word, noDefaultSet, false); 1.209 + } 1.210 + 1.211 + if (!noDefaultSet.isEmpty()) { 1.212 + failures++; 1.213 + System.err.println("Non-empty set after zeroCasesNoDefault"); 1.214 + } 1.215 + 1.216 + for(String word : words) { 1.217 + zeroCasesWithDefault(word, defaultSet, true); 1.218 + } 1.219 + if (defaultSet.size() != words.length) { 1.220 + failures++; 1.221 + System.err.println("Missing strings after zeroCasesWithDefault"); 1.222 + } 1.223 + 1.224 + return failures; 1.225 + } 1.226 + 1.227 + static int testOneCaseNoDefault() { 1.228 + int failures = 0; 1.229 + Set<String> s = new HashSet<String>(); 1.230 + s.add("foo"); 1.231 + Set<String> fooSet = Collections.unmodifiableSet(s); 1.232 + Set<String> testSet = new HashSet<String>(); 1.233 + 1.234 + oneCaseNoDefault(FOO, testSet, true); 1.235 + if (!testSet.equals(fooSet)) { 1.236 + failures++; 1.237 + System.err.println("Unexpected result from oneCaseNoDefault: didn't get {\"Foo\"}"); 1.238 + } 1.239 + 1.240 + for(String word : words) { 1.241 + oneCaseNoDefault(word, testSet, false); 1.242 + } 1.243 + if (!testSet.equals(fooSet)) { 1.244 + failures++; 1.245 + System.err.println("Unexpected result from oneCaseNoDefault: didn't get {\"Foo\"}"); 1.246 + } 1.247 + 1.248 + return failures; 1.249 + } 1.250 + 1.251 + static int testBreakOnly() { 1.252 + int failures = 0; 1.253 + 1.254 + for(String word : words) { 1.255 + failures += oneCaseBreakOnly(word, emptyStringSet, true); 1.256 + failures += oneCaseDefaultBreakOnly(word, emptyStringSet, true); 1.257 + } 1.258 + 1.259 + return failures; 1.260 + } 1.261 + 1.262 + static int testExpressionEval() { 1.263 + String s = "a"; 1.264 + int errors = 2; 1.265 + 1.266 + System.out.println("Testing expression evaluation."); 1.267 + 1.268 + switch (s + s) { 1.269 + case "aa": 1.270 + errors = 0; 1.271 + break; 1.272 + 1.273 + case "aaaa": 1.274 + errors = 1; 1.275 + System.err.println("Suspected bad expression evaluation."); 1.276 + break; 1.277 + 1.278 + default: 1.279 + throw new RuntimeException("Should not reach here."); 1.280 + } 1.281 + return errors; 1.282 + } 1.283 + 1.284 + static final String FOO = "foo"; 1.285 + 1.286 + static final String[] words = {"baz", 1.287 + "quux", 1.288 + "wombat", 1.289 + "\u0ccc\u0012"}; // hash collision with "foo" 1.290 + 1.291 + final static Set<String> emptyStringSet = Collections.emptySet(); 1.292 + 1.293 + public static void main(String... args) { 1.294 + int failures = 0; 1.295 + 1.296 + failures += testNullBehavior(); 1.297 + failures += testZeroCases(); 1.298 + failures += testOneCaseNoDefault(); 1.299 + failures += testBreakOnly(); 1.300 + failures += testExpressionEval(); 1.301 + 1.302 + if (failures > 0) { 1.303 + throw new RuntimeException(); 1.304 + } 1.305 + } 1.306 +}