1 /*
2  * Copyright 2002-2012 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.Locale;
20 import java.util.Set;
21 
22 import junit.framework.TestCase;
23 
24 /**
25  * @author Rod Johnson
26  * @author Juergen Hoeller
27  * @author Rick Evans
28  * @since 28.04.2003
29  */
30 public class ConstantsTests extends TestCase {
31 
testConstants()32 	public void testConstants() {
33 		Constants c = new Constants(A.class);
34 		assertEquals(A.class.getName(), c.getClassName());
35 		assertEquals(9, c.getSize());
36 
37 		assertEquals(c.asNumber("DOG").intValue(), A.DOG);
38 		assertEquals(c.asNumber("dog").intValue(), A.DOG);
39 		assertEquals(c.asNumber("cat").intValue(), A.CAT);
40 
41 		try {
42 			c.asNumber("bogus");
43 			fail("Can't get bogus field");
44 		}
45 		catch (ConstantException expected) {
46 		}
47 
48 		assertTrue(c.asString("S1").equals(A.S1));
49 		try {
50 			c.asNumber("S1");
51 			fail("Wrong type");
52 		}
53 		catch (ConstantException expected) {
54 		}
55 	}
56 
testGetNames()57 	public void testGetNames() {
58 		Constants c = new Constants(A.class);
59 
60 		Set<?> names = c.getNames("");
61 		assertEquals(c.getSize(), names.size());
62 		assertTrue(names.contains("DOG"));
63 		assertTrue(names.contains("CAT"));
64 		assertTrue(names.contains("S1"));
65 
66 		names = c.getNames("D");
67 		assertEquals(1, names.size());
68 		assertTrue(names.contains("DOG"));
69 
70 		names = c.getNames("d");
71 		assertEquals(1, names.size());
72 		assertTrue(names.contains("DOG"));
73 	}
74 
testGetValues()75 	public void testGetValues() {
76 		Constants c = new Constants(A.class);
77 
78 		Set<?> values = c.getValues("");
79 		assertEquals(7, values.size());
80 		assertTrue(values.contains(new Integer(0)));
81 		assertTrue(values.contains(new Integer(66)));
82 		assertTrue(values.contains(""));
83 
84 		values = c.getValues("D");
85 		assertEquals(1, values.size());
86 		assertTrue(values.contains(new Integer(0)));
87 
88 		values = c.getValues("prefix");
89 		assertEquals(2, values.size());
90 		assertTrue(values.contains(new Integer(1)));
91 		assertTrue(values.contains(new Integer(2)));
92 
93 		values = c.getValuesForProperty("myProperty");
94 		assertEquals(2, values.size());
95 		assertTrue(values.contains(new Integer(1)));
96 		assertTrue(values.contains(new Integer(2)));
97 	}
98 
testGetValuesInTurkey()99 	public void testGetValuesInTurkey() {
100 		Locale oldLocale = Locale.getDefault();
101 		Locale.setDefault(new Locale("tr", ""));
102 		try {
103 			Constants c = new Constants(A.class);
104 
105 			Set<?> values = c.getValues("");
106 			assertEquals(7, values.size());
107 			assertTrue(values.contains(new Integer(0)));
108 			assertTrue(values.contains(new Integer(66)));
109 			assertTrue(values.contains(""));
110 
111 			values = c.getValues("D");
112 			assertEquals(1, values.size());
113 			assertTrue(values.contains(new Integer(0)));
114 
115 			values = c.getValues("prefix");
116 			assertEquals(2, values.size());
117 			assertTrue(values.contains(new Integer(1)));
118 			assertTrue(values.contains(new Integer(2)));
119 
120 			values = c.getValuesForProperty("myProperty");
121 			assertEquals(2, values.size());
122 			assertTrue(values.contains(new Integer(1)));
123 			assertTrue(values.contains(new Integer(2)));
124 		}
125 		finally {
126 			Locale.setDefault(oldLocale);
127 		}
128 	}
129 
testSuffixAccess()130 	public void testSuffixAccess() {
131 		Constants c = new Constants(A.class);
132 
133 		Set<?> names = c.getNamesForSuffix("_PROPERTY");
134 		assertEquals(2, names.size());
135 		assertTrue(names.contains("NO_PROPERTY"));
136 		assertTrue(names.contains("YES_PROPERTY"));
137 
138 		Set<?> values = c.getValuesForSuffix("_PROPERTY");
139 		assertEquals(2, values.size());
140 		assertTrue(values.contains(new Integer(3)));
141 		assertTrue(values.contains(new Integer(4)));
142 	}
143 
testToCode()144 	public void testToCode() {
145 		Constants c = new Constants(A.class);
146 
147 		assertEquals(c.toCode(new Integer(0), ""), "DOG");
148 		assertEquals(c.toCode(new Integer(0), "D"), "DOG");
149 		assertEquals(c.toCode(new Integer(0), "DO"), "DOG");
150 		assertEquals(c.toCode(new Integer(0), "DoG"), "DOG");
151 		assertEquals(c.toCode(new Integer(0), null), "DOG");
152 		assertEquals(c.toCode(new Integer(66), ""), "CAT");
153 		assertEquals(c.toCode(new Integer(66), "C"), "CAT");
154 		assertEquals(c.toCode(new Integer(66), "ca"), "CAT");
155 		assertEquals(c.toCode(new Integer(66), "cAt"), "CAT");
156 		assertEquals(c.toCode(new Integer(66), null), "CAT");
157 		assertEquals(c.toCode("", ""), "S1");
158 		assertEquals(c.toCode("", "s"), "S1");
159 		assertEquals(c.toCode("", "s1"), "S1");
160 		assertEquals(c.toCode("", null), "S1");
161 		try {
162 			c.toCode("bogus", "bogus");
163 			fail("Should have thrown ConstantException");
164 		}
165 		catch (ConstantException expected) {
166 		}
167 		try {
168 			c.toCode("bogus", null);
169 			fail("Should have thrown ConstantException");
170 		}
171 		catch (ConstantException expected) {
172 		}
173 
174 		assertEquals(c.toCodeForProperty(new Integer(1), "myProperty"), "MY_PROPERTY_NO");
175 		assertEquals(c.toCodeForProperty(new Integer(2), "myProperty"), "MY_PROPERTY_YES");
176 		try {
177 			c.toCodeForProperty("bogus", "bogus");
178 			fail("Should have thrown ConstantException");
179 		}
180 		catch (ConstantException expected) {
181 		}
182 
183 		assertEquals(c.toCodeForSuffix(new Integer(0), ""), "DOG");
184 		assertEquals(c.toCodeForSuffix(new Integer(0), "G"), "DOG");
185 		assertEquals(c.toCodeForSuffix(new Integer(0), "OG"), "DOG");
186 		assertEquals(c.toCodeForSuffix(new Integer(0), "DoG"), "DOG");
187 		assertEquals(c.toCodeForSuffix(new Integer(0), null), "DOG");
188 		assertEquals(c.toCodeForSuffix(new Integer(66), ""), "CAT");
189 		assertEquals(c.toCodeForSuffix(new Integer(66), "T"), "CAT");
190 		assertEquals(c.toCodeForSuffix(new Integer(66), "at"), "CAT");
191 		assertEquals(c.toCodeForSuffix(new Integer(66), "cAt"), "CAT");
192 		assertEquals(c.toCodeForSuffix(new Integer(66), null), "CAT");
193 		assertEquals(c.toCodeForSuffix("", ""), "S1");
194 		assertEquals(c.toCodeForSuffix("", "1"), "S1");
195 		assertEquals(c.toCodeForSuffix("", "s1"), "S1");
196 		assertEquals(c.toCodeForSuffix("", null), "S1");
197 		try {
198 			c.toCodeForSuffix("bogus", "bogus");
199 			fail("Should have thrown ConstantException");
200 		}
201 		catch (ConstantException expected) {
202 		}
203 		try {
204 			c.toCodeForSuffix("bogus", null);
205 			fail("Should have thrown ConstantException");
206 		}
207 		catch (ConstantException expected) {
208 		}
209 	}
210 
testGetValuesWithNullPrefix()211 	public void testGetValuesWithNullPrefix() throws Exception {
212 		Constants c = new Constants(A.class);
213 		Set<?> values = c.getValues(null);
214 		assertEquals("Must have returned *all* public static final values", 7, values.size());
215 	}
216 
testGetValuesWithEmptyStringPrefix()217 	public void testGetValuesWithEmptyStringPrefix() throws Exception {
218 		Constants c = new Constants(A.class);
219 		Set<Object> values = c.getValues("");
220 		assertEquals("Must have returned *all* public static final values", 7, values.size());
221 	}
222 
testGetValuesWithWhitespacedStringPrefix()223 	public void testGetValuesWithWhitespacedStringPrefix() throws Exception {
224 		Constants c = new Constants(A.class);
225 		Set<?> values = c.getValues(" ");
226 		assertEquals("Must have returned *all* public static final values", 7, values.size());
227 	}
228 
testWithClassThatExposesNoConstants()229 	public void testWithClassThatExposesNoConstants() throws Exception {
230 		Constants c = new Constants(NoConstants.class);
231 		assertEquals(0, c.getSize());
232 		final Set<?> values = c.getValues("");
233 		assertNotNull(values);
234 		assertEquals(0, values.size());
235 	}
236 
testCtorWithNullClass()237 	public void testCtorWithNullClass() throws Exception {
238 		try {
239 			new Constants(null);
240 			fail("Must have thrown IllegalArgumentException");
241 		}
242 		catch (IllegalArgumentException expected) {}
243 	}
244 
245 
246 	private static final class NoConstants {
247 	}
248 
249 
250 	@SuppressWarnings("unused")
251 	private static final class A {
252 
253 		public static final int DOG = 0;
254 		public static final int CAT = 66;
255 		public static final String S1 = "";
256 
257 		public static final int PREFIX_NO = 1;
258 		public static final int PREFIX_YES = 2;
259 
260 		public static final int MY_PROPERTY_NO = 1;
261 		public static final int MY_PROPERTY_YES = 2;
262 
263 		public static final int NO_PROPERTY = 3;
264 		public static final int YES_PROPERTY = 4;
265 
266 		/** ignore these */
267 		protected static final int P = -1;
268 		protected boolean f;
269 		static final Object o = new Object();
270 	}
271 
272 }
273