/
FlightTest.java
294 lines (237 loc) · 12.4 KB
/
FlightTest.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import org.junit.Test;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import static org.junit.Assert.*;
public class FlightTest {
@Test
public void instanceCountTest(){
Flight testFlight = new Flight();
@SuppressWarnings("rawtypes")
Class c = testFlight.getClass();
try {
assertEquals(
"You must only have the instance variables specified. When looking at the number of instance variables we",
5, c.getDeclaredFields().length);
}
catch (Exception e) {
fail("Something weird went wrong");
}
}
@Test
public void instanceVariablesTest(){
Flight testFlight = new Flight();
instanceVariablePrivate("year",testFlight);
instanceVariablePrivate("startCity",testFlight);
instanceVariablePrivate("endCity",testFlight);
instanceVariablePrivate("price",testFlight);
instanceVariablePrivate("distance",testFlight);
instanceVariableStatic("year",testFlight);
instanceVariableStatic("startCity",testFlight);
instanceVariableStatic("endCity",testFlight);
instanceVariableStatic("price",testFlight);
instanceVariableStatic("distance",testFlight);
instanceVariableCorrectType("year",String.class,testFlight);
instanceVariableCorrectType("startCity", String.class,testFlight);
instanceVariableCorrectType("endCity",String.class,testFlight);
instanceVariableCorrectType("price",double.class,testFlight);
instanceVariableCorrectType("distance",int.class,testFlight);
}
@Test
public void defaultConstructorTest() {
Flight testFlight = new Flight();
constructorsPublic(testFlight);
testVariable("year",testFlight,"1970","When checking the value of year we");
testVariable("startCity",testFlight,"Boston","When checking the value of startCity we");
testVariable("endCity",testFlight,"Richmond","When checking the value of endCity we");
testVariable("price",testFlight,40.0,"When checking the value of price we");
testVariable("distance",testFlight,900,"When checking the value of year we");
}
@Test
public void parameterizedConstructorTest() {
Flight testFlight = new Flight("2017","Boston, MA (Metropolitan Area)","Richmond, VA",132.355884,395);
constructorsPublic(testFlight);
testVariable("year",testFlight,"2017","When checking the value of year we");
testVariable("startCity",testFlight,"Boston, MA (Metropolitan Area)","When checking the value of startCity we");
testVariable("endCity",testFlight,"Richmond, VA","When checking the value of endCity we");
testVariable("price",testFlight,132.355884,"When checking the value of price we");
testVariable("distance",testFlight,395,"When checking the value of year we");
}
@Test
public void getYearTest() {
Flight testFlight = createFlight("2017","Boston, MA (Metropolitan Area)","Richmond, VA",132.355884,395);
assertEquals("With an Flight object who's year instance variable is 2017, when calling getYear we","2017",testFlight.getYear());
}
@Test
public void setYearTest() {
Flight testFlight = createFlight("2017","Boston, MA (Metropolitan Area)","Richmond, VA",132.355884,395);
testFlight.setYear("2020");
testVariable("year",testFlight,"2020","After calling Flight's setYear method with an argument of 2020, for the value of year we");
}
@Test
public void getStartCityTest() {
Flight testFlight = createFlight("2017","Boston, MA (Metropolitan Area)","Richmond, VA",132.355884,395);
assertEquals("With an Flight object who's startCity instance variable is Boston, MA (Metropolitan Area), when calling getStartCity we","Boston, MA (Metropolitan Area)",testFlight.getStartCity());
}
@Test
public void setStartCityTest() {
Flight testFlight = createFlight("2017","Boston, MA (Metropolitan Area)","Richmond, VA",132.355884,395);
testFlight.setStartCity("Albuquerque, NM");
testVariable("startCity",testFlight,"Albuquerque, NM","After calling Flight's setStartCity method with an argument of Albuquerque, NM, for the value of startCity we");
}
@Test
public void getEndCityTest() {
Flight testFlight = createFlight("2017","Boston, MA (Metropolitan Area)","Richmond, VA",132.355884,395);
assertEquals("With an Flight object who's endCity instance variable is Richmond, VA, when calling getEndCity we","Richmond, VA",testFlight.getEndCity());
}
@Test
public void setEndCityTest() {
Flight testFlight = createFlight("2017","Boston, MA (Metropolitan Area)","Richmond, VA",132.355884,395);
testFlight.setEndCity("Albuquerque, NM");
testVariable("endCity",testFlight,"Albuquerque, NM","After calling Flight's setEndCity method with an argument of Albuquerque, NM, for the value of endCity we");
}
@Test
public void getPriceTest() {
Flight testFlight = createFlight("2017","Boston, MA (Metropolitan Area)","Richmond, VA",132.355884,395);
assertEquals("With an Flight object who's price instance variable is 132.355884, when calling getPrice we",132.355884,testFlight.getPrice(),.1);
}
@Test
public void setPriceTest() {
Flight testFlight = createFlight("2017","Boston, MA (Metropolitan Area)","Richmond, VA",132.355884,395);
testFlight.setPrice(172.388734);
testVariable("price",testFlight,172.388734,"After calling Flight's setPrice method with an argument of 172.388734, for the value of price we");
}
@Test
public void getDistanceTest() {
Flight testFlight = createFlight("2017","Boston, MA (Metropolitan Area)","Richmond, VA",132.355884,395);
assertEquals("With an Flight object who's distance instance variable is NONE, when calling getDistance we",395,testFlight.getDistance());
}
@Test
public void setDistanceTest() {
Flight testFlight = createFlight("2017","Boston, MA (Metropolitan Area)","Richmond, VA",132.355884,395);
testFlight.setDistance(600);
testVariable("distance",testFlight,600,"After calling Flight's setDistance method with an argument of WHIPPED_CREAM_AND_CHERRY, for the value of distance we");
}
@Test
public void toStringTest() {
Flight testFlight = createFlight("2017","Boston, MA (Metropolitan Area)","Richmond, VA",132.355884,395);
assertEquals( "2017 Boston, MA (Metropolitan Area) Richmond, VA 132.355884 395", testFlight.toString());
testFlight = createFlight("2016","Boston, MA (Metropolitan Area)","Buffalo, NY",139.2092115,474);
assertEquals( "2016 Boston, MA (Metropolitan Area) Buffalo, NY 139.2092115 474", testFlight.toString());
}
@Test
public void equalsTest() {
Flight testFlight = createFlight("2017","Boston, MA (Metropolitan Area)","Richmond, VA",132.355884,395);
Flight anotherTestFlight = createFlight("2016","Boston, MA (Metropolitan Area)","Buffalo, NY",139.2092115,474);
Flight thirdTestFlight = createFlight("2016","Boston, MA (Metropolitan Area)","Buffalo, NY",139.2092115,474);
String notFlight = "Hello I should return false!";
assertTrue( "When using .equals to compare a Flight with itself we", testFlight.equals(testFlight));
assertFalse( "When using .equals to compare a Flight with a logically different Flight we", testFlight.equals(anotherTestFlight));
assertTrue( "When using .equals to compare a Flight with a logically equivalent Flight we", anotherTestFlight.equals(thirdTestFlight));
assertTrue( "When using .equals to compare a Flight with a logically equivalent Flight (reflexive test) we", thirdTestFlight.equals(anotherTestFlight));
assertFalse( "When using .equals to compare a Flight with a String we", testFlight.equals(notFlight));
}
private Flight createFlight(String aYear, String aStartCity, String anEndCity, double aPrice, int aDistance){
Flight testFlight = new Flight();
@SuppressWarnings("rawtypes")
Class c = testFlight.getClass();
try {
Field year = c.getDeclaredField("year");
year.setAccessible(true);
year.set(testFlight, aYear);
Field startCity = c.getDeclaredField("startCity");
startCity.setAccessible(true);
startCity.set(testFlight, aStartCity);
Field endCity = c.getDeclaredField("endCity");
endCity.setAccessible(true);
endCity.set(testFlight, anEndCity);
Field price = c.getDeclaredField("price");
price.setAccessible(true);
price.set(testFlight, aPrice);
Field distance = c.getDeclaredField("distance");
distance.setAccessible(true);
distance.set(testFlight, aDistance);
} catch (Exception e) {
fail(e.toString());
}
return testFlight;
}
private void instanceVariablePrivate(String aField, Object testObject) {
@SuppressWarnings("rawtypes")
Class c = testObject.getClass();
try {
c.getDeclaredField(aField);
assertTrue("You must make your instance variables private.", Modifier.isPrivate(c.getDeclaredField(aField).getModifiers()));
} catch (NoSuchFieldException e) {
fail("Could not find the " + e.getLocalizedMessage() + " instance variable");
} catch (Exception e) {
fail("Something weird went wrong");
}
}
private void constructorsPublic(Object testObject) {
@SuppressWarnings("rawtypes")
Class c = testObject.getClass();
try {
for(Constructor aCon : c.getDeclaredConstructors()){
assertTrue("You must make your constructors public.", Modifier.isPublic(aCon.getModifiers()));
}
}catch (Exception e) {
fail("Something weird went wrong");
}
}
private void instanceVariableStatic(String aField, Object testObject) {
@SuppressWarnings("rawtypes")
Class c = testObject.getClass();
try {
c.getDeclaredField(aField);
assertEquals("Your instance variables must NOT be static.", false,
Modifier.isStatic(c.getDeclaredField(aField).getModifiers()));
} catch (NoSuchFieldException e) {
fail("Could not find the " + e.getLocalizedMessage() + " instance variable");
} catch (Exception e) {
fail("Something weird went wrong");
}
}
private void instanceVariableCorrectType(String aField, Class<?> aClass, Object testObject) {
@SuppressWarnings("rawtypes")
Class c = testObject.getClass();
try {
c.getDeclaredField(aField);
assertEquals("You must make the speed instance variable of type"+ aClass.toString() +".", aClass, c.getDeclaredField(aField).getType());
} catch (NoSuchFieldException e) {
fail("Could not find the " + e.getLocalizedMessage() + " instance variable");
} catch (Exception e) {
fail("Something weird went wrong");
}
}
private void testVariable(String aField, Object testObject, Object expected, String message){
@SuppressWarnings("rawtypes")
Class c = testObject.getClass();
try {
Field field = c.getDeclaredField(aField);
field.setAccessible(true);
Object fieldValue = field.get(testObject);
if(expected == null){
assertNull(message,fieldValue);
}
//If class is a double we have a special Junit assert to run
else if(expected.getClass().equals(Double.class)){
double doubleFieldValue = (double) fieldValue;
double doubleExpected = (double) expected;
assertEquals(message, doubleExpected, doubleFieldValue, .01);
}
//Array of some kind yay
else if(expected.getClass().isArray()){
}
else if(expected.getClass().equals(ArrayList.class)){
}
else{
assertEquals(message, expected, fieldValue);
}
}
catch (Exception e) {
fail(e.toString());
}
}
}