Search in sources :

Example 1 with TimeKeeper

use of org.opennms.netmgt.collection.api.TimeKeeper in project opennms by OpenNMS.

the class RelativeTimeTest method testYesterdayBeginningDST.

@Test
public void testYesterdayBeginningDST() {
    RelativeTime yesterday = RelativeTime.YESTERDAY;
    yesterday.setTimeKeeper(new TimeKeeper() {

        @Override
        public Date getCurrentDate() {
            Calendar cal = new GregorianCalendar(m_timeZone, Locale.ENGLISH);
            cal.set(m_startYear, m_startMonth, m_startDay, 10, 0, 0);
            return cal.getTime();
        }

        @Override
        public long getCurrentTime() {
            return getCurrentDate().getTime();
        }

        @Override
        public TimeZone getTimeZone() {
            return m_timeZone;
        }
    });
    Date start = yesterday.getStart();
    Date end = yesterday.getEnd();
    System.err.println("start = " + start);
    System.err.println("end = " + end);
    Calendar c = new GregorianCalendar(m_timeZone, Locale.ENGLISH);
    c.setTime(start);
    assertEquals(m_offset, c.get(Calendar.ZONE_OFFSET));
    assertEquals(m_startYear, c.get(Calendar.YEAR));
    assertEquals(0, c.get(Calendar.HOUR_OF_DAY));
    assertEquals(Calendar.SUNDAY, c.get(Calendar.DAY_OF_WEEK));
    assertEquals(m_startDay - 1, c.get(Calendar.DAY_OF_MONTH));
    c.setTime(end);
    assertEquals(m_offset, c.get(Calendar.ZONE_OFFSET));
    assertEquals(m_startYear, c.get(Calendar.YEAR));
    assertEquals(0, c.get(Calendar.HOUR_OF_DAY));
    assertEquals(Calendar.MONDAY, c.get(Calendar.DAY_OF_WEEK));
    assertEquals(m_startDay, c.get(Calendar.DAY_OF_MONTH));
    assertEquals("end date - start date", 82800000, end.getTime() - start.getTime());
}
Also used : TimeZone(java.util.TimeZone) TimeKeeper(org.opennms.netmgt.collection.api.TimeKeeper) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 2 with TimeKeeper

use of org.opennms.netmgt.collection.api.TimeKeeper in project opennms by OpenNMS.

the class RelativeTimeTest method testYesterdayEndingDST.

@Test
public void testYesterdayEndingDST() {
    RelativeTime yesterday = RelativeTime.YESTERDAY;
    yesterday.setTimeKeeper(new TimeKeeper() {

        @Override
        public Date getCurrentDate() {
            Calendar cal = new GregorianCalendar(m_timeZone, Locale.ENGLISH);
            cal.set(m_endYear, m_endMonth, m_endDay, 10, 0, 0);
            return cal.getTime();
        }

        @Override
        public long getCurrentTime() {
            return getCurrentDate().getTime();
        }

        @Override
        public TimeZone getTimeZone() {
            return m_timeZone;
        }
    });
    Date start = yesterday.getStart();
    Date end = yesterday.getEnd();
    Calendar c = new GregorianCalendar(m_timeZone, Locale.ENGLISH);
    c.setTime(start);
    assertEquals(m_offset, c.get(Calendar.ZONE_OFFSET));
    assertEquals(m_endYear, c.get(Calendar.YEAR));
    assertEquals(0, c.get(Calendar.HOUR_OF_DAY));
    assertEquals(Calendar.SUNDAY, c.get(Calendar.DAY_OF_WEEK));
    assertEquals(m_endDay - 1, c.get(Calendar.DAY_OF_MONTH));
    c.setTime(end);
    assertEquals(m_offset, c.get(Calendar.ZONE_OFFSET));
    assertEquals(m_endYear, c.get(Calendar.YEAR));
    assertEquals(0, c.get(Calendar.HOUR_OF_DAY));
    assertEquals(Calendar.MONDAY, c.get(Calendar.DAY_OF_WEEK));
    assertEquals(m_endDay, c.get(Calendar.DAY_OF_MONTH));
    assertEquals("end date - start date", 90000000, end.getTime() - start.getTime());
}
Also used : TimeZone(java.util.TimeZone) TimeKeeper(org.opennms.netmgt.collection.api.TimeKeeper) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 3 with TimeKeeper

