forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eugenp#2244 from eugenp/bael-964-map-util
bael-964-map-util
- Loading branch information
Showing
2 changed files
with
156 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
libraries/src/test/java/com/baeldung/commons/collections/MapUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package com.baeldung.commons.collections; | ||
|
||
import org.apache.commons.collections4.MapIterator; | ||
import org.apache.commons.collections4.MapUtils; | ||
import org.apache.commons.collections4.PredicateUtils; | ||
import org.apache.commons.collections4.TransformerUtils; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.collection.IsMapContaining.hasEntry; | ||
import static org.hamcrest.collection.IsMapWithSize.aMapWithSize; | ||
import static org.hamcrest.collection.IsMapWithSize.anEmptyMap; | ||
import static org.junit.Assert.*; | ||
|
||
public class MapUtilsTest { | ||
|
||
private String[][] color2DArray = new String[][] { | ||
{"RED", "#FF0000"}, | ||
{"GREEN", "#00FF00"}, | ||
{"BLUE", "#0000FF"} | ||
}; | ||
private String[] color1DArray = new String[] { | ||
"RED", "#FF0000", | ||
"GREEN", "#00FF00", | ||
"BLUE", "#0000FF" | ||
}; | ||
private Map<String, String> colorMap; | ||
|
||
@Before | ||
public void createMap() { | ||
this.colorMap = MapUtils.putAll(new HashMap<String, String>(), this.color2DArray); | ||
} | ||
|
||
@Test | ||
public void whenCreateMapFrom2DArray_theMapIsCreated() { | ||
this.colorMap = MapUtils.putAll(new HashMap<String, String>(), this.color2DArray); | ||
|
||
assertThat(this.colorMap, is(aMapWithSize(this.color2DArray.length))); | ||
|
||
assertThat(this.colorMap, hasEntry("RED", "#FF0000")); | ||
assertThat(this.colorMap, hasEntry("GREEN", "#00FF00")); | ||
assertThat(this.colorMap, hasEntry("BLUE", "#0000FF")); | ||
} | ||
|
||
@Test | ||
public void whenCreateMapFrom1DArray_theMapIsCreated() { | ||
this.colorMap = MapUtils.putAll(new HashMap<String, String>(), this.color1DArray); | ||
|
||
assertThat(this.colorMap, is(aMapWithSize(this.color1DArray.length / 2))); | ||
|
||
assertThat(this.colorMap, hasEntry("RED", "#FF0000")); | ||
assertThat(this.colorMap, hasEntry("GREEN", "#00FF00")); | ||
assertThat(this.colorMap, hasEntry("BLUE", "#0000FF")); | ||
} | ||
|
||
@Test | ||
public void whenVerbosePrintMap_thenMustPrintFormattedMap() { | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
PrintStream outPrint = new PrintStream(out); | ||
|
||
outPrint.println("Optional Label = "); | ||
outPrint.println("{"); | ||
outPrint.println(" RED = #FF0000"); | ||
outPrint.println(" BLUE = #0000FF"); | ||
outPrint.println(" GREEN = #00FF00"); | ||
outPrint.println("}"); | ||
|
||
String expectedOut = out.toString(); | ||
|
||
out.reset(); | ||
|
||
MapUtils.verbosePrint(outPrint, "Optional Label", this.colorMap); | ||
assertEquals(expectedOut, out.toString()); | ||
} | ||
|
||
@Test | ||
public void whenGetKeyNotPresent_thenMustReturnDefaultValue() { | ||
String defaultColorStr = "COLOR_NOT_FOUND"; | ||
String color = MapUtils.getString(this.colorMap, "BLACK", defaultColorStr); | ||
|
||
assertEquals(color, defaultColorStr); | ||
} | ||
|
||
@Test | ||
public void whenGetOnNullMap_thenMustReturnDefaultValue() { | ||
String defaultColorStr = "COLOR_NOT_FOUND"; | ||
String color = MapUtils.getString(null, "RED", defaultColorStr); | ||
|
||
assertEquals(color, defaultColorStr); | ||
} | ||
|
||
@Test | ||
public void whenInvertMap_thenMustReturnInvertedMap() { | ||
Map<String, String> invColorMap = MapUtils.invertMap(this.colorMap); | ||
assertEquals(this.colorMap.size(), invColorMap.size()); | ||
|
||
MapIterator<String, String> itColorMap | ||
= MapUtils.iterableMap(this.colorMap).mapIterator(); | ||
|
||
while (itColorMap.hasNext()) { | ||
String colorMapKey = itColorMap.next(); | ||
String colorMapValue = itColorMap.getValue(); | ||
|
||
String invColorMapValue = MapUtils.getString(invColorMap, colorMapValue); | ||
|
||
assertTrue(invColorMapValue.equals(colorMapKey)); | ||
} | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void whenCreateFixedSizedMapAndAdd_thenMustThrowException() { | ||
Map<String, String> rgbMap = MapUtils.fixedSizeMap(MapUtils.putAll( | ||
new HashMap<String, String>(), | ||
this.color1DArray)); | ||
|
||
rgbMap.put("ORANGE", "#FFA500"); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void whenAddDuplicateToUniqueValuesPredicateMap_thenMustThrowException() { | ||
Map<String, String> uniqValuesMap | ||
= MapUtils.predicatedMap(this.colorMap, null, PredicateUtils.uniquePredicate()); | ||
|
||
uniqValuesMap.put("NEW_RED", "#FF0000"); | ||
} | ||
|
||
@Test | ||
public void whenCreateLazyMap_theMapIsCreated() { | ||
Map<Integer, String> intStrMap = MapUtils.lazyMap( | ||
new HashMap<Integer, String>(), | ||
TransformerUtils.stringValueTransformer()); | ||
|
||
assertThat(intStrMap, is(anEmptyMap())); | ||
|
||
intStrMap.get(1); | ||
intStrMap.get(2); | ||
intStrMap.get(3); | ||
|
||
assertThat(intStrMap, is(aMapWithSize(3))); | ||
} | ||
} |