DGP - Fix to non-bulk densities
[debian/openrocket] / test / net / sf / openrocket / file / rocksim / BaseRocksimTest.java
1 /*
2  * BaseRocksimTest.java
3  */
4 package net.sf.openrocket.file.rocksim;
5
6 import junit.framework.TestCase;
7
8 import java.lang.reflect.Field;
9
10 import net.sf.openrocket.rocketcomponent.RocketComponent;
11
12 /**
13  * A base class for the Rocksim tests.  Includes code from the junitx.addons project.
14  */
15 public abstract class BaseRocksimTest extends TestCase {
16
17     /**
18      * Test constructor.
19      *
20      * @param name the name of the test to run.
21      */
22     public BaseRocksimTest(String name) {
23         super(name);
24     }
25     
26     public void assertContains (RocketComponent child, RocketComponent[] components) {
27         for (int i = 0; i < components.length; i++) {
28             if (components[i].equals(child)) {
29                 return;
30             }
31         }
32         fail("Component array did not contain child.");
33     }
34     
35     /**
36      * Returns the value of the field on the specified object.  The name
37      * parameter is a <code>String</code> specifying the simple name of the
38      * desired field.<p>
39      *
40      * The object is first searched for any matching field.  If no matching
41      * field is found, the superclasses are recursively searched.
42      *
43      * @exception NoSuchFieldException if a field with the specified name is
44      * not found.
45      */
46     public static Object getField(Object object,
47                                   String name)
48             throws NoSuchFieldException {
49         if (object == null) {
50             throw new IllegalArgumentException("Invalid null object argument");
51         }
52         for (Class cls = object.getClass();
53              cls != null;
54              cls = cls.getSuperclass()) {
55             try {
56                 Field field = cls.getDeclaredField(name);
57                 field.setAccessible(true);
58                 return field.get(object);
59             } catch (Exception ex) {
60                 /* in case of an exception, we will throw a new
61                  * NoSuchFieldException object */
62                 ;
63             }
64         }
65         throw new NoSuchFieldException("Could get value for field " +
66                 object.getClass().getName() + "." + name);
67     }
68
69     /**
70      * Returns the value of the field on the specified class.  The name
71      * parameter is a <code>String</code> specifying the simple name of the
72      * desired field.<p>
73      *
74      * The class is first searched for any matching field.  If no matching
75      * field is found, the superclasses are recursively searched.
76      *
77      * @exception NoSuchFieldException if a field with the specified name is
78      * not found.
79      */
80     public static Object getField(Class cls,
81                                   String name)
82             throws NoSuchFieldException {
83         if (cls == null) {
84             throw new IllegalArgumentException("Invalid null cls argument");
85         }
86         Class base = cls;
87         while (base != null) {
88             try {
89                 Field field = base.getDeclaredField(name);
90                 field.setAccessible(true);
91                 return field.get(base);
92             } catch (Exception ex) {
93                 /* in case of an exception, we will throw a new
94                  * NoSuchFieldException object */
95                 ;
96             }
97             base = base.getSuperclass();
98         }
99         throw new NoSuchFieldException("Could get value for static field " +
100                 cls.getName() + "." + name);
101     }
102
103
104 }