JAL-1684 removed reflection from ViewStyle (to Junit), and removed
[jalview.git] / test / jalview / viewmodel / styles / ViewStyleTest.java
1 package jalview.viewmodel.styles;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
6
7 import java.awt.Color;
8 import java.lang.reflect.Field;
9 import java.util.Random;
10
11 import org.junit.Assert;
12 import org.junit.Test;
13
14
15 public class ViewStyleTest
16 {
17
18   Random r = new Random();
19
20   /**
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
25    * applet is built.
26    * 
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.
29    * 
30    * @throws IllegalAccessException
31    * @throws IllegalArgumentException
32    */
33   @Test
34   public void testCopyConstructor() throws IllegalArgumentException,
35           IllegalAccessException
36   {
37     ViewStyle vs1 = new ViewStyle();
38     Field[] fields = ViewStyle.class.getDeclaredFields();
39     for (Field field : fields)
40     {
41       field.setAccessible(true);
42       changeValue(vs1, field);
43     }
44
45     ViewStyle vs2 = new ViewStyle(vs1);
46
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);
53     }
54   }
55
56   /**
57    * Change the value of one field in a ViewStyle object
58    * 
59    * @param vs
60    * @param field
61    * @throws IllegalAccessException
62    */
63   protected void changeValue(ViewStyle vs, Field field)
64           throws IllegalAccessException
65   {
66     Class<?> type = field.getType();
67     final int numValue = 1 + r.nextInt(100);
68
69     if (type.equals(boolean.class) || type.equals(Boolean.class))
70     {
71       boolean value = (Boolean) field.get(vs);
72       // System.out.println("Setting " + field.getName() + " to " + !value);
73       field.set(vs, !value);
74     }
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))
78     {
79       final int value = (int) (1 + field.getDouble(vs));
80       // System.out.println("Setting " + field.getName() + " to " + value);
81       field.set(vs, value);
82     }
83     else if (type.equals(Integer.class))
84     {
85       field.set(vs, (int) (1 + getNumberValue(field, vs)));
86     }
87     else if (type.equals(Float.class))
88     {
89       field.set(vs, (float) (1f + getNumberValue(field, vs)));
90     }
91     else if (type.equals(Long.class))
92     {
93       field.set(vs, (long) (1L + getNumberValue(field, vs)));
94     }
95     else if (type.equals(Double.class))
96     {
97       field.set(vs, 1d + getNumberValue(field, vs));
98     }
99     else if (type.equals(Short.class))
100     {
101       field.set(vs, (short) (1 + getNumberValue(field, vs)));
102     }
103     else if (type.equals(Byte.class))
104     {
105       field.set(vs, (byte) (1 + getNumberValue(field, vs)));
106     }
107     else if (type.equals(Character.class))
108     {
109       field.set(vs, (char) (1 + getNumberValue(field, vs)));
110     }
111     else if (type.equals(String.class))
112     {
113       field.set(vs, "Joe" + field.get(vs));
114     }
115     else if (type.equals(Color.class))
116     {
117       field.set(vs, Color.RED.equals(field.get(vs)) ? Color.BLACK
118               : Color.RED);
119     }
120     else
121     {
122       Assert.fail("Unhandled field type (add to test): " + field.getName()
123               + ":" + type);
124     }
125   }
126
127   private double getNumberValue(Field field, ViewStyle vs)
128           throws IllegalArgumentException, IllegalAccessException
129   {
130     if (field.get(vs) == null)
131     {
132       return 0d;
133     }
134     return ((Number) field.get(vs)).doubleValue();
135   }
136
137   /**
138    * Test that the equals method compares every field by changing them one by
139    * one in a cloned ViewStyle.
140    * 
141    * This test will fail if a new field is added to ViewStyle but not to the
142    * comparisons in ViewStyle.equals().
143    * 
144    * To confirm that this test works, temporarily comment out one of the field
145    * comparisons in ViewStyle.equals()
146    * 
147    * @throws IllegalAccessException
148    * @throws IllegalArgumentException
149    */
150   @Test
151   public void testEquals() throws IllegalArgumentException,
152           IllegalAccessException
153   {
154     ViewStyle vs1 = new ViewStyle();
155     ViewStyle vs2 = new ViewStyle(vs1);
156
157     assertFalse(vs1.equals(null));
158     assertFalse(vs1.equals(this));
159     assertTrue(vs1.equals(vs2));
160     assertTrue(vs2.equals(vs1));
161
162     Field[] fields = ViewStyle.class.getDeclaredFields();
163     for (Field field : fields)
164     {
165       field.setAccessible(true);
166       Object oldValue = field.get(vs2);
167       changeValue(vs2, field);
168       assertFalse("equals method ignores " + field.getName(),
169               vs1.equals(vs2));
170       // restore original value before testing the next field
171       field.set(vs2, oldValue);
172     }
173   }
174 }