diff --git a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java
index 2692a24d..3f40b3f1 100644
--- a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java
+++ b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java
@@ -94,12 +94,14 @@ public class VehicleImpl implements Vehicle {
}
/**
- * Sets the {@link VehicleType}.
+ * Sets the {@link VehicleType}.
*
* @param type
+ * @throws IllegalStateException if type is null
* @return this builder
*/
public Builder setType(VehicleType type){
+ if(type==null) throw new IllegalStateException("type cannot be null.");
this.type = type;
return this;
}
@@ -166,6 +168,9 @@ public class VehicleImpl implements Vehicle {
/**
* Builds and returns the vehicle.
*
+ *
if {@link VehicleType} is not set, default vehicle-type is set with id="default" and + * capacity=0 + * * @return vehicle * @throw IllegalStateException if both locationId and locationCoord is not set */ @@ -189,6 +194,8 @@ public class VehicleImpl implements Vehicle { /** * Returns empty/noVehicle which is a vehicle having no capacity, no type and no reasonable id. * + *
NoVehicle has id="noVehicle" and extends {@link VehicleImpl} + * * @return emptyVehicle */ public static NoVehicle createNoVehicle(){ @@ -219,9 +226,14 @@ public class VehicleImpl implements Vehicle { returnToDepot = builder.returnToDepot; } + /** + * Returns String with attributes of this vehicle + * + *
String has the following format [attr1=val1][attr2=val2]...[attrn=valn] + */ @Override public String toString() { - return "[id="+id+"][type="+type+"][locationId="+locationId+"][coord=" + coord + "]"; + return "[id="+id+"][type="+type+"][locationId="+locationId+"][coord=" + coord + "][isReturnToDepot=" + isReturnToDepot() + "]"; } public Coordinate getCoord() { diff --git a/jsprit-core/src/test/java/jsprit/core/problem/vehicle/VehicleImplTest.java b/jsprit-core/src/test/java/jsprit/core/problem/vehicle/VehicleImplTest.java new file mode 100644 index 00000000..5f3fa4cd --- /dev/null +++ b/jsprit-core/src/test/java/jsprit/core/problem/vehicle/VehicleImplTest.java @@ -0,0 +1,102 @@ +package jsprit.core.problem.vehicle; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; +import jsprit.core.problem.vehicle.VehicleImpl.NoVehicle; +import jsprit.core.util.Coordinate; + +import org.junit.Test; + + +public class VehicleImplTest { + + @Test + public void whenSettingTypeWithBuilder_typeShouldBeSet(){ + VehicleType type = mock(VehicleType.class); + Vehicle v = VehicleImpl.Builder.newInstance("v").setLocationId("loc").setType(type).build(); + assertEquals(type,v.getType()); + } + + @Test(expected=IllegalStateException.class) + public void whenTypeIsNull_itThrowsIllegalStateException(){ + @SuppressWarnings("unused") + Vehicle v = VehicleImpl.Builder.newInstance("v").setLocationId("loc").setType(null).build(); + } + + @Test + public void whenTypeIsNotSet_defaultTypeIsSet(){ + Vehicle v = VehicleImpl.Builder.newInstance("v").setLocationId("loc").build(); + assertEquals("default",v.getType().getTypeId()); + assertEquals(0,v.getType().getCapacity()); + } + + @Test(expected=IllegalStateException.class) + public void whenVehicleIsBuiltWithoutSettingNeitherLocationNorCoord_itThrowsAnIllegalStateException(){ + @SuppressWarnings("unused") + Vehicle v = VehicleImpl.Builder.newInstance("v").build(); + } + + @Test + public void whenVehicleIsBuiltAndReturnToDepotFlagIsNotSet_itShouldReturnToDepot(){ + Vehicle v = VehicleImpl.Builder.newInstance("v").setLocationId("loc").build(); + assertTrue(v.isReturnToDepot()); + } + + @Test + public void whenVehicleIsBuiltToReturnToDepot_itShouldReturnToDepot(){ + Vehicle v = VehicleImpl.Builder.newInstance("v").setReturnToDepot(true).setLocationId("loc").build(); + assertTrue(v.isReturnToDepot()); + } + + @Test + public void whenVehicleIsBuiltToNotReturnToDepot_itShouldNotReturnToDepot(){ + Vehicle v = VehicleImpl.Builder.newInstance("v").setReturnToDepot(false).setLocationId("loc").build(); + assertFalse(v.isReturnToDepot()); + } + + @Test + public void whenVehicleIsBuiltWithLocation_itShouldHvTheCorrectLocation(){ + Vehicle v = VehicleImpl.Builder.newInstance("v").setLocationId("loc").build(); + assertEquals("loc",v.getLocationId()); + } + + @Test + public void whenVehicleIsBuiltWithCoord_itShouldHvTheCorrectCoord(){ + Vehicle v = VehicleImpl.Builder.newInstance("v").setLocationCoord(Coordinate.newInstance(1, 2)).build(); + assertEquals(1.0,v.getCoord().getX(),0.01); + assertEquals(2.0,v.getCoord().getY(),0.01); + } + + @Test + public void whenVehicleIsBuiltAndEarliestStartIsNotSet_itShouldSetTheDefaultOfZero(){ + Vehicle v = VehicleImpl.Builder.newInstance("v").setLocationCoord(Coordinate.newInstance(1, 2)).build(); + assertEquals(0.0,v.getEarliestDeparture(),0.01); + } + + @Test + public void whenVehicleIsBuiltAndEarliestStartSet_itShouldBeSetCorrectly(){ + Vehicle v = VehicleImpl.Builder.newInstance("v").setEarliestStart(10.0).setLocationCoord(Coordinate.newInstance(1, 2)).build(); + assertEquals(10.0,v.getEarliestDeparture(),0.01); + } + + @Test + public void whenVehicleIsBuiltAndLatestArrivalIsNotSet_itShouldSetDefaultOfDoubleMaxValue(){ + Vehicle v = VehicleImpl.Builder.newInstance("v").setLocationCoord(Coordinate.newInstance(1, 2)).build(); + assertEquals(Double.MAX_VALUE,v.getLatestArrival(),0.01); + } + + @Test + public void whenVehicleIsBuiltAndLatestArrivalIsSet_itShouldBeSetCorrectly(){ + Vehicle v = VehicleImpl.Builder.newInstance("v").setLatestArrival(30.0).setLocationCoord(Coordinate.newInstance(1, 2)).build(); + assertEquals(30.0,v.getLatestArrival(),0.01); + } + + @Test + public void whenNoVehicleIsCreate_itShouldHvTheCorrectId(){ + Vehicle v = VehicleImpl.createNoVehicle(); + assertTrue(v instanceof NoVehicle); + assertEquals("noVehicle",v.getId()); + } + + +}