source: josm/trunk/test/unit/org/openstreetmap/josm/data/validation/tests/OpeningHourTestTest.java@ 6920

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

fix #9778, fix #9806 - access OSM API and JOSM website in HTTPS by default + other HTTPS links where applicable + update CONTRIBUTION

File size: 9.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.validation.tests;
3
4import static org.CustomMatchers.hasSize;
5import static org.CustomMatchers.isEmpty;
6import static org.hamcrest.CoreMatchers.is;
7import static org.hamcrest.CoreMatchers.not;
8import static org.junit.Assert.assertThat;
9
10import java.util.Arrays;
11import java.util.Collection;
12import java.util.LinkedHashSet;
13import java.util.List;
14import java.util.Set;
15
16import org.junit.Before;
17import org.junit.Test;
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.data.osm.Tag;
20import org.openstreetmap.josm.data.validation.Severity;
21import org.openstreetmap.josm.gui.tagging.TaggingPreset;
22import org.openstreetmap.josm.gui.tagging.TaggingPresetItem;
23import org.openstreetmap.josm.gui.tagging.TaggingPresetItems;
24import org.openstreetmap.josm.gui.tagging.TaggingPresetReader;
25
26/**
27 * JUnit Test of "Opening hours" validation test.
28 */
29public class OpeningHourTestTest {
30
31 private static final OpeningHourTest OPENING_HOUR_TEST = new OpeningHourTest();
32
33 /**
34 * Setup test.
35 */
36 @Before
37 public void setUp() throws Exception {
38 Main.initApplicationPreferences();
39 OPENING_HOUR_TEST.initialize();
40 }
41
42 @Test
43 public void testCheckOpeningHourSyntax1() throws Exception {
44 final String key = "opening_hours";
45 // frequently used tags according to https://taginfo.openstreetmap.org/keys/opening_hours#values
46 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "24/7"), isEmpty());
47 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "Mo-Fr 08:30-20:00"), isEmpty());
48 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "09:00-21:00"), isEmpty());
49 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "Su-Th sunset-24:00,04:00-sunrise; Fr-Sa sunset-sunrise"), isEmpty());
50 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "Su-Th sunset-24:00, 04:00-sunrise; Fr-Sa sunset-sunrise"), hasSize(1));
51 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "Su-Th sunset-24:00, 04:00-sunrise; Fr-Sa sunset-sunrise").get(0).getSeverity(), is(Severity.OTHER));
52 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "Su-Th sunset-24:00, 04:00-sunrise; Fr-Sa sunset-sunrise").get(0).getPrettifiedValue(), is("Su-Th sunset-24:00,04:00-sunrise; Fr-Sa sunset-sunrise"));
53 }
54
55 @Test
56 public void testCheckOpeningHourSyntax2() throws Exception {
57 final String key = "opening_hours";
58 final List<OpeningHourTest.OpeningHoursTestError> errors = OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "Mo-Tue");
59 assertThat(errors, hasSize(1));
60 assertThat(errors.get(0).getMessage(), is("Mo-Tue <--- (Please use the abbreviation \"Tu\" for \"tue\".)"));
61 assertThat(errors.get(0).getSeverity(), is(Severity.WARNING));
62 }
63
64 @Test
65 public void testCheckOpeningHourSyntax3() throws Exception {
66 final String key = "opening_hours";
67 final List<OpeningHourTest.OpeningHoursTestError> errors = OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "Sa-Su 10.00-20.00");
68 assertThat(errors, hasSize(2));
69 assertThat(errors.get(0).getMessage(), is("Sa-Su 10. <--- (Please use \":\" as hour/minute-separator)"));
70 assertThat(errors.get(0).getSeverity(), is(Severity.WARNING));
71 assertThat(errors.get(0).getPrettifiedValue(), is("Sa-Su 10:00-20:00"));
72 assertThat(errors.get(1).getMessage(), is("Sa-Su 10.00-20. <--- (Please use \":\" as hour/minute-separator)"));
73 assertThat(errors.get(1).getSeverity(), is(Severity.WARNING));
74 }
75
76 @Test
77 public void testCheckOpeningHourSyntax4() throws Exception {
78 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(null, null), isEmpty());
79 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(null, ""), isEmpty());
80 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(null, " "), isEmpty());
81 }
82
83 @Test
84 public void testCheckOpeningHourSyntax5() throws Exception {
85 final String key = "opening_hours";
86 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "badtext"), hasSize(1));
87 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "badtext").get(0).getMessage(),
88 is("opening_hours - ba <--- (Unexpected token: \"b\" This means that the syntax is not valid at that point or it is currently not supported.)"));
89 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "5.00 p.m-11.00 p.m"), hasSize(1));
90 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "5.00 p.m-11.00 p.m").get(0).getMessage(),
91 is("opening_hours - 5.00 p <--- (hyphen (-) or open end (+) in time range expected. For working with points in time, the mode for opening_hours.js has to be altered. Maybe wrong tag?)"));
92 }
93
94 @Test
95 public void testCheckOpeningHourSyntax6() throws Exception {
96 final String key = "opening_hours";
97 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "PH open \"always open on public holidays\""), isEmpty());
98 }
99
100 @Test
101 public void testCheckOpeningHourSyntax7() throws Exception {
102 final String key = "opening_hours";
103 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "9:00-18:00"), hasSize(1));
104 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "9:00-18:00").get(0).getSeverity(), is(Severity.OTHER));
105 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "9:00-18:00").get(0).getPrettifiedValue(), is("09:00-18:00"));
106 }
107
108 @Test
109 public void testCheckOpeningHourSyntaxTicket9367() throws Exception {
110 final String key = "opening_hours";
111 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "Mo,Tu 04-17").get(0).getSeverity(), is(Severity.WARNING));
112 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "Mo,Tu 04-17").get(0).getMessage(), is("Mo,Tu 04-17 <--- (Time range without minutes specified. Not very explicit! Please use this syntax instead e.g. \"12:00-14:00\".)"));
113 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "Mo,Tu 04-17").get(0).getPrettifiedValue(), is("Mo,Tu 04:00-17:00"));
114 }
115
116 @Test
117 public void testCheckServiceTimeSyntax1() throws Exception {
118 final String key = "service_times";
119 // frequently used tags according to https://taginfo.openstreetmap.org/keys/service_times#values
120 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "Su 10:00", OpeningHourTest.CheckMode.BOTH), isEmpty());
121 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "automatic", OpeningHourTest.CheckMode.BOTH), not(isEmpty()));
122 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "Mo-Sa 09:00-18:00", OpeningHourTest.CheckMode.BOTH), isEmpty());
123 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "Su 09:30; We 19:30", OpeningHourTest.CheckMode.BOTH), isEmpty());
124 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "Mo-Fr 00:00-00:30,04:00-00:30; Sa,Su,PH 00:00-24:00", OpeningHourTest.CheckMode.BOTH), isEmpty());
125 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "Mo-Fr 0:00-0:30,4:00-00:30; Sa,Su,PH 0:00-24:00", OpeningHourTest.CheckMode.BOTH), hasSize(1));
126 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "Mo-Fr 0:00-0:30,4:00-00:30; Sa,Su,PH 0:00-24:00", OpeningHourTest.CheckMode.BOTH).get(0).getPrettifiedValue(), is("Mo-Fr 00:00-00:30,04:00-00:30; Sa,Su,PH 00:00-24:00"));
127 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "Mo-Fr 0:00-0:30,4:00-00:30; Sa,Su,PH 0:00-24:00", OpeningHourTest.CheckMode.BOTH).get(0).getPrettifiedValue(), is("Mo-Fr 00:00-00:30,04:00-00:30; Sa,Su,PH 00:00-24:00"));
128 }
129
130 @Test
131 public void testCheckCollectionTimeSyntax1() throws Exception {
132 final String key = "collection_times";
133 // frequently used tags according to https://taginfo.openstreetmap.org/keys/collection_times#values
134 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "Mo-Sa 09:00", OpeningHourTest.CheckMode.BOTH), isEmpty());
135 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "fixme", OpeningHourTest.CheckMode.BOTH), not(isEmpty()));
136 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "daily", OpeningHourTest.CheckMode.BOTH), not(isEmpty()));
137 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "Mo-Fr 13:30,17:45,19:00; Sa 15:00; Su 11:00", OpeningHourTest.CheckMode.BOTH), isEmpty());
138 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "Mo-Fr 13:30, 17:45, 19:00; Sa 15:00; Su 11:00", OpeningHourTest.CheckMode.BOTH), hasSize(1));
139 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "Mo-Fr 13:30, 17:45, 19:00; Sa 15:00; Su 11:00", OpeningHourTest.CheckMode.BOTH).get(0).getSeverity(), is(Severity.OTHER));
140 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax(key, "Mo-Fr 13:30, 17:45, 19:00; Sa 15:00; Su 11:00", OpeningHourTest.CheckMode.BOTH).get(0).getPrettifiedValue(), is("Mo-Fr 13:30,17:45,19:00; Sa 15:00; Su 11:00"));
141 }
142
143 @Test
144 public void testPresetValues() throws Exception {
145 final Collection<TaggingPreset> presets = TaggingPresetReader.readFromPreferences(false, false);
146 final Set<Tag> values = new LinkedHashSet<Tag>();
147 for (final TaggingPreset p : presets) {
148 for (final TaggingPresetItem i : p.data) {
149 if (i instanceof TaggingPresetItems.KeyedItem &&
150 Arrays.asList("opening_hours", "service_times", "collection_times").contains(((TaggingPresetItems.KeyedItem) i).key)) {
151 for (final String v : ((TaggingPresetItems.KeyedItem) i).getValues()) {
152 values.add(new Tag(((TaggingPresetItems.KeyedItem) i).key, v));
153 }
154 }
155 }
156 }
157 for (final Tag t : values) {
158 final List<OpeningHourTest.OpeningHoursTestError> errors = OPENING_HOUR_TEST.checkOpeningHourSyntax(t.getKey(), t.getValue());
159 assertThat(t + " is valid", errors, isEmpty());
160 }
161 }
162}
Note: See TracBrowser for help on using the repository browser.