|
| 1 | +package com.baeldung.multipletypesinmap; |
| 2 | + |
| 3 | +import com.baeldung.collections.mulipletypesinmap.DynamicTypeValue; |
| 4 | +import com.baeldung.collections.mulipletypesinmap.InstantTypeValue; |
| 5 | +import com.baeldung.collections.mulipletypesinmap.IntArrayTypeValue; |
| 6 | +import com.baeldung.collections.mulipletypesinmap.IntegerTypeValue; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +import java.time.Instant; |
| 10 | +import java.time.ZoneId; |
| 11 | +import java.time.format.DateTimeFormatter; |
| 12 | +import java.util.Arrays; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | + |
| 18 | +class MultipleTypesInMapUnitTest { |
| 19 | + private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") |
| 20 | + .withZone(ZoneId.systemDefault()); |
| 21 | + |
| 22 | + private static final Integer intValue = 777; |
| 23 | + private static final int[] intArray = new int[]{2, 3, 5, 7, 11, 13}; |
| 24 | + private static final Instant instant = Instant.now(); |
| 25 | + |
| 26 | + private static final String KEY_INT = "E1 (Integer)"; |
| 27 | + private static final String KEY_INT_ARRAY = "E2 (IntArray)"; |
| 28 | + private static final String KEY_INSTANT = "E3 (Instant)"; |
| 29 | + |
| 30 | + @Test |
| 31 | + void givenThreeTypes_whenUsingRawMap_thenPrintDescription() { |
| 32 | + Map<String, Object> rawMap = new HashMap<>(); |
| 33 | + rawMap.put(KEY_INT, intValue); |
| 34 | + rawMap.put(KEY_INT_ARRAY, intArray); |
| 35 | + rawMap.put(KEY_INSTANT, instant); |
| 36 | + |
| 37 | + rawMap.forEach((k, v) -> { |
| 38 | + if (v instanceof Integer) { |
| 39 | + Integer theV = (Integer) v; |
| 40 | + String desc = String.format("The value is a %s integer: %d", theV > 0 ? "positive" : "negative", theV); |
| 41 | + System.out.println(k + " -> " + desc); |
| 42 | + assertThat(k).isEqualTo(KEY_INT); |
| 43 | + assertThat(desc).isEqualTo("The value is a positive integer: 777"); |
| 44 | + } else if (v instanceof int[]) { |
| 45 | + int[] theV = (int[]) v; |
| 46 | + String desc = String.format("The value is an array of %d integers: %s", theV.length, Arrays.toString(theV)); |
| 47 | + System.out.println(k + " -> " + desc); |
| 48 | + assertThat(k).isEqualTo(KEY_INT_ARRAY); |
| 49 | + assertThat(desc).isEqualTo("The value is an array of 6 integers: [2, 3, 5, 7, 11, 13]"); |
| 50 | + } else if (v instanceof Instant) { |
| 51 | + Instant theV = (Instant) v; |
| 52 | + String desc = String.format("The value is an instant: %s", FORMATTER.format(theV)); |
| 53 | + System.out.println(k + " -> " + desc); |
| 54 | + assertThat(k).isEqualTo(KEY_INSTANT); |
| 55 | + assertThat(desc).matches("^The value is an instant: \\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}"); |
| 56 | + } else { |
| 57 | + throw new IllegalStateException("Unknown Type found."); |
| 58 | + } |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void givenThreeTypes_whenUsingAnInterface_thenPrintDescription() { |
| 64 | + Map<String, DynamicTypeValue> theMap = new HashMap<>(); |
| 65 | + theMap.put(KEY_INT, new IntegerTypeValue(intValue)); |
| 66 | + theMap.put(KEY_INT_ARRAY, new IntArrayTypeValue(intArray)); |
| 67 | + theMap.put(KEY_INSTANT, new InstantTypeValue(instant)); |
| 68 | + |
| 69 | + theMap.forEach((k, v) -> System.out.println(k + " -> " + v.valueDescription())); |
| 70 | + |
| 71 | + assertThat(theMap.get(KEY_INT).valueDescription()).isEqualTo("The value is a positive integer: 777"); |
| 72 | + assertThat(theMap.get(KEY_INT_ARRAY).valueDescription()).isEqualTo("The value is an array of 6 integers: [2, 3, 5, 7, 11, 13]"); |
| 73 | + assertThat(theMap.get(KEY_INSTANT).valueDescription()).matches("^The value is an instant: \\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}"); |
| 74 | + } |
| 75 | +} |
0 commit comments