redaLyazidi
redaLyazidi

Reputation: 33

JUnit test failure while comparing two similar xml files

I tried to realise test with dbunit but I have a failure.

To sum up

But they aren't. There no difference to a naked eye, even diff with eclipse or notepad++ don't show anything.

I followed this tutorial : http://www.scub-foundation.org/accueil/tutoriaux/tutorial-dbunit/

I warn you it's a french tutorial, I suspect the encoding of the second file isn't UTF-8, but I didn't manage to force the writing in UTF-8.

All the files and the code are the same than in the tutorial except TestDBUnit.java.
Here's the code :

 public void testExportData() throws Exception {
    // On récupère le jeu de données du fichier XML
    IDataSet dataSet = getDatabaseDataSet();

    // Fichier XML du jeu de données d'import
    File inputFile = new File(INPUT_DATA_SET_FILENAME);
    // On vérifie que le fichier existe
    assertNotNull(inputFile);
    // Fichier XML du jeu de données d'export
    File outputFile = new File(OUTPUT_DATA_SET_FILENAME);
    FileOutputStream(outputFile), "UTF-8")));
    FlatXmlDataSet.write(dataSet, new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));
    // On compare les deux fichiers XML pour vérifier qu'ils sont identiques
    String inputDataSetString = FileUtils.readFileToString(inputFile/*,
            "UTF-8"*/).replace("  ", "\t").trim();
    String outputDataSetString = FileUtils.readFileToString(outputFile/*,
            "UTF-8"*/).replace("  ", "\t").trim();
    assertEquals(inputDataSetString, outputDataSetString);//The failure

I just add an OutputStreamWriter as Wrapper to force the encoding but it didn't change anything.

If you want any further details please do not hesitate.

Upvotes: 3

Views: 1900

Answers (1)

Duncan Jones
Duncan Jones

Reputation: 69379

Using string equality to compare XML files is unwise, unless you are testing an XML-generation library and care about pretty printing.

In all other cases, you probably care about the semantic equality of the two files (i.e. do they contain the same data?). To aid with such a comparison, consider using XMLUnit. This will compare the contents of your files, without worrying about whitespace or encoding.

Upvotes: 4

Related Questions