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: * $Id: ElementTest.java,v 1.2 2007/07/16 16:41:27 ofung Exp $
022: */
023:
024: /*
025: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
026: *
027: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
028: *
029: * The contents of this file are subject to the terms of either the GNU
030: * General Public License Version 2 only ("GPL") or the Common Development
031: * and Distribution License("CDDL") (collectively, the "License"). You
032: * may not use this file except in compliance with the License. You can obtain
033: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
034: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
035: * language governing permissions and limitations under the License.
036: *
037: * When distributing the software, include this License Header Notice in each
038: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
039: * Sun designates this particular file as subject to the "Classpath" exception
040: * as provided by Sun in the GPL Version 2 section of the License file that
041: * accompanied this code. If applicable, add the following below the License
042: * Header, with the fields enclosed by brackets [] replaced by your own
043: * identifying information: "Portions Copyrighted [year]
044: * [name of copyright owner]"
045: *
046: * Contributor(s):
047: *
048: * If you wish your version of this file to be governed by only the CDDL or
049: * only the GPL Version 2, indicate your decision by adding "[Contributor]
050: * elects to include this software in this distribution under the [CDDL or GPL
051: * Version 2] license." If you don't indicate a single choice of license, a
052: * recipient has the option to distribute your version of this file under
053: * either the CDDL, the GPL Version 2 or to extend the choice of license to
054: * its licensees as provided above. However, if you add GPL Version 2 code
055: * and therefore, elected the GPL Version 2 license, then the option applies
056: * only if the new code is made subject to such option by the copyright
057: * holder.
058: */
059:
060: /**
061: *
062: * @author SAAJ RI Development Team
063: */package element;
064:
065: import java.util.Iterator;
066:
067: import javax.xml.soap.*;
068: import javax.xml.parsers.*;
069: import javax.xml.namespace.QName;
070:
071: import junit.framework.TestCase;
072:
073: import org.w3c.dom.NodeList;
074: import org.w3c.dom.Node;
075: import org.w3c.dom.Document;
076: import org.w3c.dom.Element;
077:
078: import com.sun.xml.messaging.saaj.soap.name.NameImpl;
079:
080: public class ElementTest extends TestCase {
081:
082: public ElementTest(String name) {
083: super (name);
084: }
085:
086: public void testGetAnAttribute() throws Exception {
087: SOAPElementFactory factory = SOAPElementFactory.newInstance();
088: SOAPElement element = factory.create("testElement");
089:
090: Name originalAttributeName = NameImpl
091: .createFromTagName("unqualifiedName");
092: String originalAttributeValue = "aValue";
093: element.addAttribute(originalAttributeName,
094: originalAttributeValue);
095:
096: Name theAttributeName = null;
097: String theAttributeValue = null;
098: int count = 0;
099: for (Iterator eachAttribute = element.getAllAttributes(); eachAttribute
100: .hasNext();) {
101: theAttributeName = (Name) eachAttribute.next();
102: theAttributeValue = element
103: .getAttributeValue(theAttributeName);
104:
105: ++count;
106: assertEquals(1, count);
107: }
108:
109: assertEquals("Qualified names of attributes must match",
110: originalAttributeName.getQualifiedName(),
111: theAttributeName.getQualifiedName());
112: assertEquals("Attribute values must match",
113: originalAttributeValue, theAttributeValue);
114:
115: /*
116: for (Iterator eachAttribute = element.getAllAttributesAsQNames();
117: eachAttribute.hasNext();
118: ) {
119: assertTrue(eachAttribute.next() instanceof QName);
120: }*/
121: }
122:
123: public void testGetAttributeValue() throws Exception {
124: SOAPElementFactory factory = SOAPElementFactory.newInstance();
125: SOAPElement element = factory.create("testElement");
126:
127: Name originalAttributeName = NameImpl
128: .createFromTagName("unqualifiedName");
129: String originalAttributeValue = "aValue";
130: element.addAttribute(originalAttributeName,
131: originalAttributeValue);
132: element.removeAttribute(originalAttributeName);
133:
134: Name theAttributeName = null;
135: String theAttributeValue = null;
136: String unexpectedAttributelist = "";
137: int count = 0;
138: for (Iterator eachAttribute = element.getAllAttributes(); eachAttribute
139: .hasNext();) {
140: theAttributeName = (Name) eachAttribute.next();
141: theAttributeValue = element
142: .getAttributeValue(theAttributeName);
143:
144: ++count;
145:
146: unexpectedAttributelist += theAttributeName
147: .getQualifiedName()
148: + " = " + theAttributeValue + "\n";
149: }
150: assertEquals("Unexpected attributes:\n"
151: + unexpectedAttributelist, 0, count);
152:
153: theAttributeValue = element
154: .getAttributeValue(originalAttributeName);
155:
156: assertTrue("Should have been null but was: " + "\""
157: + theAttributeValue + "\"", null == theAttributeValue);
158: }
159:
160: public void testAddTextNode() throws Exception {
161: SOAPElementFactory factory = SOAPElementFactory.newInstance();
162: SOAPElement element = factory.create("testElement");
163:
164: String originalText = "<txt>text</txt>";
165: element.addTextNode(originalText);
166: String returnedText = element.getValue();
167:
168: assertEquals(originalText, returnedText);
169: }
170:
171: public void testAddChildElement() throws Exception {
172: SOAPElementFactory factory = SOAPElementFactory.newInstance();
173: SOAPElement element = factory.create("testElement");
174:
175: element.addChildElement("child");
176:
177: Iterator eachChild = element.getChildElements(NameImpl
178: .createFromTagName("child"));
179:
180: assertTrue("First element is there", eachChild.hasNext());
181: SOAPElement child = (SOAPElement) eachChild.next();
182: assertEquals("child", child.getTagName());
183: assertFalse("Extra elements", eachChild.hasNext());
184: }
185:
186: public void testAddHeaderElement() throws Exception {
187: MessageFactory messageFactory = MessageFactory.newInstance();
188: SOAPMessage msg = messageFactory.createMessage();
189:
190: SOAPElementFactory factory = SOAPElementFactory.newInstance();
191: SOAPElement element = factory.create("testElement");
192: element.addChildElement("child");
193:
194: SOAPHeader header = msg.getSOAPHeader();
195: header.addChildElement(element);
196:
197: Iterator eachHeader = header.getChildElements();
198:
199: assertTrue("First header is there", eachHeader.hasNext());
200: SOAPHeaderElement headerElement = (SOAPHeaderElement) eachHeader
201: .next();
202: assertEquals("testElement", headerElement.getTagName());
203: assertFalse("Extra headers", eachHeader.hasNext());
204:
205: Iterator eachChild = headerElement.getChildElements();
206: assertTrue("First header child is there", eachChild.hasNext());
207: SOAPElement child = (SOAPElement) eachChild.next();
208: assertEquals("child", child.getTagName());
209: assertFalse("Extra children", eachChild.hasNext());
210: }
211:
212: public void testGetDetailEntries() throws Exception {
213: MessageFactory mf = MessageFactory.newInstance();
214: SOAPMessage msg = mf.createMessage();
215: SOAPBody body = msg.getSOAPBody();
216: SOAPFault fault = body.addFault();
217:
218: Detail newDetail = fault.addDetail();
219: SOAPFactory soapFact = SOAPFactory.newInstance();
220: SOAPElement elem = soapFact.createElement("detailElem", "de",
221: "http://foo.org");
222: SOAPElement entry = newDetail.addChildElement(elem);
223: assertTrue(entry instanceof DetailEntry);
224:
225: Detail detail = fault.getDetail();
226: Iterator detailEntries = detail.getDetailEntries();
227:
228: while (detailEntries.hasNext()) {
229: Object obj = detailEntries.next();
230: assertTrue(obj instanceof DetailEntry);
231: }
232:
233: }
234:
235: public void testAddFault() throws Exception {
236: MessageFactory mf = MessageFactory
237: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
238: //MessageFactory mf = MessageFactory.newInstance();
239: SOAPMessage msg = mf.createMessage();
240: SOAPBody body = msg.getSOAPBody();
241: SOAPFactory soapFact = SOAPFactory
242: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
243: //SOAPFactory soapFact = SOAPFactory.newInstance();
244: SOAPFault fault = soapFact.createFault();
245: fault = (SOAPFault) body.addChildElement(fault);
246: //fault.addDetail();
247: //SOAPFactory soapFact1 = SOAPFactory.newInstance();
248: Detail detail = soapFact.createDetail();
249: fault.addChildElement(detail);
250: //msg.writeTo(System.out);
251: }
252:
253: public void testConvertHeaderElement() throws Exception {
254: MessageFactory mf = MessageFactory.newInstance();
255: SOAPMessage msg = mf.createMessage();
256: SOAPHeader header = msg.getSOAPHeader();
257:
258: SOAPFactory soapFact = SOAPFactory.newInstance();
259: SOAPElement elem = soapFact.createElement("headerElem", "he",
260: "http://foo.org");
261: SOAPElement se = header.addChildElement(elem);
262: assertTrue(se instanceof SOAPHeaderElement);
263: }
264:
265: public void testAddBodyElement() throws Exception {
266: MessageFactory messageFactory = MessageFactory.newInstance();
267: SOAPMessage msg = messageFactory.createMessage();
268:
269: SOAPElementFactory factory = SOAPElementFactory.newInstance();
270: SOAPElement element = factory.create("testElement");
271: element.addChildElement("child");
272:
273: SOAPBody body = msg.getSOAPBody();
274: body.addChildElement(element);
275:
276: Iterator eachBodyElement = body.getChildElements();
277:
278: assertTrue("Body element is there", eachBodyElement.hasNext());
279: SOAPBodyElement bodyElement = (SOAPBodyElement) eachBodyElement
280: .next();
281: assertEquals("testElement", bodyElement.getTagName());
282: assertFalse("Extra bodies", eachBodyElement.hasNext());
283:
284: Iterator eachChild = bodyElement.getChildElements();
285: assertTrue("First body child is there", eachChild.hasNext());
286: SOAPElement child = (SOAPElement) eachChild.next();
287: assertEquals("child", child.getTagName());
288: assertFalse("Extra children", eachChild.hasNext());
289: }
290:
291: public void testDetachChildElements() throws Exception {
292: SOAPElementFactory factory = SOAPElementFactory.newInstance();
293: SOAPElement element = factory.create("parent");
294:
295: element.addChildElement("one");
296: element.addChildElement("two");
297: element.addChildElement("three");
298:
299: SOAPElement child;
300: Iterator eachChild = element.getChildElements();
301: assertTrue("First child", eachChild.hasNext());
302: child = (SOAPElement) eachChild.next();
303: child.detachNode();
304: assertTrue("Second child", eachChild.hasNext());
305: child = (SOAPElement) eachChild.next();
306: child.detachNode();
307: assertTrue("Third child", eachChild.hasNext());
308: child = (SOAPElement) eachChild.next();
309: child.detachNode();
310: assertFalse("Extra children", eachChild.hasNext());
311:
312: eachChild = element.getChildElements();
313: assertFalse("Still has children", eachChild.hasNext());
314: }
315:
316: public void testAddChildElementWithQName() throws Exception {
317: SOAPElementFactory factory = SOAPElementFactory.newInstance();
318: SOAPElement element = factory.create("testElement");
319:
320: element.addChildElement("child", "prefix", "uri");
321:
322: Iterator eachChild = element.getChildElements(NameImpl.create(
323: "child", "prefix", "uri"));
324:
325: assertTrue("First element is there", eachChild.hasNext());
326: SOAPElement child = (SOAPElement) eachChild.next();
327: assertEquals("prefix:child", child.getTagName());
328: assertFalse("Extra elements", eachChild.hasNext());
329: }
330:
331: public void testAddDetailEntry() throws Exception {
332: SOAPFactory soapFactory = SOAPFactory.newInstance();
333: Name name = soapFactory.createName("BasicFaultElement", "ns0",
334: "http://faultservice.org/types");
335: Name name2 = soapFactory.createName("AdditionalElement", "ns0",
336: "http://faultservice.org/types");
337: Detail detail = soapFactory.createDetail();
338: DetailEntry entry = detail.addDetailEntry(name);
339:
340: SOAPElement child = entry.addChildElement("Project");
341: entry.addChildElement("Mi", "ns0",
342: "http://faultservice.org/types");
343: entry.addChildElement("Chiamo", "ns0",
344: "http://faultservice.org/types");
345: entry.addChildElement("JAXRPC", "ns0",
346: "http://faultservice.org/types");
347: entry.addChildElement("SI", "ns0",
348: "http://faultservice.org/types");
349: entry.addChildElement("Implementation", "ns0",
350: "http://faultservice.org/types");
351: child.addTextNode("Il mio nome e JAXRPC SI");
352: entry = detail.addDetailEntry(name2);
353:
354: child = entry.addChildElement("Project");
355: child.addTextNode("2 text");
356:
357: Node firstChild = detail.getFirstChild();
358: assertTrue(firstChild != null);
359: Node secondChild = firstChild.getNextSibling();
360: assertTrue(secondChild != null);
361: }
362:
363: public void testConvertElementWithXmlnsAttribute() throws Exception {
364: MessageFactory factory = MessageFactory.newInstance();
365: SOAPMessage message = factory.createMessage();
366: SOAPHeader header = message.getSOAPHeader();
367:
368: SOAPPart document = message.getSOAPPart();
369: Element element = document.createElementNS("http://bogus",
370: "b:foo");
371: element.setAttribute("xmlns", "http://bogus");
372:
373: header.insertBefore(element, null);
374:
375: Element firstChild = (Element) header.getChildElements().next();
376: assertTrue(firstChild instanceof SOAPHeaderElement);
377: }
378:
379: public void testCreateElementWithDomElement() throws Exception {
380:
381: DocumentBuilderFactory dbf = DocumentBuilderFactory
382: .newInstance();
383: dbf.setNamespaceAware(true);
384: DocumentBuilder db = dbf.newDocumentBuilder();
385: Document document = db.newDocument();
386:
387: Element element = document.createElementNS("http://bogus",
388: "b:foo");
389: SOAPFactory soapFactory = SOAPFactory.newInstance();
390: Element childElement = document.createElementNS("http://test",
391: "x:child");
392: element.appendChild(childElement);
393: element.setAttribute("junk", "true");
394:
395: Element soapElement = SOAPFactory.newInstance().createElement(
396: element);
397: assertTrue(soapElement instanceof SOAPElement);
398:
399: Element soapElementCopy = soapFactory
400: .createElement(soapElement);
401: // now copy and soapElement should be the same reference
402: assertTrue(soapElementCopy == soapElement);
403:
404: NodeList nl = soapElementCopy.getChildNodes();
405: assertTrue(nl.getLength() == 1);
406: Element testChild = (Element) nl.item(0);
407: assertTrue(testChild instanceof SOAPElement);
408: assertTrue(soapElementCopy.getAttribute("junk").equals("true"));
409: }
410:
411: public void testCreateElementWithDomElement2() throws Exception {
412:
413: DocumentBuilderFactory dbf = DocumentBuilderFactory
414: .newInstance();
415: dbf.setNamespaceAware(true);
416: DocumentBuilder db = dbf.newDocumentBuilder();
417: Document document = db.newDocument();
418:
419: Element element = document.createElementNS("http://bogus",
420: "foo");
421: SOAPFactory soapFactory = SOAPFactory.newInstance();
422: Element childElement = document.createElementNS("http://test",
423: "x:child");
424: element.appendChild(childElement);
425: element.setAttribute("junk", "true");
426:
427: Element soapElement = SOAPFactory.newInstance().createElement(
428: element);
429: assertTrue(soapElement instanceof SOAPElement);
430: assertTrue(soapElement.getPrefix() == null);
431: }
432:
433: public void testRemoveAttribute() throws Exception {
434:
435: // testcase for bugfix 6211152
436:
437: MessageFactory factory = MessageFactory.newInstance();
438: SOAPMessage message = factory.createMessage();
439: SOAPFactory soapFactory = SOAPFactory.newInstance();
440: SOAPElement soapElement = soapFactory.createElement("foo", "b",
441: "http://bogus");
442: soapElement = soapElement.addAttribute(soapFactory.createName(
443: "junk", "c", "http://bogus1"), "NOTDELETED");
444: soapElement = message.getSOAPBody()
445: .addChildElement(soapElement);
446: soapElement.removeAttribute(soapFactory.createName("junk", "c",
447: "http://bogus1"));
448: assertNull(soapElement.getAttributeValue(NameImpl.create(
449: "junk", "c", "http://bogus1")));
450: }
451:
452: public void testGetRole() throws Exception {
453:
454: MessageFactory factory = MessageFactory
455: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
456: SOAPMessage message = factory.createMessage();
457: Document document = message.getSOAPPart();
458:
459: SOAPElement element = (SOAPElement) document.createElementNS(
460: "http://bogus", "b:foo");
461: SOAPHeader header = message.getSOAPHeader();
462: SOAPHeaderElement hdrElement = (SOAPHeaderElement) header
463: .addChildElement(element);
464: hdrElement.setActor("http://bogus");
465: assertTrue(hdrElement.getRole() != null);
466: assertTrue(hdrElement.getActor() != null);
467:
468: hdrElement.setRole("http://bogus1");
469: assertTrue(hdrElement.getActor() != null);
470:
471: }
472:
473: public void testGetActor() throws Exception {
474:
475: MessageFactory factory = MessageFactory.newInstance();
476: SOAPMessage message = factory.createMessage();
477: Document document = message.getSOAPPart();
478:
479: SOAPElement element = (SOAPElement) document.createElementNS(
480: "http://bogus", "b:foo");
481: SOAPHeader header = message.getSOAPHeader();
482: SOAPHeaderElement hdrElement = (SOAPHeaderElement) header
483: .addChildElement(element);
484: hdrElement.setActor("http://bogus");
485: assertTrue(hdrElement.getActor() != null);
486: try {
487: hdrElement.getRole();
488: assertTrue(false);
489: } catch (UnsupportedOperationException ex) {
490: }
491: }
492:
493: public void testGetSetRelay11() throws Exception {
494:
495: MessageFactory factory = MessageFactory.newInstance();
496: SOAPMessage message = factory.createMessage();
497: Document document = message.getSOAPPart();
498:
499: SOAPElement element = (SOAPElement) document.createElementNS(
500: "http://bogus", "b:foo");
501: SOAPHeader header = message.getSOAPHeader();
502: SOAPHeaderElement hdrElement = (SOAPHeaderElement) header
503: .addChildElement(element);
504: try {
505: hdrElement.setRelay(true);
506: assertTrue(false);
507: } catch (UnsupportedOperationException ex) {
508:
509: }
510:
511: try {
512: hdrElement.getRelay();
513: assertTrue(false);
514: } catch (UnsupportedOperationException ex) {
515: }
516: }
517:
518: public void testGetSetRelay12() throws Exception {
519:
520: MessageFactory factory = MessageFactory
521: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
522: SOAPMessage message = factory.createMessage();
523: Document document = message.getSOAPPart();
524:
525: SOAPElement element = (SOAPElement) document.createElementNS(
526: "http://bogus", "b:foo");
527: SOAPHeader header = message.getSOAPHeader();
528: SOAPHeaderElement hdrElement = (SOAPHeaderElement) header
529: .addChildElement(element);
530: hdrElement.setRelay(true);
531: assertTrue(hdrElement.getRelay() == true);
532: }
533: }
|