JIDTest.java 2.54 KB
Newer Older
Gaston Dombiak's avatar
Gaston Dombiak committed
1 2 3 4 5 6 7 8 9
/**
 * Copyright (C) 2004-2007 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
 */

package org.jivesoftware.util;

10
import org.junit.Test;
Gaston Dombiak's avatar
Gaston Dombiak committed
11 12
import org.xmpp.packet.JID;

13 14 15 16
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

Gaston Dombiak's avatar
Gaston Dombiak committed
17 18 19 20 21
/**
 * Test cases for the JID class.
 *
 * @author Gaston Dombiak
 */
22
public class JIDTest {
Gaston Dombiak's avatar
Gaston Dombiak committed
23

24
    @Test
Gaston Dombiak's avatar
Gaston Dombiak committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    public void testDomain() {
        new JID("mycomapny.com");
        new JID("wfink-adm");

        boolean failed = false;
        try {
            new JID("wfink adm");
        } catch (Exception e) {
            failed = true;
        }
        assertTrue("A domain with spaces was accepted", failed);

        failed = false;
        try {
            new JID("wfink_adm");
        } catch (Exception e) {
            failed = true;
        }
        assertTrue("A domain with _ was accepted", failed);
    }

46
    @Test
Gaston Dombiak's avatar
Gaston Dombiak committed
47 48 49 50 51 52 53 54 55 56 57 58 59
    public void testUsernames() {
        new JID("john@mycomapny.com");
        new JID("john_paul@mycomapny.com");
        new JID("john-paul@mycomapny.com");
        boolean failed = false;
        try {
            new JID("john paul@mycomapny.com");
        } catch (Exception e) {
            failed = true;
        }
        assertTrue("A username with spaces was accepted", failed);
    }

60
    @Test
Gaston Dombiak's avatar
Gaston Dombiak committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    public void testCompare() {
        JID jid1 = new JID("john@mycomapny.com");
        JID jid2 = new JID("john@mycomapny.com");
        assertEquals("Failed to compare 2 similar JIDs", 0 , jid1.compareTo(jid2));
        assertEquals("Failed to recognize equal JIDs", jid1 , jid2);

        jid1 = new JID("john@mycomapny.com");
        jid2 = new JID("mycomapny.com");
        assertTrue("Failed to recognized bigger JID", jid1.compareTo(jid2) > 0);
        assertFalse("Failed to recognize different JIDs", jid1.equals(jid2));

        jid1 = new JID("john@mycomapny.com");
        jid2 = new JID("mycomapny.com/resource");
        assertTrue("Failed to recognized bigger JID", jid1.compareTo(jid2) > 0);
        assertFalse("Failed to recognize different JIDs", jid1.equals(jid2));

        jid1 = new JID("john@mycomapny.com");
        jid2 = new JID("john@mycomapny.com/resource");
        assertTrue("Failed to recognized bigger JID", jid1.compareTo(jid2) < 0);
        assertFalse("Failed to recognize different JIDs", jid1.equals(jid2));

    }
}