use of org.opennms.netmgt.collection.api.TimeKeeper in project opennms by OpenNMS.

the class CollectableService method wrapResourcesWithTimekeeper.

public static CollectionSetVisitor wrapResourcesWithTimekeeper(CollectionSetVisitor visitor, TimeKeeper timeKeeper) {
    // Wrap the given visitor and intercept the calls to visit the resources
    final CollectionSetVisitor wrappedVisitor = new CollectionSetVisitorWrapper(visitor) {

        private CollectionResource wrappedResource;

        private CollectionAttribute wrappedAttribute;

        private AttributeGroup wrappedGroup;

        @Override
        public void visitResource(CollectionResource resource) {
            // Wrap the given resource and return the custom timekeeper
            wrappedResource = new CollectionResourceWrapper(resource) {

                @Override
                public TimeKeeper getTimeKeeper() {
                    return timeKeeper;
                }
            };
            visitor.visitResource(wrappedResource);
        }

        @Override
        public void completeResource(CollectionResource resource) {
            visitor.completeResource(wrappedResource);
        }

        @Override
        public void visitAttribute(CollectionAttribute attribute) {
            // Wrap the given attribute and return the custom resource
            wrappedAttribute = new CollectionAttributeWrapper(attribute) {

                @Override
                public CollectionResource getResource() {
                    return wrappedResource;
                }
            };
            visitor.visitAttribute(wrappedAttribute);
        }

        @Override
        public void completeAttribute(CollectionAttribute attribute) {
            visitor.completeAttribute(wrappedAttribute);
        }

        @Override
        public void visitGroup(AttributeGroup group) {
            // Wrap the given attribute group and return the custom resource
            wrappedGroup = new AttributeGroupWrapper(group) {

                @Override
                public CollectionResource getResource() {
                    return wrappedResource;
                }
            };
            visitor.visitGroup(wrappedGroup);
        }

        @Override
        public void completeGroup(AttributeGroup group) {
            visitor.completeGroup(wrappedGroup);
        }
    };
    return wrappedVisitor;
}
Also used : CollectionAttribute(org.opennms.netmgt.collection.api.CollectionAttribute) CollectionResource(org.opennms.netmgt.collection.api.CollectionResource) AttributeGroupWrapper(org.opennms.netmgt.collection.support.AttributeGroupWrapper) ConstantTimeKeeper(org.opennms.netmgt.collection.support.ConstantTimeKeeper) TimeKeeper(org.opennms.netmgt.collection.api.TimeKeeper) CollectionSetVisitor(org.opennms.netmgt.collection.api.CollectionSetVisitor) AttributeGroup(org.opennms.netmgt.collection.api.AttributeGroup) CollectionAttributeWrapper(org.opennms.netmgt.collection.support.CollectionAttributeWrapper) CollectionSetVisitorWrapper(org.opennms.netmgt.collection.support.CollectionSetVisitorWrapper) CollectionResourceWrapper(org.opennms.netmgt.collection.support.CollectionResourceWrapper)

Aggregations

TimeKeeper (org.opennms.netmgt.collection.api.TimeKeeper)3 Calendar (java.util.Calendar)2 Date (java.util.Date)2 GregorianCalendar (java.util.GregorianCalendar)2 TimeZone (java.util.TimeZone)2 Test (org.junit.Test)2 AttributeGroup (org.opennms.netmgt.collection.api.AttributeGroup)1 CollectionAttribute (org.opennms.netmgt.collection.api.CollectionAttribute)1 CollectionResource (org.opennms.netmgt.collection.api.CollectionResource)1 CollectionSetVisitor (org.opennms.netmgt.collection.api.CollectionSetVisitor)1 AttributeGroupWrapper (org.opennms.netmgt.collection.support.AttributeGroupWrapper)1 CollectionAttributeWrapper (org.opennms.netmgt.collection.support.CollectionAttributeWrapper)1 CollectionResourceWrapper (org.opennms.netmgt.collection.support.CollectionResourceWrapper)1 CollectionSetVisitorWrapper (org.opennms.netmgt.collection.support.CollectionSetVisitorWrapper)1 ConstantTimeKeeper (org.opennms.netmgt.collection.support.ConstantTimeKeeper)1