source: josm/trunk/test/unit/org/openstreetmap/josm/gui/io/UploadDialogTest.java@ 15015

Last change on this file since 15015 was 15015, checked in by Don-vip, 5 years ago

see #17634 - use case insensitive comparison, forbid google as source

  • Property svn:eol-style set to native
File size: 11.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertFalse;
6import static org.junit.Assert.assertNull;
7import static org.junit.Assert.assertTrue;
8
9import java.awt.GraphicsEnvironment;
10import java.util.Arrays;
11import java.util.Collections;
12import java.util.List;
13import java.util.Map;
14import java.util.concurrent.ConcurrentHashMap;
15import java.util.function.Supplier;
16
17import javax.swing.JButton;
18import javax.swing.JOptionPane;
19
20import org.junit.Rule;
21import org.junit.Test;
22import org.openstreetmap.josm.TestUtils;
23import org.openstreetmap.josm.gui.ExtendedDialog;
24import org.openstreetmap.josm.gui.io.UploadDialog.UploadAction;
25import org.openstreetmap.josm.io.UploadStrategySpecification;
26import org.openstreetmap.josm.spi.preferences.Config;
27import org.openstreetmap.josm.testutils.JOSMTestRules;
28import org.openstreetmap.josm.testutils.mockers.ExtendedDialogMocker;
29import org.openstreetmap.josm.testutils.mockers.WindowMocker;
30
31import com.google.common.collect.ImmutableMap;
32
33import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
34import mockit.Invocation;
35import mockit.Mock;
36
37/**
38 * Unit tests of {@link UploadDialog} class.
39 */
40public class UploadDialogTest {
41
42 /**
43 * Setup tests
44 */
45 @Rule
46 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
47 public JOSMTestRules test = new JOSMTestRules().preferences();
48
49 private static class MockUploadDialog extends JOptionPane implements IUploadDialog {
50 private final String source;
51 private final String comment;
52
53 public int handleMissingCommentCalls;
54 public int handleMissingSourceCalls;
55
56 MockUploadDialog(final String comment, final String source) {
57 this.source = source;
58 this.comment = comment;
59 }
60
61 @Override
62 public void rememberUserInput() {
63 // Do nothing
64 }
65
66 @Override
67 public boolean isCanceled() {
68 return false;
69 }
70
71 @Override
72 public void handleMissingSource() {
73 this.handleMissingSourceCalls += 1;
74 }
75
76 @Override
77 public void handleMissingComment() {
78 this.handleMissingCommentCalls += 1;
79 }
80
81 @Override
82 public void handleIllegalChunkSize() {
83 // Do nothing
84 }
85
86 @Override
87 public UploadStrategySpecification getUploadStrategySpecification() {
88 return new UploadStrategySpecification();
89 }
90
91 @Override
92 public String getUploadSource() {
93 return source;
94 }
95
96 @Override
97 public String getUploadComment() {
98 return comment;
99 }
100
101 @Override
102 public Map<String, String> getTags(boolean keepEmpty) {
103 return new ConcurrentHashMap<>();
104 }
105
106 @Override
107 public void forceUpdateActiveField() {
108 // Do nothing
109 }
110 }
111
112 /**
113 * Test of {@link UploadDialog.CancelAction} class.
114 */
115 @Test
116 public void testCancelAction() {
117 if (GraphicsEnvironment.isHeadless()) {
118 TestUtils.assumeWorkingJMockit();
119 new WindowMocker();
120 }
121 MockUploadDialog uploadDialog = new MockUploadDialog(null, null);
122 new UploadDialog.CancelAction(uploadDialog).actionPerformed(null);
123 }
124
125 /**
126 * Test of {@link UploadDialog.UploadAction} class.
127 */
128 @Test
129 public void testUploadAction() {
130 TestUtils.assumeWorkingJMockit();
131 ExtendedDialogMocker edMocker = new ExtendedDialogMocker(
132 ImmutableMap.<String, Object>of(
133 "<html>Your upload comment is <i>empty</i>, or <i>very short</i>.<br /><br />This is "
134 + "technically allowed, but please consider that many users who are<br />watching changes "
135 + "in their area depend on meaningful changeset comments<br />to understand what is going "
136 + "on!<br /><br />If you spend a minute now to explain your change, you will make life<br />"
137 + "easier for many other mappers.</html>", "Revise",
138 "<html>You did not specify a source for your changes.<br />It is technically allowed, "
139 + "but this information helps<br />other users to understand the origins of the data."
140 + "<br /><br />If you spend a minute now to explain your change, you will make life"
141 + "<br />easier for many other mappers.</html>", "Revise"
142 )
143 ) {
144 @Mock
145 void setupDialog(Invocation invocation) throws Exception {
146 if (GraphicsEnvironment.isHeadless()) {
147 final int nButtons = ((String[]) TestUtils.getPrivateField(
148 ExtendedDialog.class, invocation.getInvokedInstance(), "bTexts")).length;
149 @SuppressWarnings("unchecked")
150 final List<JButton> buttons = (List<JButton>) TestUtils.getPrivateField(
151 ExtendedDialog.class, invocation.getInvokedInstance(), "buttons");
152
153 for (int i = 0; i < nButtons; i++) {
154 buttons.add(new JButton());
155 }
156 } else {
157 invocation.proceed();
158 }
159 }
160 };
161
162 MockUploadDialog uploadDialog = new MockUploadDialog("comment", "source");
163 new UploadDialog.UploadAction(uploadDialog).actionPerformed(null);
164
165 assertEquals(1, uploadDialog.handleMissingCommentCalls);
166 assertEquals(0, uploadDialog.handleMissingSourceCalls);
167 assertEquals(1, edMocker.getInvocationLog().size());
168 Object[] invocationLogEntry = edMocker.getInvocationLog().get(0);
169 assertEquals(1, (int) invocationLogEntry[0]);
170 assertEquals("Please revise upload comment", invocationLogEntry[2]);
171 edMocker.resetInvocationLog();
172
173 uploadDialog = new MockUploadDialog("", "source");
174 new UploadDialog.UploadAction(uploadDialog).actionPerformed(null);
175
176 assertEquals(1, uploadDialog.handleMissingCommentCalls);
177 assertEquals(0, uploadDialog.handleMissingSourceCalls);
178 assertEquals(1, edMocker.getInvocationLog().size());
179 invocationLogEntry = edMocker.getInvocationLog().get(0);
180 assertEquals(1, (int) invocationLogEntry[0]);
181 assertEquals("Please revise upload comment", invocationLogEntry[2]);
182 edMocker.resetInvocationLog();
183
184 uploadDialog = new MockUploadDialog("comment", "");
185 new UploadDialog.UploadAction(uploadDialog).actionPerformed(null);
186
187 assertEquals(1, uploadDialog.handleMissingCommentCalls);
188 assertEquals(0, uploadDialog.handleMissingSourceCalls);
189 assertEquals(1, edMocker.getInvocationLog().size());
190 invocationLogEntry = edMocker.getInvocationLog().get(0);
191 assertEquals(1, (int) invocationLogEntry[0]);
192 assertEquals("Please revise upload comment", invocationLogEntry[2]);
193 edMocker.resetInvocationLog();
194
195 uploadDialog = new MockUploadDialog("a comment long enough", "");
196 new UploadDialog.UploadAction(uploadDialog).actionPerformed(null);
197
198 assertEquals(0, uploadDialog.handleMissingCommentCalls);
199 assertEquals(1, uploadDialog.handleMissingSourceCalls);
200 assertEquals(1, edMocker.getInvocationLog().size());
201 invocationLogEntry = edMocker.getInvocationLog().get(0);
202 assertEquals(1, (int) invocationLogEntry[0]);
203 assertEquals("Please specify a changeset source", invocationLogEntry[2]);
204 edMocker.resetInvocationLog();
205
206 uploadDialog = new MockUploadDialog("a comment long enough", "a source long enough");
207 new UploadDialog.UploadAction(uploadDialog).actionPerformed(null);
208
209 assertEquals(0, uploadDialog.handleMissingCommentCalls);
210 assertEquals(0, uploadDialog.handleMissingSourceCalls);
211 assertEquals(0, edMocker.getInvocationLog().size());
212 }
213
214 /**
215 * Test of {@link UploadDialog.UploadAction#isUploadCommentTooShort} method.
216 */
217 @Test
218 public void testIsUploadCommentTooShort() {
219 assertTrue(UploadDialog.UploadAction.isUploadCommentTooShort(""));
220 assertTrue(UploadDialog.UploadAction.isUploadCommentTooShort("test"));
221 assertTrue(UploadDialog.UploadAction.isUploadCommentTooShort("测试"));
222 assertFalse(UploadDialog.UploadAction.isUploadCommentTooShort("geometric corrections"));
223 assertFalse(UploadDialog.UploadAction.isUploadCommentTooShort("几何校正"));
224 // test with unassigned unicode characters ==> no unicode block
225 assertTrue(UploadDialog.UploadAction.isUploadCommentTooShort("\u0860"));
226 }
227
228 private static void doTestGetLastChangesetTagFromHistory(String historyKey, Supplier<String> methodToTest, String def) {
229 Config.getPref().putList(historyKey, null);
230 Config.getPref().putInt(BasicUploadSettingsPanel.HISTORY_LAST_USED_KEY, 0);
231 Config.getPref().putInt(BasicUploadSettingsPanel.HISTORY_MAX_AGE_KEY, 30);
232 assertNull(methodToTest.get()); // age NOK (history empty)
233 Config.getPref().putList(historyKey, Arrays.asList("foo"));
234 assertNull(methodToTest.get()); // age NOK (history not empty)
235 Config.getPref().putLong(BasicUploadSettingsPanel.HISTORY_LAST_USED_KEY, System.currentTimeMillis() / 1000);
236 assertEquals("foo", methodToTest.get()); // age OK, history not empty
237 Config.getPref().putList(historyKey, null);
238 assertEquals(def, methodToTest.get()); // age OK, history empty
239 }
240
241 /**
242 * Test of {@link UploadDialog#getLastChangesetCommentFromHistory} method.
243 */
244 @Test
245 public void testGetLastChangesetCommentFromHistory() {
246 doTestGetLastChangesetTagFromHistory(
247 BasicUploadSettingsPanel.HISTORY_KEY,
248 UploadDialog::getLastChangesetCommentFromHistory,
249 null);
250 }
251
252 /**
253 * Test of {@link UploadDialog#getLastChangesetSourceFromHistory} method.
254 */
255 @Test
256 public void testGetLastChangesetSourceFromHistory() {
257 doTestGetLastChangesetTagFromHistory(
258 BasicUploadSettingsPanel.SOURCE_HISTORY_KEY,
259 UploadDialog::getLastChangesetSourceFromHistory,
260 BasicUploadSettingsPanel.getDefaultSources().get(0));
261 }
262
263 private static void doTestValidateUploadTag(String prefix) {
264 List<String> def = Collections.emptyList();
265 Config.getPref().putList(prefix + ".mandatory-terms", null);
266 Config.getPref().putList(prefix + ".forbidden-terms", null);
267 assertNull(UploadAction.validateUploadTag("foo", prefix, def, def));
268
269 Config.getPref().putList(prefix + ".mandatory-terms", Arrays.asList("foo"));
270 assertNull(UploadAction.validateUploadTag("foo", prefix, def, def));
271 assertEquals("The following required terms are missing: [foo]",
272 UploadAction.validateUploadTag("bar", prefix, def, def));
273
274 Config.getPref().putList(prefix + ".forbidden-terms", Arrays.asList("bar"));
275 assertNull(UploadAction.validateUploadTag("foo", prefix, def, def));
276 assertEquals("The following forbidden terms have been found: [bar]",
277 UploadAction.validateUploadTag("foobar", prefix, def, def));
278 assertEquals("The following forbidden terms have been found: [bar]",
279 UploadAction.validateUploadTag("FOOBAR", prefix, def, def));
280 }
281
282 /**
283 * Test of {@link UploadDialog.UploadAction#validateUploadTag} method.
284 */
285 @Test
286 public void testValidateUploadTag() {
287 doTestValidateUploadTag("upload.comment");
288 doTestValidateUploadTag("upload.source");
289 }
290}
Note: See TracBrowser for help on using the repository browser.