001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.addressing;
020:
021: import junit.framework.TestCase;
022: import org.apache.axiom.om.OMAbstractFactory;
023: import org.apache.axiom.om.OMAttribute;
024: import org.apache.axiom.om.OMElement;
025: import org.apache.axiom.om.OMFactory;
026: import org.apache.axiom.om.OMNamespace;
027:
028: import javax.xml.namespace.QName;
029: import java.util.ArrayList;
030: import java.util.Map;
031:
032: public class EndpointReferenceHelperTest extends TestCase {
033:
034: public void testToAndFromOMForFinalSpecEPR() throws Exception {
035: String address = "http://ws.apache.org/axis2";
036: EndpointReference epr = new EndpointReference(address);
037:
038: OMFactory omf = OMAbstractFactory.getOMFactory();
039: OMNamespace ns1 = omf.createOMNamespace("http://uri1",
040: "prefix1");
041: OMAttribute attr1 = omf.createOMAttribute("attr1", ns1,
042: "attr1value");
043: OMNamespace ns2 = omf.createOMNamespace("http://uri2",
044: "prefix2");
045: OMAttribute attr2 = omf.createOMAttribute("attr2", ns2,
046: "attr2value");
047: epr.addAttribute(attr1);
048: epr.addAttribute(attr2);
049: OMElement md1 = omf.createOMElement("md1", "http://mduri1",
050: "md1prefix");
051: OMElement md2 = omf.createOMElement("md2", "http://mduri2",
052: "md2prefix");
053: epr.addMetaData(md1);
054: epr.addMetaData(md2);
055: OMElement ext1 = omf.createOMElement("ext1", "http://exturi1",
056: "ext1prefix");
057: OMElement ext2 = omf.createOMElement("ext2", "http://exturi2",
058: "ext2prefix");
059: epr.addExtensibleElement(ext1);
060: epr.addExtensibleElement(ext2);
061: QName rp1Qname = new QName("http://rp1uri", "refParm1",
062: "rp1prefix");
063: QName rp2Qname = new QName("http://rp2uri", "refParm2",
064: "rp2prefix");
065: epr.addReferenceParameter(rp1Qname, "rp1");
066: epr.addReferenceParameter(rp2Qname, "rp2");
067:
068: ArrayList addressAttributes = new ArrayList();
069: addressAttributes.add(attr1);
070: addressAttributes.add(attr2);
071: epr.setAddressAttributes(addressAttributes);
072:
073: ArrayList metadataAttributes = new ArrayList();
074: metadataAttributes.add(attr1);
075: metadataAttributes.add(attr2);
076: epr.setMetadataAttributes(metadataAttributes);
077:
078: OMElement om = EndpointReferenceHelper.toOM(omf, epr,
079: new QName("http://nsurl", "localName", "prefix"),
080: AddressingConstants.Final.WSA_NAMESPACE);
081:
082: //Test deserialize using fromOM(OMElement)
083: EndpointReference deser = EndpointReferenceHelper.fromOM(om);
084:
085: assertEquals(epr.getAddress(), deser.getAddress());
086: ArrayList addrAttrs = deser.getAddressAttributes();
087: assertEquals(attr1, addrAttrs.get(0));
088: assertEquals(attr2, addrAttrs.get(1));
089:
090: ArrayList attrs = deser.getAttributes();
091: assertEquals(attr1, attrs.get(0));
092: assertEquals(attr2, attrs.get(1));
093:
094: ArrayList metadata = deser.getMetaData();
095: assertEquals(md1, metadata.get(0));
096: assertEquals(md2, metadata.get(1));
097: ArrayList mdAttrs = deser.getMetadataAttributes();
098: assertEquals(attr1, mdAttrs.get(0));
099: assertEquals(attr2, mdAttrs.get(1));
100:
101: ArrayList extelts = deser.getExtensibleElements();
102: assertEquals(ext1, extelts.get(0));
103: assertEquals(ext2, extelts.get(1));
104:
105: Map m = deser.getAllReferenceParameters();
106: assertEquals("rp1", ((OMElement) m.get(rp1Qname)).getText());
107: assertEquals("rp2", ((OMElement) m.get(rp2Qname)).getText());
108:
109: //Test deserialize using fromOM(EndpointReference, OMElement, String)
110: deser = new EndpointReference("");
111: EndpointReferenceHelper.fromOM(deser, om,
112: AddressingConstants.Final.WSA_NAMESPACE);
113:
114: assertEquals(epr.getAddress(), deser.getAddress());
115: addrAttrs = deser.getAddressAttributes();
116: assertEquals(attr1, addrAttrs.get(0));
117: assertEquals(attr2, addrAttrs.get(1));
118:
119: attrs = deser.getAttributes();
120: assertEquals(attr1, attrs.get(0));
121: assertEquals(attr2, attrs.get(1));
122:
123: metadata = deser.getMetaData();
124: assertEquals(md1, metadata.get(0));
125: assertEquals(md2, metadata.get(1));
126: mdAttrs = deser.getMetadataAttributes();
127: assertEquals(attr1, mdAttrs.get(0));
128: assertEquals(attr2, mdAttrs.get(1));
129:
130: extelts = deser.getExtensibleElements();
131: assertEquals(ext1, extelts.get(0));
132: assertEquals(ext2, extelts.get(1));
133:
134: m = deser.getAllReferenceParameters();
135: assertEquals("rp1", ((OMElement) m.get(rp1Qname)).getText());
136: assertEquals("rp2", ((OMElement) m.get(rp2Qname)).getText());
137:
138: //Failure test
139: try {
140: deser = new EndpointReference("");
141: EndpointReferenceHelper.fromOM(deser, om,
142: AddressingConstants.Submission.WSA_NAMESPACE);
143: fail("An exception should have been thrown due to failure to locate a wsa:Address field.");
144: } catch (Exception e) {
145: //pass
146: }
147: }
148:
149: public void testToAndFromOMForSubmissionSpecEPR() throws Exception {
150: String address = "http://ws.apache.org/axis2";
151: EndpointReference epr = new EndpointReference(address);
152:
153: OMFactory omf = OMAbstractFactory.getOMFactory();
154: OMNamespace ns1 = omf.createOMNamespace("http://uri1",
155: "prefix1");
156: OMAttribute attr1 = omf.createOMAttribute("attr1", ns1,
157: "attr1value");
158: OMNamespace ns2 = omf.createOMNamespace("http://uri2",
159: "prefix2");
160: OMAttribute attr2 = omf.createOMAttribute("attr2", ns2,
161: "attr2value");
162: epr.addAttribute(attr1);
163: epr.addAttribute(attr2);
164: OMElement md1 = omf.createOMElement("md1", "http://mduri1",
165: "md1prefix");
166: OMElement md2 = omf.createOMElement("md2", "http://mduri2",
167: "md2prefix");
168: epr.addMetaData(md1);
169: epr.addMetaData(md2);
170: OMElement ext1 = omf.createOMElement("ext1", "http://exturi1",
171: "ext1prefix");
172: OMElement ext2 = omf.createOMElement("ext2", "http://exturi2",
173: "ext2prefix");
174: epr.addExtensibleElement(ext1);
175: epr.addExtensibleElement(ext2);
176: QName rp1Qname = new QName("http://rp1uri", "refParm1",
177: "rp1prefix");
178: QName rp2Qname = new QName("http://rp2uri", "refParm2",
179: "rp2prefix");
180: epr.addReferenceParameter(rp1Qname, "rp1");
181: epr.addReferenceParameter(rp2Qname, "rp2");
182:
183: ArrayList addressAttributes = new ArrayList();
184: addressAttributes.add(attr1);
185: addressAttributes.add(attr2);
186: epr.setAddressAttributes(addressAttributes);
187:
188: OMElement om = EndpointReferenceHelper.toOM(omf, epr,
189: new QName("http://nsurl", "localName", "prefix"),
190: AddressingConstants.Submission.WSA_NAMESPACE);
191:
192: //Add some reference properties.
193: QName p1Qname = new QName("http://p1uri", "refProp1",
194: "p1prefix");
195: QName p2Qname = new QName("http://p2uri", "refProp2",
196: "p2prefix");
197: QName qname = new QName(
198: AddressingConstants.Submission.WSA_NAMESPACE,
199: "ReferenceProperties",
200: AddressingConstants.WSA_DEFAULT_PREFIX);
201: OMElement referenceProperties = omf.createOMElement(qname, om);
202: OMElement prop1 = omf.createOMElement(p1Qname,
203: referenceProperties);
204: OMElement prop2 = omf.createOMElement(p2Qname,
205: referenceProperties);
206: prop1.setText("p1");
207: prop2.setText("p2");
208:
209: //Test deserialize using fromOM(OMElement)
210: EndpointReference deser = EndpointReferenceHelper.fromOM(om);
211:
212: assertEquals(epr.getAddress(), deser.getAddress());
213: ArrayList addrAttrs = deser.getAddressAttributes();
214: assertEquals(attr1, addrAttrs.get(0));
215: assertEquals(attr2, addrAttrs.get(1));
216:
217: ArrayList attrs = deser.getAttributes();
218: assertEquals(attr1, attrs.get(0));
219: assertEquals(attr2, attrs.get(1));
220:
221: //Metadata will be lost unless it is saved as an extensibility element.
222: ArrayList metadata = deser.getMetaData();
223: assertNull(metadata);
224:
225: ArrayList extelts = deser.getExtensibleElements();
226: assertEquals(ext1, extelts.get(0));
227: assertEquals(ext2, extelts.get(1));
228:
229: //All reference properties are returned as reference parameters.
230: Map m = deser.getAllReferenceParameters();
231: assertEquals(4, m.size());
232: assertEquals("rp1", ((OMElement) m.get(rp1Qname)).getText());
233: assertEquals("rp2", ((OMElement) m.get(rp2Qname)).getText());
234: assertEquals("p1", ((OMElement) m.get(p1Qname)).getText());
235: assertEquals("p2", ((OMElement) m.get(p2Qname)).getText());
236:
237: //Test deserialize using fromOM(EndpointReference, OMElement, String)
238: deser = new EndpointReference("");
239: EndpointReferenceHelper.fromOM(deser, om,
240: AddressingConstants.Submission.WSA_NAMESPACE);
241:
242: assertEquals(epr.getAddress(), deser.getAddress());
243: addrAttrs = deser.getAddressAttributes();
244: assertEquals(attr1, addrAttrs.get(0));
245: assertEquals(attr2, addrAttrs.get(1));
246:
247: attrs = deser.getAttributes();
248: assertEquals(attr1, attrs.get(0));
249: assertEquals(attr2, attrs.get(1));
250:
251: //Metadata will be lost unless it is saved as an extensibility element.
252: metadata = deser.getMetaData();
253: assertNull(metadata);
254:
255: extelts = deser.getExtensibleElements();
256: assertEquals(ext1, extelts.get(0));
257: assertEquals(ext2, extelts.get(1));
258:
259: //All reference properties are returned as reference parameters.
260: m = deser.getAllReferenceParameters();
261: assertEquals(4, m.size());
262: assertEquals("rp1", ((OMElement) m.get(rp1Qname)).getText());
263: assertEquals("rp2", ((OMElement) m.get(rp2Qname)).getText());
264: assertEquals("p1", ((OMElement) m.get(p1Qname)).getText());
265: assertEquals("p2", ((OMElement) m.get(p2Qname)).getText());
266:
267: //Failure test
268: try {
269: deser = new EndpointReference("");
270: EndpointReferenceHelper.fromOM(deser, om,
271: AddressingConstants.Final.WSA_NAMESPACE);
272: fail("An exception should have been thrown due to failure to locate a wsa:Address field.");
273: } catch (Exception e) {
274: //pass
275: }
276: }
277: }
|