4 package net.sf.openrocket.file.rocksim.importt;
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;
12 import java.lang.reflect.Field;
13 import java.util.List;
16 * A base class for the Rocksim tests. Includes code from the junitx.addons project.
18 public abstract class RocksimTestBase {
21 * @see junit.framework.TestCase#setUp()
24 public void setUp() throws Exception {
25 Application.setPreferences( new SwingPreferences() );
29 public void assertContains(RocketComponent child, List<RocketComponent> components) {
30 Assert.assertTrue("Components did not contain child", components.contains(child));
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
38 * The object is first searched for any matching field. If no matching
39 * field is found, the superclasses are recursively searched.
41 * @exception NoSuchFieldException if a field with the specified name is
44 public static Object getField(Object object,
46 throws NoSuchFieldException {
48 throw new IllegalArgumentException("Invalid null object argument");
50 for (Class cls = object.getClass(); cls != null; cls = cls.getSuperclass()) {
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 */
61 throw new NoSuchFieldException("Could get value for field " +
62 object.getClass().getName() + "." + name);
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
70 * The class is first searched for any matching field. If no matching
71 * field is found, the superclasses are recursively searched.
73 * @exception NoSuchFieldException if a field with the specified name is
76 public static Object getField(Class cls,
78 throws NoSuchFieldException {
80 throw new IllegalArgumentException("Invalid null cls argument");
83 while (base != null) {
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 */
93 base = base.getSuperclass();
95 throw new NoSuchFieldException("Could get value for static field " +
96 cls.getName() + "." + name);