Path: blob/master/test/jdk/com/sun/jdi/EnumTest.java
41149 views
/*1* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 472881626* @summary JPDA: Add support for enums27* @author jjh28*29* @run build TestScaffold VMConnection TargetListener TargetAdapter30* @run compile -g EnumTest.java31* @run driver EnumTest32*/33import com.sun.jdi.*;34import com.sun.jdi.event.*;35import com.sun.jdi.request.*;3637import java.util.*;3839/********** target program **********/404142enum Coin {43penny(1), nickel(5), dime(10), quarter(25);4445Coin(int value) { this.value = value; }4647private final int value;4849public int value() { return value; }50}5152class EnumTarg {53static Coin myCoin = Coin.penny;54public static void main(String[] args){55System.out.println("Howdy!");56System.out.println("Goodbye from EnumTarg!");57}58}5960/********** test program **********/6162public class EnumTest extends TestScaffold {63ReferenceType targetClass;6465EnumTest (String args[]) {66super(args);67}6869public static void main(String[] args) throws Exception {70new EnumTest(args).startTests();71}7273void fail(String reason) throws Exception {74failure(reason);75}7677/********** test core **********/787980protected void runTests() throws Exception {81/*82* Get to the top of main()83* to determine targetClass84*/85BreakpointEvent bpe = startToMain("EnumTarg");86targetClass = bpe.location().declaringType();8788ReferenceType rt = findReferenceType("EnumTarg");89Field myField = rt.fieldByName("myCoin");90ObjectReference enumObject = (ObjectReference)rt.getValue(myField);91ClassType enumClass =(ClassType) enumObject.referenceType();92ClassType superClass = enumClass.superclass();93if (!superClass.name().equals("java.lang.Enum")) {94fail("failure: Superclass of enum class is not java.lang.Enum: " + superClass.name());95}96if (!enumClass.isEnum()) {97fail("failure: isEnum() is false but should be true");98}99if (((ClassType)rt).isEnum()) {100fail("failure: isEnum() is true for EnumTarg but should be false");101}102Field enumConstant = enumClass.fieldByName("penny");103if (!enumConstant.isEnumConstant()) {104fail("failure: The 'penny' field is not marked " +105"as an enum constant.");106}107108/*109* This isn't really part of the test, it just110* shows how to look at all the enum constants,111* but not necessarily in the correct order112*/113List allFields = enumClass.fields();114List enumConstantFields = new ArrayList();115StringBuffer enumDecl = new StringBuffer("enum " + enumClass.name() + " {");116char delim = ' ';117118for (Iterator iter = allFields.iterator(); iter.hasNext(); ) {119Field aField = (Field)iter.next();120if (aField.isEnumConstant()) {121enumDecl.append(' ');122enumDecl.append(aField.name());123enumDecl.append(delim);124delim = ',';125}126}127enumDecl.append("; };");128System.out.println("Enum decl is: " + enumDecl);129130listenUntilVMDisconnect();131132/*133* deal with results of test134* if anything has called failure("foo") testFailed will be true135*/136if (!testFailed) {137println("EnumTest: passed");138} else {139throw new Exception("EnumTest: failed");140}141}142}143144145