Merge remote-tracking branch 'upstream/v5-dev' into v5-dev

This commit is contained in:
totalo
2021-03-10 14:53:49 +08:00
201 changed files with 1959 additions and 1047 deletions

View File

@@ -3,8 +3,6 @@ package cn.hutool.core.codec;
import org.junit.Assert;
import org.junit.Test;
import cn.hutool.core.codec.BCD;
public class BCDTest {
@Test

View File

@@ -11,6 +11,7 @@ import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
@@ -694,4 +695,14 @@ public class CollUtilTest {
Assert.assertEquals(0, CollUtil.page(3, 5, objects).size());
}
@Test
public void subtractToListTest(){
List<Long> list1 = Arrays.asList(1L, 2L, 3L);
List<Long> list2 = Arrays.asList(2L, 3L);
List<Long> result = CollUtil.subtractToList(list1, list2);
Assert.assertEquals(1, result.size());
Assert.assertEquals(1L, result.get(0), 1);
}
}

View File

@@ -101,4 +101,15 @@ public class ListUtilTest {
int[] d1 = ListUtil.page(0,8,a).stream().mapToInt(Integer::valueOf).toArray();
Assert.assertArrayEquals(new int[]{1,2,3,4,5},d1);
}
@Test
public void subTest(){
final List<Integer> of = ListUtil.of(1, 2, 3, 4);
final List<Integer> sub = ListUtil.sub(of, 2, 4);
sub.remove(0);
// 对子列表操作不影响原列表
Assert.assertEquals(4, of.size());
Assert.assertEquals(1, sub.size());
}
}

View File

