Ticket #18956: 18956-alternative.patch
File 18956-alternative.patch, 12.4 KB (added by , 5 years ago) |
---|
-
src/org/openstreetmap/josm/data/validation/TestError.java
219 219 * Returns a new test error with the specified values 220 220 * 221 221 * @return a new test error with the specified values 222 * @throws IllegalArgumentException when {@link #message} or {@link #primitives} is null .222 * @throws IllegalArgumentException when {@link #message} or {@link #primitives} is null/empty. 223 223 */ 224 224 public TestError build() { 225 225 CheckParameterUtil.ensureParameterNotNull(message, "message not set"); 226 226 CheckParameterUtil.ensureParameterNotNull(primitives, "primitives not set"); 227 CheckParameterUtil.ensureThat(!primitives.isEmpty(), "primitives is empty"); 227 228 if (this.highlighted == null) { 228 229 this.highlighted = Collections.emptySet(); 229 230 } -
src/org/openstreetmap/josm/data/validation/tests/ConditionalKeys.java
8 8 import java.util.Collection; 9 9 import java.util.HashSet; 10 10 import java.util.List; 11 import java.util.Locale; 11 12 import java.util.Set; 12 13 import java.util.regex.Matcher; 13 14 import java.util.regex.Pattern; … … 122 123 return parts.length == 1 && (isRestrictionType(parts[0]) || isTransportationMode(parts[0])); 123 124 } 124 125 125 /**126 * Check if a value is valid127 * @param key The key the value is for128 * @param value The value129 * @return <code>true</code> if it is valid130 */131 public boolean isValueValid(String key, String value) {132 return validateValue(key, value) == null;133 }134 135 126 static class ConditionalParsingException extends RuntimeException { 136 127 ConditionalParsingException(String message) { 137 128 super(message); … … 190 181 * Validate a key/value pair 191 182 * @param key The key 192 183 * @param value The value 184 * @param p the primitive 193 185 * @return The error message for that value or <code>null</code> to indicate valid 194 186 */ 195 public String validateValue(String key, String value ) {187 public String validateValue(String key, String value, OsmPrimitive p) { 196 188 try { 197 189 for (final ConditionalValue conditional : ConditionalValue.parse(value)) { 198 190 // validate restriction value … … 202 194 // validate opening hour if the value contains an hour (heuristic) 203 195 for (final String condition : conditional.conditions) { 204 196 if (condition.matches(".*[0-9]:[0-9]{2}.*")) { 205 final List<TestError> errors = openingHourTest.checkOpeningHourSyntax("", condition); 197 final List<TestError> errors = openingHourTest.checkOpeningHourSyntax("", condition, p, 198 Locale.ENGLISH); 206 199 if (!errors.isEmpty()) { 207 200 return errors.get(0).getDescription(); 208 201 } … … 233 226 continue; 234 227 } 235 228 final String value = p.get(key); 236 final String error = validateValue(key, value );229 final String error = validateValue(key, value, p); 237 230 if (error != null) { 238 231 errors.add(TestError.builder(this, Severity.WARNING, 3202) 239 232 .message(tr("Error in {0} value: {1}", key, error)) -
src/org/openstreetmap/josm/data/validation/tests/OpeningHourTest.java
11 11 import java.util.Locale; 12 12 import java.util.Objects; 13 13 14 import ch.poole.openinghoursparser.OpeningHoursParser;15 import ch.poole.openinghoursparser.ParseException;16 import ch.poole.openinghoursparser.Rule;17 import ch.poole.openinghoursparser.Util;18 14 import org.openstreetmap.josm.command.ChangePropertyCommand; 19 15 import org.openstreetmap.josm.data.osm.OsmPrimitive; 20 16 import org.openstreetmap.josm.data.validation.Severity; 21 17 import org.openstreetmap.josm.data.validation.Test.TagTest; 22 18 import org.openstreetmap.josm.data.validation.TestError; 19 import org.openstreetmap.josm.tools.CheckParameterUtil; 23 20 21 import ch.poole.openinghoursparser.OpeningHoursParser; 22 import ch.poole.openinghoursparser.ParseException; 23 import ch.poole.openinghoursparser.Rule; 24 import ch.poole.openinghoursparser.Util; 25 24 26 /** 25 27 * Tests the correct usage of the opening hour syntax of the tags 26 28 * {@code opening_hours}, {@code collection_times}, {@code service_times} according to … … 45 47 * @param severity The error severity 46 48 * @param message The error message 47 49 * @param key The incriminated key, used for display. 48 * @param value The incriminated value, used for comparison with prettified value.49 50 * @param prettifiedValue The prettified value 50 51 * @param p The incriminated OSM primitive. 51 52 * @return The real test error given to JOSM validator. Can be fixable or not if a prettified values has been determined. 52 53 */ 53 private TestError createTestError(Severity severity, String message, String key, String value, StringprettifiedValue, OsmPrimitive p) {54 private TestError createTestError(Severity severity, String message, String key, String prettifiedValue, OsmPrimitive p) { 54 55 final TestError.Builder error = TestError.builder(this, severity, 2901) 55 56 .message(tr("Opening hours syntax"), message) // todo obtain English message for ignore functionality 56 .primitives(p != null ? new OsmPrimitive[] {p} : new OsmPrimitive[] {});57 if (p == null || prettifiedValue == null || prettifiedValue.equals(value)) {57 .primitives(p); 58 if (prettifiedValue == null || prettifiedValue.equals(p.get(key))) { 58 59 return error.build(); 59 60 } else { 60 61 return error.fix(() -> new ChangePropertyCommand(p, key, prettifiedValue)).build(); … … 64 65 /** 65 66 * Checks for a correct usage of the opening hour syntax of the {@code value} given, 66 67 * and returns a list containing validation errors or an empty list. Null values result in an empty list. 67 * @param key the OSM key (should be "opening_hours", "collection_times" or "service_times"). Used in error message68 * @param value the opening hour value to be checked.69 * @return a list of {@link TestError} or an empty list70 */71 public List<TestError> checkOpeningHourSyntax(final String key, final String value) {72 return checkOpeningHourSyntax(key, value, null, Locale.getDefault());73 }74 75 /**76 * Checks for a correct usage of the opening hour syntax of the {@code value} given,77 * and returns a list containing validation errors or an empty list. Null values result in an empty list.78 68 * @param key the OSM key (should be "opening_hours", "collection_times" or "service_times"). 79 69 * @param value the opening hour value to be checked. 80 70 * @param p the primitive to check/fix. … … 82 72 * @return a list of {@link TestError} or an empty list 83 73 */ 84 74 List<TestError> checkOpeningHourSyntax(final String key, final String value, OsmPrimitive p, Locale locale) { 75 CheckParameterUtil.ensureParameterNotNull(p, "primitive"); 85 76 if (value == null || value.isEmpty()) { 86 77 return Collections.emptyList(); 87 78 } … … 96 87 new OpeningHoursParser(new StringReader(value)).rules(true); 97 88 } 98 89 } catch (ParseException e) { 99 return Collections.singletonList(createTestError(Severity.WARNING, e.getMessage(), key, value,prettifiedValue, p));90 return Collections.singletonList(createTestError(Severity.WARNING, e.getMessage(), key, prettifiedValue, p)); 100 91 } 101 92 102 93 if (!includeOtherSeverityChecks() || Objects.equals(value, prettifiedValue)) { … … 103 94 return Collections.emptyList(); 104 95 } else { 105 96 final String message = tr("{0} value can be prettified", key); 106 return Collections.singletonList(createTestError(Severity.OTHER, message, key, value,prettifiedValue, p));97 return Collections.singletonList(createTestError(Severity.OTHER, message, key, prettifiedValue, p)); 107 98 } 108 99 } 109 100 -
test/unit/org/openstreetmap/josm/data/validation/tests/ConditionalKeysTest.java
7 7 import org.junit.Before; 8 8 import org.junit.Rule; 9 9 import org.junit.Test; 10 import org.openstreetmap.josm.data.coor.LatLon; 11 import org.openstreetmap.josm.data.osm.DataSet; 12 import org.openstreetmap.josm.data.osm.Node; 10 13 import org.openstreetmap.josm.testutils.JOSMTestRules; 11 14 12 15 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; … … 51 54 } 52 55 53 56 /** 54 * Unit test of {@link ConditionalKeys# isValueValid}.57 * Unit test of {@link ConditionalKeys#validateValue}. 55 58 */ 56 59 @Test 57 60 public void testValueValid() { 58 assertTrue( test.isValueValid("maxspeed:conditional", "120 @ (06:00-19:00)"));59 assertFalse( test.isValueValid("maxspeed:conditional", " @ (06:00-19:00)"));60 assertFalse( test.isValueValid("maxspeed:conditional", "120 (06:00-19:00)"));61 assertFalse( test.isValueValid("maxspeed:conditional", "120 @ ()"));62 assertFalse( test.isValueValid("maxspeed:conditional", "120 @ "));63 assertFalse( test.isValueValid("maxspeed:conditional", "120 @ (06:00/19:00)"));64 assertTrue( test.isValueValid("maxspeed:conditional", "120 @ (06:00-20:00); 100 @ (22:00-06:00)"));65 assertTrue( test.isValueValid("motor_vehicle:conditional", "delivery @ (Mo-Fr 06:00-11:00,17:00-19:00;Sa 03:30-19:00)"));66 assertTrue( test.isValueValid("motor_vehicle:conditional", "no @ (10:00-18:00 AND length>5)"));67 assertFalse( test.isValueValid("motor_vehicle:conditional", "foo @ (10:00-18:00 AND length>5)"));68 assertFalse( test.isValueValid("motor_vehicle:conditional", "no @ (10:00until18:00 AND length>5)"));69 assertTrue( test.isValueValid("maxspeed:hgv:conditional", "60 @ (weight>7.5)"));70 assertTrue( test.isValueValid("restriction:conditional", "no_left_turn @ (Mo-Fr 16:00-18:00)"));61 assertTrue(isValueValid("maxspeed:conditional", "120 @ (06:00-19:00)")); 62 assertFalse(isValueValid("maxspeed:conditional", " @ (06:00-19:00)")); 63 assertFalse(isValueValid("maxspeed:conditional", "120 (06:00-19:00)")); 64 assertFalse(isValueValid("maxspeed:conditional", "120 @ ()")); 65 assertFalse(isValueValid("maxspeed:conditional", "120 @ ")); 66 assertFalse(isValueValid("maxspeed:conditional", "120 @ (06:00/19:00)")); 67 assertTrue(isValueValid("maxspeed:conditional", "120 @ (06:00-20:00); 100 @ (22:00-06:00)")); 68 assertTrue(isValueValid("motor_vehicle:conditional", "delivery @ (Mo-Fr 06:00-11:00,17:00-19:00;Sa 03:30-19:00)")); 69 assertTrue(isValueValid("motor_vehicle:conditional", "no @ (10:00-18:00 AND length>5)")); 70 assertFalse(isValueValid("motor_vehicle:conditional", "foo @ (10:00-18:00 AND length>5)")); 71 assertFalse(isValueValid("motor_vehicle:conditional", "no @ (10:00until18:00 AND length>5)")); 72 assertTrue(isValueValid("maxspeed:hgv:conditional", "60 @ (weight>7.5)")); 73 assertTrue(isValueValid("restriction:conditional", "no_left_turn @ (Mo-Fr 16:00-18:00)")); 71 74 } 75 76 /** 77 * Check if a value is valid 78 * @param key The key the value is for 79 * @param value The value 80 * @return <code>true</code> if it is valid 81 */ 82 private boolean isValueValid(String key, String value) { 83 final Node node = new Node(LatLon.ZERO); 84 node.put(key, value); 85 new DataSet(node); 86 return test.validateValue(key, value, node) == null; 87 } 88 72 89 }