source: josm/trunk/test/unit/org/openstreetmap/josm/gui/io/SaveLayersDialogTest.java@ 19050

Last change on this file since 19050 was 19050, checked in by taylor.smock, 4 weeks ago

Revert most var changes from r19048, fix most new compile warnings and checkstyle issues

Also, document why various ErrorProne checks were originally disabled and fix
generic SonarLint issues.

  • Property svn:eol-style set to native
File size: 8.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.assertFalse;
6import static org.junit.jupiter.api.Assertions.assertTrue;
7
8import java.awt.Component;
9import java.awt.GraphicsEnvironment;
10import java.io.File;
11import java.io.FileInputStream;
12import java.io.IOException;
13import java.io.InputStream;
14import java.util.Collections;
15import java.util.List;
16
17import javax.swing.JComponent;
18import javax.swing.JLabel;
19import javax.swing.JList;
20import javax.swing.JOptionPane;
21
22import org.junit.jupiter.api.Test;
23import org.junit.jupiter.params.ParameterizedTest;
24import org.junit.jupiter.params.provider.EnumSource;
25import org.openstreetmap.josm.TestUtils;
26import org.openstreetmap.josm.data.osm.DataSet;
27import org.openstreetmap.josm.data.osm.UploadPolicy;
28import org.openstreetmap.josm.gui.layer.OsmDataLayer;
29import org.openstreetmap.josm.io.IllegalDataException;
30import org.openstreetmap.josm.io.OsmReader;
31import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
32import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
33import org.openstreetmap.josm.testutils.mockers.WindowMocker;
34
35import mockit.Invocation;
36import mockit.Mock;
37import mockit.MockUp;
38
39/**
40 * Unit tests of {@link SaveLayersDialog} class.
41 */
42@BasicPreferences
43class SaveLayersDialogTest {
44 /**
45 * Test of {@link SaveLayersDialog#confirmSaveLayerInfosOK}.
46 */
47 @Test
48 void testConfirmSaveLayerInfosOK() {
49 final List<SaveLayerInfo> list = Collections.singletonList(new SaveLayerInfo(new OsmDataLayer(new DataSet(), null, null)));
50
51 final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker() {
52 @Override
53 protected void act(final Object message) {
54 // use this opportunity to assert that our SaveLayerInfo is the single option in the JList
55 @SuppressWarnings("unchecked")
56 final JList<SaveLayerInfo> jList = (JList<SaveLayerInfo>) ((JComponent) message).getComponent(1);
57 assertEquals(1, jList.getModel().getSize());
58 assertEquals(list.get(0), jList.getModel().getElementAt(0));
59 }
60
61 @Override
62 protected String getStringFromMessage(final Object message) {
63 return ((JLabel) ((JComponent) message).getComponent(0)).getText();
64 }
65 };
66
67 jopsMocker.getMockResultMap().put(
68 "<html>1 layer has unresolved conflicts.<br>Either resolve them first or discard the "
69 + "modifications.<br>Layer with conflicts:</html>", JOptionPane.OK_OPTION
70 );
71
72 assertFalse(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel() {
73 @Override
74 public List<SaveLayerInfo> getLayersWithConflictsAndUploadRequest() {
75 return list;
76 }
77 }));
78
79 assertEquals(1, jopsMocker.getInvocationLog().size());
80 Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0);
81 assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]);
82 assertEquals("Unsaved data and conflicts", invocationLogEntry[2]);
83
84 jopsMocker.resetInvocationLog();
85 jopsMocker.getMockResultMap().clear();
86 jopsMocker.getMockResultMap().put(
87 "<html>1 layer needs saving but has no associated file.<br>Either select a file for this "
88 + "layer or discard the changes.<br>Layer without a file:</html>", JOptionPane.OK_OPTION
89 );
90
91 assertFalse(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel() {
92 @Override
93 public List<SaveLayerInfo> getLayersWithoutFilesAndSaveRequest() {
94 return list;
95 }
96 }));
97
98 assertEquals(1, jopsMocker.getInvocationLog().size());
99 invocationLogEntry = jopsMocker.getInvocationLog().get(0);
100 assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]);
101 assertEquals("Unsaved data and missing associated file", invocationLogEntry[2]);
102
103 jopsMocker.resetInvocationLog();
104 jopsMocker.getMockResultMap().clear();
105 jopsMocker.getMockResultMap().put(
106 "<html>1 layer needs saving but has an associated file<br>which cannot be written.<br>Either "
107 + "select another file for this layer or discard the changes.<br>Layer with a non-writable "
108 + "file:</html>", JOptionPane.OK_OPTION
109 );
110
111 assertFalse(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel() {
112 @Override
113 public List<SaveLayerInfo> getLayersWithIllegalFilesAndSaveRequest() {
114 return list;
115 }
116 }));
117
118 assertEquals(1, jopsMocker.getInvocationLog().size());
119 invocationLogEntry = jopsMocker.getInvocationLog().get(0);
120 assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]);
121 assertEquals("Unsaved data non-writable files", invocationLogEntry[2]);
122
123 jopsMocker.resetInvocationLog();
124 jopsMocker.getMockResultMap().clear();
125
126 assertTrue(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel()));
127 }
128
129 /**
130 * Non-regression test for #22817: No warning when deleting a layer with changes and discourages upload
131 * @param policy The upload policy to test
132 * @throws IOException if an error occurs
133 * @throws IllegalDataException if an error occurs
134 */
135 @ParameterizedTest
136 @EnumSource(value = UploadPolicy.class)
137 void testNonRegression22817(UploadPolicy policy) throws IOException, IllegalDataException {
138 File file = new File(TestUtils.getRegressionDataFile(22817, "data.osm"));
139 InputStream is = new FileInputStream(file);
140 final OsmDataLayer osmDataLayer = new OsmDataLayer(OsmReader.parseDataSet(is, null), null, null);
141 osmDataLayer.onPostLoadFromFile();
142 osmDataLayer.getDataSet().setUploadPolicy(policy);
143 osmDataLayer.setAssociatedFile(file);
144 assertTrue(osmDataLayer.getDataSet().isModified());
145 assertFalse(osmDataLayer.requiresSaveToFile());
146 assertTrue(osmDataLayer.getDataSet().requiresUploadToServer());
147 assertEquals(policy != UploadPolicy.BLOCKED, osmDataLayer.requiresUploadToServer());
148 assertEquals(policy != UploadPolicy.BLOCKED, osmDataLayer.isUploadable());
149 new WindowMocker();
150 // Needed since the *first call* is to check whether we are in a headless environment
151 new GraphicsEnvironmentMock();
152 // Needed since we need to mock out the UI
153 SaveLayersDialogMock saveLayersDialogMock = new SaveLayersDialogMock();
154 assertTrue(SaveLayersDialog.saveUnsavedModifications(Collections.singleton(osmDataLayer), SaveLayersDialog.Reason.DELETE));
155 int res = saveLayersDialogMock.getUserActionCalled;
156 if (policy == UploadPolicy.NORMAL) {
157 assertEquals(1, res, "The user should have been asked for an action on the layer");
158 } else {
159 assertEquals(0, res, "The user should not have been asked for an action on the layer");
160
161 }
162 }
163
164 private static final class GraphicsEnvironmentMock extends MockUp<GraphicsEnvironment> {
165 @Mock
166 public static boolean isHeadless(Invocation invocation) {
167 return false;
168 }
169 }
170
171 private static final class SaveLayersDialogMock extends MockUp<SaveLayersDialog> {
172 private final SaveLayersModel model = new SaveLayersModel();
173 private int getUserActionCalled = 0;
174 @Mock
175 public void $init(Component parent) {
176 // Do nothing
177 }
178
179 @Mock
180 public void prepareForSavingAndUpdatingLayers(final SaveLayersDialog.Reason reason) {
181 // Do nothing
182 }
183
184 @Mock
185 public SaveLayersModel getModel() {
186 return this.model;
187 }
188
189 @Mock
190 public void setVisible(boolean b) {
191 // Do nothing
192 }
193
194 @Mock
195 public SaveLayersDialog.UserAction getUserAction() {
196 this.getUserActionCalled++;
197 return SaveLayersDialog.UserAction.PROCEED;
198 }
199
200 @Mock
201 public void closeDialog() {
202 // Do nothing
203 }
204 }
205}
Note: See TracBrowser for help on using the repository browser.