Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f462076

Browse filesBrowse files
committed
Added m05_02_testCommandConstantFields
1 parent f638c02 commit f462076
Copy full SHA for f462076

1 file changed

+46-3Lines changed: 46 additions & 3 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/test/java/com/h2/Module05_Test.java‎

Copy file name to clipboardExpand all lines: src/test/java/com/h2/Module05_Test.java
+46-3Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,67 @@
33
import org.junit.jupiter.api.Test;
44
import org.junit.platform.commons.function.Try;
55

6+
import java.lang.reflect.Field;
67
import java.util.Optional;
8+
import java.util.Set;
79

810
import static org.junit.jupiter.api.Assertions.assertEquals;
911
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.*;
1113

1214
public class Module05_Test {
1315
private final String classToFind = "Finance";
1416

15-
public Optional<Class<?>> getAppClass() {
17+
public Optional<Class<?>> getFinanceClass() {
1618
Try<Class<?>> aClass = tryToLoadClass(classToFind);
1719
return aClass.toOptional();
1820
}
1921

2022
@Test
2123
public void m5_01_assertFinanceClassExistence() {
22-
final Optional<Class<?>> maybeClass = getAppClass();
24+
final Optional<Class<?>> maybeClass = getFinanceClass();
2325
assertTrue(maybeClass.isPresent(), classToFind + " should be present");
2426
assertEquals(classToFind, maybeClass.get().getCanonicalName());
2527
}
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+
2669
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.