package geniusweb.progress; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.io.IOException; import java.util.Arrays; import java.util.Date; import java.util.LinkedList; import java.util.List; import org.junit.Test; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import tudelft.utilities.junit.GeneralTests; public class ProgressTimeTest extends GeneralTests { private static final Date TIMEZERO = new Date(0l); private final static long TESTTIME = 10; // milliseconds! private final ProgressTime progress1 = new ProgressTime(TESTTIME, TIMEZERO); private final ProgressTime progress1a = new ProgressTime(TESTTIME, TIMEZERO); private final ProgressTime progress2 = new ProgressTime(200l, TIMEZERO); private final ObjectMapper jackson = new ObjectMapper(); private final String progressstring = "{\"ProgressTime\":{\"duration\":10,\"start\":0}}"; private final long start = 1234; private final ProgressTime pr = new ProgressTime(TESTTIME, new Date(start)); @Override public List> getGeneralTestData() { List> list = new LinkedList<>(); list.add(Arrays.asList(progress1, progress1a)); list.add(Arrays.asList(progress2)); return list; } @Override public List getGeneralTestStrings() { return Arrays.asList("ProgressTime\\[0.*10ms\\]", "ProgressTime\\[0.*200ms\\]"); } @Test public void testInit() { assertEquals((Double) 0d, progress1.get(0l)); } @Test public void testAdvancingTime() throws InterruptedException { for (long n = 0; n <= TESTTIME; n++) { assertEquals((Double) ((double) n / TESTTIME), progress1.get(n)); } } @Test public void testAdvanceWhenDeadlineReached() throws InterruptedException { ProgressTime progress = new ProgressTime(TESTTIME, TIMEZERO); assertEquals((Double) 1d, progress1.get(TESTTIME)); } @Test public void testSerialize() throws JsonProcessingException { String actual = jackson.writeValueAsString(progress1); System.out.println(actual); assertEquals(progressstring, actual); } @Test public void testDeserialize() throws JsonParseException, JsonMappingException, IOException { ProgressTime newprog = jackson.readValue(progressstring, ProgressTime.class); // we can't directly compare with progress since that's a hacked // object... assertEquals(TESTTIME, newprog.getDuration().intValue()); assertEquals(0, newprog.getStart().getTime()); } @Test public void pastDeadline() { assertTrue(pr.isPastDeadline(start + TESTTIME + 1)); assertFalse(pr.isPastDeadline(start + TESTTIME - 1)); } @Test public void terminationTime() { assertEquals(start + TESTTIME, pr.getTerminationTime().getTime()); } @Test public void get() { assertEquals((Double) 0d, pr.get(start)); assertEquals((Double) 1d, pr.get(start + TESTTIME)); assertEquals((Double) 0.5d, pr.get(start + TESTTIME / 2)); assertEquals((Double) 0d, pr.get(start - 10)); assertEquals((Double) 1d, pr.get(start + TESTTIME + 10)); } }