Changeset 6377 in josm
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/validation/tests/OpeningHourTest.java
r6376 r6377 18 18 19 19 import org.openstreetmap.josm.Main; 20 import org.openstreetmap.josm.command.ChangePropertyCommand; 20 21 import org.openstreetmap.josm.data.osm.Node; 21 22 import org.openstreetmap.josm.data.osm.OsmPrimitive; 22 23 import org.openstreetmap.josm.data.osm.Relation; 23 24 import org.openstreetmap.josm.data.osm.Way; 25 import org.openstreetmap.josm.data.validation.FixableTestError; 24 26 import org.openstreetmap.josm.data.validation.Severity; 25 27 import org.openstreetmap.josm.data.validation.Test; … … 108 110 } 109 111 112 public class OpeningHoursTestError { 113 final Severity severity; 114 final String message, prettifiedValue; 115 116 public OpeningHoursTestError(String message, Severity severity, String prettifiedValue) { 117 this.message = message; 118 this.severity = severity; 119 this.prettifiedValue = prettifiedValue; 120 } 121 122 public OpeningHoursTestError(String message, Severity severity) { 123 this(message, severity, null); 124 } 125 126 public TestError getTestError(final OsmPrimitive p, final String key) { 127 if (prettifiedValue == null) { 128 return new TestError(OpeningHourTest.this, severity, message, 2901, p); 129 } else { 130 return new FixableTestError(OpeningHourTest.this, severity, message, 2901, p, 131 new ChangePropertyCommand(p, key, prettifiedValue)); 132 } 133 } 134 135 public String getMessage() { 136 return message; 137 } 138 139 public String getPrettifiedValue() { 140 return prettifiedValue; 141 } 142 143 public Severity getSeverity() { 144 return severity; 145 } 146 } 147 110 148 /** 111 149 * Checks for a correct usage of the opening hour syntax of the {@code value} given according to … … 113 151 * validation errors or an empty list. Null values result in an empty list. 114 152 * @param value the opening hour value to be checked. 153 * @param mode whether to validate {@code value} as a time range, or points in time, or both. 115 154 * @return a list of {@link TestError} or an empty list 116 155 */ 117 public List< TestError> checkOpeningHourSyntax(final String value, CheckMode mode) {156 public List<OpeningHoursTestError> checkOpeningHourSyntax(final String value, CheckMode mode) { 118 157 if (ENGINE == null || value == null || value.trim().isEmpty()) { 119 158 return Collections.emptyList(); … … 121 160 try { 122 161 final Object r = parse(value, mode); 123 final List<TestError> errors = new ArrayList<TestError>(); 162 final List<OpeningHoursTestError> errors = new ArrayList<OpeningHoursTestError>(); 163 String prettifiedValue = null; 164 try { 165 prettifiedValue = (String) ((Invocable) ENGINE).invokeMethod(r, "prettifyValue"); 166 } catch (Exception ignore) { 167 } 124 168 for (final Object i : getList(((Invocable) ENGINE).invokeMethod(r, "getWarnings"))) { 125 errors.add(new TestError(this, Severity.WARNING, i.toString(), 2901, Collections.<OsmPrimitive>emptyList()));169 errors.add(new OpeningHoursTestError(i.toString(), Severity.WARNING, prettifiedValue)); 126 170 } 127 171 return errors; … … 131 175 .replaceAll("\\(<Unknown source.*", "") 132 176 .trim(); 133 return Arrays.asList(new TestError(this, Severity.ERROR, message, 2901, Collections.<OsmPrimitive>emptyList()));177 return Arrays.asList(new OpeningHoursTestError(message, Severity.ERROR)); 134 178 } catch (final Exception ex) { 135 179 throw new RuntimeException(ex); … … 137 181 } 138 182 139 public List< TestError> checkOpeningHourSyntax(final String value) {183 public List<OpeningHoursTestError> checkOpeningHourSyntax(final String value) { 140 184 return checkOpeningHourSyntax(value, CheckMode.TIME_RANGE); 141 185 } 142 186 143 protected void check(final OsmPrimitive p, final String tagValue, CheckMode mode) { 144 for (TestError e : checkOpeningHourSyntax(tagValue, mode)) { 145 e.setPrimitives(Collections.singletonList(p)); 146 errors.add(e); 187 protected void check(final OsmPrimitive p, final String key, CheckMode mode) { 188 for (OpeningHoursTestError e : checkOpeningHourSyntax(p.get(key), mode)) { 189 errors.add(e.getTestError(p, key)); 147 190 } 148 191 } 149 192 150 193 protected void check(final OsmPrimitive p) { 151 check(p, p.get("opening_hours"), CheckMode.TIME_RANGE);152 check(p, p.get("collection_times"), CheckMode.BOTH);153 check(p, p.get("service_times"), CheckMode.BOTH);194 check(p, "opening_hours", CheckMode.TIME_RANGE); 195 check(p, "collection_times", CheckMode.BOTH); 196 check(p, "service_times", CheckMode.BOTH); 154 197 } 155 198 -
trunk/test/unit/org/openstreetmap/josm/data/validation/tests/OpeningHourTestTest.java
r6376 r6377 6 6 import org.openstreetmap.josm.data.Preferences; 7 7 import org.openstreetmap.josm.data.validation.Severity; 8 import org.openstreetmap.josm.data.validation.TestError;9 8 10 9 import java.util.List; … … 34 33 @Test 35 34 public void testCheckOpeningHourSyntax2() throws Exception { 36 final List< TestError> errors = OPENING_HOUR_TEST.checkOpeningHourSyntax("Mo-Tue");35 final List<OpeningHourTest.OpeningHoursTestError> errors = OPENING_HOUR_TEST.checkOpeningHourSyntax("Mo-Tue"); 37 36 assertThat(errors.size(), is(1)); 38 37 assertThat(errors.get(0).getMessage(), is("Mo-Tue <--- (Please use the abbreviation \"Tu\" for \"tue\".)")); … … 42 41 @Test 43 42 public void testCheckOpeningHourSyntax3() throws Exception { 44 final List< TestError> errors = OPENING_HOUR_TEST.checkOpeningHourSyntax("Sa-Su 10.00-20.00");43 final List<OpeningHourTest.OpeningHoursTestError> errors = OPENING_HOUR_TEST.checkOpeningHourSyntax("Sa-Su 10.00-20.00"); 45 44 assertThat(errors.size(), is(2)); 46 45 assertThat(errors.get(0).getMessage(), is("Sa-Su 10. <--- (Please use \":\" as hour/minute-separator)")); 47 46 assertThat(errors.get(0).getSeverity(), is(Severity.WARNING)); 47 assertThat(errors.get(0).getPrettifiedValue(), is("Sa-Su 10:00-20:00")); 48 48 assertThat(errors.get(1).getMessage(), is("Sa-Su 10.00-20. <--- (Please use \":\" as hour/minute-separator)")); 49 49 assertThat(errors.get(1).getSeverity(), is(Severity.WARNING)); … … 62 62 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax("badtext").get(0).getMessage(), 63 63 is("opening_hours - ba <--- (Unexpected token: \"b\" This means that the syntax is not valid at that point or it is currently not supported.)")); 64 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax("5.00 p.m-11.00 p.m").size(), is(1)); 65 assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax("5.00 p.m-11.00 p.m").get(0).getMessage(), 66 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?)")); 64 67 } 65 68
Note:
See TracChangeset
for help on using the changeset viewer.