This commit is contained in:
Looly
2020-04-02 17:50:07 +08:00
parent 1218a51509
commit 17be56a99c
9 changed files with 498 additions and 205 deletions

View File

@@ -277,6 +277,32 @@ public class CollUtilTest {
Assert.assertEquals("张三", list.get(2).getName());
}
@Test
public void fieldValueMapTest() {
List<TestBean> list = CollUtil.newArrayList(new TestBean("张三", 12, DateUtil.parse("2018-05-01")), //
new TestBean("李四", 13, DateUtil.parse("2018-03-01")), //
new TestBean("王五", 12, DateUtil.parse("2018-04-01"))//
);
final Map<String, TestBean> map = CollUtil.fieldValueMap(list, "name");
Assert.assertEquals("李四", map.get("李四").getName());
Assert.assertEquals("王五", map.get("王五").getName());
Assert.assertEquals("张三", map.get("张三").getName());
}
@Test
public void fieldValueAsMapTest() {
List<TestBean> list = CollUtil.newArrayList(new TestBean("张三", 12, DateUtil.parse("2018-05-01")), //
new TestBean("李四", 13, DateUtil.parse("2018-03-01")), //
new TestBean("王五", 14, DateUtil.parse("2018-04-01"))//
);
final Map<String, Integer> map = CollUtil.fieldValueAsMap(list, "name", "age");
Assert.assertEquals(new Integer(12), map.get("张三"));
Assert.assertEquals(new Integer(13), map.get("李四"));
Assert.assertEquals(new Integer(14), map.get("王五"));
}
public static class TestBean {
private String name;
private int age;
@@ -600,4 +626,25 @@ public class CollUtilTest {
Assert.assertEquals(3, map.get("c").intValue());
Assert.assertEquals(4, map.get("d").intValue());
}
@Test
public void toMapTest(){
Collection<String> keys = CollUtil.newArrayList("a", "b", "c", "d");
final Map<String, String> map = CollUtil.toMap(keys, new HashMap<>(), (value)->"key" + value);
Assert.assertEquals("a", map.get("keya"));
Assert.assertEquals("b", map.get("keyb"));
Assert.assertEquals("c", map.get("keyc"));
Assert.assertEquals("d", map.get("keyd"));
}
@Test
public void countMapTest() {
ArrayList<String> list = CollUtil.newArrayList("a", "b", "c", "c", "a", "b", "d");
Map<String, Integer> countMap = CollUtil.countMap(list);
Assert.assertEquals(Integer.valueOf(2), countMap.get("a"));
Assert.assertEquals(Integer.valueOf(2), countMap.get("b"));
Assert.assertEquals(Integer.valueOf(2), countMap.get("c"));
Assert.assertEquals(Integer.valueOf(1), countMap.get("d"));
}
}

View File

@@ -1,11 +1,11 @@
package cn.hutool.core.collection;
import java.util.ArrayList;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Map;
/**
* {@link IterUtil} 单元测试
* @author looly
@@ -13,17 +13,6 @@ import org.junit.Test;
*/
public class IterUtilTest {
@Test
public void countMapTest() {
ArrayList<String> list = CollUtil.newArrayList("a", "b", "c", "c", "a", "b", "d");
Map<String, Integer> countMap = IterUtil.countMap(list);
Assert.assertEquals(Integer.valueOf(2), countMap.get("a"));
Assert.assertEquals(Integer.valueOf(2), countMap.get("b"));
Assert.assertEquals(Integer.valueOf(2), countMap.get("c"));
Assert.assertEquals(Integer.valueOf(1), countMap.get("d"));
}
@Test
public void fieldValueMapTest() {
ArrayList<Car> carList = CollUtil.newArrayList(new Car("123", "大众"), new Car("345", "奔驰"), new Car("567", "路虎"));

View File

@@ -6,7 +6,9 @@ import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.net.HttpCookie;
import java.net.InetAddress;
import java.util.List;
/**
* NetUtil单元测试
@@ -57,4 +59,18 @@ public class NetUtilTest {
public void isUsableLocalPortTest(){
Assert.assertTrue(NetUtil.isUsableLocalPort(80));
}
@Test
public void parseCookiesTest(){
String cookieStr = "cookieName=\"cookieValue\";Path=\"/\";Domain=\"cookiedomain.com\"";
final List<HttpCookie> httpCookies = NetUtil.parseCookies(cookieStr);
Assert.assertEquals(1, httpCookies.size());
final HttpCookie httpCookie = httpCookies.get(0);
Assert.assertEquals(0, httpCookie.getVersion());
Assert.assertEquals("cookieName", httpCookie.getName());
Assert.assertEquals("cookieValue", httpCookie.getValue());
Assert.assertEquals("/", httpCookie.getPath());
Assert.assertEquals("cookiedomain.com", httpCookie.getDomain());
}
}