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