Clover icon

Coverage Report

  1. Project Clover database Mon Nov 11 2024 20:42:03 GMT
  2. Package jalview.viewmodel.styles

File ViewStyleTest.java

 

Code metrics

40
70
6
1
253
166
31
0.44
11.67
6
5.17

Classes

Class Line # Actions
ViewStyleTest 40 70 31
0.784482878.4%
 

Contributing tests

This file is covered by 2 tests. .

Source view

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.awt.Container;
31    import java.lang.reflect.Field;
32    import java.util.ArrayList;
33    import java.util.List;
34    import java.util.Random;
35   
36    import org.testng.AssertJUnit;
37    import org.testng.annotations.BeforeClass;
38    import org.testng.annotations.Test;
39   
 
40    public class ViewStyleTest
41    {
42   
 
43  1 toggle @BeforeClass(alwaysRun = true)
44    public void setUpJvOptionPane()
45    {
46  1 JvOptionPane.setInteractiveMode(false);
47  1 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
48    }
49   
50    Random r = new Random();
51   
52    /**
53    * This test uses reflection to set all fields on a ViewStyle, make a copy of
54    * it, and verify all fields match. This test should fail if a getter/setter
55    * pair are added to the class but missing in the copy constructor. Using
56    * reflection in the copy constructor itself is broken by obfuscation when the
57    * applet is built.
58    *
59    * To prove this test works, simply comment out a line in the ViewStyle copy
60    * constructor, or add a new member field to ViewStyle.
61    *
62    * @throws IllegalAccessException
63    * @throws IllegalArgumentException
64    */
 
65  1 toggle @Test(groups = { "Functional" })
66    public void testCopyConstructor()
67    throws IllegalArgumentException, IllegalAccessException
68    {
69  1 ViewStyle vs1 = new ViewStyle();
70  1 Field[] fields = ViewStyle.class.getDeclaredFields();
71  1 for (Field field : fields)
72    {
73  46 field.setAccessible(true);
74  46 if (!copyConstructorIgnores(field))
75    {
76  45 changeValue(vs1, field);
77    }
78    }
79   
80  1 ViewStyle vs2 = new ViewStyle(vs1);
81   
82  1 for (Field field1 : fields)
83    {
84  46 if (!copyConstructorIgnores(field1))
85    {
86  45 final Object value1 = field1.get(vs1);
87  45 final Object value2 = field1.get(vs2);
88  45 String msg = "Mismatch in " + field1.getName() + "(" + value1 + "/"
89    + value2 + ") - not set in copy constructor?";
90  45 assertEquals(msg, value1, value2);
91    }
92    }
93  1 assertEquals("Hashcode not equals", vs1.hashCode(), vs2.hashCode());
94    }
95   
96    /**
97    * Add tests here for any fields that we expect to be ignored by the copy
98    * constructor
99    *
100    * @param field
101    * @return
102    */
 
103  138 toggle private boolean copyConstructorIgnores(Field field)
104    {
105    /*
106    * ignore instrumentation added by jacoco for test coverage
107    */
108  138 if (field.isSynthetic())
109    {
110  0 return true;
111    }
112  138 if (field.getType().toString().contains("com_atlassian_clover"))
113    {
114  3 return true;
115    }
116   
117  135 return false;
118    }
119   
120    /**
121    * Change the value of one field in a ViewStyle object
122    *
123    * @param vs
124    * @param field
125    * @throws IllegalAccessException
126    */
 
127  90 toggle protected void changeValue(ViewStyle vs, Field field)
128    throws IllegalAccessException
129    {
130  90 Class<?> type = field.getType();
131   
132  90 if (type.equals(boolean.class) || type.equals(Boolean.class))
133    {
134  62 boolean value = (Boolean) field.get(vs);
135    // System.out.println("Setting " + field.getName() + " to " + !value);
136  62 field.set(vs, !value);
137    }
138  28 else if (type.equals(short.class) || type.equals(int.class)
139    || type.equals(long.class) || type.equals(float.class)
140    || type.equals(double.class))
141    {
142  20 final int value = (int) (1 + field.getDouble(vs));
143    // System.out.println("Setting " + field.getName() + " to " + value);
144  20 field.set(vs, value);
145    }
146  8 else if (type.equals(Integer.class))
147    {
148  0 field.set(vs, (int) (1 + getNumberValue(field, vs)));
149    }
150  8 else if (type.equals(Float.class))
151    {
152  0 field.set(vs, (float) (1f + getNumberValue(field, vs)));
153    }
154  8 else if (type.equals(Long.class))
155    {
156  0 field.set(vs, (long) (1L + getNumberValue(field, vs)));
157    }
158  8 else if (type.equals(Double.class))
159    {
160  0 field.set(vs, 1d + getNumberValue(field, vs));
161    }
162  8 else if (type.equals(Short.class))
163    {
164  0 field.set(vs, (short) (1 + getNumberValue(field, vs)));
165    }
166  8 else if (type.equals(Byte.class))
167    {
168  0 field.set(vs, (byte) (1 + getNumberValue(field, vs)));
169    }
170  8 else if (type.equals(Character.class))
171    {
172  0 field.set(vs, (char) (1 + getNumberValue(field, vs)));
173    }
174  8 else if (type.equals(String.class))
175    {
176  2 field.set(vs, "Joe" + field.get(vs));
177    }
178  6 else if (type.equals(Color.class))
179    {
180  4 field.set(vs,
181  4 Color.RED.equals(field.get(vs)) ? Color.BLACK : Color.RED);
182    }
183  2 else if (type.equals(java.util.List.class))
184    {
185  2 List<?> list = (List<?>) field.get(vs);
186  2 List<Object> mutableList = new ArrayList<>(list);
187  2 mutableList.add("All");
188  2 field.set(vs, mutableList);
189    }
190    else
191    {
192  0 AssertJUnit.fail("Unhandled field type (add to test): "
193    + field.getName() + ":" + type);
194    }
195    }
196   
 
197  0 toggle private double getNumberValue(Field field, ViewStyle vs)
198    throws IllegalArgumentException, IllegalAccessException
199    {
200  0 if (field.get(vs) == null)
201    {
202  0 return 0d;
203    }
204  0 return ((Number) field.get(vs)).doubleValue();
205    }
206   
207    /**
208    * Test that the equals method compares every field by changing them one by
209    * one in a cloned ViewStyle.
210    *
211    * This test will fail if a new field is added to ViewStyle but not to the
212    * comparisons in ViewStyle.equals().
213    *
214    * To confirm that this test works, temporarily comment out one of the field
215    * comparisons in ViewStyle.equals()
216    *
217    * @throws IllegalAccessException
218    * @throws IllegalArgumentException
219    */
 
220  1 toggle @Test(groups = { "Functional" })
221    public void testEquals()
222    throws IllegalArgumentException, IllegalAccessException
223    {
224  1 ViewStyle vs1 = new ViewStyle();
225  1 ViewStyle vs2 = new ViewStyle(vs1);
226   
227  1 assertFalse(vs1.equals(null));
228  1 assertFalse(vs1.equals(this));
229  1 assertTrue(vs1.equals(vs2));
230  1 assertTrue(vs2.equals(vs1));
231   
232  1 Field[] fields = ViewStyle.class.getDeclaredFields();
233  1 for (Field field : fields)
234    {
235  46 if (!copyConstructorIgnores(field))
236    {
237  45 field.setAccessible(true);
238  45 Object oldValue = field.get(vs2);
239  45 changeValue(vs2, field);
240  45 assertFalse("equals method ignores " + field.getName(),
241    vs1.equals(vs2));
242   
243  45 if (vs1.hashCode() == vs2.hashCode())
244    {
245    // uncomment next line to see which fields hashCode ignores
246    // System.out.println("hashCode ignores " + field.getName());
247    }
248    // restore original value before testing the next field
249  45 field.set(vs2, oldValue);
250    }
251    }
252    }
253    }