limitations of xml unit

我怕爱的太早我们不能终老 提交于 2019-12-25 09:17:48

问题


I’m trying to compare 2 XML files, which are similar (for testing I just changed the element order with a randomize-tool ) with XMLUnit.

Are there any limitations of the max number of elements or the max complexity of elements and child elements?

As long as my XML file is not too long and the elements are pretty straightforward everything works fine.

But as soon as there are too complex elements (up to five subelements) similar returns false and I get diffs which don’t exist.

My first impression is, that the same result happens if I get too much simple elements. But that’s nor for sure right now.

Does anybody know something about default limitations of XML Unit and is there a possibility to adjust them for my experiences?

Perhaps I should mention, that I have to ignore certain Element within the complex elements. I’m not sure if that matters. It doesn’t in the less complex elements.

package newXMLComperator;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.custommonkey.xmlunit.DetailedDiff;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.Difference;
import org.custommonkey.xmlunit.XMLUnit;
import    org.custommonkey.xmlunit.examples.RecursiveElementNameAndTextQualifier;



public class Arbeiter {

private File quelle, vergleich; 

public Arbeiter(File quelle, File vergleich) {
    this.quelle=quelle; 
    this.vergleich=vergleich;
}

public void vergleichen()
{
    long tbevore = System.currentTimeMillis();
    XMLUnit.setIgnoreAttributeOrder(Boolean.TRUE);
    XMLUnit.setIgnoreWhitespace(Boolean.TRUE);
    String f1 = lesen(quelle);
    String f2 = lesen(vergleich);

    try {
        Diff diff = new Diff(f1, f2);
        diff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
        diff.overrideDifferenceListener(new IgnoreNamedElementsDifferenceListener(findSVSW_ID(quelle)));
        DetailedDiff dd = new DetailedDiff(diff);
        boolean result = dd.similar();

        StringBuilder sb = new StringBuilder(); 
        if (result ==true)
        {
            sb.append("Die Dateien sind inhaltsgleich");
        }
        else
        {
            sb.append("Die Dateien unterscheiden sich \n \n");
            List<Difference>list = dd.getAllDifferences();
            for (Difference aktuell : list)
            {
                if (!aktuell.isRecoverable())
                {
                    sb.append("Der Ausdruck "+aktuell.getControlNodeDetail().getValue()+" wurde gesucht \n");
                    sb.append("Der Ausdruck "+aktuell.getTestNodeDetail().getValue()+" wurde gefunden \n");
                    sb.append("Xpath: "+aktuell.getTestNodeDetail().getXpathLocation()+"\n \n");

                }
            }
        }
        long tafter = System.currentTimeMillis();
        String dauer = Long.toString((tafter-tbevore)/1000);
        sb.append("Die Bearbeitung dauerte " +dauer+" Sekunden \n");
        speichern(sb.toString());

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }




}

private String lesen(File datei)
{
    String result ="";
    try {
        BufferedReader leser = new BufferedReader(new FileReader(datei));
        StringBuilder sb = new StringBuilder();
        String gelesen = leser.readLine();
        while (gelesen!=null)
        {
            sb.append(gelesen);
            gelesen=leser.readLine();
        }
        result=sb.toString();
        leser.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return result;
}

private List<String> findSVSW_ID(File datei)
{
    List<String>liste = new ArrayList<String>();
    liste.add("AUFTRAGSCHLUESSEL");
    liste.add("LIEFERUNGSCHLUESSEL");
    try {
        BufferedReader leser = new BufferedReader(new FileReader(datei));
        String gelesen = leser.readLine();
        while (gelesen!=null)
        {
            if (gelesen.contains("SVSW_ID"))
            {
                String [] teile = gelesen.split(">");
                String temp = teile[0].replaceAll("<", "");
                if (!liste.contains(temp))
                {
                    String t2=temp.replaceAll(" ", "");
                    liste.add(t2);
                }

            }
            gelesen=leser.readLine();
        }


    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    return liste;
}

private void speichern(String text)
{
    File datei = new File(quelle.getParent()+"/ausgabe.txt");
    try {
        BufferedWriter schreiber = new BufferedWriter(new FileWriter(datei));
        schreiber.write(text);
        schreiber.flush();
        schreiber.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

Thanks for help


回答1:


The only real limitation is memory. XMLUnit uses DOM under the covers and big document can require a lot of memory. But if you run out of memory the result is an exception, not spurious differences.

Also depending on your ElementQualifier comparison could become pretty slow as potentially all child-elements of a test node get compared to all child-elements of a control node. This is much more sensitive to the number of children of a single node - maybe the width of the tree - than the number of levels - the height of the tree.

It is hard to say what kind of "not existing" differences you see. Usually what happens is that XMLUnit isn't comparing the elements that you think it should because the ElementQualifier is wrong.

Please note you are using XMLUnit 1.x while the current version is 2.2.1 - 2.x has a rather different API but shares the above size/speed limitations with 1.x.




回答2:


Thanks for the quick support. I will give it a further try.

After reading 3.4.4. of http://xmlunit.sourceforge.net/userguide/html/ar01s03.html I think, that the RecursiveElementNameAndTextQuaifier is the correct one, because the order of my child nodes differs between test and control node.

Is my understanding correct, that only the RecursiveElementNameAndTextQuaifier can accomplish my requirements without subjection to the number of levels in my child nodes?

The memory usage seems to be ok. While applying the .similar() method the java VM uses round about 3 GB. That shouldn’t be a problem at all.

Thanks again



来源:https://stackoverflow.com/questions/40227367/limitations-of-xml-unit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!