1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.validation.tests;
|
---|
3 |
|
---|
4 | import static org.CustomMatchers.hasSize;
|
---|
5 | import static org.CustomMatchers.isEmpty;
|
---|
6 | import static org.hamcrest.CoreMatchers.not;
|
---|
7 | import static org.hamcrest.MatcherAssert.assertThat;
|
---|
8 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
9 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
---|
10 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
11 |
|
---|
12 | import java.util.Arrays;
|
---|
13 | import java.util.Collection;
|
---|
14 | import java.util.LinkedHashSet;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.Locale;
|
---|
17 | import java.util.Set;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
20 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
21 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
22 | import org.openstreetmap.josm.data.osm.Node;
|
---|
23 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
24 | import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper;
|
---|
25 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
26 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
27 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
|
---|
28 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetItem;
|
---|
29 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetReader;
|
---|
30 | import org.openstreetmap.josm.gui.tagging.presets.items.KeyedItem;
|
---|
31 | import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
|
---|
32 | import org.openstreetmap.josm.testutils.annotations.I18n;
|
---|
33 | import org.openstreetmap.josm.tools.Logging;
|
---|
34 |
|
---|
35 | import org.junit.jupiter.api.BeforeEach;
|
---|
36 | import org.junit.jupiter.api.Test;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * JUnit Test of "Opening hours" validation test.
|
---|
40 | * @see OpeningHourTest
|
---|
41 | */
|
---|
42 | @I18n
|
---|
43 | @BasicPreferences
|
---|
44 | class OpeningHourTestTest {
|
---|
45 | private OpeningHourTest openingHourTest;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Setup test.
|
---|
49 | * @throws Exception if test cannot be initialized
|
---|
50 | */
|
---|
51 | @BeforeEach
|
---|
52 | public void setUp() throws Exception {
|
---|
53 | openingHourTest = new OpeningHourTest();
|
---|
54 | openingHourTest.initialize();
|
---|
55 | ValidatorPrefHelper.PREF_OTHER.put(true);
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Test #1 of opening_hours syntax.
|
---|
60 | */
|
---|
61 | @Test
|
---|
62 | void testCheckOpeningHourSyntax1() {
|
---|
63 | final String key = "opening_hours";
|
---|
64 | // frequently used tags according to https://taginfo.openstreetmap.org/keys/opening_hours#values
|
---|
65 | assertThat(checkOpeningHourSyntax(key, "24/7"), isEmpty());
|
---|
66 | assertThat(checkOpeningHourSyntax(key, "Mo-Fr 08:30-20:00"), isEmpty());
|
---|
67 | assertThat(checkOpeningHourSyntax(key, "Mo-Fr sunrise-sunset"), isEmpty());
|
---|
68 | assertThat(checkOpeningHourSyntax(key, "09:00-21:00"), isEmpty());
|
---|
69 | assertThat(checkOpeningHourSyntax(key, "Su-Th sunset-24:00,04:00-sunrise; Fr-Sa sunset-sunrise"), isEmpty());
|
---|
70 | assertThat(checkOpeningHourSyntax(key, "PH Su 10:00-12:00"), isEmpty()); // see #19743
|
---|
71 | assertThat(checkOpeningHourSyntax(key, "SH Mo-Fr 09:33-15:35"), isEmpty()); // see #19743
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Test translated messages.
|
---|
76 | */
|
---|
77 | @Test
|
---|
78 | void testI18n() {
|
---|
79 | final String key = "opening_hours";
|
---|
80 | String value = ".";
|
---|
81 | assertEquals(String.format("Vorgefunden wurde: \".\" \". \" in Zeile 0, Spalte 0%nErwartet wurde: <EOF>"),
|
---|
82 | checkOpeningHourSyntax(key, value, Locale.GERMAN).get(0).getDescription());
|
---|
83 | assertEquals(String.format("Encountered: \".\" \". \" at line 0, column 0%nWas expecting: <EOF>"),
|
---|
84 | checkOpeningHourSyntax(key, value, Locale.ENGLISH).get(0).getDescription());
|
---|
85 | value = "Mon-Thu 12-18";
|
---|
86 | assertEquals("Wochentag mit 3 Buchstaben in Zeile 1, Spalte 4; Wochentag mit 3 Buchstaben in Zeile 1, Spalte 9; Stunden ohne Minuten",
|
---|
87 | checkOpeningHourSyntax(key, value, Locale.GERMAN).get(0).getDescription());
|
---|
88 | assertEquals("Three character weekday at line 1, column 4; Three character weekday at line 1, column 9; Hours without minutes",
|
---|
89 | checkOpeningHourSyntax(key, value, Locale.ENGLISH).get(0).getDescription());
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Test #2 of opening_hours syntax.
|
---|
94 | */
|
---|
95 | @Test
|
---|
96 | void testCheckOpeningHourSyntax2() {
|
---|
97 | final String key = "opening_hours";
|
---|
98 | final List<TestError> errors = checkOpeningHourSyntax(key, "Mo-Tue");
|
---|
99 | assertThat(errors, hasSize(1));
|
---|
100 | assertFixEquals("Mo-Tu", errors.get(0));
|
---|
101 | assertEquals("Three character weekday at line 1, column 6", errors.get(0).getDescription());
|
---|
102 | assertEquals(Severity.WARNING, errors.get(0).getSeverity());
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Test #3 of opening_hours syntax.
|
---|
107 | */
|
---|
108 | @Test
|
---|
109 | void testCheckOpeningHourSyntax3() {
|
---|
110 | final String key = "opening_hours";
|
---|
111 | final List<TestError> errors = checkOpeningHourSyntax(key, "Sa-Su 10.00-20.00");
|
---|
112 | assertThat(errors, hasSize(1));
|
---|
113 | assertFixEquals("Sa-Su 10:00-20:00", errors.get(0));
|
---|
114 | assertEquals("Invalid minutes at line 1, column 12; Invalid minutes at line 1, column 17", errors.get(0).getDescription());
|
---|
115 | assertEquals(Severity.WARNING, errors.get(0).getSeverity());
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Test #4 of opening_hours syntax.
|
---|
120 | */
|
---|
121 | @Test
|
---|
122 | void testCheckOpeningHourSyntax4() {
|
---|
123 | assertThat(checkOpeningHourSyntax(null, null), isEmpty());
|
---|
124 | assertThat(checkOpeningHourSyntax(null, ""), isEmpty());
|
---|
125 | assertEquals("opening_hours value can be prettified",
|
---|
126 | checkOpeningHourSyntax("opening_hours", " ").get(0).getDescription());
|
---|
127 | assertEquals("null value can be prettified",
|
---|
128 | checkOpeningHourSyntax(null, " ").get(0).getDescription());
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Test #5 of opening_hours syntax.
|
---|
133 | */
|
---|
134 | @Test
|
---|
135 | void testCheckOpeningHourSyntax5() {
|
---|
136 | final String key = "opening_hours";
|
---|
137 | assertThat(checkOpeningHourSyntax(key, "badtext"), hasSize(1));
|
---|
138 | assertEquals(String.format("Encountered: <UNEXPECTED_CHAR> \"b \" at line 0, column 0%nWas expecting: <EOF>"),
|
---|
139 | checkOpeningHourSyntax(key, "badtext").get(0).getDescription().trim());
|
---|
140 | assertThat(checkOpeningHourSyntax(key, "5.00 p.m-11.00 p.m"), hasSize(1));
|
---|
141 | assertEquals(String.format("Encountered: <UNEXPECTED_CHAR> \"p \" at line 1, column 3%nWas expecting: <EOF>; " +
|
---|
142 | "Encountered: <UNEXPECTED_CHAR> \"p \" at line 1, column 13%n" +
|
---|
143 | "Was expecting: <EOF>"),
|
---|
144 | checkOpeningHourSyntax(key, "5.00 p.m-11.00 p.m").get(0).getDescription());
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Test #6 of opening_hours syntax.
|
---|
149 | */
|
---|
150 | @Test
|
---|
151 | void testCheckOpeningHourSyntax6() {
|
---|
152 | final String key = "opening_hours";
|
---|
153 | assertThat(checkOpeningHourSyntax(key, "PH open \"always open on public holidays\""), isEmpty());
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Test #7 of opening_hours syntax.
|
---|
158 | */
|
---|
159 | @Test
|
---|
160 | void testCheckOpeningHourSyntax7() {
|
---|
161 | final String key = "opening_hours";
|
---|
162 | assertThat(checkOpeningHourSyntax(key, "9:00-18:00"), hasSize(1));
|
---|
163 | assertEquals(Severity.OTHER, checkOpeningHourSyntax(key, "9:00-18:00").get(0).getSeverity());
|
---|
164 | assertFixEquals("09:00-18:00", checkOpeningHourSyntax(key, "9:00-18:00").get(0));
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Non-regression Test of opening_hours syntax for bug #9367.
|
---|
169 | */
|
---|
170 | @Test
|
---|
171 | void testCheckOpeningHourSyntaxTicket9367() {
|
---|
172 | final String key = "opening_hours";
|
---|
173 | assertEquals(Severity.WARNING, checkOpeningHourSyntax(key, "Mo,Tu 04-17").get(0).getSeverity());
|
---|
174 | assertEquals("Hours without minutes",
|
---|
175 | checkOpeningHourSyntax(key, "Mo,Tu 04-17").get(0).getDescription());
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Test #1 of service_times syntax.
|
---|
180 | */
|
---|
181 | @Test
|
---|
182 | void testCheckServiceTimeSyntax1() {
|
---|
183 | final String key = "service_times";
|
---|
184 | // frequently used tags according to https://taginfo.openstreetmap.org/keys/service_times#values
|
---|
185 | assertThat(checkOpeningHourSyntax(key, "Su 10:00"), isEmpty());
|
---|
186 | assertThat(checkOpeningHourSyntax(key, "automatic"), not(isEmpty()));
|
---|
187 | assertThat(checkOpeningHourSyntax(key, "Mo-Sa 09:00-18:00"), isEmpty());
|
---|
188 | assertThat(checkOpeningHourSyntax(key, "Su 09:30; We 19:30"), isEmpty());
|
---|
189 | // assertThat(checkOpeningHourSyntax(key, "Mo-Fr 00:00-00:30,04:00-00:30; Sa,Su,PH 00:00-24:00"), isEmpty());
|
---|
190 | assertThat(checkOpeningHourSyntax(key, "Mo-Fr 0:00-0:30,4:00-00:30; Sa,Su,PH 0:00-24:00"), hasSize(1));
|
---|
191 | assertFixEquals("Mo-Fr 00:00-00:30,04:00-00:30; Sa,Su,PH 00:00-24:00",
|
---|
192 | checkOpeningHourSyntax(key, "Mo-Fr 0:00-0:30,4:00-00:30; Sa,Su,PH 0:00-24:00").get(0));
|
---|
193 | assertFixEquals("Mo-Fr 00:00-00:30,04:00-00:30; Sa,Su,PH 00:00-24:00",
|
---|
194 | checkOpeningHourSyntax(key, "Mo-Fr 0:00-0:30,4:00-00:30; Sa,Su,PH 0:00-24:00").get(0));
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Test #1 of collection_times syntax.
|
---|
199 | */
|
---|
200 | @Test
|
---|
201 | void testCheckCollectionTimeSyntax1() {
|
---|
202 | final String key = "collection_times";
|
---|
203 | // frequently used tags according to https://taginfo.openstreetmap.org/keys/collection_times#values
|
---|
204 | assertThat(checkOpeningHourSyntax(key, "Mo-Sa 09:00"), isEmpty());
|
---|
205 | assertThat(checkOpeningHourSyntax(key, "fixme"), not(isEmpty()));
|
---|
206 | assertThat(checkOpeningHourSyntax(key, "daily"), not(isEmpty()));
|
---|
207 | assertThat(checkOpeningHourSyntax(key, "Mo-Fr 13:30,17:45,19:00; Sa 15:00; Su 11:00"), isEmpty());
|
---|
208 | assertThat(checkOpeningHourSyntax(key, "Mo-Fr 13:30, 17:45, 19:00; Sa 15:00; Su 11:00"), hasSize(1));
|
---|
209 | assertEquals(Severity.OTHER,
|
---|
210 | checkOpeningHourSyntax(key, "Mo-Fr 13:30, 17:45, 19:00; Sa 15:00; Su 11:00").get(0).getSeverity());
|
---|
211 | assertFixEquals("Mo-Fr 13:30,17:45,19:00; Sa 15:00; Su 11:00",
|
---|
212 | checkOpeningHourSyntax(key, "Mo-Fr 13:30, 17:45, 19:00; Sa 15:00; Su 11:00").get(0));
|
---|
213 | }
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Tests that predefined values in presets are correct.
|
---|
217 | */
|
---|
218 | @Test
|
---|
219 | void testPresetValues() {
|
---|
220 | final Collection<TaggingPreset> presets = TaggingPresetReader.readFromPreferences(false, false);
|
---|
221 | final Set<Tag> values = new LinkedHashSet<>();
|
---|
222 | for (final TaggingPreset p : presets) {
|
---|
223 | for (final TaggingPresetItem i : p.data) {
|
---|
224 | if (i instanceof KeyedItem &&
|
---|
225 | Arrays.asList("opening_hours", "service_times", "collection_times").contains(((KeyedItem) i).key)) {
|
---|
226 | for (final String v : ((KeyedItem) i).getValues()) {
|
---|
227 | values.add(new Tag(((KeyedItem) i).key, v));
|
---|
228 | }
|
---|
229 | }
|
---|
230 | }
|
---|
231 | }
|
---|
232 | for (final Tag t : values) {
|
---|
233 | final List<TestError> errors = checkOpeningHourSyntax(t.getKey(), t.getValue());
|
---|
234 | if (!errors.isEmpty() && errors.get(0).getDescription().startsWith("Holiday after weekday")) {
|
---|
235 | continue;
|
---|
236 | }
|
---|
237 | assertThat(t + " is valid", errors, isEmpty());
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/17932">Bug #17932</a>.
|
---|
243 | */
|
---|
244 | @Test
|
---|
245 | void testTicket17932() {
|
---|
246 | Logging.clearLastErrorAndWarnings();
|
---|
247 | assertTrue(checkOpeningHourSyntax("opening_hours", "SH off").isEmpty());
|
---|
248 | }
|
---|
249 |
|
---|
250 | private List<TestError> checkOpeningHourSyntax(final String key, final String value, final Locale... locales) {
|
---|
251 | final Locale locale = locales.length > 0 ? locales[0] : Locale.ENGLISH;
|
---|
252 | final Node node = new Node(LatLon.ZERO);
|
---|
253 | node.put(key, value);
|
---|
254 | new DataSet(node);
|
---|
255 | return openingHourTest.checkOpeningHourSyntax(key, value, node, locale);
|
---|
256 | }
|
---|
257 |
|
---|
258 | private static void assertFixEquals(String value, TestError error) {
|
---|
259 | assertNotNull(error.getFix(), "fix is not null");
|
---|
260 | assertTrue(error.getFix() instanceof ChangePropertyCommand, "fix is ChangePropertyCommand");
|
---|
261 | final ChangePropertyCommand command = (ChangePropertyCommand) error.getFix();
|
---|
262 | assertEquals(1, command.getTags().size());
|
---|
263 | assertEquals(value, command.getTags().values().iterator().next());
|
---|
264 | }
|
---|
265 | }
|
---|