@@ -5,7 +5,6 @@ import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.junit.Test;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.CharsetUtil;
/**

View File

@@ -3,9 +3,6 @@ package cn.hutool.core.convert;
import org.junit.Assert;
import org.junit.Test;
import cn.hutool.core.convert.Converter;
import cn.hutool.core.convert.ConverterRegistry;
/**
* ConverterRegistry 单元测试
* @author Looly

View File

@@ -1,25 +1,34 @@
package cn.hutool.core.convert;
import org.junit.Assert;
import org.junit.Test;
public class NumberWordFormatTest {
@Test
public void formatTest() {
String format = NumberWordFormatter.format(100.23);
Assert.assertEquals("ONE HUNDRED AND CENTS TWENTY THREE ONLY", format);
String format2 = NumberWordFormatter.format("2100.00");
Assert.assertEquals("TWO THOUSAND ONE HUNDRED AND CENTS ONLY", format2);
String format3 = NumberWordFormatter.formatValue(4384324, false);
Assert.assertEquals("4.38m", format3);
String format4 = NumberWordFormatter.formatValue(4384324);
Assert.assertEquals("438.43w", format4);
String format5 = NumberWordFormatter.formatValue(438);
Assert.assertEquals("438", format5);
}
}
package cn.hutool.core.convert;
import org.junit.Assert;
import org.junit.Test;
public class NumberWordFormatTest {
@Test
public void formatTest() {
String format = NumberWordFormatter.format(100.23);
Assert.assertEquals("ONE HUNDRED AND CENTS TWENTY THREE ONLY", format);
String format2 = NumberWordFormatter.format("2100.00");
Assert.assertEquals("TWO THOUSAND ONE HUNDRED AND CENTS ONLY", format2);
}
@Test
public void formatSimpleTest() {
String format1 = NumberWordFormatter.formatSimple(1200, false);
Assert.assertEquals("1.2k", format1);
String format2 = NumberWordFormatter.formatSimple(4384324, false);
Assert.assertEquals("4.38m", format2);
String format3 = NumberWordFormatter.formatSimple(4384324, true);
Assert.assertEquals("438.43w", format3);
String format4 = NumberWordFormatter.formatSimple(4384324);
Assert.assertEquals("438.43w", format4);
String format5 = NumberWordFormatter.formatSimple(438);
Assert.assertEquals("438", format5);
}
}

View File

@@ -6,6 +6,9 @@ import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* 文件类型判断单元测试
@@ -13,19 +16,19 @@ import java.io.File;
*
*/
public class FileTypeUtilTest {
@Test
@Ignore
public void fileTypeUtilTest() {
File file = FileUtil.file("hutool.jpg");
String type = FileTypeUtil.getType(file);
Assert.assertEquals("jpg", type);
FileTypeUtil.putFileType("ffd8ffe000104a464946", "new_jpg");
String newType = FileTypeUtil.getType(file);
Assert.assertEquals("new_jpg", newType);
}
@Test
@Ignore
public void emptyTest() {
@@ -41,4 +44,16 @@ public class FileTypeUtilTest {
String type = FileTypeUtil.getType(file);
Console.log(type);
}
@Test
@Ignore
public void ofdTest() {
File file = FileUtil.file("e:/test.ofd");
String hex = IoUtil.readHex28Upper(FileUtil.getInputStream(file));
Console.log(hex);
String type = FileTypeUtil.getType(file);
Console.log(type);
Assert.assertEquals("ofd", type);
}
}

View File

@@ -51,7 +51,13 @@ public class DataSizeUtilTest {
@Test
public void formatTest(){
final String format = DataSizeUtil.format(Long.MAX_VALUE);
Assert.assertEquals("8,192 EB", format);
String format = DataSizeUtil.format(Long.MAX_VALUE);
Assert.assertEquals("8 EB", format);
format = DataSizeUtil.format(1024L * 1024 * 1024 * 1024 * 1024);
Assert.assertEquals("1 PB", format);
format = DataSizeUtil.format(1024L * 1024 * 1024 * 1024);
Assert.assertEquals("1 TB", format);
}
}

View File

@@ -1,12 +1,12 @@
package cn.hutool.core.lang;
import java.util.Set;
import org.junit.Ignore;
import org.junit.Test;
import java.util.Set;
public class ClassScanerTest {
@Test
@Ignore
public void scanTest() {

View File

@@ -0,0 +1,35 @@
package cn.hutool.core.text;
import cn.hutool.core.lang.Console;
import org.junit.Assert;
import org.junit.Test;
import java.util.Map;
public class StrMatcherTest {
@Test
public void matcherTest(){
final StrMatcher strMatcher = new StrMatcher("${name}-${age}-${gender}-${country}-${province}-${city}-${status}");
final Map<String, String> match = strMatcher.match("小明-19-男-中国-河南-郑州-已婚");
Console.log(match);
}
@Test
public void matcherTest2(){
// 当有无匹配项的时候,按照全不匹配对待
final StrMatcher strMatcher = new StrMatcher("${name}-${age}-${gender}-${country}-${province}-${city}-${status}");
final Map<String, String> match = strMatcher.match("小明-19-男-中国-河南-郑州");
Assert.assertEquals(0, match.size());
}
@Test
public void matcherTest3(){
// 当有无匹配项的时候,按照全不匹配对待
final StrMatcher strMatcher = new StrMatcher("${name}经过${year}年");
final Map<String, String> match = strMatcher.match("小明经过20年成长为一个大人。");
Console.log(match);
Assert.assertEquals("小明", match.get("name"));
Assert.assertEquals("20", match.get("year"));
}
}

View File

@@ -17,7 +17,7 @@ import java.util.concurrent.CountDownLatch;
/**
* {@link IdUtil} 单元测试
*
*
* @author looly
*
*/
@@ -31,12 +31,12 @@ public class IdUtilTest {
String randomUUID = IdUtil.randomUUID();
Assert.assertEquals(36, randomUUID.length());
}
@Test
public void fastUUIDTest() {
String simpleUUID = IdUtil.fastSimpleUUID();
Assert.assertEquals(32, simpleUUID.length());
String randomUUID = IdUtil.fastUUID();
Assert.assertEquals(36, randomUUID.length());
}
@@ -60,25 +60,26 @@ public class IdUtilTest {
}
Console.log(timer.interval());
}
@Test
public void objectIdTest() {
String id = IdUtil.objectId();
Assert.assertEquals(24, id.length());
}
@Test
public void createSnowflakeTest() {
Snowflake snowflake = IdUtil.createSnowflake(1, 1);
long id = snowflake.nextId();
Assert.assertTrue(id > 0);
}
@Test
@Ignore
public void snowflakeBenchTest() {
final Set<Long> set = new ConcurrentHashSet<>();
final Snowflake snowflake = IdUtil.createSnowflake(1, 1);
//线程数
int threadCount = 100;
//每个线程生成的ID数
@@ -94,7 +95,7 @@ public class IdUtilTest {
latch.countDown();
});
}
//等待全部线程结束
try {
latch.await();
@@ -103,11 +104,12 @@ public class IdUtilTest {
}
Assert.assertEquals(threadCount * idCountPerThread, set.size());
}
@Test
@Ignore
public void snowflakeBenchTest2() {
final Set<Long> set = new ConcurrentHashSet<>();
//线程数
int threadCount = 100;
//每个线程生成的ID数
@@ -123,7 +125,7 @@ public class IdUtilTest {
latch.countDown();
});
}
//等待全部线程结束
try {
latch.await();

View File

@@ -5,17 +5,18 @@ import org.junit.Assert;
import org.junit.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.Set;
/**
* {@link NumberUtil} 单元测试类
*
*
* @author Looly
*
*/
public class NumberUtilTest {
@Test
public void addTest() {
Float a = 3.15f;
@@ -23,7 +24,7 @@ public class NumberUtilTest {
double result = NumberUtil.add(a, b).doubleValue();
Assert.assertEquals(7.37, result, 2);
}
@Test
public void addTest2() {
double a = 3.15f;
@@ -31,7 +32,7 @@ public class NumberUtilTest {
double result = NumberUtil.add(a, b);
Assert.assertEquals(7.37, result, 2);
}
@Test
public void addTest3() {
float a = 3.15f;
@@ -39,13 +40,13 @@ public class NumberUtilTest {
double result = NumberUtil.add(a, b, a, b).doubleValue();
Assert.assertEquals(14.74, result, 2);
}
@Test
public void addTest4() {
BigDecimal result = NumberUtil.add(new BigDecimal("133"), new BigDecimal("331"));
Assert.assertEquals(new BigDecimal("464"), result);
}
@Test
public void isIntegerTest() {
Assert.assertTrue(NumberUtil.isInteger("-12"));
@@ -54,7 +55,7 @@ public class NumberUtilTest {
Assert.assertTrue(NumberUtil.isInteger("0"));
Assert.assertFalse(NumberUtil.isInteger("23.4"));
}
@Test
public void isLongTest() {
Assert.assertTrue(NumberUtil.isLong("-12"));
@@ -63,7 +64,7 @@ public class NumberUtilTest {
Assert.assertTrue(NumberUtil.isLong("0"));
Assert.assertFalse(NumberUtil.isLong("23.4"));
}
@Test
public void isNumberTest() {
Assert.assertTrue(NumberUtil.isNumber("28.55"));
@@ -78,7 +79,7 @@ public class NumberUtilTest {
double result = NumberUtil.div(0, 1);
Assert.assertEquals(0.0, result, 0);
}
@Test
public void roundTest() {
@@ -93,7 +94,7 @@ public class NumberUtilTest {
String round4 = NumberUtil.roundStr("2.675", 2);
Assert.assertEquals("2.68", round3);
Assert.assertEquals("2.68", round4);
// 四舍六入五成双
String round31 = NumberUtil.roundStr(4.245, 2, RoundingMode.HALF_EVEN);
String round41 = NumberUtil.roundStr("4.2451", 2, RoundingMode.HALF_EVEN);
@@ -105,7 +106,7 @@ public class NumberUtilTest {
String round6 = NumberUtil.roundStr("2.6005", 2);
Assert.assertEquals("2.60", round5);
Assert.assertEquals("2.60", round6);
// 补0
String round7 = NumberUtil.roundStr(2.600, 2);
String round8 = NumberUtil.roundStr("2.600", 2);
@@ -118,7 +119,7 @@ public class NumberUtilTest {
String roundStr = NumberUtil.roundStr(2.647, 2);
Assert.assertEquals(roundStr, "2.65");
}
@Test
public void roundHalfEvenTest() {
String roundStr = NumberUtil.roundHalfEven(4.245, 2).toString();
@@ -129,7 +130,7 @@ public class NumberUtilTest {
Assert.assertEquals(roundStr, "4.25");
roundStr = NumberUtil.roundHalfEven(4.2250, 2).toString();
Assert.assertEquals(roundStr, "4.22");
roundStr = NumberUtil.roundHalfEven(1.2050, 2).toString();
Assert.assertEquals(roundStr, "1.20");
roundStr = NumberUtil.roundHalfEven(1.2150, 2).toString();
@@ -159,14 +160,14 @@ public class NumberUtilTest {
String format = NumberUtil.decimalFormat(",###", c);
Assert.assertEquals("299,792,458", format);
}
@Test
public void decimalFormatMoneyTest() {
double c = 299792400.543534534;
String format = NumberUtil.decimalFormatMoney(c);
Assert.assertEquals("299,792,400.54", format);
double value = 0.5;
String money = NumberUtil.decimalFormatMoney(value);
Assert.assertEquals("0.50", money);
@@ -176,17 +177,17 @@ public class NumberUtilTest {
public void equalsTest() {
Assert.assertTrue(NumberUtil.equals(new BigDecimal("0.00"), BigDecimal.ZERO));
}
@Test
public void formatPercentTest() {
String str = NumberUtil.formatPercent(0.33543545, 2);
Assert.assertEquals("33.54%", str);
}
@Test
public void toBigDecimalTest() {
double a = 3.14;
BigDecimal bigDecimal = NumberUtil.toBigDecimal(a);
Assert.assertEquals("3.14", bigDecimal.toString());
@@ -208,7 +209,7 @@ public class NumberUtilTest {
int min = NumberUtil.min(5,4,3,6,1);
Assert.assertEquals(1, min);
}
@Test
public void parseIntTest() {
int number = NumberUtil.parseInt("0xFF");
@@ -258,7 +259,7 @@ public class NumberUtilTest {
Number v2 = NumberUtil.parseNumber("1,482.00D");
Assert.assertEquals(1482L, v2);
}
@Test
public void parseLongTest() {
long number = NumberUtil.parseLong("0xFF");
@@ -303,18 +304,36 @@ public class NumberUtilTest {
Assert.assertEquals(120, factorial);
factorial = NumberUtil.factorial(5, 1);
Assert.assertEquals(120, factorial);
Assert.assertEquals(5, NumberUtil.factorial(5, 4));
Assert.assertEquals(2432902008176640000L, NumberUtil.factorial(20, 0));
}
@Test
public void factorialTest2(){
long factorial = NumberUtil.factorial(new BigInteger("0")).longValue();
Assert.assertEquals(1, factorial);
Assert.assertEquals(1L, NumberUtil.factorial(new BigInteger("1")).longValue());
Assert.assertEquals(1307674368000L, NumberUtil.factorial(new BigInteger("15")).longValue());
Assert.assertEquals(2432902008176640000L, NumberUtil.factorial(20));
factorial = NumberUtil.factorial(new BigInteger("5"), new BigInteger("0")).longValue();
Assert.assertEquals(120, factorial);
factorial = NumberUtil.factorial(new BigInteger("5"), BigInteger.ONE).longValue();
Assert.assertEquals(120, factorial);
Assert.assertEquals(5, NumberUtil.factorial(new BigInteger("5"), new BigInteger("4")).longValue());
Assert.assertEquals(2432902008176640000L, NumberUtil.factorial(new BigInteger("20"), BigInteger.ZERO).longValue());
}
@Test
public void mulTest(){
final BigDecimal mul = NumberUtil.mul(new BigDecimal("10"), null);
Assert.assertEquals(BigDecimal.ZERO, mul);
}
@Test
public void isPowerOfTwoTest() {
Assert.assertFalse(NumberUtil.isPowerOfTwo(-1));