package nl.hsac.fitnesse.symbols;

import fitnesse.util.Clock;
import fitnesse.util.DateAlteringClock;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import static org.junit.Assert.assertEquals;

public class NextXDayTest {
    @BeforeClass
    public static void fixClock() {
        setClock(2017, 3, 10);
    }

    @AfterClass
    public static void resetClock() {
        Clock.restoreDefaultClock();
    }

    @Test
    public void testNoIncrement() {
        checkNext(new NextFriday(), 0, 10);
        checkNext(new NextSaturday(), 0, 11);
        checkNext(new NextSunday(), 0, 12);
        checkNext(new NextMonday(), 0, 13);
        checkNext(new NextTuesday(), 0, 14);
        checkNext(new NextWednesday(), 0, 15);
        checkNext(new NextThursday(), 0, 16);
    }

    @Test
    public void testIncrementOne() {
        checkNext(new NextFriday(), 1, 17);
        checkNext(new NextSaturday(), 1, 18);
        checkNext(new NextSunday(), 1, 19);
        checkNext(new NextMonday(), 1, 20);
        checkNext(new NextTuesday(), 1, 21);
        checkNext(new NextWednesday(), 1, 22);
        checkNext(new NextThursday(), 1, 23);
    }

    @Test
    public void testIncrementMinusOne() {
        checkNext(new NextFriday(), -1, 3);
        checkNext(new NextSaturday(), -1, 4);
        checkNext(new NextSunday(), -1, 5);
        checkNext(new NextMonday(), -1, 6);
        checkNext(new NextTuesday(), -1, 7);
        checkNext(new NextWednesday(), -1, 8);
        checkNext(new NextThursday(), -1, 9);
    }

    @Test
    public void testIncrementTwo() {
        checkNext(new NextFriday(), 2, 24);
        checkNext(new NextSaturday(), 2, 25);
        checkNext(new NextSunday(), 2, 26);
        checkNext(new NextMonday(), 2, 27);
        checkNext(new NextTuesday(), 2, 28);
        checkNext(new NextWednesday(), 2, 29);
        checkNext(new NextThursday(), 2, 30);
    }

    @Test
    public void testIncrementMinusTwo() {
        checkNext(new NextWednesday(), -2, 1);
        checkNext(new NextThursday(), -2, 2);
    }

    private void checkNext(NextXDay nf, int increment, int day) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(Clock.currentDate());
        nf.addIncrement(calendar, increment);
        checkDate(2017, 3, day, calendar);
    }

    private static void setClock(int year, int month, int day) {
        DateAlteringClock d = new DateAlteringClock(new Date(year - 1900, month - 1, day));
        d.freeze();
    }

    private void checkDate(int year, int month, int day, GregorianCalendar cal) {
        assertEquals("year", year, cal.get(Calendar.YEAR));
        assertEquals("month", month - 1, cal.get(Calendar.MONTH));
        assertEquals("day", day, cal.get(Calendar.DAY_OF_MONTH));
    }
}

@@ -32,135 +32,8 @@
 s;%0A%0A
-import fitnesse.util.Clock;%0Aimport fitnesse.util.DateAlteringClock;%0Aimport org.junit.AfterClass;%0Aimport org.junit.BeforeClass;%0A
 impo
@@ -51,16 +51,16 @@
 t.Test;%0A
+
 %0Aimport 
@@ -83,31 +83,8 @@
 ar;%0A
-import java.util.Date;%0A
 impo
@@ -162,16 +162,16 @@
 quals;%0A%0A
+
 public c
@@ -194,197 +194,8 @@
 t %7B%0A
-    @BeforeClass%0A    public static void fixClock() %7B%0A        setClock(2017, 3, 10);%0A    %7D%0A%0A    @AfterClass%0A    public static void resetClock() %7B%0A        Clock.restoreDefaultClock();%0A    %7D%0A%0A
     
@@ -1955,55 +1955,19 @@
 dar(
-);%0A        calendar.setTime(Clock.currentDate()
+2017, 2, 10
 );%0A 
@@ -2065,192 +2065,8 @@
  %7D%0A%0A
-    private static void setClock(int year, int month, int day) %7B%0A        DateAlteringClock d = new DateAlteringClock(new Date(year - 1900, month - 1, day));%0A        d.freeze();%0A    %7D%0A%0A
     

Simplify test