Compare commits

...

12 Commits

Author SHA1 Message Date
Looly
74040066ac add methods 2025-11-07 01:37:42 +08:00
Looly
b5a511b9da add methods 2025-11-07 01:35:19 +08:00
Looly
1204a0033e fix test 2025-11-06 16:55:26 +08:00
Looly
50072cd4e7 fix test 2025-11-06 16:53:44 +08:00
Looly
1210d57b79 change name 2025-11-06 16:50:42 +08:00
Looly
5f48b13b88 修正开闭区间错误 2025-11-06 16:48:15 +08:00
Looly
4e0d106e91 !1385 修正开闭区间错误
Merge pull request !1385 from 身有光名/v7-dev
2025-11-06 07:47:00 +00:00
zongze.lee
ebba917222 测试 equals 方法与不同引用对象比较失败 2025-11-06 03:43:41 +08:00
zongze.lee
56ffa92e5e 测试不包含起始元素的迭代
内存溢出
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at java.base/java.util.Arrays.copyOf(Arrays.java:3481)
	at java.base/java.util.ArrayList.grow(ArrayList.java:237)
	at java.base/java.util.ArrayList.grow(ArrayList.java:244)
	at java.base/java.util.ArrayList.add(ArrayList.java:454)
	at java.base/java.util.ArrayList.add(ArrayList.java:467)
	at cn.hutool.v7.core.lang.range.RangeTest.testIteratorWithoutIncludeStart(RangeTest.java:49)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
2025-11-06 02:45:40 +08:00
zongze.lee
c0461c16f6 修正测试相邻区间合并失败
对于测试用例:
range1 = [1, 3],其上界是 Bound.atMost(3)(右闭区间)
range2 = [3, 5],其下界是 Bound.atLeast(3)(左闭区间)
在比较 Bound.atMost(3).compareTo(Bound.atLeast(3)) 时:
两者的值相等(都是3)
调用 compareIfSameBoundValue 方法
bt1 = CLOSE_UPPER_BOUND (code=2)
bt2 = CLOSE_LOWER_BOUND (code=-2)
两者是错位的(一个上界一个下界)
由于 bt1.isLowerBound() 为 false(因为是上界),所以返回 -1
这导致 boundedRange.getUpperBound().compareTo(other.getLowerBound()) < 0 为 true,判定区间不相交。
但实际上,[1,3] 和 [3,5] 在点3处是相交的,因为3既包含在第一个区间又包含在第二个区间中。
2025-11-06 02:09:57 +08:00
Looly
a3a7622419 add ai to bom 2025-11-05 18:05:50 +08:00
Looly
1fac9525ab add test 2025-10-29 23:26:17 +08:00
11 changed files with 245 additions and 125 deletions

View File

