Package ch.pollet.jzic.timezone

Source Code of ch.pollet.jzic.timezone.DatabaseTest

package ch.pollet.jzic.timezone;

import ch.pollet.jzic.timezone.Rule;
import ch.pollet.jzic.timezone.Period;
import ch.pollet.jzic.timezone.Link;
import ch.pollet.jzic.timezone.Database;
import ch.pollet.jzic.timezone.Zone;
import ch.pollet.jzic.datetime.DateTime;
import java.util.TreeSet;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

/**
*
* @author Christophe Pollet
*/
public class DatabaseTest {
    Database db;
   
    @Before
    public void setUp() {
        db = new Database();
    }

    @Test
    public void testDump() {
    }

    @Test
    public void testAddRule() {
        Rule r;

        r = new Rule();
        r.setName("rule_1");
        r.setOffset(1);
        db.addRule(r);
       
        assertTrue(db.getRules().containsKey("rule_1"));
        assertEquals(1, db.getRules().size());
        assertEquals(1, db.getRules().get("rule_1").size());
        assertTrue(db.getRules().get("rule_1").contains(r));

        r = new Rule();
        r.setName("rule_1");
        r.setOffset(2);
        db.addRule(r);
       
        assertTrue(db.getRules().containsKey("rule_1"));
        assertEquals(1, db.getRules().size());
        assertEquals(2, db.getRules().get("rule_1").size());
        assertTrue(db.getRules().get("rule_1").contains(r));

        r = new Rule();
        r.setName("rule_2");
        r.setOffset(3);
        db.addRule(r);

        assertTrue(db.getRules().containsKey("rule_2"));
        assertEquals(2, db.getRules().size());
        assertEquals(1, db.getRules().get("rule_2").size());
        assertTrue(db.getRules().get("rule_2").contains(r));
    }

    @Test
    public void testAddZone() {
        Zone z1, z2;
        DateTime dt;
       
        z1 = new Zone();
        z1.setOffset(1);
        dt = new DateTime();
        dt.setYear(2009);
        dt.setMonth(10);
        dt.setDay(3);
        dt.setHour(16);
        dt.setMinute(44);
        dt.setSecond(33);
        dt.setType(DateTime.Type.UTC);
        z1.setUntil(dt);
       
        db.addZone("zone_1", z1);
        assertTrue(db.getZones().containsKey("zone_1"));
        assertEquals(1, db.getZones().size());
        assertEquals(1, db.getZones().get("zone_1").size());
        assertTrue(db.getZones().get("zone_1").contains(z1));
        assertEquals(z1, db.getZones().get("zone_1").first());

        z2 = new Zone();
        z2.setOffset(1);
        dt = new DateTime();
        dt.setYear(2008);
        dt.setMonth(10);
        dt.setDay(3);
        dt.setHour(16);
        dt.setMinute(44);
        dt.setSecond(33);
        dt.setType(DateTime.Type.UTC);
        z2.setUntil(dt);

        db.addZone("zone_1", z2);
        assertTrue(db.getZones().containsKey("zone_1"));
        assertEquals(db.getZones().size(), 1);
        assertEquals(db.getZones().get("zone_1").size(), 2);
        assertTrue(db.getZones().get("zone_1").contains(z1));
        assertEquals(db.getZones().get("zone_1").first(), z2);
        assertEquals(db.getZones().get("zone_1").last(), z1);

        z1 = new Zone();
        z1.setOffset(1);
        dt = new DateTime();
        dt.setYear(2008);
        dt.setMonth(10);
        dt.setDay(3);
        dt.setHour(16);
        dt.setMinute(44);
        dt.setSecond(33);
        dt.setType(DateTime.Type.UTC);
        z1.setUntil(dt);

        db.addZone("zone_2", z1);
        assertTrue(db.getZones().containsKey("zone_2"));
        assertEquals(db.getZones().size(), 2);
        assertEquals(db.getZones().get("zone_2").size(), 1);
        assertTrue(db.getZones().get("zone_2").contains(z2));
    }

    @Test
    public void testAddLink() {
        Zone z = new Zone();
        DateTime dt = new DateTime();
        dt.setYear(2009);
        dt.setMonth(5);
        dt.setDay(3);
        dt.setHour(3);
        dt.setMinute(6);
        dt.setSecond(45);
        dt.setType(DateTime.Type.UTC);
        z.setUntil(dt);

        db.addZone("zone", z);
       
        Link l = new Link();
        l.setFrom("zone");
        l.setTo("alias");

        db.addLink(l);

        assertEquals(1, db.getZones().size());

        db.resolveLinks();

        assertEquals(2, db.getZones().size());
        assertTrue(db.getZones().containsKey("zone"));
        assertTrue(db.getZones().containsKey("alias"));
        assertEquals(db.getZones().get("zone"), db.getZones().get("alias"));
    }

    @Test
    public void testAddMetazone() {
        Period p = new Period();
        DateTime from = new DateTime();
        DateTime to = new DateTime();
        TreeSet<Period> metazone = new TreeSet<Period>();

        from.setYear(1);
        from.setMonth(1);
        from.setDay(1);
        from.setHour(1);
        from.setMinute(1);
        from.setSecond(1);
        from.setType(DateTime.Type.UTC);

        p.setFrom(from);

        from.setYear(2);
        from.setMonth(1);
        from.setDay(1);
        from.setHour(1);
        from.setMinute(1);
        from.setSecond(1);
        from.setType(DateTime.Type.UTC);

        p.setTo(to);

        metazone.add(p);
       
        db.addCompiledZone("p1", metazone);

        assertEquals(1, db.getCompiledZones().size());
        assertEquals(metazone, db.getCompiledZones().get("p1"));
    }
}
TOP

Related Classes of ch.pollet.jzic.timezone.DatabaseTest

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.