Merge remote-tracking branch 'origin/v6-dev' into v6-dev

# Conflicts:
#	hutool-core/src/test/java/cn/hutool/core/stream/AbstractEnhancedWrappedStreamTest.java
This commit is contained in:
VampireAchao
2022-10-17 16:49:39 +08:00
62 changed files with 2846 additions and 404 deletions

View File

@@ -575,6 +575,21 @@ public class BeanUtilTest {
Assert.assertNull(BeanUtil.copyProperties(null, Food.class));
}
@Test
public void copyPropertiesMapToMapIgnoreNullTest() {
// 测试MapToMap
final Map<String, Object> p1 = new HashMap<>();
p1.put("isSlow", true);
p1.put("name", "测试");
p1.put("subName", null);
final Map<String, Object> map = MapUtil.newHashMap();
BeanUtil.copyProperties(p1, map, CopyOptions.of().setIgnoreNullValue(true));
Assert.assertTrue((Boolean) map.get("isSlow"));
Assert.assertEquals("测试", map.get("name"));
Assert.assertFalse(map.containsKey("subName"));
}
@Test
public void copyBeanPropertiesFilterTest() {
final Food info = new Food();

View File

@@ -0,0 +1,55 @@
package cn.hutool.core.bean;
import cn.hutool.core.date.StopWatch;
import cn.hutool.core.lang.Console;
import lombok.Data;
import org.junit.Ignore;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class Issue2649Test {
@Test
@Ignore
public void toListTest() {
final List<View1> view1List = new ArrayList<>();
for (int i = 0; i < 200; i++) {
final View1 view1 = new View1();
view1.setA(String.valueOf(i));
view1.setB(String.valueOf(i));
for (int j = 0; j < 2; j++) {
final View2 view2 = new View2();
view1.setA(String.valueOf(i));
view1.setB(String.valueOf(i));
view1.getViewList().add(view2);
}
view1List.add(view1);
}
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
for (int i = 0; i < 50; i++) {
@SuppressWarnings("unused")
final List<View2> view2s = BeanUtil.copyToList(view1List, View2.class);
}
stopWatch.stop();
Console.log(stopWatch.getTotalTimeSeconds());
}
@Data
static class View1{
private String a;
private String b;
private List<View2> viewList = new ArrayList<>();
}
@Data
static class View2{
private String a;
private String b;
private List<View2> viewList = new ArrayList<>();
}
}

View File

@@ -1,6 +1,7 @@
package cn.hutool.core.cache;
import cn.hutool.core.cache.impl.LRUCache;
import cn.hutool.core.text.StrUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import org.junit.Assert;
@@ -8,6 +9,7 @@ import org.junit.Ignore;
import org.junit.Test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 见:<a href="https://github.com/dromara/hutool/issues/1895">https://github.com/dromara/hutool/issues/1895</a><br>
@@ -64,4 +66,23 @@ public class LRUCacheTest {
}
Assert.assertEquals("null123456789", sb2.toString());
}
@Test
public void issue2647Test(){
final AtomicInteger removeCount = new AtomicInteger();
final LRUCache<String, Integer> cache = CacheUtil.newLRUCache(3,1);
cache.setListener((key, value) -> {
// 共移除7次
removeCount.incrementAndGet();
//Console.log("Start remove k-v, key:{}, value:{}", key, value);
});
for (int i = 0; i < 10; i++) {
cache.put(StrUtil.format("key-{}", i), i);
}
Assert.assertEquals(7, removeCount.get());
Assert.assertEquals(3, cache.size());
}
}

View File

@@ -12,4 +12,10 @@ public class FileNameUtilTest {
name = FileNameUtil.cleanInvalid("\r1\r\n2\n");
Assert.assertEquals("12", name);
}
@Test
public void mainNameTest() {
final String s = FileNameUtil.mainName("abc.tar.gz");
Assert.assertEquals("abc", s);
}
}

View File

