Commit a50bf0ce authored by Pakulin's avatar Pakulin
Browse files

Added XmlUnit library and implemented XmlDiff using XmlUnit::Diff class

parent e65006ac
Loading
Loading
Loading
Loading
+95.4 KiB

File added.

No diff preview for this file type.

+53 −2
Original line number Diff line number Diff line
package org.etsi.mts.ttcn.part9.xmldiff;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;

import org.custommonkey.xmlunit.DetailedDiff;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.Difference;
import org.xml.sax.SAXException;

/** This class implements an algorithm to compare two XML files.
 * 
 * It is used as a utility in the ETSI STF on conformance testing for XSD processing (TTCN3 Part 9 specification).
@@ -7,6 +24,8 @@ package org.etsi.mts.ttcn.part9.xmldiff;
 *
 */
public class XmlDiff {
	protected File referenceXmlFile;

	/** Initialize the diff engine.
	 * 
	 * If {@code xsdFileNames} is <code>null</code> then the value of {@code xsdSearchPath}
@@ -20,6 +39,13 @@ public class XmlDiff {
	 * 
	 */
	public XmlDiff(String referenceXmlFile, String[] xsdFileNames, String[] xsdSearchPath) {
		this.referenceXmlFile = new File(referenceXmlFile);
		if (!this.referenceXmlFile.exists()) {
			throw new IllegalArgumentException("No such file: " + this.referenceXmlFile.getAbsolutePath());
		}
		if (!this.referenceXmlFile.canRead()) {
			throw new IllegalArgumentException("Can't read: " + this.referenceXmlFile.getAbsolutePath());
		}
		
	}
	
@@ -27,8 +53,33 @@ public class XmlDiff {
	 * @param input the text of the XML document
	 * @param errorMessage container to store details of differences. May be <code>null</code>
	 * @return <code>true</code> if the documents match and <code>false</false> otherwise.
	 * @throws XmlDiffError if an error occurs before or during diffing
	 */
	public boolean diff(String input, StringBuilder diffDetails) {
		return true;
	public boolean diff(String input, StringBuilder diffDetails) throws XmlDiffError {
		InputStream stream;
		try {
			stream = new FileInputStream(referenceXmlFile);
		} catch (FileNotFoundException e) {
			throw new XmlDiffError(e); 
		}
		Reader rd = new InputStreamReader(stream, StandardCharsets.UTF_8);
		Diff differ = null;
		try {
			differ = new Diff(rd, new StringReader(input));
		} catch (SAXException e) {
			throw new XmlDiffError("Failed to parse XML", e);
		} catch (IOException e) {
			throw new XmlDiffError("Failed to read XML", e);
		}
		boolean result = differ.identical();
		if (!result) {
			DetailedDiff details = new DetailedDiff(differ);
			@SuppressWarnings("unchecked")
			List<Difference> diffs = details.getAllDifferences();
			for (Difference diff : diffs) {
				diffDetails.append(diff.getDescription());
			}
		}
		return result;
	}
}
+26 −0
Original line number Diff line number Diff line
package org.etsi.mts.ttcn.part9.xmldiff;

public class XmlDiffError extends Exception {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1525558587675191780L;

	public XmlDiffError() {
		super();
	}

	public XmlDiffError(String message, Throwable cause) {
		super(message, cause);
	}

	public XmlDiffError(String message) {
		super(message);
	}

	public XmlDiffError(Throwable cause) {
		super(cause);
	}

}