01: /*
02: * Copyright 2006-2007 The Kuali Foundation.
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
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: package org.kuali;
17:
18: import java.lang.reflect.Field;
19: import java.util.ArrayList;
20: import java.util.List;
21: import java.util.MissingResourceException;
22: import java.util.ResourceBundle;
23:
24: import org.kuali.kfs.KFSKeyConstants;
25: import org.kuali.kfs.context.KualiTestBase;
26:
27: public class KeyConstantsTest extends KualiTestBase {
28:
29: /**
30: * checks to see if the properties defined in the KFSKeyConstants class are in the AppilcationResources.properties
31: */
32: public final void testKeyConstants() throws Exception {
33: final ResourceBundle applicationResources = ResourceBundle
34: .getBundle("ApplicationResources");
35: List<String> keys = extractConstants(KFSKeyConstants.class);
36: assertFalse("no properties define in KFSKeyConstants", keys
37: .isEmpty());
38: List<String> notFound = new ArrayList<String>();
39: for (String key : keys) {
40: try {
41: applicationResources.getString(key);
42: } catch (MissingResourceException e) {
43: notFound.add(key);
44: }
45: }
46: assertTrue(
47: "The following keys found in KFSKeyConstants.java do not map to keys in ApplicationResources.properties: "
48: + notFound, notFound.isEmpty());
49:
50: }
51:
52: public static final List<String> extractConstants(Class clazz)
53: throws Exception {
54: // get all the properties defined by class parameter
55: List<String> keys = new ArrayList<String>();
56: Field[] fields = clazz.getDeclaredFields();
57: Object classInstance = clazz.newInstance();
58: for (Field field : fields) {
59: keys.add(field.get(classInstance).toString());
60: }
61:
62: // get all the properties for the classes declared by this class
63: Class[] classes = clazz.getDeclaredClasses();
64: for (Class declaredClass : classes) {
65: keys.addAll(extractConstants(declaredClass));
66: }
67: return keys;
68: }
69: }
|