source: josm/trunk/test/unit/org/openstreetmap/josm/gui/TableCellRendererTest.java

Last change on this file was 18870, checked in by taylor.smock, 6 months ago

See #16567: Update to JUnit 5

This converts most tests to use @Annotations. There are also some performance
improvements as it relates to tests.

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