Changeset 19227 in josm for trunk


Ignore:
Timestamp:
2024-09-19T15:38:39+02:00 (3 months ago)
Author:
taylor.smock
Message:

Fix #20908: IllegalStateException: JOSM expected to find primitive in dataset after undoing a Parallel mode action

The test acts (roughly) like a user would, and performs click and drags via a new
utility class (MapModeUtils) which has various methods for performing mouse
actions on the map view.

Location:
trunk
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/mapmode/ParallelWayAction.java

    r19050 r19227  
    1313import java.awt.event.KeyEvent;
    1414import java.awt.event.MouseEvent;
     15import java.util.ArrayList;
    1516import java.util.Collection;
    1617import java.util.Collections;
     
    1819import java.util.EnumSet;
    1920import java.util.LinkedHashSet;
     21import java.util.List;
    2022import java.util.Map;
    2123import java.util.Optional;
     
    287289        // Since the created way is left selected, we need to unselect again here
    288290        if (pWays != null && pWays.getWays() != null) {
    289             getLayerManager().getEditDataSet().clearSelection(pWays.getWays());
     291            final List<Way> ways = new ArrayList<>(pWays.getWays());
     292            ways.removeIf(w -> w.getDataSet() == null);
     293            getLayerManager().getEditDataSet().clearSelection(ways);
    290294            pWays = null;
    291295        }
  • trunk/test/unit/org/openstreetmap/josm/actions/mapmode/ParallelWayActionTest.java

    r18870 r19227  
    55import static org.junit.jupiter.api.Assertions.assertTrue;
    66
     7import org.junit.jupiter.api.BeforeEach;
    78import org.junit.jupiter.api.Test;
    89import org.openstreetmap.josm.TestUtils;
    910import org.openstreetmap.josm.actions.mapmode.ParallelWayAction.Mode;
    1011import org.openstreetmap.josm.actions.mapmode.ParallelWayAction.Modifier;
     12import org.openstreetmap.josm.data.UndoRedoHandler;
     13import org.openstreetmap.josm.data.coor.LatLon;
    1114import org.openstreetmap.josm.data.osm.DataSet;
    1215import org.openstreetmap.josm.gui.MainApplication;
    1316import org.openstreetmap.josm.gui.MapFrame;
    1417import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     18import org.openstreetmap.josm.testutils.MapModeUtils;
    1519import org.openstreetmap.josm.testutils.annotations.Main;
    1620import org.openstreetmap.josm.testutils.annotations.Projection;
     21import org.openstreetmap.josm.tools.Logging;
    1722
    1823/**
     
    2227@Projection
    2328class ParallelWayActionTest {
     29    private MapFrame map;
     30    private ParallelWayAction mapMode;
     31    private DataSet dataSet;
     32
     33    @BeforeEach
     34    void setup() {
     35        OsmDataLayer layer = new OsmDataLayer(new DataSet(), "ParallelWayActionTest", null);
     36        MainApplication.getLayerManager().addLayer(layer);
     37        this.map = MainApplication.getMap();
     38        this.mapMode = new ParallelWayAction(this.map);
     39        this.dataSet = layer.getDataSet();
     40    }
     41
    2442    /**
    2543     * Unit test of {@link ParallelWayAction#enterMode} and {@link ParallelWayAction#exitMode}.
     
    2745    @Test
    2846    void testMode() {
    29         OsmDataLayer layer = new OsmDataLayer(new DataSet(), "", null);
    30         try {
    31             MainApplication.getLayerManager().addLayer(layer);
    32             MapFrame map = MainApplication.getMap();
    33             ParallelWayAction mapMode = new ParallelWayAction(map);
    34             MapMode oldMapMode = map.mapMode;
    35             assertTrue(map.selectMapMode(mapMode));
    36             assertEquals(mapMode, map.mapMode);
    37             assertTrue(map.selectMapMode(oldMapMode));
    38         } finally {
    39             MainApplication.getLayerManager().removeLayer(layer);
    40         }
     47        final MapMode oldMapMode = this.map.mapMode;
     48        assertTrue(this.map.selectMapMode(mapMode));
     49        assertEquals(this.mapMode, this.map.mapMode);
     50        assertTrue(this.map.selectMapMode(oldMapMode));
     51    }
     52
     53    /**
     54     * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/20908">#20908</a>
     55     */
     56    @Test
     57    void testNonRegression20908() {
     58        Logging.clearLastErrorAndWarnings();
     59        this.map.selectMapMode(this.map.mapModeDraw);
     60        MapModeUtils.clickAt(LatLon.ZERO);
     61        MapModeUtils.clickAt(2, new LatLon(0.0001, 0));
     62        assertEquals(3, this.dataSet.allPrimitives().size());
     63        this.map.selectMapMode(mapMode);
     64        MapModeUtils.dragFromTo(new LatLon(0.00005, 0), new LatLon(0.00005, 0.0001));
     65        assertEquals(6, this.dataSet.allPrimitives().size());
     66        UndoRedoHandler.getInstance().undo();
     67        assertEquals(3, this.dataSet.allPrimitives().size());
     68        this.map.mapMode.mousePressed(MapModeUtils.mouseClickAt(new LatLon(0.00005, 0.0001)));
     69        assertTrue(Logging.getLastErrorAndWarnings().isEmpty(), String.join("\n", Logging.getLastErrorAndWarnings()));
    4170    }
    4271
  • trunk/test/unit/org/openstreetmap/josm/tools/ExceptionUtilTest.java

    r19226 r19227  
    8787        );
    8888    }
     89
    8990    /**
    9091     * Test of {@link ExceptionUtil#explainBadRequest} method.
     
    9495    @SuppressWarnings("unchecked")
    9596    void testExplainBadRequest(Supplier<String> message, Object exception) {
    96         assertEquals(message.get(), ExceptionUtil.explainBadRequest(exception instanceof Supplier ? ((Supplier<OsmApiException>) exception).get() : (OsmApiException) exception));
     97        assertEquals(message.get(), ExceptionUtil.explainBadRequest(
     98                exception instanceof Supplier ? ((Supplier<OsmApiException>) exception).get() : (OsmApiException) exception
     99        ));
    97100    }
    98101
Note: See TracChangeset for help on using the changeset viewer.