1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
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 |
|
|
|
|
| 77.1% |
Uncovered Elements: 25 (109) |
Complexity: 30 |
Complexity Density: 0.46 |
|
37 |
|
public class ViewStyleTest |
38 |
|
{ |
39 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
40 |
1 |
@BeforeClass(alwaysRun = true)... |
41 |
|
public void setUpJvOptionPane() |
42 |
|
{ |
43 |
1 |
JvOptionPane.setInteractiveMode(false); |
44 |
1 |
JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION); |
45 |
|
} |
46 |
|
|
47 |
|
Random r = new Random(); |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
@throws |
60 |
|
@throws |
61 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 3 |
Complexity Density: 0.21 |
1PASS
|
|
62 |
1 |
@Test(groups = { "Functional" })... |
63 |
|
public void testCopyConstructor() |
64 |
|
throws IllegalArgumentException, IllegalAccessException |
65 |
|
{ |
66 |
1 |
ViewStyle vs1 = new ViewStyle(); |
67 |
1 |
Field[] fields = ViewStyle.class.getDeclaredFields(); |
68 |
1 |
for (Field field : fields) |
69 |
|
{ |
70 |
42 |
field.setAccessible(true); |
71 |
42 |
if (!copyConstructorIgnores(field)) |
72 |
|
{ |
73 |
41 |
changeValue(vs1, field); |
74 |
|
} |
75 |
|
} |
76 |
|
|
77 |
1 |
ViewStyle vs2 = new ViewStyle(vs1); |
78 |
|
|
79 |
1 |
for (Field field1 : fields) |
80 |
|
{ |
81 |
42 |
if (!copyConstructorIgnores(field1)) |
82 |
|
{ |
83 |
41 |
final Object value1 = field1.get(vs1); |
84 |
41 |
final Object value2 = field1.get(vs2); |
85 |
41 |
String msg = "Mismatch in " + field1.getName() + "(" + value1 + "/" |
86 |
|
+ value2 + ") - not set in copy constructor?"; |
87 |
41 |
assertEquals(msg, value1, value2); |
88 |
|
} |
89 |
|
} |
90 |
1 |
assertEquals("Hashcode not equals", vs1.hashCode(), vs2.hashCode()); |
91 |
|
} |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
@param |
98 |
|
@return |
99 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
100 |
126 |
private boolean copyConstructorIgnores(Field field)... |
101 |
|
{ |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
126 |
if (field.isSynthetic()) |
106 |
|
{ |
107 |
0 |
return true; |
108 |
|
} |
109 |
126 |
if (field.getType().toString().contains("com_atlassian_clover")) |
110 |
|
{ |
111 |
3 |
return true; |
112 |
|
} |
113 |
|
|
114 |
123 |
return false; |
115 |
|
} |
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
@param |
121 |
|
@param |
122 |
|
@throws |
123 |
|
|
|
|
| 66% |
Uncovered Elements: 17 (50) |
Complexity: 18 |
Complexity Density: 0.69 |
|
124 |
82 |
protected void changeValue(ViewStyle vs, Field field)... |
125 |
|
throws IllegalAccessException |
126 |
|
{ |
127 |
82 |
Class<?> type = field.getType(); |
128 |
|
|
129 |
82 |
if (type.equals(boolean.class) || type.equals(Boolean.class)) |
130 |
|
{ |
131 |
58 |
boolean value = (Boolean) field.get(vs); |
132 |
|
|
133 |
58 |
field.set(vs, !value); |
134 |
|
} |
135 |
24 |
else if (type.equals(short.class) || type.equals(int.class) |
136 |
|
|| type.equals(long.class) || type.equals(float.class) |
137 |
|
|| type.equals(double.class)) |
138 |
|
{ |
139 |
18 |
final int value = (int) (1 + field.getDouble(vs)); |
140 |
|
|
141 |
18 |
field.set(vs, value); |
142 |
|
} |
143 |
6 |
else if (type.equals(Integer.class)) |
144 |
|
{ |
145 |
0 |
field.set(vs, (int) (1 + getNumberValue(field, vs))); |
146 |
|
} |
147 |
6 |
else if (type.equals(Float.class)) |
148 |
|
{ |
149 |
0 |
field.set(vs, (float) (1f + getNumberValue(field, vs))); |
150 |
|
} |
151 |
6 |
else if (type.equals(Long.class)) |
152 |
|
{ |
153 |
0 |
field.set(vs, (long) (1L + getNumberValue(field, vs))); |
154 |
|
} |
155 |
6 |
else if (type.equals(Double.class)) |
156 |
|
{ |
157 |
0 |
field.set(vs, 1d + getNumberValue(field, vs)); |
158 |
|
} |
159 |
6 |
else if (type.equals(Short.class)) |
160 |
|
{ |
161 |
0 |
field.set(vs, (short) (1 + getNumberValue(field, vs))); |
162 |
|
} |
163 |
6 |
else if (type.equals(Byte.class)) |
164 |
|
{ |
165 |
0 |
field.set(vs, (byte) (1 + getNumberValue(field, vs))); |
166 |
|
} |
167 |
6 |
else if (type.equals(Character.class)) |
168 |
|
{ |
169 |
0 |
field.set(vs, (char) (1 + getNumberValue(field, vs))); |
170 |
|
} |
171 |
6 |
else if (type.equals(String.class)) |
172 |
|
{ |
173 |
2 |
field.set(vs, "Joe" + field.get(vs)); |
174 |
|
} |
175 |
4 |
else if (type.equals(Color.class)) |
176 |
|
{ |
177 |
4 |
field.set(vs, |
178 |
4 |
Color.RED.equals(field.get(vs)) ? Color.BLACK : Color.RED); |
179 |
|
} |
180 |
|
else |
181 |
|
{ |
182 |
0 |
AssertJUnit.fail("Unhandled field type (add to test): " |
183 |
|
+ field.getName() + ":" + type); |
184 |
|
} |
185 |
|
} |
186 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
187 |
0 |
private double getNumberValue(Field field, ViewStyle vs)... |
188 |
|
throws IllegalArgumentException, IllegalAccessException |
189 |
|
{ |
190 |
0 |
if (field.get(vs) == null) |
191 |
|
{ |
192 |
0 |
return 0d; |
193 |
|
} |
194 |
0 |
return ((Number) field.get(vs)).doubleValue(); |
195 |
|
} |
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
|
207 |
|
@throws |
208 |
|
@throws |
209 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 3 |
Complexity Density: 0.2 |
1PASS
|
|
210 |
1 |
@Test(groups = { "Functional" })... |
211 |
|
public void testEquals() |
212 |
|
throws IllegalArgumentException, IllegalAccessException |
213 |
|
{ |
214 |
1 |
ViewStyle vs1 = new ViewStyle(); |
215 |
1 |
ViewStyle vs2 = new ViewStyle(vs1); |
216 |
|
|
217 |
1 |
assertFalse(vs1.equals(null)); |
218 |
1 |
assertFalse(vs1.equals(this)); |
219 |
1 |
assertTrue(vs1.equals(vs2)); |
220 |
1 |
assertTrue(vs2.equals(vs1)); |
221 |
|
|
222 |
1 |
Field[] fields = ViewStyle.class.getDeclaredFields(); |
223 |
1 |
for (Field field : fields) |
224 |
|
{ |
225 |
42 |
if (!copyConstructorIgnores(field)) |
226 |
|
{ |
227 |
41 |
field.setAccessible(true); |
228 |
41 |
Object oldValue = field.get(vs2); |
229 |
41 |
changeValue(vs2, field); |
230 |
41 |
assertFalse("equals method ignores " + field.getName(), |
231 |
|
vs1.equals(vs2)); |
232 |
|
|
233 |
41 |
if (vs1.hashCode() == vs2.hashCode()) |
234 |
|
{ |
235 |
|
|
236 |
|
|
237 |
|
} |
238 |
|
|
239 |
41 |
field.set(vs2, oldValue); |
240 |
|
} |
241 |
|
} |
242 |
|
} |
243 |
|
} |