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;
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, List<RocketComponent> components) {
27 assertTrue("Components did not contain child", components.contains(child));
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
35 * The object is first searched for any matching field. If no matching
36 * field is found, the superclasses are recursively searched.
38 * @exception NoSuchFieldException if a field with the specified name is
41 public static Object getField(Object object,
43 throws NoSuchFieldException {
45 throw new IllegalArgumentException("Invalid null object argument");
47 for (Class cls = object.getClass(); cls != null; cls = cls.getSuperclass()) {
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 */
58 throw new NoSuchFieldException("Could get value for field " +
59 object.getClass().getName() + "." + name);
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
67 * The class is first searched for any matching field. If no matching
68 * field is found, the superclasses are recursively searched.
70 * @exception NoSuchFieldException if a field with the specified name is
73 public static Object getField(Class cls,
75 throws NoSuchFieldException {
77 throw new IllegalArgumentException("Invalid null cls argument");
80 while (base != null) {
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 */
90 base = base.getSuperclass();
92 throw new NoSuchFieldException("Could get value for static field " +
93 cls.getName() + "." + name);