-
Notifications
You must be signed in to change notification settings - Fork 0
/
MotorBikeTest.java
136 lines (107 loc) · 4.87 KB
/
MotorBikeTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* test cases of the class motorbike clas. TCSS 305 - Rentz
*/
package tests;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import org.junit.Test;
import model.vehicles.MotorBike;
import org.junit.Before;
/**
* @author hphobbs
* @version Winter 2020
*/
public class MotorBikeTest {
private MotorBike myTouringMotorBike, myNonTouringMotorBike;
@Before
public void setUp() {
myTouringMotorBike = new MotorBike("Name", "B100", true, true);
myNonTouringMotorBike = new MotorBike("Name2", "B102", false, false);
}
/**
* {@link model.vehicles.MotorBike.
*/
@Test
public void testHashCode() {
final MotorBike b1 = new MotorBike("name1", "vin1", true, true);
final MotorBike justLikeB1 = new MotorBike("name1", "vin1", true, true);
final MotorBike b2 = new MotorBike("name2", "vin2", true, false);
assertEquals("Same motorBike should return same hashCode()", b1.hashCode(),
b1.hashCode());
assertNotEquals("Different motorbikes should return different hashCode", b1.hashCode(),
b2.hashCode());
assertNotEquals("Motorbikes with same state return different hashCode since Vehicle ID is included in hashCode, and is unique",
b1.hashCode(), justLikeB1.hashCode());
}
/**
* Test whether correct rental amount is returned from calculateRentalAmount method for
* Touring MotorBikes. Test method for {@link MotorBike#calculateRentalAmount()}.
*/
@Test
public void testCalculateRentalAmount_Touring() {
final MotorBike motorBikeWithTouring = new MotorBike("test-name", "test-vin", true, true);
assertEquals(MotorBike.BIKE_FARE.add(MotorBike.TOURING_FEE),
motorBikeWithTouring.calculateRentalAmount());
}
/**
* Test whether correct rental amount is returned from calculateRentalAmount method for
* Non-Touring MotorBikes. Test method for {@link MotorBike#calculateRentalAmount()}.
*/
@Test
public void testCalculateRentalAmount_NonTouring() {
final MotorBike motorBikeWithTouring = new MotorBike("test-name", "test-vin", true, false);
assertEquals(MotorBike.BIKE_FARE, motorBikeWithTouring.calculateRentalAmount());
}
/**
* * Test whether correct string representation of MotorBike object is produced once
* toString is * called. Test method for {@link model.vehicles.MotorBike#toString()}.
*/
@Test
public void testToString() {
assertTrue("toString() does not match: " + myTouringMotorBike.toString(),
myTouringMotorBike.toString()
.matches("^MotorBike \\(ID:\\d+, Name:Name, VIN:B100, CanRent\\?:true, IsTouring\\?:true\\)$"));
}
/**
* Test method for {@link model.vehicles.MotorBike#equals(java.lang.Object)}.
*/
@Test
public void testEquals() {
final MotorBike motorBikeWithSameValues = new MotorBike("Name", "B100", true, true);
// reflexive property is satisfied
assertEquals("Equals failed - reflexive", myTouringMotorBike, myTouringMotorBike);
// if both MotorBike objects with same values, equals still returns false due to the
// vehicle ID
assertNotEquals("Equals failed - value equality", myTouringMotorBike,
motorBikeWithSameValues);
// the motorBike object must not be null.
assertNotEquals("Equals failed - null", myTouringMotorBike, null);
// two motorBike objects with different values.
assertNotEquals("Equals failed -different values", myTouringMotorBike,
myNonTouringMotorBike);
// other object is of a different class.
assertNotEquals("Equals failed - different class", myTouringMotorBike,
new ArrayList<Integer>());
}
/**
*
* {@link model.vehicles.MotorBike# MotorBike(int, java.lang.String, java.lang.String, boolean, boolean, boolean, boolean)}.
*/
@Test
public void testConstructor() {
assertEquals("Name does not match", "Name", myTouringMotorBike.getMyName());
assertEquals("VIN does not match", "B100", myTouringMotorBike.getMyVIN());
}
/**
* Test vehicle IDs are assigned sequentially.
*/
@Test
public void testVehicleIdsAreAssignedSequentially() {
final MotorBike b1 = new MotorBike("test-name", "test-vin", true, true);
final MotorBike b2 = new MotorBike("test-name", "test-vin", true, true);
assertEquals("vehicleIDs are sequential", 1,
b2.getMyVehicleID() - b1.getMyVehicleID());
}
}