<commit_before>package org.flymine.dataloader;

import junit.framework.TestCase;

import java.util.*;
import java.io.*;
import java.lang.reflect.*;
import java.net.URL;

import org.exolab.castor.mapping.*;
import org.exolab.castor.xml.*;

import org.xml.sax.InputSource;

import org.flymine.objectstore.ObjectStore;
import org.flymine.objectstore.ObjectStoreWriter;
import org.flymine.objectstore.ObjectStoreFactory;
import org.flymine.objectstore.ojb.ObjectStoreWriterOjbImpl;
import org.flymine.objectstore.ojb.ObjectStoreOjbImpl;
import org.flymine.util.TypeUtil;
import org.flymine.util.ListBean;
import org.flymine.model.testmodel.*;
import org.flymine.metadata.Model;

public class XmlDataLoaderTest extends TestCase
{
    protected ObjectStore os;
    protected ObjectStoreWriter writer;
    protected IntegrationWriter iw;
    protected int fakeId = 0;
    protected File file;
    protected Mapping map;
    protected ArrayList toDelete;

    public XmlDataLoaderTest(String arg) {
        super(arg);
    }

    public void setUp() throws Exception {
        os = ObjectStoreFactory.getObjectStore("os.unittest");
        writer = new ObjectStoreWriterOjbImpl((ObjectStoreOjbImpl) os);
        iw = new IntegrationWriterSingleSourceImpl("test", os, writer);

        URL mapFile = getClass().getClassLoader().getResource("castor_xml_testmodel.xml");
        map = new Mapping();
        map.loadMapping(mapFile);
        toDelete = new ArrayList();
    }

    public void tearDown() throws Exception {
        Iterator deleteIter = toDelete.iterator();
        while (deleteIter.hasNext()) {
            Object o = deleteIter.next();
            writer.delete(o);
        }
        file.delete();
    }


    // marshal an object, set up as an InputSource and store to DB
    public void testSimpleObject() throws Exception {
        Company c1 = new Company();
        c1.setName("c1");
        c1.setVatNumber(101);
        Address a1 = new Address();
        a1.setAddress("a1");
        c1.setAddress(a1);

        List list = new ArrayList();
        list.add(c1);
        file = new File("temp.xml");
        marshalList(list, file);

        Reader reader = new FileReader(file);
        InputSource source = new InputSource(reader);

        XmlDataLoader dl = new XmlDataLoader(Model.getInstanceByName("testmodel"), iw);
        dl.processXml(source);

        // check address was stored
        Address a2 = (Address) os.getObjectByExample(a1);
        assertNotNull("Expected address to be retieved from DB", a2);
        assertTrue("address id should be set", (a2.getId().intValue() != 0));

        // check company was stored
        Company c2 = (Company) os.getObjectByExample(c1);
        assertNotNull("Expected company to be retieved from DB", c2);
        assertTrue("company id should be set", (c2.getId().intValue() != 0));

        toDelete.add(a2);
        toDelete.add(c2);
    }

