1 package jalview.viewmodel.styles;
3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertFalse;
5 import static org.testng.AssertJUnit.assertTrue;
6 import org.testng.annotations.Test;
7 import org.testng.AssertJUnit;
9 import java.lang.reflect.Field;
10 import java.util.Random;
13 public class ViewStyleTest
16 Random r = new Random();
19 * This test uses reflection to set all fields on a ViewStyle, make a copy of
20 * it, and verify all fields match. This test should fail if a getter/setter
21 * pair are added to the class but missing in the copy constructor. Using
22 * reflection in the copy constructor itself is broken by obfuscation when the
25 * To prove this test works, simply comment out a line in the ViewStyle copy
26 * constructor, or add a new member field to ViewStyle.
28 * @throws IllegalAccessException
29 * @throws IllegalArgumentException
32 public void testCopyConstructor() throws IllegalArgumentException,
33 IllegalAccessException
35 ViewStyle vs1 = new ViewStyle();
36 Field[] fields = ViewStyle.class.getDeclaredFields();
37 for (Field field : fields)
39 field.setAccessible(true);
40 if (!copyConstructorIgnores(field.getName()))
42 changeValue(vs1, field);
46 ViewStyle vs2 = new ViewStyle(vs1);
48 for (Field field1 : fields) {
49 final Object value1 = field1.get(vs1);
50 final Object value2 = field1.get(vs2);
51 String msg = "Mismatch in " + field1.getName() + "(" + value1 + "/"
52 + value2 + ") - not set in copy constructor?";
53 assertEquals(msg, value1, value2);
55 assertEquals("Hashcode not equals", vs1.hashCode(), vs2.hashCode());
59 * Add any field names in here that we expect to be ignored by the copy
65 private boolean copyConstructorIgnores(String name)
74 * Change the value of one field in a ViewStyle object
78 * @throws IllegalAccessException
80 protected void changeValue(ViewStyle vs, Field field)
81 throws IllegalAccessException
83 Class<?> type = field.getType();
85 if (type.equals(boolean.class) || type.equals(Boolean.class))
87 boolean value = (Boolean) field.get(vs);
88 // System.out.println("Setting " + field.getName() + " to " + !value);
89 field.set(vs, !value);
91 else if (type.equals(short.class) || type.equals(int.class)
92 || type.equals(long.class) || type.equals(float.class)
93 || type.equals(double.class))
95 final int value = (int) (1 + field.getDouble(vs));
96 // System.out.println("Setting " + field.getName() + " to " + value);
99 else if (type.equals(Integer.class))
101 field.set(vs, (int) (1 + getNumberValue(field, vs)));
103 else if (type.equals(Float.class))
105 field.set(vs, (float) (1f + getNumberValue(field, vs)));
107 else if (type.equals(Long.class))
109 field.set(vs, (long) (1L + getNumberValue(field, vs)));
111 else if (type.equals(Double.class))
113 field.set(vs, 1d + getNumberValue(field, vs));
115 else if (type.equals(Short.class))
117 field.set(vs, (short) (1 + getNumberValue(field, vs)));
119 else if (type.equals(Byte.class))
121 field.set(vs, (byte) (1 + getNumberValue(field, vs)));
123 else if (type.equals(Character.class))
125 field.set(vs, (char) (1 + getNumberValue(field, vs)));
127 else if (type.equals(String.class))
129 field.set(vs, "Joe" + field.get(vs));
131 else if (type.equals(Color.class))
133 field.set(vs, Color.RED.equals(field.get(vs)) ? Color.BLACK
138 AssertJUnit.fail("Unhandled field type (add to test): " + field.getName()
143 private double getNumberValue(Field field, ViewStyle vs)
144 throws IllegalArgumentException, IllegalAccessException
146 if (field.get(vs) == null)
150 return ((Number) field.get(vs)).doubleValue();
154 * Test that the equals method compares every field by changing them one by
155 * one in a cloned ViewStyle.
157 * This test will fail if a new field is added to ViewStyle but not to the
158 * comparisons in ViewStyle.equals().
160 * To confirm that this test works, temporarily comment out one of the field
161 * comparisons in ViewStyle.equals()
163 * @throws IllegalAccessException
164 * @throws IllegalArgumentException
167 public void testEquals() throws IllegalArgumentException,
168 IllegalAccessException
170 ViewStyle vs1 = new ViewStyle();
171 ViewStyle vs2 = new ViewStyle(vs1);
173 assertFalse(vs1.equals(null));
174 assertFalse(vs1.equals(this));
175 assertTrue(vs1.equals(vs2));
176 assertTrue(vs2.equals(vs1));
178 Field[] fields = ViewStyle.class.getDeclaredFields();
179 for (Field field : fields)
181 field.setAccessible(true);
182 Object oldValue = field.get(vs2);
183 changeValue(vs2, field);
184 assertFalse("equals method ignores " + field.getName(),
187 if (vs1.hashCode() == vs2.hashCode())
189 // uncomment next line to see which fields hashCode ignores
190 // System.out.println("hashCode ignores " + field.getName());
192 // restore original value before testing the next field
193 field.set(vs2, oldValue);