|
Java example source code file (OneCaseSwitches.java)
The OneCaseSwitches.java Java example source code
/*
* Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 6827009
* @summary Positive tests for strings in switch with few alternatives.
* @compile/fail -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
Other Java examples (source code examples)Here is a short list of links related to this Java OneCaseSwitches.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.