<commit_before>package com.evolveum.midpoint.common.test;

import static junit.framework.Assert.fail;

import java.io.File;
import java.io.FileInputStream;
import java.io.StringBufferInputStream;
import java.util.List;

import org.custommonkey.xmlunit.DetailedDiff;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.Difference;
import org.custommonkey.xmlunit.DifferenceConstants;
import org.custommonkey.xmlunit.XMLUnit;
import org.xml.sax.InputSource;

import com.evolveum.midpoint.common.diff.MidPointDifferenceListener;
import com.evolveum.midpoint.common.diff.OidQualifier;

public class XmlAsserts {
	
    private static void setupXmlUnitForTest() {
        //XmlUnit setup
        //Note: compareUnmatched has to be set to false to calculate diff properly, to avoid matching of nodes that are not comparable
        XMLUnit.setCompareUnmatched(false);
        XMLUnit.setIgnoreAttributeOrder(true);
        XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);
        XMLUnit.setIgnoreComments(true);
        XMLUnit.setIgnoreWhitespace(true);
        XMLUnit.setNormalize(true);
        XMLUnit.setNormalizeWhitespace(true);
    }

    private static String readFileAsString(File fileNewXml) throws java.io.IOException{
        byte[] buffer = new byte[(int) fileNewXml.length()];
        FileInputStream f = new FileInputStream(fileNewXml);
        f.read(buffer);
        return new String(buffer);
    }
    
    public static void assertPatch(File fileNewXml, String patchedXml) throws Exception {
    	assertPatch(readFileAsString(fileNewXml), patchedXml);

    }

    public static void assertPatch(String origXml, String patchedXml) throws Exception {
        setupXmlUnitForTest();
        Diff d = new Diff(new InputSource(new StringBufferInputStream(origXml)), new InputSource(new StringBufferInputStream(patchedXml)));
        DetailedDiff dd = new DetailedDiff(d);
        dd.overrideElementQualifier(new OidQualifier());
        dd.overrideDifferenceListener(new MidPointDifferenceListener());
        List<Difference> differences = dd.getAllDifferences();

        for (Difference diff : differences) {
            switch (diff.getId()) {
//                case DifferenceConstants.NAMESPACE_PREFIX_ID:
//                    //ignore namespaces
//                    //FIXME: ^^^
//                    break;
                case DifferenceConstants.ATTR_VALUE_ID:
                    if (diff.getControlNodeDetail().getNode().getNodeName().contains("type")) {
                        //ignore attribute values for xsi type, because of namespaces
                        //FIXME: ^^^
                        break;
                    }
                case DifferenceConstants.CHILD_NODELIST_SEQUENCE_ID:
                case DifferenceConstants.SCHEMA_LOCATION_ID:
                    break;
                default:
                    fail(diff.toString());
            }
        }

    }

}
<commit_msg>preparing add user with template test finished.<commit_after>package com.evolveum.midpoint.common.test;

import static junit.framework.Assert.fail;

import java.io.File;
import java.io.FileInputStream;
import java.io.StringBufferInputStream;
import java.util.List;

import org.custommonkey.xmlunit.DetailedDiff;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.Difference;
import org.custommonkey.xmlunit.DifferenceConstants;
import org.custommonkey.xmlunit.XMLUnit;
import org.xml.sax.InputSource;

import com.evolveum.midpoint.common.diff.MidPointDifferenceListener;
import com.evolveum.midpoint.common.diff.OidQualifier;

public class XmlAsserts {

	private static void setupXmlUnitForTest() {
		// XmlUnit setup
		// Note: compareUnmatched has to be set to false to calculate diff
		// properly, to avoid matching of nodes that are not comparable
		XMLUnit.setCompareUnmatched(false);
		XMLUnit.setIgnoreAttributeOrder(true);
		XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);
		XMLUnit.setIgnoreComments(true);
		XMLUnit.setIgnoreWhitespace(true);
		XMLUnit.setNormalize(true);
		XMLUnit.setNormalizeWhitespace(true);
	}

	private static String readFileAsString(File fileNewXml) throws java.io.IOException {
		byte[] buffer = new byte[(int) fileNewXml.length()];
		FileInputStream f = new FileInputStream(fileNewXml);
		f.read(buffer);
		return new String(buffer);
	}

	public static void assertPatch(File fileNewXml, String patchedXml) throws Exception {
		assertPatch(readFileAsString(fileNewXml), patchedXml);

	}

	public static void assertPatch(String origXml, String patchedXml) throws Exception {
		setupXmlUnitForTest();
		Diff d = new Diff(new InputSource(new StringBufferInputStream(origXml)), new InputSource(
				new StringBufferInputStream(patchedXml)));
		DetailedDiff dd = new DetailedDiff(d);
		dd.overrideElementQualifier(new OidQualifier());
		dd.overrideDifferenceListener(new MidPointDifferenceListener());
		List<Difference> differences = dd.getAllDifferences();

		for (Difference diff : differences) {
			switch (diff.getId()) {
				// case DifferenceConstants.NAMESPACE_PREFIX_ID:
				// //ignore namespaces
				// //FIXME: ^^^
				// break;
				case DifferenceConstants.ATTR_VALUE_ID:
					if (diff.getControlNodeDetail().getNode().getNodeName().contains("type")) {
						// ignore attribute values for xsi type, because of
						// namespaces
						// FIXME: ^^^
						break;
					}
				case DifferenceConstants.CHILD_NODELIST_SEQUENCE_ID:
				case DifferenceConstants.SCHEMA_LOCATION_ID:
					break;
				default:
					fail(diff.toString());
			}
		}

	}

}

{'fraction_non_alphanumeric': 0.09308606409794742, 'fraction_numerical': 0.0, 'mean_word_length': 3.4157392686804453, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7252358', 'n_tokens_mistral': 1681, 'n_tokens_neox': 1548, 'n_words': 329}