KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > enums > LabeledEnumTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.core.enums;
18
19 import junit.framework.TestCase;
20
21 /**
22  * @author Keith Donald
23  */

24 public class LabeledEnumTests extends TestCase {
25
26     public void testForCodeFound() {
27         Dog golden = (Dog) new StaticLabeledEnumResolver().getLabeledEnumByCode(Dog.class, new Short JavaDoc((short) 11));
28         Dog borderCollie = (Dog) new StaticLabeledEnumResolver().getLabeledEnumByCode(Dog.class, new Short JavaDoc((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 JavaDoc((short) 1));
36             fail("Should have failed");
37         }
38         catch (IllegalArgumentException JavaDoc e) {
39             // expected
40
}
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 JavaDoc 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             // this shouldn't be neccessary
65
public Class JavaDoc getType() {
66                 return Dog.class;
67             }
68
69             public String JavaDoc 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         // Ignore this
79
public static final Other THING1 = Other.THING1;
80
81         private Dog(int code, String JavaDoc name) {
82             super(code, name);
83         }
84     }
85
86 }
87
Popular Tags