private <T,D extends GenericDAO>void roundTrip(XmlsForTesting testXMLs,D dao,ObjectRetriever<T,D> retriever,Marshaller marshaller,Unmarshaller unmarshaller){
  for (  String testXml : testXMLs.getXmls()) {
    String inputXml=testXml.trim();
    try {
      T persistableObject=(T)unmarshal(unmarshaller,inputXml);
      String unpersistedOutputXml=marshal(marshaller,persistableObject);
      Diff myDiff=new Diff(inputXml,unpersistedOutputXml);
      if (!myDiff.similar()) {
        LOGGER.error("\nNot similar: ROUND-TRIP, UNPERSISTED:\n====================\nInput XML:\n" + inputXml + "\n\nOutput XML:\n"+ unpersistedOutputXml);
        LOGGER.error("\ntoString(): \n\n" + persistableObject.toString());
      }
 else       if (!myDiff.identical()) {
        LOGGER.error("\nNot identical: ROUND-TRIP, UNPERSISTED:\n====================\nInput XML:\n" + inputXml + "\n\nOutput XML:\n"+ unpersistedOutputXml);
        LOGGER.error("\ntoString(): \n\n" + persistableObject.toString());
      }
      assertTrue("Round trip XML (not persistence) should be similar." + myDiff,myDiff.similar());
      assertTrue("Round trip XML (not persistence) should be identical." + myDiff,myDiff.identical());
      dao.insert(persistableObject);
      Long id=retriever.getPrimaryKey(persistableObject);
      assertNotNull("The stored persistableObject should not have a null primary key·",id);
      LOGGER.debug("Primary key of persisted object (mid round-trip following insert): " + id);
      T retrievedPersistable=retriever.getObjectByPrimaryKey(dao,id);
      assertNotNull("The retrieved object should not be null",retrievedPersistable);
      assertEquals("The retrieved object should be equal to the original persistableObject.",persistableObject,retrievedPersistable);
      String persistedOutputXML=marshal(marshaller,retrievedPersistable);
      myDiff=new Diff(inputXml,persistedOutputXML);
      if (!myDiff.similar()) {
        LOGGER.error("\nNot similar: ROUND-TRIP, PERSISTED:\n====================\nInput XML:\n" + inputXml + "\n\nOutput XML:\n"+ persistedOutputXML);
      }
 else       if (!myDiff.identical()) {
        LOGGER.error("\nNot identical: ROUND-TRIP, PERSISTED:\n====================\nInput XML:\n" + inputXml + "\n\nOutput XML:\n"+ persistedOutputXML);
      }
      assertTrue("Round trip XML (persisted) should be similar." + myDiff,myDiff.similar());
      assertTrue("Round trip XML (persisted) should be identical." + myDiff,myDiff.identical());
    }
 catch (    IOException e) {
      LOGGER.error("IOException thrown during XML round trip test:",e);
      fail("IOException thrown during XML round trip test (full stack trace logged):" + e.getMessage());
    }
catch (    SAXException e) {
      LOGGER.error("SAXExeption thrown when attempting comparison of XML files.",e);
      fail("SAXExeption thrown when attempting comparison of XML files (full stack trace logged): " + e.getMessage());
    }
 finally {
      dao.deleteAll();
    }
  }
}

private < T , D extends GenericDAO > void roundTrip ( XmlsForTesting testXMLs , D dao , ObjectRetriever < T , D > retriever , Marshaller marshaller , Unmarshaller unmarshaller ) { for ( String testXml : testXMLs . getXmls ( ) ) { String inputXml = testXml . trim ( ) ; try { T persistableObject = ( T ) unmarshal ( unmarshaller , inputXml ) ; String unpersistedOutputXml = marshal ( marshaller , persistableObject ) ; Diff myDiff = new Diff ( inputXml , unpersistedOutputXml ) ; if ( ! myDiff . similar ( ) ) { LOGGER . error ( "\nNot similar: ROUND-TRIP, UNPERSISTED:\n====================\nInput XML:\n" + inputXml + "\n\nOutput XML:\n" + unpersistedOutputXml ) ; LOGGER . error ( "\ntoString(): \n\n" + persistableObject . toString ( ) ) ; } else if ( ! myDiff . identical ( ) ) { LOGGER . error ( "\nNot identical: ROUND-TRIP, UNPERSISTED:\n====================\nInput XML:\n" + inputXml + "\n\nOutput XML:\n" + unpersistedOutputXml ) ; LOGGER . error ( "\ntoString(): \n\n" + persistableObject . toString ( ) ) ; } assertTrue ( "Round trip XML (not persistence) should be similar." + myDiff , myDiff . similar ( ) ) ; assertTrue ( "Round trip XML (not persistence) should be identical." + myDiff , myDiff . identical ( ) ) ; dao . insert ( persistableObject ) ; Long id = retriever . getPrimaryKey ( persistableObject ) ; assertNotNull ( "The stored persistableObject should not have a null primary key·" , id ) ; LOGGER . debug ( "Primary key of persisted object (mid round-trip following insert): " + id ) ; T retrievedPersistable = retriever . getObjectByPrimaryKey ( dao , id ) ; assertNotNull ( "The retrieved object should not be null" , retrievedPersistable ) ; assertEquals ( "The retrieved object should be equal to the original persistableObject." , persistableObject , retrievedPersistable ) ; String persistedOutputXML = marshal ( marshaller , retrievedPersistable ) ; myDiff = new Diff ( inputXml , persistedOutputXML ) ; if ( ! myDiff . similar ( ) ) { LOGGER . error ( "\nNot similar: ROUND-TRIP, PERSISTED:\n====================\nInput XML:\n" + inputXml + "\n\nOutput XML:\n" + persistedOutputXML ) ; } else if ( ! myDiff . identical ( ) ) { LOGGER . error ( "\nNot identical: ROUND-TRIP, PERSISTED:\n====================\nInput XML:\n" + inputXml + "\n\nOutput XML:\n" + persistedOutputXML ) ; } assertTrue ( "Round trip XML (persisted) should be similar." + myDiff , myDiff . similar ( ) ) ; assertTrue ( "Round trip XML (persisted) should be identical." + myDiff , myDiff . identical ( ) ) ; } catch ( IOException e ) { LOGGER . error ( "IOException thrown during XML round trip test:" , e ) ; fail ( "IOException thrown during XML round trip test (full stack trace logged):" + e . getMessage ( ) ) ; } catch ( SAXException e ) { LOGGER . error ( "SAXExeption thrown when attempting comparison of XML files." , e ) ; fail ( "SAXExeption thrown when attempting comparison of XML files (full stack trace logged): " + e . getMessage ( ) ) ; } finally { dao . deleteAll ( ) ; } } }
generic method to perform a full round - trip test on any xml type .
