Ignore:
Timestamp:
2016-09-03T12:24:18+02:00 (8 years ago)
Author:
Don-vip
Message:

sonar - squid:S3658 - Unit tests should throw exceptions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/gui/TableCellRendererTest.java

    r10758 r10937  
    22package org.openstreetmap.josm.gui;
    33
     4import static org.junit.Assert.assertNotNull;
     5
    46import java.lang.reflect.Constructor;
    5 import java.lang.reflect.InvocationTargetException;
    67import java.lang.reflect.Modifier;
    78import java.util.Arrays;
     
    5354    /**
    5455     * 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
    5559     */
    5660    @Test
    57     public void testTableCellRenderer() {
     61    public void testTableCellRenderer() throws ReflectiveOperationException {
    5862        Reflections reflections = new Reflections("org.openstreetmap.josm");
    5963        Set<Class<? extends TableCellRenderer>> renderers = reflections.getSubTypesOf(TableCellRenderer.class);
     
    6771                continue;
    6872            }
    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));
    7674        }
    7775    }
     
    8280     * @param klass the class
    8381     * @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
    8485     */
    85     private static <T> T createInstance(Class<? extends T> klass) {
     86    private static <T> T createInstance(Class<? extends T> klass) throws ReflectiveOperationException {
    8687        boolean needOuterClass = klass.isMemberClass() && !Modifier.isStatic(klass.getModifiers());
    8788        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();
    10193        }
    10294        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();
    11399        }
    114         return o;
    115100    }
    116 
    117101}
Note: See TracChangeset for help on using the changeset viewer.