JUnit Exclusive Document Subset Canonicalization

public void assertCanonicalEquals(Document expected, Document actual, String xpath) {
    
    ByteArrayOutputStream expectedBytes = new ByteArrayOutputStream();
    ByteArrayOutputStream actualBytes = new ByteArrayOutputStream();
    
    try {
        Canonicalizer expectedCanonicalizer = new Canonicalizer(
          expectedBytes, Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION);
        Nodes expectedNodes = expected.query(xpath);
        expectedCanonicalizer.write(expectedNodes);
        byte[] expectedArray = expectedBytes.toByteArray();
    
        Canonicalizer actualCanonicalizer = new Canonicalizer(
          actualBytes, Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION);
        Nodes actualNodes = actual.query(xpath);
        actualCanonicalizer.write(actualNodes);
        byte[] actualArray = actualBytes.toByteArray();
    
        assertEquals(expectedArray.length, actualArray.length);
        for (int i = 0; i < expectedArray.length; i++) {
            assertEquals(expectedArray[i], actualArray[i]);
        }
    }
    catch (IOException ex) {
        fail("IOException while canonicalizing");
    }        
    
} 

Previous | Next | Top | Cafe con Leche

Copyright 2005 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified August 16, 2005