-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractVehicleTest.java
191 lines (146 loc) · 5.9 KB
/
AbstractVehicleTest.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* test cases of the class AbstractVehicle. TCSS 305 - Rentz
*/
package tests;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import java.math.BigDecimal;
import java.util.ArrayList;
import org.junit.Before;
import org.junit.Test;
import model.vehicles.AbstractVehicle;
/**
* @author hphobbs
* @version Winter 2020
*/
public class AbstractVehicleTest {
/***/
private AbstractVehicle myVehicle;
/***/
private AbstractVehicle myVehicle2;
/***/
@Before
public void setUp() {
myVehicle = new TestAbstractVehicle("Name", "V100", true);
myVehicle2 = new TestAbstractVehicle("Name2", "V102", false);
}
/**
* {@link model.vehicles.AbstractVehicle.
*/
@Test
public void testHashCode() {
final AbstractVehicle v1 = new TestAbstractVehicle("name1", "vin1", true);
final AbstractVehicle justLikeV1 = new TestAbstractVehicle("name1", "vin1", true);
final AbstractVehicle v2 = new TestAbstractVehicle("name2", "vin2", true);
assertEquals("Same abstractVehicle should return same hashCode()", v1.hashCode(),
v1.hashCode());
assertNotEquals("Different abstractVehicles should return different hashCode",
v1.hashCode(), v2.hashCode());
assertNotEquals("AbstractVehicles with same state return different hashCode since Vehicle ID is included in hashCode, and is unique",
v1.hashCode(), justLikeV1.hashCode());
}
/**
* Test method for {@link model.vehicles.AbstractVehicle#equals(java.lang.Object)}.
*/
@Test
public void testEquals() {
final AbstractVehicle abstractVehicleWithSameValues =
new TestAbstractVehicle("Name", "C100", true);
// reflexive property is satisfied
assertEquals("Equals failed - reflexive", myVehicle, myVehicle);
// if both AbstractVehicle objects with same values, equals still returns false due to
// the vehicle ID
assertNotEquals("Equals failed - value equality", myVehicle,
abstractVehicleWithSameValues);
// the abstractVehicle object must not be null.
assertNotEquals("Equals failed - null", myVehicle, null);
// two abstractVehicle objects with different values.
assertNotEquals("Equals failed -different values", myVehicle, myVehicle2);
// other object is of a different class.
assertNotEquals("Equals failed - different class", myVehicle,
new ArrayList<Integer>());
}
/**
*
* {@link model.vehicles.AbstractVehicle# AbstractVehicle(int, java.lang.String, java.lang.String, boolean, boolean, boolean, boolean)}.
*/
@Test
public void testConstructor() {
assertEquals("Name does not match", "Name", myVehicle.getMyName());
assertEquals("VIN does not match", "V100", myVehicle.getMyVIN());
}
/**
* Test vehicle IDs are assigned sequentially.
*/
@Test
public void testVehicleIdsAreAssignedSequentially() {
final AbstractVehicle c1 = new TestAbstractVehicle("test-name", "test-vin", true);
final AbstractVehicle c2 = new TestAbstractVehicle("test-name", "test-vin", false);
assertEquals("vehicleIDs are sequential", 1,
c2.getMyVehicleID() - c1.getMyVehicleID());
}
@Test
public void testGetMyVehicleID() {
final AbstractVehicle c1 = new TestAbstractVehicle("test-name", "test-vin", true);
final AbstractVehicle c2 = new TestAbstractVehicle("test-name", "test-vin", false);
assertEquals("Getter for vehicle ID failed", 1,
c2.getMyVehicleID() - c1.getMyVehicleID(), myVehicle.getMyVehicleID());
}
@Test
public void testGetMyName() {
assertEquals("Name does not match", "Name", myVehicle.getMyName());
}
@Test
public void testGetMyVIN() {
assertEquals("getMyVIN() failed- parameterized constructor", "V100",
myVehicle.getMyVIN());
}
/**
* * Test whether correct string representation of Vehicle object is produced once toString
* * is called.
*/
/**
* * Test whether correct rental amount is returned from calculateRentalAmount method in
* the * Vehicle class.
*/
@Test
public void testCalculateRentalAmount() {
final BigDecimal rent = myVehicle.calculateRentalAmount();
assertEquals("Incorrect rental amount - calculateRentalAmount failed",
myVehicle.calculateRentalAmount(), rent);
}
/** Test whether the right Base fare is returned */
@Test
public void testGetBaseFare() {
assertEquals("getBaseFare() failed", AbstractVehicle.getBaseFare(),
new BigDecimal(10));
}
/** test whether the rental status is set */
@Test
public void testSetMyRentalStatus() {
myVehicle.setMyRentalStatus(true);
assertEquals(myVehicle.getMyRentalStatus(), true);
myVehicle.setMyRentalStatus(false);
assertEquals(myVehicle.getMyRentalStatus(), false);
}
/***/
private static class TestAbstractVehicle extends AbstractVehicle {
/**
* @param theName
* @param theVIN
* @param theRentalStatus
*/
public TestAbstractVehicle(final String theName, final String theVIN,
final boolean theRentalStatus) {
super(theName, theVIN);
}
@Override
public BigDecimal calculateRentalAmount() {
return new BigDecimal(42);
}
@Override
public String toString() {
return "test toString() value";
}
}
}