1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.validation.tests;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertFalse;
|
---|
6 | import static org.junit.Assert.assertNotNull;
|
---|
7 | import static org.junit.Assert.assertTrue;
|
---|
8 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
9 |
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import org.junit.Rule;
|
---|
13 | import org.junit.Test;
|
---|
14 | import org.openstreetmap.josm.TestUtils;
|
---|
15 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
16 | import org.openstreetmap.josm.data.validation.routines.AbstractValidator;
|
---|
17 | import org.openstreetmap.josm.data.validation.routines.EmailValidator;
|
---|
18 | import org.openstreetmap.josm.data.validation.routines.UrlValidator;
|
---|
19 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
20 |
|
---|
21 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * JUnit Test of "Internet Tags" validation test.
|
---|
25 | */
|
---|
26 | public class InternetTagsTest {
|
---|
27 |
|
---|
28 | private static final InternetTags TEST = new InternetTags();
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Setup test by initializing JOSM preferences and projection.
|
---|
32 | */
|
---|
33 | @Rule
|
---|
34 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
35 | public JOSMTestRules test = new JOSMTestRules();
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Test of valid URLs.
|
---|
39 | */
|
---|
40 | @Test
|
---|
41 | public void testValidUrls() {
|
---|
42 | testUrl("url", "www.domain.com", true); // No protocol
|
---|
43 | testUrl("url", "http://josm.openstreetmap.de", true); // Simple HTTP
|
---|
44 | testUrl("url", "http://josm.openstreetmap.de/", true); // Simple HTTP + slash
|
---|
45 | testUrl("website", "https://www.openstreetmap.org", true); // Simple HTTPS
|
---|
46 | testUrl("heritage:website", "http://www.unesco.org", true); // Key with :
|
---|
47 | testUrl("website", "http://www.nu-lounge.today", true); // see #10810: new TLD
|
---|
48 | testUrl("website", "http://xn--80akeqobjv1b0d3a.xn--p1ai", true); // see #10862: IDN URL in ASCII form
|
---|
49 | testUrl("website", "http://xn--80akeqobjv1b0d3a.xn--p1ai/", true); // see #10862: IDN URL in ASCII form + slash
|
---|
50 | testUrl("website", "http://золотаяцепь.рф", true); // see #10862: IDN URL in Unicode form
|
---|
51 | testUrl("website", "http://золотаяцепь.рф/", true); // see #10862: IDN URL in Unicode form + slash
|
---|
52 | testUrl("website", "http://www.dasideenreich.online", true); // see #12257: new TLD added August 19, 2015
|
---|
53 | }
|
---|
54 |
|
---|
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 | /**
|
---|
64 | * Test of invalid URLs.
|
---|
65 | */
|
---|
66 | @Test
|
---|
67 | public void testInvalidUrls() {
|
---|
68 | testUrl("url", "something://www.domain.com", false); // invalid protocol
|
---|
69 | testUrl("url", "http://www.domain.invalidtld", false); // invalid TLD
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Test of valid e-mails.
|
---|
74 | */
|
---|
75 | @Test
|
---|
76 | public void testValidEmails() {
|
---|
77 | testEmail("email", "contact@www.domain.com", true); // Simple email
|
---|
78 | testEmail("contact:email", "john.doe@other-domain.org", true); // Key with : + dash in domain
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Test of invalid e-mails.
|
---|
83 | */
|
---|
84 | @Test
|
---|
85 | public void testInvalidEmails() {
|
---|
86 | testEmail("email", "contact at www.domain.com", false); // No @
|
---|
87 | testEmail("contact:email", "john.doe@other-domain.invalidtld", false); // invalid TLD
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Test of invalid slashes.
|
---|
92 | */
|
---|
93 | @Test
|
---|
94 | public void testInvalidSlashes() {
|
---|
95 | TestError error = testUrl("website", "http:\\\\www.sjoekurs.no", false).get(0);
|
---|
96 | assertEquals(tr("''{0}'': {1}", "website", tr("URL contains backslashes instead of slashes")), error.getDescription());
|
---|
97 | assertNotNull(error.getFix());
|
---|
98 | }
|
---|
99 |
|
---|
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);
|
---|
102 | if (valid) {
|
---|
103 | assertTrue(errors.isEmpty());
|
---|
104 | } else {
|
---|
105 | assertFalse(errors.isEmpty());
|
---|
106 | assertNotNull(errors.get(0));
|
---|
107 | }
|
---|
108 | return errors;
|
---|
109 | }
|
---|
110 |
|
---|
111 | private static List<TestError> testUrl(String key, String value, boolean valid) {
|
---|
112 | return testKey(key, value, valid, UrlValidator.getInstance(), InternetTags.INVALID_URL);
|
---|
113 | }
|
---|
114 |
|
---|
115 | private static List<TestError> testEmail(String key, String value, boolean valid) {
|
---|
116 | return testKey(key, value, valid, EmailValidator.getInstance(), InternetTags.INVALID_EMAIL);
|
---|
117 | }
|
---|
118 | }
|
---|