forked from snychka/java-first-program
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule3_Test.java
More file actions
368 lines (276 loc) · 18.5 KB
/
Copy pathModule3_Test.java
File metadata and controls
368 lines (276 loc) · 18.5 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package com.h2;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.function.Try;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.lang.reflect.*;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.platform.commons.util.ReflectionUtils.*;
public class Module3_Test {
private final String classToFind = "com.h2.SavingsCalculator";
private final InputStream systemIn = System.in;
private final PrintStream systemOut = System.out;
private ByteArrayInputStream testIn;
private ByteArrayOutputStream testOut;
@BeforeEach
public void setUpOutput() {
testOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(testOut));
}
private void provideInput(String data) {
testIn = new ByteArrayInputStream(data.getBytes());
System.setIn(testIn);
}
private String getOutput() {
return testOut.toString();
}
@AfterEach
public void restoreSystemInputOutput() {
System.setIn(systemIn);
System.setOut(systemOut);
}
private static Optional<Class<?>> getClass(final String className) {
Try<Class<?>> aClass = tryToLoadClass(className);
return aClass.toOptional();
}
public Optional<Class<?>> getSavingsClass() {
return getClass(classToFind);
}
@Test
public void m03_01_testSavingsCalculatorExists() {
Optional<Class<?>> maybeClass = getSavingsClass();
assertTrue(maybeClass.isPresent(), classToFind + " must be created.");
}
@Test
public void m3_02_testExistenceOfPrivateFields() {
final Optional<Class<?>> maybeSavingsCalculator = getSavingsClass();
assertTrue(maybeSavingsCalculator.isPresent(), classToFind + " must exist");
final Class<?> savingsCalculator = maybeSavingsCalculator.get();
final Set<String> fieldNames = new HashSet<>(Arrays.asList("credits", "debits"));
final Field[] declaredFields = savingsCalculator.getDeclaredFields();
assertEquals(2, declaredFields.length, "2 fields (credits and debits) should be available in " + classToFind);
for (final Field field : declaredFields) {
assertTrue(isPrivate(field), field.getName() + " should be declared private");
assertEquals(float[].class, field.getType(), field.getName() + " should be of type 'float[]'");
assertTrue(fieldNames.contains(field.getName()), field.getName() + " is not a valid name. It should be either 'credits' or 'debits'");
}
}
@Test
public void m3_03_testConstructor() {
final Optional<Class<?>> maybeSavingsCalculator = getSavingsClass();
assertTrue(maybeSavingsCalculator.isPresent(), classToFind + " must exist");
final Class<?> savingsCalculator = maybeSavingsCalculator.get();
final Constructor<?>[] constructors = savingsCalculator.getDeclaredConstructors();
assertEquals(1, constructors.length, classToFind + " should have 1 constructor");
final Constructor<?> constructor = constructors[0];
assertTrue(isPublic(constructor), "constructor must be declared 'public'");
assertEquals(2, constructor.getParameterCount(), "Constructor should have 2 parameters");
for (final Parameter parameter : constructor.getParameters()) {
assertEquals(float[].class, parameter.getType(), "Constructor parameter should be of type 'float[]'");
}
}
@Test
public void m3_04_testFieldsValueSetWhenConstructorCalled() throws IllegalAccessException, InvocationTargetException, InstantiationException {
final Optional<Class<?>> maybeSavingsCalculator = getSavingsClass();
assertTrue(maybeSavingsCalculator.isPresent(), classToFind + " must exist");
final Class<?> savingsCalculator = maybeSavingsCalculator.get();
final Constructor<?>[] constructors = savingsCalculator.getDeclaredConstructors();
assertEquals(1, constructors.length, classToFind + " should have 1 constructor");
float[] credits = new float[]{10.0f, 20.0f};
float[] debits = new float[]{5.0f};
final Constructor<?> constructor = constructors[0];
int parameterCount = constructor.getParameterCount();
assertEquals(2, parameterCount, classToFind + " must have a constructor with 2 parameters - credits and debits, both should be of type 'float[]'");
Object instance = constructor.newInstance(credits, debits);
final Field[] fields = savingsCalculator.getDeclaredFields();
for (final Field field : fields) {
field.setAccessible(true);
float[] fieldValues = (float[]) field.get(instance);
if (field.getName().equals("credits")) {
assertArrayEquals(credits, fieldValues, "credits parameter should set the value in class field name 'credits'");
} else if (field.getName().equals("debits")) {
assertArrayEquals(debits, fieldValues, "debits parameter should set the value in class field name 'debits'");
}
}
}
@Test
public void m3_05_testSumOfCreditsExists() {
final String methodName = "sumOfCredits";
final Optional<Class<?>> maybeSavingsCalculator = getSavingsClass();
assertTrue(maybeSavingsCalculator.isPresent(), classToFind + " must exist");
final Class<?> savingsCalculator = maybeSavingsCalculator.get();
final Method[] methods = savingsCalculator.getDeclaredMethods();
final List<Method> filteredMethod = Arrays.stream(methods).filter(method -> method.getName().equals(methodName)).collect(Collectors.toList());
assertEquals(1, filteredMethod.size(), classToFind + " should contain a method called '" + methodName + "'");
final Method sumOfCredits = filteredMethod.get(0);
assertTrue(isPrivate(sumOfCredits), methodName + " must be declared as 'private'");
assertEquals(float.class, sumOfCredits.getReturnType(), methodName + " method must return a value of type 'float'");
}
@Test
public void m3_06_testSumOfCreditsWorksCorrectly() throws IllegalAccessException, InvocationTargetException, InstantiationException {
final Optional<Class<?>> maybeSavingsCalculator = getSavingsClass();
assertTrue(maybeSavingsCalculator.isPresent(), classToFind + " must exist");
final Class<?> savingsCalculator = maybeSavingsCalculator.get();
final Constructor<?>[] constructors = savingsCalculator.getDeclaredConstructors();
assertEquals(1, constructors.length, classToFind + " should have 1 constructor");
float[] credits = new float[]{10.0f, 20.0f};
float[] debits = new float[]{5.0f};
final Constructor<?> constructor = constructors[0];
int parameterCount = constructor.getParameterCount();
assertEquals(2, parameterCount, classToFind + " must have a constructor with 2 parameters - credits and debits, both should be of type 'float[]'");
Object instance = constructor.newInstance(credits, debits);
final String methodName = "sumOfCredits";
final Method[] methods = savingsCalculator.getDeclaredMethods();
final List<Method> filteredMethod = Arrays.stream(methods).filter(method -> method.getName().equals(methodName)).collect(Collectors.toList());
assertEquals(1, filteredMethod.size(), classToFind + " should contain a method called '" + methodName + "'");
Method method = filteredMethod.get(0);
final float result = (float) invokeMethod(method, instance);
assertEquals(30.0f, result, methodName + " is not returning sum of credits.");
}
@Test
public void m3_07_testSumOfDebitsExists() {
final String methodName = "sumOfDebits";
final Optional<Class<?>> maybeSavingsCalculator = getSavingsClass();
assertTrue(maybeSavingsCalculator.isPresent(), classToFind + " must exist");
final Class<?> savingsCalculator = maybeSavingsCalculator.get();
final Method[] methods = savingsCalculator.getDeclaredMethods();
final List<Method> filteredMethod = Arrays.stream(methods).filter(method -> method.getName().equals(methodName)).collect(Collectors.toList());
assertEquals(1, filteredMethod.size(), classToFind + " should contain a method called '" + methodName + "'");
final Method sumOfCredits = filteredMethod.get(0);
assertTrue(isPrivate(sumOfCredits), methodName + " must be declared as 'private'");
assertEquals(float.class, sumOfCredits.getReturnType(), methodName + " method must return a value of type 'float'");
}
@Test
public void m3_08_testSumOfDebitsWorksCorrectly() throws IllegalAccessException, InvocationTargetException, InstantiationException {
final Optional<Class<?>> maybeSavingsCalculator = getSavingsClass();
assertTrue(maybeSavingsCalculator.isPresent(), classToFind + " must exist");
final Class<?> savingsCalculator = maybeSavingsCalculator.get();
final Constructor<?>[] constructors = savingsCalculator.getDeclaredConstructors();
assertEquals(1, constructors.length, classToFind + " should have 1 constructor");
float[] credits = new float[]{10.0f, 20.0f};
float[] debits = new float[]{5.0f, 10.f};
final Constructor<?> constructor = constructors[0];
int parameterCount = constructor.getParameterCount();
assertEquals(2, parameterCount, classToFind + " must have a constructor with 2 parameters - credits and debits, both should be of type 'float[]'");
Object instance = constructor.newInstance(credits, debits);
final String methodName = "sumOfDebits";
final Method[] methods = savingsCalculator.getDeclaredMethods();
final List<Method> filteredMethod = Arrays.stream(methods).filter(method -> method.getName().equals(methodName)).collect(Collectors.toList());
assertEquals(1, filteredMethod.size(), classToFind + " should contain a method called '" + methodName + "'");
Method method = filteredMethod.get(0);
final float result = (float) invokeMethod(method, instance);
assertEquals(15.0f, result, methodName + " is not returning sum of debits.");
}
@Test
public void m3_09_testRemainingDaysInMonthExists() {
final String methodName = "remainingDaysInMonth";
final Optional<Class<?>> maybeSavingsCalculator = getSavingsClass();
assertTrue(maybeSavingsCalculator.isPresent(), classToFind + " must exist");
final Class<?> savingsCalculator = maybeSavingsCalculator.get();
final Method[] methods = savingsCalculator.getDeclaredMethods();
final List<Method> filteredMethod = Arrays.stream(methods).filter(method -> method.getName().equals(methodName)).collect(Collectors.toList());
assertEquals(1, filteredMethod.size(), classToFind + " should contain a method called '" + methodName + "'");
final Method method = filteredMethod.get(0);
assertTrue(isPrivate(method), methodName + " must be declared as 'private'");
assertTrue(isStatic(method), methodName + " must be declared as 'static'");
final Class<?>[] parameterTypes = method.getParameterTypes();
assertEquals(1, parameterTypes.length, methodName + " must accept 1 parameter.");
assertEquals(LocalDate.class, parameterTypes[0], methodName + " must accept only 1 parameter of type 'LocalDate'");
assertEquals(int.class, method.getReturnType(), methodName + " method must return a value of type 'int'");
}
@Test
public void m3_10_testRemainingDaysInMonthWorksCorrectly() throws IllegalAccessException, InvocationTargetException, InstantiationException {
final String methodName = "remainingDaysInMonth";
final Optional<Class<?>> maybeSavingsCalculator = getSavingsClass();
assertTrue(maybeSavingsCalculator.isPresent(), classToFind + " must exist");
final Class<?> savingsCalculator = maybeSavingsCalculator.get();
final Method[] methods = savingsCalculator.getDeclaredMethods();
final List<Method> filteredMethod = Arrays.stream(methods).filter(method -> method.getName().equals(methodName)).collect(Collectors.toList());
assertEquals(1, filteredMethod.size(), classToFind + " should contain a method called '" + methodName + "'");
final Method method = filteredMethod.get(0);
final LocalDate _1feb2020 = LocalDate.of(2020, 2, 1);
assertEquals(28, invokeMethod(method, null, _1feb2020), "For Feb 1, 2020, remainingDaysInMonth should return '29'");
final LocalDate _15Mar2020 = LocalDate.of(2020, 3, 15);
assertEquals(16, invokeMethod(method, null, _15Mar2020), "For Mar 15, 2020, remainingDaysInMonth should return '16'");
final LocalDate _2Jun2020 = LocalDate.of(2020, 6, 2);
assertEquals(28, invokeMethod(method, null, _2Jun2020), "For Jun 02, 2020, remainingDaysInMonth should return '28'");
}
@Test
public void m3_11_testCalculateExists() {
final String methodName = "calculate";
final Optional<Class<?>> maybeSavingsCalculator = getSavingsClass();
assertTrue(maybeSavingsCalculator.isPresent(), classToFind + " must exist");
final Class<?> savingsCalculator = maybeSavingsCalculator.get();
final Method[] methods = savingsCalculator.getDeclaredMethods();
final List<Method> filteredMethod = Arrays.stream(methods).filter(method -> method.getName().equals(methodName)).collect(Collectors.toList());
assertEquals(1, filteredMethod.size(), classToFind + " should contain a method called '" + methodName + "'");
final Method method = filteredMethod.get(0);
assertTrue(isPublic(method), methodName + " must be declared as 'public'");
assertEquals(float.class, method.getReturnType(), methodName + " method must return a value of type 'float'");
}
@Test
public void m3_12_testCalculateWorksCorrectly() throws IllegalAccessException, InvocationTargetException, InstantiationException {
final String methodName = "calculate";
final Optional<Class<?>> maybeSavingsCalculator = getSavingsClass();
assertTrue(maybeSavingsCalculator.isPresent(), classToFind + " must exist");
final Class<?> savingsCalculator = maybeSavingsCalculator.get();
final Constructor<?>[] constructors = savingsCalculator.getDeclaredConstructors();
assertEquals(1, constructors.length, classToFind + " should have 1 constructor");
float[] credits = new float[]{10.0f, 20.0f};
float[] debits = new float[]{5.0f};
final Constructor<?> constructor = constructors[0];
int parameterCount = constructor.getParameterCount();
assertEquals(2, parameterCount, classToFind + " must have a constructor with 2 parameters - credits and debits, both should be of type 'float[]'");
Object instance = constructor.newInstance(credits, debits);
final Method[] methods = savingsCalculator.getDeclaredMethods();
final List<Method> filteredMethod = Arrays.stream(methods).filter(method -> method.getName().equals(methodName)).collect(Collectors.toList());
assertEquals(1, filteredMethod.size(), classToFind + " should contain a method called '" + methodName + "'");
final Method method = filteredMethod.get(0);
assertEquals(25.0f, invokeMethod(method, instance), "The calculate function should return '25.0' as the return value for credits=[10.0f, 20.0f], debits=[5.0f]");
}
@Test
public void m3_13_testMainExists() {
final String methodName = "main";
final Optional<Class<?>> maybeSavingsCalculator = getSavingsClass();
assertTrue(maybeSavingsCalculator.isPresent(), classToFind + " must exist");
final Class<?> savingsCalculator = maybeSavingsCalculator.get();
final Method[] methods = savingsCalculator.getDeclaredMethods();
final List<Method> filteredMethod = Arrays.stream(methods).filter(method -> method.getName().equals(methodName)).collect(Collectors.toList());
assertEquals(1, filteredMethod.size(), classToFind + " should contain a method called '" + methodName + "'");
final Method method = filteredMethod.get(0);
assertTrue(isPublic(method), methodName + " must be declared as 'public'");
assertTrue(isStatic(method), methodName + " must be declared as 'static'");
assertEquals(void.class, method.getReturnType(), methodName + " method must return a value of type 'void'");
final Class<?>[] parameterTypes = method.getParameterTypes();
assertEquals(1, parameterTypes.length, methodName + " must accept 1 parameter.");
assertEquals(String[].class, parameterTypes[0], methodName + " must accept only 1 parameter of type 'String[]'");
}
@Test
public void m3_14_testMainMethodPrintsCorrectOutput() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
final String methodName = "main";
final String remainingDaysInMonthMethodName = "remainingDaysInMonth";
final Optional<Class<?>> maybeSavingsCalculator = getSavingsClass();
assertTrue(maybeSavingsCalculator.isPresent(), classToFind + " must exist");
final Class<?> savingsCalculator = maybeSavingsCalculator.get();
final Method[] methods = savingsCalculator.getDeclaredMethods();
final List<Method> filteredMethod = Arrays.stream(methods).filter(method -> method.getName().equals(methodName)).collect(Collectors.toList());
assertEquals(1, filteredMethod.size(), classToFind + " should contain a method called '" + methodName + "'");
final List<Method> remainingDaysMethod = Arrays.stream(methods).filter(method -> method.getName().equals(remainingDaysInMonthMethodName)).collect(Collectors.toList());
assertEquals(1, remainingDaysMethod.size(), classToFind + " should contain a method called '" + remainingDaysInMonthMethodName + "'");
final LocalDate _1feb2020 = LocalDate.now();
final int remainingDays = (int) invokeMethod(remainingDaysMethod.get(0), null, _1feb2020);
final String credits = "10.0,20.0";
final String debits = "5.0,20.0";
Method method = savingsCalculator.getMethod("main", String[].class);
method.invoke(null, (Object) new String[]{credits, debits});
assertEquals("Net Savings = 5.0, remaining days in month = " + remainingDays + "\n", testOut.toString());
}
}