source: timeline/src/test/java/geniusweb/progress/ProgressRoundsTest.java@ 7

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

Initial Release

File size: 3.3 KB
RevLine 
[1]1package geniusweb.progress;
2
3import static org.junit.Assert.assertEquals;
4import static org.mockito.Mockito.mock;
5import static org.mockito.Mockito.when;
6
7import java.io.IOException;
8import java.util.Arrays;
9import java.util.Date;
10import java.util.LinkedList;
11import java.util.List;
12
13import org.junit.Before;
14import org.junit.Test;
15
16import com.fasterxml.jackson.core.JsonParseException;
17import com.fasterxml.jackson.core.JsonProcessingException;
18import com.fasterxml.jackson.databind.JsonMappingException;
19import com.fasterxml.jackson.databind.ObjectMapper;
20
21import geniusweb.deadline.DeadlineRounds;
22import geniusweb.progress.Progress;
23import geniusweb.progress.ProgressRounds;
24import geniusweb.progress.ProgressTime;
25import tudelft.utilities.junit.GeneralTests;
26
27public class ProgressRoundsTest extends GeneralTests<Progress> {
28 private DeadlineRounds deadline = mock(DeadlineRounds.class);
29
30 private final Date date = new Date(999l);
31 private final Date date0 = new Date(0l);
32
33 private final Progress deadline1 = new ProgressRounds(20, 0, date);
34 private final Progress deadline2 = new ProgressRounds(20, 0, date);
35 private final Progress deadline3 = new ProgressRounds(30, 0, date);
36 private final Progress deadline4 = new ProgressTime(20l, date0);
37 private final Progress deadline5 = new ProgressTime(20l, date);
38 private ProgressRounds progress;
39 private final static Integer TESTROUNDS = 10;
40 private final ObjectMapper jackson = new ObjectMapper();
41 private final String progressstring = "{\"rounds\":{\"duration\":10,\"currentRound\":0,\"endtime\":999}}";
42
43 @Before
44 public void before() {
45 when(deadline.getRounds()).thenReturn(TESTROUNDS);
46 progress = new ProgressRounds(deadline.getRounds(), 0, date);
47
48 }
49
50 @Override
51 public List<List<Progress>> getGeneralTestData() {
52 List<List<Progress>> list = new LinkedList<>();
53 list.add(Arrays.asList(deadline1, deadline2));
54 list.add(Arrays.asList(deadline3));
55 list.add(Arrays.asList(deadline4));
56 list.add(Arrays.asList(deadline5));
57 return list;
58 }
59
60 @Override
61 public List<String> getGeneralTestStrings() {
62 return Arrays.asList("ProgressRounds\\[0.*20\\]",
63 "ProgressRounds\\[0.*30\\]", "ProgressTime\\[0.*20ms\\]",
64 "ProgressTime\\[999.*20ms\\]");
65 }
66
67 @Test
68 public void testInit() {
69 assertEquals((Double) 0d, progress.get(1l));
70 }
71
72 @Test
73 public void testAdvance() {
74 ProgressRounds p = progress;
75 int n = 0;
76 while (n <= TESTROUNDS) {
77 assertEquals((Double) ((double) n / TESTROUNDS), p.get(1l));
78 n++;
79 p = p.advance();
80 }
81 }
82
83 @Test
84 public void testAdvanceWhenDeadlineReached() {
85 ProgressRounds p = new ProgressRounds(TESTROUNDS, 9, date);
86 p = p.advance();
87 assertEquals((Double) 1d, p.get(1l));
88 }
89
90 @Test
91 public void testSerialize() throws JsonProcessingException {
92 System.out.println(jackson.writeValueAsString(progress));
93 assertEquals(progressstring, jackson.writeValueAsString(progress));
94
95 }
96
97 @Test
98 public void testDeserialize()
99 throws JsonParseException, JsonMappingException, IOException {
100 assertEquals(progress,
101 jackson.readValue(progressstring, Progress.class));
102
103 }
104
105 @Test(expected = IllegalArgumentException.class)
106 public void testIllegalArg1() {
107 new ProgressRounds(10, 11, date);
108 }
109
110 @Test(expected = IllegalArgumentException.class)
111 public void testIllegalArg2() {
112 new ProgressRounds(10, -1, date);
113 }
114
115}
Note: See TracBrowser for help on using the repository browser.