Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.unitils.jodatime;
  
  
  import  org.joda.time.DateTime;
  import  org.joda.time.DateTimeUtils;
  import  org.joda.time.Duration;
  import  org.joda.time.Period;
 import  org.joda.time.format.DateTimeFormat;
Module to run test with a provided datetime. Possible uses:
  • 1. time can be frozen to a specific datetime using the FixedDateTime annotation on test class or method.
  • 2. time can be an offset of the current datetime using the OffsetDateTime annotation on test class or method.
When an annotation is used at class level it will be used by default for all its test methods. For a specific test method

Author(s):
Christophe De Blende
Jeroen Horemans
Thomas De Rycke
Willemijn Wouters
Since:
1.0.0
 
 public class JodaTimeModule implements Module {
 
     public void init(Properties configuration) {
         //do nothing
     }
 
     public void afterInit() {
         //do nothing
     }
 
     public TestListener getTestListener() {
         return new DateTimeModuleListener();
     }
 
     protected static class DateTimeModuleListener extends TestListener {
 
         private long fixedMillis;
 
         private long offsetMillis;
 
         @Override
         public void afterTestTearDown(Object testObjectMethod testMethod) {
             DateTimeUtils.setCurrentMillisSystem();
         }
 
         @Override
         public void beforeTestClass(Class<?> testClass) {
             // the module is only instantiated once. So when the beforeTestClass lifecycle callback occurs make sure the members are reset.
              = 0L;
              = 0L;
 
             if (testClass.isAnnotationPresent(FixedDateTime.class)) {
                 FixedDateTime fixedDateTime = testClass.getAnnotation(FixedDateTime.class);
                  = calculateFixedMillis(fixedDateTime);
                 DateTimeUtils.setCurrentMillisFixed();
 
 
             } else if (testClass.isAnnotationPresent(OffsetDateTime.class)) {
                 OffsetDateTime offsetDateTime = testClass.getAnnotation(OffsetDateTime.class);
                  = calculateOffsetMillis(offsetDateTime);
                 DateTimeUtils.setCurrentMillisOffset();
 
             } else {
                 DateTimeUtils.setCurrentMillisSystem();
             }
         }
 
         @Override
         public void beforeTestSetUp(Object testObjectMethod testMethod) {
             if (testMethod.isAnnotationPresent(FixedDateTime.class)) {
                 FixedDateTime fixedDateTime = testMethod.getAnnotation(FixedDateTime.class);
                 DateTimeUtils.setCurrentMillisFixed(calculateFixedMillis(fixedDateTime));
 
             } else if (testMethod.isAnnotationPresent(OffsetDateTime.class)) {
                 OffsetDateTime offsetDateTime = testMethod.getAnnotation(OffsetDateTime.class);
                 DateTimeUtils.setCurrentMillisOffset(calculateOffsetMillis(offsetDateTime));
 
             } else if ( != 0L) {
                 // if the method doesn't have an annotation for fixeddatetime, but there is one at class level we use the calculated value
                 // that was defined at class level.
                 DateTimeUtils.setCurrentMillisFixed();
 
             } else if ( != 0L) {
                // if the method doesn't have an annotation for offsetdatetime, but there is one at class level we use the calculated value
                // that was defined at class level.
                DateTimeUtils.setCurrentMillisOffset();
            } else {
                // if there is no annotation no where we use the real current time.
                DateTimeUtils.setCurrentMillisSystem();
            }
        }
        private long calculateFixedMillis(FixedDateTime fixedDateTime) {
            if (fixedDateTime.datetime() == null || fixedDateTime.datetime().length() == 0) {
                // default use the current time as a fixed datetime.
                return new DateTime().getMillis();
            }
            DateTime dateTime = DateTimeFormat.forPattern(fixedDateTime.pattern()).parseDateTime(fixedDateTime.datetime());
            return dateTime.getMillis();
        }
        private long calculateOffsetMillis(OffsetDateTime offsetDateTime) {
            Period period = new Period(offsetDateTime.years(), offsetDateTime.months(), offsetDateTime.weeks(), offsetDateTime.days(),
                offsetDateTime.hours(), offsetDateTime.minutes(), offsetDateTime.seconds(), offsetDateTime.millis());
            DateTime currentDate = new DateTime();
            DateTime offisetDate = currentDate.plus(period);
            Duration duration = new Duration(currentDateoffisetDate);
            return duration.getMillis();
        }
    }
New to GrepCode? Check out our FAQ X