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

Last change on this file since 10937 was 10937, checked in by Don-vip, 8 years ago

sonar - squid:S3658 - Unit tests should throw exceptions

  • Property svn:eol-style set to native
File size: 4.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import static org.junit.Assert.assertNotNull;
5
6import java.lang.reflect.Constructor;
7import java.lang.reflect.Modifier;
8import java.util.Arrays;
9import java.util.Collection;
10import java.util.Set;
11
12import javax.swing.JTable;
13import javax.swing.table.TableCellRenderer;
14
15import org.junit.Assert;
16import org.junit.BeforeClass;
17import org.junit.Test;
18import org.openstreetmap.josm.JOSMFixture;
19import org.openstreetmap.josm.tools.Utils;
20import 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 */
38public 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}
Note: See TracBrowser for help on using the repository browser.