StringUtilsTest.java 1.59 KB
Newer Older
1 2
package org.jivesoftware.util;

3 4
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
5 6 7 8 9 10 11 12

import org.junit.Test;

public class StringUtilsTest {
	
    @Test
    public void testValidDomainNames() {
    	
13 14 15
        assertValidDomainName("www.mycompany.com");
        assertValidDomainName("www.my-company.com");
        assertValidDomainName("abc.de");
16 17
        assertValidDomainName("tronçon.be", "xn--tronon-zua.be");
        assertValidDomainName("öbb.at", "xn--bb-eka.at");
18

19 20 21 22 23
    }
    
    @Test
    public void testInvalidDomainNames() {
    	
24 25 26 27 28 29 30 31 32 33 34 35 36
        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));
37 38
    }

39 40 41 42 43
	private void assertInvalidDomainName(String domain, String expectedCause) {
		try {
        	StringUtils.validateDomainName(domain);
        	fail("Domain should not be valid: " + domain);
        } catch (IllegalArgumentException iae) {
44 45
        	// this is not part of the official API, so leave off for now
        	//assertEquals("Unexpected cause: " + iae.getMessage(), expectedCause, iae.getMessage());
46 47
        }
	}
48
}