aoqi@0: /* aoqi@0: * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 6827009 aoqi@0: * @summary Positive tests for strings in switch with few alternatives. aoqi@0: * @compile/fail -source 6 OneCaseSwitches.java aoqi@0: * @compile OneCaseSwitches.java aoqi@0: * @run main OneCaseSwitches aoqi@0: * @author Joseph D. Darcy aoqi@0: */ aoqi@0: aoqi@0: import java.lang.reflect.*; aoqi@0: import java.lang.annotation.*; aoqi@0: import java.util.*; aoqi@0: import static java.lang.annotation.RetentionPolicy.*; aoqi@0: aoqi@0: public class OneCaseSwitches { aoqi@0: @Retention(RUNTIME) aoqi@0: @interface TestMeForNull {} aoqi@0: aoqi@0: @TestMeForNull aoqi@0: public static int zeroCasesNoDefault(String s, Set stringSet, boolean expected) { aoqi@0: int failures = 0; aoqi@0: switch(s) { aoqi@0: } aoqi@0: return failures; aoqi@0: } aoqi@0: aoqi@0: @TestMeForNull aoqi@0: public static int zeroCasesWithDefault(String s, Set stringSet, boolean expected) { aoqi@0: int failures = 2; aoqi@0: boolean addResult; aoqi@0: aoqi@0: switch(s) { aoqi@0: default: aoqi@0: failures = 0; aoqi@0: addResult = stringSet.add(s); aoqi@0: if (addResult != expected) { aoqi@0: failures++; aoqi@0: System.err.println("zeroCaseWithDefault: Expectedly got add result of " + addResult + aoqi@0: " on string " + s); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return failures; aoqi@0: } aoqi@0: aoqi@0: @TestMeForNull aoqi@0: public static int zeroCasesWithDefaultBreak(String s, Set stringSet, boolean expected) { aoqi@0: int failures = 2; aoqi@0: boolean addResult; aoqi@0: aoqi@0: switch(s) { aoqi@0: default: aoqi@0: failures = zeroCasesWithDefault(s, stringSet, expected); aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: return failures; aoqi@0: } aoqi@0: aoqi@0: @TestMeForNull aoqi@0: public static int oneCaseNoDefault(String s, Set stringSet, boolean expected) { aoqi@0: int failures = 2; aoqi@0: boolean addResult; aoqi@0: aoqi@0: switch(s) { aoqi@0: case "foo": aoqi@0: failures = 0; aoqi@0: addResult = stringSet.add(s); aoqi@0: if (addResult != expected) { aoqi@0: failures++; aoqi@0: System.err.println("oneCaseNoDefault: Unexpectedly got add result of " + addResult + aoqi@0: " on string " + s); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return failures; aoqi@0: } aoqi@0: aoqi@0: @TestMeForNull aoqi@0: public static int oneCaseNoDefaultBreak(String s, Set stringSet, boolean expected) { aoqi@0: int failures = 2; aoqi@0: boolean addResult; aoqi@0: aoqi@0: switch(s) { aoqi@0: case "foo": aoqi@0: failures = oneCaseNoDefaultBreak(s, stringSet, expected); aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: return failures; aoqi@0: } aoqi@0: aoqi@0: @TestMeForNull aoqi@0: public static int oneCaseWithDefault(String s, Set stringSet, boolean expected) { aoqi@0: int failures = 2; aoqi@0: boolean addResult;; aoqi@0: aoqi@0: switch(s) { aoqi@0: case "foo": aoqi@0: failures = 0; aoqi@0: addResult = stringSet.add(s); aoqi@0: if (addResult != expected) { aoqi@0: failures++; aoqi@0: System.err.println("oneCaseNoDefault: Expectedly got add result of " + addResult + aoqi@0: " on string " + s); aoqi@0: } aoqi@0: break; aoqi@0: default: aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: return failures; aoqi@0: } aoqi@0: aoqi@0: @TestMeForNull aoqi@0: public static int oneCaseBreakOnly(String s, Set stringSet, boolean expected) { aoqi@0: int failures = 1; aoqi@0: switch(s) { aoqi@0: case "foo": aoqi@0: break; aoqi@0: } aoqi@0: failures = 0; aoqi@0: return failures; aoqi@0: } aoqi@0: aoqi@0: @TestMeForNull aoqi@0: public static int oneCaseDefaultBreakOnly(String s, Set stringSet, boolean expected) { aoqi@0: int failures = 1; aoqi@0: switch(s) { aoqi@0: default: aoqi@0: break; aoqi@0: } aoqi@0: failures = 0; aoqi@0: return failures; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: static int testNullBehavior() { aoqi@0: int failures = 0; aoqi@0: int count = 0; aoqi@0: aoqi@0: Method[] methods = OneCaseSwitches.class.getDeclaredMethods(); aoqi@0: aoqi@0: try { aoqi@0: for(Method method : methods) { aoqi@0: count++; aoqi@0: try { aoqi@0: if (method.isAnnotationPresent(TestMeForNull.class)) { aoqi@0: System.out.println("Testing method " + method); aoqi@0: method.invoke(null, (String)null, emptyStringSet, false); aoqi@0: failures++; aoqi@0: System.err.println("Didn't get NPE as expected from " + method); aoqi@0: } aoqi@0: } catch (InvocationTargetException ite) { // Expected aoqi@0: Throwable targetException = ite.getTargetException(); aoqi@0: if (! (targetException instanceof NullPointerException)) { aoqi@0: failures++; // Wrong exception thrown aoqi@0: System.err.println("Didn't get expected target exception NPE, got " + aoqi@0: ite.getClass().getName()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } catch (Exception e) { aoqi@0: throw new RuntimeException(e); aoqi@0: } aoqi@0: aoqi@0: if (count == 0) { aoqi@0: failures++; aoqi@0: System.err.println("Did not find any annotated methods."); aoqi@0: } aoqi@0: return failures; aoqi@0: } aoqi@0: aoqi@0: static int testZeroCases() { aoqi@0: int failures = 0; aoqi@0: Set noDefaultSet = new HashSet(); aoqi@0: Set defaultSet = new HashSet(); aoqi@0: aoqi@0: zeroCasesNoDefault(FOO, noDefaultSet, false); aoqi@0: for(String word : words) { aoqi@0: zeroCasesNoDefault(word, noDefaultSet, false); aoqi@0: } aoqi@0: aoqi@0: if (!noDefaultSet.isEmpty()) { aoqi@0: failures++; aoqi@0: System.err.println("Non-empty set after zeroCasesNoDefault"); aoqi@0: } aoqi@0: aoqi@0: for(String word : words) { aoqi@0: zeroCasesWithDefault(word, defaultSet, true); aoqi@0: } aoqi@0: if (defaultSet.size() != words.length) { aoqi@0: failures++; aoqi@0: System.err.println("Missing strings after zeroCasesWithDefault"); aoqi@0: } aoqi@0: aoqi@0: return failures; aoqi@0: } aoqi@0: aoqi@0: static int testOneCaseNoDefault() { aoqi@0: int failures = 0; aoqi@0: Set s = new HashSet(); aoqi@0: s.add("foo"); aoqi@0: Set fooSet = Collections.unmodifiableSet(s); aoqi@0: Set testSet = new HashSet(); aoqi@0: aoqi@0: oneCaseNoDefault(FOO, testSet, true); aoqi@0: if (!testSet.equals(fooSet)) { aoqi@0: failures++; aoqi@0: System.err.println("Unexpected result from oneCaseNoDefault: didn't get {\"Foo\"}"); aoqi@0: } aoqi@0: aoqi@0: for(String word : words) { aoqi@0: oneCaseNoDefault(word, testSet, false); aoqi@0: } aoqi@0: if (!testSet.equals(fooSet)) { aoqi@0: failures++; aoqi@0: System.err.println("Unexpected result from oneCaseNoDefault: didn't get {\"Foo\"}"); aoqi@0: } aoqi@0: aoqi@0: return failures; aoqi@0: } aoqi@0: aoqi@0: static int testBreakOnly() { aoqi@0: int failures = 0; aoqi@0: aoqi@0: for(String word : words) { aoqi@0: failures += oneCaseBreakOnly(word, emptyStringSet, true); aoqi@0: failures += oneCaseDefaultBreakOnly(word, emptyStringSet, true); aoqi@0: } aoqi@0: aoqi@0: return failures; aoqi@0: } aoqi@0: aoqi@0: static int testExpressionEval() { aoqi@0: String s = "a"; aoqi@0: int errors = 2; aoqi@0: aoqi@0: System.out.println("Testing expression evaluation."); aoqi@0: aoqi@0: switch (s + s) { aoqi@0: case "aa": aoqi@0: errors = 0; aoqi@0: break; aoqi@0: aoqi@0: case "aaaa": aoqi@0: errors = 1; aoqi@0: System.err.println("Suspected bad expression evaluation."); aoqi@0: break; aoqi@0: aoqi@0: default: aoqi@0: throw new RuntimeException("Should not reach here."); aoqi@0: } aoqi@0: return errors; aoqi@0: } aoqi@0: aoqi@0: static final String FOO = "foo"; aoqi@0: aoqi@0: static final String[] words = {"baz", aoqi@0: "quux", aoqi@0: "wombat", aoqi@0: "\u0ccc\u0012"}; // hash collision with "foo" aoqi@0: aoqi@0: final static Set emptyStringSet = Collections.emptySet(); aoqi@0: aoqi@0: public static void main(String... args) { aoqi@0: int failures = 0; aoqi@0: aoqi@0: failures += testNullBehavior(); aoqi@0: failures += testZeroCases(); aoqi@0: failures += testOneCaseNoDefault(); aoqi@0: failures += testBreakOnly(); aoqi@0: failures += testExpressionEval(); aoqi@0: aoqi@0: if (failures > 0) { aoqi@0: throw new RuntimeException(); aoqi@0: } aoqi@0: } aoqi@0: }