setIgnoreWhitespace(true);
// The file "Teste.xml" can be found at project`s class directory
assertXMLEqual(new FileReader(getClass().getResource("Teste.xml").getFile()) ,new StringReader(tXMLCode));
}
WHY DOES IT HAPPENS? AM I DOING SOMETHING WRONG? DOES MY CODE NEEDS SOMETHING MORE?
This is the message that is given to me:
1) testeExecuteStart(br.ufrj.siga.turma.TestXMLTurmaManagerSessionBean)junit.framework.AssertionFailedError: org.custommonkey.xmlunit.Diff Expected: <mvc>
</mvc>, but was: <mvc> </mvc>
at org.custommonkey.xmlunit.XMLTestCase.assertXMLEqual(XMLTestCase.java:112)
at br.ufrj.siga.turma.TestXMLTurmaManagerSessionBean.testeExecuteStart(TestXMLTurmaManagerSessionBean.java:941)
FAILURES!!!
Does anybody can help me? Thanks!
:)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2002-04-25
I found I had to pre process my files to removed tabs and white spaces between elements, otherwise they were read as text nodes. Here's my preprocessor. I have not had a problem since I used this:
package com.yellowshirt.util.xml;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import java.util.*;
/**
* This is a utility class that can be used to remove formatting from an XML source file.
* This class was developed to be used in conjuction with XMLUnit API
* http://xmlunit.sourceforge.net/ for unit testing XML. The API was failing on
* comparisons where XML files were identical except for spaces and line breaks
* between element tags. It was interpreting these as text nodes, and causing
* comparisons to fail. Please see the XMLUnit API for information on using
* it to test XML documents. Use this class to pre-process your XML files
* so that XMLUnit will not report false differences in your XML files.
*
*/
public class XFileCleanUp {
/**
* Removes all non-characters between element tags and around text nodes. The
* resulting file will be one long String i.e
* <tag1>
* <tag2> My Text
* </tag2>
* </tag1>
*
* Will become:
*
* <tag1><tag2>My Text</tag2></tag1>
*
* @param in the input file to be processed
* @param out the output file to write the processed file to.
*
*/
public synchronized static void normalizeXmlFile(File in, File out) throws Exception {
ArrayList list = new ArrayList();
int nextChar = -1;
StringBuffer buff = new StringBuffer();
BufferedReader reader = new BufferedReader(new FileReader(in));
nextChar = reader.read();
while (nextChar != -1) {
buff = new StringBuffer();
if (nextChar == '<') {
buff.append((char)nextChar);
nextChar = reader.read();
while (nextChar != '>') {
buff.append((char)nextChar);
nextChar = reader.read();
}
buff.append((char)nextChar);//add closing '>'
list.add(buff.toString().trim());//add this tag to the array list
processText(nextChar, reader, buff, list);//now add text node if there is one
}
else {
nextChar = reader.read();
}
}
FileWriter writer = new FileWriter(out, false);
for (int i = 0; i < list.size(); i++) {
writer.write((String)list.get(i));
}
writer.close();
}
private synchronized static void processText(int nextChar, BufferedReader reader, StringBuffer buff, ArrayList list) throws Exception {
StringBuffer textBuff = new StringBuffer();
reader.mark(1);//mark current position
nextChar = reader.read();
if (nextChar == '<') {
reader.reset();
return;
}
else {
while (nextChar != -1 && nextChar != '<') {
textBuff.append((char)nextChar);
reader.mark(1);//mark current position
nextChar = reader.read();
}
reader.reset();
String textString = textBuff.toString().trim();
if (textString.length() == 0)
return;
else {
list.add(textString);
return;
}
}
}
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This method has been enhanced in the 0.6 distribution so that empty Text nodes are stripped from Documents compared in a Diff.
Thanks to aakture for pointing the way!
Tim
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2002-07-18
In distribution 0.6 the problem has not solved.
I found out that a similar test always returns false. The assertion failed even if the XML files are similar. Is this another bug or did I make something wrong? Has anyone had the same problem?
Ralf
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Without some more context it's hard to dive in with a direct answer. Can you post some test methods that break when you expect them to pass?
The general situation that started this thread is this... When parsing XML (e.g. from a file) that contains line breaks and form feeds, the parser is allowed to interpret those characters as empty TEXT elements. These empty elements are _not_ ignored by default when perfoperfrming a diff with XMLUnit. However, from the 0.6 release onwards calling the static method XMLUnit.setIgnoreWhitespace(true) _will_ cause the empty TEXT elements to be ignored. (Have a look at the method testXMLUnitDoesNotWorkWellWithFiles() in test_Diff.java.)
Hope this helps,
Tim
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'd tried to make my tests with JXUnit with no success while using comparasion of files, just with strings.
The code that works well:
public void testeExecuteStart() throws Exception{
String tXMLCode;
String tXMLCompare;
tXMLCode = "<?xml version='1.0' encoding='iso-8859-1' ?>" +
"<mvc>" +
" <model>" +
" <EDIT>" +
" </EDIT>" +
" <list>" +
" </list>" +
" <select_>" +
" <JavaBean _class='TurmaSelect' _id='' table='Turma' label='Turma'>" +
" <properties>" +
" <property name='codigoTurma' type='java.lang.String' value='' />" +
" <property name='codigoTurmaRestricao' type='java.lang.String' value='' />" +
" </properties>" +
" </JavaBean>" +
" </select_>" +
" </model>" +
"</mvc>";
tXMLCompare = tXMLCode;
setIgnoreWhitespace(true);
assertXMLEqual(tXMLCompare,tXMLCode);
}
The code that does not works well:
public void testeExecuteStart() throws Exception{
String tXMLCode;
tXMLCode = "<?xml version='1.0' encoding='iso-8859-1' ?>" +
"<mvc>" +
" <model>" +
" <EDIT>" +
" </EDIT>" +
" <list>" +
" </list>" +
" <select_>" +
" <JavaBean _class='TurmaSelect' _id='' table='Turma' label='Turma'>" +
" <properties>" +
" <property name='codigoTurma' type='java.lang.String' value='' />" +
" <property name='codigoTurmaRestricao' type='java.lang.String' value='' />" +
" </properties>" +
" </JavaBean>" +
" </select_>" +
" </model>" +
"</mvc>";
setIgnoreWhitespace(true);
// The file "Teste.xml" can be found at project`s class directory
assertXMLEqual(new FileReader(getClass().getResource("Teste.xml").getFile()) ,new StringReader(tXMLCode));
}
WHY DOES IT HAPPENS? AM I DOING SOMETHING WRONG? DOES MY CODE NEEDS SOMETHING MORE?
This is the message that is given to me:
1) testeExecuteStart(br.ufrj.siga.turma.TestXMLTurmaManagerSessionBean)junit.framework.AssertionFailedError: org.custommonkey.xmlunit.Diff Expected: <mvc>
</mvc>, but was: <mvc> </mvc>
at org.custommonkey.xmlunit.XMLTestCase.assertXMLEqual(XMLTestCase.java:112)
at br.ufrj.siga.turma.TestXMLTurmaManagerSessionBean.testeExecuteStart(TestXMLTurmaManagerSessionBean.java:941)
FAILURES!!!
Does anybody can help me? Thanks!
:)
I found I had to pre process my files to removed tabs and white spaces between elements, otherwise they were read as text nodes. Here's my preprocessor. I have not had a problem since I used this:
package com.yellowshirt.util.xml;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import java.util.*;
/**
* This is a utility class that can be used to remove formatting from an XML source file.
* This class was developed to be used in conjuction with XMLUnit API
* http://xmlunit.sourceforge.net/ for unit testing XML. The API was failing on
* comparisons where XML files were identical except for spaces and line breaks
* between element tags. It was interpreting these as text nodes, and causing
* comparisons to fail. Please see the XMLUnit API for information on using
* it to test XML documents. Use this class to pre-process your XML files
* so that XMLUnit will not report false differences in your XML files.
*
*/
public class XFileCleanUp {
/**
* Removes all non-characters between element tags and around text nodes. The
* resulting file will be one long String i.e
* <tag1>
* <tag2> My Text
* </tag2>
* </tag1>
*
* Will become:
*
* <tag1><tag2>My Text</tag2></tag1>
*
* @param in the input file to be processed
* @param out the output file to write the processed file to.
*
*/
public synchronized static void normalizeXmlFile(File in, File out) throws Exception {
ArrayList list = new ArrayList();
int nextChar = -1;
StringBuffer buff = new StringBuffer();
BufferedReader reader = new BufferedReader(new FileReader(in));
nextChar = reader.read();
while (nextChar != -1) {
buff = new StringBuffer();
if (nextChar == '<') {
buff.append((char)nextChar);
nextChar = reader.read();
while (nextChar != '>') {
buff.append((char)nextChar);
nextChar = reader.read();
}
buff.append((char)nextChar);//add closing '>'
list.add(buff.toString().trim());//add this tag to the array list
processText(nextChar, reader, buff, list);//now add text node if there is one
}
else {
nextChar = reader.read();
}
}
FileWriter writer = new FileWriter(out, false);
for (int i = 0; i < list.size(); i++) {
writer.write((String)list.get(i));
}
writer.close();
}
private synchronized static void processText(int nextChar, BufferedReader reader, StringBuffer buff, ArrayList list) throws Exception {
StringBuffer textBuff = new StringBuffer();
reader.mark(1);//mark current position
nextChar = reader.read();
if (nextChar == '<') {
reader.reset();
return;
}
else {
while (nextChar != -1 && nextChar != '<') {
textBuff.append((char)nextChar);
reader.mark(1);//mark current position
nextChar = reader.read();
}
reader.reset();
String textString = textBuff.toString().trim();
if (textString.length() == 0)
return;
else {
list.add(textString);
return;
}
}
}
}
This method has been enhanced in the 0.6 distribution so that empty Text nodes are stripped from Documents compared in a Diff.
Thanks to aakture for pointing the way!
Tim
In distribution 0.6 the problem has not solved.
I found out that a similar test always returns false. The assertion failed even if the XML files are similar. Is this another bug or did I make something wrong? Has anyone had the same problem?
Ralf
Without some more context it's hard to dive in with a direct answer. Can you post some test methods that break when you expect them to pass?
The general situation that started this thread is this... When parsing XML (e.g. from a file) that contains line breaks and form feeds, the parser is allowed to interpret those characters as empty TEXT elements. These empty elements are _not_ ignored by default when perfoperfrming a diff with XMLUnit. However, from the 0.6 release onwards calling the static method XMLUnit.setIgnoreWhitespace(true) _will_ cause the empty TEXT elements to be ignored. (Have a look at the method testXMLUnitDoesNotWorkWellWithFiles() in test_Diff.java.)
Hope this helps,
Tim