mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-08-18 20:38:02 +08:00
fix code
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
package cn.hutool.core.convert;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.TypeReference;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@@ -7,12 +12,6 @@ import java.util.LinkedHashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.TypeReference;
|
||||
|
||||
/**
|
||||
* 转换为集合测试
|
||||
*
|
||||
@@ -107,7 +106,7 @@ public class ConvertToCollectionTest {
|
||||
public void numberToListTest() {
|
||||
Integer i = 1;
|
||||
ArrayList<?> list = Convert.convert(ArrayList.class, i);
|
||||
Assert.assertTrue(i == list.get(0));
|
||||
Assert.assertSame(i, list.get(0));
|
||||
|
||||
BigDecimal b = BigDecimal.ONE;
|
||||
ArrayList<?> list2 = Convert.convert(ArrayList.class, b);
|
||||
|
||||
@@ -67,12 +67,12 @@ public class DateTimeTest {
|
||||
|
||||
// 默认情况下DateTime为可变对象
|
||||
DateTime offsite = dateTime.offset(DateField.YEAR, 0);
|
||||
Assert.assertTrue(offsite == dateTime);
|
||||
Assert.assertSame(offsite, dateTime);
|
||||
|
||||
// 设置为不可变对象后变动将返回新对象
|
||||
dateTime.setMutable(false);
|
||||
offsite = dateTime.offset(DateField.YEAR, 0);
|
||||
Assert.assertFalse(offsite == dateTime);
|
||||
Assert.assertNotSame(offsite, dateTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -86,6 +86,7 @@ public class DateTimeTest {
|
||||
|
||||
@Test
|
||||
public void monthTest() {
|
||||
//noinspection ConstantConditions
|
||||
int month = DateUtil.parse("2017-07-01").month();
|
||||
Assert.assertEquals(6, month);
|
||||
}
|
||||
@@ -93,6 +94,7 @@ public class DateTimeTest {
|
||||
@Test
|
||||
public void weekOfYearTest() {
|
||||
DateTime date = DateUtil.parse("2016-12-27");
|
||||
//noinspection ConstantConditions
|
||||
Assert.assertEquals(2016, date.year());
|
||||
//跨年的周返回的总是1
|
||||
Assert.assertEquals(1, date.weekOfYear());
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
package cn.hutool.core.img;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.Color;
|
||||
import java.awt.Font;
|
||||
import java.awt.Image;
|
||||
@@ -7,14 +12,6 @@ import java.awt.Rectangle;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
|
||||
public class ImgUtilTest {
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
package cn.hutool.core.lang;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.date.DateField;
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.lang.Range;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* {@link Range} 单元测试
|
||||
@@ -20,16 +18,11 @@ public class RangeTest {
|
||||
DateTime start = DateUtil.parse("2017-01-01");
|
||||
DateTime end = DateUtil.parse("2017-01-02");
|
||||
|
||||
final Range<DateTime> range = new Range<DateTime>(start, end, new Range.Steper<DateTime>(){
|
||||
|
||||
@Override
|
||||
public DateTime step(DateTime current, DateTime end, int index) {
|
||||
if(current.isAfterOrEquals(end)) {
|
||||
return null;
|
||||
}
|
||||
return current.offsetNew(DateField.DAY_OF_YEAR, 1);
|
||||
final Range<DateTime> range = new Range<>(start, end, (current, end1, index) -> {
|
||||
if (current.isAfterOrEquals(end1)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return current.offsetNew(DateField.DAY_OF_YEAR, 1);
|
||||
});
|
||||
|
||||
Assert.assertTrue(range.hasNext());
|
||||
@@ -41,14 +34,7 @@ public class RangeTest {
|
||||
|
||||
@Test
|
||||
public void intRangeTest() {
|
||||
final Range<Integer> range = new Range<Integer>(1, 1, new Range.Steper<Integer>(){
|
||||
|
||||
@Override
|
||||
public Integer step(Integer current, Integer end, int index) {
|
||||
return current >= end ? null : current +10;
|
||||
}
|
||||
|
||||
});
|
||||
final Range<Integer> range = new Range<>(1, 1, (current, end, index) -> current >= end ? null : current + 10);
|
||||
|
||||
Assert.assertTrue(range.hasNext());
|
||||
Assert.assertEquals(Integer.valueOf(1), range.next());
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package cn.hutool.core.lang;
|
||||
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.thread.ThreadUtil;
|
||||
import lombok.Data;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SingletonTest {
|
||||
|
||||
@Test
|
||||
public void getTest(){
|
||||
// 此测试中使用1000个线程获取单例对象,其间对象只被创建一次
|
||||
ThreadUtil.concurrencyTest(1000, ()-> Singleton.get(TestBean.class));
|
||||
}
|
||||
|
||||
@Data
|
||||
static class TestBean{
|
||||
private static volatile TestBean testSingleton;
|
||||
|
||||
public TestBean(){
|
||||
if(null != testSingleton){
|
||||
throw new UtilException("单例测试中,对象被创建了两次!");
|
||||
}
|
||||
testSingleton = this;
|
||||
}
|
||||
|
||||
private String name;
|
||||
private String age;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
package cn.hutool.core.math;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 组合单元测试
|
||||
*
|
||||
@@ -49,6 +49,6 @@ public class CombinationTest {
|
||||
Assert.assertEquals(Combination.countAll(5), selectAll.size());
|
||||
|
||||
List<String[]> list2 = combination.select(0);
|
||||
Assert.assertTrue(1 == list2.size());
|
||||
Assert.assertEquals(1, list2.size());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,42 +1,27 @@
|
||||
package cn.hutool.core.swing;
|
||||
|
||||
import java.awt.datatransfer.Clipboard;
|
||||
import java.awt.datatransfer.Transferable;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.swing.clipboard.ClipboardUtil;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.swing.clipboard.ClipboardListener;
|
||||
import cn.hutool.core.swing.clipboard.ClipboardUtil;
|
||||
|
||||
public class ClipboardMonitorTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void monitorTest() {
|
||||
// 第一个监听
|
||||
ClipboardUtil.listen(new ClipboardListener() {
|
||||
|
||||
@Override
|
||||
public Transferable onChange(Clipboard clipboard, Transferable contents) {
|
||||
Object object = ClipboardUtil.getStr(contents);
|
||||
Console.log("1# {}", object);
|
||||
return contents;
|
||||
}
|
||||
|
||||
ClipboardUtil.listen((clipboard, contents) -> {
|
||||
Object object = ClipboardUtil.getStr(contents);
|
||||
Console.log("1# {}", object);
|
||||
return contents;
|
||||
}, false);
|
||||
|
||||
// 第二个监听
|
||||
ClipboardUtil.listen(new ClipboardListener() {
|
||||
|
||||
@Override
|
||||
public Transferable onChange(Clipboard clipboard, Transferable contents) {
|
||||
Object object = ClipboardUtil.getStr(contents);
|
||||
Console.log("2# {}", object);
|
||||
return contents;
|
||||
}
|
||||
|
||||
ClipboardUtil.listen((clipboard, contents) -> {
|
||||
Object object = ClipboardUtil.getStr(contents);
|
||||
Console.log("2# {}", object);
|
||||
return contents;
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
package cn.hutool.core.text.csv;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
public class CsvParserTest {
|
||||
|
||||
@@ -15,6 +14,7 @@ public class CsvParserTest {
|
||||
StringReader reader = StrUtil.getReader("aaa,b\"bba\",ccc");
|
||||
CsvParser parser = new CsvParser(reader, null);
|
||||
CsvRow row = parser.nextRow();
|
||||
//noinspection ConstantConditions
|
||||
Assert.assertEquals("b\"bba\"", row.getRawList().get(1));
|
||||
IoUtil.close(parser);
|
||||
}
|
||||
@@ -24,6 +24,7 @@ public class CsvParserTest {
|
||||
StringReader reader = StrUtil.getReader("aaa,\"bba\"bbb,ccc");
|
||||
CsvParser parser = new CsvParser(reader, null);
|
||||
CsvRow row = parser.nextRow();
|
||||
//noinspection ConstantConditions
|
||||
Assert.assertEquals("\"bba\"bbb", row.getRawList().get(1));
|
||||
IoUtil.close(parser);
|
||||
}
|
||||
@@ -33,6 +34,7 @@ public class CsvParserTest {
|
||||
StringReader reader = StrUtil.getReader("aaa,\"bba\",ccc");
|
||||
CsvParser parser = new CsvParser(reader, null);
|
||||
CsvRow row = parser.nextRow();
|
||||
//noinspection ConstantConditions
|
||||
Assert.assertEquals("bba", row.getRawList().get(1));
|
||||
IoUtil.close(parser);
|
||||
}
|
||||
@@ -42,6 +44,7 @@ public class CsvParserTest {
|
||||
StringReader reader = StrUtil.getReader("aaa,\"\",ccc");
|
||||
CsvParser parser = new CsvParser(reader, null);
|
||||
CsvRow row = parser.nextRow();
|
||||
//noinspection ConstantConditions
|
||||
Assert.assertEquals("", row.getRawList().get(1));
|
||||
IoUtil.close(parser);
|
||||
}
|
||||
|
||||
@@ -24,9 +24,7 @@ public class CsvUtilTest {
|
||||
@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.notEmpty(csvRow.getRawList()));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -11,14 +11,10 @@ public class ConcurrencyTesterTest {
|
||||
@Test
|
||||
@Ignore
|
||||
public void concurrencyTesterTest() {
|
||||
ConcurrencyTester tester = ThreadUtil.concurrencyTest(100, new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
long delay = RandomUtil.randomLong(100, 1000);
|
||||
ThreadUtil.sleep(delay);
|
||||
Console.log("{} test finished, delay: {}", Thread.currentThread().getName(), delay);
|
||||
}
|
||||
ConcurrencyTester tester = ThreadUtil.concurrencyTest(100, () -> {
|
||||
long delay = RandomUtil.randomLong(100, 1000);
|
||||
ThreadUtil.sleep(delay);
|
||||
Console.log("{} test finished, delay: {}", Thread.currentThread().getName(), delay);
|
||||
});
|
||||
Console.log(tester.getInterval());
|
||||
}
|
||||
|
||||
@@ -9,13 +9,7 @@ public class ThreadUtilTest {
|
||||
public void executeTest() {
|
||||
final boolean isValid = true;
|
||||
|
||||
ThreadUtil.execute(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Assert.assertTrue(isValid);
|
||||
}
|
||||
});
|
||||
ThreadUtil.execute(() -> Assert.assertTrue(isValid));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* EnumUtil单元测试
|
||||
@@ -49,17 +48,18 @@ public class EnumUtilTest {
|
||||
@Test
|
||||
public void getNameFieldMapTest() {
|
||||
Map<String, Object> enumMap = EnumUtil.getNameFieldMap(TestEnum.class, "type");
|
||||
assert enumMap != null;
|
||||
Assert.assertEquals("type1", enumMap.get("TEST1"));
|
||||
}
|
||||
|
||||
public enum TestEnum{
|
||||
TEST1("type1"), TEST2("type2"), TEST3("type3");
|
||||
|
||||
|
||||
TestEnum(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
private String type;
|
||||
|
||||
private final String type;
|
||||
private String name;
|
||||
|
||||
public String getType() {
|
||||
|
||||
@@ -1,13 +1,5 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.collection.ConcurrentHashSet;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.date.TimeInterval;
|
||||
@@ -15,6 +7,13 @@ import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.lang.Snowflake;
|
||||
import cn.hutool.core.thread.ThreadUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
/**
|
||||
* {@link IdUtil} 单元测试
|
||||
@@ -56,6 +55,7 @@ public class IdUtilTest {
|
||||
|
||||
timer.restart();
|
||||
for (int i = 0; i < 1000000; i++) {
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
UUID.randomUUID().toString().replace("-", "");
|
||||
}
|
||||
Console.log(timer.interval());
|
||||
@@ -85,17 +85,13 @@ public class IdUtilTest {
|
||||
final int idCountPerThread = 10000;
|
||||
final CountDownLatch latch = new CountDownLatch(threadCount);
|
||||
for(int i =0; i < threadCount; i++) {
|
||||
ThreadUtil.execute(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
for(int i =0; i < idCountPerThread; i++) {
|
||||
long id = snowflake.nextId();
|
||||
set.add(id);
|
||||
ThreadUtil.execute(() -> {
|
||||
for(int i1 = 0; i1 < idCountPerThread; i1++) {
|
||||
long id = snowflake.nextId();
|
||||
set.add(id);
|
||||
// Console.log("Add new id: {}", id);
|
||||
}
|
||||
latch.countDown();
|
||||
}
|
||||
latch.countDown();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -118,17 +114,13 @@ public class IdUtilTest {
|
||||
final int idCountPerThread = 10000;
|
||||
final CountDownLatch latch = new CountDownLatch(threadCount);
|
||||
for(int i =0; i < threadCount; i++) {
|
||||
ThreadUtil.execute(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
for(int i =0; i < idCountPerThread; i++) {
|
||||
long id = IdUtil.getSnowflake(1, 1).nextId();
|
||||
set.add(id);
|
||||
ThreadUtil.execute(() -> {
|
||||
for(int i1 = 0; i1 < idCountPerThread; i1++) {
|
||||
long id = IdUtil.getSnowflake(1, 1).nextId();
|
||||
set.add(id);
|
||||
// Console.log("Add new id: {}", id);
|
||||
}
|
||||
latch.countDown();
|
||||
}
|
||||
latch.countDown();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user