Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
317   738   106   15.1
148   499   0.33   21
21     5.05  
1    
 
  AbstractValueObjectTestCase       Line # 38 317 106 84.2% 0.84156376
 
  (50)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10    * This program is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    * GNU General Public License for more details.
14    */
15   
16    package org.qedeq.kernel.se.test;
17   
18    import java.lang.reflect.Constructor;
19    import java.lang.reflect.Field;
20    import java.lang.reflect.InvocationTargetException;
21    import java.lang.reflect.Method;
22    import java.util.ArrayList;
23    import java.util.HashSet;
24    import java.util.List;
25    import java.util.Set;
26   
27    import org.qedeq.base.test.QedeqTestCase;
28    import org.qedeq.kernel.se.base.list.Element;
29    import org.qedeq.kernel.se.dto.list.DefaultAtom;
30   
31   
32    /**
33    * Basis for value object tests.
34    * @see #testAll()
35    *
36    * @author Michael Meyling
37    */
 
38    public abstract class AbstractValueObjectTestCase extends QedeqTestCase {
39   
40    /** Set of all methods to check. */
41    private Set methodsToCheck;
42   
43    /** Set of already checked methods. */
44    private Set checkedMethods;
45   
 
46  109 toggle protected void setUp() throws Exception {
47  109 methodsToCheck = new HashSet();
48  109 checkedMethods = new HashSet();
49   
50  109 final Class clazz = getTestedClass();
51   
52  109 Method[] methods = clazz.getDeclaredMethods();
53  1386 for (int i = 0; i < methods.length; i++) {
54  1277 if (methods[i].getName().startsWith("$VRi")) { // because of emma
55  0 continue;
56    }
57  1277 getMethodsToCheck().add(methods[i].getName());
58    }
59    }
60   
61    /**
62    * Return the value object class to test.
63    *
64    * @return Class to test.
65    */
66    protected abstract Class getTestedClass();
67   
68    /**
69    * Test everything we could. This includes getter and setter, but
70    * the combination of <code>add</code>, <code>size</code> and
71    * <code>get(int)</code> is possible too.
72    * <p>
73    * This method also tests <code>equal</code>, <code>hashCode</code>
74    * and <code>toString</code>.
75    * <p>
76    * Preconditions for using this method are the following.
77    * <ul>
78    * <li>For every attribute of the tested class exists a combination
79    * of getter and setter with matching names or a triple of
80    * <code>add</code>, <code>size</code>, <code>get(int)</code>.</li>
81    * <li>Every attribute is of type {@link String}, {@link Integer},
82    * {@link Boolean}, {@link List} or fulfills the preconditions for
83    * itself.</li>
84    * </ul>
85    *
86    * @throws Exception Something went wrong. Perhaps the preconditions were
87    * violated.
88    */
 
89  50 toggle public void testAll() throws Exception {
90  50 checkGetterAndSetter();
91  50 checkToString();
92  50 checkHashCode();
93  50 checkEquals();
94  50 checkAllChecked();
95    }
96   
97    /**
98    * Check the setters and getters and <code>add</code>, <code>size</code> and
99    * <code>get(int)</code>.
100    * If the attribute names don't match the method names we have a problem.
101    *
102    * @throws Exception Something went wrong. Perhaps the preconditions in {@link #testAll()}
103    * were violated.
104    */
 
105  50 toggle protected void checkGetterAndSetter() throws Exception {
106  50 final Class clazz = getTestedClass();
107  50 Field[] attrs = clazz.getDeclaredFields();
108  50 if (attrs.length == 0) {
109  0 fail("no attributes found in class " + clazz.getName());
110    }
111    // iterate over every attribute of the value object
112  179 for (int i = 0; i < attrs.length; i++) {
113  129 String attrName = attrs[i].getName();
114  129 if (attrName.startsWith("__")) { // because of clover, ignore those attributes
115  0 continue;
116    }
117  129 if (attrName.startsWith("$VRc")) { // because of emma, ignore those attributes
118  0 continue;
119    }
120  129 if (attrName.startsWith("serialVersionUID")) { // because of emma, ignore those attributes
121  0 continue;
122    }
123  129 boolean tested = false;
124  129 tested = tested || testGetSetAdd(clazz, attrs[i]);
125  129 tested = tested || testAddSizeGet(clazz, attrs[i]);
126  129 if (!tested) {
127  0 fail("could not test attribute " + attrName + " in class " + clazz.getName());
128    }
129    }
130    }
131   
132    /**
133    * Tests getter and setter methods for an attribute of a class. Also tests an <code>add</code>
134    * method for list attributes.
135    *
136    * @param clazz Attribute is in this class.
137    * @param attr Test methods for this attribute.
138    * @return Did this test match? If <code>true</code> all those methods were tested.
139    * @throws Exception Test failure.
140    */
 
141  129 toggle protected boolean testGetSetAdd(final Class clazz, final Field attr) throws Exception {
142  129 final Method[] methods = clazz.getDeclaredMethods();
143  129 final String attrName = attr.getName();
144  129 Method getter = null;
145  129 Method setter = null;
146  129 Method adder = null;
147  1828 for (int j = 0; j < methods.length; j++) {
148  1699 if (methods[j].getName().equals("get" + getUName(attrName))) {
149  115 getter = methods[j];
150    }
151  1699 if (methods[j].getName().equals("set" + getUName(attrName))) {
152  115 setter = methods[j];
153    }
154  1699 if ((methods[j].getName() + "List").equals("add" + getUName(attrName))) {
155  21 adder = methods[j];
156    }
157    }
158    // are getter and setter known?
159  129 if (getter == null || setter == null) {
160  14 return false;
161    }
162  115 final Object vo = getObject(clazz);
163  115 final Object result1 = getter.invoke(vo, new Object[0]);
164  115 if (attr.getType().isPrimitive()) {
165  1 if (attr.getType() == Integer.TYPE) {
166  1 if (!result1.equals(new Integer(0))) {
167  0 return false;
168    }
169    } else {
170  0 throw new RuntimeException("only int supported yet");
171    }
172    } else {
173  114 assertNull(result1);
174    }
175  115 final Object value = getFilledObject(getter.getReturnType(), clazz, attrName);
176  115 setter.invoke(vo, new Object[] {value});
177  115 final Object result2 = getter.invoke(vo, new Object[0]);
178  115 assertEquals(value, result2);
179  115 if (adder != null) {
180  7 final Method size1 = result2.getClass().getMethod("size", new Class[0]);
181  7 final int number1 = ((Integer) size1.invoke(result2, new Object[0])).intValue();
182   
183  7 final Object add = getFilledObject(adder.getParameterTypes()[0], clazz, attrName);
184  7 adder.invoke(vo, new Object[] {add});
185   
186  7 final Object result3 = getter.invoke(vo, new Object[0]);
187  7 final Method size2 = result3.getClass().getMethod("size", new Class[0]);
188  7 final int number2 = ((Integer) size2.invoke(result3, new Object[0])).intValue();
189  7 assertEquals(number1 + 1, number2);
190  7 addChecked(adder);
191  7 removeMethodToCheck(adder.getName());
192    }
193  115 if (attr.getType().isPrimitive()) {
194  1 if (attr.getType() == Integer.TYPE) {
195  1 setter.invoke(vo, new Object[] {new Integer(0)});
196    } else {
197  0 throw new RuntimeException("only int supported yet");
198    }
199    } else {
200  114 setter.invoke(vo, new Object[] {null});
201    }
202  115 final Object result4 = getter.invoke(vo, new Object[0]);
203  115 if (attr.getType().isPrimitive()) {
204  1 if (attr.getType() == Integer.TYPE) {
205  1 assertEquals(new Integer(0), result4);
206    } else {
207  0 throw new RuntimeException("only int supported yet");
208    }
209    } else {
210  114 assertNull(result4);
211    }
212  115 addChecked(setter);
213  115 removeMethodToCheck(setter.getName());
214  115 addChecked(getter);
215  115 removeMethodToCheck(getter.getName());
216  115 return true;
217    }
218   
 
219  279 toggle protected void addChecked(Method adder) {
220  279 checkedMethods.add(adder.getName());
221    }
222   
223    /**
224    * Tests <code>add</code>, <code>get</code> and <code>size</code> methods for an attribute of a
225    * class.
226    *
227    * @param clazz Attribute is in this class.
228    * @param attr Test methods for this attribute.
229    * @return Did this test match? If <code>true</code> all those methods were tested.
230    * @throws Exception Test failure.
231    */
 
232  14 toggle protected boolean testAddSizeGet(final Class clazz, final Field attr) throws Exception {
233  14 final Method[] methods = clazz.getDeclaredMethods();
234  14 final String attrName = attr.getName();
235  14 Method getter = null;
236  14 Method setter = null;
237    // check if attribute could be set by "add"
238  14 Method size = null;
239  98 for (int j = 0; j < methods.length; j++) {
240  84 if (methods[j].getName().equals("add")) {
241  14 setter = methods[j];
242    }
243  84 if (methods[j].getName().equals("get")) {
244  14 getter = methods[j];
245    }
246  84 if (methods[j].getName().equals("size")) {
247  14 size = methods[j];
248    }
249    }
250  14 if (getter == null || setter == null || size == null) { // testable?
251  0 return false;
252    }
253  14 final Object vo = getObject(clazz);
254  14 try {
255  14 getter.invoke(vo, new Object[] {new Integer(0)});
256  0 fail("IndexOutOfBoundsException expected");
257    } catch (InvocationTargetException e) {
258  14 if (!(e.getCause() instanceof IndexOutOfBoundsException)) {
259  0 fail("IndexOutOfBoundsException expected");
260    }
261    }
262  14 final Object zero = size.invoke(vo, new Object[0]);
263  14 assertEquals(new Integer(0), zero);
264  14 final Object value = getFilledObject(getter.getReturnType(), clazz, attrName);
265  14 setter.invoke(vo, new Object[] {value});
266  14 final Object result2 = getter.invoke(vo, new Object[] {new Integer(0)});
267  14 assertEquals(value, result2);
268  14 final Object one = size.invoke(vo, new Object[0]);
269  14 assertEquals(new Integer(1), one);
270  14 assertFalse(vo.equals(getFilledObject(getter.getReturnType(), clazz, attrName)));
271  14 addChecked(setter);
272  14 removeMethodToCheck(setter.getName());
273  14 addChecked(getter);
274  14 removeMethodToCheck(getter.getName());
275  14 addChecked(size);
276  14 removeMethodToCheck(size.getName());
277  14 return true;
278    }
279   
280    /**
281    * Test the <code>toString</code> method. This method is also tested with other check methods.
282    *
283    * @throws Exception Something went wrong. Perhaps the preconditions in {@link #testAll()}
284    * were violated.
285    */
 
286  50 toggle protected void checkToString() throws Exception {
287    {
288  50 final Object vo1 = getObject(getTestedClass());
289  50 assertNotNull(vo1.toString());
290  50 final Object vo2 = getObject(getTestedClass());
291  50 assertEquals(vo1.toString(), vo2.toString());
292    }
293    {
294  50 final Object vo1 = getFilledObject(getTestedClass());
295  50 assertNotNull(vo1.toString());
296  50 final Object vo2 = getFilledObject(getTestedClass());
297  50 assertEquals(vo1.toString(), vo2.toString());
298    }
299  50 removeMethodToCheck("toString");
300    }
301   
302    /**
303    * Remove method from list of methods to test. Be careful: overloaded
304    * methods must be tested within one method!
305    *
306    * @param name Remove this method name.
307    */
 
308  681 toggle protected final void removeMethodToCheck(final String name) {
309  681 getMethodsToCheck().remove(name);
310    }
311   
312    /**
313    * Test the <code>hashCode</code> method. This method is also tested with other check methods.
314    *
315    * @throws Exception Something went wrong. Perhaps the preconditions in {@link #testAll()}
316    * were violated.
317    */
 
318  50 toggle protected void checkHashCode() throws Exception {
319    {
320  50 final Object vo1 = getObject(getTestedClass());
321  50 final Object vo2 = getObject(getTestedClass());
322  50 assertTrue(vo1.hashCode() == vo2.hashCode());
323  50 final Method[] methods = getTestedClass().getDeclaredMethods();
324  562 for (int i = 0; i < methods.length; i++) {
325  512 if (methods[i].getName().startsWith("set")) {
326  115 final Method setter = methods[i];
327  115 if (setter.getParameterTypes().length > 1) {
328  0 continue;
329    }
330  115 final Class setClazz = setter.getParameterTypes()[0];
331  115 Object value1;
332  115 if (setClazz.isPrimitive()) {
333  1 if (setClazz == Integer.TYPE) {
334  1 value1 = new Integer(17);
335    } else {
336  0 throw new RuntimeException(setter.getName() + " not yet supported: " + setClazz);
337    }
338    } else {
339  114 value1 = getFilledObject(setClazz, getTestedClass(),
340    setter.getName());
341    }
342  115 setter.invoke(vo1, new Object[] {value1});
343  115 assertTrue(vo1.hashCode() != vo2.hashCode());
344  115 setter.invoke(vo2, new Object[] {value1});
345  115 assertTrue(vo1.hashCode() == vo2.hashCode());
346  115 final Object value2 = getFilledObject(setClazz, getTestedClass(),
347    setter.getName());
348  115 setter.invoke(vo2, new Object[] {value2});
349  115 assertTrue(vo1.hashCode() == vo2.hashCode());
350    }
351    }
352    }
353    {
354  50 final Object vo1 = getFilledObject(getTestedClass());
355  50 final Object vo2 = getFilledObject(getTestedClass());
356  50 assertEquals(vo1.hashCode(), vo2.hashCode());
357    }
358  50 removeMethodToCheck("hashCode");
359    }
360   
361    /**
362    * Test the <code>equals</code> method. This method is also tested with other check methods.
363    *
364    * @throws Exception Something went wrong. Perhaps the preconditions in {@link #testAll()}
365    * were violated.
366    */
 
367  50 toggle protected void checkEquals() throws Exception {
368  50 checkEqualsFillUp();
369  50 checkEqualsForEachSetter();
370    {
371  50 final Object vo1 = getFilledObject(getTestedClass());
372  50 final Object vo2 = getFilledObject(getTestedClass());
373  50 final Object vo3 = getEmptyObject(getTestedClass(), null, null);
374  50 assertTrue(vo1.equals(vo1));
375  50 assertTrue(vo1.equals(vo2));
376  50 assertTrue(vo2.equals(vo1));
377  50 assertFalse(vo1.equals(null));
378  50 assertFalse(vo1.equals(vo3));
379    }
380    {
381  50 Field[] attrs = getTestedClass().getDeclaredFields();
382  179 for (int i = 0; i < attrs.length; i++) {
383  129 if (attrs[i].getName().startsWith("__")) { // because of clover
384  0 continue;
385    }
386  129 if (attrs[i].getName().startsWith("$VRc")) { // because of emma
387  0 continue;
388    }
389  129 if (attrs[i].getName().startsWith("serialVersionUID")) { // because of emma, ignore those attributes
390  0 continue;
391    }
392  129 final Object vo1 = getFilledObject(attrs[i].getType(), getTestedClass(), "",
393    attrs[i].getName());
394  129 final Object vo2 = getFilledObject(attrs[i].getType(), getTestedClass(), "",
395    attrs[i].getName());
396  129 assertTrue(vo1.equals(vo1));
397  129 assertTrue(vo1.equals(vo2));
398  129 assertTrue(vo2.equals(vo1));
399    }
400    }
401  50 removeMethodToCheck("equals");
402    }
403   
404    /**
405    * Check equals after calling setter on empty object.
406    *
407    * @throws Exception
408    */
 
409  50 toggle protected void checkEqualsForEachSetter() throws Exception {
410    {
411  50 final Method[] methods = getTestedClass().getDeclaredMethods();
412  562 for (int i = 0; i < methods.length; i++) {
413  512 final Object vo1 = getObject(getTestedClass());
414  512 final Object vo2 = getObject(getTestedClass());
415  512 if (methods[i].getName().startsWith("set")
416    || methods[i].getName().startsWith("add")) {
417  136 final Method setter = methods[i];
418  136 if (setter.getParameterTypes().length > 1) {
419  0 continue;
420    }
421  136 final Class setClazz = setter.getParameterTypes()[0];
422  136 if (setClazz.isPrimitive()) {
423  1 if (setClazz == Integer.TYPE) {
424  1 setter.invoke(vo1, new Object[] {new Integer(0)});
425    } else {
426  0 throw new RuntimeException(setter.getName() + " not yet supported: " + setClazz);
427    }
428    } else {
429  135 setter.invoke(vo1, new Object[] {null});
430    }
431  136 if (methods[i].getName().startsWith("set")) {
432  115 assertTrue(vo1.equals(vo2));
433  115 assertTrue(vo2.equals(vo1));
434    } else {
435  21 assertFalse(vo1.equals(vo2));
436  21 assertFalse(vo2.equals(vo1));
437    }
438  136 final Object value1 = getFilledObject(setClazz, getTestedClass(),
439    setter.getName());
440  136 setter.invoke(vo2, new Object[] {value1});
441  136 assertFalse(vo1.equals(vo2));
442  136 assertFalse(vo2.equals(vo1));
443  136 assertTrue(vo1.hashCode() != vo2.hashCode());
444  136 assertFalse(vo1.toString().equals(vo2.toString()));
445    }
446    }
447    }
448    }
449   
450    /**
451    * Check equals during successive fill up by calling setters.
452    *
453    * @throws Exception
454    */
 
455  50 toggle protected void checkEqualsFillUp() throws Exception {
456    {
457  50 final Object vo1 = getObject(getTestedClass());
458  50 final Object vo2 = getObject(getTestedClass());
459  50 assertTrue(vo1.equals(vo1));
460  50 assertTrue(vo1.equals(vo2));
461  50 assertTrue(vo2.equals(vo1));
462  50 assertFalse(vo1.equals(null));
463   
464  50 final Method[] methods = getTestedClass().getDeclaredMethods();
465  562 for (int i = 0; i < methods.length; i++) {
466  512 if (methods[i].getName().startsWith("set")
467    || methods[i].getName().startsWith("add")) {
468  136 final Method setter = methods[i];
469  136 if (setter.getParameterTypes().length > 1) {
470  0 continue;
471    }
472  136 final Class setClazz = setter.getParameterTypes()[0];
473  136 final Object value1 = getFilledObject(setClazz, getTestedClass(),
474    setter.getName());
475  136 setter.invoke(vo1, new Object[] {value1});
476  136 assertFalse(vo1.equals(vo2));
477  136 assertFalse(vo2.equals(vo1));
478  136 assertFalse(vo1.equals(null));
479  136 setter.invoke(vo2, new Object[] {value1});
480  136 assertTrue(vo1.equals(vo2));
481  136 assertTrue(vo2.equals(vo1));
482  136 assertTrue(vo2.equals(vo1));
483  136 final Object value2 = getFilledObject(setClazz, getTestedClass(),
484    setter.getName());
485  136 setter.invoke(vo2, new Object[] {value2});
486  136 if (methods[i].getName().startsWith("set")) {
487  115 assertTrue(vo1.equals(vo2));
488  115 assertTrue(vo2.equals(vo1));
489    } else {
490  21 assertFalse(vo1.equals(vo2));
491  21 assertFalse(vo2.equals(vo1));
492  21 setter.invoke(vo1, new Object[] {value2});
493  21 assertTrue(vo1.equals(vo2));
494  21 assertTrue(vo2.equals(vo1));
495    }
496    }
497  512 assertTrue(vo1.hashCode() == vo2.hashCode());
498  512 assertTrue(vo1.toString().equals(vo2.toString()));
499    }
500    }
501    }
502   
503    /**
504    * Test if all methods of the value object were tested..
505    */
 
506  50 toggle protected void checkAllChecked() {
507  50 if (!getMethodsToCheck().isEmpty()) {
508  0 final String[] missing = (String[]) getMethodsToCheck().toArray(new String[0]);
509  0 final StringBuffer buffer
510    = new StringBuffer("tests for following methods are missing: ");
511  0 for (int i = 0; i < missing.length; i++) {
512  0 if (i > 0) {
513  0 buffer.append(", ");
514    }
515  0 buffer.append("\"" + missing[i] + "\"");
516    }
517  0 fail(buffer.toString());
518    }
519    }
520   
 
521  2008 toggle protected Set getMethodsToCheck() {
522  2008 return methodsToCheck;
523    }
524   
525    /**
526    * Creates an data filled instance of a class.
527    *
528    * @param clazz Create instance of this class.
529    * @return Object filled by its setters.
530    * @throws Exception Something went wrong. Perhaps the preconditions in {@link #testAll()} were
531    * violated.
532    */
 
533  300 toggle public final Object getFilledObject(final Class clazz) throws Exception {
534  300 return getFilledObject(clazz, null, "", "");
535    }
536   
537    /**
538    * Creates an data filled instance of a class.
539    *
540    * @param clazz Create instance of this class.
541    * @param parent This is the class that contains an element of <code>clazz</code>.
542    * Maybe <code>null</code>.
543    * @param attribute Attribute name of parent that shall be filled.
544    * @return Object filled by its setters.
545    * @throws Exception Something went wrong. Perhaps the preconditions in {@link #testAll()}
546    * were violated.
547    */
 
548  8802 toggle protected final Object getFilledObject(final Class clazz, final Class parent,
549    final String attribute) throws Exception {
550  8802 return getFilledObject(clazz, parent, attribute, "");
551    }
552   
553    /**
554    * Creates an data filled instance of a class.
555    *
556    * @param clazz Create instance of this class.
557    * @param parent This is the class that contains an element of <code>clazz</code>.
558    * Maybe <code>null</code>.
559    * @param attribute Attribute name of parent that shall be filled.
560    * @param ignore Fill every attribute but not this one. Could be empty, but shouldn't be
561    * <code>null</code>.
562    * @return Object filled by its setters.
563    * @throws Exception Something went wrong. Perhaps the preconditions in {@link #testAll()}
564    * were violated.
565    */
 
566  9360 toggle protected final Object getFilledObject(final Class clazz, final Class parent,
567    final String attribute, final String ignore) throws Exception {
568   
569  9360 Object vo;
570  9360 if (clazz.isPrimitive()) {
571  7 if (clazz == Integer.TYPE) {
572  7 vo = new Integer(17);
573    } else {
574  0 throw new RuntimeException(parent.getName() + "." + attribute + " not yet supported: " + clazz);
575    }
576    } else {
577  9353 vo = getObject(clazz, parent, attribute);
578    }
579  9360 final Class voClazz = vo.getClass();
580   
581  9360 if (clazz.equals(String.class) || clazz.equals(Integer.class) || clazz.equals(Boolean.class)
582    || clazz.equals(List.class)) {
583  4539 return vo;
584    }
585   
586  4821 Method[] methods = voClazz.getDeclaredMethods();
587    // boolean found = false;
588  42839 for (int i = 0; i < methods.length; i++) {
589    // System.out.println("\t" + methods[i].getName());
590  38018 if (methods[i].getName().startsWith("set") && !methods[i].equals("set"
591    + getUName(ignore))) {
592  6323 final Method setter = methods[i];
593  6323 if (setter.getParameterTypes().length > 1) {
594  0 fail("setter with more than one parameter found: " + setter.getName());
595    }
596  6323 final Class setClazz = setter.getParameterTypes()[0];
597  6323 Object value;
598  6323 if (setClazz.isPrimitive()) {
599  6 if (setClazz == Integer.TYPE) {
600  6 value = new Integer(13);
601    } else {
602  0 throw new RuntimeException(setter.getName() + " not yet supported: " + setClazz);
603    }
604    } else {
605  6317 value = getFilledObject(setClazz, clazz, setter.getName());
606    // System.out.println(clazz.getName() + "." + setter.getName());
607    // System.out.println(value.getClass()+ ":" + value);
608    }
609  6323 setter.invoke(vo, new Object[] {value});
610    // found = true;
611  31695 } else if (methods[i].getName().equals("add")) {
612  1698 final Method adder = methods[i];
613  1698 if (adder.getParameterTypes().length > 1) {
614  0 final StringBuffer buffer = new StringBuffer("in class \"" + clazz
615    + "\" method \"add\" with more than one parameter found: "
616    + adder.getName());
617  0 for (int j = 0; j < adder.getParameterTypes().length; j++) {
618  0 buffer.append(" " + adder.getParameterTypes()[j]);
619    }
620  0 fail(buffer.toString());
621    }
622  1698 final Class setClazz = adder.getParameterTypes()[0];
623  1698 if (setClazz != clazz) {
624  1698 final Object value = getFilledObject(setClazz, clazz, adder.getName());
625  1698 adder.invoke(vo, new Object[] {value});
626    // found = true;
627    }
628    }
629    }
630    /*
631    if (!found) {
632    fail("nothing to set in object of class " + clazz.getName());
633    }
634    */
635  4821 return vo;
636    }
637   
638    /**
639    * Get (if possible) empty instance of an class.
640    *
641    * @param clazz For this class an instance is wanted.
642    * @return Just the result of the default constructor (if existing).
643    * @throws Exception Something went wrong. Perhaps the preconditions in {@link #testAll()}
644    * were violated.
645    */
 
646  1453 toggle protected Object getObject(final Class clazz) throws Exception {
647  1453 return getObject(clazz, null, "");
648    }
649   
650    /**
651    * Get (if possible) empty instance of an class.
652    *
653    * @param clazz For this class an instance is wanted.
654    * @param parent This class has <code>clazz</code> as an attribute. Maybe <code>null</code>.
655    * @param attribute Attribute name of parent that shall be filled.
656    * @return Just the result of the default constructor (if existing).
657    * @throws Exception Something went wrong. Perhaps the preconditions in {@link #testAll()}
658    * were violated.
659    */
 
660  10806 toggle protected Object getObject(final Class clazz, final Class parent, final String attribute)
661    throws Exception {
662  10806 final Object vo = getEmptyObject(clazz, parent, attribute);
663  10806 if (vo == null) {
664  0 fail("no default constructor for " + clazz.getName() + " found");
665    }
666  10806 return vo;
667    }
668   
669    /**
670    * Gets an implementation for an abstract class.
671    *
672    * @param clazz Get implementation for this class.
673    * @return Concrete class, if any.
674    */
675    protected abstract Class abstractToConcreteClass(Class clazz);
676   
677    /**
678    * Get (if possible) empty instance of an class. This method could be overwritten to get more
679    * objects.
680    *
681    * @param clazz For this class an instance is wanted.
682    * @param parent This class has <code>clazz</code> as an attribute. Maybe <code>null</code>.
683    * @param attribute Attribute name of parent that shall be filled.
684    * @return Just the result of the default constructor (if existing). Might be
685    * <code>null</code>.
686    * @throws Exception Something went wrong. Perhaps the preconditions in {@link #testAll()}
687    * were violated.
688    */
 
689  10856 toggle protected Object getEmptyObject(Class clazz, final Class parent, final String attribute)
690    throws Exception {
691   
692  10856 if (abstractToConcreteClass(clazz) != null) {
693  788 clazz = abstractToConcreteClass(clazz);
694    }
695  10856 if (clazz.equals(Element.class)) { // application specific
696  487 return new DefaultAtom((parent != null ? parent.getName() + ":" : "") + attribute);
697  10369 } else if (clazz.equals(String.class)) {
698  4396 return (parent != null ? parent.getName() + ":" : "") + new String("StringAtom:"
699    + attribute);
700  5973 } else if (clazz.equals(Integer.class)) {
701  0 return new Integer(10);
702  5973 } else if (clazz.equals(Boolean.class)) {
703  115 return new Boolean("true");
704  5858 } else if (clazz.equals(List.class)) {
705  28 return new ArrayList();
706    }
707  5830 Constructor[] constructors = clazz.getConstructors();
708  5830 Constructor constructor = null;
709  14544 for (int j = 0; j < constructors.length; j++) {
710  8714 if (constructors[j].getParameterTypes().length == 0) {
711  5830 constructor = constructors[j];
712    }
713    }
714  5830 if (constructor == null) {
715  0 return null;
716    }
717  5830 try {
718  5830 return constructor.newInstance(new Object[0]);
719    } catch (final Exception e) {
720  0 System.out.println(constructor);
721  0 throw e;
722    }
723    }
724   
725    /**
726    * Get name with first letter upper case.
727    *
728    * @param name
729    * @return Name with first letter upper case.
730    */
 
731  11420 toggle public static final String getUName(final String name) {
732  11420 if (name.length() > 0) {
733  5163 return name.substring(0, 1).toUpperCase() + name.substring(1);
734    }
735  6257 return "";
736    }
737   
738    }