}
public static Map<String, Object> toMap(String... array) {
+ if (array.length%2==1) {
+ throw new IllegalArgumentException("Not an even number of arguments");
+ }
Map<String, Object> map = new LinkedHashMap<String, Object>();
for (int i = 0; i < array.length; i += 2) {
Object value = array[i + 1];
import java.util.Date;
import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
import static org.fest.assertions.MapAssert.entry;
public class EncodingUtilsTest {
assertThat(EncodingUtils.toMap("1", "one", "2", "two")).hasSize(2).includes(entry("1", "one"), entry("2", "two"));
assertThat(EncodingUtils.toMap("foo", null)).isEmpty();
}
+
+ @Test
+ public void toMap_should_fail_if_odd_arguments() {
+ try {
+ EncodingUtils.toMap("foo");
+ fail();
+ } catch (IllegalArgumentException e) {
+ assertThat(e).hasMessage("Not an even number of arguments");
+ }
+ }
}