1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
/*
* @test /nodynamiccopyright/
* @bug 6827009 8078561
* @summary Positive tests for strings in switch with few alternatives.
* @compile/fail/ref=OneCaseSwitches.out -XDrawDiagnostics -source 6 OneCaseSwitches.java
* @compile OneCaseSwitches.java
* @run main OneCaseSwitches
* @author Joseph D. Darcy
*/
import java.lang.reflect.*;
import java.lang.annotation.*;
import java.util.*;
import static java.lang.annotation.RetentionPolicy.*;
public class OneCaseSwitches {
@Retention(RUNTIME)
@interface TestMeForNull {}
@TestMeForNull
public static int zeroCasesNoDefault(String s, Set<String> stringSet, boolean expected) {
int failures = 0;
switch(s) {
}
return failures;
}
@TestMeForNull
public static int zeroCasesWithDefault(String s, Set<String> stringSet, boolean expected) {
int failures = 2;
boolean addResult;
switch(s) {
default:
failures = 0;
addResult = stringSet.add(s);
if (addResult != expected) {
failures++;
System.err.println("zeroCaseWithDefault: Expectedly got add result of " + addResult +
" on string " + s);
}
}
return failures;
}
@TestMeForNull
public static int zeroCasesWithDefaultBreak(String s, Set<String> stringSet, boolean expected) {
int failures = 2;
boolean addResult;
switch(s) {
default:
failures = zeroCasesWithDefault(s, stringSet, expected);
break;
}
return failures;
}
@TestMeForNull
public static int oneCaseNoDefault(String s, Set<String> stringSet, boolean expected) {
int failures = 2;
boolean addResult;
switch(s) {
case "foo":
failures = 0;
addResult = stringSet.add(s);
if (addResult != expected) {
failures++;
System.err.println("oneCaseNoDefault: Unexpectedly got add result of " + addResult +
" on string " + s);
}
}
return failures;
}
@TestMeForNull
public static int oneCaseNoDefaultBreak(String s, Set<String> stringSet, boolean expected) {
int failures = 2;
boolean addResult;
switch(s) {
case "foo":
failures = oneCaseNoDefaultBreak(s, stringSet, expected);
break;
}
return failures;
}
@TestMeForNull
public static int oneCaseWithDefault(String s, Set<String> stringSet, boolean expected) {
int failures = 2;
boolean addResult;;
switch(s) {
case "foo":
failures = 0;
addResult = stringSet.add(s);
if (addResult != expected) {
failures++;
System.err.println("oneCaseNoDefault: Expectedly got add result of " + addResult +
" on string " + s);
}
break;
default:
break;
}
return failures;
}
@TestMeForNull
public static int oneCaseBreakOnly(String s, Set<String> stringSet, boolean expected) {
int failures = 1;
switch(s) {
case "foo":
break;
}
failures = 0;
return failures;
}
@TestMeForNull
public static int oneCaseDefaultBreakOnly(String s, Set<String> stringSet, boolean expected) {
int failures = 1;
switch(s) {
default:
break;
}
failures = 0;
return failures;
}
static int testNullBehavior() {
int failures = 0;
int count = 0;
Method[] methods = OneCaseSwitches.class.getDeclaredMethods();
try {
for(Method method : methods) {
count++;
try {
if (method.isAnnotationPresent(TestMeForNull.class)) {
System.out.println("Testing method " + method);
method.invoke(null, (String)null, emptyStringSet, false);
failures++;
System.err.println("Didn't get NPE as expected from " + method);
}
} catch (InvocationTargetException ite) { // Expected
Throwable targetException = ite.getTargetException();
if (! (targetException instanceof NullPointerException)) {
failures++; // Wrong exception thrown
System.err.println("Didn't get expected target exception NPE, got " +
ite.getClass().getName());
}
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
if (count == 0) {
failures++;
System.err.println("Did not find any annotated methods.");
}
return failures;
}
static int testZeroCases() {
int failures = 0;
Set<String> noDefaultSet = new HashSet<String>();
Set<String> defaultSet = new HashSet<String>();
zeroCasesNoDefault(FOO, noDefaultSet, false);
for(String word : words) {
zeroCasesNoDefault(word, noDefaultSet, false);
}
if (!noDefaultSet.isEmpty()) {
failures++;
System.err.println("Non-empty set after zeroCasesNoDefault");
}
for(String word : words) {
zeroCasesWithDefault(word, defaultSet, true);
}
if (defaultSet.size() != words.length) {
failures++;
System.err.println("Missing strings after zeroCasesWithDefault");
}
return failures;
}
static int testOneCaseNoDefault() {
int failures = 0;
Set<String> s = new HashSet<String>();
s.add("foo");
Set<String> fooSet = Collections.unmodifiableSet(s);
Set<String> testSet = new HashSet<String>();
oneCaseNoDefault(FOO, testSet, true);
if (!testSet.equals(fooSet)) {
failures++;
System.err.println("Unexpected result from oneCaseNoDefault: didn't get {\"Foo\"}");
}
for(String word : words) {
oneCaseNoDefault(word, testSet, false);
}
if (!testSet.equals(fooSet)) {
failures++;
System.err.println("Unexpected result from oneCaseNoDefault: didn't get {\"Foo\"}");
}
return failures;
}
static int testBreakOnly() {
int failures = 0;
for(String word : words) {
failures += oneCaseBreakOnly(word, emptyStringSet, true);
failures += oneCaseDefaultBreakOnly(word, emptyStringSet, true);
}
return failures;
}
static int testExpressionEval() {
String s = "a";
int errors = 2;
System.out.println("Testing expression evaluation.");
switch (s + s) {
case "aa":
errors = 0;
break;
case "aaaa":
errors = 1;
System.err.println("Suspected bad expression evaluation.");
break;
default:
throw new RuntimeException("Should not reach here.");
}
return errors;
}
static final String FOO = "foo";
static final String[] words = {"baz",
"quux",
"wombat",
"\u0ccc\u0012"}; // hash collision with "foo"
final static Set<String> emptyStringSet = Collections.emptySet();
public static void main(String... args) {
int failures = 0;
failures += testNullBehavior();
failures += testZeroCases();
failures += testOneCaseNoDefault();
failures += testBreakOnly();
failures += testExpressionEval();
if (failures > 0) {
throw new RuntimeException();
}
}
}
|