Thu, 10 Oct 2013 14:58:04 +0200
8001931: The new build system whitespace cleanup
Reviewed-by: tbell, simonis, erikj
darcy@430 | 1 | /* |
ohair@554 | 2 | * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. |
darcy@430 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
darcy@430 | 4 | * |
darcy@430 | 5 | * This code is free software; you can redistribute it and/or modify it |
darcy@430 | 6 | * under the terms of the GNU General Public License version 2 only, as |
darcy@430 | 7 | * published by the Free Software Foundation. |
darcy@430 | 8 | * |
darcy@430 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
darcy@430 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
darcy@430 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
darcy@430 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
darcy@430 | 13 | * accompanied this code). |
darcy@430 | 14 | * |
darcy@430 | 15 | * You should have received a copy of the GNU General Public License version |
darcy@430 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
darcy@430 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
darcy@430 | 18 | * |
ohair@554 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 20 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 21 | * questions. |
darcy@430 | 22 | */ |
darcy@430 | 23 | |
darcy@430 | 24 | /* |
darcy@430 | 25 | * @test |
darcy@430 | 26 | * @bug 6827009 |
darcy@430 | 27 | * @summary Positive tests for strings in switch with few alternatives. |
darcy@430 | 28 | * @compile/fail -source 6 OneCaseSwitches.java |
darcy@430 | 29 | * @compile OneCaseSwitches.java |
darcy@430 | 30 | * @run main OneCaseSwitches |
darcy@430 | 31 | * @author Joseph D. Darcy |
darcy@430 | 32 | */ |
darcy@430 | 33 | |
darcy@430 | 34 | import java.lang.reflect.*; |
darcy@430 | 35 | import java.lang.annotation.*; |
darcy@430 | 36 | import java.util.*; |
darcy@430 | 37 | import static java.lang.annotation.RetentionPolicy.*; |
darcy@430 | 38 | |
darcy@430 | 39 | public class OneCaseSwitches { |
darcy@430 | 40 | @Retention(RUNTIME) |
darcy@430 | 41 | @interface TestMeForNull {} |
darcy@430 | 42 | |
darcy@430 | 43 | @TestMeForNull |
darcy@430 | 44 | public static int zeroCasesNoDefault(String s, Set<String> stringSet, boolean expected) { |
darcy@430 | 45 | int failures = 0; |
darcy@430 | 46 | switch(s) { |
darcy@430 | 47 | } |
darcy@430 | 48 | return failures; |
darcy@430 | 49 | } |
darcy@430 | 50 | |
darcy@430 | 51 | @TestMeForNull |
darcy@430 | 52 | public static int zeroCasesWithDefault(String s, Set<String> stringSet, boolean expected) { |
darcy@430 | 53 | int failures = 2; |
darcy@430 | 54 | boolean addResult; |
darcy@430 | 55 | |
darcy@430 | 56 | switch(s) { |
darcy@430 | 57 | default: |
darcy@430 | 58 | failures = 0; |
darcy@430 | 59 | addResult = stringSet.add(s); |
darcy@430 | 60 | if (addResult != expected) { |
darcy@430 | 61 | failures++; |
darcy@430 | 62 | System.err.println("zeroCaseWithDefault: Expectedly got add result of " + addResult + |
darcy@430 | 63 | " on string " + s); |
darcy@430 | 64 | } |
darcy@430 | 65 | } |
darcy@430 | 66 | |
darcy@430 | 67 | return failures; |
darcy@430 | 68 | } |
darcy@430 | 69 | |
darcy@430 | 70 | @TestMeForNull |
darcy@430 | 71 | public static int zeroCasesWithDefaultBreak(String s, Set<String> stringSet, boolean expected) { |
darcy@430 | 72 | int failures = 2; |
darcy@430 | 73 | boolean addResult; |
darcy@430 | 74 | |
darcy@430 | 75 | switch(s) { |
darcy@430 | 76 | default: |
darcy@430 | 77 | failures = zeroCasesWithDefault(s, stringSet, expected); |
darcy@430 | 78 | break; |
darcy@430 | 79 | } |
darcy@430 | 80 | |
darcy@430 | 81 | return failures; |
darcy@430 | 82 | } |
darcy@430 | 83 | |
darcy@430 | 84 | @TestMeForNull |
darcy@430 | 85 | public static int oneCaseNoDefault(String s, Set<String> stringSet, boolean expected) { |
darcy@430 | 86 | int failures = 2; |
darcy@430 | 87 | boolean addResult; |
darcy@430 | 88 | |
darcy@430 | 89 | switch(s) { |
darcy@430 | 90 | case "foo": |
darcy@430 | 91 | failures = 0; |
darcy@430 | 92 | addResult = stringSet.add(s); |
darcy@430 | 93 | if (addResult != expected) { |
darcy@430 | 94 | failures++; |
darcy@430 | 95 | System.err.println("oneCaseNoDefault: Unexpectedly got add result of " + addResult + |
darcy@430 | 96 | " on string " + s); |
darcy@430 | 97 | } |
darcy@430 | 98 | } |
darcy@430 | 99 | |
darcy@430 | 100 | return failures; |
darcy@430 | 101 | } |
darcy@430 | 102 | |
darcy@430 | 103 | @TestMeForNull |
darcy@430 | 104 | public static int oneCaseNoDefaultBreak(String s, Set<String> stringSet, boolean expected) { |
darcy@430 | 105 | int failures = 2; |
darcy@430 | 106 | boolean addResult; |
darcy@430 | 107 | |
darcy@430 | 108 | switch(s) { |
darcy@430 | 109 | case "foo": |
darcy@430 | 110 | failures = oneCaseNoDefaultBreak(s, stringSet, expected); |
darcy@430 | 111 | break; |
darcy@430 | 112 | } |
darcy@430 | 113 | |
darcy@430 | 114 | return failures; |
darcy@430 | 115 | } |
darcy@430 | 116 | |
darcy@430 | 117 | @TestMeForNull |
darcy@430 | 118 | public static int oneCaseWithDefault(String s, Set<String> stringSet, boolean expected) { |
darcy@430 | 119 | int failures = 2; |
darcy@430 | 120 | boolean addResult;; |
darcy@430 | 121 | |
darcy@430 | 122 | switch(s) { |
darcy@430 | 123 | case "foo": |
darcy@430 | 124 | failures = 0; |
darcy@430 | 125 | addResult = stringSet.add(s); |
darcy@430 | 126 | if (addResult != expected) { |
darcy@430 | 127 | failures++; |
darcy@430 | 128 | System.err.println("oneCaseNoDefault: Expectedly got add result of " + addResult + |
darcy@430 | 129 | " on string " + s); |
darcy@430 | 130 | } |
darcy@430 | 131 | break; |
darcy@430 | 132 | default: |
darcy@430 | 133 | break; |
darcy@430 | 134 | } |
darcy@430 | 135 | |
darcy@430 | 136 | return failures; |
darcy@430 | 137 | } |
darcy@430 | 138 | |
darcy@430 | 139 | @TestMeForNull |
darcy@430 | 140 | public static int oneCaseBreakOnly(String s, Set<String> stringSet, boolean expected) { |
darcy@430 | 141 | int failures = 1; |
darcy@430 | 142 | switch(s) { |
darcy@430 | 143 | case "foo": |
darcy@430 | 144 | break; |
darcy@430 | 145 | } |
darcy@430 | 146 | failures = 0; |
darcy@430 | 147 | return failures; |
darcy@430 | 148 | } |
darcy@430 | 149 | |
darcy@430 | 150 | @TestMeForNull |
darcy@430 | 151 | public static int oneCaseDefaultBreakOnly(String s, Set<String> stringSet, boolean expected) { |
darcy@430 | 152 | int failures = 1; |
darcy@430 | 153 | switch(s) { |
darcy@430 | 154 | default: |
darcy@430 | 155 | break; |
darcy@430 | 156 | } |
darcy@430 | 157 | failures = 0; |
darcy@430 | 158 | return failures; |
darcy@430 | 159 | } |
darcy@430 | 160 | |
darcy@430 | 161 | |
darcy@430 | 162 | static int testNullBehavior() { |
darcy@430 | 163 | int failures = 0; |
darcy@430 | 164 | int count = 0; |
darcy@430 | 165 | |
darcy@430 | 166 | Method[] methods = OneCaseSwitches.class.getDeclaredMethods(); |
darcy@430 | 167 | |
darcy@430 | 168 | try { |
darcy@430 | 169 | for(Method method : methods) { |
darcy@430 | 170 | count++; |
darcy@430 | 171 | try { |
darcy@430 | 172 | if (method.isAnnotationPresent(TestMeForNull.class)) { |
darcy@430 | 173 | System.out.println("Testing method " + method); |
darcy@430 | 174 | method.invoke(null, (String)null, emptyStringSet, false); |
darcy@430 | 175 | failures++; |
darcy@430 | 176 | System.err.println("Didn't get NPE as expected from " + method); |
darcy@430 | 177 | } |
darcy@430 | 178 | } catch (InvocationTargetException ite) { // Expected |
darcy@430 | 179 | Throwable targetException = ite.getTargetException(); |
darcy@430 | 180 | if (! (targetException instanceof NullPointerException)) { |
darcy@430 | 181 | failures++; // Wrong exception thrown |
darcy@430 | 182 | System.err.println("Didn't get expected target exception NPE, got " + |
darcy@430 | 183 | ite.getClass().getName()); |
darcy@430 | 184 | } |
darcy@430 | 185 | } |
darcy@430 | 186 | } |
darcy@430 | 187 | } catch (Exception e) { |
darcy@430 | 188 | throw new RuntimeException(e); |
darcy@430 | 189 | } |
darcy@430 | 190 | |
darcy@430 | 191 | if (count == 0) { |
darcy@430 | 192 | failures++; |
darcy@430 | 193 | System.err.println("Did not find any annotated methods."); |
darcy@430 | 194 | } |
darcy@430 | 195 | return failures; |
darcy@430 | 196 | } |
darcy@430 | 197 | |
darcy@430 | 198 | static int testZeroCases() { |
darcy@430 | 199 | int failures = 0; |
darcy@430 | 200 | Set<String> noDefaultSet = new HashSet<String>(); |
darcy@430 | 201 | Set<String> defaultSet = new HashSet<String>(); |
darcy@430 | 202 | |
darcy@430 | 203 | zeroCasesNoDefault(FOO, noDefaultSet, false); |
darcy@430 | 204 | for(String word : words) { |
darcy@430 | 205 | zeroCasesNoDefault(word, noDefaultSet, false); |
darcy@430 | 206 | } |
darcy@430 | 207 | |
darcy@430 | 208 | if (!noDefaultSet.isEmpty()) { |
darcy@430 | 209 | failures++; |
darcy@430 | 210 | System.err.println("Non-empty set after zeroCasesNoDefault"); |
darcy@430 | 211 | } |
darcy@430 | 212 | |
darcy@430 | 213 | for(String word : words) { |
darcy@430 | 214 | zeroCasesWithDefault(word, defaultSet, true); |
darcy@430 | 215 | } |
darcy@430 | 216 | if (defaultSet.size() != words.length) { |
darcy@430 | 217 | failures++; |
darcy@430 | 218 | System.err.println("Missing strings after zeroCasesWithDefault"); |
darcy@430 | 219 | } |
darcy@430 | 220 | |
darcy@430 | 221 | return failures; |
darcy@430 | 222 | } |
darcy@430 | 223 | |
darcy@430 | 224 | static int testOneCaseNoDefault() { |
darcy@430 | 225 | int failures = 0; |
darcy@430 | 226 | Set<String> s = new HashSet<String>(); |
darcy@430 | 227 | s.add("foo"); |
darcy@430 | 228 | Set<String> fooSet = Collections.unmodifiableSet(s); |
darcy@430 | 229 | Set<String> testSet = new HashSet<String>(); |
darcy@430 | 230 | |
darcy@430 | 231 | oneCaseNoDefault(FOO, testSet, true); |
darcy@430 | 232 | if (!testSet.equals(fooSet)) { |
darcy@430 | 233 | failures++; |
darcy@430 | 234 | System.err.println("Unexpected result from oneCaseNoDefault: didn't get {\"Foo\"}"); |
darcy@430 | 235 | } |
darcy@430 | 236 | |
darcy@430 | 237 | for(String word : words) { |
darcy@430 | 238 | oneCaseNoDefault(word, testSet, false); |
darcy@430 | 239 | } |
darcy@430 | 240 | if (!testSet.equals(fooSet)) { |
darcy@430 | 241 | failures++; |
darcy@430 | 242 | System.err.println("Unexpected result from oneCaseNoDefault: didn't get {\"Foo\"}"); |
darcy@430 | 243 | } |
darcy@430 | 244 | |
darcy@430 | 245 | return failures; |
darcy@430 | 246 | } |
darcy@430 | 247 | |
darcy@430 | 248 | static int testBreakOnly() { |
darcy@430 | 249 | int failures = 0; |
darcy@430 | 250 | |
darcy@430 | 251 | for(String word : words) { |
darcy@430 | 252 | failures += oneCaseBreakOnly(word, emptyStringSet, true); |
darcy@430 | 253 | failures += oneCaseDefaultBreakOnly(word, emptyStringSet, true); |
darcy@430 | 254 | } |
darcy@430 | 255 | |
darcy@430 | 256 | return failures; |
darcy@430 | 257 | } |
darcy@430 | 258 | |
darcy@430 | 259 | static int testExpressionEval() { |
darcy@430 | 260 | String s = "a"; |
darcy@430 | 261 | int errors = 2; |
darcy@430 | 262 | |
darcy@430 | 263 | System.out.println("Testing expression evaluation."); |
darcy@430 | 264 | |
darcy@430 | 265 | switch (s + s) { |
darcy@430 | 266 | case "aa": |
darcy@430 | 267 | errors = 0; |
darcy@430 | 268 | break; |
darcy@430 | 269 | |
darcy@430 | 270 | case "aaaa": |
darcy@430 | 271 | errors = 1; |
darcy@430 | 272 | System.err.println("Suspected bad expression evaluation."); |
darcy@430 | 273 | break; |
darcy@430 | 274 | |
darcy@430 | 275 | default: |
darcy@430 | 276 | throw new RuntimeException("Should not reach here."); |
darcy@430 | 277 | } |
darcy@430 | 278 | return errors; |
darcy@430 | 279 | } |
darcy@430 | 280 | |
darcy@430 | 281 | static final String FOO = "foo"; |
darcy@430 | 282 | |
darcy@430 | 283 | static final String[] words = {"baz", |
darcy@430 | 284 | "quux", |
darcy@430 | 285 | "wombat", |
darcy@430 | 286 | "\u0ccc\u0012"}; // hash collision with "foo" |
darcy@430 | 287 | |
darcy@430 | 288 | final static Set<String> emptyStringSet = Collections.emptySet(); |
darcy@430 | 289 | |
darcy@430 | 290 | public static void main(String... args) { |
darcy@430 | 291 | int failures = 0; |
darcy@430 | 292 | |
darcy@430 | 293 | failures += testNullBehavior(); |
darcy@430 | 294 | failures += testZeroCases(); |
darcy@430 | 295 | failures += testOneCaseNoDefault(); |
darcy@430 | 296 | failures += testBreakOnly(); |
darcy@430 | 297 | failures += testExpressionEval(); |
darcy@430 | 298 | |
darcy@430 | 299 | if (failures > 0) { |
darcy@430 | 300 | throw new RuntimeException(); |
darcy@430 | 301 | } |
darcy@430 | 302 | } |
darcy@430 | 303 | } |