Changeset 14410 in josm for trunk/test


Ignore:
Timestamp:
2018-11-03T22:09:03+01:00 (14 months ago)
Author:
Don-vip
Message:

fix #16943 - ChangesetCacheManagerTest: fix for non-headless mode (patch by ris)

Location:
trunk/test/unit/org/openstreetmap/josm
Files:
2 edited

Legend:

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

    r14318 r14410  
    204204
    205205    /**
     206     * Sets a private field value.
     207     * @param obj object
     208     * @param fieldName private field name
     209     * @param value replacement value
     210     * @throws ReflectiveOperationException if a reflection operation error occurs
     211     */
     212    public static void setPrivateField(
     213        final Object obj,
     214        final String fieldName,
     215        final Object value
     216    ) throws ReflectiveOperationException {
     217        setPrivateField(obj.getClass(), obj, fieldName, value);
     218    }
     219
     220    /**
     221     * Sets a private field value.
     222     * @param cls object class
     223     * @param obj object
     224     * @param fieldName private field name
     225     * @param value replacement value
     226     * @throws ReflectiveOperationException if a reflection operation error occurs
     227     */
     228    public static void setPrivateField(
     229        final Class<?> cls,
     230        final Object obj,
     231        final String fieldName,
     232        final Object value
     233    ) throws ReflectiveOperationException {
     234        Field f = cls.getDeclaredField(fieldName);
     235        AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
     236            f.setAccessible(true);
     237            return null;
     238        });
     239        f.set(obj, value);
     240    }
     241
     242    /**
    206243     * Returns a private static field value.
    207244     * @param cls object class
  • trunk/test/unit/org/openstreetmap/josm/gui/dialogs/changeset/ChangesetCacheManagerTest.java

    r14138 r14410  
    22package org.openstreetmap.josm.gui.dialogs.changeset;
    33
     4import static org.junit.Assert.assertEquals;
    45import static org.junit.Assert.assertNotNull;
     6import static org.junit.Assert.assertTrue;
     7
     8import java.awt.GraphicsEnvironment;
     9import java.awt.event.ActionEvent;
     10import javax.swing.JButton;
     11import javax.swing.JDialog;
    512
    613import java.util.Collections;
     
    916import org.junit.Rule;
    1017import org.junit.Test;
     18import org.openstreetmap.josm.TestUtils;
    1119import org.openstreetmap.josm.data.osm.Changeset;
    1220import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager.CancelAction;
     
    1927import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager.RemoveFromCacheAction;
    2028import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager.ShowDetailAction;
     29import org.openstreetmap.josm.gui.dialogs.changeset.query.ChangesetQueryDialog;
    2130import org.openstreetmap.josm.testutils.JOSMTestRules;
     31import org.openstreetmap.josm.testutils.mockers.HelpAwareOptionPaneMocker;
     32import org.openstreetmap.josm.testutils.mockers.WindowMocker;
     33
     34import com.google.common.collect.ImmutableMap;
     35
     36import mockit.Invocation;
     37import mockit.Mock;
     38import mockit.MockUp;
    2239
    2340import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     
    103120    @Test
    104121    public void testDownloadMyChangesets() {
     122        TestUtils.assumeWorkingJMockit();
     123        final HelpAwareOptionPaneMocker haMocker = new HelpAwareOptionPaneMocker(
     124            ImmutableMap.<String, Object>of(
     125                "<html>JOSM is currently running with an anonymous user. It cannot download<br>"
     126                + "your changesets from the OSM server unless you enter your OSM user name<br>"
     127                + "in the JOSM preferences.</html>",
     128                "OK"
     129            )
     130        );
     131
    105132        new DownloadMyChangesets().actionPerformed(null);
     133
     134        assertEquals(1, haMocker.getInvocationLog().size());
     135        Object[] invocationLogEntry = haMocker.getInvocationLog().get(0);
     136        assertEquals(0, (int) invocationLogEntry[0]);
     137        assertEquals("Warning", invocationLogEntry[2]);
    106138    }
    107139
     
    111143    @Test
    112144    public void testDownloadSelectedChangesetContentAction() {
     145        if (GraphicsEnvironment.isHeadless()) {
     146            TestUtils.assumeWorkingJMockit();
     147            // to allow us to construct a JDialog
     148            new WindowMocker();
     149        }
     150
    113151        DownloadSelectedChangesetContentAction action = new DownloadSelectedChangesetContentAction(ChangesetCacheManager.buildModel());
    114152        action.valueChanged(null);
    115         action.actionPerformed(null);
     153        action.actionPerformed(new ActionEvent(new JDialog().getComponent(0), ActionEvent.ACTION_PERFORMED, "foo"));
    116154    }
    117155
     
    121159    @Test
    122160    public void testDownloadSelectedChangesetsAction() {
     161        if (GraphicsEnvironment.isHeadless()) {
     162            TestUtils.assumeWorkingJMockit();
     163            // to allow us to construct a JDialog
     164            new WindowMocker();
     165        }
     166
    123167        DownloadSelectedChangesetsAction action = new DownloadSelectedChangesetsAction(ChangesetCacheManager.buildModel());
    124168        action.valueChanged(null);
    125         action.actionPerformed(null);
     169        action.actionPerformed(new ActionEvent(new JDialog().getComponent(0), ActionEvent.ACTION_PERFORMED, "foo"));
    126170    }
    127171
     
    131175    @Test
    132176    public void testQueryAction() {
     177        TestUtils.assumeWorkingJMockit();
     178
     179        // set up mockers to simulate the dialog being cancelled
     180        final boolean[] dialogShown = new boolean[] {false};
     181        if (GraphicsEnvironment.isHeadless()) {
     182            new WindowMocker();
     183        }
     184        new MockUp<JDialog>() {
     185            @Mock
     186            void setVisible(final Invocation invocation, final boolean visible) throws Exception {
     187                if (visible) {
     188                    ((JButton) TestUtils.getComponentByName((JDialog) invocation.getInvokedInstance(), "cancelButton")).doClick();
     189                    dialogShown[0] = true;
     190                }
     191                // critically, don't proceed into implementation
     192            }
     193        };
     194        new MockUp<ChangesetQueryDialog>() {
     195            @Mock
     196            void setVisible(final Invocation invocation, final boolean visible) throws Exception {
     197                if (GraphicsEnvironment.isHeadless()) {
     198                    // we have to mock the behaviour quite coarsely as much of ChangesetQueryDialog will
     199                    // raise a HeadlessException
     200                    if (visible) {
     201                        TestUtils.setPrivateField(ChangesetQueryDialog.class, invocation.getInvokedInstance(), "canceled", true);
     202                        dialogShown[0] = true;
     203                    }
     204                } else {
     205                    // proceeding into the implementation allows a bit more of the target code to be
     206                    // covered, actual mocking is performed on JDialog's setVisible()
     207                    invocation.proceed(visible);
     208                }
     209            }
     210        };
     211
    133212        new QueryAction().actionPerformed(null);
     213
     214        assertTrue(dialogShown[0]);
    134215    }
    135216
Note: See TracChangeset for help on using the changeset viewer.