1 16 17 package org.springframework.core.enums; 18 19 import junit.framework.TestCase; 20 21 24 public class LabeledEnumTests extends TestCase { 25 26 public void testForCodeFound() { 27 Dog golden = (Dog) new StaticLabeledEnumResolver().getLabeledEnumByCode(Dog.class, new Short ((short) 11)); 28 Dog borderCollie = (Dog) new StaticLabeledEnumResolver().getLabeledEnumByCode(Dog.class, new Short ((short) 13)); 29 assertSame(golden, Dog.GOLDEN_RETRIEVER); 30 assertSame(borderCollie, Dog.BORDER_COLLIE); 31 } 32 33 public void testDoesNotMatchWrongClass() { 34 try { 35 LabeledEnum none = new StaticLabeledEnumResolver().getLabeledEnumByCode(Dog.class, new Short ((short) 1)); 36 fail("Should have failed"); 37 } 38 catch (IllegalArgumentException e) { 39 } 41 } 42 43 public void testEquals() { 44 assertEquals("Code equality means equals", Dog.GOLDEN_RETRIEVER, new Dog(11, "Golden Retriever")); 45 assertFalse("Code inequality means notEquals", Dog.GOLDEN_RETRIEVER.equals(new Dog(12, "Golden Retriever"))); 46 } 47 48 49 public static class Other extends ShortCodedLabeledEnum { 50 51 public static Other THING1 = new Other(1, "Thing1"); 52 53 public static Other THING2 = new Other(2, "Thing2"); 54 55 public Other(int code, String name) { 56 super(code, name); 57 } 58 } 59 60 61 public static class Dog extends ShortCodedLabeledEnum { 62 63 public static final Dog GOLDEN_RETRIEVER = new Dog(11, null) { 64 public Class getType() { 66 return Dog.class; 67 } 68 69 public String getLabel() { 70 return "Golden Retriever"; 71 } 72 }; 73 74 public static final Dog BORDER_COLLIE = new Dog(13, "Border Collie"); 75 76 public static final Dog WHIPPET = new Dog(14, "Whippet"); 77 78 public static final Other THING1 = Other.THING1; 80 81 private Dog(int code, String name) { 82 super(code, name); 83 } 84 } 85 86 } 87 | Popular Tags |