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.saaj;
021:
022: import junit.framework.TestCase;
023: import org.w3c.dom.Node;
024: import org.w3c.dom.NodeList;
025:
026: import javax.xml.soap.MessageFactory;
027: import javax.xml.soap.MimeHeaders;
028: import javax.xml.soap.Name;
029: import javax.xml.soap.SOAPBody;
030: import javax.xml.soap.SOAPBodyElement;
031: import javax.xml.soap.SOAPElement;
032: import javax.xml.soap.SOAPException;
033: import javax.xml.soap.SOAPFactory;
034: import javax.xml.soap.SOAPMessage;
035: import javax.xml.soap.Text;
036: import java.io.ByteArrayInputStream;
037: import java.io.IOException;
038:
039: public class TextTest extends TestCase {
040:
041: public TextTest(String name) {
042: super (name);
043: }
044:
045: //Test SAAJ addTextNode performance
046: public void testAddTextNode() throws Exception {
047: SOAPFactory soapFactory = SOAPFactory.newInstance();
048: MessageFactory factory = MessageFactory.newInstance();
049: SOAPMessage message = factory.createMessage();
050: SOAPBody body = message.getSOAPBody();
051:
052: // Create the base element
053: Name bodyName = soapFactory.createName("VBGenReceiver", "xsi",
054: "http://www.w3.org/2001/XMLSchema-instance");
055: SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
056:
057: // Create the MetaData Tag
058: Name name = soapFactory.createName("MetaData");
059: SOAPElement metaData = bodyElement.addChildElement(name);
060:
061: //Create the SKey Tag
062: name = soapFactory.createName("SKey");
063: SOAPElement sKey = metaData.addChildElement(name);
064: sKey.addTextNode("SKEY001");
065:
066: //Create Object Tag
067: name = soapFactory.createName("Object");
068: SOAPElement object = bodyElement.addChildElement(name);
069:
070: //Create Book ID Tag
071: name = soapFactory.createName("BookID");
072: SOAPElement bookID = object.addChildElement(name);
073: bookID.addTextNode("BookID002");
074:
075: //Create OrderID tag
076: name = soapFactory.createName("OrderID");
077: SOAPElement orderID = object.addChildElement(name);
078: orderID.addTextNode("OrderID003");
079:
080: //create PurchaseID tage
081: name = soapFactory.createName("PurchaseID");
082: SOAPElement purchaseID = object.addChildElement(name);
083: purchaseID.addTextNode("PurchaseID005");
084:
085: //create LanguageID Tag
086: name = soapFactory.createName("LanguageID");
087: SOAPElement languageID = object.addChildElement(name);
088: languageID.addTextNode("LanguageID004");
089:
090: //create LanguageID Tag
091: name = soapFactory.createName("LanguageName");
092: SOAPElement languageName = object.addChildElement(name);
093: languageName.addTextNode("LanguageName006");
094:
095: //create LanguageID Tag
096: name = soapFactory.createName("Title");
097: SOAPElement title = object.addChildElement(name);
098: title.addTextNode("Title007");
099:
100: //create LanguageID Tag
101: name = soapFactory.createName("Author");
102: SOAPElement author = object.addChildElement(name);
103: author.addTextNode("Author008");
104:
105: //create LanguageID Tag
106: name = soapFactory.createName("Format");
107: SOAPElement format = bodyElement.addChildElement(name);
108:
109: //create LanguageID Tag
110: name = soapFactory.createName("Type");
111: SOAPElement formatType = format.addChildElement(name);
112: formatType.addTextNode("Type009");
113:
114: //create LanguageID Tag
115: name = soapFactory.createName("Delivery");
116: SOAPElement delivery = bodyElement.addChildElement(name);
117:
118: //create LanguageID Tag
119: name = soapFactory.createName("Name");
120: SOAPElement delName = delivery.addChildElement(name);
121: delName.addTextNode("Name010");
122:
123: //create LanguageID Tag
124: name = soapFactory.createName("Address1");
125: SOAPElement address1 = delivery.addChildElement(name);
126: address1.addTextNode("Address1011");
127:
128: //create LanguageID Tag
129: name = soapFactory.createName("Address2");
130: SOAPElement address2 = delivery.addChildElement(name);
131: address2.addTextNode("Address2012");
132:
133: //create LanguageID Tag
134: name = soapFactory.createName("City");
135: SOAPElement city = delivery.addChildElement(name);
136: city.addTextNode("City013");
137:
138: //create LanguageID Tag
139: name = soapFactory.createName("State");
140: SOAPElement state = delivery.addChildElement(name);
141: state.addTextNode("State014");
142:
143: //create LanguageID Tag
144: name = soapFactory.createName("PostalCode");
145: SOAPElement postalCode = delivery.addChildElement(name);
146: postalCode.addTextNode("PostalCode015");
147:
148: //message.writeTo(System.out);
149: //System.out.flush();
150: }
151:
152: public void testComment() throws SOAPException, IOException {
153:
154: String xmlString = "<?xml version='1.0' encoding='utf-8'?> "
155: + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"
156: + "<soapenv:Header></soapenv:Header>"
157: + "<soapenv:Body>"
158: + "<Node:abc xmlns:Node=\"http://www.simpletest.org\">"
159: + "This is some text"
160: + "<!--This is comment-->This is other text"
161: + "<!--This is another comment-->This is some other text"
162: + "</Node:abc>" + "</soapenv:Body>"
163: + "</soapenv:Envelope>";
164:
165: MessageFactory mf = MessageFactory.newInstance();
166: SOAPMessage message = mf.createMessage(new MimeHeaders(),
167: new ByteArrayInputStream(xmlString.getBytes()));
168:
169: SOAPBody body = message.getSOAPBody();
170: Node bodyElement = body.getFirstChild();
171: NodeList textNodes = bodyElement.getChildNodes();
172:
173: assertEquals(5, textNodes.getLength());
174:
175: for (int i = 0; i < textNodes.getLength(); i++) {
176: Node node = textNodes.item(i);
177: boolean isComment;
178: isComment = ((Text) node).isComment();
179: if (i == 1 || i == 3) {
180: assertEquals(true, isComment);
181: } else {
182: assertEquals(false, isComment);
183: }
184: }
185: }
186: }
|