source: timeline/src/test/java/geniusweb/progress/ProgressTimeTest.java@ 24

Last change on this file since 24 was 1, checked in by bart, 5 years ago

Initial Release

File size: 2.5 KB
RevLine 
[1]1package geniusweb.progress;
2
3import static org.junit.Assert.assertEquals;
4
5import java.io.IOException;
6import java.util.Arrays;
7import java.util.Date;
8import java.util.LinkedList;
9import java.util.List;
10
11import org.junit.Test;
12
13import com.fasterxml.jackson.core.JsonParseException;
14import com.fasterxml.jackson.core.JsonProcessingException;
15import com.fasterxml.jackson.databind.JsonMappingException;
16import com.fasterxml.jackson.databind.ObjectMapper;
17
18import geniusweb.progress.Progress;
19import geniusweb.progress.ProgressTime;
20import tudelft.utilities.junit.GeneralTests;
21
22public 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}
Note: See TracBrowser for help on using the repository browser.