Ticket #18200: 18200.patch

File 18200.patch, 7.7 KB (added by taylor.smock, 10 months ago)

Modify methods to be package instead of private, indicate via javadoc that they are visible for JMockit, update JMockit to 1.49

  • ivy.xml

     
    5050            <artifact name="org.jacoco.ant" type="jar" maven:classifier="nodeps"/>
    5151        </dependency>
    5252        <!-- jmockit->default - Don't upgrade JMockit until https://josm.openstreetmap.de/ticket/18200 is resolved -->
    53         <dependency conf="jmockit->default" org="org.jmockit" name="jmockit" rev="1.44"/>
     53        <dependency conf="jmockit->default" org="org.jmockit" name="jmockit" rev="1.49"/>
    5454        <!-- test->default -->
    5555        <dependency conf="test->default" org="com.github.spotbugs" name="spotbugs-annotations" rev="4.0.1"/>
    5656        <dependency conf="test->default" org="com.github.stefanbirkner" name="system-rules" rev="1.19.0">
  • src/org/openstreetmap/josm/gui/MapViewState.java

     
    131131        this(projecting, mvs.viewWidth, mvs.viewHeight, mvs.scale, mvs.topLeft, mvs.topLeftInWindow, mvs.topLeftOnScreen);
    132132    }
    133133
    134     private static Point findTopLeftInWindow(JComponent position) {
     134    /**
     135     * This is visible for JMockit.
     136     *
     137     * @param position The component to get the top left position of its window
     138     */
     139    static Point findTopLeftInWindow(JComponent position) {
    135140        Point result = new Point();
    136141        // better than using swing utils, since this allows us to use the method if no screen is present.
    137142        Container component = position;
     
    143148        return result;
    144149    }
    145150
    146     private static Point findTopLeftOnScreen(JComponent position) {
     151    /**
     152     * This is visible for JMockit.
     153     *
     154     * @param position The component to get the top left position of its screen
     155     */
     156    static Point findTopLeftOnScreen(JComponent position) {
    147157        try {
    148158            return position.getLocationOnScreen();
    149159        } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) {
  • src/org/openstreetmap/josm/gui/util/GuiHelper.java

     
    203203        }
    204204    }
    205205
    206     private static void handleEDTException(Throwable t) {
     206    /**
     207     * Handle exceptions in the EDT. This should only be used in {@link GuiHelper}
     208     * and {@link org.openstreetmap.josm.testutils.mockers.EDTAssertionMocker}.
     209     *
     210     * @param t The throwable to handle
     211     */
     212    static void handleEDTException(Throwable t) {
    207213        Logging.logWithStackTrace(Logging.LEVEL_ERROR, t, "Exception raised in EDT");
    208214    }
    209215
  • test/unit/org/openstreetmap/josm/testutils/mockers/EDTAssertionMocker.java

     
    1313 */
    1414public class EDTAssertionMocker extends MockUp<GuiHelper> {
    1515    @Mock
    16     private static void handleEDTException(final Invocation invocation, final Throwable t) {
     16    static void handleEDTException(final Invocation invocation, final Throwable t) throws Throwable {
    1717        final Throwable cause = t.getCause();
    1818        if (cause instanceof AssertionError) {
    1919            throw (AssertionError) cause;
  • test/unit/org/openstreetmap/josm/testutils/mockers/ExtendedDialogMocker.java

     
    55
    66import java.awt.Component;
    77import java.awt.GraphicsEnvironment;
     8import java.lang.reflect.Field;
    89import java.util.Arrays;
    910import java.util.Map;
    1011import java.util.Optional;
    1112import java.util.WeakHashMap;
    1213
     14import org.junit.platform.commons.util.ReflectionUtils;
    1315import org.openstreetmap.josm.TestUtils;
    1416import org.openstreetmap.josm.gui.ExtendedDialog;
    1517import org.openstreetmap.josm.tools.Logging;
    1618
    17 import mockit.Deencapsulation;
    1819import mockit.Invocation;
    1920import mockit.Mock;
    20 import mockit.internal.reflection.FieldReflection;
    2121
    2222/**
    2323 * MockUp for {@link ExtendedDialog} allowing a test to pre-seed uses of {@link ExtendedDialog}
     
    7676    }
    7777
    7878    protected int getButtonPositionFromLabel(final ExtendedDialog instance, final String label) {
    79         final String[] bTexts = Deencapsulation.getField(instance, "bTexts");
     79        final String[] bTexts = (String[]) ReflectionUtils.tryToReadFieldValue(ExtendedDialog.class, "bTexts", instance).toOptional()
     80            .orElse(null);
    8081        final int position = Arrays.asList(bTexts).indexOf(label);
    8182        if (position == -1) {
    8283            fail("Unable to find button labeled \"" + label + "\". Instead found: " + Arrays.toString(bTexts));
     
    150151    }
    151152
    152153    @Mock
    153     private void setVisible(final Invocation invocation, final boolean value) {
     154    private void setVisible(final Invocation invocation, final boolean value) throws Throwable {
    154155        if (value == true) {
    155156            try {
    156157                final ExtendedDialog instance = invocation.getInvokedInstance();
     
    157158                this.act(instance);
    158159                final int mockResult = this.getMockResult(instance);
    159160                // TODO check validity of mockResult?
    160                 FieldReflection.setField(instance.getClass(), instance, "result", mockResult);
     161                Field resultField = instance.getClass().getDeclaredField("result");
     162                resultField.setAccessible(true);
     163                resultField.set(instance, mockResult);
    161164                Logging.info(
    162165                    "{0} answering {1} to ExtendedDialog with content {2}",
    163166                    this.getClass().getName(),
     
    165168                    this.getString(instance)
    166169                );
    167170                this.getInvocationLogInternal().add(this.getInvocationLogEntry(instance, mockResult));
    168             } catch (AssertionError e) {
     171            } catch (AssertionError | NoSuchFieldException | IllegalAccessException e) {
    169172                // in case this exception gets ignored by the calling thread we want to signify this failure
    170173                // in the invocation log. it's hard to know what to add to the log in these cases as it's
    171174                // probably unsafe to call getInvocationLogEntry, so add the exception on its own.
  • test/unit/org/openstreetmap/josm/testutils/mockers/WindowlessMapViewStateMocker.java

     
    1616 */
    1717public class WindowlessMapViewStateMocker extends MockUp<MapViewState> {
    1818    @Mock
    19     private static Point findTopLeftInWindow(JComponent position) {
     19    static Point findTopLeftInWindow(JComponent position) {
    2020        return new Point();
    2121    }
    2222
    2323    @Mock
    24     private static Point findTopLeftOnScreen(JComponent position) {
     24    static Point findTopLeftOnScreen(JComponent position) {
    2525        // in our imaginary universe the window is always (10, 10) from the top left of the screen
    2626        Point topLeftInWindow = findTopLeftInWindow(position);
    2727        return new Point(topLeftInWindow.x + 10, topLeftInWindow.y + 10);