mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
Merge remote-tracking branch 'origin/v5-dev' into v5-dev
This commit is contained in:
@@ -278,4 +278,38 @@ public class NumberChineseFormatterTest {
|
||||
// 非法字符
|
||||
NumberChineseFormatter.chineseToNumber("一百你三");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singleMoneyTest(){
|
||||
String format = NumberChineseFormatter.format(0.01, false, true);
|
||||
Assert.assertEquals("一分", format);
|
||||
format = NumberChineseFormatter.format(0.10, false, true);
|
||||
Assert.assertEquals("一角", format);
|
||||
format = NumberChineseFormatter.format(0.12, false, true);
|
||||
Assert.assertEquals("一角二分", format);
|
||||
|
||||
format = NumberChineseFormatter.format(1.00, false, true);
|
||||
Assert.assertEquals("一元整", format);
|
||||
format = NumberChineseFormatter.format(1.10, false, true);
|
||||
Assert.assertEquals("一元一角", format);
|
||||
format = NumberChineseFormatter.format(1.02, false, true);
|
||||
Assert.assertEquals("一元零二分", format);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singleNumberTest(){
|
||||
String format = NumberChineseFormatter.format(0.01, false, false);
|
||||
Assert.assertEquals("零点零一", format);
|
||||
format = NumberChineseFormatter.format(0.10, false, false);
|
||||
Assert.assertEquals("零点一", format);
|
||||
format = NumberChineseFormatter.format(0.12, false, false);
|
||||
Assert.assertEquals("零点一二", format);
|
||||
|
||||
format = NumberChineseFormatter.format(1.00, false, false);
|
||||
Assert.assertEquals("一", format);
|
||||
format = NumberChineseFormatter.format(1.10, false, false);
|
||||
Assert.assertEquals("一点一", format);
|
||||
format = NumberChineseFormatter.format(1.02, false, false);
|
||||
Assert.assertEquals("一点零二", format);
|
||||
}
|
||||
}
|
||||
|
@@ -298,6 +298,12 @@ public class FileUtilTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void loopFilesTest2() {
|
||||
FileUtil.loopFiles("").forEach(Console::log);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void loopFilesWithDepthTest() {
|
||||
|
@@ -11,8 +11,6 @@ import org.junit.Test;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* {@link Opt}的单元测试
|
||||
@@ -72,7 +70,7 @@ public class OptTest {
|
||||
User user = new User();
|
||||
// 相当于上面peek的动态参数调用,更加灵活,你可以像操作数组一样去动态设置中间的步骤,也可以使用这种方式去编写你的代码
|
||||
// 可以一行搞定
|
||||
Opt.ofNullable("hutool").peeks(user::setUsername, user::setNickname, System.out::println);
|
||||
Opt.ofNullable("hutool").peeks(user::setUsername, user::setNickname);
|
||||
// 也可以在适当的地方换行使得代码的可读性提高
|
||||
Opt.of(user).peeks(
|
||||
u -> Assert.assertEquals("hutool", u.getNickname()),
|
||||
@@ -83,15 +81,11 @@ public class OptTest {
|
||||
|
||||
// 注意,传入的lambda中,对包裹内的元素执行赋值操作并不会影响到原来的元素,这是java语言的特性。。。
|
||||
// 这也是为什么我们需要getter和setter而不直接给bean中的属性赋值中的其中一个原因
|
||||
String name = Opt.ofNullable("hutool").peeks(username -> username = "123", username -> username = "456", n -> Assert.assertEquals("hutool", n)).get();
|
||||
String name = Opt.ofNullable("hutool").peeks(
|
||||
username -> username = "123", username -> username = "456",
|
||||
n -> Assert.assertEquals("hutool", n)).get();
|
||||
Assert.assertEquals("hutool", name);
|
||||
|
||||
// 在控制台打印n次hutool
|
||||
int n = 10;
|
||||
@SuppressWarnings("unchecked")
|
||||
Consumer<String>[] actions = Stream.<Consumer<String>>generate(() -> System.out::println).limit(n).toArray(Consumer[]::new);
|
||||
Opt.ofNullable("hutool").peeks(actions);
|
||||
|
||||
// 当然,以下情况不会抛出NPE,但也没什么意义
|
||||
Opt.ofNullable("hutool").peeks().peeks().peeks();
|
||||
Opt.ofNullable(null).peeks(i -> {
|
||||
|
@@ -227,4 +227,13 @@ public class ValidatorTest {
|
||||
Validator.validateIpv4("255.255.255.255", "Error ip");
|
||||
Validator.validateIpv4("127.0.0.0", "Error ip");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isUrlTest(){
|
||||
String content = "https://detail.tmall.com/item.htm?" +
|
||||
"id=639428931841&ali_refid=a3_430582_1006:1152464078:N:Sk5vwkMVsn5O6DcnvicELrFucL21A32m:0af8611e23c1d07697e";
|
||||
|
||||
Assert.assertTrue(Validator.isMatchRegex(Validator.URL, content));
|
||||
Assert.assertTrue(Validator.isMatchRegex(Validator.URL_HTTP, content));
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,60 @@
|
||||
package cn.hutool.core.lang.reflect;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 见:https://gitee.com/dromara/hutool/pulls/447/files
|
||||
*
|
||||
* TODO 同时继承泛型和实现泛型接口需要解析,此处为F
|
||||
*/
|
||||
public class ActualTypeMapperPoolTest {
|
||||
|
||||
@Test
|
||||
public void getTypeArgumentTest(){
|
||||
final Map<Type, Type> typeTypeMap = ActualTypeMapperPool.get(FinalClass.class);
|
||||
typeTypeMap.forEach((key, value)->{
|
||||
if("A".equals(key.getTypeName())){
|
||||
Assert.assertEquals(Character.class, value);
|
||||
} else if("B".equals(key.getTypeName())){
|
||||
Assert.assertEquals(Boolean.class, value);
|
||||
} else if("C".equals(key.getTypeName())){
|
||||
Assert.assertEquals(String.class, value);
|
||||
} else if("D".equals(key.getTypeName())){
|
||||
Assert.assertEquals(Double.class, value);
|
||||
} else if("E".equals(key.getTypeName())){
|
||||
Assert.assertEquals(Integer.class, value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTypeArgumentStrKeyTest(){
|
||||
final Map<String, Type> typeTypeMap = ActualTypeMapperPool.getStrKeyMap(FinalClass.class);
|
||||
typeTypeMap.forEach((key, value)->{
|
||||
if("A".equals(key)){
|
||||
Assert.assertEquals(Character.class, value);
|
||||
} else if("B".equals(key)){
|
||||
Assert.assertEquals(Boolean.class, value);
|
||||
} else if("C".equals(key)){
|
||||
Assert.assertEquals(String.class, value);
|
||||
} else if("D".equals(key)){
|
||||
Assert.assertEquals(Double.class, value);
|
||||
} else if("E".equals(key)){
|
||||
Assert.assertEquals(Integer.class, value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public interface BaseInterface<A, B, C> {}
|
||||
public interface FirstInterface<A, B, D, E> extends BaseInterface<A, B, String> {}
|
||||
public interface SecondInterface<A, B, F> extends BaseInterface<A, B, String> {}
|
||||
|
||||
public static class BaseClass<A, D> implements FirstInterface<A, Boolean, D, Integer> {}
|
||||
public static class FirstClass extends BaseClass<Character, Double> implements SecondInterface<Character, Boolean, FirstClass> {}
|
||||
public static class SecondClass extends FirstClass {}
|
||||
public static class FinalClass extends SecondClass {}
|
||||
}
|
@@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -66,4 +67,13 @@ public class TreeTest {
|
||||
|
||||
Assert.assertEquals(treeNodes.size(), 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void walkTest(){
|
||||
List<String> ids = new ArrayList<>();
|
||||
final Tree<String> tree = TreeUtil.buildSingle(nodeList, "0");
|
||||
tree.walk((tr)-> ids.add(tr.getId()));
|
||||
|
||||
Assert .assertEquals(7, ids.size());
|
||||
}
|
||||
}
|
||||
|
@@ -306,4 +306,11 @@ public class UrlBuilderTest {
|
||||
|
||||
Assert.assertEquals("https://domain.cn/api/xxx/bbb", url);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void percent2BTest(){
|
||||
String url = "http://xxx.cn/a?Signature=3R013Bj9Uq4YeISzAs2iC%2BTVCL8%3D";
|
||||
final UrlBuilder of = UrlBuilder.ofHttpWithoutEncode(url);
|
||||
Assert.assertEquals(url, of.toString());
|
||||
}
|
||||
}
|
||||
|
@@ -3,6 +3,7 @@ package cn.hutool.core.net;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.net.url.UrlBuilder;
|
||||
import cn.hutool.core.net.url.UrlQuery;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
@@ -99,4 +100,18 @@ public class UrlQueryTest {
|
||||
query = URLUtil.buildQuery(map, StandardCharsets.UTF_8);
|
||||
Assert.assertEquals("password==&username%3D=SSM", query);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void plusTest(){
|
||||
// 根据RFC3986,在URL中,+是安全字符,即此符号不转义
|
||||
final String a = UrlQuery.of(MapUtil.of("a+b", "1+2")).build(CharsetUtil.CHARSET_UTF_8);
|
||||
Assert.assertEquals("a+b=1+2", a);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void spaceTest(){
|
||||
// 根据RFC3986,在URL中,空格编码为"%20"
|
||||
final String a = UrlQuery.of(MapUtil.of("a ", " ")).build(CharsetUtil.CHARSET_UTF_8);
|
||||
Assert.assertEquals("a%20=%20", a);
|
||||
}
|
||||
}
|
||||
|
@@ -122,4 +122,17 @@ public class SplitIterTest {
|
||||
final List<String> strings = splitIter.toList(false);
|
||||
Assert.assertEquals(3, strings.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void splitToSingleTest(){
|
||||
String text = "";
|
||||
SplitIter splitIter = new SplitIter(text,
|
||||
new CharFinder(':'),
|
||||
3,
|
||||
false
|
||||
);
|
||||
|
||||
final List<String> strings = splitIter.toList(false);
|
||||
Assert.assertEquals(1, strings.size());
|
||||
}
|
||||
}
|
||||
|
@@ -54,4 +54,12 @@ public class StrSpliterTest {
|
||||
Assert.assertEquals(Long.valueOf(1L), split.get(0));
|
||||
Assert.assertEquals(Long.valueOf(2L), split.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void splitEmptyTest(){
|
||||
String str = "";
|
||||
final String[] split = str.split(",");
|
||||
final String[] strings = StrSplitter.splitToArray(str, ",", -1, false, false);
|
||||
Assert.assertArrayEquals(split, strings);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,47 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* 坐标转换工具类单元测试<br>
|
||||
* 测试参考:https://github.com/wandergis/coordtransform
|
||||
*
|
||||
* @author hongzhe.qin, looly
|
||||
*/
|
||||
public class CoordinateUtilTest {
|
||||
|
||||
@Test
|
||||
public void gcj02ToBd09Test() {
|
||||
final CoordinateUtil.Coordinate gcj02 = CoordinateUtil.gcj02ToBd09(116.404, 39.915);
|
||||
Assert.assertEquals(116.41036949371029D, gcj02.getLng(), 15);
|
||||
Assert.assertEquals(39.92133699351021D, gcj02.getLat(), 15);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bd09toGcj02Test(){
|
||||
final CoordinateUtil.Coordinate gcj02 = CoordinateUtil.bd09ToGcj02(116.404, 39.915);
|
||||
Assert.assertEquals(116.39762729119315D, gcj02.getLng(), 15);
|
||||
Assert.assertEquals(39.90865673957631D, gcj02.getLat(), 15);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void gcj02ToWgs84(){
|
||||
final CoordinateUtil.Coordinate gcj02 = CoordinateUtil.wgs84ToGcj02(116.404, 39.915);
|
||||
Assert.assertEquals(116.39775550083061D, gcj02.getLng(), 15);
|
||||
Assert.assertEquals(39.91359571849836D, gcj02.getLat(), 15);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wgs84ToGcj02Test(){
|
||||
final CoordinateUtil.Coordinate gcj02 = CoordinateUtil.wgs84ToGcj02(116.404, 39.915);
|
||||
Assert.assertEquals(116.41024449916938D, gcj02.getLng(), 15);
|
||||
Assert.assertEquals(39.91640428150164D, gcj02.getLat(), 15);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wgs84toBd09(){
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -11,6 +11,7 @@ import org.junit.Test;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
@@ -169,15 +170,20 @@ public class ZipUtilTest {
|
||||
String file3 = "d:/test/asn1.key";
|
||||
|
||||
String zip = "d:/test/test2.zip";
|
||||
try (OutputStream out = new FileOutputStream(zip)){
|
||||
//实际应用中, out 为 HttpServletResponse.getOutputStream
|
||||
ZipUtil.zip(out, Charset.defaultCharset(), false, null,
|
||||
new File(file1),
|
||||
new File(file2),
|
||||
new File(file3)
|
||||
);
|
||||
} catch (IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
//实际应用中, out 为 HttpServletResponse.getOutputStream
|
||||
ZipUtil.zip(FileUtil.getOutputStream(zip), Charset.defaultCharset(), false, null,
|
||||
new File(file1),
|
||||
new File(file2),
|
||||
new File(file3)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void zipToStreamTest(){
|
||||
String zip = "d:/test/testToStream.zip";
|
||||
OutputStream out = FileUtil.getOutputStream(zip);
|
||||
ZipUtil.zip(out, new String[]{"sm1_alias.txt"},
|
||||
new InputStream[]{FileUtil.getInputStream("d:/test/sm4_1.txt")});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user