Here is a JUnit test case to test the OM's conformance against W3C test suite (available at the url http://dev.w3.org/cvsweb/2001/XML-Test-Suite/). Make sure to have in the directory of test case a folder named "XMLSuite" which contains in it all the unzipped files of the W3C XML test suite.
Here is the listing of the test case code.
/*
* Created on Apr 8, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package junittesting;
import java.io.File;
import org.custommonkey.xmlunit.XMLTestCase;
import org.custommonkey.xmlunit.Diff;
import org.apache.axis.om.OMElement;
import org.apache.axis.om.impl.llom.factory.OMXMLBuilderFactory;
import org.apache.axis.om.impl.llom.builder.StAXOMBuilder;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.stream.XMLStreamException;
import org.apache.axis.om.OMFactory;
import java.io.FileOutputStream;
import java.io.FileReader;
/**
* @author sunja07
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class XMLConformanceTest extends XMLTestCase {
private static int successCount=0;
private static int parsedCount=0;
private static int fileCount=0;
/**
* @param name
*/
public XMLConformanceTest(String name) {
super(name);
}
public void setUp() {
}
public void testXMLConformance() throws Exception{
File testSuiteDirectory = new File("XMLSuite");
ProcessDir(testSuiteDirectory);
System.out.println("Total file count is " + fileCount);
System.out.println("Parsed count is " + parsedCount +". This is just partial success");
System.out.println("Complete success count is " + successCount);
}
public void ProcessDir(File dir) throws Exception
{
if (dir.isDirectory()) {
//System.out.println("Processing Directory: "+dir.getAbsolutePath() );
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
File child = (new File(dir, children[i]));
ProcessDir(child);
}
} else { //meaning you got a file
//check if it's xml file
String absPath = dir.getAbsolutePath();
if(absPath.endsWith(".xml")) {
//process it
testSingleFileConformance(absPath);
} else {
//ignore non .xml files
}
}
}
public void testSingleFileConformance(String absolutePath) throws Exception{
OMElement rootElement;
fileCount++;
//get a stax om builder out of the file. LOAD step
try {
StAXOMBuilder staxOMBuilder=OMXMLBuilderFactory.createStAXOMBuilder( OMFactory.newInstance(), XMLInputFactory.newInstance().createXMLStreamReader(new FileReader(absolutePath)));
rootElement = staxOMBuilder.getDocumentElement();
} catch (Exception e) {
System.err.println("Exception trying to get hold of rootElement: " + e.getMessage());
System.err.println("in file: "+ absolutePath +"\n");
return;
}
//we will write output into the file named TempOutputFile.txt in current directory. SAVE step
String tempFile = "TempOutputFile.txt";
XMLStreamWriter writer;
try {
writer = XMLOutputFactory.newInstance().createXMLStreamWriter(new FileOutputStream(tempFile));
rootElement.serialize(writer, true);
}catch(XMLStreamException e) {
System.err.println("Error in creating XMLStreamWriter to write parsed xml into");
return;
} catch (Exception e) {
System.err.println("Exception while serializing: " + e.getMessage());
System.err.println("in file: "+ absolutePath +"\n");
return;
}
writer.flush();
writer.close();
parsedCount++;
//Comparing the equality of the TempOutputFile.txt and the input xml is due. COMPARE step
Diff diff;
try {
diff = compareXML(new FileReader(absolutePath), new FileReader("TempOutputFile.txt"));
} catch (Exception e) {
System.out.println("Error comparing original and generated files for: "+absolutePath);
return;
}
try {
assertXMLEqual(diff,true);
successCount++;
} catch (Error e) {
System.out.println("XMLEquality failed for file: "+absolutePath);
return;
}
}
public void tearDown() {
}
}