1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.validation.tests;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertNotNull;
|
---|
5 | import static org.junit.Assert.assertNull;
|
---|
6 |
|
---|
7 | import org.junit.BeforeClass;
|
---|
8 | import org.junit.Test;
|
---|
9 | import org.openstreetmap.josm.JOSMFixture;
|
---|
10 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
11 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
12 | import org.openstreetmap.josm.data.validation.routines.AbstractValidator;
|
---|
13 | import org.openstreetmap.josm.data.validation.routines.EmailValidator;
|
---|
14 | import org.openstreetmap.josm.data.validation.routines.UrlValidator;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * JUnit Test of "Internet Tags" validation test.
|
---|
18 | */
|
---|
19 | public class InternetTagsTest {
|
---|
20 |
|
---|
21 | private static InternetTags TEST;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Setup test by initializing JOSM preferences and projection.
|
---|
25 | */
|
---|
26 | @BeforeClass
|
---|
27 | public static void setUp() {
|
---|
28 | JOSMFixture.createUnitTestFixture().init();
|
---|
29 | TEST = new InternetTags();
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Test of "Internet Tags" validation test.
|
---|
34 | */
|
---|
35 | @Test
|
---|
36 | public void test() {
|
---|
37 |
|
---|
38 | // Valid URLs
|
---|
39 | testUrl("url", "http://josm.openstreetmap.de", true); // Simple HTTP
|
---|
40 | testUrl("url", "http://josm.openstreetmap.de/", true); // Simple HTTP + slash
|
---|
41 | testUrl("website", "https://www.openstreetmap.org", true); // Simple HTTPS
|
---|
42 | testUrl("heritage:website", "http://www.unesco.org", true); // Key with :
|
---|
43 | testUrl("website", "http://www.nu-lounge.today", true); // see #10810: new TLD
|
---|
44 | testUrl("website", "http://xn--80akeqobjv1b0d3a.xn--p1ai", true); // see #10862: IDN URL in ASCII form
|
---|
45 | testUrl("website", "http://xn--80akeqobjv1b0d3a.xn--p1ai/", true); // see #10862: IDN URL in ASCII form + slash
|
---|
46 | testUrl("website", "http://золотаяцепь.рф", true); // see #10862: IDN URL in Unicode form
|
---|
47 | testUrl("website", "http://золотаяцепь.рф/", true); // see #10862: IDN URL in Unicode form + slash
|
---|
48 |
|
---|
49 | // Invalid URLs
|
---|
50 | testUrl("url", "www.domain.com", false); // No protocol
|
---|
51 | testUrl("url", "something://www.domain.com", false); // invalid protocol
|
---|
52 | testUrl("url", "http://www.domain.invalidtld", false); // invalid TLD
|
---|
53 |
|
---|
54 | // Valid E-mails
|
---|
55 | testEmail("email", "contact@www.domain.com", true); // Simple email
|
---|
56 | testEmail("contact:email", "john.doe@other-domain.org", true); // Key with : + dash in domain
|
---|
57 |
|
---|
58 | // Invalid E-mails
|
---|
59 | testEmail("email", "contact at www.domain.com", false); // No @
|
---|
60 | testEmail("contact:email", "john.doe@other-domain.invalidtld", false); // invalid TLD
|
---|
61 | }
|
---|
62 |
|
---|
63 | private static void testKey(String key, String value, boolean valid, AbstractValidator validator, int code) {
|
---|
64 | TestError error = TEST.validateTag(OsmUtils.createPrimitive("node "+key+"="+value+""), key, validator, code);
|
---|
65 | if (valid) {
|
---|
66 | assertNull(error != null ? error.getMessage() : null, error);
|
---|
67 | } else {
|
---|
68 | assertNotNull(error);
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | private static void testUrl(String key, String value, boolean valid) {
|
---|
73 | testKey(key, value, valid, UrlValidator.getInstance(), InternetTags.INVALID_URL);
|
---|
74 | }
|
---|
75 |
|
---|
76 | private static void testEmail(String key, String value, boolean valid) {
|
---|
77 | testKey(key, value, valid, EmailValidator.getInstance(), InternetTags.INVALID_EMAIL);
|
---|
78 | }
|
---|
79 | }
|
---|