package test.utils;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.axis.encoding.DeserializationContext;
import test.AxisTestBase;
import org.apache.axis.utils.XMLUtils;
import org.apache.axis.message.PrefixedQName;
import org.apache.axis.message.MessageElement;
import org.apache.axis.Constants;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.custommonkey.xmlunit.XMLUnit;
import javax.xml.parsers.SAXParser;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPBodyElement;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PipedOutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.Iterator;
public class TestXMLUtils extends AxisTestBase
{
public TestXMLUtils (String name) {
super(name);
}
public static Test suite() {
return new TestSuite(TestXMLUtils.class);
}
public void setup() {
}
public void testNewDocumentNoArgConstructor() throws Exception
{
Document doc = XMLUtils.newDocument();
assertNotNull("Did not get a new Document", doc);
}
public void testNewDocumentInputSource() throws Exception
{
Reader reader = (Reader)this.getTestXml("reader");
InputSource inputsrc = new InputSource(reader);
Document doc = XMLUtils.newDocument(inputsrc);
assertNotNull("Did not get a new Document", doc);
}
public void testNewDocumentInputStream() throws Exception
{
InputStream iostream = (InputStream)this.getTestXml("inputstream");
InputSource inputsrc = new InputSource(iostream);
Document doc = XMLUtils.newDocument(inputsrc);
assertNotNull("Did not get a new Document", doc);
}
/* This test will fail unless you are connected to the Web, so just skip
* it unless you really want to test it. When not connected to the Web you
* will get an UnknownHostException.
*/
public void testNewDocumentURI() throws Exception
{
if(isOnline()) {
String uri = "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd";
Document doc = XMLUtils.newDocument(uri);
assertNotNull("Did not get a new Document", doc);
}
}
public void testDocumentToString() throws Exception
{
Reader reader = (Reader)this.getTestXml("reader");
InputSource inputsrc = new InputSource(reader);
Document doc = XMLUtils.newDocument(inputsrc);
String xmlString = (String)this.getTestXml("string");
String result = XMLUtils.DocumentToString(doc);
assertXMLEqual("xmlString is not the same as result", xmlString, result);
}
/**
* This test method is somewhat complex, but it solves a problem people have
* asked me about, which is how to unit test a method that has void return
* type but writes its output to a writer. So half the reason for
* creating and using it here is as a reference point.
*/
public void testElementToWriter() throws Exception
{
/* Get the Document and one of its elements. */
Reader xmlReader = (Reader)this.getTestXml("reader");
InputSource inputsrc = new InputSource(xmlReader);
Document doc = XMLUtils.newDocument(inputsrc);
NodeList nl = doc.getElementsByTagName("display-name");
Element elem = (Element)nl.item(0);
String expected = "Apache-Axis";
/*
* Create a PipedOutputStream to get the output from the tested method.
* Pass the PipedOutputStream to the ConsumerPipe's constructor, which
* will create a PipedInputStream in a separate thread.
*/
PipedOutputStream out = new PipedOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(out);
ConsumerPipe cpipe = new ConsumerPipe(out);
/*
* Call the method under test, passing the PipedOutStream to trap the
* results.
*/
XMLUtils.ElementToWriter(elem, writer);
/*
* The output of the test will be piped to ConsumerPipe's PipedInputStream, which
* is used to read the bytes of the stream into an array. It then creates a
* String for comparison from that byte array.
*/
out.flush();
String result = cpipe.getResult();
//don't forget to close this end of the pipe (ConsumerPipe closes the other end).
out.close();
assertEquals("Did not get the expected result", expected, result);
}
/**
* For explanation of the methodology used to test this method, see notes in
* previous test method.
*/
public void testDocumentToStream() throws Exception
{
Reader reader = (Reader)this.getTestXml("reader");
InputSource inputsrc = new InputSource(reader);
Document doc = XMLUtils.newDocument(inputsrc);
PipedOutputStream out = new PipedOutputStream();
ConsumerPipe cpipe = new ConsumerPipe(out);
XMLUtils.DocumentToStream(doc, out);
out.flush();
String result = cpipe.getResult();
out.close();
String expected = (String)this.getTestXml("string");
assertXMLEqual("Did not get the expected result", expected, result);
}
public void testElementToString() throws Exception
{
Reader reader = (Reader)this.getTestXml("reader");
InputSource inputsrc = new InputSource(reader);
Document doc = XMLUtils.newDocument(inputsrc);
NodeList nl = doc.getElementsByTagName("display-name");
Element elem = (Element)nl.item(0);
String expected = "Apache-Axis";
String result = XMLUtils.ElementToString(elem);
assertEquals("Element tag name is not 'display-name', it is: " + elem.getTagName(),
"display-name", elem.getTagName());
assertEquals("Did not get the expected result", expected, result);
}
public void testGetInnerXMLString() throws Exception
{
Reader reader = (Reader)this.getTestXml("reader");
InputSource inputsrc = new InputSource(reader);
Document doc = XMLUtils.newDocument(inputsrc);
NodeList nl = doc.getElementsByTagName("display-name");
Element elem = (Element)nl.item(0);
String expected = "Apache-Axis";
String result = XMLUtils.getInnerXMLString(elem);
assertEquals(expected, result);
}
public void testGetPrefix() throws Exception
{
Document doc = XMLUtils.newDocument();
Element elem = doc.createElementNS("", "svg");
elem.setAttribute("xmlns:svg", "\"http://www.w3.org/2000/svg\"");
elem.setAttribute("xmlns:xlink", "\"http://www.w3.org/1999/xlink\"");
elem.setAttribute("xmlns:xhtml", "\"http://www.w3.org/1999/xhtml\"");
String expected = "svg";
String result = XMLUtils.getPrefix("\"http://www.w3.org/2000/svg\"", elem);
assertEquals("Did not get the expected result", expected, result);
expected = "xlink";
result = XMLUtils.getPrefix("\"http://www.w3.org/1999/xlink\"", elem);
assertEquals("Did not get the expected result", expected, result);
expected = "xhtml";
result = XMLUtils.getPrefix("\"http://www.w3.org/1999/xhtml\"", elem);
assertEquals("Did not get the expected result", expected, result);
}
public void testGetNamespace() throws Exception
{
String testDoc = "