001: package com.bm.utils;
002:
003: import java.beans.PropertyDescriptor;
004: import java.lang.reflect.InvocationTargetException;
005: import java.util.ArrayList;
006: import java.util.List;
007:
008: import junit.framework.Assert;
009:
010: import org.apache.commons.beanutils.PropertyUtilsBean;
011:
012: import com.bm.datagen.Generator;
013: import com.bm.datagen.annotations.GeneratorType;
014: import com.bm.datagen.empty.EmptyCollection;
015: import com.bm.datagen.random.primitive.PrimitiveRandomBooleanGenerator;
016: import com.bm.datagen.random.primitive.PrimitiveRandomDateGenerator;
017: import com.bm.datagen.random.primitive.PrimitiveRandomDoubleGenerator;
018: import com.bm.datagen.random.primitive.PrimitiveRandomIntegerGenerator;
019: import com.bm.datagen.random.primitive.PrimitiveRandomLongGenerator;
020: import com.bm.datagen.random.primitive.PrimitiveRandomShortGenerator;
021: import com.bm.datagen.random.primitive.PrimitiveRandomStringGenerator;
022:
023: /**
024: * This class exectues a simple test of getter setters. All seeters with
025: * primitive or basic type will be executed.
026: *
027: * @author Daniel Wiese
028: * @since 17.04.2006
029: */
030: public class SimpleGetterSetterTest extends Assert {
031:
032: private static final org.apache.log4j.Logger log = org.apache.log4j.Logger
033: .getLogger(SimpleGetterSetterTest.class);
034:
035: private static final List<Generator> DEFAULT_GENERATORS = new ArrayList<Generator>();
036:
037: private final Class toTestClass;
038:
039: private final PropertyUtilsBean propUtils = new PropertyUtilsBean();
040:
041: private final PropertyDescriptor[] properties;
042:
043: private final Object toTestObj;
044:
045: static {
046: DEFAULT_GENERATORS.add(new PrimitiveRandomDateGenerator());
047: DEFAULT_GENERATORS.add(new PrimitiveRandomBooleanGenerator());
048: DEFAULT_GENERATORS.add(new PrimitiveRandomIntegerGenerator());
049: DEFAULT_GENERATORS.add(new PrimitiveRandomLongGenerator());
050: DEFAULT_GENERATORS.add(new PrimitiveRandomShortGenerator());
051: DEFAULT_GENERATORS.add(new PrimitiveRandomStringGenerator());
052: DEFAULT_GENERATORS.add(new PrimitiveRandomDoubleGenerator());
053: DEFAULT_GENERATORS.add(new EmptyCollection());
054: }
055:
056: /**
057: * Constructor.
058: *
059: * @param toTest -
060: * the bean to test.
061: */
062: public SimpleGetterSetterTest(Object toTest) {
063: this .toTestClass = toTest.getClass();
064: properties = this .propUtils
065: .getPropertyDescriptors(this .toTestClass);
066: this .toTestObj = toTest;
067:
068: }
069:
070: /**
071: * Test all primitive getters/setters of the bean instance.
072: *
073: * @author Daniel Wiese
074: * @since 17.04.2006
075: */
076: public void testGetterSetter() {
077: for (PropertyDescriptor current : properties) {
078: if (current.getWriteMethod() != null
079: && current.getReadMethod() != null) {
080: Class[] params = current.getWriteMethod()
081: .getParameterTypes();
082: if (params.length == 1 && params[0] != null) {
083: Class toCreate = Ejb3Utils
084: .getNonPrimitiveType(params[0]);
085: Object[] value = new Object[1];
086: value[0] = this .getValue(toCreate);
087: if (value[0] != null) {
088: try {
089: log.debug("Testing property (get-/set "
090: + current.getName() + "()) ...");
091: // call the setter method
092: current.getWriteMethod().invoke(
093: this .toTestObj, value);
094:
095: // call the getter method
096: Object result = current.getReadMethod()
097: .invoke(this .toTestObj,
098: (Object[]) null);
099: assertNotNull(
100: "The result of getter-property ("
101: + current.getName()
102: + ") is null, but set.. was called (Expected: "
103: + value[0] + ")", result);
104: assertEquals(
105: "The result of getter-property ("
106: + current.getName()
107: + ") is (" + result
108: + ") but not (" + value[0]
109: + ")", value[0], result);
110: } catch (IllegalArgumentException e) {
111: log.error(
112: "WrongArgument-Can't invoke the setter of property ("
113: + current.getName() + ")",
114: e);
115: fail("WrongArgument-Can't invoke the setter of property ("
116: + current.getName() + ")");
117: throw new IllegalArgumentException(e);
118: } catch (IllegalAccessException e) {
119: log.error(
120: "WrongArgument-Can't invoke the setter of property ("
121: + current.getName() + ")",
122: e);
123: fail("WrongArgument-Can't invoke the setter of property ("
124: + current.getName() + ")");
125: throw new IllegalArgumentException(e);
126: } catch (InvocationTargetException e) {
127: log.error(
128: "Can't invoke the setter of property ("
129: + current.getName() + ")",
130: e);
131: fail("Can't invoke the setter of property ("
132: + current.getName() + ")");
133: throw new IllegalArgumentException(e);
134: }
135:
136: }
137: }
138: }
139:
140: }
141: }
142:
143: private Object getValue(Class type) {
144: Object back = null;
145: for (Generator current : DEFAULT_GENERATORS) {
146: final GeneratorType gType = Ejb3Utils
147: .getGeneratorTypeAnnotation(current);
148: if (type.equals(Ejb3Utils.getNonPrimitiveType(gType
149: .className()))) {
150: back = current.getValue();
151: break;
152: }
153: }
154:
155: return back;
156: }
157:
158: }
|