0

I have series of xml files placed in 2 separate folders as below. My objective is to read each file one at a time from both folders and apply xmlunit comaprison methods.

Folder1 : actual1.xml actual2.xml actual3.xml

Folder2 : compare1.xml compare2.xml compare3.xml

Part1: Am reading each file at a time from both folders by using below script. I welcome suggestions if there are more simpler methods to do this

log.info "**********Read files from Folder1************"
def xml_file1 = []  
new File("D:\\GroovyTest\\Folder1").eachFile{ f ->  
f (f.isFile()&& f.name.contains('.xml'))  
{  
def filename = f.name[0..-1]  
xml_file1.add(filename)  
log.info filename  
}  
}
if (xml_file1.size() <1)  
{  
testRunner.fail("No request files found")  
}

log.info "**********Read files from Folder2************"

def xml_file2 = []  
new File("D:\\GroovyTest\\Folder2").eachFile{ f ->  
if (f.isFile()&& f.name.contains('.xml'))  
{  
def filename = f.name[0..-1]  
xml_file2.add(filename)  
log.info filename  
}  
}  
if (xml_file2.size() <1)  
{  
testRunner.fail("No request files found")  
}

Part2: Script to perform comparison for each combination of xml files contained in array xml_file1 and xml_file2.

Am actually stuck at this part as the below script works for single files if each xml file is kept in a string, but i have to pass an array as arguments since i have series of xml files to be compared. I get a run time error - groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.io.FileInputStream(java.util.ArrayList) error at line: 60

InputStream xml_stream1 = new FileInputStream(xml_file1)  
String xml1 = getStringFromInputStream(xml_stream1)  
InputStream xml_stream2 = new FileInputStream(xml_file2)  
String xml2 = getStringFromInputStream(xml_stream2)  
def factory = TransformerFactory.newInstance()  
def transformer = factory.newTransformer(new StreamSource(new StringReader(xslt)))  
StreamResult result_xml1 = new StreamResult(new StringWriter());  
transformer.transform(new StreamSource(new StringReader(xml1)), result_xml1)  
xml1 = result_xml1.getWriter().toString()  
StreamResult result_xml2 = new   StreamResult(newStringWriter());  
transformer.transform(new StreamSource(new StringReader(xml2)), result_xml2)  
xml2 = result_xml2.getWriter().toString()  
XMLUnit.setIgnoreComments(true)  
DifferenceListener differenceListener = newIgnoreTextAndAttributeValuesDifferenceListener();  
DetailedDiff myDiff = new DetailedDiff(new Diff(xml1, xml2));  
myDiff.overrideDifferenceListener(differenceListener);  
myDiff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());  
log.info "similar ? " + myDiff.similar()  
log.info "identical ? " + myDiff.identical()  
List allDifferences = myDiff.getAllDifferences();  
for (Object object : allDifferences) {  
Difference difference = (Difference)object;  
log.info(difference);  
}

Could someone also help me with methods to ignore empty tags during comparison? Thanks

yashm7
  • 11
  • 2
  • 1
    If you are just comparing files on disk, I do not understand why you are doing it through SoapUI. What am I missing? – SiKing Jan 30 '15 at 18:12
  • @SiKing : The requests to the webservices are fired through SOAPUI and the responses are captured and placed in local drive in xml format. – yashm7 Feb 01 '15 at 11:42
  • can someone take a look at this plz. Thanks – yashm7 Feb 03 '15 at 08:55
  • 1
    You will want to iterate over your `xml_file1` array, pick the matching file from `xml_file2` and then perform your comparison for single files. You may want to not use arrays but a map to keep the "matching" files together. – Stefan Bodewig Feb 08 '15 at 10:14

0 Answers0