[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 geniusweb.progress.Progress;
|
---|
| 23 | import geniusweb.progress.ProgressRounds;
|
---|
| 24 | import geniusweb.progress.ProgressTime;
|
---|
| 25 | import tudelft.utilities.junit.GeneralTests;
|
---|
| 26 |
|
---|
| 27 | public 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 | }
|
---|