This commit is contained in:
Looly
2023-04-22 02:36:32 +08:00
parent 7387150f1a
commit 16e05bf9f5
108 changed files with 1244 additions and 857 deletions

View File

@@ -649,7 +649,7 @@ public class BeanUtilTest {
final Station station2 = new Station();
BeanUtil.copyProperties(station, station2);
Assertions.assertEquals(new Long(123456L), station2.getId());
Assertions.assertEquals(Long.valueOf(123456L), station2.getId());
}
static class Station extends Tree<Long> {}

View File

@@ -1,11 +1,11 @@
package org.dromara.hutool.core.collection;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.dromara.hutool.core.date.StopWatch;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.lang.page.PageInfo;
import org.dromara.hutool.core.util.RandomUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@@ -262,4 +262,11 @@ public class ListUtilTest {
final List<Integer> reverse = ListUtil.reverseNew(view);
Assertions.assertEquals("[3, 2, 1]", reverse.toString());
}
@Test
void reverseNewTest2() {
final List<Integer> list = ListUtil.of(1, 2, 3);
ListUtil.reverseNew(list);
}
}

View File

@@ -1,6 +1,6 @@
package org.dromara.hutool.core.lang;
import org.dromara.hutool.core.exceptions.UtilException;
import org.dromara.hutool.core.exceptions.HutoolException;
import org.dromara.hutool.core.thread.ThreadUtil;
import lombok.Data;
import org.junit.jupiter.api.Assertions;
@@ -23,7 +23,7 @@ public class SingletonTest {
public TestBean(){
if(null != testSingleton){
throw new UtilException("单例测试中,对象被创建了两次!");
throw new HutoolException("单例测试中,对象被创建了两次!");
}
testSingleton = this;
}

View File

@@ -1,7 +1,7 @@
package org.dromara.hutool.core.lang;
import org.dromara.hutool.core.collection.ConcurrentHashSet;
import org.dromara.hutool.core.exceptions.UtilException;
import org.dromara.hutool.core.exceptions.HutoolException;
import org.dromara.hutool.core.lang.id.IdUtil;
import org.dromara.hutool.core.lang.id.Snowflake;
import org.dromara.hutool.core.text.StrUtil;
@@ -62,7 +62,7 @@ public class SnowflakeTest {
ThreadUtil.concurrencyTest(100, () -> {
for (int i = 0; i < 50000; i++) {
if(!ids.add(snowflake.nextId())){
throw new UtilException("重复ID");
throw new HutoolException("重复ID");
}
}
});
@@ -99,7 +99,7 @@ public class SnowflakeTest {
ThreadUtil.concurrencyTest(100, () -> {
for (int i = 0; i < 50000; i++) {
if(!ids.add(snowflake.nextId())){
throw new UtilException("重复ID");
throw new HutoolException("重复ID");
}
}
});

View File

@@ -12,6 +12,7 @@ import org.dromara.hutool.core.reflect.lookup.LookupUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import java.lang.invoke.*;
import java.lang.reflect.Constructor;
@@ -264,19 +265,19 @@ public class LambdaFactoryTest {
@SneakyThrows
private void loop(final int count, final Task... tasks) {
Arrays.stream(tasks)
.peek(task -> {
final LambdaFactoryTest.SupplierThrowable runnable = task.getRunnable();
long cost = System.nanoTime();
for (int i = 0; i < count; i++) {
runnable.get();
}
cost = System.nanoTime() - cost;
task.setCost(cost);
task.setCount(count);
})
.sorted(Comparator.comparing(Task::getCost))
.map(Task::format)
.forEach(System.out::println);
.peek(task -> {
final LambdaFactoryTest.SupplierThrowable runnable = task.getRunnable();
long cost = System.nanoTime();
for (int i = 0; i < count; i++) {
runnable.get();
}
cost = System.nanoTime() - cost;
task.setCost(cost);
task.setCount(count);
})
.sorted(Comparator.comparing(Task::getCost))
.map(Task::format)
.forEach(System.out::println);
System.out.println("--------------------------------------------");
}
@@ -318,9 +319,11 @@ public class LambdaFactoryTest {
@SuppressWarnings("unchecked")
@Test
@EnabledForJreRange(max = org.junit.jupiter.api.condition.JRE.JAVA_8)
public void buildStringTest() {
final char[] a = "1234".toCharArray();
// JDK8下无此构造方法
final Constructor<String> constructor = ConstructorUtil.getConstructor(String.class, char[].class, boolean.class);
final BiFunction<char[], Boolean, String> function = LambdaFactory.build(BiFunction.class, constructor);
final String apply = function.apply(a, true);

View File

@@ -2,21 +2,35 @@ package org.dromara.hutool.core.map;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import java.util.concurrent.ConcurrentHashMap;
public class Issue2349Test {
@Test
public void computeIfAbsentTest(){
@EnabledForJreRange(max = org.junit.jupiter.api.condition.JRE.JAVA_8)
public void computeIfAbsentTest() {
// https://blog.csdn.net/xiaochao_bos/article/details/103789991
// 使用ConcurrentHashMap会造成死循环
// SafeConcurrentHashMap用于修复此问题
final ConcurrentHashMap<String,Integer> map=new SafeConcurrentHashMap<>(16);
map.computeIfAbsent("AaAa", key->map.computeIfAbsent("BBBB",key2->42));
final ConcurrentHashMap<String, Integer> map = new SafeConcurrentHashMap<>(16);
map.computeIfAbsent("AaAa", key -> map.computeIfAbsent("BBBB", key2 -> 42));
Assertions.assertEquals(2, map.size());
Assertions.assertEquals(Integer.valueOf(42), map.get("AaAa"));
Assertions.assertEquals(Integer.valueOf(42), map.get("BBBB"));
}
@Test
@EnabledForJreRange(min = org.junit.jupiter.api.condition.JRE.JAVA_9)
public void issue11986ForJava17Test() {
// https://github.com/apache/dubbo/issues/11986
final ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
// JDK9+ has been resolved JDK-8161372 bug, when cause dead then throw IllegalStateException
Assertions.assertThrows(IllegalStateException.class, () -> {
map.computeIfAbsent("AaAa", key -> map.computeIfAbsent("BBBB", key2 -> 42));
});
}
}

View File

@@ -272,4 +272,10 @@ public class MapUtilTest {
Assertions.assertEquals(Integer.valueOf(42), map.get("AaAa"));
Assertions.assertEquals(Integer.valueOf(42), map.get("BBBB"));
}
@Test
void createMapTest() {
final Map<Object, Object> map = MapUtil.createMap(MapUtil.view(new HashMap<>()).getClass());
Assertions.assertEquals(HashMap.class, map.getClass());
}
}

View File

@@ -1,10 +1,14 @@
package org.dromara.hutool.core.reflect;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.dromara.hutool.core.date.Week;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Map;
public class ConstructorUtilTest {
@@ -22,7 +26,7 @@ public class ConstructorUtilTest {
Assertions.assertEquals(0, intValue);
final Integer integer = ConstructorUtil.newInstanceIfPossible(Integer.class);
Assertions.assertEquals(new Integer(0), integer);
Assertions.assertEquals(Integer.valueOf(0), integer);
final Map<?, ?> map = ConstructorUtil.newInstanceIfPossible(Map.class);
Assertions.assertNotNull(map);
@@ -36,4 +40,32 @@ public class ConstructorUtilTest {
final int[] intArray = ConstructorUtil.newInstanceIfPossible(int[].class);
Assertions.assertArrayEquals(new int[0], intArray);
}
@Test
void newInstanceTest() {
final TestBean testBean = ConstructorUtil.newInstance(TestBean.class);
Assertions.assertNull(testBean.getA());
Assertions.assertEquals(0, testBean.getB());
}
@Test
void newInstanceAllArgsTest() {
final TestBean testBean = ConstructorUtil.newInstance(TestBean.class, "aValue", 1);
Assertions.assertEquals("aValue", testBean.getA());
Assertions.assertEquals(1, testBean.getB());
}
@Test
void newInstanceHashtableTest() {
final Hashtable<?, ?> testBean = ConstructorUtil.newInstance(Hashtable.class);
Assertions.assertNotNull(testBean);
}
@Data
@NoArgsConstructor
@AllArgsConstructor
private static class TestBean{
private String a;
private int b;
}
}

View File

@@ -14,13 +14,13 @@ public class MethodHandleUtilTest {
final Duck duck = (Duck) Proxy.newProxyInstance(
ClassLoaderUtil.getClassLoader(),
new Class[] { Duck.class },
MethodHandleUtil::invokeSpecial);
MethodHandleUtil::invoke);
Assertions.assertEquals("Quack", duck.quack());
// 测试子类执行default方法
final Method quackMethod = MethodUtil.getMethod(Duck.class, "quack");
String quack = MethodHandleUtil.invokeSpecial(new BigDuck(), quackMethod);
String quack = MethodHandleUtil.invoke(new BigDuck(), quackMethod);
Assertions.assertEquals("Quack", quack);
// 测试反射执行默认方法
@@ -51,7 +51,7 @@ public class MethodHandleUtilTest {
@Test
public void invokeTest(){
// 测试执行普通方法
final int size = MethodHandleUtil.invokeSpecial(new BigDuck(),
final int size = MethodHandleUtil.invoke(new BigDuck(),
MethodUtil.getMethod(BigDuck.class, "getSize"));
Assertions.assertEquals(36, size);
}

View File

@@ -1,12 +1,12 @@
package org.dromara.hutool.core.reflect;
import lombok.Data;
import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.date.StopWatch;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.lang.test.bean.ExamInfoDict;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.SystemUtil;
import lombok.Data;
import org.dromara.hutool.core.util.JdkUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@@ -14,7 +14,7 @@ import org.junit.jupiter.api.Test;
import java.lang.reflect.Method;
public class MethodUtilTest {
private static final String JAVA_VERSION = SystemUtil.get("java.version", false);
private static final boolean isGteJdk15 = getJavaVersion() >= 15;
/**
* jdk版本是否>= jdk15
@@ -23,10 +23,7 @@ public class MethodUtilTest {
* @author dazer
*/
private static int getJavaVersion() {
if (JAVA_VERSION.startsWith("1.")) {
return Integer.parseInt(JAVA_VERSION.split("\\.")[1]);
}
return Integer.parseInt(JAVA_VERSION.split("\\.")[0]);
return JdkUtil.JVM_VERSION;
}
@Test
@@ -244,6 +241,6 @@ public class MethodUtilTest {
final TestClass testClass = new TestClass();
final Method method = MethodUtil.getMethod(TestClass.class, "setA", int.class);
Assertions.assertThrows(IllegalArgumentException.class,
() -> MethodUtil.invoke(testClass, method, "NaN"));
() -> MethodUtil.invoke(testClass, method, "aaa"));
}
}

View File

@@ -12,15 +12,24 @@
package org.dromara.hutool.core.reflect.lookup;
import org.dromara.hutool.core.reflect.MethodUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
public class LookupUtilTest {
class LookupUtilTest {
@Test
public void findMethodTest() throws Throwable {
void lookupTest() {
final MethodHandles.Lookup lookup = LookupUtil.lookup();
Assertions.assertNotNull(lookup);
}
@Test
void findMethodTest() throws Throwable {
MethodHandle handle = LookupUtil.findMethod(Duck.class, "quack",
MethodType.methodType(String.class));
Assertions.assertNotNull(handle);
@@ -37,7 +46,7 @@ public class LookupUtilTest {
}
@Test
public void findStaticMethodTest() throws Throwable {
void findStaticMethodTest() throws Throwable {
final MethodHandle handle = LookupUtil.findMethod(Duck.class, "getDuck",
MethodType.methodType(String.class, int.class));
Assertions.assertNotNull(handle);
@@ -48,7 +57,7 @@ public class LookupUtilTest {
}
@Test
public void findPrivateMethodTest() throws Throwable {
void findPrivateMethodTest() throws Throwable {
final MethodHandle handle = LookupUtil.findMethod(BigDuck.class, "getPrivateValue",
MethodType.methodType(String.class));
Assertions.assertNotNull(handle);
@@ -58,7 +67,7 @@ public class LookupUtilTest {
}
@Test
public void findSuperMethodTest() throws Throwable {
void findSuperMethodTest() throws Throwable {
// 查找父类的方法
final MethodHandle handle = LookupUtil.findMethod(BigDuck.class, "quack",
MethodType.methodType(String.class));
@@ -69,7 +78,7 @@ public class LookupUtilTest {
}
@Test
public void findPrivateStaticMethodTest() throws Throwable {
void findPrivateStaticMethodTest() throws Throwable {
final MethodHandle handle = LookupUtil.findMethod(BigDuck.class, "getPrivateStaticValue",
MethodType.methodType(String.class));
Assertions.assertNotNull(handle);
@@ -78,6 +87,51 @@ public class LookupUtilTest {
Assertions.assertEquals("private static value", invoke);
}
@Test
void unreflectTest() throws Throwable {
final MethodHandle handle = LookupUtil.unreflect(
MethodUtil.getMethodByName(BigDuck.class, "getSize"));
final int invoke = (int) handle.invoke(new BigDuck());
Assertions.assertEquals(36, invoke);
}
@Test
void unreflectPrivateTest() throws Throwable {
final MethodHandle handle = LookupUtil.unreflect(
MethodUtil.getMethodByName(BigDuck.class, "getPrivateValue"));
final String invoke = (String) handle.invoke(new BigDuck());
Assertions.assertEquals("private value", invoke);
}
@Test
void unreflectPrivateStaticTest() throws Throwable {
final MethodHandle handle = LookupUtil.unreflect(
MethodUtil.getMethodByName(BigDuck.class, "getPrivateStaticValue"));
final String invoke = (String) handle.invoke();
Assertions.assertEquals("private static value", invoke);
}
@Test
void unreflectDefaultTest() throws Throwable {
final MethodHandle handle = LookupUtil.unreflect(
MethodUtil.getMethodByName(BigDuck.class, "quack"));
final String invoke = (String) handle.invoke(new BigDuck());
Assertions.assertEquals("Quack", invoke);
}
@Test
void unreflectStaticInInterfaceTest() throws Throwable {
final MethodHandle handle = LookupUtil.unreflect(
MethodUtil.getMethodByName(BigDuck.class, "getDuck"));
final String invoke = (String) handle.invoke(1);
Assertions.assertEquals("Duck 1", invoke);
}
interface Duck {
default String quack() {
return "Quack";

View File

@@ -12,7 +12,7 @@
package org.dromara.hutool.core.spi;
import org.dromara.hutool.core.exceptions.UtilException;
import org.dromara.hutool.core.exceptions.HutoolException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -36,7 +36,7 @@ public class ListServiceLoaderTest {
@Test
void getServiceClassNotExistTest() {
final ListServiceLoader<TestSPI1> serviceLoader = ListServiceLoader.of(TestSPI1.class);
Assertions.assertThrows(UtilException.class, ()->{
Assertions.assertThrows(HutoolException.class, ()->{
serviceLoader.getServiceClass(0);
});
}

View File

@@ -12,7 +12,7 @@
package org.dromara.hutool.core.spi;
import org.dromara.hutool.core.exceptions.UtilException;
import org.dromara.hutool.core.exceptions.HutoolException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -36,7 +36,7 @@ public class MapServiceLoaderTest {
@Test
void getServiceClassNotExistTest() {
final MapServiceLoader<TestSPI1> serviceLoader = MapServiceLoader.of(TestSPI1.class);
Assertions.assertThrows(UtilException.class, ()->{
Assertions.assertThrows(HutoolException.class, ()->{
serviceLoader.getServiceClass("service2");
});
}

View File

@@ -5,7 +5,7 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.exceptions.UtilException;
import org.dromara.hutool.core.exceptions.HutoolException;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.text.placeholder.StrTemplate;
import org.dromara.hutool.core.text.placeholder.template.NamedPlaceholderStrTemplate;
@@ -592,7 +592,7 @@ public class StrTemplateTest {
.removeFeatures(StrTemplate.Feature.FORMAT_MISSING_KEY_PRINT_WHOLE_PLACEHOLDER)
.build();
Assertions.assertEquals("this is aaa for 666", template2.format(MapUtil.builder("tableName", "aaa").put("id", "666").build()));
Assertions.assertThrows(UtilException.class, () -> template2.format(MapUtil.builder("tableName", "aaa").build()));
Assertions.assertThrows(HutoolException.class, () -> template2.format(MapUtil.builder("tableName", "aaa").build()));
// ##### 空字符串策略 #####
template = StrTemplate.ofNamed(commonTemplate)

View File

@@ -1,6 +1,6 @@
package org.dromara.hutool.core.util;
import org.dromara.hutool.core.exceptions.CloneRuntimeException;
import org.dromara.hutool.core.exceptions.CloneException;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.junit.jupiter.api.Assertions;
@@ -46,7 +46,7 @@ public class CloneTest {
try {
return (Cat) super.clone();
} catch (final CloneNotSupportedException e) {
throw new CloneRuntimeException(e);
throw new CloneException(e);
}
}
}
@@ -67,7 +67,7 @@ public class CloneTest {
try {
return (Dog) super.clone();
} catch (final CloneNotSupportedException e) {
throw new CloneRuntimeException(e);
throw new CloneException(e);
}
}
}

View File

@@ -1,7 +1,7 @@
package org.dromara.hutool.core.util;
import org.dromara.hutool.core.exceptions.CloneRuntimeException;
import org.dromara.hutool.core.exceptions.CloneException;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.junit.jupiter.api.Assertions;
@@ -41,7 +41,7 @@ public class DefaultCloneTest {
try {
return (Car) super.clone();
} catch (final CloneNotSupportedException e) {
throw new CloneRuntimeException(e);
throw new CloneException(e);
}
}
}

View File

@@ -3,7 +3,7 @@ package org.dromara.hutool.core.util;
import org.dromara.hutool.core.collection.ConcurrentHashSet;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.date.StopWatch;
import org.dromara.hutool.core.exceptions.UtilException;
import org.dromara.hutool.core.exceptions.HutoolException;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.lang.id.IdUtil;
import org.dromara.hutool.core.lang.id.Snowflake;
@@ -104,7 +104,7 @@ public class IdUtilTest {
try {
latch.await();
} catch (final InterruptedException e) {
throw new UtilException(e);
throw new HutoolException(e);
}
Assertions.assertEquals(threadCount * idCountPerThread, set.size());
}
@@ -134,7 +134,7 @@ public class IdUtilTest {
try {
latch.await();
} catch (final InterruptedException e) {
throw new UtilException(e);
throw new HutoolException(e);
}
Assertions.assertEquals(threadCount * idCountPerThread, set.size());
}

View File

@@ -438,6 +438,13 @@ public class NumberUtilTest {
}
@Test
public void parseIntOfNaNTest() {
// https://stackoverflow.com/questions/5876369/why-does-casting-double-nan-to-int-not-throw-an-exception-in-java
final int v1 = NumberUtil.parseInt("NaN");
Assertions.assertEquals(0, v1);
}
@Test
public void parseNumberTest() {
// from 5.4.8 issue#I23ORQ@Gitee
@@ -488,6 +495,13 @@ public class NumberUtilTest {
Assertions.assertEquals(255, v1);
}
@Test
public void parseNumberOfNaNTest() {
// https://stackoverflow.com/questions/5876369/why-does-casting-double-nan-to-int-not-throw-an-exception-in-java
final Number v1 = NumberUtil.parseNumber("NaN");
Assertions.assertEquals(0, v1.intValue());
}
@Test
public void parseLongTest() {
long number = NumberUtil.parseLong("0xFF");

View File

@@ -9,10 +9,7 @@ import org.junit.jupiter.api.Test;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
import java.util.function.Function;
/**
@@ -137,7 +134,12 @@ public class ObjUtilTest {
Assertions.assertNotNull(result4);
}
@Test
void cloneListTest() {
final ArrayList<Integer> list = ListUtil.of(1, 2);
final ArrayList<Integer> clone = ObjUtil.clone(list);
Assertions.assertEquals(list, clone);
}
@Test
public void cloneTest() {