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