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