|
| 1 | +package com.baeldung.commons.collections; |
| 2 | + |
| 3 | +import org.apache.commons.collections4.MapIterator; |
| 4 | +import org.apache.commons.collections4.MapUtils; |
| 5 | +import org.apache.commons.collections4.PredicateUtils; |
| 6 | +import org.apache.commons.collections4.TransformerUtils; |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import java.io.ByteArrayOutputStream; |
| 11 | +import java.io.PrintStream; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +import static org.hamcrest.Matchers.is; |
| 16 | +import static org.hamcrest.collection.IsMapContaining.hasEntry; |
| 17 | +import static org.hamcrest.collection.IsMapWithSize.aMapWithSize; |
| 18 | +import static org.hamcrest.collection.IsMapWithSize.anEmptyMap; |
| 19 | +import static org.junit.Assert.*; |
| 20 | + |
| 21 | +public class MapUtilsTest { |
| 22 | + |
| 23 | + private String[][] color2DArray = new String[][] { |
| 24 | + {"RED", "#FF0000"}, |
| 25 | + {"GREEN", "#00FF00"}, |
| 26 | + {"BLUE", "#0000FF"} |
| 27 | + }; |
| 28 | + private String[] color1DArray = new String[] { |
| 29 | + "RED", "#FF0000", |
| 30 | + "GREEN", "#00FF00", |
| 31 | + "BLUE", "#0000FF" |
| 32 | + }; |
| 33 | + private Map<String, String> colorMap; |
| 34 | + |
| 35 | + @Before |
| 36 | + public void createMap() { |
| 37 | + this.colorMap = MapUtils.putAll(new HashMap<String, String>(), this.color2DArray); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void whenCreateMapFrom2DArray_theMapIsCreated() { |
| 42 | + this.colorMap = MapUtils.putAll(new HashMap<String, String>(), this.color2DArray); |
| 43 | + |
| 44 | + assertThat(this.colorMap, is(aMapWithSize(this.color2DArray.length))); |
| 45 | + |
| 46 | + assertThat(this.colorMap, hasEntry("RED", "#FF0000")); |
| 47 | + assertThat(this.colorMap, hasEntry("GREEN", "#00FF00")); |
| 48 | + assertThat(this.colorMap, hasEntry("BLUE", "#0000FF")); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void whenCreateMapFrom1DArray_theMapIsCreated() { |
| 53 | + this.colorMap = MapUtils.putAll(new HashMap<String, String>(), this.color1DArray); |
| 54 | + |
| 55 | + assertThat(this.colorMap, is(aMapWithSize(this.color1DArray.length / 2))); |
| 56 | + |
| 57 | + assertThat(this.colorMap, hasEntry("RED", "#FF0000")); |
| 58 | + assertThat(this.colorMap, hasEntry("GREEN", "#00FF00")); |
| 59 | + assertThat(this.colorMap, hasEntry("BLUE", "#0000FF")); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void whenVerbosePrintMap_thenMustPrintFormattedMap() { |
| 64 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 65 | + PrintStream outPrint = new PrintStream(out); |
| 66 | + |
| 67 | + outPrint.println("Optional Label = "); |
| 68 | + outPrint.println("{"); |
| 69 | + outPrint.println(" RED = #FF0000"); |
| 70 | + outPrint.println(" BLUE = #0000FF"); |
| 71 | + outPrint.println(" GREEN = #00FF00"); |
| 72 | + outPrint.println("}"); |
| 73 | + |
| 74 | + String expectedOut = out.toString(); |
| 75 | + |
| 76 | + out.reset(); |
| 77 | + |
| 78 | + MapUtils.verbosePrint(outPrint, "Optional Label", this.colorMap); |
| 79 | + assertEquals(expectedOut, out.toString()); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void whenGetKeyNotPresent_thenMustReturnDefaultValue() { |
| 84 | + String defaultColorStr = "COLOR_NOT_FOUND"; |
| 85 | + String color = MapUtils.getString(this.colorMap, "BLACK", defaultColorStr); |
| 86 | + |
| 87 | + assertEquals(color, defaultColorStr); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void whenGetOnNullMap_thenMustReturnDefaultValue() { |
| 92 | + String defaultColorStr = "COLOR_NOT_FOUND"; |
| 93 | + String color = MapUtils.getString(null, "RED", defaultColorStr); |
| 94 | + |
| 95 | + assertEquals(color, defaultColorStr); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void whenInvertMap_thenMustReturnInvertedMap() { |
| 100 | + Map<String, String> invColorMap = MapUtils.invertMap(this.colorMap); |
| 101 | + assertEquals(this.colorMap.size(), invColorMap.size()); |
| 102 | + |
| 103 | + MapIterator<String, String> itColorMap |
| 104 | + = MapUtils.iterableMap(this.colorMap).mapIterator(); |
| 105 | + |
| 106 | + while (itColorMap.hasNext()) { |
| 107 | + String colorMapKey = itColorMap.next(); |
| 108 | + String colorMapValue = itColorMap.getValue(); |
| 109 | + |
| 110 | + String invColorMapValue = MapUtils.getString(invColorMap, colorMapValue); |
| 111 | + |
| 112 | + assertTrue(invColorMapValue.equals(colorMapKey)); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Test(expected = IllegalArgumentException.class) |
| 117 | + public void whenCreateFixedSizedMapAndAdd_thenMustThrowException() { |
| 118 | + Map<String, String> rgbMap = MapUtils.fixedSizeMap(MapUtils.putAll( |
| 119 | + new HashMap<String, String>(), |
| 120 | + this.color1DArray)); |
| 121 | + |
| 122 | + rgbMap.put("ORANGE", "#FFA500"); |
| 123 | + } |
| 124 | + |
| 125 | + @Test(expected = IllegalArgumentException.class) |
| 126 | + public void whenAddDuplicateToUniqueValuesPredicateMap_thenMustThrowException() { |
| 127 | + Map<String, String> uniqValuesMap |
| 128 | + = MapUtils.predicatedMap(this.colorMap, null, PredicateUtils.uniquePredicate()); |
| 129 | + |
| 130 | + uniqValuesMap.put("NEW_RED", "#FF0000"); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void whenCreateLazyMap_theMapIsCreated() { |
| 135 | + Map<Integer, String> intStrMap = MapUtils.lazyMap( |
| 136 | + new HashMap<Integer, String>(), |
| 137 | + TransformerUtils.stringValueTransformer()); |
| 138 | + |
| 139 | + assertThat(intStrMap, is(anEmptyMap())); |
| 140 | + |
| 141 | + intStrMap.get(1); |
| 142 | + intStrMap.get(2); |
| 143 | + intStrMap.get(3); |
| 144 | + |
| 145 | + assertThat(intStrMap, is(aMapWithSize(3))); |
| 146 | + } |
| 147 | +} |
0 commit comments