[35] | 1 | package geniusweb.progress;
|
---|
| 2 |
|
---|
| 3 | import static org.junit.Assert.assertEquals;
|
---|
| 4 | import static org.junit.Assert.assertFalse;
|
---|
| 5 | import static org.junit.Assert.assertTrue;
|
---|
| 6 |
|
---|
| 7 | import java.io.IOException;
|
---|
| 8 | import java.util.Arrays;
|
---|
| 9 | import java.util.Date;
|
---|
| 10 | import java.util.LinkedList;
|
---|
| 11 | import java.util.List;
|
---|
| 12 |
|
---|
| 13 | import org.junit.Test;
|
---|
| 14 |
|
---|
| 15 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
| 16 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
| 17 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
| 18 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
| 19 |
|
---|
| 20 | import tudelft.utilities.junit.GeneralTests;
|
---|
| 21 |
|
---|
| 22 | public class ProgressTimeTest extends GeneralTests<Progress> {
|
---|
| 23 | private static final Date TIMEZERO = new Date(0l);
|
---|
| 24 | private final static long TESTTIME = 10; // milliseconds!
|
---|
| 25 | private final ProgressTime progress1 = new ProgressTime(TESTTIME, TIMEZERO);
|
---|
| 26 | private final ProgressTime progress1a = new ProgressTime(TESTTIME,
|
---|
| 27 | TIMEZERO);
|
---|
| 28 | private final ProgressTime progress2 = new ProgressTime(200l, TIMEZERO);
|
---|
| 29 |
|
---|
| 30 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
| 31 | private final String progressstring = "{\"ProgressTime\":{\"duration\":10,\"start\":0}}";
|
---|
| 32 |
|
---|
| 33 | private final long start = 1234;
|
---|
| 34 | private final ProgressTime pr = new ProgressTime(TESTTIME, new Date(start));
|
---|
| 35 |
|
---|
| 36 | @Override
|
---|
| 37 | public List<List<Progress>> getGeneralTestData() {
|
---|
| 38 | List<List<Progress>> list = new LinkedList<>();
|
---|
| 39 | list.add(Arrays.asList(progress1, progress1a));
|
---|
| 40 | list.add(Arrays.asList(progress2));
|
---|
| 41 | return list;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | @Override
|
---|
| 45 | public List<String> getGeneralTestStrings() {
|
---|
| 46 | return Arrays.asList("ProgressTime\\[0.*10ms\\]",
|
---|
| 47 | "ProgressTime\\[0.*200ms\\]");
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | @Test
|
---|
| 51 | public void testInit() {
|
---|
| 52 | assertEquals((Double) 0d, progress1.get(0l));
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | @Test
|
---|
| 56 | public void testAdvancingTime() throws InterruptedException {
|
---|
| 57 | for (long n = 0; n <= TESTTIME; n++) {
|
---|
| 58 | assertEquals((Double) ((double) n / TESTTIME), progress1.get(n));
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | @Test
|
---|
| 63 | public void testAdvanceWhenDeadlineReached() throws InterruptedException {
|
---|
| 64 | ProgressTime progress = new ProgressTime(TESTTIME, TIMEZERO);
|
---|
| 65 | assertEquals((Double) 1d, progress1.get(TESTTIME));
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | @Test
|
---|
| 69 | public void testSerialize() throws JsonProcessingException {
|
---|
| 70 | String actual = jackson.writeValueAsString(progress1);
|
---|
| 71 | System.out.println(actual);
|
---|
| 72 | assertEquals(progressstring, actual);
|
---|
| 73 |
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | @Test
|
---|
| 77 | public void testDeserialize()
|
---|
| 78 | throws JsonParseException, JsonMappingException, IOException {
|
---|
| 79 | ProgressTime newprog = jackson.readValue(progressstring,
|
---|
| 80 | ProgressTime.class);
|
---|
| 81 | // we can't directly compare with progress since that's a hacked
|
---|
| 82 | // object...
|
---|
| 83 | assertEquals(TESTTIME, newprog.getDuration().intValue());
|
---|
| 84 | assertEquals(0, newprog.getStart().getTime());
|
---|
| 85 |
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | @Test
|
---|
| 89 | public void pastDeadline() {
|
---|
| 90 | assertTrue(pr.isPastDeadline(start + TESTTIME + 1));
|
---|
| 91 | assertFalse(pr.isPastDeadline(start + TESTTIME - 1));
|
---|
| 92 |
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | @Test
|
---|
| 96 | public void terminationTime() {
|
---|
| 97 | assertEquals(start + TESTTIME, pr.getTerminationTime().getTime());
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | @Test
|
---|
| 101 | public void get() {
|
---|
| 102 | assertEquals((Double) 0d, pr.get(start));
|
---|
| 103 | assertEquals((Double) 1d, pr.get(start + TESTTIME));
|
---|
| 104 | assertEquals((Double) 0.5d, pr.get(start + TESTTIME / 2));
|
---|
| 105 | assertEquals((Double) 0d, pr.get(start - 10));
|
---|
| 106 | assertEquals((Double) 1d, pr.get(start + TESTTIME + 10));
|
---|
| 107 |
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | }
|
---|