Changeset 14803 in josm
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/validation/tests/InternetTags.java
r13489 r14803 5 5 import static org.openstreetmap.josm.tools.I18n.tr; 6 6 7 import java.util.ArrayList; 8 import java.util.List; 7 9 import java.util.function.Supplier; 8 10 … … 65 67 for (String i : keys) { 66 68 if (i.equals(k)) { 67 TestError error = validateTag(p, k, validator, code); 68 if (error != null) { 69 errors.add(error); 70 } 71 break; 69 return errors.addAll(validateTag(p, k, validator, code)); 72 70 } 73 71 } … … 83 81 * @return The error if the validation fails, {@code null} otherwise 84 82 * @since 7824 83 * @since 14803 (return type) 85 84 */ 86 public TestError validateTag(OsmPrimitive p, String k, AbstractValidator validator, int code) { 85 public List<TestError> validateTag(OsmPrimitive p, String k, AbstractValidator validator, int code) { 87 86 return doValidateTag(p, k, null, validator, code); 88 87 } … … 97 96 * @return The error if the validation fails, {@code null} otherwise 98 97 */ 99 private TestError doValidateTag(OsmPrimitive p, String k, String v, AbstractValidator validator, int code) { 100 TestError error = null; 101 String value = v != null ? v : p.get(k); 102 if (!validator.isValid(value)) { 103 Supplier<Command> fix = null; 104 String errMsg = validator.getErrorMessage(); 105 if (tr("URL contains an invalid protocol: {0}", (String) null).equals(errMsg)) { 106 // Special treatment to allow URLs without protocol. See UrlValidator#isValid 107 String proto = validator instanceof EmailValidator ? "mailto://" : "http://"; 108 return doValidateTag(p, k, proto+value, validator, code); 109 } else if (tr("URL contains an invalid authority: {0}", (String) null).equals(errMsg) 110 && value.contains("\\") && validator.isValid(value.replaceAll("\\\\", "/"))) { 111 // Special treatment to autofix URLs with backslashes. See UrlValidator#isValid 112 errMsg = tr("URL contains backslashes instead of slashes"); 113 fix = () -> new ChangePropertyCommand(p, k, value.replaceAll("\\\\", "/")); 98 private List<TestError> doValidateTag(OsmPrimitive p, String k, String v, AbstractValidator validator, int code) { 99 List<TestError> errors = new ArrayList<>(); 100 String values = v != null ? v : p.get(k); 101 for (String value : values.split(";")) { 102 if (!validator.isValid(value)) { 103 Supplier<Command> fix = null; 104 String errMsg = validator.getErrorMessage(); 105 if (tr("URL contains an invalid protocol: {0}", (String) null).equals(errMsg)) { 106 // Special treatment to allow URLs without protocol. See UrlValidator#isValid 107 String proto = validator instanceof EmailValidator ? "mailto://" : "http://"; 108 return doValidateTag(p, k, proto+value, validator, code); 109 } else if (tr("URL contains an invalid authority: {0}", (String) null).equals(errMsg) 110 && value.contains("\\") && validator.isValid(value.replaceAll("\\\\", "/"))) { 111 // Special treatment to autofix URLs with backslashes. See UrlValidator#isValid 112 errMsg = tr("URL contains backslashes instead of slashes"); 113 fix = () -> new ChangePropertyCommand(p, k, value.replaceAll("\\\\", "/")); 114 } 115 errors.add(TestError.builder(this, Severity.WARNING, code) 116 .message(validator.getValidatorName(), marktr("''{0}'': {1}"), k, errMsg) 117 .primitives(p) 118 .fix(fix) 119 .build()); 114 120 } 115 error = TestError.builder(this, Severity.WARNING, code)116 .message(validator.getValidatorName(), marktr("''{0}'': {1}"), k, errMsg)117 .primitives(p)118 .fix(fix)119 .build();120 121 } 121 return error; 122 return errors; 122 123 } 123 124 -
trunk/test/unit/org/openstreetmap/josm/data/validation/tests/InternetTagsTest.java
r13489 r14803 3 3 4 4 import static org.junit.Assert.assertEquals; 5 import static org.junit.Assert.assertFalse; 5 6 import static org.junit.Assert.assertNotNull; 6 import static org.junit.Assert.assert Null;7 import static org.junit.Assert.assertTrue; 7 8 import static org.openstreetmap.josm.tools.I18n.tr; 9 10 import java.util.List; 8 11 9 12 import org.junit.Rule; … … 51 54 52 55 /** 56 * Test multiple URLs. 57 */ 58 @Test 59 public void testMultipleUrls() { 60 testUrl("url", "http://www.domain-a.com;https://www.domain-b.com", true); // multiple values 61 } 62 63 /** 53 64 * Test of invalid URLs. 54 65 */ … … 82 93 @Test 83 94 public void testInvalidSlashes() { 84 TestError error = testUrl("website", "http:\\\\www.sjoekurs.no", false); 95 TestError error = testUrl("website", "http:\\\\www.sjoekurs.no", false).get(0); 85 96 assertEquals(tr("''{0}'': {1}", "website", tr("URL contains backslashes instead of slashes")), error.getDescription()); 86 97 assertNotNull(error.getFix()); 87 98 } 88 99 89 private static TestError testKey(String key, String value, boolean valid, AbstractValidator validator, int code) { 90 TestError error= TEST.validateTag(TestUtils.addFakeDataSet(TestUtils.newNode(key+"="+value+"")), key, validator, code);100 private static List<TestError> testKey(String key, String value, boolean valid, AbstractValidator validator, int code) { 101 List<TestError> errors = TEST.validateTag(TestUtils.addFakeDataSet(TestUtils.newNode(key+"="+value+"")), key, validator, code); 91 102 if (valid) { 92 assert Null(error != null ? error.getMessage() : null, error);103 assertTrue(errors.isEmpty()); 93 104 } else { 94 assertNotNull(error); 105 assertFalse(errors.isEmpty()); 106 assertNotNull(errors.get(0)); 95 107 } 96 return error; 108 return errors; 97 109 } 98 110 99 private static TestError testUrl(String key, String value, boolean valid) { 111 private static List<TestError> testUrl(String key, String value, boolean valid) { 100 112 return testKey(key, value, valid, UrlValidator.getInstance(), InternetTags.INVALID_URL); 101 113 } 102 114 103 private static TestError testEmail(String key, String value, boolean valid) { 115 private static List<TestError> testEmail(String key, String value, boolean valid) { 104 116 return testKey(key, value, valid, EmailValidator.getInstance(), InternetTags.INVALID_EMAIL); 105 117 }
Note:
See TracChangeset
for help on using the changeset viewer.