added more tests for CourseTemplate

Change-Id: Ic2e70fefe9c9dbf7928141da1b9ed3a244ed3103
This commit is contained in:
Axel Uhl
2019-07-04 18:03:26 +02:00
parent e2b64fd08d
commit 2f68ec5b21
3 changed files with 54 additions and 3 deletions
@@ -1,6 +1,8 @@
package com.sap.sailing.domain.coursetemplate.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeNoException;
import java.util.ArrayList;
import java.util.Arrays;
@@ -81,4 +83,43 @@ public class TestLapConfiguration {
final List<ControlPointTemplate> expected = Arrays.asList(startFinish, top, gate, top, gate, top, startFinish);
verify(waypointsOfOneLapper, expected);
}
@Test
public void testExceptionInCaseMarkIsMissing() {
startBoat = new MarkTemplateImpl("Start Boat", "SB", /* color */ null, /* shape */ null, /* pattern */ null, MarkType.STARTBOAT);
pin = new MarkTemplateImpl("Pin End", "Pin", /* color */ null, /* shape */ null, /* pattern */ null, MarkType.BUOY);
startFinish = new ControlPointTemplateImpl("Start/Finish", Arrays.asList(startBoat, pin));
try {
courseTemplate = new CourseTemplateImpl("Test",
/* marks */ Arrays.asList(startBoat),
/* waypoints */ Arrays.asList(new WaypointTemplateImpl(startFinish, PassingInstruction.Line)));
fail("Expected an IllegalArgumentException due to missing mark <pin> but it wasn't thrown");
} catch (IllegalArgumentException e) {
// expected
}
}
@Test
public void testZeroLapsOkIfNoRepeatablePart() {
startBoat = new MarkTemplateImpl("Start Boat", "SB", /* color */ null, /* shape */ null, /* pattern */ null, MarkType.STARTBOAT);
pin = new MarkTemplateImpl("Pin End", "Pin", /* color */ null, /* shape */ null, /* pattern */ null, MarkType.BUOY);
startFinish = new ControlPointTemplateImpl("Start/Finish", Arrays.asList(startBoat, pin));
courseTemplate = new CourseTemplateImpl("Test", /* marks */ Arrays.asList(startBoat, pin),
/* waypoints */ Arrays.asList(new WaypointTemplateImpl(startFinish, PassingInstruction.Line)));
try {
courseTemplate.getWaypoints(0);
} catch (IllegalArgumentException e) {
assumeNoException("No IllegalArgumentException should have been thrown for zero laps because the course has no repeatable part", e);
}
}
@Test
public void testIllegalArgumentExceptionForZeroLaps() {
try {
courseTemplate.getWaypoints(/* illegal to request 0 laps if course template defines repeatable part */ 0);
fail("Expected an IllegalArgumentException but none was thrown");
} catch (IllegalArgumentException e) {
// expected
}
}
}
@@ -43,9 +43,16 @@ public interface CourseTemplate extends NamedWithID {
* to repeat this sub-sequence. Typically, the repeatable sub-sequence will be repeated one times fewer than the
* {@code numberOfLaps}. For example, in a typical windward-leeward "L" course we would have
* {@code Start/Finish, [1, 4p/4s], 1, Start/Finish}. For an "L1" course with only one lap, we'd like to have
* {@code Start/Finish, 1, Start/Finish}, so the repeatable sub-sequence, enclosed by the brackets in the example above,
* will occur zero times. For an "L2" the repeatable sub-sequence will occur once, and so on. However, an implementation
* is free to choose an interpretation of {@code numberOfLaps} that meets callers' expectations.
* {@code Start/Finish, 1, Start/Finish}, so the repeatable sub-sequence, enclosed by the brackets in the example
* above, will occur zero times. For an "L2" the repeatable sub-sequence will occur once, and so on. However, an
* implementation is free to choose an interpretation of {@code numberOfLaps} that meets callers' expectations.
*
* @param numberOfLaps
* if the course defines a repeatable part, the number of laps at least needs to be {@code 1} for the
* default implementation, and an {@link IllegalArgumentException} shall be thrown in case a value less
* than {@code 1} is used if this template specifies a repeatable part. Note again that the number of
* repetitions of the repeatable part is usually one less than the number of laps, therefore this
* limitation.
*/
Iterable<WaypointTemplate> getWaypoints(int numberOfLaps);
}
@@ -80,6 +80,9 @@ public class CourseTemplateImpl extends NamedWithIDImpl implements CourseTemplat
public Iterable<WaypointTemplate> getWaypoints(int numberOfLaps) {
final Iterable<WaypointTemplate> result;
if (hasRepeatablePart()) {
if (numberOfLaps < 1) {
throw new IllegalArgumentException("The course template "+this+" has a repeatable part, hence the number of laps needs to be at least 1.");
}
final List<WaypointTemplate> resultList = new LinkedList<>();
for (int i=0; i<waypoints.size(); i++) {
if (i == zeroBasedIndexOfRepeatablePartStart) {