|
3 | 3 | import org.junit.jupiter.api.Test; |
4 | 4 | import org.junit.platform.commons.function.Try; |
5 | 5 |
|
| 6 | +import java.lang.reflect.Field; |
6 | 7 | import java.util.Optional; |
| 8 | +import java.util.Set; |
7 | 9 |
|
8 | 10 | import static org.junit.jupiter.api.Assertions.assertEquals; |
9 | 11 | import static org.junit.jupiter.api.Assertions.assertTrue; |
10 | | -import static org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass; |
| 12 | +import static org.junit.platform.commons.util.ReflectionUtils.*; |
11 | 13 |
|
12 | 14 | public class Module05_Test { |
13 | 15 | private final String classToFind = "Finance"; |
14 | 16 |
|
15 | | - public Optional<Class<?>> getAppClass() { |
| 17 | + public Optional<Class<?>> getFinanceClass() { |
16 | 18 | Try<Class<?>> aClass = tryToLoadClass(classToFind); |
17 | 19 | return aClass.toOptional(); |
18 | 20 | } |
19 | 21 |
|
20 | 22 | @Test |
21 | 23 | public void m5_01_assertFinanceClassExistence() { |
22 | | - final Optional<Class<?>> maybeClass = getAppClass(); |
| 24 | + final Optional<Class<?>> maybeClass = getFinanceClass(); |
23 | 25 | assertTrue(maybeClass.isPresent(), classToFind + " should be present"); |
24 | 26 | assertEquals(classToFind, maybeClass.get().getCanonicalName()); |
25 | 27 | } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void m05_02_testCommandConstantFields() throws IllegalAccessException { |
| 31 | + final Optional<Class<?>> maybeClass = getFinanceClass(); |
| 32 | + assertTrue(maybeClass.isPresent(), classToFind + " should be present"); |
| 33 | + assertEquals(classToFind, maybeClass.get().getCanonicalName()); |
| 34 | + |
| 35 | + final Class<?> aClass = maybeClass.get(); |
| 36 | + final Field[] fields = aClass.getDeclaredFields(); |
| 37 | + |
| 38 | + assertEquals(3, fields.length, classToFind + " should have 3 fields"); |
| 39 | + |
| 40 | + final Set<String> fieldNames = Set.of("BEST_LOAN_RATES", "SAVINGS_CALCULATOR", "MORTGAGE_CALCULATOR"); |
| 41 | + for(Field field: fields) { |
| 42 | + String fieldName = field.getName(); |
| 43 | + assertTrue(fieldNames.contains(fieldName), fieldName + " is not a valid field name. It should be among BEST_LOAN_RATES, SAVINGS_CALCULATOR, MORTGAGE_CALCULATOR"); |
| 44 | + assertTrue(isPublic(field), fieldName + " must be declared 'public'"); |
| 45 | + assertTrue(isStatic(field), fieldName + " must be declared 'static'"); |
| 46 | + assertTrue(isFinal(field), fieldName + " must be declared 'final'"); |
| 47 | + |
| 48 | + switch (fieldName) { |
| 49 | + case "BEST_LOAN_RATES": |
| 50 | + assertEquals("bestLoanRates", field.get(null), "BEST_LOAN_RATES must have a value of 'bestLoanRates'"); |
| 51 | + break; |
| 52 | + case "SAVINGS_CALCULATOR": |
| 53 | + assertEquals("savingsCalculator", field.get(null), "SAVINGS_CALCULATOR must have a value of 'savingsCalculator'"); |
| 54 | + break; |
| 55 | + case "MORTGAGE_CALCULATOR": |
| 56 | + assertEquals("mortgageCalculator", field.get(null), "MORTGAGE_CALCULATOR must have a value of 'mortgageCalculator'"); |
| 57 | + break; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + /* |
| 63 | + * 1. Existence of BEST_LOAN_RATES, SAVINGS_CALCULATOR, MORTGAGE_CALCULATOR |
| 64 | + * 2. isPublic, isStatic, isFinal |
| 65 | + * 3. Right values for each field |
| 66 | + */ |
| 67 | + } |
| 68 | + |
26 | 69 | } |
0 commit comments