4 package net.sf.openrocket.file.rocksim;
6 import java.lang.reflect.Field;
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;
15 * A base class for the Rocksim tests. Includes code from the junitx.addons project.
17 public abstract class RocksimTestBase extends TestCase {
22 * @param name the name of the test to run.
24 public RocksimTestBase(String name) {
30 * @see junit.framework.TestCase#setUp()
33 protected void setUp() throws Exception {
35 Application.setPreferences( new Prefs() );
39 public void assertContains(RocketComponent child, List<RocketComponent> components) {
40 assertTrue("Components did not contain child", components.contains(child));
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
48 * The object is first searched for any matching field. If no matching
49 * field is found, the superclasses are recursively searched.
51 * @exception NoSuchFieldException if a field with the specified name is
54 public static Object getField(Object object,
56 throws NoSuchFieldException {
58 throw new IllegalArgumentException("Invalid null object argument");
60 for (Class cls = object.getClass(); cls != null; cls = cls.getSuperclass()) {
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 */
71 throw new NoSuchFieldException("Could get value for field " +
72 object.getClass().getName() + "." + name);
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
80 * The class is first searched for any matching field. If no matching
81 * field is found, the superclasses are recursively searched.
83 * @exception NoSuchFieldException if a field with the specified name is
86 public static Object getField(Class cls,
88 throws NoSuchFieldException {
90 throw new IllegalArgumentException("Invalid null cls argument");
93 while (base != null) {
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 */
103 base = base.getSuperclass();
105 throw new NoSuchFieldException("Could get value for static field " +
106 cls.getName() + "." + name);