@@ -0,0 +1,212 @@
package cn.hutool.core.lang.range;
import org.junit.Assert;
import org.junit.Test;
/**
* test for {@link Bound}
*/
@SuppressWarnings("EqualsWithItself")
public class BoundTest {
@Test
public void testEquals() {
final Bound<Integer> bound = new FiniteBound<>(1, BoundType.OPEN_UPPER_BOUND);
Assert.assertEquals(bound, bound);
Assert.assertEquals(bound, new FiniteBound<>(1, BoundType.OPEN_UPPER_BOUND));
Assert.assertNotEquals(bound, new FiniteBound<>(2, BoundType.OPEN_UPPER_BOUND));
Assert.assertNotEquals(bound, new FiniteBound<>(1, BoundType.OPEN_LOWER_BOUND));
Assert.assertNotEquals(bound, null);
}
@Test
public void testHashCode() {
final int hashCode = new FiniteBound<>(1, BoundType.OPEN_UPPER_BOUND).hashCode();
Assert.assertEquals(hashCode, new FiniteBound<>(1, BoundType.OPEN_UPPER_BOUND).hashCode());
Assert.assertNotEquals(hashCode, new FiniteBound<>(2, BoundType.OPEN_UPPER_BOUND).hashCode());
Assert.assertNotEquals(hashCode, new FiniteBound<>(1, BoundType.OPEN_LOWER_BOUND).hashCode());
}
@Test
public void testNoneLowerBound() {
final Bound<Integer> bound = Bound.noneLowerBound();
// negate
Assert.assertEquals(bound, bound.negate());
// test
Assert.assertTrue(bound.test(Integer.MAX_VALUE));
// getType
Assert.assertEquals(BoundType.OPEN_LOWER_BOUND, bound.getType());
// getValue
Assert.assertNull(bound.getValue());
// toString
Assert.assertEquals("(" + "-\u221e", bound.descBound());
// compareTo
Assert.assertEquals(0, bound.compareTo(bound));
Assert.assertEquals(-1, bound.compareTo(Bound.atMost(1)));
Assert.assertEquals(BoundedRange.all(), bound.toRange());
Assert.assertEquals("{x | x > -\u221e}", bound.toString());
}
@Test
public void testNoneUpperBound() {
final Bound<Integer> bound = Bound.noneUpperBound();
// negate
Assert.assertEquals(bound, bound.negate());
// test
Assert.assertTrue(bound.test(Integer.MAX_VALUE));
// getType
Assert.assertEquals(BoundType.OPEN_UPPER_BOUND, bound.getType());
// getValue
Assert.assertNull(bound.getValue());
// toString
Assert.assertEquals("+\u221e" + ")", bound.descBound());
// compareTo
Assert.assertEquals(0, bound.compareTo(bound));
Assert.assertEquals(1, bound.compareTo(Bound.atMost(1)));
Assert.assertEquals(BoundedRange.all(), bound.toRange());
Assert.assertEquals("{x | x < +\u221e}", bound.toString());
}
@Test
public void testGreatThan() {
// { x | x > 0}
Bound<Integer> bound = Bound.greaterThan(0);
// test
Assert.assertTrue(bound.test(1));
Assert.assertFalse(bound.test(0));
Assert.assertFalse(bound.test(-1));
// getType
Assert.assertEquals(BoundType.OPEN_LOWER_BOUND, bound.getType());
// getValue
Assert.assertEquals((Integer)0, bound.getValue());
// toString
Assert.assertEquals("(0", bound.descBound());
Assert.assertEquals("{x | x > 0}", bound.toString());
// compareTo
Assert.assertEquals(0, bound.compareTo(bound));
Assert.assertEquals(-1, bound.compareTo(Bound.noneUpperBound()));
Assert.assertEquals(1, bound.compareTo(Bound.atLeast(-1)));
Assert.assertEquals(-1, bound.compareTo(Bound.atLeast(2)));
Assert.assertEquals(1, bound.compareTo(Bound.lessThan(0)));
Assert.assertEquals(1, bound.compareTo(Bound.atMost(0)));
Assert.assertEquals(-1, bound.compareTo(Bound.atMost(2)));
Assert.assertEquals(1, bound.compareTo(Bound.noneLowerBound()));
// { x | x >= 0}
bound = bound.negate();
Assert.assertEquals((Integer)0, bound.getValue());
Assert.assertEquals(BoundType.CLOSE_UPPER_BOUND, bound.getType());
Assert.assertNotNull(bound.toRange());
}
@Test
public void testAtLeast() {
// { x | x >= 0}
Bound<Integer> bound = Bound.atLeast(0);
// test
Assert.assertTrue(bound.test(1));
Assert.assertTrue(bound.test(0));
Assert.assertFalse(bound.test(-1));
// getType
Assert.assertEquals(BoundType.CLOSE_LOWER_BOUND, bound.getType());
// getValue
Assert.assertEquals((Integer)0, bound.getValue());
// toString
Assert.assertEquals("[0", bound.descBound());
Assert.assertEquals("{x | x >= 0}", bound.toString());
// compareTo
Assert.assertEquals(0, bound.compareTo(bound));
Assert.assertEquals(-1, bound.compareTo(Bound.noneUpperBound()));
Assert.assertEquals(1, bound.compareTo(Bound.greaterThan(-1)));
Assert.assertEquals(-1, bound.compareTo(Bound.greaterThan(0)));
Assert.assertEquals(1, bound.compareTo(Bound.lessThan(0)));
Assert.assertEquals(1, bound.compareTo(Bound.atMost(0)));
Assert.assertEquals(-1, bound.compareTo(Bound.atMost(2)));
Assert.assertEquals(1, bound.compareTo(Bound.noneLowerBound()));
// { x | x < 0}
bound = bound.negate();
Assert.assertEquals((Integer)0, bound.getValue());
Assert.assertEquals(BoundType.OPEN_UPPER_BOUND, bound.getType());
Assert.assertNotNull(bound.toRange());
}
@Test
public void testLessThan() {
// { x | x < 0}
Bound<Integer> bound = Bound.lessThan(0);
// test
Assert.assertFalse(bound.test(1));
Assert.assertFalse(bound.test(0));
Assert.assertTrue(bound.test(-1));
// getType
Assert.assertEquals(BoundType.OPEN_UPPER_BOUND, bound.getType());
// getValue
Assert.assertEquals((Integer)0, bound.getValue());
// toString
Assert.assertEquals("0)", bound.descBound());
Assert.assertEquals("{x | x < 0}", bound.toString());
// compareTo
Assert.assertEquals(0, bound.compareTo(bound));
Assert.assertEquals(-1, bound.compareTo(Bound.noneUpperBound()));
Assert.assertEquals(1, bound.compareTo(Bound.greaterThan(-1)));
Assert.assertEquals(-1, bound.compareTo(Bound.greaterThan(0)));
Assert.assertEquals(1, bound.compareTo(Bound.lessThan(-1)));
Assert.assertEquals(-1, bound.compareTo(Bound.atMost(0)));
Assert.assertEquals(1, bound.compareTo(Bound.atMost(-1)));
Assert.assertEquals(1, bound.compareTo(Bound.noneLowerBound()));
// { x | x >= 0}
bound = bound.negate();
Assert.assertEquals((Integer)0, bound.getValue());
Assert.assertEquals(BoundType.CLOSE_LOWER_BOUND, bound.getType());
Assert.assertNotNull(bound.toRange());
}
@Test
public void testAtMost() {
// { x | x <= 0}
Bound<Integer> bound = Bound.atMost(0);
// test
Assert.assertFalse(bound.test(1));
Assert.assertTrue(bound.test(0));
Assert.assertTrue(bound.test(-1));
// getType
Assert.assertEquals(BoundType.CLOSE_UPPER_BOUND, bound.getType());
// getValue
Assert.assertEquals((Integer)0, bound.getValue());
// toString
Assert.assertEquals("0]", bound.descBound());
Assert.assertEquals("{x | x <= 0}", bound.toString());
// compareTo
Assert.assertEquals(0, bound.compareTo(bound));
Assert.assertEquals(-1, bound.compareTo(Bound.noneUpperBound()));
Assert.assertEquals(1, bound.compareTo(Bound.greaterThan(-1)));
Assert.assertEquals(-1, bound.compareTo(Bound.greaterThan(0)));
Assert.assertEquals(1, bound.compareTo(Bound.atMost(-1)));
Assert.assertEquals(1, bound.compareTo(Bound.lessThan(0)));
Assert.assertEquals(1, bound.compareTo(Bound.lessThan(-1)));
Assert.assertEquals(1, bound.compareTo(Bound.noneLowerBound()));
// { x | x > 0}
bound = bound.negate();
Assert.assertEquals((Integer)0, bound.getValue());
Assert.assertEquals(BoundType.OPEN_LOWER_BOUND, bound.getType());
Assert.assertNotNull(bound.toRange());
}
}

