PageRenderTime 26ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/ojc-core/hl7bc/hl7bcimpl/test/com/sun/jbi/hl7bc/util/XmlUtilTest.java

https://bitbucket.org/pymma/openesb-components
Java | 228 lines | 145 code | 34 blank | 49 comment | 8 complexity | da26a287f7d2917d164c1af843333f00 MD5 | raw file
  1. /*
  2. * BEGIN_HEADER - DO NOT EDIT
  3. *
  4. * The contents of this file are subject to the terms
  5. * of the Common Development and Distribution License
  6. * (the "License"). You may not use this file except
  7. * in compliance with the License.
  8. *
  9. * You can obtain a copy of the license at
  10. * https://open-jbi-components.dev.java.net/public/CDDLv1.0.html.
  11. * See the License for the specific language governing
  12. * permissions and limitations under the License.
  13. *
  14. * When distributing Covered Code, include this CDDL
  15. * HEADER in each file and include the License file at
  16. * https://open-jbi-components.dev.java.net/public/CDDLv1.0.html.
  17. * If applicable add the following below this CDDL HEADER,
  18. * with the fields enclosed by brackets "[]" replaced with
  19. * your own identifying information: Portions Copyright
  20. * [year] [name of copyright owner]
  21. */
  22. /*
  23. * @(#)XmlUtilTest.java
  24. *
  25. * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
  26. *
  27. * END_HEADER - DO NOT EDIT
  28. */
  29. package com.sun.jbi.hl7bc.util;
  30. import java.io.File;
  31. import java.io.FileInputStream;
  32. import java.io.InputStream;
  33. import junit.framework.*;
  34. import java.io.StringReader;
  35. import javax.xml.parsers.DocumentBuilder;
  36. import javax.xml.parsers.DocumentBuilderFactory;
  37. import org.w3c.dom.Document;
  38. import org.w3c.dom.Element;
  39. import org.w3c.dom.Node;
  40. import org.w3c.dom.Text;
  41. import org.xml.sax.InputSource;
  42. /**
  43. *
  44. * @author sweng
  45. */
  46. public class XmlUtilTest extends TestCase {
  47. XmlUtil instance = new XmlUtil();
  48. public XmlUtilTest(String testName) {
  49. super(testName);
  50. }
  51. protected void setUp() throws Exception {
  52. }
  53. protected void tearDown() throws Exception {
  54. }
  55. /**
  56. * Test of createDocument method, of class com.sun.jbi.hl7bc.util.XmlUtil.
  57. */
  58. public void testCreateDocument() throws Exception {
  59. System.out.println("Testing createDocument");
  60. Document result = XmlUtil.createDocument(true);
  61. assertTrue(result instanceof Document);
  62. result = XmlUtil.createDocument(true, new InputSource(new StringReader("<Foo>something</Foo>")));
  63. assertTrue(result instanceof Document);
  64. System.out.println("Successfully tested createDocument");
  65. }
  66. /**
  67. * Test of createDocumentFromXML method, of class com.sun.jbi.hl7bc.util.XmlUtil.
  68. */
  69. public void testCreateDocumentFromXML() throws Exception {
  70. System.out.println("Testing createDocumentFromXML");
  71. boolean namespaceAware = true;
  72. String xml = "<Foo> my test </Foo>";
  73. Document expResult = null;
  74. Document result = XmlUtil.createDocumentFromXML(namespaceAware, xml);
  75. Element node = result.getDocumentElement();
  76. assertEquals("Foo", node.getNodeName());
  77. Node child = node.getFirstChild();
  78. assertTrue(child instanceof Text);
  79. assertEquals(" my test ", ((Text)child).getNodeValue());
  80. assertTrue(result instanceof Document);
  81. System.out.println("Successfully tested createDocumentFromXML");
  82. }
  83. /**
  84. * Test of getText method, of class com.sun.jbi.hl7bc.util.XmlUtil.
  85. */
  86. public void testGetText() {
  87. System.out.println("Testing getText");
  88. try {
  89. InputStream is = new FileInputStream(new File("test/com/sun/jbi/hl7bc/util/testInput/TestGetText.xml"));
  90. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  91. factory.setNamespaceAware(true);
  92. DocumentBuilder builder = factory.newDocumentBuilder();
  93. Document document = builder.parse(is);
  94. Node node = document.getDocumentElement();
  95. String result = XmlUtil.getText((Node)node);
  96. assertEquals(" Hello World!!!\n \n \n \n \n \n \n", result);
  97. } catch (Exception e) {
  98. fail("Failed to test getText");
  99. }
  100. System.out.println("Successfull tested getText");
  101. }
  102. /**
  103. * Test of transformToBytes method, of class com.sun.jbi.hl7bc.util.XmlUtil.
  104. */
  105. public void testTransformToBytes() throws Exception {
  106. System.out.println("Testing transformToBytes");
  107. InputStream is = new FileInputStream(new File("test/com/sun/jbi/hl7bc/util/testInput/TestGetText.xml"));
  108. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  109. factory.setNamespaceAware(true);
  110. DocumentBuilder builder = factory.newDocumentBuilder();
  111. Document document = builder.parse(is);
  112. Node node = document.getDocumentElement();
  113. // test with omit xml declaration flag true
  114. byte[] expResult = ("<helloWorld xmlns=\"urn:Foo\"> Hello World!!!\r\n" +
  115. " <string>input0</string>\r\n" +
  116. " <byte>a</byte>\r\n" +
  117. " <short>5</short>\r\n" +
  118. " <int>12345</int>\r\n" +
  119. " <long>12345</long>\r\n" +
  120. " <float>2.22</float>\r\n" +
  121. "</helloWorld>\r\n").getBytes();
  122. byte[] result = XmlUtil.transformToBytes(node, "utf-8", true);
  123. for (int ii = 0; ii < expResult.length; ii++) {
  124. assertTrue(expResult[ii] == result[ii]);
  125. }
  126. expResult = ("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" +
  127. "<helloWorld xmlns=\"urn:Foo\"> Hello World!!!\r\n" +
  128. " <string>input0</string>\r\n" +
  129. " <byte>a</byte>\r\n" +
  130. " <short>5</short>\r\n" +
  131. " <int>12345</int>\r\n" +
  132. " <long>12345</long>\r\n" +
  133. " <float>2.22</float>\r\n" +
  134. "</helloWorld>\r\n").getBytes();
  135. result = XmlUtil.transformToBytes(node, "utf-8", false);
  136. for (int ii = 0; ii < expResult.length; ii++) {
  137. assertTrue(expResult[ii] == result[ii]);
  138. }
  139. result = XmlUtil.transformToBytes(node, "utf-8", false, "xml");
  140. for (int ii = 0; ii < expResult.length; ii++) {
  141. assertTrue(expResult[ii] == result[ii]);
  142. }
  143. expResult = ("Hello World!!!").getBytes();
  144. node = document.createTextNode("Hello World!!!");
  145. result = XmlUtil.transformToBytes(node, "utf-8", false, "text");
  146. for (int ii = 0; ii < expResult.length; ii++) {
  147. assertTrue(expResult[ii] == result[ii]);
  148. }
  149. System.out.println("Successfully tested transformToBytes");
  150. }
  151. /**
  152. * Test of transformToString method, of class com.sun.jbi.hl7bc.util.XmlUtil.
  153. */
  154. public void testTransformToString() throws Exception {
  155. System.out.println("Testing transformToString");
  156. InputStream is = new FileInputStream(new File("test/com/sun/jbi/hl7bc/util/testInput/TestGetText.xml"));
  157. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  158. factory.setNamespaceAware(true);
  159. DocumentBuilder builder = factory.newDocumentBuilder();
  160. Document document = builder.parse(is);
  161. Node node = document.getDocumentElement();
  162. // test with omit xml declaration flag true
  163. String expResult = "<helloWorld xmlns=\"urn:Foo\"> Hello World!!!\r\n" +
  164. " <string>input0</string>\r\n" +
  165. " <byte>a</byte>\r\n" +
  166. " <short>5</short>\r\n" +
  167. " <int>12345</int>\r\n" +
  168. " <long>12345</long>\r\n" +
  169. " <float>2.22</float>\r\n" +
  170. "</helloWorld>\r\n";
  171. String result = XmlUtil.transformToString(node, "utf-8", true);
  172. System.out.println("expResult is [" + expResult + "]");
  173. System.out.println("result is [" + result + "]");
  174. assertEquals(expResult, result);
  175. expResult = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" +
  176. "<helloWorld xmlns=\"urn:Foo\"> Hello World!!!\r\n" +
  177. " <string>input0</string>\r\n" +
  178. " <byte>a</byte>\r\n" +
  179. " <short>5</short>\r\n" +
  180. " <int>12345</int>\r\n" +
  181. " <long>12345</long>\r\n" +
  182. " <float>2.22</float>\r\n" +
  183. "</helloWorld>\r\n";
  184. result = XmlUtil.transformToString(node, "utf-8", false);
  185. assertEquals(expResult, result);
  186. result = XmlUtil.transformToString(node, "utf-8", false, "xml");
  187. assertEquals(expResult, result);
  188. expResult = "Hello World!!!";
  189. node = document.createTextNode("Hello World!!!");
  190. result = XmlUtil.transformToString(node, "utf-8", false, "text");
  191. assertEquals(expResult, result);
  192. System.out.println("Successfully tested transformToString");
  193. }
  194. }