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