1 package org.org.usurper;
2
3 import java.beans.BeanInfo;
4 import java.beans.IntrospectionException;
5 import java.beans.Introspector;
6 import java.beans.PropertyDescriptor;
7 import java.lang.reflect.Array;
8 import java.lang.reflect.Field;
9 import java.math.BigDecimal;
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18
19 import org.org.usurper.dummydomain.BigDecimalHandler;
20 import org.org.usurper.dummydomain.BigIntegerHandler;
21 import org.org.usurper.dummydomain.DummyCountCallback;
22 import org.org.usurper.dummydomain.DummyVO;
23 import org.org.usurper.dummydomain.SpecificIntegerFieldHandler;
24 import org.org.usurper.dummydomain.SpecificStringFieldHandler;
25 import org.org.usurper.handlers.basic.AbstractPropertyTypeHandler;
26 import org.org.usurper.handlers.basic.AbstractSpecificPropertyHandler;
27 import org.org.usurper.handlers.basic.ArrayHandler;
28 import org.org.usurper.handlers.basic.EnumHandler;
29 import org.org.usurper.model.PropertyTypeDefinition;
30 import org.org.usurper.model.SpecificPropertyDefinition;
31 import org.org.usurper.setup.IUsurperGeneratorSetup;
32 import org.org.usurper.setup.constants.OnMissingHandlers;
33 import org.org.usurper.setup.constants.PropertyWritingMechanism;
34
35 public final class TestCommons {
36
37 private TestCommons() {
38 }
39
40 public final static class NullPropertyException extends Exception {
41
42
43
44
45 private static final long serialVersionUID = 2281733649024485007L;
46
47
48
49
50 public NullPropertyException() {
51 super();
52 }
53
54
55
56
57
58 public NullPropertyException(String message, Throwable cause) {
59 super(message, cause);
60 }
61
62
63
64
65 public NullPropertyException(String message) {
66 super(message);
67 }
68
69
70
71
72 public NullPropertyException(Throwable cause) {
73 super(cause);
74 }
75
76 }
77
78 public final static class BadSetupException extends Exception {
79
80
81
82
83 private static final long serialVersionUID = -5417693717950210139L;
84
85
86
87
88 public BadSetupException() {
89 super();
90 }
91
92
93
94
95
96 public BadSetupException(String message, Throwable cause) {
97 super(message, cause);
98 }
99
100
101
102
103 public BadSetupException(String message) {
104 super(message);
105 }
106
107
108
109
110 public BadSetupException(Throwable cause) {
111 super(cause);
112 }
113
114 }
115
116 public static final String HANDLED_STRING = "HANDLED_STRING";
117 public static final BigInteger BIG_INTEGER_VALUE = new BigInteger("2");
118 public static List<BigInteger> BIG_INTEGER_VALUES_LIST;
119 public static Set<AbstractPropertyTypeHandler> PROPERTY_TYPE_HANDLERS_SET;
120 public static Set<AbstractSpecificPropertyHandler> SPECIFIC_PROPERTY_HANDLERS_SET;
121 static {
122 List<BigInteger> tmpList = BIG_INTEGER_VALUES_LIST = new ArrayList<BigInteger>();
123 tmpList.add(new BigInteger("1"));
124 tmpList.add(new BigInteger("2"));
125 tmpList.add(new BigInteger("3"));
126 BIG_INTEGER_VALUES_LIST = Collections.unmodifiableList(tmpList);
127 Set<AbstractPropertyTypeHandler> tmpList2 = new HashSet<AbstractPropertyTypeHandler>();
128 tmpList2.add(new BigDecimalHandler(new PropertyTypeDefinition(BigDecimal.class)));
129 tmpList2.add(new BigIntegerHandler(new PropertyTypeDefinition(BigInteger.class)));
130 PROPERTY_TYPE_HANDLERS_SET = tmpList2;
131 Set<AbstractSpecificPropertyHandler> tmpList3 = new HashSet<AbstractSpecificPropertyHandler>();
132 tmpList3.add(new SpecificIntegerFieldHandler(new SpecificPropertyDefinition(DummyVO.class, "integerField")));
133 tmpList3.add(new SpecificStringFieldHandler(new SpecificPropertyDefinition(DummyVO.class, "stringField")));
134 SPECIFIC_PROPERTY_HANDLERS_SET = tmpList3;
135 }
136
137 public static final DummyCountCallback DUMMY_COLLECTION_COUNT_CALLBACK = new DummyCountCallback();
138 public static final EnumHandler ENUM_HANDLER = new EnumHandler();
139 public static final ArrayHandler ARRAY_HANDLER = new ArrayHandler();
140
141 public static void auditVO(Object valueObject) throws NullPropertyException {
142 try {
143 BeanInfo beanInfo = Introspector.getBeanInfo(valueObject.getClass());
144 PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
145 for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
146 if (!propertyDescriptor.getName().equals("class")) {
147 String propertyName = propertyDescriptor.getName();
148 try {
149 Object result = getProperty(valueObject, propertyName);
150 if (result == null) {
151 throw new NullPropertyException("Property <" + propertyName + "> should not be null");
152 }
153 if (result.getClass().isArray()) {
154 if (Array.getLength(result) == 0) {
155 throw new NullPropertyException("Array Property <" + propertyName + "> should not be empty");
156 }
157 }
158 if (result instanceof Collection) {
159 if (Collections.frequency((Collection<?>) result, null) != 0) {
160 throw new NullPropertyException("Collection Property <" + propertyName + "> should not contain any null item");
161 }
162 }
163 if (result instanceof Map) {
164 if (Collections.frequency(((Map<?, ?>) result).keySet(), null) != 0) {
165 throw new NullPropertyException("Map Property <" + propertyName + "> should not contain any null key");
166 }
167 if (Collections.frequency(((Map<?, ?>) result).values(), null) != 0) {
168 throw new NullPropertyException("Map Property <" + propertyName + "> should not contain any null value");
169 }
170 }
171 } catch (IllegalAccessException e) {
172 System.err.println("cannot access bean property (illegal access) :" + valueObject.getClass().getName() + "." + propertyName);
173 } catch (NoSuchFieldException e) {
174 System.err.println("no such field :" + valueObject.getClass().getName() + "." + propertyName);
175 }
176 }
177 }
178 } catch (IntrospectionException e) {
179 throw new UsurperException("cannot get Bean Info from bean :" + valueObject.getClass().getName(), e);
180 }
181 }
182
183 public static Object getProperty(Object object, String propertyName) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
184 Field field = object.getClass().getDeclaredField(propertyName);
185 field.setAccessible(true);
186 Object value = field.get(object);
187 return value;
188 }
189
190 public static void auditSetup(IUsurperGeneratorSetup usurperGeneratorSetup) throws BadSetupException {
191 auditParameter("Array handler should be an " + ArrayHandler.class.getName(), usurperGeneratorSetup.getArrayHandler().getClass().getName().equals(ArrayHandler.class.getName()));
192 auditParameter("Enum handler should be an " + EnumHandler.class.getName(), usurperGeneratorSetup.getEnumHandler().getClass().getName().equals(EnumHandler.class.getName()));
193 auditParameter("Collection callback should be a " + DummyCountCallback.class.getName(), usurperGeneratorSetup.getCountCallback().getClass().getName().equals(DummyCountCallback.class.getName()));
194 auditParameter("On missing handlers behaviour should be " + OnMissingHandlers.SKIP, OnMissingHandlers.SKIP.equals(usurperGeneratorSetup.getOnMissingHandlers()));
195 auditParameter("Property writing mechanism should be " + PropertyWritingMechanism.USE_SETTERS, PropertyWritingMechanism.USE_SETTERS.equals(usurperGeneratorSetup.getPropertyWritingMechanism()));
196
197 assertNotNull("Property Type Handlers should not be null", usurperGeneratorSetup.getPropertyTypeHandlersMap());
198 assertNotNull("Specific Property Handlers should not be null", usurperGeneratorSetup.getSpecificPropertyHandlersMap());
199
200 final String expectedPropertyTypeHandlerClassName = BigDecimalHandler.class.getName();
201 auditParameter("Property Type handler for BigDecimal should be: " + expectedPropertyTypeHandlerClassName, usurperGeneratorSetup.getPropertyTypeHandlersMap().get(new PropertyTypeDefinition(BigDecimal.class)).getClass().getName().equals(expectedPropertyTypeHandlerClassName));
202 auditParameter("Specific Property handler for DummyVO.integerField should be: " + SpecificIntegerFieldHandler.class.getName(), usurperGeneratorSetup.getSpecificPropertyHandlersMap().get(new SpecificPropertyDefinition(DummyVO.class, "integerField")).getClass().getName().equals(SpecificIntegerFieldHandler.class.getName()));
203
204 final String expectedPropertyTypeHandlerClassName2 = BigIntegerHandler.class.getName();
205 auditParameter("Property Type handler for BigInteger should be: " + expectedPropertyTypeHandlerClassName2, usurperGeneratorSetup.getPropertyTypeHandlersMap().get(new PropertyTypeDefinition(BigInteger.class)).getClass().getName().equals(expectedPropertyTypeHandlerClassName2));
206 auditParameter("Specific Property handler for DummyVO.integerField should be: " + SpecificStringFieldHandler.class.getName(), usurperGeneratorSetup.getSpecificPropertyHandlersMap().get(new SpecificPropertyDefinition(DummyVO.class, "stringField")).getClass().getName().equals(SpecificStringFieldHandler.class.getName()));
207 }
208
209 private static void assertNotNull(String message, Object toBeTested) throws BadSetupException {
210 if (toBeTested == null) {
211 throw new BadSetupException(message);
212 }
213 }
214
215 private static void auditParameter(String message, Boolean clauseOk) throws BadSetupException {
216 if (!clauseOk) {
217 throw new BadSetupException(message);
218 }
219 }
220 }