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.saaj;
020:
021: import junit.framework.TestCase;
022:
023: import javax.xml.soap.MessageFactory;
024: import javax.xml.soap.MimeHeaders;
025: import javax.xml.soap.Name;
026: import javax.xml.soap.SOAPBody;
027: import javax.xml.soap.SOAPElement;
028: import javax.xml.soap.SOAPEnvelope;
029: import javax.xml.soap.SOAPHeader;
030: import javax.xml.soap.SOAPMessage;
031: import javax.xml.soap.SOAPPart;
032: import javax.xml.soap.Text;
033: import java.io.ByteArrayInputStream;
034: import java.io.ByteArrayOutputStream;
035: import java.util.Iterator;
036:
037: public class PrefixesTest extends TestCase {
038:
039: public PrefixesTest(String name) {
040: super (name);
041: }
042:
043: public void testAddingPrefixesForChildElements() throws Exception {
044: MessageFactory factory = MessageFactory.newInstance();
045: SOAPMessage msg = factory.createMessage();
046: SOAPPart sp = msg.getSOAPPart();
047: SOAPEnvelope se = sp.getEnvelope();
048: SOAPBody sb = se.getBody();
049: SOAPElement el1 = sb.addBodyElement(se.createName("element1",
050: "prefix1", "http://www.sun.com"));
051: el1.addChildElement(se.createName("element2", "prefix2",
052: "http://www.apache.org"));
053:
054: ByteArrayOutputStream baos = new ByteArrayOutputStream();
055: msg.writeTo(baos);
056:
057: String xml = new String(baos.toByteArray());
058: assertTrue(xml.indexOf("prefix1") != -1);
059: assertTrue(xml.indexOf("prefix2") != -1);
060: assertTrue(xml.indexOf("http://www.sun.com") != -1);
061: assertTrue(xml.indexOf("http://www.apache.org") != -1);
062: }
063:
064: public void testAttribute() throws Exception {
065: String soappacket = "<soapenv:Envelope xmlns:soapenv =\"http://schemas.xmlsoap.org/soap/envelope/\"\n"
066: + " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n"
067: + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"
068: + " <soapenv:Body>\n"
069: + " <t:helloworld t:name=\"test\" xmlns:t='http://test.org/Test'>Hello</t:helloworld>\n"
070: + " </soapenv:Body>\n" + "</soapenv:Envelope>";
071:
072: SOAPMessage msg = MessageFactory.newInstance().createMessage(
073: new MimeHeaders(),
074: new ByteArrayInputStream(soappacket.getBytes()));
075: SOAPBody body = msg.getSOAPPart().getEnvelope().getBody();
076: validateBody(body.getChildElements());
077: }
078:
079: private void validateBody(Iterator iter) {
080: while (iter.hasNext()) {
081: final Object obj = iter.next();
082: if (obj instanceof Text) {
083: //System.out.println("\n- Text Ignored.");
084: } else {
085: final SOAPElement soapElement = (SOAPElement) obj;
086: final Iterator attIter = soapElement.getAllAttributes();
087: while (attIter.hasNext()) {
088: final Name name = (Name) attIter.next();
089: assertEquals("test", soapElement
090: .getAttributeValue(name));
091: assertEquals("t", name.getPrefix());
092: assertEquals("t:name", name.getQualifiedName());
093: assertEquals("name", name.getLocalName());
094: assertEquals("http://test.org/Test", name.getURI());
095: }
096:
097: final Iterator childElementIter = soapElement
098: .getChildElements();
099: if (childElementIter == null)
100: return;
101: validateBody(childElementIter);
102: }
103: }
104: }
105:
106: public void testAttrPrifix() {
107: try {
108: MessageFactory fac = MessageFactory.newInstance();
109:
110: SOAPMessage msg = fac.createMessage();
111: SOAPEnvelope env = msg.getSOAPPart().getEnvelope();
112: SOAPHeader header = msg.getSOAPHeader();
113:
114: Name name = env.createName("Local", "pre1", "http://test1");
115: SOAPElement local = header.addChildElement(name);
116:
117: Name name2 = env.createName("Local1", "pre1",
118: "http://test1");
119: SOAPElement local2 = local.addChildElement(name2);
120:
121: Name aName = env.createName("attrib", "pre1",
122: "http://test1");
123: local2.addAttribute(aName, "value");
124:
125: ByteArrayOutputStream baos = new ByteArrayOutputStream();
126: msg.writeTo(baos);
127:
128: String xml = new String(baos.toByteArray());
129:
130: assertTrue(xml.indexOf("xmlns:http://test1") == -1);
131: assertTrue(xml.indexOf("pre1:attrib=\"value\"") > 0);
132:
133: } catch (Exception e) {
134: e.printStackTrace();
135: fail(e.getMessage());
136: }
137: }
138: }
|