Changeset 13108 in josm


Ignore:
Timestamp:
2017-11-10T22:37:58+01:00 (6 years ago)
Author:
Don-vip
Message:

fix #15538 - add robustness to AlignInLineAction

Location:
trunk
Files:
2 edited

Legend:

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

    r13107 r13108  
    173173
    174174        try {
    175             MainApplication.undoRedo.add(buildCommand());
     175            Command cmd = buildCommand(getLayerManager().getEditDataSet());
     176            if (cmd != null) {
     177                MainApplication.undoRedo.add(cmd);
     178            }
    176179        } catch (InvalidSelection except) {
    177180            Logging.debug(except);
     
    184187    /**
    185188     * Builds "align in line" command depending on the selected objects.
     189     * @param ds data set in which the command operates
    186190     * @return the resulting command to execute to perform action
    187191     * @throws InvalidSelection if a polygon is selected, or if a node is used by 3 or more ways
    188      * @since 12562
    189      */
    190     public Command buildCommand() throws InvalidSelection {
    191         DataSet ds = getLayerManager().getEditDataSet();
     192     * @since 13108
     193     */
     194    public Command buildCommand(DataSet ds) throws InvalidSelection {
    192195        List<Node> selectedNodes = new ArrayList<>(ds.getSelectedNodes());
    193196        List<Way> selectedWays = new ArrayList<>(ds.getSelectedWays());
     
    276279                throw new InvalidSelection(tr("Intersection of three or more ways can not be solved. Abort."));
    277280        }
    278         return new SequenceCommand(tr("Align Nodes in Line"), cmds);
     281        return cmds.isEmpty() ? null : new SequenceCommand(tr("Align Nodes in Line"), cmds);
    279282    }
    280283
  • trunk/test/unit/org/openstreetmap/josm/actions/AlignInLineActionTest.java

    r12643 r13108  
    44import static org.junit.Assert.assertEquals;
    55import static org.junit.Assert.assertNotNull;
     6import static org.junit.Assert.assertNull;
    67
    78import org.junit.Before;
     
    1617import org.openstreetmap.josm.data.osm.Way;
    1718import org.openstreetmap.josm.gui.MainApplication;
    18 import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    1919import org.openstreetmap.josm.testutils.JOSMTestRules;
    2020
     
    5656    public void testNodesOpenWay() throws InvalidSelection {
    5757        DataSet dataSet = new DataSet();
    58         OsmDataLayer layer = new OsmDataLayer(dataSet, OsmDataLayer.createNewName(), null);
    5958
    6059        // Create test points, lower left is (0,0).
     
    6766        Node point3 = new Node(new EastNorth(1, 1));
    6867
    69         try {
    70             MainApplication.getLayerManager().addLayer(layer);
    71 
    72             // Create an open way.
    73             createWay(dataSet, point1, point2, point3);
    74 
    75             // Select nodes to align.
    76             dataSet.addSelected(point1, point2, point3);
    77 
    78             action.buildCommand().executeCommand();
    79         } finally {
    80             // Ensure we clean the place before leaving, even if test fails.
    81             MainApplication.getLayerManager().removeLayer(layer);
    82         }
     68        // Create an open way.
     69        createWay(dataSet, point1, point2, point3);
     70
     71        // Select nodes to align.
     72        dataSet.addSelected(point1, point2, point3);
     73
     74        action.buildCommand(dataSet).executeCommand();
    8375
    8476        // Points 1 and 3 are the extremities and must not have moved. Only point 2 must have moved.
     
    9688    public void testNodesClosedWay() throws InvalidSelection {
    9789        DataSet dataSet = new DataSet();
    98         OsmDataLayer layer = new OsmDataLayer(dataSet, OsmDataLayer.createNewName(), null);
    9990
    10091        // Create test points, lower left is (0,0).
     
    10899        Node point4 = new Node(new EastNorth(0, 2));
    109100
    110         try {
    111             MainApplication.getLayerManager().addLayer(layer);
    112 
    113             // Create a closed way.
    114             createWay(dataSet, point1, point2, point3, point4, point1);
    115             // Select nodes to align (point1 must be in the second position to exhibit the bug).
    116             dataSet.addSelected(point4, point1, point2);
    117 
    118             action.buildCommand().executeCommand();
    119         } finally {
    120             // Ensure we clean the place before leaving, even if test fails.
    121             MainApplication.getLayerManager().removeLayer(layer);
    122         }
     101        // Create a closed way.
     102        createWay(dataSet, point1, point2, point3, point4, point1);
     103        // Select nodes to align (point1 must be in the second position to exhibit the bug).
     104        dataSet.addSelected(point4, point1, point2);
     105
     106        action.buildCommand(dataSet).executeCommand();
    123107
    124108        // Only point 1 must have moved.
     
    137121    public void testNodesOpenWays() throws InvalidSelection {
    138122        DataSet dataSet = new DataSet();
    139         OsmDataLayer layer = new OsmDataLayer(dataSet, OsmDataLayer.createNewName(), null);
    140123
    141124        // Create test points, lower left is (0,0).
     
    149132        Node point4 = new Node(new EastNorth(2, 0));
    150133
    151         try {
    152             MainApplication.getLayerManager().addLayer(layer);
    153 
    154             // Create 2 ways.
    155             createWay(dataSet, point1, point2);
    156             createWay(dataSet, point3, point4);
    157 
    158             // Select nodes to align.
    159             dataSet.addSelected(point1, point2, point3, point4);
    160 
    161             // Points must align between points 1 and 4.
    162             action.buildCommand().executeCommand();
    163         } finally {
    164             // Ensure we clean the place before leaving, even if test fails.
    165             MainApplication.getLayerManager().removeLayer(layer);
    166         }
     134        // Create 2 ways.
     135        createWay(dataSet, point1, point2);
     136        createWay(dataSet, point3, point4);
     137
     138        // Select nodes to align.
     139        dataSet.addSelected(point1, point2, point3, point4);
     140
     141        // Points must align between points 1 and 4.
     142        action.buildCommand(dataSet).executeCommand();
    167143
    168144        assertCoordEq(point1, 0, 2);
     
    173149
    174150    /**
     151     * Test case: only a two-nodes way selected.
     152     * @throws InvalidSelection never
     153     */
     154    @Test
     155    public void testSimpleWay() throws InvalidSelection {
     156        DataSet dataSet = new DataSet();
     157
     158        // Create test points, lower left is (0,0).
     159        //
     160        // 1 - -
     161        // - - 2
     162        Node point1 = new Node(new EastNorth(0, 2));
     163        Node point2 = new Node(new EastNorth(2, 1));
     164
     165        // Creates and select a single way.
     166        dataSet.addSelected(createWay(dataSet, point1, point2));
     167
     168        // No command must be created (nothing to do)
     169        assertNull(action.buildCommand(dataSet));
     170    }
     171
     172    /**
    175173     * Create a way made of the provided nodes and select nodes.
    176174     *
    177175     * @param dataSet Dataset in which adding nodes.
    178176     * @param nodes List of nodes to add to dataset.
    179      */
    180     private void createWay(DataSet dataSet, Node... nodes) {
     177     * @return created way
     178     */
     179    private Way createWay(DataSet dataSet, Node... nodes) {
    181180        Way way = new Way();
    182181        dataSet.addPrimitive(way);
     
    189188            way.addNode(node);
    190189        }
     190        return way;
    191191    }
    192192
Note: See TracChangeset for help on using the changeset viewer.