Skip to content

Instantly share code, notes, and snippets.

@nbuesing
Created May 27, 2014 05:10
Show Gist options
  • Save nbuesing/619bdc9edfe699889673 to your computer and use it in GitHub Desktop.
Save nbuesing/619bdc9edfe699889673 to your computer and use it in GitHub Desktop.
package com.objectpartners.buesing.util;
import org.junit.Assert;
import org.junit.Test;
import javax.naming.OperationNotSupportedException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import static com.objectpartners.buesing.util.MapUtils.entry;
/**
* @author Neil Buesing
*/
public class MapUtilsTest {
@Test
public void testAsMap() {
Map<String, Integer> map = MapUtils.asMap(entry("A", 1), entry("B", 2));
Assert.assertEquals(2, map.size());
Assert.assertEquals(Integer.valueOf(1), map.get("A"));
Assert.assertEquals(Integer.valueOf(2), map.get("B"));
}
@Test
public void testAsOrderedMap() {
Map<String, Integer> map = MapUtils.asOrderedMap(
entry("Z", 1), entry("Y", 9), entry("X", 2), entry("W", 7), entry("C", 3), entry("B", 6), entry("A", 4)
);
Assert.assertEquals(7, map.size());
Assert.assertEquals(Arrays.asList("Z", "Y", "X", "W", "C", "B", "A"), new ArrayList<String>(map.keySet()));
Assert.assertEquals(Arrays.asList(1, 9, 2, 7, 3, 6, 4), new ArrayList<Integer>(map.values()));
}
@Test
public void testAsUnmodifiableMap() {
Map<String, Integer> map = MapUtils.asUnmodifiableMap(entry("A", 1), entry("B", 2));
Assert.assertEquals(2, map.size());
Assert.assertEquals(Integer.valueOf(1), map.get("A"));
Assert.assertEquals(Integer.valueOf(2), map.get("B"));
try {
map.put("AA", 33);
Assert.fail("failed to throw exception");
} catch (UnsupportedOperationException e) {
// expected
}
}
@Test
public void testAsUnmodifiableOrderedMap() {
Map<String, Integer> map = MapUtils.asUnmodifiableOrderedMap(
entry("Z", 1), entry("Y", 9), entry("X", 2), entry("W", 7), entry("C", 3), entry("B", 6), entry("A", 4)
);
Assert.assertEquals(7, map.size());
Assert.assertEquals(Arrays.asList("Z", "Y", "X", "W", "C", "B", "A"), new ArrayList<String>(map.keySet()));
Assert.assertEquals(Arrays.asList(1, 9, 2, 7, 3, 6, 4), new ArrayList<Integer>(map.values()));
try {
map.put("AA", 33);
Assert.fail("failed to throw exception");
} catch (UnsupportedOperationException e) {
// expected
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment