Skip to content

Instantly share code, notes, and snippets.

@mikeycmccarthy
Created April 20, 2012 07:20
Show Gist options
  • Save mikeycmccarthy/2426817 to your computer and use it in GitHub Desktop.
Save mikeycmccarthy/2426817 to your computer and use it in GitHub Desktop.
Testing win restriction dates
import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;
import static org.joda.time.Duration.standardHours;
import static org.junit.Assert.assertEquals;
@RunWith(value = Parameterized.class)
public class WinRestrictionExpiryCalculatorUTest {
private static DateTime now;
private static DateTime tenHoursAgo;
private static DateTime fifteenHoursAgo;
private final int restrictionLengthInHours;
private final DateTime lastWinDate;
private final DateTime expectedNextEligibleToWinDate;
// Because parameterized tests run the data instantiation in a static block, we need to set the current date in a static block
static {
DateTimeUtils.setCurrentMillisFixed(new DateTime().getMillis());
now = DateTime.now();
tenHoursAgo = now.minus(standardHours(10));
fifteenHoursAgo = DateTime.now().minus(standardHours(15));
}
@Parameterized.Parameters
public static Collection<Object[]> data() {
Object[][] data = new Object[][]{
{7, now, now.plus(standardHours(7))},
{36, tenHoursAgo, tenHoursAgo.plus(standardHours(36))},
{36, fifteenHoursAgo, fifteenHoursAgo.plus(standardHours(36))},
{36, null, null}};
return Arrays.asList(data);
}
private WinRestrictionExpiryCalculator testObject;
public WinRestrictionExpiryCalculatorUTest(int restrictionLengthInHours, DateTime lastWinDate, DateTime expectedNextEligibleToWinDate) {
this.restrictionLengthInHours = restrictionLengthInHours;
this.lastWinDate = lastWinDate;
this.expectedNextEligibleToWinDate = expectedNextEligibleToWinDate;
}
@Before
public void setUp() throws Exception {
testObject = new WinRestrictionExpiryCalculator();
}
@After
public void tearDown() throws Exception {
DateTimeUtils.setCurrentMillisSystem();
}
@Test
public void testWinRestrictionExpiry() throws Exception {
assertEquals(expectedNextEligibleToWinDate, testObject.calculateWinRestrictionExpiry(lastWinDate, standardHours(restrictionLengthInHours)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment