This commit is contained in:
Looly
2022-06-16 19:19:56 +08:00
parent e491349b81
commit 40ff9f051e
26 changed files with 160 additions and 119 deletions

View File

@@ -6,6 +6,7 @@ import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.test.bean.ExamInfoDict;
import cn.hutool.core.text.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.SystemUtil;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
@@ -13,10 +14,25 @@ import org.junit.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
* jdk15: 删除了 object registerNatives
* @return 反馈jdk版本7、8、11、15、17
* @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]);
}
@Test
public void getMethodsTest() {
Method[] methods = MethodUtil.getMethods(ExamInfoDict.class);
Assert.assertEquals(20, methods.length);
Assert.assertEquals(isGteJdk15 ? 19 : 20, methods.length);
//过滤器测试
methods = MethodUtil.getMethods(ExamInfoDict.class, t -> Integer.class.equals(t.getReturnType()));
@@ -28,7 +44,7 @@ public class MethodUtilTest {
//null过滤器测试
methods = MethodUtil.getMethods(ExamInfoDict.class, null);
Assert.assertEquals(20, methods.length);
Assert.assertEquals(isGteJdk15 ? 19 : 20, methods.length);
final Method method2 = methods[0];
Assert.assertNotNull(method2);
}
@@ -110,7 +126,7 @@ public class MethodUtilTest {
public void getMethodsFromClassExtends() {
// 继承情况下,需解决方法去重问题
Method[] methods = MethodUtil.getMethods(ReflectUtilTest.C2.class);
Assert.assertEquals(15, methods.length);
Assert.assertEquals(isGteJdk15 ? 14 : 15, methods.length);
// 排除Object中的方法
// 3个方法包括类

View File

@@ -1,5 +1,6 @@
package cn.hutool.core.clone;
package cn.hutool.core.util;
import cn.hutool.core.exceptions.CloneRuntimeException;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.junit.Assert;
@@ -36,7 +37,7 @@ public class CloneTest {
*
*/
@Data
static class Cat implements Cloneable<Cat>{
static class Cat implements Cloneable{
private String name = "miaomiao";
private int age = 2;
@@ -57,8 +58,17 @@ public class CloneTest {
*/
@EqualsAndHashCode(callSuper = false)
@Data
static class Dog extends CloneSupport<Dog>{
static class Dog implements Cloneable{
private String name = "wangwang";
private int age = 3;
@Override
public Dog clone() {
try {
return (Dog) super.clone();
} catch (final CloneNotSupportedException e) {
throw new CloneRuntimeException(e);
}
}
}
}

View File

@@ -1,6 +1,7 @@
package cn.hutool.core.clone;
package cn.hutool.core.util;
import cn.hutool.core.exceptions.CloneRuntimeException;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.junit.Assert;
@@ -18,7 +19,7 @@ public class DefaultCloneTest {
oldCar.setId(1);
oldCar.setWheelList(Stream.of(new Wheel("h")).collect(Collectors.toList()));
final Car newCar = oldCar.clone0();
final Car newCar = oldCar.clone();
Assert.assertEquals(oldCar.getId(), newCar.getId());
Assert.assertEquals(oldCar.getWheelList(), newCar.getWheelList());
@@ -31,9 +32,18 @@ public class DefaultCloneTest {
}
@Data
static class Car implements DefaultCloneable<Car> {
static class Car implements Cloneable {
private Integer id;
private List<Wheel> wheelList;
@Override
public Car clone() {
try {
return (Car) super.clone();
} catch (final CloneNotSupportedException e) {
throw new CloneRuntimeException(e);
}
}
}
@Data

View File

@@ -1,116 +0,0 @@
package cn.hutool.core.util;
import org.junit.Assert;
import org.junit.Test;
import javax.xml.bind.annotation.*;
/**
* {@link JAXBUtil} 工具类
* 测试 xml 和 bean 互转工具类
*
* @author dazer
*/
public class JAXBUtilTest {
private final String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
"<school>\n" +
" <school_name>西安市第一中学</school_name>\n" +
" <school_address>西安市雁塔区长安堡一号</school_address>\n" +
" <room>\n" +
" <room_no>101</room_no>\n" +
" <room_name>101教室</room_name>\n" +
" </room>\n" +
"</school>\n";
@Test
public void beanToXmlTest() {
final SchoolVo schoolVo = new SchoolVo();
schoolVo.setSchoolName("西安市第一中学");
schoolVo.setSchoolAddress("西安市雁塔区长安堡一号");
final SchoolVo.RoomVo roomVo = new SchoolVo.RoomVo();
roomVo.setRoomName("101教室");
roomVo.setRoomNo("101");
schoolVo.setRoom(roomVo);
Assert.assertEquals(xmlStr, JAXBUtil.beanToXml(schoolVo));
}
@Test
public void xmlToBeanTest() {
final SchoolVo schoolVo = JAXBUtil.xmlToBean(xmlStr, SchoolVo.class);
Assert.assertNotNull(schoolVo);
Assert.assertEquals("西安市第一中学", schoolVo.getSchoolName());
Assert.assertEquals("西安市雁塔区长安堡一号", schoolVo.getSchoolAddress());
Assert.assertEquals("101教室", schoolVo.getRoom().getRoomName());
Assert.assertEquals("101", schoolVo.getRoom().getRoomNo());
}
@XmlRootElement(name = "school")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder={"schoolName", "schoolAddress", "room"})
public static class SchoolVo {
@XmlElement(name = "school_name", required = true)
private String schoolName;
@XmlElement(name = "school_address", required = true)
private String schoolAddress;
@XmlElement(name = "room", required = true)
private RoomVo room;
@XmlTransient
public String getSchoolName() {
return schoolName;
}
public void setSchoolName(final String schoolName) {
this.schoolName = schoolName;
}
@XmlTransient
public String getSchoolAddress() {
return schoolAddress;
}
public void setSchoolAddress(final String schoolAddress) {
this.schoolAddress = schoolAddress;
}
@XmlTransient
public RoomVo getRoom() {
return room;
}
public void setRoom(final RoomVo room) {
this.room = room;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder={"roomNo", "roomName"})
public static final class RoomVo {
@XmlElement(name = "room_no", required = true)
private String roomNo;
@XmlElement(name = "room_name", required = true)
private String roomName;
@XmlTransient
public String getRoomNo() {
return roomNo;
}
public void setRoomNo(final String roomNo) {
this.roomNo = roomNo;
}
@XmlTransient
public String getRoomName() {
return roomName;
}
public void setRoomName(final String roomName) {
this.roomName = roomName;
}
}
}
}

View File

@@ -1,9 +1,9 @@
package cn.hutool.core.util;
import cn.hutool.core.clone.CloneSupport;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.exceptions.CloneRuntimeException;
import org.junit.Assert;
import org.junit.Test;
@@ -75,10 +75,19 @@ public class ObjUtilTest {
Assert.assertEquals("OK", obj2.doSomeThing());
}
static class Obj extends CloneSupport<Obj> {
static class Obj implements Cloneable {
public String doSomeThing() {
return "OK";
}
@Override
public Obj clone() {
try {
return (Obj) super.clone();
} catch (CloneNotSupportedException e) {
throw new CloneRuntimeException(e);
}
}
}
@Test

View File

@@ -1,39 +0,0 @@
package cn.hutool.core.util;
import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.io.resource.ResourceUtil;
import org.junit.Assert;
import org.junit.Test;
import javax.script.CompiledScript;
import javax.script.ScriptException;
/**
* 脚本单元测试类
*
* @author looly
*
*/
public class ScriptUtilTest {
@Test
public void compileTest() {
final CompiledScript script = ScriptUtil.compile("print('Script test!');");
try {
script.eval();
} catch (final ScriptException e) {
throw new UtilException(e);
}
}
@Test
public void evalTest() {
ScriptUtil.eval("print('Script test!');");
}
@Test
public void invokeTest() {
final Object result = ScriptUtil.invoke(ResourceUtil.readUtf8Str("filter1.js"), "filter1", 2, 1);
Assert.assertTrue((Boolean) result);
}
}

View File

@@ -1,6 +0,0 @@
function filter1(a, b) {
if (a > b) {
return a > b;
}
return false;
}