JAL-1889 ignore Clover instrumentation in test (second attempt)
[jalview.git] / test / jalview / viewmodel / styles / ViewStyleTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.viewmodel.styles;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import jalview.gui.JvOptionPane;
28
29 import java.awt.Color;
30 import java.lang.reflect.Field;
31 import java.util.Random;
32
33 import org.testng.AssertJUnit;
34 import org.testng.annotations.BeforeClass;
35 import org.testng.annotations.Test;
36
37 public class ViewStyleTest
38 {
39
40   @BeforeClass(alwaysRun = true)
41   public void setUpJvOptionPane()
42   {
43     JvOptionPane.setInteractiveMode(false);
44     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
45   }
46
47   Random r = new Random();
48
49   /**
50    * This test uses reflection to set all fields on a ViewStyle, make a copy of
51    * it, and verify all fields match. This test should fail if a getter/setter
52    * pair are added to the class but missing in the copy constructor. Using
53    * reflection in the copy constructor itself is broken by obfuscation when the
54    * applet is built.
55    * 
56    * To prove this test works, simply comment out a line in the ViewStyle copy
57    * constructor, or add a new member field to ViewStyle.
58    * 
59    * @throws IllegalAccessException
60    * @throws IllegalArgumentException
61    */
62   @Test(groups = { "Functional" })
63   public void testCopyConstructor() throws IllegalArgumentException,
64           IllegalAccessException
65   {
66     ViewStyle vs1 = new ViewStyle();
67     Field[] fields = ViewStyle.class.getDeclaredFields();
68     for (Field field : fields)
69     {
70       field.setAccessible(true);
71       if (!copyConstructorIgnores(field))
72       {
73         changeValue(vs1, field);
74       }
75     }
76
77     ViewStyle vs2 = new ViewStyle(vs1);
78
79     for (Field field1 : fields)
80     {
81       if (!copyConstructorIgnores(field1))
82       {
83         final Object value1 = field1.get(vs1);
84         final Object value2 = field1.get(vs2);
85         String msg = "Mismatch in " + field1.getName() + "(" + value1 + "/"
86               + value2 + ") - not set in copy constructor?";
87         assertEquals(msg, value1, value2);
88       }
89     }
90     assertEquals("Hashcode not equals", vs1.hashCode(), vs2.hashCode());
91   }
92
93   /**
94    * Add tests here for any fields that we expect to be ignored by 
95    * the copy constructor
96    * 
97    * @param field
98    * @return
99    */
100   private boolean copyConstructorIgnores(Field field)
101   {
102     /*
103      * just instrumentation added by test coverage while testing
104      */
105     String type = field.getClass().toString();
106     if (type.toString().contains("com_atlassian_clover"))
107     {
108       // instrumentation added for test coverage - ignore
109       return true;
110     }
111     return false;
112   }
113
114   /**
115    * Change the value of one field in a ViewStyle object
116    * 
117    * @param vs
118    * @param field
119    * @throws IllegalAccessException
120    */
121   protected void changeValue(ViewStyle vs, Field field)
122           throws IllegalAccessException
123   {
124     Class<?> type = field.getType();
125
126     if (type.equals(boolean.class) || type.equals(Boolean.class))
127     {
128       boolean value = (Boolean) field.get(vs);
129       // System.out.println("Setting " + field.getName() + " to " + !value);
130       field.set(vs, !value);
131     }
132     else if (type.equals(short.class) || type.equals(int.class)
133             || type.equals(long.class) || type.equals(float.class)
134             || type.equals(double.class))
135     {
136       final int value = (int) (1 + field.getDouble(vs));
137       // System.out.println("Setting " + field.getName() + " to " + value);
138       field.set(vs, value);
139     }
140     else if (type.equals(Integer.class))
141     {
142       field.set(vs, (int) (1 + getNumberValue(field, vs)));
143     }
144     else if (type.equals(Float.class))
145     {
146       field.set(vs, (float) (1f + getNumberValue(field, vs)));
147     }
148     else if (type.equals(Long.class))
149     {
150       field.set(vs, (long) (1L + getNumberValue(field, vs)));
151     }
152     else if (type.equals(Double.class))
153     {
154       field.set(vs, 1d + getNumberValue(field, vs));
155     }
156     else if (type.equals(Short.class))
157     {
158       field.set(vs, (short) (1 + getNumberValue(field, vs)));
159     }
160     else if (type.equals(Byte.class))
161     {
162       field.set(vs, (byte) (1 + getNumberValue(field, vs)));
163     }
164     else if (type.equals(Character.class))
165     {
166       field.set(vs, (char) (1 + getNumberValue(field, vs)));
167     }
168     else if (type.equals(String.class))
169     {
170       field.set(vs, "Joe" + field.get(vs));
171     }
172     else if (type.equals(Color.class))
173     {
174       field.set(vs, Color.RED.equals(field.get(vs)) ? Color.BLACK
175               : Color.RED);
176     }
177     else
178     {
179       AssertJUnit.fail("Unhandled field type (add to test): "
180               + field.getName() + ":" + type);
181     }
182   }
183
184   private double getNumberValue(Field field, ViewStyle vs)
185           throws IllegalArgumentException, IllegalAccessException
186   {
187     if (field.get(vs) == null)
188     {
189       return 0d;
190     }
191     return ((Number) field.get(vs)).doubleValue();
192   }
193
194   /**
195    * Test that the equals method compares every field by changing them one by
196    * one in a cloned ViewStyle.
197    * 
198    * This test will fail if a new field is added to ViewStyle but not to the
199    * comparisons in ViewStyle.equals().
200    * 
201    * To confirm that this test works, temporarily comment out one of the field
202    * comparisons in ViewStyle.equals()
203    * 
204    * @throws IllegalAccessException
205    * @throws IllegalArgumentException
206    */
207   @Test(groups = { "Functional" })
208   public void testEquals() throws IllegalArgumentException,
209           IllegalAccessException
210   {
211     ViewStyle vs1 = new ViewStyle();
212     ViewStyle vs2 = new ViewStyle(vs1);
213
214     assertFalse(vs1.equals(null));
215     assertFalse(vs1.equals(this));
216     assertTrue(vs1.equals(vs2));
217     assertTrue(vs2.equals(vs1));
218
219     Field[] fields = ViewStyle.class.getDeclaredFields();
220     for (Field field : fields)
221     {
222       if (!copyConstructorIgnores(field))
223       {
224         field.setAccessible(true);
225         Object oldValue = field.get(vs2);
226         changeValue(vs2, field);
227         assertFalse("equals method ignores " + field.getName(),
228               vs1.equals(vs2));
229
230         if (vs1.hashCode() == vs2.hashCode())
231         {
232           // uncomment next line to see which fields hashCode ignores
233           // System.out.println("hashCode ignores " + field.getName());
234         }
235         // restore original value before testing the next field
236         field.set(vs2, oldValue);
237       }
238     }
239   }
240 }