J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @bug 4728816 |
| 27 | * @summary JPDA: Add support for enums |
| 28 | * |
| 29 | * @author jjh |
| 30 | * |
| 31 | * @run build TestScaffold VMConnection TargetListener TargetAdapter |
| 32 | * @run compile -source 1.5 -target 1.5 -g EnumTest.java |
| 33 | * @run main EnumTest |
| 34 | */ |
| 35 | import com.sun.jdi.*; |
| 36 | import com.sun.jdi.event.*; |
| 37 | import com.sun.jdi.request.*; |
| 38 | |
| 39 | import java.util.*; |
| 40 | |
| 41 | /********** target program **********/ |
| 42 | |
| 43 | |
| 44 | enum Coin { |
| 45 | penny(1), nickel(5), dime(10), quarter(25); |
| 46 | |
| 47 | Coin(int value) { this.value = value; } |
| 48 | |
| 49 | private final int value; |
| 50 | |
| 51 | public int value() { return value; } |
| 52 | } |
| 53 | |
| 54 | class EnumTarg { |
| 55 | static Coin myCoin = Coin.penny; |
| 56 | public static void main(String[] args){ |
| 57 | System.out.println("Howdy!"); |
| 58 | System.out.println("Goodbye from EnumTarg!"); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /********** test program **********/ |
| 63 | |
| 64 | public class EnumTest extends TestScaffold { |
| 65 | ReferenceType targetClass; |
| 66 | |
| 67 | EnumTest (String args[]) { |
| 68 | super(args); |
| 69 | } |
| 70 | |
| 71 | public static void main(String[] args) throws Exception { |
| 72 | new EnumTest(args).startTests(); |
| 73 | } |
| 74 | |
| 75 | void fail(String reason) throws Exception { |
| 76 | failure(reason); |
| 77 | } |
| 78 | |
| 79 | /********** test core **********/ |
| 80 | |
| 81 | |
| 82 | protected void runTests() throws Exception { |
| 83 | /* |
| 84 | * Get to the top of main() |
| 85 | * to determine targetClass |
| 86 | */ |
| 87 | BreakpointEvent bpe = startToMain("EnumTarg"); |
| 88 | targetClass = bpe.location().declaringType(); |
| 89 | |
| 90 | ReferenceType rt = findReferenceType("EnumTarg"); |
| 91 | Field myField = rt.fieldByName("myCoin"); |
| 92 | ObjectReference enumObject = (ObjectReference)rt.getValue(myField); |
| 93 | ClassType enumClass =(ClassType) enumObject.referenceType(); |
| 94 | ClassType superClass = enumClass.superclass(); |
| 95 | if (!superClass.name().equals("java.lang.Enum")) { |
| 96 | fail("failure: Superclass of enum class is not java.lang.Enum: " + superClass.name()); |
| 97 | } |
| 98 | if (!enumClass.isEnum()) { |
| 99 | fail("failure: isEnum() is false but should be true"); |
| 100 | } |
| 101 | if (((ClassType)rt).isEnum()) { |
| 102 | fail("failure: isEnum() is true for EnumTarg but should be false"); |
| 103 | } |
| 104 | Field enumConstant = enumClass.fieldByName("penny"); |
| 105 | if (!enumConstant.isEnumConstant()) { |
| 106 | fail("failure: The 'penny' field is not marked " + |
| 107 | "as an enum constant."); |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * This isn't really part of the test, it just |
| 112 | * shows how to look at all the enum constants, |
| 113 | * but not necessarily in the correct order |
| 114 | */ |
| 115 | List allFields = enumClass.fields(); |
| 116 | List enumConstantFields = new ArrayList(); |
| 117 | StringBuffer enumDecl = new StringBuffer("enum " + enumClass.name() + " {"); |
| 118 | char delim = ' '; |
| 119 | |
| 120 | for (Iterator iter = allFields.iterator(); iter.hasNext(); ) { |
| 121 | Field aField = (Field)iter.next(); |
| 122 | if (aField.isEnumConstant()) { |
| 123 | enumDecl.append(' '); |
| 124 | enumDecl.append(aField.name()); |
| 125 | enumDecl.append(delim); |
| 126 | delim = ','; |
| 127 | } |
| 128 | } |
| 129 | enumDecl.append("; };"); |
| 130 | System.out.println("Enum decl is: " + enumDecl); |
| 131 | |
| 132 | listenUntilVMDisconnect(); |
| 133 | |
| 134 | /* |
| 135 | * deal with results of test |
| 136 | * if anything has called failure("foo") testFailed will be true |
| 137 | */ |
| 138 | if (!testFailed) { |
| 139 | println("EnumTest: passed"); |
| 140 | } else { |
| 141 | throw new Exception("EnumTest: failed"); |
| 142 | } |
| 143 | } |
| 144 | } |