1 /*------------------------------------------------------------------------------
2 Name: XmlMethodsTest.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 ------------------------------------------------------------------------------*/
6
7 package org.xmlBlaster.test.classtest;
8
9 import java.io.ByteArrayOutputStream;
10 import java.io.StringReader;
11
12 import javax.xml.parsers.DocumentBuilder;
13 import javax.xml.parsers.DocumentBuilderFactory;
14 import javax.xml.parsers.ParserConfigurationException;
15
16 import org.custommonkey.xmlunit.XMLTestCase;
17 import org.custommonkey.xmlunit.XMLUnit;
18 import java.util.logging.Logger;
19 import org.w3c.dom.Document;
20 import org.xml.sax.InputSource;
21 import org.xmlBlaster.util.Global;
22 import org.xmlBlaster.util.XmlBlasterException;
23 import org.xmlBlaster.util.XmlNotPortable;
24 import org.xmlBlaster.util.def.ErrorCode;
25
26 /**
27 * Test ClientProperty.
28 * @author <a href="mailto:michele@laghi.eu">Michele Laghi</a>
29 * <p />
30 * All methods starting with 'test' and without arguments are invoked automatically
31 * <p />
32 * Invoke: java -Djava.compiler= junit.textui.TestRunner -noloading org.xmlBlaster.test.classtest.XmlMethodsTest
33 * @see org.xmlBlaster.client.script.XmlScriptInterpreter
34 * @see <a href="http://www.xmlblaster.org/xmlBlaster/doc/requirements/engine.qos.xmlScript.html">The client.xmlScript requirement</a>
35 */
36 public class XmlMethodsTest extends XMLTestCase {
37 protected final static String ME = "XmlMethodsTest";
38 protected Global glob;
39 private static Logger log = Logger.getLogger(XmlMethodsTest.class.getName());
40
41 public XmlMethodsTest(String name) {
42 this(new Global(), name);
43 }
44
45 public XmlMethodsTest(Global global, String name) {
46 super(name);
47 XMLUnit.setIgnoreWhitespace(true);
48 }
49
50 protected void setUp() {
51 this.glob = Global.instance();
52
53 }
54
55 protected void testWriteNode() throws Exception {
56 String txt = "<xmlBlaster>\n" +
57 " <connect>\n" +
58 " <qos>\n" +
59 " <securityService type='htpasswd' version='1.0'>\n" +
60 " <![CDATA[\n" +
61 " <user>michele</user>\n" +
62 " <passwd>secret</passwd>\n" +
63 " ]]>" +
64 " </securityService>\n" +
65 " <session name='client/joe/3' timeout='3600000' maxSessions='10'" +
66 " clearSessions='false' reconnectSameClientOnly='false'/>\n" +
67 " <ptp>true</ptp>\n" +
68 " <duplicateUpdates>false</duplicateUpdates>\n" +
69 " <queue relating='callback' maxEntries='1000' maxBytes='4000000'>\n" +
70 " <callback type='IOR' sessionId='4e56890ghdFzj0' pingInterval='10000'\n" +
71 " retries='-1' delay='10000' oneway='false' dispatchPlugin='undef'>\n" +
72 " IOR:10000010033200000099000010....\n" +
73 " <burstMode collectTime='400' />\n" +
74 " <compress type='gzip' minSize='3000'/>\n" +
75 " <ptp>true</ptp>\n" +
76 " </callback>\n" +
77 " </queue>\n" +
78 " <!-- a client specific property: here it could be the bean to invoke on updates -->\n" +
79 " <clientProperty name='onMessageDefault'>beanName</clientProperty>\n" +
80 " </qos>\n" +
81 " </connect>\n" +
82 "</xmlBlaster>\n";
83
84 // assertXpathExists("/qos/securityService[@type='htpasswd']", qos);
85
86 StringReader reader = new StringReader(txt);
87 InputSource input = new InputSource(reader);
88 //input.setEncoding("UTF-8");
89 //input.setEncoding("ISO-8859-2");
90 //input.setSystemId("9999999999");
91
92 try {
93 DocumentBuilderFactory dbf = glob.getDocumentBuilderFactory();
94 DocumentBuilder db = dbf.newDocumentBuilder();
95 Document xmlDoc = db.parse(input);
96
97 ByteArrayOutputStream out = XmlNotPortable.writeNode(xmlDoc.getDocumentElement());
98 String response = new String(out.toByteArray());
99 log.info(response);
100 reader = new StringReader(response);
101 input = new InputSource(reader);
102 Document xmlDoc1 = db.parse(input);
103 this.assertXMLEqual("", xmlDoc, xmlDoc1);
104 }
105 catch (ParserConfigurationException e) {
106 log.severe("Problems when building DOM parser: " + e.toString() + "\n" + txt);
107 throw new XmlBlasterException(glob, ErrorCode.RESOURCE_CONFIGURATION, ME, "Problems when building DOM tree from your XML-ASCII string\n" + txt, e);
108 }
109 catch (java.io.IOException e) {
110 log.severe("Problems when building DOM tree from your XML-ASCII string: " + e.toString() + "\n" + txt);
111 throw new XmlBlasterException(glob, ErrorCode.RESOURCE_CONFIGURATION, ME, "Problems when building DOM tree from your XML-ASCII string:\n" + txt, e);
112 }
113 catch (org.xml.sax.SAXException e) {
114 log.warning("Problems when building DOM tree from your XML-ASCII string: " + e.toString() + "\n" + txt);
115 throw new XmlBlasterException(glob, ErrorCode.RESOURCE_CONFIGURATION, ME, "Problems when building DOM tree from your XML-ASCII string:\n" + txt, e);
116 }
117
118 }
119
120
121 /**
122 * <pre>
123 * java org.xmlBlaster.test.classtest.XmlMethodsTest
124 * </pre>
125 */
126 public static void main(String args[])
127 {
128 try {
129 Global global = new Global(args);
130 XmlMethodsTest test = new XmlMethodsTest(global, "XmlMethodsTest");
131 test.setUp();
132 test.testWriteNode();
133 test.tearDown();
134 }
135 catch(Throwable e) {
136 e.printStackTrace();
137 fail(e.toString());
138 }
139 }
140 }
syntax highlighted by Code2HTML, v. 0.9.1