Package de.timefinder.core.io.xml

Source Code of de.timefinder.core.io.xml.XmlImportTest

/*
*  Copyright 2009 Peter Karich, peat_hal 'at' users 'dot' sourceforge 'dot' net.
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*  under the License.
*/
package de.timefinder.core.io.xml;

import de.timefinder.algo.constraint.DifferentDayConstraint;
import de.timefinder.algo.constraint.EventOrderConstraint;
import de.timefinder.algo.constraint.MinGapsConstraint;
import de.timefinder.algo.constraint.PersonITCRasterConstraint;
import de.timefinder.algo.constraint.RasterConstraint;
import de.timefinder.data.DataPool;
import de.timefinder.data.Event;
import de.timefinder.data.Feature;
import de.timefinder.data.Location;
import de.timefinder.data.Person;
import de.timefinder.data.Role;
import de.timefinder.data.access.Dao;
import de.timefinder.data.access.EventDao;
import de.timefinder.data.access.FeatureDao;
import de.timefinder.data.access.LocationDao;
import de.timefinder.data.access.PersonDao;
import java.io.StringReader;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

/**
* @author Peter Karich, peat_hal 'at' users 'dot' sourceforge 'dot' net
*/
public class XmlImportTest extends AbstractIOTester {

    public XmlImportTest() {
    }

    @Before
    @Override
    public void setUp() {
        super.setUp();
    }

    @Test
    public void testGetResults() throws Exception {
        setupExport();
        new XmlExport(dataPool, settings, writer).doWork();

        for (Dao dao : dataPool.getDaos()) {
            dao.getMap().clear();
        }
//        System.out.println(writer.toString());
        int days = settings.getNumberOfDays();
        int slots = settings.getTimeslotsPerDay();
        long millis = settings.getMillisPerTimeslot();

        // disturb values to check if successfully read
        settings.setNumberOfDays(days + 1);
        settings.setTimeslotsPerDay(slots + 1);
        settings.setMillisPerTimeslot(millis + 1);

        XmlImport importer = new XmlImport(dataPool, settings, new StringReader(writer.toString()));
        importer.doWork();

        assertEquals(4, dataPool.getDaos().size());
        assertEquals(days, settings.getNumberOfDays());
        assertEquals(slots, settings.getTimeslotsPerDay());
        assertEquals(millis, settings.getMillisPerTimeslot());

        Dao<Event> tmpEDao = dataPool.getDao(Event.class);
        assertEquals(4, dataPool.getDao(Person.class).getAll().size());
        assertEquals(4, dataPool.getDao(Feature.class).getAll().size());
        assertEquals(4, tmpEDao.getAll().size());

        Dao<Location> tmpLDao = dataPool.getDao(Location.class);
        assertEquals(4, tmpLDao.getAll().size());

        Event tmpEv1 = tmpEDao.findFirstByName("event1");
        Event tmpEv2 = tmpEDao.findFirstByName("event2");
        Event tmpEv3 = tmpEDao.findFirstByName("event3");
        Event tmpEv4 = tmpEDao.findFirstByName("event4");
        Person tmpPerson1 = dataPool.getDao(Person.class).findFirstByName("person1");
        Person tmpPerson4 = dataPool.getDao(Person.class).findFirstByName("person4");

        long max = -1;
        for (Event event : eDao.getAll()) {
            if (event.getId() > max)
                max = event.getId();
        }
        Event ev = eDao.create();
        eDao.attach(ev);
        assertTrue(max < ev.getId());
        eDao.detach(ev);

        assertEquals("description", tmpPerson1.getDescription());
        assertEquals(2, tmpPerson1.getEvents().size());
        assertTrue(tmpPerson1.getEvents().contains(tmpEv1));
        assertTrue(tmpPerson1.getEvents().contains(tmpEv2));

        assertEquals(2, tmpEv1.getPersons().size());
        assertTrue(tmpEv1.getPersons().contains(tmpPerson1));
        assertTrue(tmpEv1.getPersons().contains(tmpPerson4));

        assertTrue(tmpPerson4.getEvents().contains(tmpEv1));
        assertEquals(Role.TEACHER, tmpPerson4.getRole(tmpEv1));

        RasterConstraint rasterC = tmpEv1.getConstraint(RasterConstraint.class);
        assertFalse(rasterC.getRaster().getForbidden().isAssigned(0));
        assertTrue(rasterC.getRaster().getForbidden().isAssigned(1));
        assertTrue(rasterC.getRaster().getForbidden().isAssigned(7));
        assertFalse(rasterC.getRaster().getForbidden().isAssigned(8));

        PersonITCRasterConstraint personC1 = tmpPerson1.getConstraint(PersonITCRasterConstraint.class);
        assertEquals(tmpPerson1, personC1.getPerson());
        assertEquals(3.4f, personC1.getWeight());

        assertNotNull(tmpEv2.getConstraint(PersonITCRasterConstraint.class));
        EventOrderConstraint orderC1 = tmpEv2.getConstraint(EventOrderConstraint.class);
        assertEquals(1, orderC1.getBefores().size());
        assertEquals(1, orderC1.getFollows().size());

        assertEquals(tmpEv1, orderC1.getBefores().iterator().next());
        assertEquals(tmpEv4, orderC1.getFollows().iterator().next());

        Location tmpLoc1 = tmpLDao.findFirstByName("location1");
        assertTrue(tmpLoc1.getEvents().contains(tmpEv1));
        assertEquals(tmpLoc1, tmpEv1.getLocation());

        DifferentDayConstraint ddc = tmpEv1.getConstraint(DifferentDayConstraint.class);
        assertTrue(ddc.getEvents().contains(tmpEv2));

        MinGapsConstraint mgc = tmpEv1.getConstraint(MinGapsConstraint.class);
        assertTrue(mgc.getEvents().contains(tmpEv3));
        assertTrue(mgc.isCountEarly());

        oldAssertations(dataPool);
    }

