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

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

fix #21064 - Add JUnit 5 extension for preferences (patch by taylor.smock)

  • Property svn:eol-style set to native
File size: 7.6 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.assertNull;
7import static org.junit.jupiter.api.Assertions.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.JOptionPane;
18
19import org.junit.jupiter.api.Test;
20import org.openstreetmap.josm.TestUtils;
21import org.openstreetmap.josm.data.osm.DataSet;
22import org.openstreetmap.josm.gui.io.UploadDialog.UploadAction;
23import org.openstreetmap.josm.io.UploadStrategySpecification;
24import org.openstreetmap.josm.spi.preferences.Config;
25import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
26import org.openstreetmap.josm.testutils.mockers.WindowMocker;
27
28/**
29 * Unit tests of {@link UploadDialog} class.
30 */
31@BasicPreferences
32class UploadDialogTest {
33 private static class MockUploadDialog extends JOptionPane implements IUploadDialog {
34 private final String source;
35 private final String comment;
36
37 MockUploadDialog(final String comment, final String source) {
38 this.source = source;
39 this.comment = comment;
40 }
41
42 @Override
43 public void rememberUserInput() {
44 // Do nothing
45 }
46
47 @Override
48 public boolean isCanceled() {
49 return false;
50 }
51
52 @Override
53 public void handleMissingSource() {
54 }
55
56 @Override
57 public void handleMissingComment() {
58 }
59
60 @Override
61 public void handleIllegalChunkSize() {
62 // Do nothing
63 }
64
65 @Override
66 public UploadStrategySpecification getUploadStrategySpecification() {
67 return new UploadStrategySpecification();
68 }
69
70 @Override
71 public String getUploadSource() {
72 return source;
73 }
74
75 @Override
76 public String getUploadComment() {
77 return comment;
78 }
79
80 @Override
81 public Map<String, String> getTags(boolean keepEmpty) {
82 return new ConcurrentHashMap<>();
83 }
84
85 @Override
86 public void forceUpdateActiveField() {
87 // Do nothing
88 }
89 }
90
91 /**
92 * Test of {@link UploadDialog.CancelAction} class.
93 */
94 @Test
95 void testCancelAction() {
96 if (GraphicsEnvironment.isHeadless()) {
97 TestUtils.assumeWorkingJMockit();
98 new WindowMocker();
99 }
100 MockUploadDialog uploadDialog = new MockUploadDialog(null, null);
101 new UploadDialog.CancelAction(uploadDialog).actionPerformed(null);
102 }
103
104 /**
105 * Test of {@link UploadDialog.UploadAction#isUploadCommentTooShort} method.
106 */
107 @Test
108 void testIsUploadCommentTooShort() {
109 assertTrue(UploadDialog.UploadAction.isUploadCommentTooShort(""));
110 assertTrue(UploadDialog.UploadAction.isUploadCommentTooShort("test"));
111 assertTrue(UploadDialog.UploadAction.isUploadCommentTooShort("测试"));
112 assertFalse(UploadDialog.UploadAction.isUploadCommentTooShort("geometric corrections"));
113 assertFalse(UploadDialog.UploadAction.isUploadCommentTooShort("几何校正"));
114 // test with unassigned unicode characters ==> no unicode block
115 assertTrue(UploadDialog.UploadAction.isUploadCommentTooShort("\u0860"));
116 }
117
118 private static void doTestGetLastChangesetTagFromHistory(String historyKey, Supplier<String> methodToTest, String def) {
119 Config.getPref().putList(historyKey, null);
120 Config.getPref().putInt(BasicUploadSettingsPanel.HISTORY_LAST_USED_KEY, 0);
121 Config.getPref().putInt(BasicUploadSettingsPanel.HISTORY_MAX_AGE_KEY, 30);
122 assertNull(methodToTest.get()); // age NOK (history empty)
123 Config.getPref().putList(historyKey, Arrays.asList("foo"));
124 assertNull(methodToTest.get()); // age NOK (history not empty)
125 Config.getPref().putLong(BasicUploadSettingsPanel.HISTORY_LAST_USED_KEY, System.currentTimeMillis() / 1000);
126 assertEquals("foo", methodToTest.get()); // age OK, history not empty
127 Config.getPref().putList(historyKey, null);
128 assertEquals(def, methodToTest.get()); // age OK, history empty
129 }
130
131 /**
132 * Test of {@link UploadDialog#getLastChangesetCommentFromHistory} method.
133 */
134 @Test
135 void testGetLastChangesetCommentFromHistory() {
136 doTestGetLastChangesetTagFromHistory(
137 BasicUploadSettingsPanel.HISTORY_KEY,
138 UploadDialog::getLastChangesetCommentFromHistory,
139 null);
140 }
141
142 /**
143 * Test of {@link UploadDialog#getLastChangesetSourceFromHistory} method.
144 */
145 @Test
146 void testGetLastChangesetSourceFromHistory() {
147 doTestGetLastChangesetTagFromHistory(
148 BasicUploadSettingsPanel.SOURCE_HISTORY_KEY,
149 UploadDialog::getLastChangesetSourceFromHistory,
150 BasicUploadSettingsPanel.getDefaultSources().get(0));
151 }
152
153 private static void doTestValidateUploadTag(String prefix) {
154 List<String> def = Collections.emptyList();
155 Config.getPref().putList(prefix + ".mandatory-terms", null);
156 Config.getPref().putList(prefix + ".forbidden-terms", null);
157 assertNull(UploadAction.validateUploadTag("foo", prefix, def, def, def));
158
159 Config.getPref().putList(prefix + ".mandatory-terms", Arrays.asList("foo"));
160 assertNull(UploadAction.validateUploadTag("foo", prefix, def, def, def));
161 assertEquals("The following required terms are missing: [foo]",
162 UploadAction.validateUploadTag("bar", prefix, def, def, def));
163
164 Config.getPref().putList(prefix + ".forbidden-terms", Arrays.asList("bar"));
165 assertNull(UploadAction.validateUploadTag("foo", prefix, def, def, def));
166 assertEquals("The following forbidden terms have been found: [bar]",
167 UploadAction.validateUploadTag("foobar", prefix, def, def, def));
168 assertEquals("The following forbidden terms have been found: [bar]",
169 UploadAction.validateUploadTag("FOOBAR", prefix, def, def, def));
170
171 Config.getPref().putList(prefix + ".exception-terms", Arrays.asList("barosm"));
172 assertEquals("The following forbidden terms have been found: [bar]",
173 UploadAction.validateUploadTag("foobar", prefix, def, def, def));
174 assertEquals("The following forbidden terms have been found: [bar]",
175 UploadAction.validateUploadTag("FOOBAR", prefix, def, def, def));
176 assertNull(UploadAction.validateUploadTag("foobarosm", prefix, def, def, def));
177 assertNull(UploadAction.validateUploadTag("FOOBAROSM", prefix, def, def, def));
178 }
179
180 /**
181 * Test of {@link UploadDialog.UploadAction#validateUploadTag} method.
182 */
183 @Test
184 void testValidateUploadTag() {
185 doTestValidateUploadTag("upload.comment");
186 doTestValidateUploadTag("upload.source");
187 }
188
189 @Test
190 void testGetCommentWithDataSetHashTag() {
191 assertEquals("", UploadDialog.getCommentWithDataSetHashTag(null, null));
192 DataSet ds = new DataSet();
193 assertEquals("foo", UploadDialog.getCommentWithDataSetHashTag("foo", ds));
194 ds.getChangeSetTags().put("hashtags", "bar");
195 assertEquals("foo #bar", UploadDialog.getCommentWithDataSetHashTag("foo", ds));
196 ds.getChangeSetTags().put("hashtags", "bar;baz;#bar");
197 assertEquals("foo #bar #baz", UploadDialog.getCommentWithDataSetHashTag("foo", ds));
198 }
199}
Note: See TracBrowser for help on using the repository browser.