Fairly substantial refactoring of preference system. Created abstract class net...
[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 import net.sf.openrocket.startup.Application;
12 import net.sf.openrocket.util.Prefs;
13
14 /**
15  * A base class for the Rocksim tests.  Includes code from the junitx.addons project.
16  */
17 public abstract class RocksimTestBase extends TestCase {
18         
19         /**
20          * Test constructor.
21          *
22          * @param name the name of the test to run.
23          */
24         public RocksimTestBase(String name) {
25                 super(name);
26         }
27         
28         
29         /* (non-Javadoc)
30          * @see junit.framework.TestCase#setUp()
31          */
32         @Override
33         protected void setUp() throws Exception {
34                 super.setUp();
35                 Application.setPreferences( new Prefs() );
36         }
37
38
39         public void assertContains(RocketComponent child, List<RocketComponent> components) {
40                 assertTrue("Components did not contain child", components.contains(child));
41         }
42         
43         /**
44          * Returns the value of the field on the specified object.  The name
45          * parameter is a <code>String</code> specifying the simple name of the
46          * desired field.<p>
47          *
48          * The object is first searched for any matching field.  If no matching
49          * field is found, the superclasses are recursively searched.
50          *
51          * @exception NoSuchFieldException if a field with the specified name is
52          * not found.
53          */
54         public static Object getField(Object object,
55                                                                         String name)
56                         throws NoSuchFieldException {
57                 if (object == null) {
58                         throw new IllegalArgumentException("Invalid null object argument");
59                 }
60                 for (Class cls = object.getClass(); cls != null; cls = cls.getSuperclass()) {
61                         try {
62                                 Field field = cls.getDeclaredField(name);
63                                 field.setAccessible(true);
64                                 return field.get(object);
65                         } catch (Exception ex) {
66                                 /* in case of an exception, we will throw a new
67                                  * NoSuchFieldException object */
68                                 ;
69                         }
70                 }
71                 throw new NoSuchFieldException("Could get value for field " +
72                                 object.getClass().getName() + "." + name);
73         }
74         
75         /**
76          * Returns the value of the field on the specified class.  The name
77          * parameter is a <code>String</code> specifying the simple name of the
78          * desired field.<p>
79          *
80          * The class is first searched for any matching field.  If no matching
81          * field is found, the superclasses are recursively searched.
82          *
83          * @exception NoSuchFieldException if a field with the specified name is
84          * not found.
85          */
86         public static Object getField(Class cls,
87                                                                         String name)
88                         throws NoSuchFieldException {
89                 if (cls == null) {
90                         throw new IllegalArgumentException("Invalid null cls argument");
91                 }
92                 Class base = cls;
93                 while (base != null) {
94                         try {
95                                 Field field = base.getDeclaredField(name);
96                                 field.setAccessible(true);
97                                 return field.get(base);
98                         } catch (Exception ex) {
99                                 /* in case of an exception, we will throw a new
100                                  * NoSuchFieldException object */
101                                 ;
102                         }
103                         base = base.getSuperclass();
104                 }
105                 throw new NoSuchFieldException("Could get value for static field " +
106                                 cls.getName() + "." + name);
107         }
108         
109
110 }