1 package jalview.viewmodel.styles;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
8 import java.lang.reflect.Field;
9 import java.util.Random;
11 import org.junit.Assert;
12 import org.junit.Test;
15 public class ViewStyleTest
18 Random r = new Random();
21 * This test uses reflection to set all fields on a ViewStyle, make a copy of
22 * it, and verify all fields match. This test should fail if a getter/setter
23 * pair are added to the class but missing in the copy constructor. Using
24 * reflection in the copy constructor itself is broken by obfuscation when the
27 * To prove this test works, simply comment out a line in the ViewStyle copy
28 * constructor, or add a new member field to ViewStyle.
30 * @throws IllegalAccessException
31 * @throws IllegalArgumentException
34 public void testCopyConstructor() throws IllegalArgumentException,
35 IllegalAccessException
37 ViewStyle vs1 = new ViewStyle();
38 Field[] fields = ViewStyle.class.getDeclaredFields();
39 for (Field field : fields)
41 field.setAccessible(true);
42 changeValue(vs1, field);
45 ViewStyle vs2 = new ViewStyle(vs1);
47 for (Field field1 : fields) {
48 final Object value1 = field1.get(vs1);
49 final Object value2 = field1.get(vs2);
50 String msg = "Mismatch in " + field1.getName() + "(" + value1 + "/"
51 + value2 + ") - not set in copy constructor?";
52 assertEquals(msg, value1, value2);
54 assertEquals("Hashcode not equals", vs1.hashCode(), vs2.hashCode());
58 * Change the value of one field in a ViewStyle object
62 * @throws IllegalAccessException
64 protected void changeValue(ViewStyle vs, Field field)
65 throws IllegalAccessException
67 Class<?> type = field.getType();
69 if (type.equals(boolean.class) || type.equals(Boolean.class))
71 boolean value = (Boolean) field.get(vs);
72 // System.out.println("Setting " + field.getName() + " to " + !value);
73 field.set(vs, !value);
75 else if (type.equals(short.class) || type.equals(int.class)
76 || type.equals(long.class) || type.equals(float.class)
77 || type.equals(double.class))
79 final int value = (int) (1 + field.getDouble(vs));
80 // System.out.println("Setting " + field.getName() + " to " + value);
83 else if (type.equals(Integer.class))
85 field.set(vs, (int) (1 + getNumberValue(field, vs)));
87 else if (type.equals(Float.class))
89 field.set(vs, (float) (1f + getNumberValue(field, vs)));
91 else if (type.equals(Long.class))
93 field.set(vs, (long) (1L + getNumberValue(field, vs)));
95 else if (type.equals(Double.class))
97 field.set(vs, 1d + getNumberValue(field, vs));
99 else if (type.equals(Short.class))
101 field.set(vs, (short) (1 + getNumberValue(field, vs)));
103 else if (type.equals(Byte.class))
105 field.set(vs, (byte) (1 + getNumberValue(field, vs)));
107 else if (type.equals(Character.class))
109 field.set(vs, (char) (1 + getNumberValue(field, vs)));
111 else if (type.equals(String.class))
113 field.set(vs, "Joe" + field.get(vs));
115 else if (type.equals(Color.class))
117 field.set(vs, Color.RED.equals(field.get(vs)) ? Color.BLACK
122 Assert.fail("Unhandled field type (add to test): " + field.getName()
127 private double getNumberValue(Field field, ViewStyle vs)
128 throws IllegalArgumentException, IllegalAccessException
130 if (field.get(vs) == null)
134 return ((Number) field.get(vs)).doubleValue();
138 * Test that the equals method compares every field by changing them one by
139 * one in a cloned ViewStyle.
141 * This test will fail if a new field is added to ViewStyle but not to the
142 * comparisons in ViewStyle.equals().
144 * To confirm that this test works, temporarily comment out one of the field
145 * comparisons in ViewStyle.equals()
147 * @throws IllegalAccessException
148 * @throws IllegalArgumentException
151 public void testEquals() throws IllegalArgumentException,
152 IllegalAccessException
154 ViewStyle vs1 = new ViewStyle();
155 ViewStyle vs2 = new ViewStyle(vs1);
157 assertFalse(vs1.equals(null));
158 assertFalse(vs1.equals(this));
159 assertTrue(vs1.equals(vs2));
160 assertTrue(vs2.equals(vs1));
162 Field[] fields = ViewStyle.class.getDeclaredFields();
163 for (Field field : fields)
165 field.setAccessible(true);
166 Object oldValue = field.get(vs2);
167 changeValue(vs2, field);
168 assertFalse("equals method ignores " + field.getName(),
171 if (vs1.hashCode() == vs2.hashCode())
173 // uncomment next line to see which fields hashCode ignores
174 // System.out.println("hashCode ignores " + field.getName());
176 // restore original value before testing the next field
177 field.set(vs2, oldValue);