View File

@@ -0,0 +1,83 @@
package cn.hutool.core.lang.range;
import org.junit.Assert;
import org.junit.Test;
/**
* test for {@link BoundType}
*/
public class BoundTypeTest {
@Test
public void testIsDislocated() {
Assert.assertTrue(BoundType.CLOSE_LOWER_BOUND.isDislocated(BoundType.CLOSE_UPPER_BOUND));
Assert.assertTrue(BoundType.CLOSE_LOWER_BOUND.isDislocated(BoundType.OPEN_UPPER_BOUND));
Assert.assertFalse(BoundType.CLOSE_LOWER_BOUND.isDislocated(BoundType.CLOSE_LOWER_BOUND));
Assert.assertFalse(BoundType.CLOSE_LOWER_BOUND.isDislocated(BoundType.OPEN_LOWER_BOUND));
}
@Test
public void testIsLowerBound() {
Assert.assertFalse(BoundType.CLOSE_UPPER_BOUND.isLowerBound());
Assert.assertFalse(BoundType.OPEN_UPPER_BOUND.isLowerBound());
Assert.assertTrue(BoundType.CLOSE_LOWER_BOUND.isLowerBound());
Assert.assertTrue(BoundType.OPEN_LOWER_BOUND.isLowerBound());
}
@Test
public void testIsUpperBound() {
Assert.assertTrue(BoundType.CLOSE_UPPER_BOUND.isUpperBound());
Assert.assertTrue(BoundType.OPEN_UPPER_BOUND.isUpperBound());
Assert.assertFalse(BoundType.CLOSE_LOWER_BOUND.isUpperBound());
Assert.assertFalse(BoundType.OPEN_LOWER_BOUND.isUpperBound());
}
@Test
public void testIsOpen() {
Assert.assertFalse(BoundType.CLOSE_UPPER_BOUND.isOpen());
Assert.assertTrue(BoundType.OPEN_UPPER_BOUND.isOpen());
Assert.assertFalse(BoundType.CLOSE_LOWER_BOUND.isOpen());
Assert.assertTrue(BoundType.OPEN_LOWER_BOUND.isOpen());
}
@Test
public void testIsClose() {
Assert.assertTrue(BoundType.CLOSE_UPPER_BOUND.isClose());
Assert.assertFalse(BoundType.OPEN_UPPER_BOUND.isClose());
Assert.assertTrue(BoundType.CLOSE_LOWER_BOUND.isClose());
Assert.assertFalse(BoundType.OPEN_LOWER_BOUND.isClose());
}
@Test
public void testNegate() {
Assert.assertEquals(BoundType.CLOSE_UPPER_BOUND, BoundType.OPEN_LOWER_BOUND.negate());
Assert.assertEquals(BoundType.OPEN_UPPER_BOUND, BoundType.CLOSE_LOWER_BOUND.negate());
Assert.assertEquals(BoundType.OPEN_LOWER_BOUND, BoundType.CLOSE_UPPER_BOUND.negate());
Assert.assertEquals(BoundType.CLOSE_LOWER_BOUND, BoundType.OPEN_UPPER_BOUND.negate());
}
@Test
public void testGetSymbol() {
Assert.assertEquals("]", BoundType.CLOSE_UPPER_BOUND.getSymbol());
Assert.assertEquals(")", BoundType.OPEN_UPPER_BOUND.getSymbol());
Assert.assertEquals("[", BoundType.CLOSE_LOWER_BOUND.getSymbol());
Assert.assertEquals("(", BoundType.OPEN_LOWER_BOUND.getSymbol());
}
@Test
public void testGetCode() {
Assert.assertEquals(2, BoundType.CLOSE_UPPER_BOUND.getCode());
Assert.assertEquals(1, BoundType.OPEN_UPPER_BOUND.getCode());
Assert.assertEquals(-1, BoundType.OPEN_LOWER_BOUND.getCode());
Assert.assertEquals(-2, BoundType.CLOSE_LOWER_BOUND.getCode());
}
@Test
public void testGetOperator() {
Assert.assertEquals("<=", BoundType.CLOSE_UPPER_BOUND.getOperator());
Assert.assertEquals("<", BoundType.OPEN_UPPER_BOUND.getOperator());
Assert.assertEquals(">", BoundType.OPEN_LOWER_BOUND.getOperator());
Assert.assertEquals(">=", BoundType.CLOSE_LOWER_BOUND.getOperator());
}
}

