| 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; |
| 8 |
import java.io.ByteArrayInputStream; |
| 9 |
import java.io.ByteArrayOutputStream; |
| 10 |
import java.io.ObjectInputStream; |
| 11 |
import java.io.ObjectOutputStream; |
| 12 |
|
| 13 |
public class TestQName extends TestCase |
| 14 |
{ |
| 15 |
public TestQName (String 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 qname = new QName("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 qname = new QName("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 qname1 = new QName("LOCALPART"); |
| 46 |
QName qname2 = new QName(null, "LOCALPART"); |
| 47 |
QName qname3 = new QName("", "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 qname1 = new QName("", ""); |
| 57 |
QName qname2 = new QName("PREFIX", "LOCALPART"); |
| 58 |
QName qname3 = new QName("PREFIX", "LOCALPART"); |
| 59 |
QName qname4 = new QName("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 control = new QName("xsl", "text"); |
| 79 |
QName compare = new QName("xsl", "text"); |
| 80 |
QName contrast = new QName("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 |
| 86 |
{ |
| 87 |
QName q1 = new QName("Matthew", "Duftler"); |
| 88 |
|
| 89 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 90 |
ObjectOutputStream oos = new ObjectOutputStream(baos); |
| 91 |
|
| 92 |
oos.writeObject(q1); |
| 93 |
oos.flush(); |
| 94 |
oos.close(); |
| 95 |
|
| 96 |
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); |
| 97 |
ObjectInputStream ois = new ObjectInputStream(bais); |
| 98 |
QName q2 = (QName)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 |
} |