From 11e7ad21b05d51608e09285ec0b1efe717970fa3 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Mon, 23 Feb 2009 23:44:49 +0000 Subject: [PATCH] Added tests --- .../util/DefaultMultiValueMap.java | 29 +++++++ .../util/DefaultMultiValueMapTests.java | 83 +++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 org.springframework.core/src/test/java/org/springframework/util/DefaultMultiValueMapTests.java diff --git a/org.springframework.core/src/main/java/org/springframework/util/DefaultMultiValueMap.java b/org.springframework.core/src/main/java/org/springframework/util/DefaultMultiValueMap.java index ef82902e272..cb9c014e024 100644 --- a/org.springframework.core/src/main/java/org/springframework/util/DefaultMultiValueMap.java +++ b/org.springframework.core/src/main/java/org/springframework/util/DefaultMultiValueMap.java @@ -17,6 +17,7 @@ package org.springframework.util; import java.util.Collection; +import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -32,6 +33,18 @@ public class DefaultMultiValueMap implements MultiValueMap { private final Map> wrappee; + /** + * Constructs a new intance of the {@code DefaultMultiValueMap} wrapping a plain {@link LinkedHashMap}. + */ + public DefaultMultiValueMap() { + this(new LinkedHashMap>()); + } + + /** + * Constructs a new intance of the {@code DefaultMultiValueMap} wrapping the given map. + * + * @param wrappee the map to be wrapped + */ public DefaultMultiValueMap(Map> wrappee) { Assert.notNull(wrappee, "'wrappee' must not be null"); this.wrappee = wrappee; @@ -112,4 +125,20 @@ public class DefaultMultiValueMap implements MultiValueMap { public Set>> entrySet() { return wrappee.entrySet(); } + + @Override + public int hashCode() { + return wrappee.hashCode(); + } + + @Override + public boolean equals(Object obj) { + return this.wrappee.equals(obj); + } + + @Override + public String toString() { + return wrappee.toString(); + } + } diff --git a/org.springframework.core/src/test/java/org/springframework/util/DefaultMultiValueMapTests.java b/org.springframework.core/src/test/java/org/springframework/util/DefaultMultiValueMapTests.java new file mode 100644 index 00000000000..2888f5dc998 --- /dev/null +++ b/org.springframework.core/src/test/java/org/springframework/util/DefaultMultiValueMapTests.java @@ -0,0 +1,83 @@ +/* + * Copyright 2002-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.util; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; + +/** + * @author Arjen Poutsma + */ +public class DefaultMultiValueMapTests { + + private DefaultMultiValueMap map; + + @Before + public void setUp() { + map = new DefaultMultiValueMap(); + } + + @Test + public void add() { + map.add("key", "value1"); + map.add("key", "value2"); + assertEquals(1, map.size()); + List expected = new ArrayList(2); + expected.add("value1"); + expected.add("value2"); + assertEquals(expected, map.get("key")); + } + + @Test + public void getFirst() { + List values = new ArrayList(2); + values.add("value1"); + values.add("value2"); + map.put("key", values); + assertEquals("value1", map.getFirst("key")); + assertNull(map.getFirst("other")); + } + + @Test + public void set() { + map.set("key", "value1"); + map.set("key", "value2"); + assertEquals(1, map.size()); + assertEquals(Collections.singletonList("value2"), map.get("key")); + } + + @Test + public void equals() { + map.set("key1", "value1"); + assertEquals(map, map); + MultiValueMap o1 = new DefaultMultiValueMap(); + o1.set("key1", "value1"); + assertEquals(map, o1); + assertEquals(o1, map); + Map> o2 = new HashMap>(); + o2.put("key1", Collections.singletonList("value1")); + assertEquals(map, o2); + assertEquals(o2, map); + } +}