Changeset 10937 in josm for trunk/test/unit/org/openstreetmap/josm/gui/TableCellRendererTest.java
- Timestamp:
- 2016-09-03T12:24:18+02:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/gui/TableCellRendererTest.java
r10758 r10937 2 2 package org.openstreetmap.josm.gui; 3 3 4 import static org.junit.Assert.assertNotNull; 5 4 6 import java.lang.reflect.Constructor; 5 import java.lang.reflect.InvocationTargetException;6 7 import java.lang.reflect.Modifier; 7 8 import java.util.Arrays; … … 53 54 /** 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 55 59 */ 56 60 @Test 57 public void testTableCellRenderer() {61 public void testTableCellRenderer() throws ReflectiveOperationException { 58 62 Reflections reflections = new Reflections("org.openstreetmap.josm"); 59 63 Set<Class<? extends TableCellRenderer>> renderers = reflections.getSubTypesOf(TableCellRenderer.class); … … 67 71 continue; 68 72 } 69 TableCellRenderer tcr = createInstance(klass); 70 try { 71 tcr.getTableCellRendererComponent(tbl, null, false, false, 0, 0); 72 } catch (NullPointerException npe) { 73 npe.printStackTrace(); 74 Assert.fail("NPE in getTableCellRendererComponent"); 75 } 73 assertNotNull(createInstance(klass).getTableCellRendererComponent(tbl, null, false, false, 0, 0)); 76 74 } 77 75 } … … 82 80 * @param klass the class 83 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 84 85 */ 85 private static <T> T createInstance(Class<? extends T> klass) {86 private static <T> T createInstance(Class<? extends T> klass) throws ReflectiveOperationException { 86 87 boolean needOuterClass = klass.isMemberClass() && !Modifier.isStatic(klass.getModifiers()); 87 88 Constructor<? extends T> c; 88 try { 89 if (needOuterClass) { 90 c = klass.getDeclaredConstructor(klass.getDeclaringClass()); 91 } else { 92 c = klass.getDeclaredConstructor(); 93 } 94 } catch (NoSuchMethodException ex) { 95 // no default constructor - to fix this, add a default constructor 96 // to the class or add the class to the SKIP_TEST list above 97 Assert.fail("No default constructor - cannot test TableCellRenderer: " + ex); 98 return null; 99 } catch (SecurityException ex) { 100 throw new RuntimeException(ex); 89 if (needOuterClass) { 90 c = klass.getDeclaredConstructor(klass.getDeclaringClass()); 91 } else { 92 c = klass.getDeclaredConstructor(); 101 93 } 102 94 Utils.setObjectsAccessible(c); 103 T o; 104 try { 105 if (needOuterClass) { 106 Object outerInstance = createInstance(klass.getDeclaringClass()); 107 o = c.newInstance(outerInstance); 108 } else { 109 o = c.newInstance(); 110 } 111 } catch (InstantiationException | IllegalArgumentException | IllegalAccessException | InvocationTargetException ex) { 112 throw new RuntimeException(ex); 95 if (needOuterClass) { 96 return c.newInstance(createInstance(klass.getDeclaringClass())); 97 } else { 98 return c.newInstance(); 113 99 } 114 return o;115 100 } 116 117 101 }
Note:
See TracChangeset
for help on using the changeset viewer.