KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > utils > TestQName


1 package test.utils;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6
7 import javax.xml.namespace.QName JavaDoc;
8 import java.io.ByteArrayInputStream JavaDoc;
9 import java.io.ByteArrayOutputStream JavaDoc;
10 import java.io.ObjectInputStream JavaDoc;
11 import java.io.ObjectOutputStream JavaDoc;
12
13 public class TestQName extends TestCase
14 {
15     public TestQName (String JavaDoc name) {
16         super(name);
17     }
18
19     public static Test suite() {
20         return new TestSuite(TestQName.class);
21     }
22
23     protected void setup() {
24     }
25
26     public void testQName2StringConstructor()
27     {
28         QName JavaDoc qname = new QName JavaDoc("rdf","article");
29         assertNotNull("qname was null. Something really wrong here.", qname);
30         assertEquals("Namespace URI was not 'rdf', it was: " + qname.getNamespaceURI(),
31                      "rdf", qname.getNamespaceURI());
32         assertEquals("LocalPart was not 'article', it was: " + qname.getLocalPart(),
33                      "article", qname.getLocalPart());
34     }
35
36     public void testToString()
37     {
38         QName JavaDoc qname = new QName JavaDoc("PREFIX", "LOCALPART");
39         assertEquals("qname was not the same as '{PREFIX}LOCALPART', it was: " + qname.toString(),
40                      "{PREFIX}LOCALPART", qname.toString());
41     }
42
43     public void testNullQname()
44     {
45         QName JavaDoc qname1 = new QName JavaDoc("LOCALPART");
46         QName JavaDoc qname2 = new QName JavaDoc(null, "LOCALPART");
47         QName JavaDoc qname3 = new QName JavaDoc("", "LOCALPART");
48
49         assertEquals("omitted namespace ", "LOCALPART", qname1.toString());
50         assertEquals("null namespace ", "LOCALPART", qname2.toString());
51         assertEquals("empty namespace ", "LOCALPART", qname3.toString());
52     }
53
54     public void testEquals()
55     {
56         QName JavaDoc qname1 = new QName JavaDoc("", "");
57         QName JavaDoc qname2 = new QName JavaDoc("PREFIX", "LOCALPART");
58         QName JavaDoc qname3 = new QName JavaDoc("PREFIX", "LOCALPART");
59         QName JavaDoc qname4 = new QName JavaDoc("PREFIX", "DIFFLOCALPART");
60         //need a fully implemented mock Element class...
61
//Element elem = new MockElement();
62
////QName qname5 = new QName("PREFIX:LOCALPART", elem);
63

64         // the following should NOT throw a NullPointerException
65
assertTrue("qname1 is the same as qname2", !qname1.equals(qname2));
66        
67         //Note: this test is comparing the same two QName objects as above, but
68
//due to the order and the implementation of the QName.equals() method,
69
//this test passes without incurring a NullPointerException.
70
assertTrue("qname2 is the same as qname1", !qname2.equals(qname1));
71
72         assertTrue("qname2 is not the same as qname2", qname2.equals(qname3));
73         assertTrue("qname3 is the same as qname4", !qname3.equals(qname4));
74     }
75     
76     public void testHashCode()
77     {
78         QName JavaDoc control = new QName JavaDoc("xsl", "text");
79         QName JavaDoc compare = new QName JavaDoc("xsl", "text");
80         QName JavaDoc contrast = new QName JavaDoc("xso", "text");
81         assertEquals("control hashcode does not equal compare.hashcode", control.hashCode(), compare.hashCode());
82         assertTrue("control hashcode is not equivalent to compare.hashcode", !(control.hashCode() == contrast.hashCode()));
83     }
84
85     public void testSerializable() throws Exception JavaDoc
86     {
87         QName JavaDoc q1 = new QName JavaDoc("Matthew", "Duftler");
88
89         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
90         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
91
92         oos.writeObject(q1);
93         oos.flush();
94         oos.close();
95
96         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
97         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
98         QName JavaDoc q2 = (QName JavaDoc)ois.readObject();
99
100         assertEquals("q1 is not the same as q2", q1, q2);
101         assertEquals("q2 is not the same as q1", q2, q1);
102     }
103 }
104
Popular Tags