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.Rule;
|
---|
8 | import org.junit.Test;
|
---|
9 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
10 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
11 | import org.openstreetmap.josm.data.validation.routines.AbstractValidator;
|
---|
12 | import org.openstreetmap.josm.data.validation.routines.EmailValidator;
|
---|
13 | import org.openstreetmap.josm.data.validation.routines.UrlValidator;
|
---|
14 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
15 |
|
---|
16 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * JUnit Test of "Internet Tags" validation test.
|
---|
20 | */
|
---|
21 | public class InternetTagsTest {
|
---|
22 |
|
---|
23 | private static final InternetTags TEST = new InternetTags();
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Setup test by initializing JOSM preferences and projection.
|
---|
27 | */
|
---|
28 | @Rule
|
---|
29 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
30 | public JOSMTestRules test = new JOSMTestRules();
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Test of valid URLs.
|
---|
34 | */
|
---|
35 | @Test
|
---|
36 | public void testValidUrls() {
|
---|
37 | testUrl("url", "www.domain.com", true); // No protocol
|
---|
38 | testUrl("url", "http://josm.openstreetmap.de", true); // Simple HTTP
|
---|
39 | testUrl("url", "http://josm.openstreetmap.de/", true); // Simple HTTP + slash
|
---|
40 | testUrl("website", "https://www.openstreetmap.org", true); // Simple HTTPS
|
---|
41 | testUrl("heritage:website", "http://www.unesco.org", true); // Key with :
|
---|
42 | testUrl("website", "http://www.nu-lounge.today", true); // see #10810: new TLD
|
---|
43 | testUrl("website", "http://xn--80akeqobjv1b0d3a.xn--p1ai", true); // see #10862: IDN URL in ASCII form
|
---|
44 | testUrl("website", "http://xn--80akeqobjv1b0d3a.xn--p1ai/", true); // see #10862: IDN URL in ASCII form + slash
|
---|
45 | testUrl("website", "http://золотаяцепь.рф", true); // see #10862: IDN URL in Unicode form
|
---|
46 | testUrl("website", "http://золотаяцепь.рф/", true); // see #10862: IDN URL in Unicode form + slash
|
---|
47 | testUrl("website", "http://www.dasideenreich.online", true); // see #12257: new TLD added August 19, 2015
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Test of invalid URLs.
|
---|
52 | */
|
---|
53 | @Test
|
---|
54 | public void testInvalidUrls() {
|
---|
55 | testUrl("url", "something://www.domain.com", false); // invalid protocol
|
---|
56 | testUrl("url", "http://www.domain.invalidtld", false); // invalid TLD
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Test of valid e-mails.
|
---|
61 | */
|
---|
62 | @Test
|
---|
63 | public void testValidEmails() {
|
---|
64 | testEmail("email", "contact@www.domain.com", true); // Simple email
|
---|
65 | testEmail("contact:email", "john.doe@other-domain.org", true); // Key with : + dash in domain
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Test of invalid e-mails.
|
---|
70 | */
|
---|
71 | @Test
|
---|
72 | public void testInvalidEmails() {
|
---|
73 | testEmail("email", "contact at www.domain.com", false); // No @
|
---|
74 | testEmail("contact:email", "john.doe@other-domain.invalidtld", false); // invalid TLD
|
---|
75 | }
|
---|
76 |
|
---|
77 | private static void testKey(String key, String value, boolean valid, AbstractValidator validator, int code) {
|
---|
78 | TestError error = TEST.validateTag(OsmUtils.createPrimitive("node "+key+"="+value+""), key, validator, code);
|
---|
79 | if (valid) {
|
---|
80 | assertNull(error != null ? error.getMessage() : null, error);
|
---|
81 | } else {
|
---|
82 | assertNotNull(error);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | private static void testUrl(String key, String value, boolean valid) {
|
---|
87 | testKey(key, value, valid, UrlValidator.getInstance(), InternetTags.INVALID_URL);
|
---|
88 | }
|
---|
89 |
|
---|
90 | private static void testEmail(String key, String value, boolean valid) {
|
---|
91 | testKey(key, value, valid, EmailValidator.getInstance(), InternetTags.INVALID_EMAIL);
|
---|
92 | }
|
---|
93 | }
|
---|