Merge branch 'v6-dev' into v6-dev-List

# Conflicts:
#	hutool-core/src/test/java/org/dromara/hutool/core/collection/CollUtilTest.java
This commit is contained in:
kongweiguang
2023-06-30 19:12:46 +08:00
43 changed files with 1263 additions and 254 deletions

View File

@@ -1090,6 +1090,26 @@ public class CollUtilTest {
}
}
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
@Data
static class Cat extends Animal {
public Cat(final String name, final Integer age) {
super(name, age);
}
}
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
@Data
static class Pig extends Animal {
public Pig(final String name, final Integer age) {
super(name, age);
}
}
@Test
public void getFirstTest() {
Assertions.assertNull(CollUtil.getFirst(null));
@@ -1162,6 +1182,29 @@ public class CollUtilTest {
Assertions.assertNull(CollUtil.max(null));
}
@Test
public void unionExtendTest() {
final List<Dog> dog = Arrays.asList(new Dog("dog1", 12), new Dog("dog2", 12));
final List<Cat> cat = Arrays.asList(new Cat("cat1", 12), new Cat("cat2", 12));
Assertions.assertEquals(CollUtil.union(dog, cat).size(), dog.size() + cat.size());
}
@Test
public void unionAllExtendTest() {
final List<Dog> dog = Arrays.asList(new Dog("dog1", 12), new Dog("dog2", 12));
final List<Cat> cat = Arrays.asList(new Cat("cat1", 12), new Cat("cat2", 12));
final List<Pig> pig = Arrays.asList(new Pig("pig1", 12), new Pig("pig2", 12));
Assertions.assertEquals(CollUtil.unionAll(dog, cat, pig).size(), dog.size() + cat.size() + pig.size());
}
@Test
public void unionDistinctExtendTest() {
final List<Dog> dog = Arrays.asList(new Dog("dog1", 12), new Dog("dog1", 12)); // same
final List<Cat> cat = Arrays.asList(new Cat("cat1", 12), new Cat("cat2", 12));
final List<Pig> pig = Arrays.asList(new Pig("pig1", 12), new Pig("pig2", 12));
Assertions.assertEquals(CollUtil.unionDistinct(dog, cat, pig).size(), 5);
}
@Test
public void flatListTest1() {

View File

@@ -1088,4 +1088,11 @@ public class DateUtilTest {
Assertions.assertEquals(71, DateUtil.age(DateUtil.parse("1952-02-13"), DateUtil.parse("2023-02-14")));
Assertions.assertEquals(0, DateUtil.age(DateUtil.parse("2023-02-14"), DateUtil.parse("2023-02-14")));
}
@Test
void issueI7H34NTest() {
final DateTime parse = DateUtil.parse("2019-10-22T09:56:03.000123Z");
Assertions.assertNotNull(parse);
Assertions.assertEquals("2019-10-22 09:56:03", parse.toString());
}
}

View File

@@ -93,4 +93,10 @@ public class PathUtilTest {
public void moveTest2(){
PathUtil.move(Paths.get("D:\\project\\test1.txt"), Paths.get("D:\\project\\test2.txt"), false);
}
@Test
public void issue3179Test() {
final String mimeType = PathUtil.getMimeType(Paths.get("xxxx.jpg"));
Assertions.assertEquals("image/jpeg", mimeType);
}
}

View File

@@ -285,4 +285,26 @@ public class MapUtilTest {
final Map<String, Object> map = MapUtil.renameKey(v1, "name", "newName");
Assertions.assertEquals("张三", map.get("newName"));
}
@Test
public void removeNullValueTest() {
final Dict v1 = Dict.of().set("id", 12).set("name", null).set("age", null);
final Map<String, Object> map = MapUtil.removeNullValue(v1);
Assertions.assertEquals(1, map.size());
}
@Test
public void issue3162Test() {
final Map<String, Object> map = new HashMap<String, Object>() {
private static final long serialVersionUID = 1L;
{
put("a", "1");
put("b", "2");
put("c", "3");
}};
final Map<String, Object> filtered = MapUtil.filter(map, "a", "b");
Assertions.assertEquals(2, filtered.size());
Assertions.assertEquals("1", filtered.get("a"));
Assertions.assertEquals("2", filtered.get("b"));
}
}

View File

@@ -229,4 +229,13 @@ public class Ipv4UtilTest {
final boolean match = ReUtil.isMatch(PatternPool.MAC_ADDRESS, macAddress);
Assertions.assertTrue(match);
}
@Test
public void matchesTest() {
final boolean matches1 = Ipv4Util.matches("127.*.*.1", "127.0.0.1");
Assertions.assertTrue(matches1);
final boolean matches2 = Ipv4Util.matches("192.168.*.1", "127.0.0.1");
Assertions.assertFalse(matches2);
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.core.reflect;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Type;
public class IssueI7CRIWTest {
@Test
void getTypeArgumentsTest() {
// 无法从继承获取泛型,则从接口获取
Type type = TypeUtil.getTypeArgument(C.class);
Assertions.assertEquals(type, String.class);
// 继承和第一个接口都非泛型接口,则从找到的第一个泛型接口获取
type = TypeUtil.getTypeArgument(D.class);
Assertions.assertEquals(type, String.class);
}
static class A{
}
static class AT<T>{
}
interface Face1<T>{
}
interface Face2{
}
static class B extends A{
}
static class C extends A implements Face1<String>{
}
static class D extends A implements Face2, Face1<String>{
}
}

View File

@@ -287,9 +287,33 @@ public class CharSequenceUtilTest {
@Test
void codeLengthTest() {
String a = "🍒🐽";
final String a = "🍒🐽";
final int i = StrUtil.codeLength(a);
Assertions.assertEquals(4, a.length());
Assertions.assertEquals(2, i);
}
@Test
public void limitByteLengthUtf8Test() {
final String str = "这是This一段中英文";
String ret = StrUtil.limitByteLengthUtf8(str, 12, true);
Assertions.assertEquals("这是Thi...", ret);
ret = StrUtil.limitByteLengthUtf8(str, 13, true);
Assertions.assertEquals("这是This...", ret);
ret = StrUtil.limitByteLengthUtf8(str, 14, true);
Assertions.assertEquals("这是This...", ret);
ret = StrUtil.limitByteLengthUtf8(str, 999, true);
Assertions.assertEquals(str, ret);
}
@Test
public void limitByteLengthTest() {
final String str = "This is English";
final String ret = StrUtil.limitByteLength(str, CharsetUtil.ISO_8859_1,10, 1, false);
Assertions.assertEquals("This is En", ret);
}
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.core.thread;
import org.dromara.hutool.core.lang.Console;
import java.util.concurrent.ExecutorService;
public class Issue3167Test {
public static void main(final String[] args) {
final ExecutorService executorService = ThreadUtil.newExecutor(2);
for (int i = 0; i < 1035; i++) {
final int finalI = i;
executorService.submit(() -> {
Console.log(Thread.currentThread().getName(), finalI);
ThreadUtil.sleep(5000);
});
}
}
}