001: package org.objectweb.celtix.bus.bindings.xml;
002:
003: import java.io.*;
004: import javax.xml.namespace.QName;
005: import org.w3c.dom.*;
006: import junit.framework.TestCase;
007:
008: import org.objectweb.celtix.helpers.XMLUtils;
009:
010: public class XMLMessageTest extends TestCase {
011:
012: protected XMLMessageFactory msgFactory = XMLMessageFactory
013: .newInstance();
014: private XMLUtils xmlUtils = new XMLUtils();
015:
016: protected void setUp() throws Exception {
017: }
018:
019: public void testXMLMessageFactory() {
020: assertNotNull(msgFactory);
021: }
022:
023: public void testDefaultXMLMessage() throws Exception {
024: XMLMessage message = msgFactory.createMessage();
025: assertNotNull(message);
026: assertNotNull(message.getRoot());
027: String expected = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
028: ByteArrayOutputStream out = new ByteArrayOutputStream();
029: message.writeTo(out);
030: assertTrue(expected.equalsIgnoreCase(out.toString()));
031: }
032:
033: public void testStreamXMLMessage() {
034: InputStream is = getClass().getResourceAsStream(
035: "resources/SayHiWrappedResp.xml");
036: XMLMessage message = msgFactory.createMessage(is);
037: assertNotNull(message);
038: assertNotNull(message.getRoot());
039: }
040:
041: public void testXMLFaultMessage() throws Exception {
042: XMLMessage message = msgFactory.createMessage();
043: XMLFault fault = message.addFault();
044: assertNotNull(fault);
045: fault.addFaultString("exception raised");
046: assertEquals("exception raised", fault.getFaultString());
047: ByteArrayOutputStream out = new ByteArrayOutputStream();
048: message.writeTo(out);
049: String expected = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
050: expected += "<XMLFault xmlns=\"http://celtix.objectweb.org/bindings/xmlformat\">";
051: expected += "<faultstring>exception raised</faultstring></XMLFault>";
052: assertTrue(expected.equalsIgnoreCase(out.toString()));
053: }
054:
055: public void testXMLFaultDetail() throws Exception {
056: XMLMessage message = msgFactory.createMessage();
057: XMLFault fault = message.addFault();
058: assertNotNull(fault);
059:
060: fault.addFaultDetail();
061: String namespace = "http://objectweb.org/hello_world_soap_http/types";
062: Node detailNode = xmlUtils.createElementNS(
063: fault.getFaultRoot(), new QName(namespace,
064: "faultDetail"));
065: Node minor = xmlUtils.createElementNS(detailNode, new QName(
066: namespace, "minor"));
067: Node major = xmlUtils.createElementNS(detailNode, new QName(
068: namespace, "major"));
069:
070: Text minorValue = xmlUtils.createTextNode(minor, "1");
071: Text majorValue = xmlUtils.createTextNode(major, "2");
072: minor.appendChild(minorValue);
073: major.appendChild(majorValue);
074: detailNode.appendChild(minor);
075: detailNode.appendChild(major);
076:
077: fault.appendFaultDetail(detailNode);
078:
079: ByteArrayOutputStream out = new ByteArrayOutputStream();
080: message.writeTo(out);
081:
082: InputStream is = getClass().getResourceAsStream(
083: "resources/xmlfaultdetail.xml");
084: Document expectDOM = xmlUtils.parse(is);
085:
086: assertEquals(xmlUtils.toString(expectDOM), message.toString());
087: }
088:
089: public void testCreateFaultXMLMessage() throws Exception {
090: InputStream is = getClass().getResourceAsStream(
091: "resources/xmlfault.xml");
092: XMLMessage message = msgFactory.createMessage(is);
093: assertNotNull(message);
094:
095: XMLFault xmlFault = message.getFault();
096: assertNotNull(xmlFault);
097:
098: assertEquals(
099: "org.objectweb.hello_world_xml_http.wrapped.PingMeFault: PingMeFault raised by server",
100: xmlFault.getFaultString());
101: assertNotNull(xmlFault.getFaultDetail());
102: }
103: }
|