4 package net.sf.openrocket.file.rocksim;
6 import junit.framework.TestCase;
8 import java.lang.reflect.Field;
10 import net.sf.openrocket.rocketcomponent.RocketComponent;
13 * A base class for the Rocksim tests. Includes code from the junitx.addons project.
15 public abstract class RocksimTestBase extends TestCase {
20 * @param name the name of the test to run.
22 public RocksimTestBase(String name) {
26 public void assertContains (RocketComponent child, RocketComponent[] components) {
27 for (int i = 0; i < components.length; i++) {
28 if (components[i].equals(child)) {
32 fail("Component array did not contain child.");
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
40 * The object is first searched for any matching field. If no matching
41 * field is found, the superclasses are recursively searched.
43 * @exception NoSuchFieldException if a field with the specified name is
46 public static Object getField(Object object,
48 throws NoSuchFieldException {
50 throw new IllegalArgumentException("Invalid null object argument");
52 for (Class cls = object.getClass();
54 cls = cls.getSuperclass()) {
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 */
65 throw new NoSuchFieldException("Could get value for field " +
66 object.getClass().getName() + "." + name);
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
74 * The class is first searched for any matching field. If no matching
75 * field is found, the superclasses are recursively searched.
77 * @exception NoSuchFieldException if a field with the specified name is
80 public static Object getField(Class cls,
82 throws NoSuchFieldException {
84 throw new IllegalArgumentException("Invalid null cls argument");
87 while (base != null) {
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 */
97 base = base.getSuperclass();
99 throw new NoSuchFieldException("Could get value for static field " +
100 cls.getName() + "." + name);