KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > ConstantsTests


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;
18
19 import java.util.Set JavaDoc;
20
21 import junit.framework.TestCase;
22
23 /**
24  * @author Rod Johnson
25  * @author Juergen Hoeller
26  * @since 28.04.2003
27  */

28 public class ConstantsTests extends TestCase {
29
30     public void testConstants() {
31         Constants c = new Constants(A.class);
32         assertEquals(A.class.getName(), c.getClassName());
33         assertEquals(5, c.getSize());
34         
35         assertEquals(c.asNumber("DOG").intValue(), A.DOG);
36         assertEquals(c.asNumber("dog").intValue(), A.DOG);
37         assertEquals(c.asNumber("cat").intValue(), A.CAT);
38
39         try {
40             c.asNumber("bogus");
41             fail("Can't get bogus field");
42         }
43         catch (ConstantException ex) {
44         }
45
46         assertTrue(c.asString("S1").equals(A.S1));
47         try {
48             c.asNumber("S1");
49             fail("Wrong type");
50         }
51         catch (ConstantException ex) {
52         }
53
54         Set JavaDoc values = c.getValues("");
55         assertEquals(c.getSize(), values.size());
56         assertTrue(values.contains(new Integer JavaDoc(0)));
57         assertTrue(values.contains(new Integer JavaDoc(66)));
58         assertTrue(values.contains(""));
59
60         values = c.getValues("D");
61         assertEquals(1, values.size());
62         assertTrue(values.contains(new Integer JavaDoc(0)));
63
64         values = c.getValuesForProperty("myProperty");
65         assertEquals(2, values.size());
66         assertTrue(values.contains(new Integer JavaDoc(1)));
67         assertTrue(values.contains(new Integer JavaDoc(2)));
68
69         assertEquals(c.toCode(new Integer JavaDoc(0), ""), "DOG");
70         assertEquals(c.toCode(new Integer JavaDoc(0), "D"), "DOG");
71         assertEquals(c.toCode(new Integer JavaDoc(0), "DO"), "DOG");
72         assertEquals(c.toCode(new Integer JavaDoc(0), "DoG"), "DOG");
73         assertEquals(c.toCode(new Integer JavaDoc(66), ""), "CAT");
74         assertEquals(c.toCode(new Integer JavaDoc(66), "C"), "CAT");
75         assertEquals(c.toCode(new Integer JavaDoc(66), "ca"), "CAT");
76         assertEquals(c.toCode(new Integer JavaDoc(66), "cAt"), "CAT");
77         assertEquals(c.toCode("", ""), "S1");
78         assertEquals(c.toCode("", "s"), "S1");
79         assertEquals(c.toCode("", "s1"), "S1");
80         try {
81             c.toCode("bogus", "bogus");
82             fail("Should have thrown ConstantException");
83         }
84         catch (ConstantException ex) {
85             // expected
86
}
87
88         assertEquals(c.toCodeForProperty(new Integer JavaDoc(1), "myProperty"), "MY_PROPERTY_NO");
89         assertEquals(c.toCodeForProperty(new Integer JavaDoc(2), "myProperty"), "MY_PROPERTY_YES");
90         try {
91             c.toCodeForProperty("bogus", "bogus");
92             fail("Should have thrown ConstantException");
93         }
94         catch (ConstantException ex) {
95             // expected
96
}
97     }
98     
99     
100     public static class A {
101         
102         public static final int DOG = 0;
103         public static final int CAT = 66;
104         public static final String JavaDoc S1 = "";
105
106         public static final int MY_PROPERTY_NO = 1;
107         public static final int MY_PROPERTY_YES = 2;
108
109         /** ignore these */
110         protected static final int P = -1;
111         protected boolean f;
112         static final Object JavaDoc o = new Object JavaDoc();
113     }
114
115 }
116
Popular Tags