2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.viewmodel.styles;
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertTrue;
27 import java.awt.Color;
28 import java.lang.reflect.Field;
29 import java.util.Random;
31 import org.testng.AssertJUnit;
32 import org.testng.annotations.Test;
34 public class ViewStyleTest
37 Random r = new Random();
40 * This test uses reflection to set all fields on a ViewStyle, make a copy of
41 * it, and verify all fields match. This test should fail if a getter/setter
42 * pair are added to the class but missing in the copy constructor. Using
43 * reflection in the copy constructor itself is broken by obfuscation when the
46 * To prove this test works, simply comment out a line in the ViewStyle copy
47 * constructor, or add a new member field to ViewStyle.
49 * @throws IllegalAccessException
50 * @throws IllegalArgumentException
52 @Test(groups = { "Functional" })
53 public void testCopyConstructor() throws IllegalArgumentException,
54 IllegalAccessException
56 ViewStyle vs1 = new ViewStyle();
57 Field[] fields = ViewStyle.class.getDeclaredFields();
58 for (Field field : fields)
60 field.setAccessible(true);
61 if (!copyConstructorIgnores(field.getName()))
63 changeValue(vs1, field);
67 ViewStyle vs2 = new ViewStyle(vs1);
69 for (Field field1 : fields)
71 final Object value1 = field1.get(vs1);
72 final Object value2 = field1.get(vs2);
73 String msg = "Mismatch in " + field1.getName() + "(" + value1 + "/"
74 + value2 + ") - not set in copy constructor?";
75 assertEquals(msg, value1, value2);
77 assertEquals("Hashcode not equals", vs1.hashCode(), vs2.hashCode());
81 * Add any field names in here that we expect to be ignored by the copy
87 private boolean copyConstructorIgnores(String name)
96 * Change the value of one field in a ViewStyle object
100 * @throws IllegalAccessException
102 protected void changeValue(ViewStyle vs, Field field)
103 throws IllegalAccessException
105 Class<?> type = field.getType();
107 if (type.equals(boolean.class) || type.equals(Boolean.class))
109 boolean value = (Boolean) field.get(vs);
110 // System.out.println("Setting " + field.getName() + " to " + !value);
111 field.set(vs, !value);
113 else if (type.equals(short.class) || type.equals(int.class)
114 || type.equals(long.class) || type.equals(float.class)
115 || type.equals(double.class))
117 final int value = (int) (1 + field.getDouble(vs));
118 // System.out.println("Setting " + field.getName() + " to " + value);
119 field.set(vs, value);
121 else if (type.equals(Integer.class))
123 field.set(vs, (int) (1 + getNumberValue(field, vs)));
125 else if (type.equals(Float.class))
127 field.set(vs, (float) (1f + getNumberValue(field, vs)));
129 else if (type.equals(Long.class))
131 field.set(vs, (long) (1L + getNumberValue(field, vs)));
133 else if (type.equals(Double.class))
135 field.set(vs, 1d + getNumberValue(field, vs));
137 else if (type.equals(Short.class))
139 field.set(vs, (short) (1 + getNumberValue(field, vs)));
141 else if (type.equals(Byte.class))
143 field.set(vs, (byte) (1 + getNumberValue(field, vs)));
145 else if (type.equals(Character.class))
147 field.set(vs, (char) (1 + getNumberValue(field, vs)));
149 else if (type.equals(String.class))
151 field.set(vs, "Joe" + field.get(vs));
153 else if (type.equals(Color.class))
155 field.set(vs, Color.RED.equals(field.get(vs)) ? Color.BLACK
160 AssertJUnit.fail("Unhandled field type (add to test): "
161 + field.getName() + ":" + type);
165 private double getNumberValue(Field field, ViewStyle vs)
166 throws IllegalArgumentException, IllegalAccessException
168 if (field.get(vs) == null)
172 return ((Number) field.get(vs)).doubleValue();
176 * Test that the equals method compares every field by changing them one by
177 * one in a cloned ViewStyle.
179 * This test will fail if a new field is added to ViewStyle but not to the
180 * comparisons in ViewStyle.equals().
182 * To confirm that this test works, temporarily comment out one of the field
183 * comparisons in ViewStyle.equals()
185 * @throws IllegalAccessException
186 * @throws IllegalArgumentException
188 @Test(groups = { "Functional" })
189 public void testEquals() throws IllegalArgumentException,
190 IllegalAccessException
192 ViewStyle vs1 = new ViewStyle();
193 ViewStyle vs2 = new ViewStyle(vs1);
195 assertFalse(vs1.equals(null));
196 assertFalse(vs1.equals(this));
197 assertTrue(vs1.equals(vs2));
198 assertTrue(vs2.equals(vs1));
200 Field[] fields = ViewStyle.class.getDeclaredFields();
201 for (Field field : fields)
203 field.setAccessible(true);
204 Object oldValue = field.get(vs2);
205 changeValue(vs2, field);
206 assertFalse("equals method ignores " + field.getName(),
209 if (vs1.hashCode() == vs2.hashCode())
211 // uncomment next line to see which fields hashCode ignores
212 // System.out.println("hashCode ignores " + field.getName());
214 // restore original value before testing the next field
215 field.set(vs2, oldValue);