org.unitils.jodatime.JodaTimeModule Maven / Gradle / Ivy
The newest version!
package org.unitils.jodatime;
import java.lang.reflect.Method;
import java.util.Properties;
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;
import org.unitils.core.Module;
import org.unitils.core.TestListener;
import org.unitils.jodatime.annotation.FixedDateTime;
import org.unitils.jodatime.annotation.OffsetDateTime;
/**
* Module to run test with a provided datetime.
*
* Possible uses:
*
* - 1. time can be frozen to a specific datetime using the {@link FixedDateTime} annotation on test class or method.
* - 2. time can be an offset of the current datetime using the {@link 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 Christophe De Blende
* @author Jeroen Horemans
* @author Thomas De Rycke
* @author Willemijn Wouters
*
* @since 1.0.0
*
*/
public class JodaTimeModule implements Module {
@Override
public void init(Properties configuration) {
//do nothing
}
@Override
public void afterInit() {
//do nothing
}
protected void changeDate(FixedDateTime dateTime) {
DateTimeUtils.setCurrentMillisFixed(calculateFixedMillis(dateTime));
}
protected void changeDate(OffsetDateTime dateTime) {
DateTimeUtils.setCurrentMillisOffset(calculateOffsetMillis(dateTime));
}
protected 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();
}
protected 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(currentDate, offisetDate);
return duration.getMillis();
}
@Override
public TestListener getTestListener() {
return new DateTimeModuleListener();
}
protected class DateTimeModuleListener extends TestListener {
@Override
public void afterTestTearDown(Object testObject, Method testMethod) {
DateTimeUtils.setCurrentMillisSystem();
}
@Override
public void beforeTestSetUp(Object testObject, Method testMethod) {
if (testMethod.isAnnotationPresent(FixedDateTime.class)) {
changeDate(testMethod.getAnnotation(FixedDateTime.class));
} else if (testMethod.isAnnotationPresent(OffsetDateTime.class)) {
changeDate(testMethod.getAnnotation(OffsetDateTime.class));
} else if (testObject.getClass().isAnnotationPresent(FixedDateTime.class)) {
changeDate(testObject.getClass().getAnnotation(FixedDateTime.class));
} else if (testObject.getClass().isAnnotationPresent(OffsetDateTime.class)) {
changeDate(testObject.getClass().getAnnotation(OffsetDateTime.class));
} else {
// if there is no annotation no where we use the real current time.
DateTimeUtils.setCurrentMillisSystem();
}
}
}
}