@@ -95,6 +95,11 @@
<artifactId>hutool-swing</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>hutool-ai</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
@@ -147,6 +152,10 @@
<groupId>${project.parent.groupId}</groupId>
<artifactId>hutool-swing</artifactId>
</dependency>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>hutool-ai</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -191,7 +191,10 @@ class FiniteBound<T extends Comparable<? super T>> implements Bound<T> {
}
/**
* 当两个边界的值相等时,判断它们在坐标轴上位置的先后顺序
* 当两个边界的值相等时,判断它们在坐标轴上位置的先后顺序
*
* @param bound 另一边界
* @return 比较位置顺序,-1当前边界在前0重合1当前边界在后
*/
private int compareIfSameBoundValue(final Bound<T> bound) {
final BoundType bt1 = this.getType();
@@ -200,8 +203,14 @@ class FiniteBound<T extends Comparable<? super T>> implements Bound<T> {
if (bt1 == bt2) {
return 0;
}
// 一为左边界,一为右边界,则左边界恒在右边界后
// 一为左边界,一为右边界
if (bt1.isDislocated(bt2)) {
// pr#1385@Gitee 特殊情况:右闭区间与左闭区间在同一点时,认为它们重合(用于区间相交判断)
if ((bt1 == BoundType.CLOSE_UPPER_BOUND && bt2 == BoundType.CLOSE_LOWER_BOUND) ||
(bt1 == BoundType.CLOSE_LOWER_BOUND && bt2 == BoundType.CLOSE_UPPER_BOUND)) {
return 0;
}
// 一般情况:左边界恒在右边界后
return bt1.isLowerBound() ? 1 : -1;
}
// 都为左边界,则封闭边界在前,若都为右边界,则封闭边界在后

View File

@@ -165,6 +165,10 @@ public class Range<T> implements Iterable<T>, Serializable {
* @return 下一步进
*/
private T safeStep(final T base) {
// 添加边界检查
if (base == null || (base.equals(end))) {
return null;
}
final int index = this.index;
T next = null;
try {

View File

@@ -52,7 +52,8 @@ public class PhantomObj<T> extends PhantomReference<T> implements Ref<T>{
if (other == this) {
return true;
} else if (other instanceof PhantomObj) {
return ObjUtil.equals(((PhantomObj<?>) other).get(), get());
// 比较原始对象的哈希码,因为虚引用无法获取原始对象
return this.hashCode == ((PhantomObj<?>)other).hashCode;
}
return false;
}

View File

@@ -0,0 +1,16 @@
package cn.hutool.v7.core.io.file;
import cn.hutool.v7.core.io.IORuntimeException;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class Issue4121Test {
@Test
void testListFileNames_NonExistentDirectory() {
assertThrows(IORuntimeException.class, () -> {
FileUtil.listFileNames("/non/existent/path");
});
}
}

View File

@@ -16,73 +16,94 @@
package cn.hutool.v7.core.lang.range;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* test for {@link Bound}
*/
@SuppressWarnings("EqualsWithItself")
public class BoundTest {
@Test
@DisplayName("测试相邻区间合并")
void testUnionIfIntersectedWithAdjacentRanges() {
BoundedRange<Integer> range1 = BoundedRange.close(1, 3);
BoundedRange<Integer> range2 = BoundedRange.close(3, 5);
BoundedRange<Integer> result = BoundedRangeOperation.unionIfIntersected(range1, range2);
assertEquals(Bound.atLeast(1), result.getLowerBound());
assertEquals(Bound.atMost(5), result.getUpperBound());
}
@Test
void isDisjointTest(){
BoundedRange<Integer> range1 = BoundedRange.close(1, 3);
BoundedRange<Integer> range2 = BoundedRange.close(3, 5);
// 点重合,相交
assertFalse(range1.isDisjoint(range2));
}
@Test
public void testEquals() {
final Bound<Integer> bound = new FiniteBound<>(1, BoundType.OPEN_UPPER_BOUND);
Assertions.assertEquals(bound, bound);
Assertions.assertEquals(new FiniteBound<>(1, BoundType.OPEN_UPPER_BOUND), bound);
Assertions.assertNotEquals(new FiniteBound<>(2, BoundType.OPEN_UPPER_BOUND), bound);
Assertions.assertNotEquals(new FiniteBound<>(1, BoundType.OPEN_LOWER_BOUND), bound);
Assertions.assertNotEquals(null, bound);
assertEquals(bound, bound);
assertEquals(new FiniteBound<>(1, BoundType.OPEN_UPPER_BOUND), bound);
assertNotEquals(new FiniteBound<>(2, BoundType.OPEN_UPPER_BOUND), bound);
assertNotEquals(new FiniteBound<>(1, BoundType.OPEN_LOWER_BOUND), bound);
assertNotEquals(null, bound);
}
@Test
public void testHashCode() {
final int hashCode = new FiniteBound<>(1, BoundType.OPEN_UPPER_BOUND).hashCode();
Assertions.assertEquals(hashCode, new FiniteBound<>(1, BoundType.OPEN_UPPER_BOUND).hashCode());
Assertions.assertNotEquals(hashCode, new FiniteBound<>(2, BoundType.OPEN_UPPER_BOUND).hashCode());
Assertions.assertNotEquals(hashCode, new FiniteBound<>(1, BoundType.OPEN_LOWER_BOUND).hashCode());
assertEquals(hashCode, new FiniteBound<>(1, BoundType.OPEN_UPPER_BOUND).hashCode());
assertNotEquals(hashCode, new FiniteBound<>(2, BoundType.OPEN_UPPER_BOUND).hashCode());
assertNotEquals(hashCode, new FiniteBound<>(1, BoundType.OPEN_LOWER_BOUND).hashCode());
}
@Test
public void testNoneLowerBound() {
final Bound<Integer> bound = Bound.noneLowerBound();
// negate
Assertions.assertEquals(bound, bound.negate());
assertEquals(bound, bound.negate());
// test
Assertions.assertTrue(bound.test(Integer.MAX_VALUE));
assertTrue(bound.test(Integer.MAX_VALUE));
// getType
Assertions.assertEquals(BoundType.OPEN_LOWER_BOUND, bound.getType());
assertEquals(BoundType.OPEN_LOWER_BOUND, bound.getType());
// getValue
Assertions.assertNull(bound.getValue());
assertNull(bound.getValue());
// toString
Assertions.assertEquals("(" + "-∞", bound.descBound());
assertEquals("(" + "-∞", bound.descBound());
// compareTo
Assertions.assertEquals(0, bound.compareTo(bound));
Assertions.assertEquals(-1, bound.compareTo(Bound.atMost(1)));
assertEquals(0, bound.compareTo(bound));
assertEquals(-1, bound.compareTo(Bound.atMost(1)));
Assertions.assertEquals(BoundedRange.all(), bound.toRange());
Assertions.assertEquals("{x | x > -∞}", bound.toString());
assertEquals(BoundedRange.all(), bound.toRange());
assertEquals("{x | x > -∞}", bound.toString());
}
@Test
public void testNoneUpperBound() {
final Bound<Integer> bound = Bound.noneUpperBound();
// negate
Assertions.assertEquals(bound, bound.negate());
assertEquals(bound, bound.negate());
// test
Assertions.assertTrue(bound.test(Integer.MAX_VALUE));
assertTrue(bound.test(Integer.MAX_VALUE));
// getType
Assertions.assertEquals(BoundType.OPEN_UPPER_BOUND, bound.getType());
assertEquals(BoundType.OPEN_UPPER_BOUND, bound.getType());
// getValue
Assertions.assertNull(bound.getValue());
assertNull(bound.getValue());
// toString
Assertions.assertEquals("+∞" + ")", bound.descBound());
assertEquals("+∞" + ")", bound.descBound());
// compareTo
Assertions.assertEquals(0, bound.compareTo(bound));
Assertions.assertEquals(1, bound.compareTo(Bound.atMost(1)));
assertEquals(0, bound.compareTo(bound));
assertEquals(1, bound.compareTo(Bound.atMost(1)));
Assertions.assertEquals(BoundedRange.all(), bound.toRange());
Assertions.assertEquals("{x | x < +∞}", bound.toString());
assertEquals(BoundedRange.all(), bound.toRange());
assertEquals("{x | x < +∞}", bound.toString());
}
@Test
@@ -91,33 +112,33 @@ public class BoundTest {
Bound<Integer> bound = Bound.greaterThan(0);
// test
Assertions.assertTrue(bound.test(1));
Assertions.assertFalse(bound.test(0));
Assertions.assertFalse(bound.test(-1));
assertTrue(bound.test(1));
assertFalse(bound.test(0));
assertFalse(bound.test(-1));
// getType
Assertions.assertEquals(BoundType.OPEN_LOWER_BOUND, bound.getType());
assertEquals(BoundType.OPEN_LOWER_BOUND, bound.getType());
// getValue
Assertions.assertEquals((Integer)0, bound.getValue());
assertEquals((Integer)0, bound.getValue());
// toString
Assertions.assertEquals("(0", bound.descBound());
Assertions.assertEquals("{x | x > 0}", bound.toString());
assertEquals("(0", bound.descBound());
assertEquals("{x | x > 0}", bound.toString());
// compareTo
Assertions.assertEquals(0, bound.compareTo(bound));
Assertions.assertEquals(-1, bound.compareTo(Bound.noneUpperBound()));
Assertions.assertEquals(1, bound.compareTo(Bound.atLeast(-1)));
Assertions.assertEquals(-1, bound.compareTo(Bound.atLeast(2)));
Assertions.assertEquals(1, bound.compareTo(Bound.lessThan(0)));
Assertions.assertEquals(1, bound.compareTo(Bound.atMost(0)));
Assertions.assertEquals(-1, bound.compareTo(Bound.atMost(2)));
Assertions.assertEquals(1, bound.compareTo(Bound.noneLowerBound()));
assertEquals(0, bound.compareTo(bound));
assertEquals(-1, bound.compareTo(Bound.noneUpperBound()));
assertEquals(1, bound.compareTo(Bound.atLeast(-1)));
assertEquals(-1, bound.compareTo(Bound.atLeast(2)));
assertEquals(1, bound.compareTo(Bound.lessThan(0)));
assertEquals(1, bound.compareTo(Bound.atMost(0)));
assertEquals(-1, bound.compareTo(Bound.atMost(2)));
assertEquals(1, bound.compareTo(Bound.noneLowerBound()));
// { x | x >= 0}
bound = bound.negate();
Assertions.assertEquals((Integer)0, bound.getValue());
Assertions.assertEquals(BoundType.CLOSE_UPPER_BOUND, bound.getType());
assertEquals((Integer)0, bound.getValue());
assertEquals(BoundType.CLOSE_UPPER_BOUND, bound.getType());
Assertions.assertNotNull(bound.toRange());
assertNotNull(bound.toRange());
}
@Test
@@ -126,33 +147,34 @@ public class BoundTest {
Bound<Integer> bound = Bound.atLeast(0);
// test
Assertions.assertTrue(bound.test(1));
Assertions.assertTrue(bound.test(0));
Assertions.assertFalse(bound.test(-1));
assertTrue(bound.test(1));
assertTrue(bound.test(0));
assertFalse(bound.test(-1));
// getType
Assertions.assertEquals(BoundType.CLOSE_LOWER_BOUND, bound.getType());
assertEquals(BoundType.CLOSE_LOWER_BOUND, bound.getType());
// getValue
Assertions.assertEquals((Integer)0, bound.getValue());
assertEquals((Integer)0, bound.getValue());
// toString
Assertions.assertEquals("[0", bound.descBound());
Assertions.assertEquals("{x | x >= 0}", bound.toString());
assertEquals("[0", bound.descBound());
assertEquals("{x | x >= 0}", bound.toString());
// compareTo
Assertions.assertEquals(0, bound.compareTo(bound));
Assertions.assertEquals(-1, bound.compareTo(Bound.noneUpperBound()));
Assertions.assertEquals(1, bound.compareTo(Bound.greaterThan(-1)));
Assertions.assertEquals(-1, bound.compareTo(Bound.greaterThan(0)));
Assertions.assertEquals(1, bound.compareTo(Bound.lessThan(0)));
Assertions.assertEquals(1, bound.compareTo(Bound.atMost(0)));
Assertions.assertEquals(-1, bound.compareTo(Bound.atMost(2)));
Assertions.assertEquals(1, bound.compareTo(Bound.noneLowerBound()));
assertEquals(0, bound.compareTo(bound));
assertEquals(-1, bound.compareTo(Bound.noneUpperBound()));
assertEquals(1, bound.compareTo(Bound.greaterThan(-1)));
assertEquals(-1, bound.compareTo(Bound.greaterThan(0)));
assertEquals(1, bound.compareTo(Bound.lessThan(0)));
// pr#1385@gitee 特殊情况:右闭区间与左闭区间在同一点时,认为这个边界相等
assertEquals(0, bound.compareTo(Bound.atMost(0)));
assertEquals(-1, bound.compareTo(Bound.atMost(2)));
assertEquals(1, bound.compareTo(Bound.noneLowerBound()));
// { x | x < 0}
bound = bound.negate();
Assertions.assertEquals((Integer)0, bound.getValue());
Assertions.assertEquals(BoundType.OPEN_UPPER_BOUND, bound.getType());
assertEquals((Integer)0, bound.getValue());
assertEquals(BoundType.OPEN_UPPER_BOUND, bound.getType());
Assertions.assertNotNull(bound.toRange());
assertNotNull(bound.toRange());
}
@Test
@@ -161,33 +183,33 @@ public class BoundTest {
Bound<Integer> bound = Bound.lessThan(0);
// test
Assertions.assertFalse(bound.test(1));
Assertions.assertFalse(bound.test(0));
Assertions.assertTrue(bound.test(-1));
assertFalse(bound.test(1));
assertFalse(bound.test(0));
assertTrue(bound.test(-1));
// getType
Assertions.assertEquals(BoundType.OPEN_UPPER_BOUND, bound.getType());
assertEquals(BoundType.OPEN_UPPER_BOUND, bound.getType());
// getValue
Assertions.assertEquals((Integer)0, bound.getValue());
assertEquals((Integer)0, bound.getValue());
// toString
Assertions.assertEquals("0)", bound.descBound());
Assertions.assertEquals("{x | x < 0}", bound.toString());
assertEquals("0)", bound.descBound());
assertEquals("{x | x < 0}", bound.toString());
// compareTo
Assertions.assertEquals(0, bound.compareTo(bound));
Assertions.assertEquals(-1, bound.compareTo(Bound.noneUpperBound()));
Assertions.assertEquals(1, bound.compareTo(Bound.greaterThan(-1)));
Assertions.assertEquals(-1, bound.compareTo(Bound.greaterThan(0)));
Assertions.assertEquals(1, bound.compareTo(Bound.lessThan(-1)));
Assertions.assertEquals(-1, bound.compareTo(Bound.atMost(0)));
Assertions.assertEquals(1, bound.compareTo(Bound.atMost(-1)));
Assertions.assertEquals(1, bound.compareTo(Bound.noneLowerBound()));
assertEquals(0, bound.compareTo(bound));
assertEquals(-1, bound.compareTo(Bound.noneUpperBound()));
assertEquals(1, bound.compareTo(Bound.greaterThan(-1)));
assertEquals(-1, bound.compareTo(Bound.greaterThan(0)));
assertEquals(1, bound.compareTo(Bound.lessThan(-1)));
assertEquals(-1, bound.compareTo(Bound.atMost(0)));
assertEquals(1, bound.compareTo(Bound.atMost(-1)));
assertEquals(1, bound.compareTo(Bound.noneLowerBound()));
// { x | x >= 0}
bound = bound.negate();
Assertions.assertEquals((Integer)0, bound.getValue());
Assertions.assertEquals(BoundType.CLOSE_LOWER_BOUND, bound.getType());
assertEquals((Integer)0, bound.getValue());
assertEquals(BoundType.CLOSE_LOWER_BOUND, bound.getType());
Assertions.assertNotNull(bound.toRange());
assertNotNull(bound.toRange());
}
@Test
@@ -196,33 +218,33 @@ public class BoundTest {
Bound<Integer> bound = Bound.atMost(0);
// test
Assertions.assertFalse(bound.test(1));
Assertions.assertTrue(bound.test(0));
Assertions.assertTrue(bound.test(-1));
assertFalse(bound.test(1));
assertTrue(bound.test(0));
assertTrue(bound.test(-1));
// getType
Assertions.assertEquals(BoundType.CLOSE_UPPER_BOUND, bound.getType());
assertEquals(BoundType.CLOSE_UPPER_BOUND, bound.getType());
// getValue
Assertions.assertEquals((Integer)0, bound.getValue());
assertEquals((Integer)0, bound.getValue());
// toString
Assertions.assertEquals("0]", bound.descBound());
Assertions.assertEquals("{x | x <= 0}", bound.toString());
assertEquals("0]", bound.descBound());
assertEquals("{x | x <= 0}", bound.toString());
// compareTo
Assertions.assertEquals(0, bound.compareTo(bound));
Assertions.assertEquals(-1, bound.compareTo(Bound.noneUpperBound()));
Assertions.assertEquals(1, bound.compareTo(Bound.greaterThan(-1)));
Assertions.assertEquals(-1, bound.compareTo(Bound.greaterThan(0)));
Assertions.assertEquals(1, bound.compareTo(Bound.atMost(-1)));
Assertions.assertEquals(1, bound.compareTo(Bound.lessThan(0)));
Assertions.assertEquals(1, bound.compareTo(Bound.lessThan(-1)));
Assertions.assertEquals(1, bound.compareTo(Bound.noneLowerBound()));
assertEquals(0, bound.compareTo(bound));
assertEquals(-1, bound.compareTo(Bound.noneUpperBound()));
assertEquals(1, bound.compareTo(Bound.greaterThan(-1)));
assertEquals(-1, bound.compareTo(Bound.greaterThan(0)));
assertEquals(1, bound.compareTo(Bound.atMost(-1)));
assertEquals(1, bound.compareTo(Bound.lessThan(0)));
assertEquals(1, bound.compareTo(Bound.lessThan(-1)));
assertEquals(1, bound.compareTo(Bound.noneLowerBound()));
// { x | x > 0}
bound = bound.negate();
Assertions.assertEquals((Integer)0, bound.getValue());
Assertions.assertEquals(BoundType.OPEN_LOWER_BOUND, bound.getType());
assertEquals((Integer)0, bound.getValue());
assertEquals(BoundType.OPEN_LOWER_BOUND, bound.getType());
Assertions.assertNotNull(bound.toRange());
assertNotNull(bound.toRange());
}
}

View File

@@ -156,7 +156,7 @@ public class BoundedRangeTest {
// isXXX
Assertions.assertTrue(range.isDisjoint(BoundedRange.open(-5, 0))); // (-5, 0)
Assertions.assertTrue(range.isDisjoint(BoundedRange.close(-5, 0))); // [-5, 0]
Assertions.assertFalse(range.isDisjoint(BoundedRange.close(-5, 0))); // [-5, 0]
Assertions.assertTrue(range.isIntersected(BoundedRange.open(-5, 1))); // [-5, 1]
Assertions.assertFalse(range.isSubset(BoundedRange.open(0, 5))); // (0, 5)
Assertions.assertFalse(range.isProperSubset(BoundedRange.open(0, 5))); // (0, 5)

View File

@@ -22,13 +22,17 @@ import cn.hutool.v7.core.date.DateTime;
import cn.hutool.v7.core.date.DateUtil;
import cn.hutool.v7.core.text.StrUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* {@link Range} 单元测试
*
@@ -36,6 +40,18 @@ import java.util.NoSuchElementException;
*/
public class RangeTest {
@Test
@DisplayName("测试不包含起始元素的迭代")
void testIteratorWithoutIncludeStart() {
Range<Integer> range = new Range<>(1, 5, (current, end, index) -> current + 1, false, true);
List<Integer> elements = new ArrayList<>();
for (Integer i : range) {
elements.add(i);
}
assertEquals(List.of(2, 3, 4, 5), elements);
}
@Test
public void dateRangeTest() {
final DateTime start = DateUtil.parse("2017-01-01");
@@ -50,9 +66,9 @@ public class RangeTest {
final Iterator<DateTime> iterator = range.iterator();
Assertions.assertTrue(iterator.hasNext());
Assertions.assertEquals(DateUtil.parse("2017-01-01"), iterator.next());
assertEquals(DateUtil.parse("2017-01-01"), iterator.next());
Assertions.assertTrue(iterator.hasNext());
Assertions.assertEquals(DateUtil.parse("2017-01-02"), iterator.next());
assertEquals(DateUtil.parse("2017-01-02"), iterator.next());
Assertions.assertFalse(iterator.hasNext());
}
@@ -75,11 +91,11 @@ public class RangeTest {
final StringBuilder sb = new StringBuilder();
DateUtil.rangeConsume(start, end, DateField.DAY_OF_YEAR, a -> sb.append(DateTime.of(a).dayOfMonth()).append("#"));
Assertions.assertEquals("1#2#3#", sb.toString());
assertEquals("1#2#3#", sb.toString());
final StringBuilder sb2 = new StringBuilder();
DateUtil.rangeConsume(null, null, DateField.DAY_OF_YEAR, a -> sb2.append(DateTime.of(a).dayOfMonth()).append("#"));
Assertions.assertEquals(StrUtil.EMPTY, sb2.toString());
assertEquals(StrUtil.EMPTY, sb2.toString());
}
@Test
@@ -90,11 +106,11 @@ public class RangeTest {
final DateRange range = DateUtil.range(start, end, DateField.MONTH);
final Iterator<DateTime> iterator = range.iterator();
Assertions.assertTrue(iterator.hasNext());
Assertions.assertEquals(DateUtil.parse("2021-01-31"), iterator.next());
assertEquals(DateUtil.parse("2021-01-31"), iterator.next());
Assertions.assertTrue(iterator.hasNext());
Assertions.assertEquals(DateUtil.parse("2021-02-28"), iterator.next());
assertEquals(DateUtil.parse("2021-02-28"), iterator.next());
Assertions.assertTrue(iterator.hasNext());
Assertions.assertEquals(DateUtil.parse("2021-03-31"), iterator.next());
assertEquals(DateUtil.parse("2021-03-31"), iterator.next());
Assertions.assertFalse(iterator.hasNext());
}
@@ -104,7 +120,7 @@ public class RangeTest {
final Iterator<Integer> iterator = range.iterator();
Assertions.assertTrue(iterator.hasNext());
Assertions.assertEquals(Integer.valueOf(1), iterator.next());
assertEquals(Integer.valueOf(1), iterator.next());
Assertions.assertFalse(iterator.hasNext());
}
@@ -116,9 +132,9 @@ public class RangeTest {
// 测试包含开始和结束情况下步进为1的情况
DateRange range = DateUtil.range(start, end, DateField.DAY_OF_YEAR);
Iterator<DateTime> iterator = range.iterator();
Assertions.assertEquals(iterator.next(), DateUtil.parse("2017-01-01"));
Assertions.assertEquals(iterator.next(), DateUtil.parse("2017-01-02"));
Assertions.assertEquals(iterator.next(), DateUtil.parse("2017-01-03"));
assertEquals(iterator.next(), DateUtil.parse("2017-01-01"));
assertEquals(iterator.next(), DateUtil.parse("2017-01-02"));
assertEquals(iterator.next(), DateUtil.parse("2017-01-03"));
try {
iterator.next();
Assertions.fail("已超过边界,下一个元素不应该存在!");
@@ -128,8 +144,8 @@ public class RangeTest {
// 测试多步进的情况
range = new DateRange(start, end, DateField.DAY_OF_YEAR, 2);
iterator = range.iterator();
Assertions.assertEquals(DateUtil.parse("2017-01-01"), iterator.next());
Assertions.assertEquals(DateUtil.parse("2017-01-03"), iterator.next());
assertEquals(DateUtil.parse("2017-01-01"), iterator.next());
assertEquals(DateUtil.parse("2017-01-03"), iterator.next());
}
@Test
@@ -140,9 +156,9 @@ public class RangeTest {
// 测试不包含开始结束时间的情况
final DateRange range = new DateRange(start, end, DateField.DAY_OF_YEAR, 1, false, false);
final Iterator<DateTime> iterator = range.iterator();
Assertions.assertEquals(DateUtil.parse("2017-01-02"), iterator.next());
Assertions.assertEquals(DateUtil.parse("2017-01-03"), iterator.next());
Assertions.assertEquals(DateUtil.parse("2017-01-04"), iterator.next());
assertEquals(DateUtil.parse("2017-01-02"), iterator.next());
assertEquals(DateUtil.parse("2017-01-03"), iterator.next());
assertEquals(DateUtil.parse("2017-01-04"), iterator.next());
try {
iterator.next();
Assertions.fail("不包含结束时间情况下,下一个元素不应该存在!");
@@ -156,8 +172,8 @@ public class RangeTest {
final Date end = DateUtil.parse("2017-01-31");
final List<DateTime> rangeToList = DateUtil.rangeToList(start, end, DateField.DAY_OF_YEAR);
Assertions.assertEquals(DateUtil.parse("2017-01-01"), rangeToList.get(0));
Assertions.assertEquals(DateUtil.parse("2017-01-02"), rangeToList.get(1));
assertEquals(DateUtil.parse("2017-01-01"), rangeToList.get(0));
assertEquals(DateUtil.parse("2017-01-02"), rangeToList.get(1));
}
@@ -173,8 +189,8 @@ public class RangeTest {
final DateRange endRange = DateUtil.range(start1, end1, DateField.DAY_OF_YEAR);
// 交集
final List<DateTime> dateTimes = DateUtil.rangeContains(startRange, endRange);
Assertions.assertEquals(1, dateTimes.size());
Assertions.assertEquals(DateUtil.parse("2017-01-31"), dateTimes.get(0));
assertEquals(1, dateTimes.size());
assertEquals(DateUtil.parse("2017-01-31"), dateTimes.get(0));
}
@Test
@@ -190,8 +206,8 @@ public class RangeTest {
// 差集
final List<DateTime> dateTimes1 = DateUtil.rangeNotContains(startRange, endRange);
Assertions.assertEquals(1, dateTimes1.size());
Assertions.assertEquals(DateUtil.parse("2017-01-31"), dateTimes1.get(0));
assertEquals(1, dateTimes1.size());
assertEquals(DateUtil.parse("2017-01-31"), dateTimes1.get(0));
}
}

View File

@@ -0,0 +1,31 @@
package cn.hutool.v7.core.lang.ref;
import java.lang.ref.ReferenceQueue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class PhantomObjTest {
private ReferenceQueue<String> queue;
private String testObject;
private PhantomObj<String> phantomObj;
@BeforeEach
void setUp() {
queue = new ReferenceQueue<>();
testObject = "test";
phantomObj = new PhantomObj<>(testObject, queue);
}
@Test
@DisplayName("测试 equals 方法与不同引用对象比较")
void testEqualsWithDifferentReferent() {
String differentObject = "different";
PhantomObj<String> anotherPhantomObj = new PhantomObj<>(differentObject, queue);
assertFalse(phantomObj.equals(anotherPhantomObj));
}
}

View File

@@ -45,6 +45,18 @@ import java.util.Map;
*/
public class HttpDownloader {
/**
* 创建下载器,使用自定义引擎<br>
* 自定义引擎使用完毕后不会关闭
*
* @param engine 引擎
* @param url 请求地址
* @return 下载器
*/
public static HttpDownloader of(ClientEngine engine, String url) {
return of(url).setEngine(engine).setCloseEngine(false);
}
/**
* 创建下载器
*