mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
update to junit5
This commit is contained in:
@@ -3,13 +3,13 @@ package cn.hutool.cron;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.lang.id.IdUtil;
|
||||
import cn.hutool.cron.pattern.CronPattern;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TaskTableTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
@Disabled
|
||||
public void toStringTest(){
|
||||
final TaskTable taskTable = new TaskTable();
|
||||
taskTable.add(IdUtil.fastUUID(), new CronPattern("*/10 * * * * *"), ()-> Console.log("Task 1"));
|
||||
|
@@ -6,8 +6,8 @@ import cn.hutool.cron.CronUtil;
|
||||
import cn.hutool.cron.TaskExecutor;
|
||||
import cn.hutool.cron.listener.TaskListener;
|
||||
import cn.hutool.cron.task.Task;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* 定时任务样例
|
||||
@@ -15,7 +15,7 @@ import org.junit.Test;
|
||||
public class CronTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
@Disabled
|
||||
public void customCronTest() {
|
||||
CronUtil.schedule("*/2 * * * * *", (Task) () -> Console.log("Task excuted."));
|
||||
|
||||
@@ -28,7 +28,7 @@ public class CronTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
@Disabled
|
||||
public void cronTest() {
|
||||
// 支持秒级别定时任务
|
||||
CronUtil.setMatchSecond(true);
|
||||
@@ -40,7 +40,7 @@ public class CronTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
@Disabled
|
||||
public void cronWithListenerTest() {
|
||||
CronUtil.getScheduler().addListener(new TaskListener() {
|
||||
@Override
|
||||
@@ -68,7 +68,7 @@ public class CronTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
@Disabled
|
||||
public void addAndRemoveTest() {
|
||||
final String id = CronUtil.schedule("*/2 * * * * *", (Runnable) () -> Console.log("task running : 2s"));
|
||||
|
||||
|
@@ -1,26 +1,26 @@
|
||||
package cn.hutool.cron.pattern;
|
||||
|
||||
import cn.hutool.cron.CronException;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class CronPatternBuilderTest {
|
||||
|
||||
@Test
|
||||
public void buildMatchAllTest(){
|
||||
String build = CronPatternBuilder.of().build();
|
||||
Assert.assertEquals("* * * * *", build);
|
||||
Assertions.assertEquals("* * * * *", build);
|
||||
|
||||
build = CronPatternBuilder.of()
|
||||
.set(Part.SECOND, "*")
|
||||
.build();
|
||||
Assert.assertEquals("* * * * * *", build);
|
||||
Assertions.assertEquals("* * * * * *", build);
|
||||
|
||||
build = CronPatternBuilder.of()
|
||||
.set(Part.SECOND, "*")
|
||||
.set(Part.YEAR, "*")
|
||||
.build();
|
||||
Assert.assertEquals("* * * * * * *", build);
|
||||
Assertions.assertEquals("* * * * * * *", build);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -29,16 +29,18 @@ public class CronPatternBuilderTest {
|
||||
.set(Part.SECOND, "*")
|
||||
.setRange(Part.HOUR, 2, 9)
|
||||
.build();
|
||||
Assert.assertEquals("* * 2-9 * * *", build);
|
||||
Assertions.assertEquals("* * 2-9 * * *", build);
|
||||
}
|
||||
|
||||
@Test(expected = CronException.class)
|
||||
@Test
|
||||
public void buildRangeErrorTest(){
|
||||
final String build = CronPatternBuilder.of()
|
||||
Assertions.assertThrows(CronException.class, ()->{
|
||||
final String build = CronPatternBuilder.of()
|
||||
.set(Part.SECOND, "*")
|
||||
// 55无效值
|
||||
.setRange(Part.HOUR, 2, 55)
|
||||
.build();
|
||||
Assert.assertEquals("* * 2-9 * * *", build);
|
||||
Assertions.assertEquals("* * 2-9 * * *", build);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -3,8 +3,8 @@ package cn.hutool.cron.pattern;
|
||||
import cn.hutool.core.date.DateField;
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
@@ -16,35 +16,35 @@ public class CronPatternNextMatchTest {
|
||||
CronPattern pattern = new CronPattern("* * * * * * *");
|
||||
DateTime date = DateUtil.truncate(DateUtil.now(), DateField.SECOND);
|
||||
Calendar calendar = pattern.nextMatchAfter(date.toCalendar());
|
||||
Assert.assertEquals(date.getTime(), DateUtil.date(calendar).getTime());
|
||||
Assertions.assertEquals(date.getTime(), DateUtil.date(calendar).getTime());
|
||||
|
||||
// 匹配所有分,返回下一分钟
|
||||
pattern = new CronPattern("0 * * * * * *");
|
||||
date = DateUtil.parse("2022-04-08 07:44:16");
|
||||
//noinspection ConstantConditions
|
||||
calendar = pattern.nextMatchAfter(date.toCalendar());
|
||||
Assert.assertEquals(DateUtil.parse("2022-04-08 07:45:00"), DateUtil.date(calendar));
|
||||
Assertions.assertEquals(DateUtil.parse("2022-04-08 07:45:00"), DateUtil.date(calendar));
|
||||
|
||||
// 匹配所有时,返回下一小时
|
||||
pattern = new CronPattern("0 0 * * * * *");
|
||||
date = DateUtil.parse("2022-04-08 07:44:16");
|
||||
//noinspection ConstantConditions
|
||||
calendar = pattern.nextMatchAfter(date.toCalendar());
|
||||
Assert.assertEquals(DateUtil.parse("2022-04-08 08:00:00"), DateUtil.date(calendar));
|
||||
Assertions.assertEquals(DateUtil.parse("2022-04-08 08:00:00"), DateUtil.date(calendar));
|
||||
|
||||
// 匹配所有天,返回明日
|
||||
pattern = new CronPattern("0 0 0 * * * *");
|
||||
date = DateUtil.parse("2022-04-08 07:44:16");
|
||||
//noinspection ConstantConditions
|
||||
calendar = pattern.nextMatchAfter(date.toCalendar());
|
||||
Assert.assertEquals(DateUtil.parse("2022-04-09 00:00:00"), DateUtil.date(calendar));
|
||||
Assertions.assertEquals(DateUtil.parse("2022-04-09 00:00:00"), DateUtil.date(calendar));
|
||||
|
||||
// 匹配所有月,返回下一月
|
||||
pattern = new CronPattern("0 0 0 1 * * *");
|
||||
date = DateUtil.parse("2022-04-08 07:44:16");
|
||||
//noinspection ConstantConditions
|
||||
calendar = pattern.nextMatchAfter(date.toCalendar());
|
||||
Assert.assertEquals(DateUtil.parse("2022-05-01 00:00:00"), DateUtil.date(calendar));
|
||||
Assertions.assertEquals(DateUtil.parse("2022-05-01 00:00:00"), DateUtil.date(calendar));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -56,36 +56,36 @@ public class CronPatternNextMatchTest {
|
||||
Calendar calendar = pattern.nextMatchAfter(
|
||||
DateUtil.parse("2022-04-12 09:12:12").toCalendar());
|
||||
|
||||
Assert.assertTrue(pattern.match(calendar, true));
|
||||
Assert.assertEquals("2022-04-12 09:12:23", DateUtil.date(calendar).toString());
|
||||
Assertions.assertTrue(pattern.match(calendar, true));
|
||||
Assertions.assertEquals("2022-04-12 09:12:23", DateUtil.date(calendar).toString());
|
||||
|
||||
// 秒超出规定值的最大值,分+1,秒取最小值
|
||||
//noinspection ConstantConditions
|
||||
calendar = pattern.nextMatchAfter(
|
||||
DateUtil.parse("2022-04-12 09:09:24").toCalendar());
|
||||
Assert.assertTrue(pattern.match(calendar, true));
|
||||
Assert.assertEquals("2022-04-12 09:12:23", DateUtil.date(calendar).toString());
|
||||
Assertions.assertTrue(pattern.match(calendar, true));
|
||||
Assertions.assertEquals("2022-04-12 09:12:23", DateUtil.date(calendar).toString());
|
||||
|
||||
// 秒超出规定值的最大值,分不变,小时+1,秒和分使用最小值
|
||||
//noinspection ConstantConditions
|
||||
calendar = pattern.nextMatchAfter(
|
||||
DateUtil.parse("2022-04-12 09:12:24").toCalendar());
|
||||
Assert.assertTrue(pattern.match(calendar, true));
|
||||
Assert.assertEquals("2022-04-12 10:12:23", DateUtil.date(calendar).toString());
|
||||
Assertions.assertTrue(pattern.match(calendar, true));
|
||||
Assertions.assertEquals("2022-04-12 10:12:23", DateUtil.date(calendar).toString());
|
||||
|
||||
// 天超出规定值的最大值,月+1,天、时、分、秒取最小值
|
||||
//noinspection ConstantConditions
|
||||
calendar = pattern.nextMatchAfter(
|
||||
DateUtil.parse("2022-04-13 09:12:24").toCalendar());
|
||||
Assert.assertTrue(pattern.match(calendar, true));
|
||||
Assert.assertEquals("2022-05-12 00:12:23", DateUtil.date(calendar).toString());
|
||||
Assertions.assertTrue(pattern.match(calendar, true));
|
||||
Assertions.assertEquals("2022-05-12 00:12:23", DateUtil.date(calendar).toString());
|
||||
|
||||
// 跨年
|
||||
//noinspection ConstantConditions
|
||||
calendar = pattern.nextMatchAfter(
|
||||
DateUtil.parse("2021-12-22 00:00:00").toCalendar());
|
||||
Assert.assertTrue(pattern.match(calendar, true));
|
||||
Assert.assertEquals("2022-01-12 00:12:23", DateUtil.date(calendar).toString());
|
||||
Assertions.assertTrue(pattern.match(calendar, true));
|
||||
Assertions.assertEquals("2022-01-12 00:12:23", DateUtil.date(calendar).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -95,6 +95,6 @@ public class CronPatternNextMatchTest {
|
||||
final DateTime time = DateUtil.parse("2022-04-03");
|
||||
assert time != null;
|
||||
final Calendar calendar = pattern.nextMatchAfter(time.toCalendar());
|
||||
Assert.assertEquals("2022-04-09 01:01:01", DateUtil.date(calendar).toString());
|
||||
Assertions.assertEquals("2022-04-09 01:01:01", DateUtil.date(calendar).toString());
|
||||
}
|
||||
}
|
||||
|
@@ -2,8 +2,8 @@ package cn.hutool.cron.pattern;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.cron.CronException;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* 定时任务单元测试类
|
||||
@@ -103,11 +103,11 @@ public class CronPatternTest {
|
||||
@Test
|
||||
public void CronPatternTest2() {
|
||||
CronPattern pattern = CronPattern.of("0/30 * * * *");
|
||||
Assert.assertTrue(pattern.match(DateUtil.parse("2018-10-09 12:00:00").getTime(), false));
|
||||
Assert.assertTrue(pattern.match(DateUtil.parse("2018-10-09 12:30:00").getTime(), false));
|
||||
Assertions.assertTrue(pattern.match(DateUtil.parse("2018-10-09 12:00:00").getTime(), false));
|
||||
Assertions.assertTrue(pattern.match(DateUtil.parse("2018-10-09 12:30:00").getTime(), false));
|
||||
|
||||
pattern = CronPattern.of("32 * * * *");
|
||||
Assert.assertTrue(pattern.match(DateUtil.parse("2018-10-09 12:32:00").getTime(), false));
|
||||
Assertions.assertTrue(pattern.match(DateUtil.parse("2018-10-09 12:32:00").getTime(), false));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -159,10 +159,12 @@ public class CronPatternTest {
|
||||
assertMatch(pattern, "2017-12-02 23:59:59");
|
||||
}
|
||||
|
||||
@Test(expected = CronException.class)
|
||||
@Test
|
||||
public void rangeYearTest() {
|
||||
// year的范围是1970~2099年,超出报错
|
||||
CronPattern.of("0/1 * * * 1/1 ? 2020-2120");
|
||||
Assertions.assertThrows(CronException.class, ()->{
|
||||
// year的范围是1970~2099年,超出报错
|
||||
CronPattern.of("0/1 * * * 1/1 ? 2020-2120");
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -173,7 +175,7 @@ public class CronPatternTest {
|
||||
*/
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
private void assertMatch(final CronPattern pattern, final String date) {
|
||||
Assert.assertTrue(pattern.match(DateUtil.parse(date).toCalendar(), false));
|
||||
Assert.assertTrue(pattern.match(DateUtil.parse(date).toCalendar(), true));
|
||||
Assertions.assertTrue(pattern.match(DateUtil.parse(date).toCalendar(), false));
|
||||
Assertions.assertTrue(pattern.match(DateUtil.parse(date).toCalendar(), true));
|
||||
}
|
||||
}
|
||||
|
@@ -1,8 +1,8 @@
|
||||
package cn.hutool.cron.pattern;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
@@ -13,35 +13,35 @@ public class CronPatternUtilTest {
|
||||
public void matchedDatesTest() {
|
||||
//测试每30秒执行
|
||||
final List<Date> matchedDates = CronPatternUtil.matchedDates("0/30 * 8-18 * * ?", DateUtil.parse("2018-10-15 14:33:22"), 5, true);
|
||||
Assert.assertEquals(5, matchedDates.size());
|
||||
Assert.assertEquals("2018-10-15 14:33:30", matchedDates.get(0).toString());
|
||||
Assert.assertEquals("2018-10-15 14:34:00", matchedDates.get(1).toString());
|
||||
Assert.assertEquals("2018-10-15 14:34:30", matchedDates.get(2).toString());
|
||||
Assert.assertEquals("2018-10-15 14:35:00", matchedDates.get(3).toString());
|
||||
Assert.assertEquals("2018-10-15 14:35:30", matchedDates.get(4).toString());
|
||||
Assertions.assertEquals(5, matchedDates.size());
|
||||
Assertions.assertEquals("2018-10-15 14:33:30", matchedDates.get(0).toString());
|
||||
Assertions.assertEquals("2018-10-15 14:34:00", matchedDates.get(1).toString());
|
||||
Assertions.assertEquals("2018-10-15 14:34:30", matchedDates.get(2).toString());
|
||||
Assertions.assertEquals("2018-10-15 14:35:00", matchedDates.get(3).toString());
|
||||
Assertions.assertEquals("2018-10-15 14:35:30", matchedDates.get(4).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchedDatesTest2() {
|
||||
//测试每小时执行
|
||||
final List<Date> matchedDates = CronPatternUtil.matchedDates("0 0 */1 * * *", DateUtil.parse("2018-10-15 14:33:22"), 5, true);
|
||||
Assert.assertEquals(5, matchedDates.size());
|
||||
Assert.assertEquals("2018-10-15 15:00:00", matchedDates.get(0).toString());
|
||||
Assert.assertEquals("2018-10-15 16:00:00", matchedDates.get(1).toString());
|
||||
Assert.assertEquals("2018-10-15 17:00:00", matchedDates.get(2).toString());
|
||||
Assert.assertEquals("2018-10-15 18:00:00", matchedDates.get(3).toString());
|
||||
Assert.assertEquals("2018-10-15 19:00:00", matchedDates.get(4).toString());
|
||||
Assertions.assertEquals(5, matchedDates.size());
|
||||
Assertions.assertEquals("2018-10-15 15:00:00", matchedDates.get(0).toString());
|
||||
Assertions.assertEquals("2018-10-15 16:00:00", matchedDates.get(1).toString());
|
||||
Assertions.assertEquals("2018-10-15 17:00:00", matchedDates.get(2).toString());
|
||||
Assertions.assertEquals("2018-10-15 18:00:00", matchedDates.get(3).toString());
|
||||
Assertions.assertEquals("2018-10-15 19:00:00", matchedDates.get(4).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchedDatesTest3() {
|
||||
//测试最后一天
|
||||
final List<Date> matchedDates = CronPatternUtil.matchedDates("0 0 */1 L * *", DateUtil.parse("2018-10-30 23:33:22"), 5, true);
|
||||
Assert.assertEquals(5, matchedDates.size());
|
||||
Assert.assertEquals("2018-10-31 00:00:00", matchedDates.get(0).toString());
|
||||
Assert.assertEquals("2018-10-31 01:00:00", matchedDates.get(1).toString());
|
||||
Assert.assertEquals("2018-10-31 02:00:00", matchedDates.get(2).toString());
|
||||
Assert.assertEquals("2018-10-31 03:00:00", matchedDates.get(3).toString());
|
||||
Assert.assertEquals("2018-10-31 04:00:00", matchedDates.get(4).toString());
|
||||
Assertions.assertEquals(5, matchedDates.size());
|
||||
Assertions.assertEquals("2018-10-31 00:00:00", matchedDates.get(0).toString());
|
||||
Assertions.assertEquals("2018-10-31 01:00:00", matchedDates.get(1).toString());
|
||||
Assertions.assertEquals("2018-10-31 02:00:00", matchedDates.get(2).toString());
|
||||
Assertions.assertEquals("2018-10-31 03:00:00", matchedDates.get(3).toString());
|
||||
Assertions.assertEquals("2018-10-31 04:00:00", matchedDates.get(4).toString());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user