package org.intermine.util;

/*
 * Copyright (C) 2002-2011 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.Collection;

import org.custommonkey.xmlunit.DetailedDiff;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.ElementNameAndAttributeQualifier;
import org.custommonkey.xmlunit.XMLTestCase;
import org.custommonkey.xmlunit.XMLUnit;
import org.intermine.metadata.Model;
import org.intermine.model.InterMineObject;

public class XmlBindingTest extends XMLTestCase {
    protected XmlBinding binding;

    public void setUp() throws Exception {
        binding = new XmlBinding(Model.getInstanceByName("testmodel"));
    }

    public void testRoundTrip() throws Exception {
        StringWriter sw = new StringWriter();
        InputStream original = getClass().getClassLoader().getResourceAsStream("testmodel_data.xml");
        XMLUnit.setIgnoreWhitespace(true);
        Collection unmarshalled = (Collection) binding.unmarshal(original);
        setIds(unmarshalled);
        binding.marshal(unmarshalled, sw);

        // read the original into a string
        BufferedReader originalReader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream("testmodel_data.xml"), "UTF8"));
        StringWriter originalWriter = new StringWriter();
        int c = originalReader.read();
        while (c != -1) {
            originalWriter.write(c);
            c = originalReader.read();
        }
        //System.out.println("Original: " + originalWriter.toString());
        //System.out.println("Generated: " + sw.toString());
        Diff diff = new Diff(originalWriter.toString(), sw.toString());
        DetailedDiff detail = new DetailedDiff(diff);
        detail.overrideElementQualifier(new ElementNameAndAttributeQualifier());
        assertTrue(detail.getAllDifferences().toString() + ": Original: " + originalWriter.toString() + ", Generated: " + sw.toString(), detail.similar());
    }


    protected void setIds(Collection c) throws Exception {
        int i=1;
        for (Object o : c) {
            if (o instanceof InterMineObject) {
                ((InterMineObject) o).setId(new Integer(i++));
            }
        }
    }
}

@@ -472,16 +472,54 @@
 ction;%0A%0A
+import org.apache.commons.io.IOUtils;%0A
 import o
@@ -1439,546 +1439,115 @@
     
-// read the original into a string%0A        BufferedReader originalReader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(%22testmodel_data.xml%22), %22UTF8%22));%0A        StringWriter originalWriter = new StringWriter();%0A        int c = originalReader.read();%0A        while (c != -1) %7B%0A            originalWriter.write(c);%0A            c = originalReader.read();%0A        %7D%0A        //System.out.println(%22Original: %22 + originalWriter.toString());%0A        //System.out.println(%22Generated: %22 + sw.toString());
+String expected = IOUtils.toString(getClass().getClassLoader().getResourceAsStream(%22testmodel_data.xml%22));%0A
 %0A   
@@ -1576,33 +1576,16 @@
 iff(
-originalWriter.toString()
+expected
 , sw
@@ -1813,33 +1813,16 @@
 %22 + 
-originalWriter.toString()
+expected
  + %22

Use IOUtils to reduce overall verbosity