001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali;
017:
018: import java.lang.reflect.Method;
019: import java.util.Collection;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.Map;
023: import java.util.Set;
024:
025: import org.kuali.core.ConfigProperties;
026: import org.kuali.kfs.context.KualiTestBase;
027: import org.kuali.test.ConfigureContext;
028: import org.springframework.beans.BeanUtils;
029:
030: /**
031: * This class tests the Config constants.
032: */
033: @ConfigureContext
034: public class ConfigPropertiesTest extends KualiTestBase {
035: private static final String SIMPLE_TESTING_KEY = "simpleTestingKey";
036: private static final String SIMPLE_TESTING_VALUE = "simpleTestingValue";
037: private static final String COMPLEX_TESTING_KEY = "complex.testing.key";
038: private static final String COMPLEX_TESTING_VALUE = "complex testing value";
039:
040: private static boolean setUpOnce = false;
041: private static ConfigProperties configConstants;
042:
043: @Override
044: protected void setUp() throws Exception {
045: super .setUp();
046:
047: if (!setUpOnce) {
048: setUpOnce = true;
049: configConstants = new ConfigProperties();
050: }
051: }
052:
053: public final void testSize() throws Exception {
054: assertTrue(configConstants.size() > 0);
055: }
056:
057: public final void testIsEmpty() throws Exception {
058: assertFalse(configConstants.isEmpty());
059: }
060:
061: public final void testContainsKey_invalidKey() throws Exception {
062: boolean failedAsExpected = false;
063:
064: try {
065: configConstants.containsKey(null);
066: } catch (IllegalArgumentException e) {
067: failedAsExpected = true;
068: }
069:
070: assertTrue(failedAsExpected);
071: }
072:
073: public final void testContainsKey_unknownKey() throws Exception {
074: assertFalse(configConstants.containsKey("hopefully unused key"));
075: }
076:
077: public final void testContainsKey_simpleKey() throws Exception {
078: assertTrue(configConstants.containsKey(SIMPLE_TESTING_KEY));
079: }
080:
081: public final void testContainsKey_complexKey() throws Exception {
082: assertTrue(configConstants.containsKey(COMPLEX_TESTING_KEY));
083: }
084:
085: public final void testContainsValue_invalidValue() throws Exception {
086: boolean failedAsExpected = false;
087:
088: try {
089: configConstants.containsValue(null);
090: } catch (IllegalArgumentException e) {
091: failedAsExpected = true;
092: }
093:
094: assertTrue(failedAsExpected);
095: }
096:
097: public final void testContainsValue_unknownValue() throws Exception {
098: assertFalse(configConstants
099: .containsValue("hopefully unknown value"));
100: }
101:
102: public final void testContainsValue_value1() throws Exception {
103: assertTrue(configConstants.containsValue(SIMPLE_TESTING_VALUE));
104: }
105:
106: public final void testContainsValue_value2() throws Exception {
107: assertTrue(configConstants.containsValue(COMPLEX_TESTING_VALUE));
108: }
109:
110: public final void testValues_readOnly() throws Exception {
111: Collection values = configConstants.values();
112:
113: assertEquals(configConstants.size(), values.size());
114: assertTrue(values.contains(SIMPLE_TESTING_VALUE));
115: assertTrue(values.contains(COMPLEX_TESTING_VALUE));
116: }
117:
118: public final void testValues_readWrite() throws Exception {
119: Collection values = configConstants.values();
120:
121: boolean failedAsExpected = false;
122: try {
123: values.remove(SIMPLE_TESTING_VALUE);
124: } catch (UnsupportedOperationException e) {
125: failedAsExpected = true;
126: }
127:
128: assertTrue(failedAsExpected);
129: }
130:
131: public final void testEntrySet_readOnly() throws Exception {
132: Set entrySet = configConstants.entrySet();
133:
134: assertEquals(configConstants.size(), entrySet.size());
135: boolean foundSimple = false;
136: boolean foundComplex = false;
137: for (Iterator i = entrySet.iterator(); i.hasNext();) {
138: Map.Entry e = (Map.Entry) i.next();
139: if (e.getKey().equals(SIMPLE_TESTING_KEY)) {
140: foundSimple = true;
141: } else if (e.getKey().equals(COMPLEX_TESTING_KEY)) {
142: foundComplex = true;
143: }
144: }
145:
146: assertTrue(foundSimple);
147: assertTrue(foundComplex);
148: }
149:
150: public final void testEntrySet_readWrite() throws Exception {
151: Set entrySet = configConstants.entrySet();
152:
153: boolean failedAsExpected = false;
154: try {
155: entrySet.clear();
156: } catch (UnsupportedOperationException e) {
157: failedAsExpected = true;
158: }
159:
160: assertTrue(failedAsExpected);
161: }
162:
163: public final void testKeySet_readOnly() throws Exception {
164: Set keySet = configConstants.keySet();
165:
166: assertEquals(configConstants.size(), keySet.size());
167: assertTrue(keySet.contains(SIMPLE_TESTING_KEY));
168: assertTrue(keySet.contains(COMPLEX_TESTING_KEY));
169: }
170:
171: public final void testKeySet_readWrite() throws Exception {
172: Set keySet = configConstants.keySet();
173:
174: boolean failedAsExpected = false;
175: try {
176: keySet.clear();
177: } catch (UnsupportedOperationException e) {
178: failedAsExpected = true;
179: }
180:
181: assertTrue(failedAsExpected);
182: }
183:
184: public final void testPutAll() throws Exception {
185: HashMap h = new HashMap();
186: h.put("something", "new");
187:
188: boolean failedAsExpected = false;
189: try {
190: configConstants.putAll(h);
191: } catch (UnsupportedOperationException e) {
192: failedAsExpected = true;
193: }
194:
195: assertTrue(failedAsExpected);
196: }
197:
198: public final void testClear() throws Exception {
199: boolean failedAsExpected = false;
200: try {
201: configConstants.clear();
202: } catch (UnsupportedOperationException e) {
203: failedAsExpected = true;
204: }
205:
206: assertTrue(failedAsExpected);
207: }
208:
209: public final void testRemove() throws Exception {
210: boolean failedAsExpected = false;
211: try {
212: configConstants.remove(SIMPLE_TESTING_KEY);
213: } catch (UnsupportedOperationException e) {
214: failedAsExpected = true;
215: }
216:
217: assertTrue(failedAsExpected);
218: }
219:
220: public final void testPut() throws Exception {
221: boolean failedAsExpected = false;
222: try {
223: configConstants.put("something", "new");
224: } catch (UnsupportedOperationException e) {
225: failedAsExpected = true;
226: }
227:
228: assertTrue(failedAsExpected);
229: }
230:
231: public final void testGet_invalidKey() {
232: boolean failedAsExpected = false;
233:
234: try {
235: configConstants.get(null);
236: } catch (IllegalArgumentException e) {
237: failedAsExpected = true;
238: }
239:
240: assertTrue(failedAsExpected);
241: }
242:
243: public final void testGet_unknownKey() {
244: Object value = configConstants.get("hopefully unknown key");
245:
246: assertNull(value);
247: }
248:
249: public final void testGet_simpleKey() {
250: String value = configConstants.get(SIMPLE_TESTING_KEY)
251: .toString();
252:
253: assertNotNull(value);
254: assertEquals(SIMPLE_TESTING_VALUE, value);
255: }
256:
257: public final void testGet_complexKey() {
258: String value = configConstants.get(COMPLEX_TESTING_KEY)
259: .toString();
260:
261: assertNotNull(value);
262: assertEquals(COMPLEX_TESTING_VALUE, value);
263: }
264:
265: public final void testGet_multiLevel() throws Exception {
266: Class[] getParamTypes = { Object.class };
267:
268: Object level1 = configConstants.get("complex");
269:
270: Method m1 = BeanUtils.findMethod(level1.getClass(), "get",
271: getParamTypes);
272: Object level2 = m1.invoke(level1, new Object[] { "testing" });
273:
274: Method m2 = BeanUtils.findMethod(level2.getClass(), "get",
275: getParamTypes);
276: Object level3 = m2.invoke(level2, new Object[] { "key" });
277:
278: String value = level3.toString();
279:
280: assertNotNull(value);
281: assertEquals(COMPLEX_TESTING_VALUE, value);
282: }
283: }
|