001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.assembler.classic;
017:
018: import junit.framework.TestCase;
019:
020: import javax.xml.namespace.QName;
021: import java.lang.annotation.Annotation;
022: import java.lang.reflect.Constructor;
023: import java.lang.reflect.Field;
024: import java.lang.reflect.Method;
025: import java.lang.reflect.Modifier;
026: import java.lang.reflect.ParameterizedType;
027: import java.lang.reflect.Type;
028: import java.util.ArrayList;
029: import java.util.List;
030: import java.util.Properties;
031: import java.util.Set;
032:
033: /**
034: * @version $Rev: 602704 $ $Date: 2007-12-09 09:58:22 -0800 $
035: */
036: public class OpenEjbConfigurationValidationTest extends TestCase {
037:
038: private List<Class> seen = new ArrayList<Class>();
039:
040: public void testValidate() throws Exception {
041: seen.add(Object.class);
042: validate(OpenEjbConfiguration.class);
043: }
044:
045: private void validate(Class clazz) throws Exception {
046: if (clazz == null)
047: return;
048: if (seen.contains(clazz))
049: return;
050:
051: validate(clazz.getSuperclass());
052:
053: seen.add(clazz);
054:
055: String simpleName = clazz.getSimpleName();
056:
057: Constructor[] constructors = clazz.getDeclaredConstructors();
058: assertEquals("constructors are not allowed: " + simpleName, 1,
059: constructors.length);
060: assertEquals("constructors are not allowed: " + simpleName, 0,
061: constructors[0].getParameterTypes().length);
062:
063: Method[] methods = clazz.getDeclaredMethods();
064: assertEquals("methods are not allowed: " + simpleName, 0,
065: methods.length);
066:
067: Annotation[] annotations = clazz.getDeclaredAnnotations();
068: assertEquals("annotations are not allowed: " + simpleName, 0,
069: annotations.length);
070:
071: Field[] fields = clazz.getDeclaredFields();
072: for (Field field : fields) {
073:
074: assertTrue("Non-public fields are not allowed: "
075: + simpleName + "." + field.getName(), Modifier
076: .isPublic(field.getModifiers()));
077:
078: annotations = clazz.getDeclaredAnnotations();
079: assertEquals("annotations are not allowed: " + simpleName
080: + "." + field.getName(), 0, annotations.length);
081:
082: Class type = field.getType();
083: if (type.isArray()) {
084: type = type.getComponentType();
085: }
086:
087: if (List.class.isAssignableFrom(type)) {
088: type = getGenericType(field);
089: assertNotNull("Lists must have a generic type: "
090: + simpleName + "." + field.getName(), type);
091: }
092:
093: if (Set.class.isAssignableFrom(type)) {
094: type = getGenericType(field);
095: assertNotNull("Sets must have a generic type: "
096: + simpleName + "." + field.getName(), type);
097: }
098:
099: if (type.isPrimitive()) {
100: continue;
101: }
102:
103: if (String.class.isAssignableFrom(type)) {
104: continue;
105: }
106:
107: if (Properties.class.isAssignableFrom(type)) {
108: continue;
109: }
110:
111: if (QName.class.isAssignableFrom(type)) {
112: continue;
113: }
114:
115: if (InfoObject.class.isAssignableFrom(type)) {
116: validate(type);
117: continue;
118: }
119:
120: fail("Field is not of an allowed type: " + simpleName + "."
121: + field.getName());
122: }
123: }
124:
125: private Class getGenericType(Field field) throws Exception {
126: Type genericType = field.getGenericType();
127:
128: if (genericType instanceof ParameterizedType) {
129: ParameterizedType parameterizedType = (ParameterizedType) genericType;
130: Type firstParamType = parameterizedType
131: .getActualTypeArguments()[0];
132: return (Class) firstParamType;
133: } else if (genericType instanceof Class) {
134: return (Class) genericType;
135: } else {
136: return null;
137: }
138: }
139: }
|