mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
Merge remote-tracking branch 'upstream/v5-dev' into v5-dev
This commit is contained in:
@@ -1,62 +1,95 @@
|
||||
package cn.hutool.core.bean;
|
||||
|
||||
import lombok.Data;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.bean.DynaBean;
|
||||
|
||||
/**
|
||||
* {@link DynaBean}单元测试
|
||||
* @author Looly
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public class DynaBeanTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void beanTest(){
|
||||
public void beanTest() {
|
||||
User user = new User();
|
||||
DynaBean bean = DynaBean.create(user);
|
||||
bean.set("name", "李华");
|
||||
bean.set("age", 12);
|
||||
|
||||
|
||||
String name = bean.get("name");
|
||||
Assert.assertEquals(user.getName(), name);
|
||||
int age = bean.get("age");
|
||||
Assert.assertEquals(user.getAge(), age);
|
||||
|
||||
|
||||
//重复包装测试
|
||||
DynaBean bean2 = new DynaBean(bean);
|
||||
User user2 = bean2.getBean();
|
||||
Assert.assertEquals(user, user2);
|
||||
|
||||
|
||||
//执行指定方法
|
||||
Object invoke = bean2.invoke("testMethod");
|
||||
Assert.assertEquals("test for 李华", invoke);
|
||||
}
|
||||
|
||||
public static class User{
|
||||
|
||||
|
||||
@Test
|
||||
public void beanByStaticClazzConstructorTest() {
|
||||
String name_before = "李华";
|
||||
int age_before = 12;
|
||||
DynaBean bean = DynaBean.create(User.class);
|
||||
bean.set("name", name_before);
|
||||
bean.set("age", age_before);
|
||||
|
||||
String name_after = bean.get("name");
|
||||
Assert.assertEquals(name_before, name_after);
|
||||
int age_after = bean.get("age");
|
||||
Assert.assertEquals(age_before, age_after);
|
||||
|
||||
//重复包装测试
|
||||
DynaBean bean2 = new DynaBean(bean);
|
||||
User user2 = bean2.getBean();
|
||||
User user1 = bean.getBean();
|
||||
Assert.assertEquals(user1, user2);
|
||||
|
||||
//执行指定方法
|
||||
Object invoke = bean2.invoke("testMethod");
|
||||
Assert.assertEquals("test for 李华", invoke);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void beanByInstanceClazzConstructorTest() {
|
||||
String name_before = "李华";
|
||||
int age_before = 12;
|
||||
DynaBean bean = new DynaBean(User.class);
|
||||
bean.set("name", name_before);
|
||||
bean.set("age", age_before);
|
||||
|
||||
String name_after = bean.get("name");
|
||||
Assert.assertEquals(name_before, name_after);
|
||||
int age_after = bean.get("age");
|
||||
Assert.assertEquals(age_before, age_after);
|
||||
|
||||
//重复包装测试
|
||||
DynaBean bean2 = new DynaBean(bean);
|
||||
User user2 = bean2.getBean();
|
||||
User user1 = bean.getBean();
|
||||
Assert.assertEquals(user1, user2);
|
||||
|
||||
//执行指定方法
|
||||
Object invoke = bean2.invoke("testMethod");
|
||||
Assert.assertEquals("test for 李华", invoke);
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class User {
|
||||
private String name;
|
||||
private int age;
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String testMethod(){
|
||||
|
||||
public String testMethod() {
|
||||
return "test for " + this.name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User [name=" + name + ", age=" + age + "]";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -376,6 +376,15 @@ public class FileUtilTest {
|
||||
public void getMimeTypeTest() {
|
||||
String mimeType = FileUtil.getMimeType("test2Write.jpg");
|
||||
Assert.assertEquals("image/jpeg", mimeType);
|
||||
|
||||
mimeType = FileUtil.getMimeType("test2Write.html");
|
||||
Assert.assertEquals("text/html", mimeType);
|
||||
|
||||
mimeType = FileUtil.getMimeType("main.css");
|
||||
Assert.assertEquals("text/css", mimeType);
|
||||
|
||||
mimeType = FileUtil.getMimeType("test.js");
|
||||
Assert.assertEquals("application/x-javascript", mimeType);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@@ -25,7 +25,16 @@ public class PathUtilTest {
|
||||
public void copyTest(){
|
||||
PathUtil.copy(
|
||||
Paths.get("d:/Red2_LYY"),
|
||||
Paths.get("d:/test/")
|
||||
Paths.get("d:/test/aaa/aaa.txt")
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void copyContentTest(){
|
||||
PathUtil.copyContent(
|
||||
Paths.get("d:/Red2_LYY"),
|
||||
Paths.get("d:/test/aaa/")
|
||||
);
|
||||
}
|
||||
|
||||
@@ -37,7 +46,10 @@ public class PathUtilTest {
|
||||
|
||||
@Test
|
||||
public void getMimeTypeTest(){
|
||||
final String mimeType = PathUtil.getMimeType(Paths.get("d:/test/test.jpg"));
|
||||
String mimeType = PathUtil.getMimeType(Paths.get("d:/test/test.jpg"));
|
||||
Assert.assertEquals("image/jpeg", mimeType);
|
||||
|
||||
mimeType = PathUtil.getMimeType(Paths.get("d:/test/test.mov"));
|
||||
Assert.assertEquals("video/quicktime", mimeType);
|
||||
}
|
||||
}
|
||||
|
@@ -5,6 +5,7 @@ import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.thread.ConcurrencyTester;
|
||||
import cn.hutool.core.thread.ThreadUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
@@ -66,4 +67,12 @@ public class SnowflakeTest {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSnowflakeLengthTest(){
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
final long l = IdUtil.getSnowflake(0, 0).nextId();
|
||||
Assert.assertEquals(19, StrUtil.toString(l).length());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,8 +1,9 @@
|
||||
package cn.hutool.core.text.csv;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -16,26 +17,55 @@ public class CsvUtilTest {
|
||||
//从文件中读取CSV数据
|
||||
CsvData data = reader.read(FileUtil.file("test.csv"));
|
||||
List<CsvRow> rows = data.getRows();
|
||||
for (CsvRow csvRow : rows) {
|
||||
Assert.notEmpty(csvRow.getRawList());
|
||||
}
|
||||
final CsvRow row0 = rows.get(0);
|
||||
Assert.assertEquals("sss,sss", row0.get(0));
|
||||
Assert.assertEquals("姓名", row0.get(1));
|
||||
Assert.assertEquals("性别", row0.get(2));
|
||||
Assert.assertEquals("关注\"对象\"", row0.get(3));
|
||||
Assert.assertEquals("年龄", row0.get(4));
|
||||
Assert.assertEquals("", row0.get(5));
|
||||
Assert.assertEquals("\"", row0.get(6));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readTest2() {
|
||||
CsvReader reader = CsvUtil.getReader();
|
||||
reader.read(FileUtil.getUtf8Reader("test.csv"), (csvRow)-> Assert.notEmpty(csvRow.getRawList()));
|
||||
reader.read(FileUtil.getUtf8Reader("test.csv"), (csvRow)-> {
|
||||
// 只有一行,所以直接判断
|
||||
Assert.assertEquals("sss,sss", csvRow.get(0));
|
||||
Assert.assertEquals("姓名", csvRow.get(1));
|
||||
Assert.assertEquals("性别", csvRow.get(2));
|
||||
Assert.assertEquals("关注\"对象\"", csvRow.get(3));
|
||||
Assert.assertEquals("年龄", csvRow.get(4));
|
||||
Assert.assertEquals("", csvRow.get(5));
|
||||
Assert.assertEquals("\"", csvRow.get(6));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readTest3() {
|
||||
CsvReader reader = CsvUtil.getReader();
|
||||
reader.read(FileUtil.getUtf8Reader("test.csv"), Console::log);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void writeTest() {
|
||||
CsvWriter writer = CsvUtil.getWriter("e:/testWrite.csv", CharsetUtil.CHARSET_UTF_8);
|
||||
CsvWriter writer = CsvUtil.getWriter("d:/test/testWrite.csv", CharsetUtil.CHARSET_UTF_8);
|
||||
writer.write(
|
||||
new String[] {"a1", "b1", "c1", "123345346456745756756785656"},
|
||||
new String[] {"a2", "b2", "c2"},
|
||||
new String[] {"a3", "b3", "c3"}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void readLfTest(){
|
||||
final CsvReader reader = CsvUtil.getReader();
|
||||
final CsvData read = reader.read(FileUtil.file("d:/test/rw_test.csv"));
|
||||
for (CsvRow row : read) {
|
||||
Console.log(row);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -43,7 +43,7 @@ public class IdcardUtilTest {
|
||||
Assert.assertEquals("150102198807303035", convert15To18);
|
||||
|
||||
String convert15To18Second = IdcardUtil.convert15To18("330102200403064");
|
||||
Assert.assertEquals("33010219200403064x", convert15To18Second);
|
||||
Assert.assertEquals("33010219200403064X", convert15To18Second);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -89,8 +89,20 @@ public class IdcardUtilTest {
|
||||
|
||||
@Test
|
||||
public void isValidCard18Test(){
|
||||
final boolean isValidCard18 = IdcardUtil.isValidCard18("3301022011022000D6");
|
||||
boolean isValidCard18 = IdcardUtil.isValidCard18("3301022011022000D6");
|
||||
Assert.assertFalse(isValidCard18);
|
||||
|
||||
// 不忽略大小写情况下,X严格校验必须大写
|
||||
isValidCard18 = IdcardUtil.isValidCard18("33010219200403064x", false);
|
||||
Assert.assertFalse(isValidCard18);
|
||||
isValidCard18 = IdcardUtil.isValidCard18("33010219200403064X", false);
|
||||
Assert.assertTrue(isValidCard18);
|
||||
|
||||
// 非严格校验下大小写皆可
|
||||
isValidCard18 = IdcardUtil.isValidCard18("33010219200403064x");
|
||||
Assert.assertTrue(isValidCard18);
|
||||
isValidCard18 = IdcardUtil.isValidCard18("33010219200403064X");
|
||||
Assert.assertTrue(isValidCard18);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@@ -1,11 +1,10 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.util.RuntimeUtil;
|
||||
|
||||
/**
|
||||
* 命令行单元测试
|
||||
* @author looly
|
||||
@@ -26,4 +25,9 @@ public class RuntimeUtilTest {
|
||||
String str = RuntimeUtil.execForStr("cmd /c dir");
|
||||
Console.log(str);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getUsableMemoryTest(){
|
||||
Assert.assertTrue(RuntimeUtil.getUsableMemory() > 0);
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
@@ -8,6 +9,7 @@ import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
@@ -102,8 +104,9 @@ public class ZipUtilTest {
|
||||
try (OutputStream out = new FileOutputStream(zip)){
|
||||
//实际应用中, out 为 HttpServletResponse.getOutputStream
|
||||
ZipUtil.zip(out, Charset.defaultCharset(), false, null, new File(dir));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1 +1,2 @@
|
||||
"sss,sss",姓名,"性别",关注"对象",年龄
|
||||
# 这是一行注释,读取时应忽略
|
||||
"sss,sss",姓名,"性别",关注"对象",年龄,"","""
|
Can't render this file because it contains an unexpected character in line 1 and column 33.
|
Reference in New Issue
Block a user