mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
Merge remote-tracking branch 'gitee/v6-dev' into feat-stream-ext
This commit is contained in:
@@ -1,29 +0,0 @@
|
||||
package cn.hutool.core.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 用于单元测试的注解类<br>
|
||||
* 注解类相关说明见:<a href="https://www.cnblogs.com/xdp-gacl/p/3622275.html">https://www.cnblogs.com/xdp-gacl/p/3622275.html</a>
|
||||
*
|
||||
* @author looly
|
||||
*/
|
||||
// Retention注解决定MyAnnotation注解的生命周期
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
// Target注解决定MyAnnotation注解可以加在哪些成分上,如加在类身上,或者属性身上,或者方法身上等成分
|
||||
@Target({ ElementType.METHOD, ElementType.TYPE })
|
||||
public @interface AnnotationForTest {
|
||||
|
||||
/**
|
||||
* 注解的默认属性值
|
||||
*
|
||||
* @return 属性值
|
||||
*/
|
||||
String value() default "";
|
||||
|
||||
@Alias("value")
|
||||
String retry() default "";
|
||||
}
|
@@ -1,49 +1,136 @@
|
||||
package cn.hutool.core.annotation;
|
||||
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.annotation.*;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* test for {@link AnnotationUtil}
|
||||
*/
|
||||
public class AnnotationUtilTest {
|
||||
|
||||
@Test
|
||||
public void getCombinationAnnotationsTest(){
|
||||
final Annotation[] annotations = AnnotationUtil.getAnnotations(ClassWithAnnotation.class, true);
|
||||
Assert.assertNotNull(annotations);
|
||||
public void testToCombination() {
|
||||
final CombinationAnnotationElement element = AnnotationUtil.toCombination(ClassForTest.class);
|
||||
Assert.assertEquals(2, element.getAnnotations().length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAnnotations() {
|
||||
Annotation[] annotations = AnnotationUtil.getAnnotations(ClassForTest.class, true);
|
||||
Assert.assertEquals(2, annotations.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCombinationAnnotationsWithClassTest(){
|
||||
final AnnotationForTest[] annotations = AnnotationUtil.getCombinationAnnotations(ClassWithAnnotation.class, AnnotationForTest.class);
|
||||
Assert.assertNotNull(annotations);
|
||||
annotations = AnnotationUtil.getAnnotations(ClassForTest.class, false);
|
||||
Assert.assertEquals(1, annotations.length);
|
||||
Assert.assertEquals("测试", annotations[0].value());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAnnotationValueTest() {
|
||||
final Object value = AnnotationUtil.getAnnotationValue(ClassWithAnnotation.class, AnnotationForTest.class);
|
||||
Assert.assertEquals("测试", value);
|
||||
|
||||
public void testGetCombinationAnnotations() {
|
||||
final MetaAnnotationForTest[] annotations = AnnotationUtil.getCombinationAnnotations(ClassForTest.class, MetaAnnotationForTest.class);
|
||||
Assert.assertEquals(1, annotations.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAnnotationSyncAlias() {
|
||||
// 直接获取
|
||||
Assert.assertEquals("", ClassWithAnnotation.class.getAnnotation(AnnotationForTest.class).retry());
|
||||
public void testAnnotations() {
|
||||
MetaAnnotationForTest[] annotations1 = AnnotationUtil.getAnnotations(ClassForTest.class, false, MetaAnnotationForTest.class);
|
||||
Assert.assertEquals(0, annotations1.length);
|
||||
annotations1 = AnnotationUtil.getAnnotations(ClassForTest.class, true, MetaAnnotationForTest.class);
|
||||
Assert.assertEquals(1, annotations1.length);
|
||||
|
||||
// 加别名适配
|
||||
final AnnotationForTest annotation = AnnotationUtil.getAnnotationAlias(ClassWithAnnotation.class, AnnotationForTest.class);
|
||||
Assert.assertEquals("测试", annotation.retry());
|
||||
Annotation[] annotations2 = AnnotationUtil.getAnnotations(
|
||||
ClassForTest.class, false, t -> ObjUtil.equals(t.annotationType(), MetaAnnotationForTest.class)
|
||||
);
|
||||
Assert.assertEquals(0, annotations2.length);
|
||||
annotations2 = AnnotationUtil.getAnnotations(
|
||||
ClassForTest.class, true, t -> ObjUtil.equals(t.annotationType(), MetaAnnotationForTest.class)
|
||||
);
|
||||
Assert.assertEquals(1, annotations2.length);
|
||||
}
|
||||
|
||||
@AnnotationForTest("测试")
|
||||
@RepeatAnnotationForTest
|
||||
static class ClassWithAnnotation{
|
||||
public void test(){
|
||||
|
||||
}
|
||||
@Test
|
||||
public void testGetAnnotation() {
|
||||
final MetaAnnotationForTest annotation = AnnotationUtil.getAnnotation(ClassForTest.class, MetaAnnotationForTest.class);
|
||||
Assert.assertNotNull(annotation);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHasAnnotation() {
|
||||
Assert.assertTrue(AnnotationUtil.hasAnnotation(ClassForTest.class, MetaAnnotationForTest.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAnnotationValue() {
|
||||
final AnnotationForTest annotation = ClassForTest.class.getAnnotation(AnnotationForTest.class);
|
||||
Assert.assertEquals(annotation.value(), AnnotationUtil.getAnnotationValue(ClassForTest.class, AnnotationForTest.class));
|
||||
Assert.assertEquals(annotation.value(), AnnotationUtil.getAnnotationValue(ClassForTest.class, AnnotationForTest.class, "value"));
|
||||
Assert.assertNull(AnnotationUtil.getAnnotationValue(ClassForTest.class, AnnotationForTest.class, "property"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAnnotationValueMap() {
|
||||
final AnnotationForTest annotation = ClassForTest.class.getAnnotation(AnnotationForTest.class);
|
||||
final Map<String, Object> valueMap = AnnotationUtil.getAnnotationValueMap(ClassForTest.class, AnnotationForTest.class);
|
||||
Assert.assertNotNull(valueMap);
|
||||
Assert.assertEquals(1, valueMap.size());
|
||||
Assert.assertEquals(annotation.value(), valueMap.get("value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetRetentionPolicy() {
|
||||
final RetentionPolicy policy = AnnotationForTest.class.getAnnotation(Retention.class).value();
|
||||
Assert.assertEquals(policy, AnnotationUtil.getRetentionPolicy(AnnotationForTest.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetTargetType() {
|
||||
final ElementType[] types = AnnotationForTest.class.getAnnotation(Target.class).value();
|
||||
Assert.assertArrayEquals(types, AnnotationUtil.getTargetType(AnnotationForTest.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsDocumented() {
|
||||
Assert.assertFalse(AnnotationUtil.isDocumented(AnnotationForTest.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsInherited() {
|
||||
Assert.assertFalse(AnnotationUtil.isInherited(AnnotationForTest.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetValue() {
|
||||
final AnnotationForTest annotation = ClassForTest.class.getAnnotation(AnnotationForTest.class);
|
||||
final String newValue = "is a new value";
|
||||
Assert.assertNotEquals(newValue, annotation.value());
|
||||
AnnotationUtil.setValue(annotation, "value", newValue);
|
||||
Assert.assertEquals(newValue, annotation.value());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAnnotationAlias() {
|
||||
final MetaAnnotationForTest annotation = AnnotationUtil.getAnnotationAlias(AnnotationForTest.class, MetaAnnotationForTest.class);
|
||||
Assert.assertEquals(annotation.value(), annotation.alias());
|
||||
Assert.assertEquals(MetaAnnotationForTest.class, annotation.annotationType());
|
||||
}
|
||||
|
||||
@Target(ElementType.TYPE_USE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
private @interface MetaAnnotationForTest{
|
||||
@Alias(value = "alias")
|
||||
String value() default "";
|
||||
String alias() default "";
|
||||
}
|
||||
|
||||
@MetaAnnotationForTest("foo")
|
||||
@Target(ElementType.TYPE_USE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
private @interface AnnotationForTest{
|
||||
String value() default "";
|
||||
}
|
||||
|
||||
@AnnotationForTest("foo")
|
||||
private static class ClassForTest{}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,60 @@
|
||||
package cn.hutool.core.annotation;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* test for {@link CombinationAnnotationElement}
|
||||
*/
|
||||
public class CombinationAnnotationElementTest {
|
||||
|
||||
@Test
|
||||
public void testOf() {
|
||||
CombinationAnnotationElement element = CombinationAnnotationElement.of(ClassForTest.class, a -> true);
|
||||
Assert.assertNotNull(element);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsAnnotationPresent() {
|
||||
CombinationAnnotationElement element = CombinationAnnotationElement.of(ClassForTest.class, a -> true);
|
||||
Assert.assertTrue(element.isAnnotationPresent(MetaAnnotationForTest.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAnnotation() {
|
||||
AnnotationForTest annotation1 = ClassForTest.class.getAnnotation(AnnotationForTest.class);
|
||||
MetaAnnotationForTest annotation2 = AnnotationForTest.class.getAnnotation(MetaAnnotationForTest.class);
|
||||
CombinationAnnotationElement element = CombinationAnnotationElement.of(ClassForTest.class, a -> true);
|
||||
Assert.assertEquals(annotation1, element.getAnnotation(AnnotationForTest.class));
|
||||
Assert.assertEquals(annotation2, element.getAnnotation(MetaAnnotationForTest.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAnnotations() {
|
||||
CombinationAnnotationElement element = CombinationAnnotationElement.of(ClassForTest.class, a -> true);
|
||||
Annotation[] annotations = element.getAnnotations();
|
||||
Assert.assertEquals(2, annotations.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDeclaredAnnotations() {
|
||||
CombinationAnnotationElement element = CombinationAnnotationElement.of(ClassForTest.class, a -> true);
|
||||
Annotation[] annotations = element.getDeclaredAnnotations();
|
||||
Assert.assertEquals(2, annotations.length);
|
||||
}
|
||||
|
||||
@Target(ElementType.TYPE_USE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
private @interface MetaAnnotationForTest{ }
|
||||
|
||||
@MetaAnnotationForTest
|
||||
@Target(ElementType.TYPE_USE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
private @interface AnnotationForTest{ }
|
||||
|
||||
@AnnotationForTest
|
||||
private static class ClassForTest{}
|
||||
|
||||
}
|
@@ -1,16 +0,0 @@
|
||||
package cn.hutool.core.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* @author hongda.li 2022-04-26 17:09
|
||||
*/
|
||||
@AnnotationForTest("repeat-annotation")
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
// Target注解决定MyAnnotation注解可以加在哪些成分上,如加在类身上,或者属性身上,或者方法身上等成分
|
||||
@Target({ ElementType.METHOD, ElementType.TYPE })
|
||||
public @interface RepeatAnnotationForTest {
|
||||
}
|
@@ -4,6 +4,7 @@ import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.DayOfWeek;
|
||||
import java.util.Calendar;
|
||||
|
||||
public class WeekTest {
|
||||
|
||||
@@ -46,6 +47,9 @@ public class WeekTest {
|
||||
Assert.assertEquals(Week.THURSDAY, Week.of(DayOfWeek.THURSDAY));
|
||||
Assert.assertEquals(Week.FRIDAY, Week.of(DayOfWeek.FRIDAY));
|
||||
Assert.assertEquals(Week.SATURDAY, Week.of(DayOfWeek.SATURDAY));
|
||||
Assert.assertEquals(Week.SATURDAY, Week.of(Calendar.SATURDAY));
|
||||
Assert.assertNull(Week.of(10));
|
||||
Assert.assertNull(Week.of(-1));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -58,4 +62,17 @@ public class WeekTest {
|
||||
Assert.assertEquals(DayOfWeek.SATURDAY, Week.SATURDAY.toJdkDayOfWeek());
|
||||
Assert.assertEquals(DayOfWeek.SUNDAY, Week.SUNDAY.toJdkDayOfWeek());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toChineseTest(){
|
||||
Assert.assertEquals("周一",Week.MONDAY.toChinese("周"));
|
||||
Assert.assertEquals("星期一",Week.MONDAY.toChinese("星期"));
|
||||
Assert.assertEquals("星期二",Week.TUESDAY.toChinese("星期"));
|
||||
Assert.assertEquals("星期三",Week.WEDNESDAY.toChinese("星期"));
|
||||
Assert.assertEquals("星期四",Week.THURSDAY.toChinese("星期"));
|
||||
Assert.assertEquals("星期五",Week.FRIDAY.toChinese("星期"));
|
||||
Assert.assertEquals("星期六",Week.SATURDAY.toChinese("星期"));
|
||||
Assert.assertEquals("星期日",Week.SUNDAY.toChinese("星期"));
|
||||
Assert.assertEquals("星期一",Week.MONDAY.toChinese());
|
||||
}
|
||||
}
|
||||
|
@@ -3,6 +3,9 @@ package cn.hutool.core.date;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
public class ZodiacTest {
|
||||
|
||||
@Test
|
||||
@@ -10,6 +13,11 @@ public class ZodiacTest {
|
||||
Assert.assertEquals("摩羯座", Zodiac.getZodiac(Month.JANUARY, 19));
|
||||
Assert.assertEquals("水瓶座", Zodiac.getZodiac(Month.JANUARY, 20));
|
||||
Assert.assertEquals("巨蟹座", Zodiac.getZodiac(6, 17));
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(2022, Calendar.JULY, 17);
|
||||
Assert.assertEquals("巨蟹座", Zodiac.getZodiac(calendar.getTime()));
|
||||
Assert.assertEquals("巨蟹座", Zodiac.getZodiac(calendar));
|
||||
Assert.assertNull(Zodiac.getZodiac((Calendar) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -17,5 +25,11 @@ public class ZodiacTest {
|
||||
Assert.assertEquals("狗", Zodiac.getChineseZodiac(1994));
|
||||
Assert.assertEquals("狗", Zodiac.getChineseZodiac(2018));
|
||||
Assert.assertEquals("猪", Zodiac.getChineseZodiac(2019));
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(2022, Calendar.JULY, 17);
|
||||
Assert.assertEquals("虎", Zodiac.getChineseZodiac(calendar.getTime()));
|
||||
Assert.assertEquals("虎", Zodiac.getChineseZodiac(calendar));
|
||||
Assert.assertNull(Zodiac.getChineseZodiac(1899));
|
||||
Assert.assertNull(Zodiac.getChineseZodiac((Calendar) null));
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,16 @@
|
||||
package cn.hutool.core.date;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.ZoneId;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class ZoneUtilTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Assert.assertEquals(ZoneId.systemDefault(), ZoneUtil.toZoneId(null));
|
||||
Assert.assertEquals(TimeZone.getDefault(), ZoneUtil.toTimeZone(null));
|
||||
}
|
||||
}
|
@@ -0,0 +1,160 @@
|
||||
package cn.hutool.core.map;
|
||||
|
||||
import cn.hutool.core.map.multi.CollectionValueMap;
|
||||
import cn.hutool.core.map.multi.MultiValueMap;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class CollectionValueMapTest {
|
||||
|
||||
@Test
|
||||
public void putTest() {
|
||||
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
|
||||
Assert.assertNull(map.put(1, Arrays.asList("a", "b")));
|
||||
Collection<String> collection = map.put(1, Arrays.asList("c", "d"));
|
||||
Assert.assertEquals(Arrays.asList("a", "b"), collection);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putAllTest() {
|
||||
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
|
||||
Map<Integer, Collection<String>> source = new HashMap<>();
|
||||
source.put(1, Arrays.asList("a", "b", "c"));
|
||||
map.putAll(source);
|
||||
Assert.assertEquals(1, map.size());
|
||||
Assert.assertEquals(Arrays.asList("a", "b", "c"), map.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putValueTest() {
|
||||
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
|
||||
Assert.assertTrue(map.putValue(1, "a"));
|
||||
Assert.assertTrue(map.putValue(1, "b"));
|
||||
Assert.assertTrue(map.putValue(1, "c"));
|
||||
Assert.assertEquals(1, map.size());
|
||||
Assert.assertEquals(Arrays.asList("a", "b", "c"), map.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putAllValueTest() {
|
||||
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
|
||||
Assert.assertTrue(map.putAllValues(1, Arrays.asList("a", "b", "c")));
|
||||
Assert.assertEquals(1, map.size());
|
||||
Assert.assertEquals(Arrays.asList("a", "b", "c"), map.get(1));
|
||||
|
||||
Map<Integer, Collection<String>> source = new HashMap<>();
|
||||
Assert.assertTrue(map.putValue(1, "e"));
|
||||
Assert.assertTrue(map.putValue(1, "f"));
|
||||
map.putAllValues(source);
|
||||
Assert.assertEquals(Arrays.asList("a", "b", "c", "e", "f"), map.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putValuesTest() {
|
||||
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
|
||||
Assert.assertTrue(map.putValues(1, "a", "b", "c"));
|
||||
Assert.assertEquals(Arrays.asList("a", "b", "c"), map.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFilterAllValues() {
|
||||
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
|
||||
Assert.assertTrue(map.putValues(1, "a", "b", "c"));
|
||||
Assert.assertTrue(map.putValues(2, "a", "b", "c"));
|
||||
|
||||
Assert.assertEquals(map, map.filterAllValues((k, v) -> StrUtil.equals(v, "a")));
|
||||
Assert.assertEquals(Collections.singletonList("a"), map.getValues(1));
|
||||
Assert.assertEquals(Collections.singletonList("a"), map.getValues(2));
|
||||
|
||||
Assert.assertEquals(map, map.filterAllValues(v -> !StrUtil.equals(v, "a")));
|
||||
Assert.assertEquals(Collections.emptyList(), map.getValues(1));
|
||||
Assert.assertEquals(Collections.emptyList(), map.getValues(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReplaceAllValues() {
|
||||
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
|
||||
Assert.assertTrue(map.putValues(1, "a", "b", "c"));
|
||||
Assert.assertTrue(map.putValues(2, "a", "b", "c"));
|
||||
|
||||
Assert.assertEquals(map, map.replaceAllValues((k, v) -> v + "2"));
|
||||
Assert.assertEquals(Arrays.asList("a2", "b2", "c2"), map.getValues(1));
|
||||
Assert.assertEquals(Arrays.asList("a2", "b2", "c2"), map.getValues(2));
|
||||
|
||||
Assert.assertEquals(map, map.replaceAllValues(v -> v + "3"));
|
||||
Assert.assertEquals(Arrays.asList("a23", "b23", "c23"), map.getValues(1));
|
||||
Assert.assertEquals(Arrays.asList("a23", "b23", "c23"), map.getValues(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeValueTest() {
|
||||
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
|
||||
map.putValues(1, "a", "b", "c");
|
||||
Assert.assertFalse(map.removeValue(1, "d"));
|
||||
Assert.assertTrue(map.removeValue(1, "c"));
|
||||
Assert.assertEquals(Arrays.asList("a", "b"), map.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeAllValuesTest() {
|
||||
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
|
||||
map.putValues(1, "a", "b", "c");
|
||||
Assert.assertFalse(map.removeAllValues(1, Arrays.asList("e", "f")));
|
||||
Assert.assertTrue(map.removeAllValues(1, Arrays.asList("b", "c")));
|
||||
Assert.assertEquals(Collections.singletonList("a"), map.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeValuesTest() {
|
||||
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
|
||||
map.putValues(1, "a", "b", "c");
|
||||
Assert.assertFalse(map.removeValues(1, "e", "f"));
|
||||
Assert.assertTrue(map.removeValues(1, "b", "c"));
|
||||
Assert.assertEquals(Collections.singletonList("a"), map.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getValuesTest() {
|
||||
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
|
||||
map.putValues(1, "a", "b", "c");
|
||||
Assert.assertEquals(Collections.emptyList(), map.getValues(2));
|
||||
Assert.assertEquals(Arrays.asList("a", "b", "c"), map.getValues(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sizeTest() {
|
||||
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
|
||||
map.putValues(1, "a", "b", "c");
|
||||
Assert.assertEquals(0, map.size(2));
|
||||
Assert.assertEquals(3, map.size(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allForEachTest() {
|
||||
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
|
||||
map.putValues(1, "a", "b", "c");
|
||||
List<Integer> keys = new ArrayList<>();
|
||||
List<String> values = new ArrayList<>();
|
||||
map.allForEach((k, v) -> {
|
||||
keys.add(k);
|
||||
values.add(v);
|
||||
});
|
||||
Assert.assertEquals(Arrays.asList(1, 1, 1), keys);
|
||||
Assert.assertEquals(Arrays.asList("a", "b", "c"), values);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allValuesTest() {
|
||||
MultiValueMap<Integer, String> map = new CollectionValueMap<>(new LinkedHashMap<>());
|
||||
map.putAllValues(1, Arrays.asList("a", "b", "c"));
|
||||
map.putAllValues(2, Arrays.asList("d", "e"));
|
||||
Assert.assertEquals(
|
||||
Arrays.asList("a", "b", "c", "d", "e"),
|
||||
map.allValues()
|
||||
);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,160 @@
|
||||
package cn.hutool.core.map;
|
||||
|
||||
import cn.hutool.core.map.multi.ListValueMap;
|
||||
import cn.hutool.core.map.multi.MultiValueMap;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class ListValueMapTest {
|
||||
|
||||
@Test
|
||||
public void putTest() {
|
||||
MultiValueMap<Integer, String> map = new ListValueMap<>();
|
||||
Assert.assertNull(map.put(1, Arrays.asList("a", "b")));
|
||||
Collection<String> collection = map.put(1, Arrays.asList("c", "d"));
|
||||
Assert.assertEquals(Arrays.asList("a", "b"), collection);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putAllTest() {
|
||||
MultiValueMap<Integer, String> map = new ListValueMap<>();
|
||||
Map<Integer, Collection<String>> source = new HashMap<>();
|
||||
source.put(1, Arrays.asList("a", "b", "c"));
|
||||
map.putAll(source);
|
||||
Assert.assertEquals(1, map.size());
|
||||
Assert.assertEquals(Arrays.asList("a", "b", "c"), map.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putValueTest() {
|
||||
MultiValueMap<Integer, String> map = new ListValueMap<>();
|
||||
Assert.assertTrue(map.putValue(1, "a"));
|
||||
Assert.assertTrue(map.putValue(1, "b"));
|
||||
Assert.assertTrue(map.putValue(1, "c"));
|
||||
Assert.assertEquals(1, map.size());
|
||||
Assert.assertEquals(Arrays.asList("a", "b", "c"), map.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putAllValueTest() {
|
||||
MultiValueMap<Integer, String> map = new ListValueMap<>();
|
||||
Assert.assertTrue(map.putAllValues(1, Arrays.asList("a", "b", "c")));
|
||||
Assert.assertEquals(1, map.size());
|
||||
Assert.assertEquals(Arrays.asList("a", "b", "c"), map.get(1));
|
||||
|
||||
Map<Integer, Collection<String>> source = new HashMap<>();
|
||||
Assert.assertTrue(map.putValue(1, "e"));
|
||||
Assert.assertTrue(map.putValue(1, "f"));
|
||||
map.putAllValues(source);
|
||||
Assert.assertEquals(Arrays.asList("a", "b", "c", "e", "f"), map.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putValuesTest() {
|
||||
MultiValueMap<Integer, String> map = new ListValueMap<>();
|
||||
Assert.assertTrue(map.putValues(1, "a", "b", "c"));
|
||||
Assert.assertEquals(Arrays.asList("a", "b", "c"), map.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeValueTest() {
|
||||
MultiValueMap<Integer, String> map = new ListValueMap<>();
|
||||
map.putValues(1, "a", "b", "c");
|
||||
Assert.assertFalse(map.removeValue(1, "d"));
|
||||
Assert.assertTrue(map.removeValue(1, "c"));
|
||||
Assert.assertEquals(Arrays.asList("a", "b"), map.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeAllValuesTest() {
|
||||
MultiValueMap<Integer, String> map = new ListValueMap<>();
|
||||
map.putValues(1, "a", "b", "c");
|
||||
Assert.assertFalse(map.removeAllValues(1, Arrays.asList("e", "f")));
|
||||
Assert.assertTrue(map.removeAllValues(1, Arrays.asList("b", "c")));
|
||||
Assert.assertEquals(Collections.singletonList("a"), map.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeValuesTest() {
|
||||
MultiValueMap<Integer, String> map = new ListValueMap<>();
|
||||
map.putValues(1, "a", "b", "c");
|
||||
Assert.assertFalse(map.removeValues(1, "e", "f"));
|
||||
Assert.assertTrue(map.removeValues(1, "b", "c"));
|
||||
Assert.assertEquals(Collections.singletonList("a"), map.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFilterAllValues() {
|
||||
MultiValueMap<Integer, String> map = new ListValueMap<>();
|
||||
Assert.assertTrue(map.putValues(1, "a", "b", "c"));
|
||||
Assert.assertTrue(map.putValues(2, "a", "b", "c"));
|
||||
|
||||
Assert.assertEquals(map, map.filterAllValues((k, v) -> StrUtil.equals(v, "a")));
|
||||
Assert.assertEquals(Collections.singletonList("a"), map.getValues(1));
|
||||
Assert.assertEquals(Collections.singletonList("a"), map.getValues(2));
|
||||
|
||||
Assert.assertEquals(map, map.filterAllValues(v -> !StrUtil.equals(v, "a")));
|
||||
Assert.assertEquals(Collections.emptyList(), map.getValues(1));
|
||||
Assert.assertEquals(Collections.emptyList(), map.getValues(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReplaceAllValues() {
|
||||
MultiValueMap<Integer, String> map = new ListValueMap<>();
|
||||
Assert.assertTrue(map.putValues(1, "a", "b", "c"));
|
||||
Assert.assertTrue(map.putValues(2, "a", "b", "c"));
|
||||
|
||||
Assert.assertEquals(map, map.replaceAllValues((k, v) -> v + "2"));
|
||||
Assert.assertEquals(Arrays.asList("a2", "b2", "c2"), map.getValues(1));
|
||||
Assert.assertEquals(Arrays.asList("a2", "b2", "c2"), map.getValues(2));
|
||||
|
||||
Assert.assertEquals(map, map.replaceAllValues(v -> v + "3"));
|
||||
Assert.assertEquals(Arrays.asList("a23", "b23", "c23"), map.getValues(1));
|
||||
Assert.assertEquals(Arrays.asList("a23", "b23", "c23"), map.getValues(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getValuesTest() {
|
||||
MultiValueMap<Integer, String> map = new ListValueMap<>();
|
||||
map.putValues(1, "a", "b", "c");
|
||||
Assert.assertEquals(Collections.emptyList(), map.getValues(2));
|
||||
Assert.assertEquals(Arrays.asList("a", "b", "c"), map.getValues(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sizeTest() {
|
||||
MultiValueMap<Integer, String> map = new ListValueMap<>();
|
||||
map.putValues(1, "a", "b", "c");
|
||||
Assert.assertEquals(0, map.size(2));
|
||||
Assert.assertEquals(3, map.size(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allForEachTest() {
|
||||
MultiValueMap<Integer, String> map = new ListValueMap<>();
|
||||
map.putValues(1, "a", "b", "c");
|
||||
List<Integer> keys = new ArrayList<>();
|
||||
List<String> values = new ArrayList<>();
|
||||
map.allForEach((k, v) -> {
|
||||
keys.add(k);
|
||||
values.add(v);
|
||||
});
|
||||
Assert.assertEquals(Arrays.asList(1, 1, 1), keys);
|
||||
Assert.assertEquals(Arrays.asList("a", "b", "c"), values);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allValuesTest() {
|
||||
MultiValueMap<Integer, String> map = new ListValueMap<>();
|
||||
map.putAllValues(1, Arrays.asList("a", "b", "c"));
|
||||
map.putAllValues(2, Arrays.asList("d", "e"));
|
||||
Assert.assertEquals(
|
||||
Arrays.asList("a", "b", "c", "d", "e"),
|
||||
map.allValues()
|
||||
);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,162 @@
|
||||
package cn.hutool.core.map;
|
||||
|
||||
import cn.hutool.core.map.multi.MultiValueMap;
|
||||
import cn.hutool.core.map.multi.SetValueMap;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class SetValueMapTest {
|
||||
|
||||
@Test
|
||||
public void putTest() {
|
||||
MultiValueMap<Integer, String> map = new SetValueMap<>();
|
||||
Assert.assertNull(map.put(1, Arrays.asList("a", "b")));
|
||||
Collection<String> collection = map.put(1, Arrays.asList("c", "d"));
|
||||
Assert.assertEquals(Arrays.asList("a", "b"), collection);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putAllTest() {
|
||||
MultiValueMap<Integer, String> map = new SetValueMap<>();
|
||||
Map<Integer, Collection<String>> source = new HashMap<>();
|
||||
source.put(1, Arrays.asList("a", "b", "c"));
|
||||
map.putAll(source);
|
||||
Assert.assertEquals(1, map.size());
|
||||
Assert.assertEquals(Arrays.asList("a", "b", "c"), map.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putValueTest() {
|
||||
MultiValueMap<Integer, String> map = new SetValueMap<>();
|
||||
Assert.assertTrue(map.putValue(1, "a"));
|
||||
Assert.assertTrue(map.putValue(1, "b"));
|
||||
Assert.assertTrue(map.putValue(1, "c"));
|
||||
Assert.assertEquals(1, map.size());
|
||||
Assert.assertEquals(new HashSet<>(Arrays.asList("a", "b", "c")), map.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putAllValueTest() {
|
||||
MultiValueMap<Integer, String> map = new SetValueMap<>();
|
||||
Assert.assertTrue(map.putAllValues(1, Arrays.asList("a", "b", "c")));
|
||||
Assert.assertEquals(1, map.size());
|
||||
Assert.assertEquals(new HashSet<>(Arrays.asList("a", "b", "c")), map.get(1));
|
||||
|
||||
Map<Integer, Collection<String>> source = new HashMap<>();
|
||||
Assert.assertTrue(map.putValue(1, "e"));
|
||||
Assert.assertFalse(map.putValue(1, "e"));
|
||||
Assert.assertTrue(map.putValue(1, "f"));
|
||||
Assert.assertFalse(map.putValue(1, "f"));
|
||||
map.putAllValues(source);
|
||||
Assert.assertEquals(new HashSet<>(Arrays.asList("a", "b", "c", "e", "f")), map.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putValuesTest() {
|
||||
MultiValueMap<Integer, String> map = new SetValueMap<>();
|
||||
Assert.assertTrue(map.putValues(1, "a", "b", "c"));
|
||||
Assert.assertEquals(new HashSet<>(Arrays.asList("a", "b", "c")), map.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeValueTest() {
|
||||
MultiValueMap<Integer, String> map = new SetValueMap<>();
|
||||
map.putValues(1, "a", "b", "c");
|
||||
Assert.assertFalse(map.removeValue(1, "d"));
|
||||
Assert.assertTrue(map.removeValue(1, "c"));
|
||||
Assert.assertEquals(new HashSet<>(Arrays.asList("a", "b")), map.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeAllValuesTest() {
|
||||
MultiValueMap<Integer, String> map = new SetValueMap<>();
|
||||
map.putValues(1, "a", "b", "c");
|
||||
Assert.assertFalse(map.removeAllValues(1, Arrays.asList("e", "f")));
|
||||
Assert.assertTrue(map.removeAllValues(1, Arrays.asList("b", "c")));
|
||||
Assert.assertEquals(Collections.singleton("a"), map.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeValuesTest() {
|
||||
MultiValueMap<Integer, String> map = new SetValueMap<>();
|
||||
map.putValues(1, "a", "b", "c");
|
||||
Assert.assertFalse(map.removeValues(1, "e", "f"));
|
||||
Assert.assertTrue(map.removeValues(1, "b", "c"));
|
||||
Assert.assertEquals(Collections.singleton("a"), map.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFilterAllValues() {
|
||||
MultiValueMap<Integer, String> map = new SetValueMap<>();
|
||||
Assert.assertTrue(map.putValues(1, "a", "b", "c"));
|
||||
Assert.assertTrue(map.putValues(2, "a", "b", "c"));
|
||||
|
||||
Assert.assertEquals(map, map.filterAllValues((k, v) -> StrUtil.equals(v, "a")));
|
||||
Assert.assertEquals(Collections.singleton("a"), map.getValues(1));
|
||||
Assert.assertEquals(Collections.singleton("a"), map.getValues(2));
|
||||
|
||||
Assert.assertEquals(map, map.filterAllValues(v -> !StrUtil.equals(v, "a")));
|
||||
Assert.assertEquals(Collections.emptySet(), map.getValues(1));
|
||||
Assert.assertEquals(Collections.emptySet(), map.getValues(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReplaceAllValues() {
|
||||
MultiValueMap<Integer, String> map = new SetValueMap<>();
|
||||
Assert.assertTrue(map.putValues(1, "a", "b", "c"));
|
||||
Assert.assertTrue(map.putValues(2, "a", "b", "c"));
|
||||
|
||||
Assert.assertEquals(map, map.replaceAllValues((k, v) -> v + "2"));
|
||||
Assert.assertEquals(new HashSet<>(Arrays.asList("a2", "b2", "c2")), map.getValues(1));
|
||||
Assert.assertEquals(new HashSet<>(Arrays.asList("a2", "b2", "c2")), map.getValues(2));
|
||||
|
||||
Assert.assertEquals(map, map.replaceAllValues(v -> v + "3"));
|
||||
Assert.assertEquals(new HashSet<>(Arrays.asList("a23", "b23", "c23")), map.getValues(1));
|
||||
Assert.assertEquals(new HashSet<>(Arrays.asList("a23", "b23", "c23")), map.getValues(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getValuesTest() {
|
||||
MultiValueMap<Integer, String> map = new SetValueMap<>();
|
||||
map.putValues(1, "a", "b", "c");
|
||||
Assert.assertEquals(Collections.emptyList(), map.getValues(2));
|
||||
Assert.assertEquals(new HashSet<>(Arrays.asList("a", "b", "c")), map.getValues(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sizeTest() {
|
||||
MultiValueMap<Integer, String> map = new SetValueMap<>();
|
||||
map.putValues(1, "a", "b", "c");
|
||||
Assert.assertEquals(0, map.size(2));
|
||||
Assert.assertEquals(3, map.size(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allForEachTest() {
|
||||
MultiValueMap<Integer, String> map = new SetValueMap<>();
|
||||
map.putValues(1, "a", "b", "c");
|
||||
List<Integer> keys = new ArrayList<>();
|
||||
List<String> values = new ArrayList<>();
|
||||
map.allForEach((k, v) -> {
|
||||
keys.add(k);
|
||||
values.add(v);
|
||||
});
|
||||
Assert.assertEquals(Arrays.asList(1, 1, 1), keys);
|
||||
Assert.assertEquals(Arrays.asList("a", "b", "c"), values);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allValuesTest() {
|
||||
MultiValueMap<Integer, String> map = new SetValueMap<>();
|
||||
map.putAllValues(1, Arrays.asList("a", "b", "c"));
|
||||
map.putAllValues(2, Arrays.asList("d", "e"));
|
||||
Assert.assertEquals(
|
||||
Arrays.asList("a", "b", "c", "d", "e"),
|
||||
map.allValues()
|
||||
);
|
||||
}
|
||||
|
||||
}
|
@@ -1,18 +1,16 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.exceptions.CloneRuntimeException;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class ObjUtilTest {
|
||||
|
||||
@@ -84,7 +82,7 @@ public class ObjUtilTest {
|
||||
public Obj clone() {
|
||||
try {
|
||||
return (Obj) super.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
} catch (final CloneNotSupportedException e) {
|
||||
throw new CloneRuntimeException(e);
|
||||
}
|
||||
}
|
||||
@@ -99,15 +97,20 @@ public class ObjUtilTest {
|
||||
|
||||
@Test
|
||||
public void defaultIfNullTest() {
|
||||
final String dateStr = "2020-10-23 15:12:30";
|
||||
final Instant result1 = ObjUtil.defaultIfNull(dateStr,
|
||||
(v) -> DateUtil.parse(v.toString(), DatePattern.NORM_DATETIME_PATTERN).toInstant(), Instant::now);
|
||||
Assert.assertNotNull(result1);
|
||||
final Object val1 = new Object();
|
||||
final Object val2 = new Object();
|
||||
|
||||
final String nullValue = null;
|
||||
final Instant result2 = ObjUtil.defaultIfNull(nullValue,
|
||||
(v) -> DateUtil.parse(v.toString(), DatePattern.NORM_DATETIME_PATTERN).toInstant(), Instant::now);
|
||||
Assert.assertNotNull(result2);
|
||||
Assert.assertSame(val1, ObjUtil.defaultIfNull(val1, () -> val2));
|
||||
Assert.assertSame(val2, ObjUtil.defaultIfNull(null, () -> val2));
|
||||
|
||||
Assert.assertSame(val1, ObjUtil.defaultIfNull(val1, val2));
|
||||
Assert.assertSame(val2, ObjUtil.defaultIfNull(null, val2));
|
||||
|
||||
Assert.assertSame(val1, ObjUtil.defaultIfNull(val1, Function.identity(), () -> val2));
|
||||
Assert.assertSame(val2, ObjUtil.defaultIfNull(null, Function.identity(), () -> val2));
|
||||
|
||||
Assert.assertSame(val1, ObjUtil.defaultIfNull(val1, Function.identity(), val2));
|
||||
Assert.assertSame(val2, ObjUtil.defaultIfNull(null, Function.identity(), val2));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -119,7 +122,7 @@ public class ObjUtilTest {
|
||||
|
||||
@Test
|
||||
public void cloneIfPossibleTest() {
|
||||
String a = "a";
|
||||
final String a = "a";
|
||||
final String a2 = ObjUtil.cloneIfPossible(a);
|
||||
Assert.assertNotSame(a, a2);
|
||||
}
|
||||
|
Reference in New Issue
Block a user