    // taken from xstream import
    private void oldAssertations(DataPool importDataPool) {
        PersonDao newPDao = (PersonDao) importDataPool.getDao(Person.class);
        EventDao newEDao = (EventDao) importDataPool.getDao(Event.class);
        FeatureDao newFDao = (FeatureDao) importDataPool.getDao(Feature.class);
        LocationDao newLDao = (LocationDao) importDataPool.getDao(Location.class);

        assertEquals(4, newPDao.getAll().size());
        assertEquals(4, newFDao.getAll().size());
        assertEquals(4, newEDao.getAll().size());
        assertEquals(4, newLDao.getAll().size());

        assertNotNull(newPDao);
        assertNotNull(newFDao);
        assertNotNull(newEDao);
        assertNotNull(newLDao);

        Person newPerson1 = newPDao.findFirstByName("person1");
        Person newPerson2 = newPDao.findFirstByName("person2");
        Person newPerson3 = newPDao.findFirstByName("person3");
        Person newPerson4 = newPDao.findFirstByName("person4");

        Event newEv1 = newEDao.findFirstByName("event1");
        Event newEv2 = newEDao.findFirstByName("event2");
        Event newEv3 = newEDao.findFirstByName("event3");
        Event newEv4 = newEDao.findFirstByName("event4");

        Location newLoc1 = newLDao.findFirstByName("location1");
        Location newLoc2 = newLDao.findFirstByName("location2");
        Location newLoc3 = newLDao.findFirstByName("location3");
        Location newLoc4 = newLDao.findFirstByName("location4");

        Feature newFeature1 = newFDao.findFirstByName("feature1");
        Feature newFeature2 = newFDao.findFirstByName("feature2");

        assertTrue(newPerson1.getEventsMap().containsKey(newEv1));
        assertTrue(newPerson1.getEventsMap().containsKey(newEv2));
        assertTrue(newPerson2.getEventsMap().containsKey(newEv3));
        assertTrue(newPerson2.getEventsMap().containsKey(newEv4));
        assertTrue(newPerson3.getEventsMap().containsKey(newEv3));
        assertTrue(newPerson4.getEventsMap().containsKey(newEv1));

        // the following could fail if we change the hashcode while
        // deserialization with xstream
        assertTrue(newEv1.getPersonsMap().containsKey(newPerson1));
        assertTrue(newEv1.getPersonsMap().containsKey(newPerson4));
        assertTrue(newEv2.getPersonsMap().containsKey(newPerson1));
        assertTrue(newEv3.getPersonsMap().containsKey(newPerson2));
        assertTrue(newEv3.getPersonsMap().containsKey(newPerson3));
        assertTrue(newEv4.getPersonsMap().containsKey(newPerson2));

        assertTrue(newLoc1.getEvents().contains(newEv1));
        assertTrue(newLoc1.getEvents().contains(newEv2));
        assertTrue(newLoc3.getEvents().contains(newEv4));
        assertTrue(newLoc4.getEvents().contains(newEv3));

        assertTrue(newLoc1.getFeatures().contains(newFeature1));
        assertTrue(newLoc4.getFeatures().contains(newFeature2));
    }
}
TOP

Related Classes of de.timefinder.core.io.xml.XmlImportTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.