package org.dstadler.commons.collections;

import static org.junit.Assert.assertEquals;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import org.dstadler.commons.testing.PrivateConstructorCoverage;

/**
 *
 * @author dominik.stadler
 */
public class MapUtilsTest {

	@Test
	public void testSortingByValue() {
		HashMap<String, Integer> map = new HashMap<>();
		map.put("One", 1);
		map.put("Two", 2);
		map.put("Three", 3);

		List<Map.Entry<String, Integer>> sorted = MapUtils.sortByValue(map);
		assertEquals("First", "One", sorted.get(0).getKey());
		assertEquals("Second", "Two", sorted.get(1).getKey());
		assertEquals("Third", "Three", sorted.get(2).getKey());
	}

	@Test
	public void testSortingByValueAndKey() {
		HashMap<String, Integer> map = new HashMap<>();
		map.put("One", 1);
		map.put("Two", 2);
		map.put("Three", 3);

		List<Map.Entry<String, Integer>> sorted = MapUtils.sortByValueAndKey(map);
		assertEquals("First", "One", sorted.get(0).getKey());
		assertEquals("Second", "Two", sorted.get(1).getKey());
		assertEquals("Third", "Three", sorted.get(2).getKey());

		map.put("Threa", 3);
		map.put("Threb", 3);
		map.put("Threc", 3);
		map.put("Thred", 3);
		map.put("Thref", 3);

		sorted = MapUtils.sortByValueAndKey(map);
		assertEquals("First", "One", sorted.get(0).getKey());
		assertEquals("Second", "Two", sorted.get(1).getKey());
		assertEquals("Third", "Threa", sorted.get(2).getKey());
		assertEquals("Third", "Threb", sorted.get(3).getKey());
		assertEquals("Third", "Threc", sorted.get(4).getKey());
		assertEquals("Third", "Thred", sorted.get(5).getKey());
		assertEquals("Third", "Three", sorted.get(6).getKey());
		assertEquals("Third", "Thref", sorted.get(7).getKey());
	}

	// helper method to get coverage of the unused constructor
	 @Test
	 public void testPrivateConstructor() throws Exception {
	 	PrivateConstructorCoverage.executePrivateConstructor(MapUtils.class);
	 }
}

@@ -49,44 +49,87 @@
 ort 
-static org.junit.Assert.assertEquals
+org.dstadler.commons.testing.PrivateConstructorCoverage;%0D%0Aimport org.junit.Test
 ;%0D%0A%0D
@@ -204,32 +204,39 @@
 .Map;%0D%0A%0D%0Aimport 
+static 
 org.junit.Test;%0D
@@ -233,79 +233,27 @@
 nit.
-Test;%0D%0A%0D%0Aimport org.dstadler.commons.testing.PrivateConstructorCoverage
+Assert.assertEquals
 ;%0D%0A%0D
@@ -740,24 +740,496 @@
 ());%0D%0A%09%7D%0D%0A%0D%0A
+%09@Test%0D%0A%09public void testSortingByValueEqualValue() %7B%0D%0A%09%09HashMap%3CString, Integer%3E map = new HashMap%3C%3E();%0D%0A%09%09map.put(%22One%22, 3);%0D%0A%09%09map.put(%22Two%22, 2);%0D%0A%09%09map.put(%22Three%22, 2);%0D%0A%09%09map.put(%22Four%22, 1);%0D%0A%0D%0A%09%09List%3CMap.Entry%3CString, Integer%3E%3E sorted = MapUtils.sortByValue(map);%0D%0A%09%09assertEquals(%22Four%22, sorted.get(0).getKey());%0D%0A%09%09assertEquals(%22Two%22, sorted.get(1).getKey());%0D%0A%09%09assertEquals(%22Three%22, sorted.get(2).getKey());%0D%0A%09%09assertEquals(%22One%22, sorted.get(3).getKey());%0D%0A%09%7D%0D%0A%0D%0A
 %09@Test%0D%0A%09pub

Verify what happens in SortByValue with equal values