package org.intermine.util;

/*
 * Copyright (C) 2002-2010 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.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Collection;
import java.util.Iterator;

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);

        BufferedReader originalReader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream("testmodel_data.xml")));
        StringBuffer originalBuffer = new StringBuffer();
        int c = originalReader.read();
        while (c != -1) {
            originalBuffer.append((char) c);
            c = originalReader.read();
        }
        //System.out.println("Original: " + originalBuffer.toString());
        //System.out.println("Generated: " + sw.toString());
        Diff diff = new Diff(originalBuffer.toString(), sw.toString());
        DetailedDiff detail = new DetailedDiff(diff);
        detail.overrideElementQualifier(new ElementNameAndAttributeQualifier());
        assertTrue(detail.getAllDifferences().toString() + ": Original: " + originalBuffer.toString() + ", Generated: " + sw.toString(), detail.similar());
        original = getClass().getClassLoader().getResourceAsStream("testmodel_data.xml");
    }


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

@@ -355,114 +355,8 @@
 er;%0A
-import java.io.BufferedWriter;%0Aimport java.io.File;%0Aimport java.io.FileReader;%0Aimport java.io.FileWriter;%0A
 impo
@@ -417,37 +417,8 @@
 er;%0A
-import java.io.StringReader;%0A
 impo
@@ -474,35 +474,8 @@
 ion;
-%0Aimport java.util.Iterator;
 %0A%0Aim
@@ -1389,16 +1389,59 @@
 , sw);%0A%0A
+        // read the original into a string%0A
         
@@ -1582,16 +1582,24 @@
 ta.xml%22)
+, %22UTF8%22
 ));%0A    
@@ -1608,20 +1608,20 @@
   String
-Buff
+Writ
 er origi
@@ -1623,20 +1623,20 @@
 original
-Buff
+Writ
 er = new
@@ -1642,20 +1642,20 @@
 w String
-Buff
+Writ
 er();%0A  
@@ -1741,29 +1741,21 @@
 inal
-Buffer.append((char) 
+Writer.write(
 c);%0A
@@ -1847,36 +1847,36 @@
 al: %22 + original
-Buff
+Writ
 er.toString());%0A
@@ -1965,36 +1965,36 @@
 ew Diff(original
-Buff
+Writ
 er.toString(), s
@@ -2227,20 +2227,20 @@
 original
-Buff
+Writ
 er.toStr
@@ -2303,98 +2303,8 @@
 ));%0A
-        original = getClass().getClassLoader().getResourceAsStream(%22testmodel_data.xml%22);%0A
     

Fix test to read UTF8 correctly.