1 | package geniusweb.progress;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 |
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.util.Arrays;
|
---|
7 | import java.util.Date;
|
---|
8 | import java.util.LinkedList;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import org.junit.Test;
|
---|
12 |
|
---|
13 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
14 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
15 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
16 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
17 |
|
---|
18 | import geniusweb.progress.Progress;
|
---|
19 | import geniusweb.progress.ProgressTime;
|
---|
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 = "{\"time\":{\"duration\":10,\"start\":0}}";
|
---|
32 |
|
---|
33 | @Override
|
---|
34 | public List<List<Progress>> getGeneralTestData() {
|
---|
35 | List<List<Progress>> list = new LinkedList<>();
|
---|
36 | list.add(Arrays.asList(progress1, progress1a));
|
---|
37 | list.add(Arrays.asList(progress2));
|
---|
38 | return list;
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public List<String> getGeneralTestStrings() {
|
---|
43 | return Arrays.asList("ProgressTime\\[0.*10ms\\]",
|
---|
44 | "ProgressTime\\[0.*200ms\\]");
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Test
|
---|
48 | public void testInit() {
|
---|
49 | assertEquals((Double) 0d, progress1.get(0l));
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Test
|
---|
53 | public void testAdvancingTime() throws InterruptedException {
|
---|
54 | for (long n = 0; n <= TESTTIME; n++) {
|
---|
55 | assertEquals((Double) ((double) n / TESTTIME), progress1.get(n));
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Test
|
---|
60 | public void testAdvanceWhenDeadlineReached() throws InterruptedException {
|
---|
61 | ProgressTime progress = new ProgressTime(TESTTIME, TIMEZERO);
|
---|
62 | assertEquals((Double) 1d, progress1.get(TESTTIME));
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Test
|
---|
66 | public void testSerialize() throws JsonProcessingException {
|
---|
67 | String actual = jackson.writeValueAsString(progress1);
|
---|
68 | System.out.println(actual);
|
---|
69 | assertEquals(progressstring, actual);
|
---|
70 |
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Test
|
---|
74 | public void testDeserialize()
|
---|
75 | throws JsonParseException, JsonMappingException, IOException {
|
---|
76 | ProgressTime newprog = jackson.readValue(progressstring,
|
---|
77 | ProgressTime.class);
|
---|
78 | // we can't directly compare with progress since that's a hacked
|
---|
79 | // object...
|
---|
80 | assertEquals(TESTTIME, newprog.getDuration().intValue());
|
---|
81 | assertEquals(0, newprog.getStart().getTime());
|
---|
82 |
|
---|
83 | }
|
---|
84 |
|
---|
85 | }
|
---|