1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.ws.security.message.token;
21
22 import org.apache.ws.security.WSSecurityException;
23
24
25
26
27 public class ReferenceTest extends org.junit.Assert {
28
29 private static final String
30 TEST_REFERENCE_TEMPLATE =
31 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
32 + "<wss:Reference "
33 + "xmlns:wss=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" "
34 + "ValueType=\"TheValueType\" "
35 + "URI=\"TheURI\" "
36 + "/>"
37 ;
38
39 private static final String
40 BOGUS_REFERENCE_TEMPLATE =
41 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
42 + "<wss:Reference "
43 + "xmlns:wss=\"http://something-completely-different\" "
44 + "ValueType=\"TheValueType\" "
45 + "URI=\"TheURI\" "
46 + "/>"
47 ;
48
49 private Reference ref;
50 private Reference refEqual;
51 private Reference refNotEqual;
52
53 public ReferenceTest() throws Exception{
54 ref = new Reference (createReferenceDocument(TEST_REFERENCE_TEMPLATE, "test", "equalscheck").getDocumentElement());
55 refEqual = new Reference (createReferenceDocument(TEST_REFERENCE_TEMPLATE, "test", "equalscheck").getDocumentElement());
56 refNotEqual = new Reference (createReferenceDocument(TEST_REFERENCE_TEMPLATE, "test", "unequalscheck").getDocumentElement());
57 }
58
59
60 @org.junit.Test
61 public void
62 testConstructor() throws Exception {
63
64
65
66 try {
67 new Reference((org.w3c.dom.Element) null);
68 fail("Expected failure on null Element passed to ctor");
69 } catch (final WSSecurityException e) {
70
71 }
72
73
74
75 try {
76 new Reference(
77 createReferenceDocument(
78 BOGUS_REFERENCE_TEMPLATE,
79 "foo", "bar"
80 ).getDocumentElement()
81 );
82 fail("Expected failure on bogus template");
83 } catch (final Exception e) {
84
85 }
86
87
88
89 new Reference(
90 createReferenceDocument(
91 TEST_REFERENCE_TEMPLATE,
92 "foo", "bar"
93 )
94 );
95 new Reference(
96 createReferenceDocument(
97 TEST_REFERENCE_TEMPLATE,
98 "foo", "bar"
99 ).getDocumentElement()
100 );
101 }
102
103 @org.junit.Test
104 public void
105 testAccessors() throws Exception {
106 final Reference ref = new Reference(
107 createReferenceDocument(
108 TEST_REFERENCE_TEMPLATE,
109 "foo", "bar"
110 ).getDocumentElement()
111 );
112 assertEquals(ref.getValueType(), "foo");
113 assertEquals(ref.getURI(), "bar");
114 }
115
116 @org.junit.Test
117 public void testEquals() throws Exception{
118 assertTrue(ref.equals(refEqual));
119 assertTrue(refEqual.equals(ref));
120 assertFalse(ref.equals(refNotEqual));
121 assertFalse(ref.equals(null));
122 assertFalse(ref.equals("string"));
123 }
124
125 @org.junit.Test
126 public void testHashcode() throws Exception{
127 assertEquals(ref.hashCode(), refEqual.hashCode());
128 assertTrue(!(ref.hashCode() == refNotEqual.hashCode()));
129 }
130
131 private static org.w3c.dom.Document
132 createReferenceDocument(
133 final String template,
134 final String valueType,
135 final String uri
136 ) throws javax.xml.parsers.ParserConfigurationException,
137 org.xml.sax.SAXException,
138 java.io.IOException {
139 final java.io.InputStream in =
140 new java.io.ByteArrayInputStream(
141 template.replaceFirst(
142 "TheValueType", valueType
143 ).replaceFirst(
144 "TheURI", uri
145 ).getBytes()
146 );
147 final javax.xml.parsers.DocumentBuilderFactory factory =
148 javax.xml.parsers.DocumentBuilderFactory.newInstance();
149 factory.setNamespaceAware(true);
150 final javax.xml.parsers.DocumentBuilder builder = factory.newDocumentBuilder();
151 return builder.parse(in);
152 }
153 }