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.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 |
|
|
|
|
| 78.4% |
Uncovered Elements: 25 (116) |
Complexity: 31 |
Complexity Density: 0.44 |
|
40 |
|
public class ViewStyleTest |
41 |
|
{ |
42 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
43 |
1 |
@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 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
@throws |
63 |
|
@throws |
64 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 3 |
Complexity Density: 0.21 |
1PASS
|
|
65 |
1 |
@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 |
|
|
98 |
|
|
99 |
|
|
100 |
|
@param |
101 |
|
@return |
102 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
103 |
138 |
private boolean copyConstructorIgnores(Field field)... |
104 |
|
{ |
105 |
|
|
106 |
|
|
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 |
|
|
122 |
|
|
123 |
|
@param |
124 |
|
@param |
125 |
|
@throws |
126 |
|
|
|
|
| 70.2% |
Uncovered Elements: 17 (57) |
Complexity: 19 |
Complexity Density: 0.61 |
|
127 |
90 |
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 |
|
|
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 |
|
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
197 |
0 |
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 |
|
|
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
@throws |
218 |
|
@throws |
219 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 3 |
Complexity Density: 0.2 |
1PASS
|
|
220 |
1 |
@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 |
|
|
246 |
|
|
247 |
|
} |
248 |
|
|
249 |
45 |
field.set(vs2, oldValue); |
250 |
|
} |
251 |
|
} |
252 |
|
} |
253 |
|
} |