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:
020: package org.apache.axis2.addressing;
021:
022: import junit.framework.TestCase;
023: import junit.textui.TestRunner;
024: import org.apache.axiom.om.OMAbstractFactory;
025: import org.apache.axiom.om.OMAttribute;
026: import org.apache.axiom.om.OMElement;
027: import org.apache.axiom.om.OMFactory;
028: import org.apache.axiom.om.OMNamespace;
029:
030: import javax.xml.namespace.QName;
031: import java.io.ByteArrayInputStream;
032: import java.io.ByteArrayOutputStream;
033: import java.io.ObjectInputStream;
034: import java.io.ObjectOutputStream;
035: import java.util.ArrayList;
036: import java.util.Map;
037:
038: public class EndpointReferenceTypeTest extends TestCase {
039:
040: EndpointReference endpointReference;
041: private String address = "htttp://wwww.openource.lk/~chinthaka";
042:
043: public static void main(String[] args) {
044: TestRunner.run(EndpointReferenceTypeTest.class);
045: }
046:
047: /*
048: * @see TestCase#setUp()
049: */
050: protected void setUp() throws Exception {
051: super .setUp();
052: endpointReference = new EndpointReference(address);
053: }
054:
055: public void testGetAndSetAddress() {
056: assertEquals("Address not set properly in the constructor",
057: address, endpointReference.getAddress());
058:
059: String newAddress = "http://www.axis2.com";
060: endpointReference.setAddress(newAddress);
061: assertEquals("Address not set properly in the setter method",
062: newAddress, endpointReference.getAddress());
063: }
064:
065: public void testGetAndSetReferenceParameters() {
066: for (int i = 0; i < 10; i++) {
067: endpointReference.addReferenceParameter(new QName(
068: "http://www.opensouce.lk/" + i, "" + i), "value "
069: + i * 50);
070: }
071:
072: Map retrievedReferenceParameters = endpointReference
073: .getAllReferenceParameters();
074: for (int i = 0; i < 10; i++) {
075: OMElement referenceParameter = (OMElement) retrievedReferenceParameters
076: .get(new QName("http://www.opensouce.lk/" + i, ""
077: + i));
078: assertEquals(
079: "Input value differs from what is taken out from AnyContentType",
080: referenceParameter.getText(), "value " + i * 50);
081: }
082: }
083:
084: public void testHasAnonymousAddress() {
085: // Default EndpointReference does not has 'anonymous address'
086: assertFalse(endpointReference.hasAnonymousAddress());
087:
088: // EndpointReference with 2005/08 Anonymous address
089: EndpointReference epr200508anon = new EndpointReference(
090: AddressingConstants.Final.WSA_ANONYMOUS_URL);
091: assertTrue(epr200508anon.hasAnonymousAddress());
092:
093: // EndpointReference with 2004/08 Anonymous address
094: EndpointReference epr200408anon = new EndpointReference(
095: AddressingConstants.Submission.WSA_ANONYMOUS_URL);
096: assertTrue(epr200408anon.hasAnonymousAddress());
097: }
098:
099: public void testHasNoneAddress() {
100: // Default EndpointReference does not has 'anonymous address'
101: assertFalse(endpointReference.hasNoneAddress());
102:
103: // EndpointReference with 2005/08 None address
104: EndpointReference epr200508none = new EndpointReference(
105: AddressingConstants.Final.WSA_NONE_URI);
106: assertTrue(epr200508none.hasNoneAddress());
107: }
108:
109: public void testSerializationDeserialization() throws Exception {
110: String address = "http://ws.apache.org/axis2";
111: EndpointReference epr = new EndpointReference(address);
112:
113: OMFactory omf = OMAbstractFactory.getOMFactory();
114: OMNamespace ns1 = omf.createOMNamespace("http://uri1",
115: "prefix1");
116: OMAttribute attr1 = omf.createOMAttribute("attr1", ns1,
117: "attr1value");
118: OMNamespace ns2 = omf.createOMNamespace("http://uri2",
119: "prefix2");
120: OMAttribute attr2 = omf.createOMAttribute("attr2", ns2,
121: "attr2value");
122: epr.addAttribute(attr1);
123: epr.addAttribute(attr2);
124: OMElement md1 = omf.createOMElement("md1", "http://mduri1",
125: "md1prefix");
126: OMElement md2 = omf.createOMElement("md2", "http://mduri2",
127: "md2prefix");
128: epr.addMetaData(md1);
129: epr.addMetaData(md2);
130: OMElement ext1 = omf.createOMElement("ext1", "http://exturi1",
131: "ext1prefix");
132: OMElement ext2 = omf.createOMElement("ext2", "http://exturi2",
133: "ext2prefix");
134: epr.addExtensibleElement(ext1);
135: epr.addExtensibleElement(ext2);
136: QName rp1Qname = new QName("http://rp1uri", "refParm1",
137: "rp1prefix");
138: QName rp2Qname = new QName("http://rp2uri", "refParm2",
139: "rp2prefix");
140: epr.addReferenceParameter(rp1Qname, "rp1");
141: epr.addReferenceParameter(rp2Qname, "rp2");
142:
143: ArrayList addressAttributes = new ArrayList();
144: addressAttributes.add(attr1);
145: addressAttributes.add(attr2);
146: epr.setAddressAttributes(addressAttributes);
147:
148: ArrayList metadataAttributes = new ArrayList();
149: metadataAttributes.add(attr1);
150: metadataAttributes.add(attr2);
151: epr.setMetadataAttributes(metadataAttributes);
152:
153: ByteArrayOutputStream baos = new ByteArrayOutputStream();
154: ObjectOutputStream oos = new ObjectOutputStream(baos);
155: oos.writeObject(epr);
156:
157: ByteArrayInputStream bais = new ByteArrayInputStream(baos
158: .toByteArray());
159: ObjectInputStream ois = new ObjectInputStream(bais);
160: EndpointReference deserializedEPR = (EndpointReference) ois
161: .readObject();
162:
163: assertEquals(epr.getAddress(), deserializedEPR.getAddress());
164: ArrayList addrAttrs = deserializedEPR.getAddressAttributes();
165: assertAttributesEqual(attr1, (OMAttribute) addrAttrs.get(0));
166: assertAttributesEqual(attr2, (OMAttribute) addrAttrs.get(1));
167:
168: ArrayList attrs = deserializedEPR.getAttributes();
169: assertAttributesEqual(attr1, (OMAttribute) attrs.get(0));
170: assertAttributesEqual(attr2, (OMAttribute) attrs.get(1));
171:
172: ArrayList metadata = deserializedEPR.getMetaData();
173: assertEquals(md1.toString(), metadata.get(0).toString());
174: assertEquals(md2.toString(), metadata.get(1).toString());
175: ArrayList mdAttrs = deserializedEPR.getMetadataAttributes();
176: assertAttributesEqual(attr1, (OMAttribute) mdAttrs.get(0));
177: assertAttributesEqual(attr2, (OMAttribute) mdAttrs.get(1));
178:
179: ArrayList extelts = deserializedEPR.getExtensibleElements();
180: assertEquals(ext1.toString(), extelts.get(0).toString());
181: assertEquals(ext2.toString(), extelts.get(1).toString());
182:
183: Map m = deserializedEPR.getAllReferenceParameters();
184: assertEquals("rp1", ((OMElement) m.get(rp1Qname)).getText());
185: assertEquals("rp2", ((OMElement) m.get(rp2Qname)).getText());
186: }
187:
188: private void assertAttributesEqual(OMAttribute attribute1,
189: OMAttribute attribute2) {
190: if (!attribute1.getAttributeValue().equals(
191: attribute2.getAttributeValue())
192: || (!attribute1.getQName()
193: .equals(attribute2.getQName()))) {
194: fail("expected:" + attribute1.getQName() + "="
195: + attribute1.getAttributeValue() + " but got "
196: + attribute2.getQName() + "="
197: + attribute2.getAttributeValue());
198: }
199: }
200: }
|