1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00

add vehicle test

This commit is contained in:
oblonski 2014-01-15 11:35:46 -05:00
parent 7d8e32fc8d
commit 623e89fb23
2 changed files with 116 additions and 2 deletions

View file

@ -94,12 +94,14 @@ public class VehicleImpl implements Vehicle {
} }
/** /**
* Sets the {@link VehicleType}. * Sets the {@link VehicleType}.<br>
* *
* @param type * @param type
* @throws IllegalStateException if type is null
* @return this builder * @return this builder
*/ */
public Builder setType(VehicleType type){ public Builder setType(VehicleType type){
if(type==null) throw new IllegalStateException("type cannot be null.");
this.type = type; this.type = type;
return this; return this;
} }
@ -166,6 +168,9 @@ public class VehicleImpl implements Vehicle {
/** /**
* Builds and returns the vehicle. * Builds and returns the vehicle.
* *
* <p>if {@link VehicleType} is not set, default vehicle-type is set with id="default" and
* capacity=0
*
* @return vehicle * @return vehicle
* @throw IllegalStateException if both locationId and locationCoord is not set * @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. * Returns empty/noVehicle which is a vehicle having no capacity, no type and no reasonable id.
* *
* <p>NoVehicle has id="noVehicle" and extends {@link VehicleImpl}
*
* @return emptyVehicle * @return emptyVehicle
*/ */
public static NoVehicle createNoVehicle(){ public static NoVehicle createNoVehicle(){
@ -219,9 +226,14 @@ public class VehicleImpl implements Vehicle {
returnToDepot = builder.returnToDepot; returnToDepot = builder.returnToDepot;
} }
/**
* Returns String with attributes of this vehicle
*
* <p>String has the following format [attr1=val1][attr2=val2]...[attrn=valn]
*/
@Override @Override
public String toString() { 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() { public Coordinate getCoord() {

View file

@ -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());
}
}