View File

@@ -0,0 +1,283 @@
package cn.hutool.core.lang.range;
import org.junit.Assert;
import org.junit.Test;
/**
* test for {@link BoundedRange}
*/
public class BoundedRangeTest {
@Test
public void testEquals() {
final BoundedRange<Integer> range = new BoundedRange<>(
Bound.greaterThan(0), Bound.lessThan(10)
);
Assert.assertEquals(range, range);
Assert.assertNotEquals(range, null);
Assert.assertEquals(range, new BoundedRange<>(
Bound.greaterThan(0), Bound.lessThan(10)
));
Assert.assertNotEquals(range, new BoundedRange<>(
Bound.greaterThan(1), Bound.lessThan(10)
));
}
@Test
public void testHashCode() {
final int hasCode = new BoundedRange<>(
Bound.greaterThan(0), Bound.lessThan(10)
).hashCode();
Assert.assertEquals(hasCode, new BoundedRange<>(
Bound.greaterThan(0), Bound.lessThan(10)
).hashCode());
Assert.assertNotEquals(hasCode, new BoundedRange<>(
Bound.greaterThan(1), Bound.lessThan(10)
).hashCode());
}
@Test
public void testAll() {
final BoundedRange<Integer> range = BoundedRange.all();
Assert.assertEquals("(-∞, +∞)", range.toString());
// getBound
Assert.assertFalse(range.hasLowerBound());
Assert.assertFalse(range.hasUpperBound());
Assert.assertNull(range.getLowerBoundValue());
Assert.assertNull(range.getUpperBoundValue());
// test
Assert.assertFalse(range.isEmpty());
Assert.assertTrue(range.test(Integer.MAX_VALUE));
Assert.assertTrue(range.test(Integer.MIN_VALUE));
// isXXX
Assert.assertFalse(range.isDisjoint(BoundedRange.open(0, 5)));
Assert.assertEquals(range, range);
Assert.assertNotEquals(range, BoundedRange.open(0, 5));
Assert.assertTrue(range.isIntersected(BoundedRange.open(0, 5)));
Assert.assertTrue(range.isIntersected(range));
Assert.assertFalse(range.isSubset(BoundedRange.open(0, 5)));
Assert.assertTrue(range.isSubset(range));
Assert.assertFalse(range.isProperSubset(BoundedRange.open(0, 5)));
Assert.assertFalse(range.isProperSubset(range));
Assert.assertTrue(range.isSuperset(BoundedRange.open(0, 5)));
Assert.assertTrue(range.isSuperset(range));
Assert.assertTrue(range.isProperSuperset(BoundedRange.open(0, 5)));
Assert.assertFalse(range.isProperSuperset(range));
// operate
Assert.assertEquals(range, range.unionIfIntersected(BoundedRange.open(0, 5)));
Assert.assertEquals(range, range.span(BoundedRange.open(0, 5)));
Assert.assertNull(range.gap(BoundedRange.open(0, 5)));
Assert.assertEquals(range, range.intersection(range));
Assert.assertEquals(BoundedRange.open(0, 5), range.intersection(BoundedRange.open(0, 5)));
// sub
Assert.assertEquals("(0, +∞)", range.subGreatThan(0).toString());
Assert.assertEquals("[0, +∞)", range.subAtLeast(0).toString());
Assert.assertEquals("(-∞, 0)", range.subLessThan(0).toString());
Assert.assertEquals("(-∞, 0]", range.subAtMost(0).toString());
}
@Test
public void testOpen() {
final BoundedRange<Integer> range = BoundedRange.open(0, 5);
Assert.assertEquals("(0, 5)", range.toString());
// getBound
Assert.assertTrue(range.hasLowerBound());
Assert.assertTrue(range.hasUpperBound());
Assert.assertEquals((Integer)0, range.getLowerBoundValue());
Assert.assertEquals((Integer)5, range.getUpperBoundValue());
// test
Assert.assertFalse(range.isEmpty());
Assert.assertFalse(range.test(6));
Assert.assertFalse(range.test(5));
Assert.assertTrue(range.test(3));
Assert.assertFalse(range.test(0));
Assert.assertFalse(range.test(-1));
// isXXX
Assert.assertEquals(range, range);
Assert.assertTrue(range.isDisjoint(BoundedRange.open(-5, 0))); // (-5, 0)
Assert.assertTrue(range.isDisjoint(BoundedRange.close(-5, 0))); // [-5, 0]
Assert.assertTrue(range.isIntersected(BoundedRange.close(-5, 1))); // [-5, 1]
Assert.assertTrue(range.isSubset(BoundedRange.close(0, 5))); // [0, 5]
Assert.assertTrue(range.isProperSubset(BoundedRange.close(0, 5))); // [0, 5]
Assert.assertFalse(range.isSuperset(BoundedRange.close(0, 5))); // [0, 5]
Assert.assertFalse(range.isProperSuperset(BoundedRange.close(0, 5))); // [0, 5]
// operate
Assert.assertEquals("(0, 10]", range.unionIfIntersected(BoundedRange.close(4, 10)).toString());
Assert.assertEquals("(0, 10)", range.span(BoundedRange.open(9, 10)).toString());
Assert.assertEquals("(-2, 0]", range.gap(BoundedRange.close(-10, -2)).toString());
Assert.assertEquals("(3, 5)", range.intersection(BoundedRange.open(3, 10)).toString());
// sub
Assert.assertEquals("(3, 5)", range.subGreatThan(3).toString());
Assert.assertEquals("[3, 5)", range.subAtLeast(3).toString());
Assert.assertEquals("(0, 3)", range.subLessThan(3).toString());
Assert.assertEquals("(0, 3]", range.subAtMost(3).toString());
}
@Test
public void testClose() {
final BoundedRange<Integer> range = BoundedRange.close(0, 5);
Assert.assertEquals("[0, 5]", range.toString());
// getBound
Assert.assertTrue(range.hasLowerBound());
Assert.assertTrue(range.hasUpperBound());
Assert.assertEquals((Integer)0, range.getLowerBoundValue());
Assert.assertEquals((Integer)5, range.getUpperBoundValue());
// test
Assert.assertFalse(range.isEmpty());
Assert.assertFalse(range.test(6));
Assert.assertTrue(range.test(5));
Assert.assertTrue(range.test(0));
Assert.assertFalse(range.test(-1));
// isXXX
Assert.assertEquals(range, range);
Assert.assertTrue(range.isDisjoint(BoundedRange.open(-5, 0))); // (-5, 0)
Assert.assertTrue(range.isDisjoint(BoundedRange.close(-5, 0))); // [-5, 0]
Assert.assertTrue(range.isIntersected(BoundedRange.open(-5, 1))); // [-5, 1]
Assert.assertFalse(range.isSubset(BoundedRange.open(0, 5))); // (0, 5)
Assert.assertFalse(range.isProperSubset(BoundedRange.open(0, 5))); // (0, 5)
Assert.assertTrue(range.isSuperset(BoundedRange.open(0, 5))); // (0, 5)
Assert.assertTrue(range.isProperSuperset(BoundedRange.open(0, 5))); // (0, 5)
// operate
Assert.assertEquals("[0, 5]", range.unionIfIntersected(BoundedRange.open(5, 10)).toString());
Assert.assertEquals("[0, 10]", range.unionIfIntersected(BoundedRange.close(4, 10)).toString());
Assert.assertEquals("[0, 10)", range.span(BoundedRange.open(9, 10)).toString());
Assert.assertEquals("(-2, 0)", range.gap(BoundedRange.close(-10, -2)).toString());
Assert.assertEquals("(3, 5]", range.intersection(BoundedRange.open(3, 10)).toString());
Assert.assertNull(range.intersection(BoundedRange.open(5, 10)));
// sub
Assert.assertEquals("(3, 5]", range.subGreatThan(3).toString());
Assert.assertEquals("[3, 5]", range.subAtLeast(3).toString());
Assert.assertEquals("[0, 3)", range.subLessThan(3).toString());
Assert.assertEquals("[0, 3]", range.subAtMost(3).toString());
}
@Test
public void testOpenClose() {
final BoundedRange<Integer> range = BoundedRange.openClose(0, 5);
Assert.assertEquals("(0, 5]", range.toString());
// getBound
Assert.assertTrue(range.hasLowerBound());
Assert.assertTrue(range.hasUpperBound());
Assert.assertEquals((Integer)0, range.getLowerBoundValue());
Assert.assertEquals((Integer)5, range.getUpperBoundValue());
// test
Assert.assertFalse(range.isEmpty());
Assert.assertFalse(range.test(6));
Assert.assertTrue(range.test(5));
Assert.assertFalse(range.test(0));
Assert.assertFalse(range.test(-1));
}
@Test
public void testCloseOpen() {
final BoundedRange<Integer> range = BoundedRange.closeOpen(0, 5);
Assert.assertEquals("[0, 5)", range.toString());
// getBound
Assert.assertTrue(range.hasLowerBound());
Assert.assertTrue(range.hasUpperBound());
Assert.assertEquals((Integer)0, range.getLowerBoundValue());
Assert.assertEquals((Integer)5, range.getUpperBoundValue());
// test
Assert.assertFalse(range.isEmpty());
Assert.assertFalse(range.test(6));
Assert.assertFalse(range.test(5));
Assert.assertTrue(range.test(0));
Assert.assertFalse(range.test(-1));
}
@Test
public void testGreatThan() {
final BoundedRange<Integer> range = BoundedRange.greaterThan(0);
Assert.assertEquals("(0, +∞)", range.toString());
// getBound
Assert.assertTrue(range.hasLowerBound());
Assert.assertFalse(range.hasUpperBound());
Assert.assertEquals((Integer)0, range.getLowerBoundValue());
Assert.assertNull(range.getUpperBoundValue());
// test
Assert.assertFalse(range.isEmpty());
Assert.assertTrue(range.test(6));
Assert.assertTrue(range.test(5));
Assert.assertFalse(range.test(0));
Assert.assertFalse(range.test(-1));
}
@Test
public void testAtLeast() {
final BoundedRange<Integer> range = BoundedRange.atLeast(0);
Assert.assertEquals("[0, +∞)", range.toString());
// getBound
Assert.assertTrue(range.hasLowerBound());
Assert.assertFalse(range.hasUpperBound());
Assert.assertEquals((Integer)0, range.getLowerBoundValue());
Assert.assertNull(range.getUpperBoundValue());
// test
Assert.assertFalse(range.isEmpty());
Assert.assertTrue(range.test(6));
Assert.assertTrue(range.test(5));
Assert.assertTrue(range.test(0));
Assert.assertFalse(range.test(-1));
}
@Test
public void testLessThan() {
final BoundedRange<Integer> range = BoundedRange.lessThan(5);
Assert.assertEquals("(-∞, 5)", range.toString());
// getBound
Assert.assertTrue(range.hasUpperBound());
Assert.assertFalse(range.hasLowerBound());
Assert.assertEquals((Integer)5, range.getUpperBoundValue());
Assert.assertNull(range.getLowerBoundValue());
// test
Assert.assertFalse(range.isEmpty());
Assert.assertFalse(range.test(6));
Assert.assertFalse(range.test(5));
Assert.assertTrue(range.test(0));
Assert.assertTrue(range.test(-1));
}
@Test
public void testAtMost() {
final BoundedRange<Integer> range = BoundedRange.atMost(5);
Assert.assertEquals("(-∞, 5]", range.toString());
// getBound
Assert.assertTrue(range.hasUpperBound());
Assert.assertFalse(range.hasLowerBound());
Assert.assertEquals((Integer)5, range.getUpperBoundValue());
Assert.assertNull(range.getLowerBoundValue());
// test
Assert.assertFalse(range.isEmpty());
Assert.assertFalse(range.test(6));
Assert.assertTrue(range.test(5));
Assert.assertTrue(range.test(0));
Assert.assertTrue(range.test(-1));
}
}

