package geniusweb.progress; import static org.junit.Assert.assertEquals; 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 geniusweb.progress.Progress; import geniusweb.progress.ProgressTime; 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 = "{\"time\":{\"duration\":10,\"start\":0}}"; @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()); } }