Skip to content

Commit

Permalink
Merge pull request eugenp#2244 from eugenp/bael-964-map-util
Browse files Browse the repository at this point in the history
bael-964-map-util
  • Loading branch information
slavisa-baeldung authored Jul 10, 2017
2 parents 2f3398b + c2a0fba commit ecac42f
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 1 deletion.
10 changes: 9 additions & 1 deletion libraries/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
<props>${basedir}/datanucleus.properties</props>
<log4jConfiguration>${basedir}/log4j.properties</log4jConfiguration>
<verbose>true</verbose>
<fork>false</fork> <!-- Solve windows line too long error -->
<fork>false</fork>
<!-- Solve windows line too long error -->
</configuration>
<executions>
<execution>
Expand Down Expand Up @@ -372,6 +373,12 @@
<version>${awaitility.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
<version>${org.hamcrest.java-hamcrest.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<multiverse.version>0.7.0</multiverse.version>
Expand Down Expand Up @@ -404,6 +411,7 @@
<java-lsh.version>0.10</java-lsh.version>
<pact.version>3.5.0</pact.version>
<awaitility.version>3.0.0</awaitility.version>
<org.hamcrest.java-hamcrest.version>2.0.0.0</org.hamcrest.java-hamcrest.version>
</properties>

</project>
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)));
}
}

0 comments on commit ecac42f

Please sign in to comment.