File: TestQName.java

package info (click to toggle)
axis 1.4-29
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 52,100 kB
  • sloc: java: 129,124; xml: 10,602; jsp: 983; sh: 84; cs: 36; makefile: 18
file content (103 lines) | stat: -rw-r--r-- 3,786 bytes parent folder | download | duplicates (10)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package test.utils;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import javax.xml.namespace.QName;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class TestQName extends TestCase
{
    public TestQName (String name) {
        super(name);
    }

    public static Test suite() {
        return new TestSuite(TestQName.class);
    }

    protected void setup() {
    }

    public void testQName2StringConstructor()
    {
        QName qname = new QName("rdf","article");
        assertNotNull("qname was null.  Something really wrong here.", qname); 
        assertEquals("Namespace URI was not 'rdf', it was: " + qname.getNamespaceURI(),
                     "rdf", qname.getNamespaceURI()); 
        assertEquals("LocalPart was not 'article', it was: " + qname.getLocalPart(),
                     "article", qname.getLocalPart()); 
    }

    public void testToString()
    {
        QName qname = new QName("PREFIX", "LOCALPART");
        assertEquals("qname was not the same as '{PREFIX}LOCALPART', it was: " + qname.toString(),
                     "{PREFIX}LOCALPART", qname.toString());
    }

    public void testNullQname()
    {
        QName qname1 = new QName("LOCALPART");
        QName qname2 = new QName(null, "LOCALPART");
        QName qname3 = new QName("", "LOCALPART");

        assertEquals("omitted namespace ", "LOCALPART", qname1.toString());
        assertEquals("null namespace ", "LOCALPART", qname2.toString());
        assertEquals("empty namespace ", "LOCALPART", qname3.toString());
    }

    public void testEquals()
    {
        QName qname1 = new QName("", "");
        QName qname2 = new QName("PREFIX", "LOCALPART");
        QName qname3 = new QName("PREFIX", "LOCALPART");
        QName qname4 = new QName("PREFIX", "DIFFLOCALPART");
        //need a fully implemented mock Element class...
        //Element elem = new MockElement();        
        ////QName qname5 = new QName("PREFIX:LOCALPART", elem);

        // the following should NOT throw a NullPointerException
        assertTrue("qname1 is the same as qname2", !qname1.equals(qname2));
       
        //Note: this test is comparing the same two QName objects as above, but
        //due to the order and the implementation of the QName.equals() method,
        //this test passes without incurring a NullPointerException. 
        assertTrue("qname2 is the same as qname1", !qname2.equals(qname1));

        assertTrue("qname2 is not the same as qname2", qname2.equals(qname3));
        assertTrue("qname3 is the same as qname4", !qname3.equals(qname4));
    }
    
    public void testHashCode()
    {
        QName control = new QName("xsl", "text");
        QName compare = new QName("xsl", "text");
        QName contrast = new QName("xso", "text");
        assertEquals("control hashcode does not equal compare.hashcode", control.hashCode(), compare.hashCode());
        assertTrue("control hashcode is not equivalent to compare.hashcode", !(control.hashCode() == contrast.hashCode()));
    }

    public void testSerializable() throws Exception
    {
        QName q1 = new QName("Matthew", "Duftler");

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);

        oos.writeObject(q1);
        oos.flush();
        oos.close();

        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bais);
        QName q2 = (QName)ois.readObject();

        assertEquals("q1 is not the same as q2", q1, q2);
        assertEquals("q2 is not the same as q1", q2, q1);
    }
}