Changeset 18706 in josm


Ignore:
Timestamp:
2023-04-20T22:13:25+02:00 (19 months ago)
Author:
taylor.smock
Message:

Fix #22817: Don't delete layer with changes and a non-normal upload policy (patch by gaben, modified)

Patch modifications are as follows:

  • A non-regression test
  • Account for UploadPolicy.BLOCKED causing requiresUploadToServer to return false.
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/io/SaveLayersDialog.java

    r18466 r18706  
    8282    }
    8383
    84     private enum UserAction {
     84    /**
     85     * The action a user decided to take with respect to an operation
     86     */
     87    enum UserAction {
    8588        /** save/upload layers was successful, proceed with operation */
    8689        PROCEED,
     
    113116        if (!GraphicsEnvironment.isHeadless()) {
    114117            SaveLayersDialog dialog = new SaveLayersDialog(MainApplication.getMainFrame());
    115             List<AbstractModifiableLayer> layersWithUnmodifiedChanges = new ArrayList<>();
     118            List<AbstractModifiableLayer> layersWithUnsavedChanges = new ArrayList<>();
    116119            for (Layer l: selectedLayers) {
    117120                if (!(l instanceof AbstractModifiableLayer)) {
     
    122125                        ((!odl.isSavable() && !odl.isUploadable()) ||
    123126                                odl.requiresSaveToFile() ||
    124                                 (odl.requiresUploadToServer() && !odl.isUploadDiscouraged()))) {
    125                     layersWithUnmodifiedChanges.add(odl);
     127                                odl.requiresUploadToServer())) {
     128                    layersWithUnsavedChanges.add(odl);
    126129                }
    127130            }
    128131            dialog.prepareForSavingAndUpdatingLayers(reason);
    129             if (!layersWithUnmodifiedChanges.isEmpty()) {
    130                 dialog.getModel().populate(layersWithUnmodifiedChanges);
     132            if (!layersWithUnsavedChanges.isEmpty()) {
     133                dialog.getModel().populate(layersWithUnsavedChanges);
    131134                dialog.setVisible(true);
    132135                switch(dialog.getUserAction()) {
  • trunk/test/unit/org/openstreetmap/josm/gui/io/SaveLayersDialogTest.java

    r17275 r18706  
    66import static org.junit.jupiter.api.Assertions.assertTrue;
    77
     8import java.awt.Component;
     9import java.awt.GraphicsEnvironment;
     10import java.io.File;
    811import java.util.Collections;
    912import java.util.List;
     13
    1014import javax.swing.JComponent;
    1115import javax.swing.JLabel;
     
    1317import javax.swing.JOptionPane;
    1418
    15 import org.junit.jupiter.api.extension.RegisterExtension;
     19import mockit.Invocation;
     20import mockit.Mock;
     21import mockit.MockUp;
    1622import org.junit.jupiter.api.Test;
     23import org.junit.jupiter.params.ParameterizedTest;
     24import org.junit.jupiter.params.provider.EnumSource;
     25import org.openstreetmap.josm.command.AddPrimitivesCommand;
     26import org.openstreetmap.josm.data.coor.LatLon;
    1727import org.openstreetmap.josm.data.osm.DataSet;
     28import org.openstreetmap.josm.data.osm.Node;
     29import org.openstreetmap.josm.data.osm.UploadPolicy;
    1830import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    19 import org.openstreetmap.josm.testutils.JOSMTestRules;
     31import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
    2032import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
    21 
    22 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     33import org.openstreetmap.josm.testutils.mockers.WindowMocker;
    2334
    2435/**
    2536 * Unit tests of {@link SaveLayersDialog} class.
    2637 */
     38@BasicPreferences
    2739class SaveLayersDialogTest {
    28 
    29     /**
    30      * Setup tests
    31      */
    32     @RegisterExtension
    33     @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    34     public JOSMTestRules test = new JOSMTestRules();
    35 
    3640    /**
    3741     * Test of {@link SaveLayersDialog#confirmSaveLayerInfosOK}.
     
    118122        assertTrue(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel()));
    119123    }
     124
     125    /**
     126     * Non-regression test for #22817: No warning when deleting a layer with changes and discourages upload
     127     * @param policy The upload policy to test
     128     */
     129    @ParameterizedTest
     130    @EnumSource(value = UploadPolicy.class)
     131    void testNonRegression22817(UploadPolicy policy) {
     132        final OsmDataLayer osmDataLayer = new OsmDataLayer(new DataSet(), null, null);
     133        osmDataLayer.getDataSet().setUploadPolicy(policy);
     134        // BLOCKED files don't have a way to become blocked via the UI, so they must be loaded from disk.
     135        if (policy == UploadPolicy.BLOCKED) {
     136            osmDataLayer.setAssociatedFile(new File("/dev/null"));
     137        }
     138        new AddPrimitivesCommand(Collections.singletonList(new Node(LatLon.ZERO).save()), Collections.emptyList(), osmDataLayer.getDataSet())
     139                .executeCommand();
     140        assertTrue(osmDataLayer.getDataSet().isModified());
     141        new WindowMocker();
     142        // Needed since the *first call* is to check whether we are in a headless environment
     143        new GraphicsEnvironmentMock();
     144        // Needed since we need to mock out the UI
     145        SaveLayersDialogMock saveLayersDialogMock = new SaveLayersDialogMock();
     146
     147        assertTrue(SaveLayersDialog.saveUnsavedModifications(Collections.singleton(osmDataLayer), SaveLayersDialog.Reason.DELETE));
     148        assertEquals(1, saveLayersDialogMock.getUserActionCalled, "The user should have been asked for an action on the layer");
     149    }
     150
     151    private static class GraphicsEnvironmentMock extends MockUp<GraphicsEnvironment> {
     152        @Mock
     153        public static boolean isHeadless(Invocation invocation) {
     154            return false;
     155        }
     156    }
     157
     158    private static class SaveLayersDialogMock extends MockUp<SaveLayersDialog> {
     159        private final SaveLayersModel model = new SaveLayersModel();
     160        private int getUserActionCalled = 0;
     161        @Mock
     162        public void $init(Component parent) {
     163            // Do nothing
     164        }
     165
     166        @Mock
     167        public void prepareForSavingAndUpdatingLayers(final SaveLayersDialog.Reason reason) {
     168            // Do nothing
     169        }
     170
     171        @Mock
     172        public SaveLayersModel getModel() {
     173            return this.model;
     174        }
     175
     176        @Mock
     177        public void setVisible(boolean b) {
     178            // Do nothing
     179        }
     180
     181        @Mock
     182        public SaveLayersDialog.UserAction getUserAction() {
     183            this.getUserActionCalled++;
     184            return SaveLayersDialog.UserAction.PROCEED;
     185        }
     186
     187        @Mock
     188        public void closeDialog() {
     189            // Do nothing
     190        }
     191    }
    120192}
Note: See TracChangeset for help on using the changeset viewer.