View File

@@ -1,4 +1,4 @@
package cn.hutool.core.lang;
package cn.hutool.core.lang.range;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateRange;

View File

@@ -1,5 +1,6 @@
package cn.hutool.core.map;
import cn.hutool.core.io.SerializeUtil;
import org.junit.Assert;
import org.junit.Test;
@@ -20,4 +21,16 @@ public class CamelCaseMapTest {
Assert.assertEquals("OK", map.get("customKey"));
Assert.assertEquals("OK", map.get("custom_key"));
}
@Test
public void serializableKeyFuncTest() {
final CamelCaseMap<String, String> map = new CamelCaseMap<>();
map.put("serializable_key", "OK");
final CamelCaseMap<String, String> deSerializableMap = SerializeUtil.deserialize(SerializeUtil.serialize(map));
Assert.assertEquals("OK", deSerializableMap.get("serializable_key"));
Assert.assertEquals("OK", deSerializableMap.get("serializableKey"));
deSerializableMap.put("serializable_func", "OK");
Assert.assertEquals("OK", deSerializableMap.get("serializable_func"));
Assert.assertEquals("OK", deSerializableMap.get("serializableFunc"));
}
}

View File

@@ -16,8 +16,11 @@ public class UrlBuilderTest {
@Test
public void buildTest() {
final String buildUrl = UrlBuilder.of().setHost("www.hutool.cn").build();
final UrlBuilder builder = UrlBuilder.of();
final String buildUrl = builder.setHost("www.hutool.cn").build();
Assert.assertEquals("http://www.hutool.cn/", buildUrl);
Assert.assertEquals(buildUrl, 80, builder.getPortWithDefault());
}
@Test
@@ -26,9 +29,11 @@ public class UrlBuilderTest {
String buildUrl = UrlBuilder.of().setScheme("http").setHost("192.168.1.1").setPort(8080).setWithEndTag(false).build();
Assert.assertEquals("http://192.168.1.1:8080", buildUrl);
buildUrl = UrlBuilder.of().setScheme("http").setHost("192.168.1.1").setPort(8080).addQuery("url", "http://192.168.1.1/test/1")
final UrlBuilder urlBuilder = UrlBuilder.of();
buildUrl = urlBuilder.setScheme("http").setHost("192.168.1.1").setPort(8080).addQuery("url", "http://192.168.1.1/test/1")
.setWithEndTag(false).build();
Assert.assertEquals("http://192.168.1.1:8080?url=http://192.168.1.1/test/1", buildUrl);
Assert.assertEquals(buildUrl, 8080, urlBuilder.getPortWithDefault());
}
@Test
@@ -459,7 +464,7 @@ public class UrlBuilderTest {
}
@Test
public void getAuthorityTest(){
public void getAuthorityTest() {
final UrlBuilder builder = UrlBuilder.ofHttp("127.0.0.1:8080")
.addQuery("param[0].field", "编码");
@@ -467,7 +472,7 @@ public class UrlBuilderTest {
}
@Test
public void addPathTest(){
public void addPathTest() {
//https://gitee.com/dromara/hutool/issues/I5O4ML
UrlBuilder.of().addPath("");
UrlBuilder.of().addPath("/");

View File

@@ -181,7 +181,6 @@ public class AbstractEnhancedWrappedStreamTest {
final List<Integer> list = asList(1, 2, 3);
final Map<Boolean, List<Integer>> map = new HashMap<Boolean, List<Integer>>() {
private static final long serialVersionUID = 1L;
{
put(Boolean.TRUE, singletonList(2));
put(Boolean.FALSE, asList(1, 3));
@@ -625,7 +624,6 @@ public class AbstractEnhancedWrappedStreamTest {
public void testToEntries() {
final Map<Integer, Integer> expect = new HashMap<Integer, Integer>() {
private static final long serialVersionUID = 1L;
{
put(1, 1);
put(2, 2);

View File

@@ -63,6 +63,9 @@ public class ArrayUtilTest {
public void newArrayTest() {
final String[] newArray = ArrayUtil.newArray(String.class, 3);
Assert.assertEquals(3, newArray.length);
final Object[] newArray2 = ArrayUtil.newArray(3);
Assert.assertEquals(3, newArray2.length);
}
@Test
@@ -508,10 +511,20 @@ public class ArrayUtilTest {
}
@Test
public void setOrAppendTest(){
String[] arr = new String[0];
String[] newArr = ArrayUtil.setOrAppend(arr, 0, "Good");// ClassCastException
public void setOrAppendTest() {
final String[] arr = new String[0];
final String[] newArr = ArrayUtil.setOrAppend(arr, 0, "Good");// ClassCastException
Assert.assertArrayEquals(new String[]{"Good"}, newArr);
// 非空数组替换第一个元素
int[] arr2 = new int[]{1};
int[] o = ArrayUtil.setOrAppend(arr2, 0, 2);
Assert.assertArrayEquals(new int[]{2}, o);
// 空数组追加
arr2 = new int[0];
o = ArrayUtil.setOrAppend(arr2, 0, 2);
Assert.assertArrayEquals(new int[]{2}, o);
}
@Test
@@ -522,4 +535,46 @@ public class ArrayUtilTest {
final String[] c = {"d", "e"};
Assert.assertTrue(ArrayUtil.containsAll(c, resultO[0], resultO[1]));
}
@Test
public void hasNonNullTest() {
String[] a = {null, "e"};
Assert.assertTrue(ArrayUtil.hasNonNull(a));
a = new String[]{null, null};
Assert.assertFalse(ArrayUtil.hasNonNull(a));
a = new String[]{"", null};
Assert.assertTrue(ArrayUtil.hasNonNull(a));
a = new String[]{null};
Assert.assertFalse(ArrayUtil.hasNonNull(a));
a = new String[]{};
Assert.assertFalse(ArrayUtil.hasNonNull(a));
a = null;
Assert.assertFalse(ArrayUtil.hasNonNull(a));
}
@Test
public void isAllNullTest() {
String[] a = {null, "e"};
Assert.assertFalse(ArrayUtil.isAllNull(a));
a = new String[]{null, null};
Assert.assertTrue(ArrayUtil.isAllNull(a));
a = new String[]{"", null};
Assert.assertFalse(ArrayUtil.isAllNull(a));
a = new String[]{null};
Assert.assertTrue(ArrayUtil.isAllNull(a));
a = new String[]{};
Assert.assertTrue(ArrayUtil.isAllNull(a));
a = null;
Assert.assertTrue(ArrayUtil.isAllNull(a));
}
}

View File

@@ -55,4 +55,13 @@ public class CharUtilTest {
Assert.assertEquals('⑫', CharUtil.toCloseByNumber(12));
Assert.assertEquals('⑳', CharUtil.toCloseByNumber(20));
}
@Test
public void issueI5UGSQTest(){
char c = '\u3164';
Assert.assertTrue(CharUtil.isBlankChar(c));
c = '\u2800';
Assert.assertTrue(CharUtil.isBlankChar(c));
}
}