    private void marshalList(List list, File file) throws Exception {
        Writer writer = new FileWriter(file);
        Marshaller marshaller = new Marshaller(writer);
        marshaller.setMapping(map);<commit_msg>Added test for loading XML data from a file<commit_after>package org.flymine.dataloader;

import junit.framework.TestCase;

import java.util.*;
import java.io.*;
import java.lang.reflect.*;
import java.net.URL;

import org.exolab.castor.mapping.*;
import org.exolab.castor.xml.*;

import org.xml.sax.InputSource;

import org.flymine.objectstore.ObjectStore;
import org.flymine.objectstore.ObjectStoreWriter;
import org.flymine.objectstore.ObjectStoreFactory;
import org.flymine.objectstore.ojb.ObjectStoreWriterOjbImpl;
import org.flymine.objectstore.ojb.ObjectStoreOjbImpl;
import org.flymine.util.TypeUtil;
import org.flymine.util.ListBean;
import org.flymine.model.testmodel.*;
import org.flymine.metadata.Model;

public class XmlDataLoaderTest extends TestCase
{
    protected ObjectStore os;
    protected ObjectStoreWriter writer;
    protected IntegrationWriter iw;
    protected int fakeId = 0;
    protected File file;
    protected Mapping map;
    protected ArrayList toDelete;

    public XmlDataLoaderTest(String arg) {
        super(arg);
    }

    public void setUp() throws Exception {
        os = ObjectStoreFactory.getObjectStore("os.unittest");
        writer = new ObjectStoreWriterOjbImpl((ObjectStoreOjbImpl) os);
        iw = new IntegrationWriterSingleSourceImpl("test", os, writer);

        URL mapFile = getClass().getClassLoader().getResource("castor_xml_testmodel.xml");
        map = new Mapping();
        map.loadMapping(mapFile);
        toDelete = new ArrayList();
    }

    public void tearDown() throws Exception {
        Iterator deleteIter = toDelete.iterator();
        while (deleteIter.hasNext()) {
            Object o = deleteIter.next();
            writer.delete(o);
        }
        if (file != null) {
            file.delete();
        }
    }


    // marshal an object, set up as an InputSource and store to DB
    public void testSimpleObject() throws Exception {
        Company c1 = new Company();
        c1.setName("c1");
        c1.setVatNumber(101);
        Address a1 = new Address();
        a1.setAddress("a1");
        c1.setAddress(a1);

        List list = new ArrayList();
        list.add(c1);
        file = new File("temp.xml");
        marshalList(list, file);

        Reader reader = new FileReader(file);
        InputSource source = new InputSource(reader);

        XmlDataLoader dl = new XmlDataLoader(Model.getInstanceByName("testmodel"), iw);
        dl.processXml(source);

        // check address was stored
        Address a2 = (Address) os.getObjectByExample(a1);
        assertNotNull("Expected address to be retieved from DB", a2);
        assertTrue("address id should be set", (a2.getId().intValue() != 0));

        // check company was stored
        Company c2 = (Company) os.getObjectByExample(c1);
        assertNotNull("Expected company to be retieved from DB", c2);
        assertTrue("company id should be set", (c2.getId().intValue() != 0));

        toDelete.add(a2);
        toDelete.add(c2);
    }

    // marshal an object, set up as an InputSource and store to DB
    public void testSubclassedObject() throws Exception {
        Manager m1 = new Manager();
        m1.setName("m1");
        m1.setTitle("Pointy Haired Boss");
        Address a1 = new Address();
        a1.setAddress("a1");
        m1.setAddress(a1);

        List list = new ArrayList();
        list.add(m1);
        file = new File("temp.xml");
        marshalList(list, file);

        Reader reader = new FileReader(file);
        InputSource source = new InputSource(reader);

        XmlDataLoader dl = new XmlDataLoader(Model.getInstanceByName("testmodel"), iw);
        dl.processXml(source);

        // check address was stored
        Address a2 = (Address) os.getObjectByExample(a1);
        assertNotNull("Expected address to be retieved from DB", a2);
        assertTrue("address id should be set", (a2.getId().intValue() != 0));

        // check company was stored
        Manager m2 = (Manager) os.getObjectByExample(m1);
        assertNotNull("Expected company to be retieved from DB", m2);
        assertTrue("manager id should be set", (m2.getId().intValue() != 0));

        toDelete.add(a2);
        toDelete.add(m2);
    }

    public void testStoreFromFile() throws Exception {
        XmlDataLoader dl = new XmlDataLoader(Model.getInstanceByName("testmodel"), iw);
        InputStream testData = getClass().getClassLoader().getResourceAsStream("test/testmodel.xml");
        dl.processXml(new InputSource(testData));

        // Just test that a specific Company is there
        Address a1 = new Address();
        a1.setAddress("Company Street, BVille");
        Company c1 = new Company();
        c1.setName("CompanyB");
        c1.setAddress(a1);
        Company c2 = (Company) os.getObjectByExample(c1);

        // Could only know the vatNumber if it got it from the database
        assertNotNull(c2);
        assertEquals(5678, c2.getVatNumber());

        // Read in the file again in order to delete the objects in it
        InputStream testData2 = getClass().getClassLoader().getResourceAsStream("test/testmodel.xml");
        Unmarshaller unmarshaller = new Unmarshaller(map);
        unmarshaller.setMapping(map);
        List objects = (List) unmarshaller.unmarshal(new InputSource(testData2));

        Iterator iter = objects.iterator();
        while (iter.hasNext()) {
            Object obj = iter.next();
            Object obj2 = os.getObjectByExample(obj);
            toDelete.add(obj2);
        }

    }



    private void marshalList(List list, File file) throws Exception {
        Writer writer = new FileWriter(file);
        Marshaller marshaller = new Marshaller(writer);
        marshaller.setMapping(map);
{'fraction_non_alphanumeric': 0.10942077980617534, 'fraction_numerical': 0.010367365336939374, 'mean_word_length': 2.8721640488656197, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 13}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1463358', 'n_tokens_mistral': 2570, 'n_tokens_neox': 2504, 'n_words': 658}