add jacoco

This commit is contained in:
Looly
2026-01-23 13:59:12 +08:00
parent 768cf399d5
commit 11b6481bce
5 changed files with 103 additions and 99 deletions

View File

@@ -426,18 +426,17 @@ public class RandomUtil {
}
/**
* 获得指定范围内的随机数
* 获得指定范围内的随机数<br>
* 保留小数使用{@link RoundingMode#DOWN}是因为始终要保证随机数在指定范围内
*
* @param minInclude 最小数(包含)
* @param maxExclude 最大数(不包含)
* @param scale 保留小数位数
* @param roundingMode 保留小数的模式 {@link RoundingMode}
* @param minInclude 最小数(包含)
* @param maxExclude 最大数(不包含)
* @param scale 保留小数位数,使用{@link RoundingMode#DOWN}
* @return 随机数
* @since 4.0.8
*/
public static double randomDouble(final double minInclude, final double maxExclude, final int scale,
final RoundingMode roundingMode) {
return NumberUtil.round(randomDouble(minInclude, maxExclude), scale, roundingMode).doubleValue();
public static double randomDouble(final double minInclude, final double maxExclude, final int scale) {
return NumberUtil.round(randomDouble(minInclude, maxExclude), scale, RoundingMode.DOWN).doubleValue();
}
/**
@@ -452,15 +451,15 @@ public class RandomUtil {
}
/**
* 获得指定范围内的随机数
* 获得指定范围内的随机数<br>
* 保留小数使用{@link RoundingMode#DOWN}是因为始终要保证随机数在指定范围内
*
* @param scale 保留小数位数
* @param roundingMode 保留小数的模式 {@link RoundingMode}
* @param scale 保留小数位数,使用{@link RoundingMode#DOWN}
* @return 随机数
* @since 4.0.8
*/
public static double randomDouble(final int scale, final RoundingMode roundingMode) {
return NumberUtil.round(randomDouble(), scale, roundingMode).doubleValue();
public static double randomDouble(final int scale) {
return NumberUtil.round(randomDouble(), scale, RoundingMode.DOWN).doubleValue();
}
/**
@@ -476,16 +475,16 @@ public class RandomUtil {
}
/**
* 获得指定范围内的随机数
* 获得指定范围内的随机数<br>
* 保留小数使用{@link RoundingMode#DOWN}是因为始终要保证随机数在指定范围内
*
* @param limit 限制随机数的范围,不包括这个数
* @param scale 保留小数位数
* @param roundingMode 保留小数的模式 {@link RoundingMode}
* @param limit 限制随机数的范围,不包括这个数
* @param scale 保留小数位数,使用{@link RoundingMode#DOWN}
* @return 随机数
* @since 4.0.8
*/
public static double randomDouble(final double limit, final int scale, final RoundingMode roundingMode) {
return NumberUtil.round(randomDouble(limit), scale, roundingMode).doubleValue();
public static double randomDouble(final double limit, final int scale) {
return NumberUtil.round(randomDouble(limit), scale, RoundingMode.DOWN).doubleValue();
}
// endregion
@@ -712,7 +711,7 @@ public class RandomUtil {
*/
public static String randomLettersAndNumbersWithoutStr(final int length, final String elemData) {
char[] baseChars = LETTERS_NUMBERS_CHARS;
if(StrUtil.isNotBlank(elemData)){
if (StrUtil.isNotBlank(elemData)) {
baseChars = ArrayUtil.removeElements(baseChars, elemData.toCharArray());
}
return randomString(baseChars, length);
@@ -728,7 +727,7 @@ public class RandomUtil {
*/
public static String randomLettersAndNumbersLowerWithoutStr(final int length, final String elemData) {
char[] baseChars = LETTERS_NUMBERS_LOWER_CHARS;
if(StrUtil.isNotBlank(elemData)){
if (StrUtil.isNotBlank(elemData)) {
baseChars = ArrayUtil.removeElements(baseChars, elemData.toLowerCase().toCharArray());
}
return randomString(baseChars, length);
@@ -846,11 +845,11 @@ public class RandomUtil {
* 获得一个随机的字符串
*
* @param baseChars 随机字符选取的样本
* @param length 字符串的长度
* @param length 字符串的长度
* @return 随机字符串
*/
public static char[] randomChars(final char[] baseChars, final int length) {
if(ArrayUtil.isEmpty(baseChars)){
if (ArrayUtil.isEmpty(baseChars)) {
throw new IllegalArgumentException("baseChars can not be empty !");
}
Assert.isTrue(length >= 1, "Length can not less than 1 !");

View File

@@ -16,7 +16,6 @@
package cn.hutool.v7.core.reflect.method;
import lombok.Data;
import cn.hutool.v7.core.array.ArrayUtil;
import cn.hutool.v7.core.date.StopWatch;
import cn.hutool.v7.core.lang.Console;
@@ -24,89 +23,79 @@ import cn.hutool.v7.core.lang.test.bean.ExamInfoDict;
import cn.hutool.v7.core.reflect.ClassUtil;
import cn.hutool.v7.core.reflect.ReflectTestBeans;
import cn.hutool.v7.core.text.StrUtil;
import cn.hutool.v7.core.util.JdkUtil;
import org.junit.jupiter.api.Assertions;
import lombok.Data;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Method;
public class MethodUtilTest extends ReflectTestBeans {
import static org.junit.jupiter.api.Assertions.*;
private static final boolean isGteJdk15 = getJavaVersion() >= 15;
/**
* jdk版本是否>= jdk15
* jdk15: 删除了 object registerNatives
* @return 反馈jdk版本7、8、11、15、17
* @author dazer
*/
private static int getJavaVersion() {
return JdkUtil.JVM_VERSION;
}
public class MethodUtilTest extends ReflectTestBeans {
@Test
public void getMethodsTest() {
Method[] methods = MethodUtil.getMethods(ExamInfoDict.class);
Assertions.assertEquals(isGteJdk15 ? 19 : 20, methods.length);
assertTrue(methods.length >= 19 && methods.length <= 20);
//过滤器测试
methods = MethodUtil.getMethods(ExamInfoDict.class, t -> Integer.class.equals(t.getReturnType()));
Assertions.assertEquals(4, methods.length);
assertEquals(4, methods.length);
final Method method = methods[0];
Assertions.assertNotNull(method);
assertNotNull(method);
//null过滤器测试
methods = MethodUtil.getMethods(ExamInfoDict.class, null);
Assertions.assertEquals(isGteJdk15 ? 19 : 20, methods.length);
assertTrue(methods.length >= 19 && methods.length <= 20);
final Method method2 = methods[0];
Assertions.assertNotNull(method2);
assertNotNull(method2);
}
@Test
public void getMethodTest() {
Method method = MethodUtil.getMethod(ExamInfoDict.class, "getId");
Assertions.assertEquals("getId", method.getName());
Assertions.assertEquals(0, method.getParameterTypes().length);
assertEquals("getId", method.getName());
assertEquals(0, method.getParameterTypes().length);
method = MethodUtil.getMethod(ExamInfoDict.class, "getId", Integer.class);
Assertions.assertEquals("getId", method.getName());
Assertions.assertEquals(1, method.getParameterTypes().length);
assertEquals("getId", method.getName());
assertEquals(1, method.getParameterTypes().length);
}
@Test
public void getMethodIgnoreCaseTest() {
Method method = MethodUtil.getMethodIgnoreCase(ExamInfoDict.class, "getId");
Assertions.assertEquals("getId", method.getName());
Assertions.assertEquals(0, method.getParameterTypes().length);
assertEquals("getId", method.getName());
assertEquals(0, method.getParameterTypes().length);
method = MethodUtil.getMethodIgnoreCase(ExamInfoDict.class, "GetId");
Assertions.assertEquals("getId", method.getName());
Assertions.assertEquals(0, method.getParameterTypes().length);
assertEquals("getId", method.getName());
assertEquals(0, method.getParameterTypes().length);
method = MethodUtil.getMethodIgnoreCase(ExamInfoDict.class, "setanswerIs", Integer.class);
Assertions.assertEquals("setAnswerIs", method.getName());
Assertions.assertEquals(1, method.getParameterTypes().length);
assertEquals("setAnswerIs", method.getName());
assertEquals(1, method.getParameterTypes().length);
}
@Test
public void invokeTest() {
final AClass testClass = new AClass();
MethodUtil.invoke(testClass, "setA", 10);
Assertions.assertEquals(10, testClass.getA());
assertEquals(10, testClass.getA());
}
@Test
public void getDeclaredMethodsTest() {
Class<?> type = TestBenchClass.class;
Method[] methods = type.getDeclaredMethods();
Assertions.assertArrayEquals(methods, MethodUtil.getDeclaredMethods(type));
Assertions.assertSame(MethodUtil.getDeclaredMethods(type), MethodUtil.getDeclaredMethods(type));
assertArrayEquals(methods, MethodUtil.getDeclaredMethods(type));
assertSame(MethodUtil.getDeclaredMethods(type), MethodUtil.getDeclaredMethods(type));
type = Object.class;
methods = type.getDeclaredMethods();
Assertions.assertArrayEquals(methods, MethodUtil.getDeclaredMethods(type));
assertArrayEquals(methods, MethodUtil.getDeclaredMethods(type));
}
@Test
@@ -156,19 +145,26 @@ public class MethodUtilTest extends ReflectTestBeans {
public void getMethodsFromClassExtends() {
// 继承情况下,需解决方法去重问题
Method[] methods = MethodUtil.getMethods(C2.class);
Assertions.assertEquals(isGteJdk15 ? 14 : 15, methods.length);
// 在使用jacoco时会多出jacocoInit方法在此兼容
assertTrue(methods.length >= 14 && methods.length <= 15);
// 排除Object中的方法
// 3个方法包括类
methods = MethodUtil.getMethodsDirectly(C2.class, true, false);
Assertions.assertEquals(3, methods.length);
assertTrue(methods.length >= 3 && methods.length <= 4);
int index = 0;
if(methods[index].toString().contains("jacocoInit")){
// 引入jacoco后会出jacocoInit方法忽略之
index++;
}
// getA属于本类
Assertions.assertEquals("public void cn.hutool.v7.core.reflect.ReflectTestBeans$C2.getA()", methods[0].toString());
assertEquals("public void cn.hutool.v7.core.reflect.ReflectTestBeans$C2.getA()", methods[index].toString());
// getB属于父类
Assertions.assertEquals("public void cn.hutool.v7.core.reflect.ReflectTestBeans$C1.getB()", methods[1].toString());
assertEquals("public void cn.hutool.v7.core.reflect.ReflectTestBeans$C1.getB()", methods[index + 1].toString());
// getC属于接口中的默认方法
Assertions.assertEquals("public default void cn.hutool.v7.core.reflect.ReflectTestBeans$TestInterface1.getC()", methods[2].toString());
assertEquals("public default void cn.hutool.v7.core.reflect.ReflectTestBeans$TestInterface1.getC()", methods[index + 2].toString());
}
@Test
@@ -176,47 +172,47 @@ public class MethodUtilTest extends ReflectTestBeans {
// 对于接口直接调用Class.getMethods方法获取所有方法因为接口都是public方法
// 因此此处得到包括TestInterface1、TestInterface2、TestInterface3中一共4个方法
final Method[] methods = MethodUtil.getMethods(TestInterface3.class);
Assertions.assertEquals(4, methods.length);
assertEquals(4, methods.length);
// 接口里调用getMethods和getPublicMethods效果相同
final Method[] publicMethods = MethodUtil.getPublicMethods(TestInterface3.class);
Assertions.assertArrayEquals(methods, publicMethods);
assertArrayEquals(methods, publicMethods);
}
@Test
public void getPublicMethod() {
final Method superPublicMethod = MethodUtil.getPublicMethod(TestSubClass.class, false, "publicMethod");
Assertions.assertNotNull(superPublicMethod);
assertNotNull(superPublicMethod);
final Method superPrivateMethod = MethodUtil.getPublicMethod(TestSubClass.class, false, "privateMethod");
Assertions.assertNull(superPrivateMethod);
assertNull(superPrivateMethod);
final Method publicMethod = MethodUtil.getPublicMethod(TestSubClass.class, false, "publicSubMethod");
Assertions.assertNotNull(publicMethod);
assertNotNull(publicMethod);
final Method privateMethod = MethodUtil.getPublicMethod(TestSubClass.class, false, "privateSubMethod");
Assertions.assertNull(privateMethod);
assertNull(privateMethod);
}
@Test
public void getDeclaredMethod() {
final Method noMethod = MethodUtil.getMethod(TestSubClass.class, "noMethod");
Assertions.assertNull(noMethod);
assertNull(noMethod);
final Method privateMethod = MethodUtil.getMethod(TestSubClass.class, "privateMethod");
Assertions.assertNotNull(privateMethod);
assertNotNull(privateMethod);
final Method publicMethod = MethodUtil.getMethod(TestSubClass.class, "publicMethod");
Assertions.assertNotNull(publicMethod);
assertNotNull(publicMethod);
final Method publicSubMethod = MethodUtil.getMethod(TestSubClass.class, "publicSubMethod");
Assertions.assertNotNull(publicSubMethod);
assertNotNull(publicSubMethod);
final Method privateSubMethod = MethodUtil.getMethod(TestSubClass.class, "privateSubMethod");
Assertions.assertNotNull(privateSubMethod);
assertNotNull(privateSubMethod);
}
@Test
public void issue2625Test(){
// 内部类继承的情况下父类方法会被定义为桥接方法因此按照pr#1965@Github判断返回值的继承关系来代替判断桥接。
final Method getThis = MethodUtil.getMethod(A.C.class, "getThis");
Assertions.assertTrue(getThis.isBridge());
assertTrue(getThis.isBridge());
}
@SuppressWarnings("InnerClassMayBeStatic")
@@ -243,7 +239,7 @@ public class MethodUtilTest extends ReflectTestBeans {
final TestClass testClass = new TestClass();
final Method method = MethodUtil.getMethod(TestClass.class, "setA", int.class);
MethodUtil.invoke(testClass, method, 10);
Assertions.assertEquals(10, testClass.getA());
assertEquals(10, testClass.getA());
}
@Test
@@ -251,14 +247,14 @@ public class MethodUtilTest extends ReflectTestBeans {
final TestClass testClass = new TestClass();
final Method method = MethodUtil.getMethod(TestClass.class, "setA", int.class);
MethodUtil.invoke(testClass, method, "10");
Assertions.assertEquals(10, testClass.getA());
assertEquals(10, testClass.getA());
}
@Test
public void invokeMethodWithParamConvertFailedTest() {
final TestClass testClass = new TestClass();
final Method method = MethodUtil.getMethod(TestClass.class, "setA", int.class);
Assertions.assertThrows(IllegalArgumentException.class,
assertThrows(IllegalArgumentException.class,
() -> MethodUtil.invoke(testClass, method, "aaa"));
}
}

View File

@@ -23,7 +23,6 @@ import cn.hutool.v7.core.math.NumberUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.math.RoundingMode;
import java.util.List;
import java.util.Locale;
import java.util.Set;
@@ -46,7 +45,7 @@ public class RandomUtilTest {
@Test
public void randomDoubleTest() {
final double randomDouble = RandomUtil.randomDouble(0, 1, 0, RoundingMode.HALF_UP);
final double randomDouble = RandomUtil.randomDouble(0, 1, 0);
assertTrue(randomDouble <= 1);
}
@@ -136,7 +135,7 @@ public class RandomUtilTest {
@Test
public void testRandomDoubleWithScaleAndRoundingMode() {
// 测试基本功能生成0到10之间的随机数保留2位小数四舍五入
final double result = RandomUtil.randomDouble(10.0, 2, RoundingMode.HALF_UP);
final double result = RandomUtil.randomDouble(10.0, 2);
// 验证结果在[0, 10)范围内
assertTrue(result >= 0.0 && result < 10.0,
@@ -159,17 +158,17 @@ public class RandomUtilTest {
final double limit = 5.0;
// 测试向上舍入
final double upResult = RandomUtil.randomDouble(limit, 2, RoundingMode.UP);
final double upResult = RandomUtil.randomDouble(limit, 2);
assertTrue(upResult >= 0.0 && upResult < limit,
"UP模式下随机数应该在[0, 5)范围内");
// 测试向下舍入
final double downResult = RandomUtil.randomDouble(limit, 2, RoundingMode.DOWN);
final double downResult = RandomUtil.randomDouble(limit, 2);
assertTrue(downResult >= 0.0 && downResult < limit,
"DOWN模式下随机数应该在[0, 5)范围内");
// 测试四舍五入
final double halfUpResult = RandomUtil.randomDouble(limit, 2, RoundingMode.HALF_UP);
final double halfUpResult = RandomUtil.randomDouble(limit, 2);
assertTrue(halfUpResult >= 0.0 && halfUpResult < limit,
"HALF_UP模式下随机数应该在[0, 5)范围内");
}
@@ -179,7 +178,7 @@ public class RandomUtilTest {
*/
@Test
public void testRandomDoubleWithZeroScale() {
final double result = RandomUtil.randomDouble(10.0, 0, RoundingMode.HALF_UP);
final double result = RandomUtil.randomDouble(10.0, 0);
// 验证结果在[0, 10)范围内
assertTrue(result >= 0.0 && result < 10.0,
@@ -195,7 +194,7 @@ public class RandomUtilTest {
*/
@Test
public void testRandomDoubleWithNullRoundingMode() {
final double result = RandomUtil.randomDouble(10.0, 2, null);
final double result = RandomUtil.randomDouble(10.0, 2);
// 验证结果在[0, 10)范围内
assertTrue(result >= 0.0 && result < 10.0,
@@ -215,7 +214,7 @@ public class RandomUtilTest {
*/
@Test
public void testRandomDoubleWithSmallLimit() {
final double result = RandomUtil.randomDouble(0.001, 5, RoundingMode.HALF_UP);
final double result = RandomUtil.randomDouble(0.001, 5);
// 验证结果在[0, 0.001)范围内
assertTrue(result >= 0.0 && result < 0.001,
@@ -227,7 +226,7 @@ public class RandomUtilTest {
*/
@Test
public void testRandomDoubleWithHighPrecision() {
final double result = RandomUtil.randomDouble(100.0, 10, RoundingMode.HALF_UP);
final double result = RandomUtil.randomDouble(100.0, 10);
// 验证结果在[0, 100)范围内
assertTrue(result >= 0.0 && result < 100.0,

View File

@@ -26,7 +26,6 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.security.SecureRandom;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
@@ -199,7 +198,7 @@ public class TestRandomUtilByDoubao {
assertTrue(doubleInRange >= min && doubleInRange < max);
// 测试保留小数位数
final double doubleWithScale = RandomUtil.randomDouble(min, max, 2, RoundingMode.HALF_UP);
final double doubleWithScale = RandomUtil.randomDouble(min, max, 2);
// 验证小数位数不超过 2 位
final String doubleStr = String.valueOf(doubleWithScale);
int dotIndex = doubleStr.indexOf('.');
@@ -208,7 +207,7 @@ public class TestRandomUtilByDoubao {
}
// 测试无范围保留小数
final double doubleScaleOnly = RandomUtil.randomDouble(3, RoundingMode.HALF_UP);
final double doubleScaleOnly = RandomUtil.randomDouble(3);
final String scaleStr = String.valueOf(doubleScaleOnly);
dotIndex = scaleStr.indexOf('.');
if (dotIndex != -1) {

31
pom.xml
View File

@@ -229,16 +229,27 @@
</plugin>
<!-- 测试覆盖度 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
<check/>
</configuration>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.14</version>
<executions>
<execution>
<id>jacoco-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/jacoco-report</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>