1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertNotNull;
|
---|
5 |
|
---|
6 | import java.lang.reflect.Constructor;
|
---|
7 | import java.lang.reflect.Modifier;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.Set;
|
---|
11 |
|
---|
12 | import javax.swing.JTable;
|
---|
13 | import javax.swing.table.TableCellRenderer;
|
---|
14 |
|
---|
15 | import org.junit.Assert;
|
---|
16 | import org.junit.BeforeClass;
|
---|
17 | import org.junit.Test;
|
---|
18 | import org.openstreetmap.josm.JOSMFixture;
|
---|
19 | import org.openstreetmap.josm.tools.Utils;
|
---|
20 | import org.reflections.Reflections;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Checks if all classes implementing the {@link TableCellRenderer} interface do
|
---|
24 | * accept a null value as second parameter for
|
---|
25 | * {@link TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable,
|
---|
26 | * java.lang.Object, boolean, boolean, int, int)}.
|
---|
27 | *
|
---|
28 | * For unknown reason java sometimes call getTableCellRendererComponent method
|
---|
29 | * with value = null. Every implementation of {@code getTableCellRendererComponent}
|
---|
30 | * must fail gracefully when null is passed as value parameter.
|
---|
31 | *
|
---|
32 | * This test scans the classpath for classes implementing {@code TableCellRenderer},
|
---|
33 | * creates an instance and calls {@code getTableCellRendererComponent} with null
|
---|
34 | * value to check if a NPE is thrown.
|
---|
35 | *
|
---|
36 | * @see <a href="https://josm.openstreetmap.de/ticket/6301">#6301</a>
|
---|
37 | */
|
---|
38 | public class TableCellRendererTest {
|
---|
39 |
|
---|
40 | // list of classes that cannot be easily tested and are verified either manually or another unit tests
|
---|
41 | private static final Collection<String> SKIP_TEST = Arrays.asList(
|
---|
42 | "org.openstreetmap.josm.gui.dialogs.FilterDialog$BooleanRenderer",
|
---|
43 | "org.openstreetmap.josm.gui.dialogs.relation.SelectionTableCellRenderer"
|
---|
44 | );
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Setup test.
|
---|
48 | */
|
---|
49 | @BeforeClass
|
---|
50 | public static void setUp() {
|
---|
51 | JOSMFixture.createFunctionalTestFixture().init(true);
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Unit test of all table cell renderers against null values.
|
---|
56 | * @throws NoSuchMethodException no default constructor - to fix this, add a default constructor to the class
|
---|
57 | * or add the class to the SKIP_TEST list above
|
---|
58 | * @throws ReflectiveOperationException if an error occurs
|
---|
59 | */
|
---|
60 | @Test
|
---|
61 | public void testTableCellRenderer() throws ReflectiveOperationException {
|
---|
62 | Reflections reflections = new Reflections("org.openstreetmap.josm");
|
---|
63 | Set<Class<? extends TableCellRenderer>> renderers = reflections.getSubTypesOf(TableCellRenderer.class);
|
---|
64 | Assert.assertTrue(renderers.size() >= 10); // if it finds less than 10 classes, something is broken
|
---|
65 | JTable tbl = new JTable(2, 2);
|
---|
66 | for (Class<? extends TableCellRenderer> klass : renderers) {
|
---|
67 | if (Modifier.isAbstract(klass.getModifiers()) || SKIP_TEST.contains(klass.getName())) {
|
---|
68 | continue;
|
---|
69 | }
|
---|
70 | if (klass.isAnonymousClass()) {
|
---|
71 | continue;
|
---|
72 | }
|
---|
73 | assertNotNull(createInstance(klass).getTableCellRendererComponent(tbl, null, false, false, 0, 0));
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Create an instance of a class assuming it has a no-args constructor.
|
---|
79 | * @param <T> the class or a super-type of the class
|
---|
80 | * @param klass the class
|
---|
81 | * @return an instance of the class
|
---|
82 | * @throws NoSuchMethodException no default constructor - to fix this, add a default constructor to the class
|
---|
83 | * or add the class to the SKIP_TEST list above
|
---|
84 | * @throws ReflectiveOperationException if an error occurs
|
---|
85 | */
|
---|
86 | private static <T> T createInstance(Class<? extends T> klass) throws ReflectiveOperationException {
|
---|
87 | boolean needOuterClass = klass.isMemberClass() && !Modifier.isStatic(klass.getModifiers());
|
---|
88 | Constructor<? extends T> c;
|
---|
89 | if (needOuterClass) {
|
---|
90 | c = klass.getDeclaredConstructor(klass.getDeclaringClass());
|
---|
91 | } else {
|
---|
92 | c = klass.getDeclaredConstructor();
|
---|
93 | }
|
---|
94 | Utils.setObjectsAccessible(c);
|
---|
95 | if (needOuterClass) {
|
---|
96 | return c.newInstance(createInstance(klass.getDeclaringClass()));
|
---|
97 | } else {
|
---|
98 | return c.newInstance();
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|