JUnit Document Subset Canonicalization (using XOM 1.1)

public void assertCanonicalEquals(Document expected, Document actual, String xpath) {
        
  ByteArrayOutputStream expectedBytes = new ByteArrayOutputStream();
  ByteArrayOutputStream actualBytes = new ByteArrayOutputStream();
  
  try {
    Canonicalizer expectedCanonicalizer = new Canonicalizer(expectedBytes);
    Nodes expectedNodes = expected.query(xpath);
    expectedCanonicalizer.write(expectedNodes);
    byte[] expectedArray = expectedBytes.toByteArray();
  
    Canonicalizer actualCanonicalizer = new Canonicalizer(actualBytes);
    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, 2006 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified October 31, 2005