001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: /*
021: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
022: *
023: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
024: *
025: * The contents of this file are subject to the terms of either the GNU
026: * General Public License Version 2 only ("GPL") or the Common Development
027: * and Distribution License("CDDL") (collectively, the "License"). You
028: * may not use this file except in compliance with the License. You can obtain
029: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
030: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
031: * language governing permissions and limitations under the License.
032: *
033: * When distributing the software, include this License Header Notice in each
034: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
035: * Sun designates this particular file as subject to the "Classpath" exception
036: * as provided by Sun in the GPL Version 2 section of the License file that
037: * accompanied this code. If applicable, add the following below the License
038: * Header, with the fields enclosed by brackets [] replaced by your own
039: * identifying information: "Portions Copyrighted [year]
040: * [name of copyright owner]"
041: *
042: * Contributor(s):
043: *
044: * If you wish your version of this file to be governed by only the CDDL or
045: * only the GPL Version 2, indicate your decision by adding "[Contributor]
046: * elects to include this software in this distribution under the [CDDL or GPL
047: * Version 2] license." If you don't indicate a single choice of license, a
048: * recipient has the option to distribute your version of this file under
049: * either the CDDL, the GPL Version 2 or to extend the choice of license to
050: * its licensees as provided above. However, if you add GPL Version 2 code
051: * and therefore, elected the GPL Version 2 license, then the option applies
052: * only if the new code is made subject to such option by the copyright
053: * holder.
054: */
055:
056: package soap;
057:
058: import java.io.ByteArrayInputStream;
059: import java.util.Iterator;
060: import java.util.Locale;
061:
062: import javax.xml.namespace.QName;
063: import javax.xml.soap.*;
064: import javax.xml.transform.stream.StreamSource;
065:
066: import junit.framework.TestCase;
067:
068: import com.sun.xml.messaging.saaj.soap.impl.ElementImpl;
069:
070: public class QNameTest extends TestCase {
071:
072: public QNameTest(String name) {
073: super (name);
074: }
075:
076: public void testAddDetailEntry() throws Exception {
077: SOAPFactory soapFactory = SOAPFactory.newInstance();
078: QName name = new QName("http://faultservice.org/types",
079: "BasicFaultElement", "ns0");
080: QName name2 = new QName("http://faultservice.org/types",
081: "AdditionalElement", "ns0");
082: Detail detail = soapFactory.createDetail();
083: DetailEntry entry = detail.addDetailEntry(name);
084:
085: SOAPElement child = entry.addChildElement("Project");
086: entry.addChildElement("Mi", "ns0",
087: "http://faultservice.org/types");
088: entry.addChildElement("Chiamo", "ns0",
089: "http://faultservice.org/types");
090: entry.addChildElement("JAXRPC", "ns0",
091: "http://faultservice.org/types");
092: entry.addChildElement("SI", "ns0",
093: "http://faultservice.org/types");
094: entry.addChildElement("Implementation", "ns0",
095: "http://faultservice.org/types");
096: child.addTextNode("Il mio nome e JAXRPC SI");
097: entry = detail.addDetailEntry(name2);
098:
099: child = entry.addChildElement("Project");
100: child.addTextNode("2 text");
101:
102: org.w3c.dom.Node firstChild = detail.getFirstChild();
103: assertTrue(firstChild != null);
104: org.w3c.dom.Node secondChild = firstChild.getNextSibling();
105: assertTrue(secondChild != null);
106: }
107:
108: public void testAddFault2() throws Exception {
109: String testDoc = "<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'\n"
110: + " xmlns:ns1='http://example.com/wsdl'>\n"
111: + " <env:Body>\n"
112: + " <ns1:sayHello>\n"
113: + " <String_1>Duke!</String_1>\n"
114: + " </ns1:sayHello>\n"
115: + " </env:Body>\n"
116: + "</env:Envelope>\n";
117:
118: byte[] testDocBytes = testDoc.getBytes("UTF-8");
119: ByteArrayInputStream bais = new ByteArrayInputStream(
120: testDocBytes);
121: StreamSource strSource = new StreamSource(bais);
122: MessageFactory mf = MessageFactory.newInstance();
123: SOAPMessage sm = mf.createMessage();
124: SOAPPart sp = sm.getSOAPPart();
125: sp.setContent(strSource);
126: SOAPEnvelope envelope = sp.getEnvelope();
127: String prefix = envelope.getElementName().getPrefix();
128: String uri = envelope.getElementName().getURI();
129: SOAPBody body = envelope.getBody();
130: QName name = new QName(uri, "Server", prefix);
131: try {
132: SOAPFault fault = body.addFault(name,
133: "This is a Server fault");
134: assertTrue(fault != null);
135: } catch (Exception e) {
136: e.printStackTrace();
137: fail();
138: }
139: }
140:
141: public void testAddFault3() throws Exception {
142: String testDoc = "<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'\n"
143: + " xmlns:ns1='http://example.com/wsdl'>\n"
144: + " <env:Body>\n"
145: + " <ns1:sayHello>\n"
146: + " <String_1>Duke!</String_1>\n"
147: + " </ns1:sayHello>\n"
148: + " </env:Body>\n"
149: + "</env:Envelope>\n";
150:
151: byte[] testDocBytes = testDoc.getBytes("UTF-8");
152: ByteArrayInputStream bais = new ByteArrayInputStream(
153: testDocBytes);
154: StreamSource strSource = new StreamSource(bais);
155: MessageFactory mf = MessageFactory.newInstance();
156: SOAPMessage sm = mf.createMessage();
157: SOAPPart sp = sm.getSOAPPart();
158: sp.setContent(strSource);
159: SOAPEnvelope envelope = sp.getEnvelope();
160: String prefix = envelope.getElementName().getPrefix();
161: String uri = envelope.getElementName().getURI();
162: SOAPBody body = envelope.getBody();
163: QName name = new QName(uri, "Server", prefix);
164: Locale l = new Locale("en", "US");
165: try {
166: SOAPFault fault = body.addFault(name,
167: "This is a Server fault", l);
168: assertTrue(fault != null);
169: } catch (Exception e) {
170: e.printStackTrace();
171: fail();
172: }
173: }
174:
175: public void testAddBodyElement() throws Exception {
176: MessageFactory msgFactory = MessageFactory.newInstance();
177:
178: SOAPMessage msg = msgFactory.createMessage();
179:
180: SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
181:
182: SOAPHeader hdr = envelope.getHeader();
183: SOAPBody bdy = envelope.getBody();
184:
185: SOAPHeaderElement transaction = hdr.addHeaderElement(envelope
186: .createName("Transaction", "t", "some-uri"));
187:
188: transaction.setMustUnderstand(true);
189: transaction.addTextNode("5");
190:
191: SOAPBodyElement ltp = bdy.addBodyElement(new QName(
192: "some-other-uri", "GetLastTradePrice", "m"));
193: //msg.writeTo(System.out);
194: assertTrue(ltp.getElementName().getLocalName().equals(
195: "GetLastTradePrice"));
196: }
197:
198: public void testAddChildElement() throws Exception {
199: SOAPElementFactory factory = SOAPElementFactory.newInstance();
200: SOAPElement element = factory.create("testElement");
201:
202: element.addChildElement(new QName("uri", "child", "prefix"));
203:
204: Iterator eachChild = element.getChildElements(new QName("uri",
205: "child", "prefix"));
206: assertTrue("First element is there", eachChild.hasNext());
207: SOAPElement child = (SOAPElement) eachChild.next();
208: assertEquals("prefix:child", child.getTagName());
209: assertFalse("Extra elements", eachChild.hasNext());
210: }
211:
212: public void testAddAttribute() throws Exception {
213: SOAPElementFactory factory = SOAPElementFactory.newInstance();
214: SOAPElement element = factory.create("testElement");
215:
216: QName originalAttributeName = new QName("unqualifiedName");
217: String originalAttributeValue = "aValue";
218: element.addAttribute(originalAttributeName,
219: originalAttributeValue);
220:
221: Name theAttributeName = null;
222: String theAttributeValue = null;
223: int count = 0;
224: for (Iterator eachAttribute = element.getAllAttributes(); eachAttribute
225: .hasNext();) {
226: theAttributeName = (Name) eachAttribute.next();
227: theAttributeValue = element
228: .getAttributeValue(theAttributeName);
229:
230: ++count;
231: assertEquals(1, count);
232: }
233:
234: assertEquals("Qualified names of attributes must match",
235: ElementImpl.getQualifiedName(originalAttributeName),
236: theAttributeName.getQualifiedName());
237: assertEquals("Attribute values must match",
238: originalAttributeValue, theAttributeValue);
239:
240: }
241:
242: public void testGetAttributeValue() throws Exception {
243: SOAPElementFactory factory = SOAPElementFactory.newInstance();
244: SOAPElement element = factory.create("testElement");
245:
246: QName originalAttributeName = new QName("unqualifiedName");
247: String originalAttributeValue = "aValue";
248: element.addAttribute(originalAttributeName,
249: originalAttributeValue);
250: assertTrue(originalAttributeValue.equals(element
251: .getAttributeValue(originalAttributeName)));
252: element.removeAttribute(originalAttributeName);
253:
254: Name theAttributeName = null;
255: String theAttributeValue = null;
256: String unexpectedAttributelist = "";
257: int count = 0;
258: for (Iterator eachAttribute = element.getAllAttributes(); eachAttribute
259: .hasNext();) {
260:
261: theAttributeName = (Name) eachAttribute.next();
262: theAttributeValue = element
263: .getAttributeValue(theAttributeName);
264:
265: ++count;
266: unexpectedAttributelist += theAttributeName
267: .getQualifiedName()
268: + " = " + theAttributeValue + "\n";
269: }
270:
271: assertEquals("Unexpected attributes:\n"
272: + unexpectedAttributelist, 0, count);
273:
274: theAttributeValue = element
275: .getAttributeValue(originalAttributeName);
276:
277: assertTrue("Should have been null but was: " + "\""
278: + theAttributeValue + "\"", null == theAttributeValue);
279: }
280:
281: public void testCreateElement() throws Exception {
282: SOAPFactory sFactory = SOAPFactory.newInstance();
283: MessageFactory msgFactory = MessageFactory.newInstance();
284: SOAPEnvelope envelope;
285: try {
286: SOAPMessage msg = msgFactory.createMessage();
287: envelope = msg.getSOAPPart().getEnvelope();
288:
289: SOAPHeader hdr = envelope.getHeader();
290: SOAPBody bdy = envelope.getBody();
291: SOAPElement elem = sFactory.createElement(new QName(
292: "localname"));
293: bdy.addChildElement(elem);
294:
295: } catch (Exception e) {
296: e.printStackTrace();
297: fail("No exception should be thrown" + e.getMessage());
298: }
299: }
300:
301: public void testSetFaultCode() throws Exception {
302: MessageFactory msgFactory = MessageFactory.newInstance();
303: SOAPMessage msg = msgFactory.createMessage();
304: SOAPPart soapPart = msg.getSOAPPart();
305: SOAPEnvelope envelope = soapPart.getEnvelope();
306: SOAPHeader hdr = envelope.getHeader();
307: SOAPBody body = envelope.getBody();
308:
309: SOAPFault sf = body.addFault();
310: String faultCodeLocalName = "Client2";
311: String faultCodePrefix = "fcp";
312: String faultCodeUri = "http://test/fault/code";
313: QName faultCodeName = new QName(faultCodeUri,
314: faultCodeLocalName, faultCodePrefix);
315: sf.setFaultCode(faultCodeName);
316: sf.setFaultString("This is the fault string");
317: QName faultCode = sf.getFaultCodeAsQName();
318: assertEquals(faultCodePrefix, faultCode.getPrefix());
319: assertEquals(faultCodeUri, faultCode.getNamespaceURI());
320: assertEquals(faultCodeLocalName, faultCode.getLocalPart());
321: }
322:
323: public void testAddHeaderElement() throws Exception {
324: SOAPMessage message = MessageFactory.newInstance()
325: .createMessage();
326: SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
327: SOAPHeader header = envelope.getHeader();
328:
329: QName name1 = new QName("foo", "firstElement", "");
330: QName name2 = new QName("foo", "secondElement", "");
331:
332: SOAPHeaderElement element1 = header.addHeaderElement(name1);
333: element1.setActor("theActor");
334:
335: SOAPHeaderElement element2 = header.addHeaderElement(name2);
336: element2.setActor("theActor");
337:
338: Iterator eachElement = header.extractHeaderElements("theActor");
339:
340: assertTrue("First element is there", eachElement.hasNext());
341: SOAPHeaderElement outElement1 = (SOAPHeaderElement) eachElement
342: .next();
343: assertTrue("Second element is there", eachElement.hasNext());
344: SOAPHeaderElement outElement2 = (SOAPHeaderElement) eachElement
345: .next();
346: assertFalse("No more elements", eachElement.hasNext());
347:
348: assertEquals("First element is correct", element1, outElement1);
349: assertEquals("Second element is correct", element2, outElement2);
350: }
351: }
|