1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package org.jivesoftware.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.Test;
public class StringUtilsTest {
@Test
public void testValidDomainNames() {
assertValidDomainName("www.mycompany.com");
assertValidDomainName("www.my-company.com");
assertValidDomainName("abc.de");
assertValidDomainName("tronçon.be", "xn--tronon-zua.be");
assertValidDomainName("öbb.at", "xn--bb-eka.at");
}
@Test
public void testInvalidDomainNames() {
assertInvalidDomainName("www.my_company.com", "Contains non-LDH characters");
assertInvalidDomainName("www.-dash.com", "Has leading or trailing hyphen");
assertInvalidDomainName("www.dash-.com", "Has leading or trailing hyphen");
assertInvalidDomainName("abc.<test>.de", "Contains non-LDH characters");
}
private void assertValidDomainName(String domain) {
assertValidDomainName(domain, domain);
}
private void assertValidDomainName(String domain, String expected) {
assertEquals("Domain should be valid: " + domain, expected, StringUtils.validateDomainName(domain));
}
private void assertInvalidDomainName(String domain, String expectedCause) {
try {
StringUtils.validateDomainName(domain);
fail("Domain should not be valid: " + domain);
} catch (IllegalArgumentException iae) {
// this is not part of the official API, so leave off for now
//assertEquals("Unexpected cause: " + iae.getMessage(), expectedCause, iae.getMessage());
}
}
}