1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertFalse;
|
---|
6 | import static org.junit.Assert.assertNull;
|
---|
7 | import static org.junit.Assert.assertTrue;
|
---|
8 |
|
---|
9 | import java.awt.GraphicsEnvironment;
|
---|
10 | import java.util.Arrays;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Map;
|
---|
14 | import java.util.concurrent.ConcurrentHashMap;
|
---|
15 | import java.util.function.Supplier;
|
---|
16 |
|
---|
17 | import javax.swing.JButton;
|
---|
18 | import javax.swing.JOptionPane;
|
---|
19 |
|
---|
20 | import org.junit.Rule;
|
---|
21 | import org.junit.Test;
|
---|
22 | import org.openstreetmap.josm.TestUtils;
|
---|
23 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
24 | import org.openstreetmap.josm.gui.io.UploadDialog.UploadAction;
|
---|
25 | import org.openstreetmap.josm.io.UploadStrategySpecification;
|
---|
26 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
27 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
28 | import org.openstreetmap.josm.testutils.mockers.ExtendedDialogMocker;
|
---|
29 | import org.openstreetmap.josm.testutils.mockers.WindowMocker;
|
---|
30 |
|
---|
31 | import com.google.common.collect.ImmutableMap;
|
---|
32 |
|
---|
33 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
34 | import mockit.Invocation;
|
---|
35 | import mockit.Mock;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Unit tests of {@link UploadDialog} class.
|
---|
39 | */
|
---|
40 | public 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 | }
|
---|