This commit is contained in:
Looly
2022-04-28 01:30:17 +08:00
parent e0ac5e9961
commit d78219c60c
248 changed files with 621 additions and 3407 deletions

View File

@@ -0,0 +1,107 @@
package cn.hutool.core.cache;
import cn.hutool.core.cache.impl.FIFOCache;
import cn.hutool.core.cache.impl.LRUCache;
import cn.hutool.core.cache.impl.WeakCache;
import cn.hutool.core.lang.Console;
import cn.hutool.core.thread.ConcurrencyTester;
import cn.hutool.core.thread.ThreadUtil;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 缓存单元测试
*
* @author looly
*
*/
public class CacheConcurrentTest {
@Test
@Ignore
public void fifoCacheTest() {
int threadCount = 4000;
final Cache<String, String> cache = new FIFOCache<>(3);
// 由于缓存容量只有3当加入第四个元素的时候根据FIFO规则最先放入的对象将被移除
for (int i = 0; i < threadCount; i++) {
ThreadUtil.execute(() -> {
cache.put("key1", "value1", System.currentTimeMillis() * 3);
cache.put("key2", "value2", System.currentTimeMillis() * 3);
cache.put("key3", "value3", System.currentTimeMillis() * 3);
cache.put("key4", "value4", System.currentTimeMillis() * 3);
ThreadUtil.sleep(1000);
cache.put("key5", "value5", System.currentTimeMillis() * 3);
cache.put("key6", "value6", System.currentTimeMillis() * 3);
cache.put("key7", "value7", System.currentTimeMillis() * 3);
cache.put("key8", "value8", System.currentTimeMillis() * 3);
Console.log("put all");
});
}
for (int i = 0; i < threadCount; i++) {
ThreadUtil.execute(() -> show(cache));
}
System.out.println("==============================");
ThreadUtil.sleep(10000);
}
@Test
@Ignore
public void lruCacheTest() {
int threadCount = 40000;
final Cache<String, String> cache = new LRUCache<>(1000);
for (int i = 0; i < threadCount; i++) {
final int index = i;
ThreadUtil.execute(() -> {
cache.put("key1"+ index, "value1");
cache.put("key2"+ index, "value2", System.currentTimeMillis() * 3);
int size = cache.size();
int capacity = cache.capacity();
if(size > capacity) {
Console.log("{} {}", size, capacity);
}
ThreadUtil.sleep(1000);
size = cache.size();
capacity = cache.capacity();
if(size > capacity) {
Console.log("## {} {}", size, capacity);
}
});
}
ThreadUtil.sleep(5000);
}
private void show(Cache<String, String> cache) {
for (Object tt : cache) {
Console.log(tt);
}
}
@Test
public void effectiveTest() {
// 模拟耗时操作消耗时间
int delay = 2000;
AtomicInteger ai = new AtomicInteger(0);
WeakCache<Integer, Integer> weakCache = new WeakCache<>(60 * 1000);
ConcurrencyTester concurrencyTester = ThreadUtil.concurrencyTest(32, () -> {
int i = ai.incrementAndGet() % 4;
weakCache.get(i, () -> {
ThreadUtil.sleep(delay);
return i;
});
});
long interval = concurrencyTester.getInterval();
// 总耗时应与单次操作耗时在同一个数量级
Assert.assertTrue(interval < delay * 2);
}
}

View File

@@ -0,0 +1,117 @@
package cn.hutool.core.cache;
import cn.hutool.core.cache.impl.TimedCache;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import org.junit.Assert;
import org.junit.Test;
/**
* 缓存测试用例
* @author Looly
*
*/
public class CacheTest {
@Test
public void fifoCacheTest(){
Cache<String,String> fifoCache = CacheUtil.newFIFOCache(3);
fifoCache.setListener((key, value)->{
// 监听测试此测试中只有key1被移除测试是否监听成功
Assert.assertEquals("key1", key);
Assert.assertEquals("value1", value);
});
fifoCache.put("key1", "value1", DateUnit.SECOND.getMillis() * 3);
fifoCache.put("key2", "value2", DateUnit.SECOND.getMillis() * 3);
fifoCache.put("key3", "value3", DateUnit.SECOND.getMillis() * 3);
fifoCache.put("key4", "value4", DateUnit.SECOND.getMillis() * 3);
//由于缓存容量只有3当加入第四个元素的时候根据FIFO规则最先放入的对象将被移除
String value1 = fifoCache.get("key1");
Assert.assertNull(value1);
}
@Test
public void fifoCacheCapacityTest(){
Cache<String,String> fifoCache = CacheUtil.newFIFOCache(100);
for (int i = 0; i < RandomUtil.randomInt(100, 1000); i++) {
fifoCache.put("key" + i, "value" + i);
}
Assert.assertEquals(100, fifoCache.size());
}
@Test
public void lfuCacheTest(){
Cache<String, String> lfuCache = CacheUtil.newLFUCache(3);
lfuCache.put("key1", "value1", DateUnit.SECOND.getMillis() * 3);
//使用次数+1
lfuCache.get("key1");
lfuCache.put("key2", "value2", DateUnit.SECOND.getMillis() * 3);
lfuCache.put("key3", "value3", DateUnit.SECOND.getMillis() * 3);
lfuCache.put("key4", "value4", DateUnit.SECOND.getMillis() * 3);
//由于缓存容量只有3当加入第四个元素的时候根据LFU规则最少使用的将被移除2,3被移除
String value1 = lfuCache.get("key1");
String value2 = lfuCache.get("key2");
String value3 = lfuCache.get("key3");
Assert.assertNotNull(value1);
Assert.assertNull(value2);
Assert.assertNull(value3);
}
@Test
public void lruCacheTest(){
Cache<String, String> lruCache = CacheUtil.newLRUCache(3);
//通过实例化对象创建
// LRUCache<String, String> lruCache = new LRUCache<String, String>(3);
lruCache.put("key1", "value1", DateUnit.SECOND.getMillis() * 3);
lruCache.put("key2", "value2", DateUnit.SECOND.getMillis() * 3);
lruCache.put("key3", "value3", DateUnit.SECOND.getMillis() * 3);
//使用时间推近
lruCache.get("key1");
lruCache.put("key4", "value4", DateUnit.SECOND.getMillis() * 3);
String value1 = lruCache.get("key1");
Assert.assertNotNull(value1);
//由于缓存容量只有3当加入第四个元素的时候根据LRU规则最少使用的将被移除2被移除
String value2 = lruCache.get("key2");
Assert.assertNull(value2);
}
@Test
public void timedCacheTest(){
TimedCache<String, String> timedCache = CacheUtil.newTimedCache(4);
// TimedCache<String, String> timedCache = new TimedCache<String, String>(DateUnit.SECOND.getMillis() * 3);
timedCache.put("key1", "value1", 1);//1毫秒过期
timedCache.put("key2", "value2", DateUnit.SECOND.getMillis() * 5);//5秒过期
timedCache.put("key3", "value3");//默认过期(4毫秒)
timedCache.put("key4", "value4", Long.MAX_VALUE);//永不过期
//启动定时任务每5毫秒秒检查一次过期
timedCache.schedulePrune(5);
//等待5毫秒
ThreadUtil.sleep(5);
//5毫秒后由于value2设置了5毫秒过期因此只有value2被保留下来
String value1 = timedCache.get("key1");
Assert.assertNull(value1);
String value2 = timedCache.get("key2");
Assert.assertEquals("value2", value2);
//5毫秒后由于设置了默认过期key3只被保留4毫秒因此为null
String value3 = timedCache.get("key3");
Assert.assertNull(value3);
String value3Supplier = timedCache.get("key3", () -> "Default supplier");
Assert.assertEquals("Default supplier", value3Supplier);
// 永不过期
String value4 = timedCache.get("key4");
Assert.assertEquals("value4", value4);
//取消定时清理
timedCache.cancelPruneSchedule();
}
}

View File

@@ -0,0 +1,19 @@
package cn.hutool.core.cache;
import org.junit.Assert;
import org.junit.Test;
import cn.hutool.core.cache.file.LFUFileCache;
/**
* 文件缓存单元测试
* @author looly
*
*/
public class FileCacheTest {
@Test
public void lfuFileCacheTest() {
LFUFileCache cache = new LFUFileCache(1000, 500, 2000);
Assert.assertNotNull(cache);
}
}

View File

@@ -0,0 +1,67 @@
package cn.hutool.core.cache;
import cn.hutool.core.cache.impl.LRUCache;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.util.concurrent.CountDownLatch;
/**
* 见:<a href="https://github.com/dromara/hutool/issues/1895">https://github.com/dromara/hutool/issues/1895</a><br>
* 并发问题测试在5.7.15前LRUCache存在并发问题多线程get后map结构变更导致null的位置不确定
* 并可能引起死锁。
*/
public class LRUCacheTest {
@Test
@Ignore
public void putTest(){
//https://github.com/dromara/hutool/issues/2227
LRUCache<String, String> cache = CacheUtil.newLRUCache(100, 10);
for (int i = 0; i < 10000; i++) {
//ThreadUtil.execute(()-> cache.put(RandomUtil.randomString(5), "1243", 10));
ThreadUtil.execute(()-> cache.get(RandomUtil.randomString(5), ()->RandomUtil.randomString(10)));
}
ThreadUtil.sleep(3000);
}
@Test
public void readWriteTest() throws InterruptedException {
LRUCache<Integer, Integer> cache = CacheUtil.newLRUCache(10);
for (int i = 0; i < 10; i++) {
cache.put(i, i);
}
CountDownLatch countDownLatch = new CountDownLatch(10);
// 10个线程分别读0-9 10000次
for (int i = 0; i < 10; i++) {
int finalI = i;
new Thread(() -> {
for (int j = 0; j < 10000; j++) {
cache.get(finalI);
}
countDownLatch.countDown();
}).start();
}
// 等待读线程结束
countDownLatch.await();
// 按顺序读0-9
StringBuilder sb1 = new StringBuilder();
for (int i = 0; i < 10; i++) {
sb1.append(cache.get(i));
}
Assert.assertEquals("0123456789", sb1.toString());
// 新加11此时0最久未使用应该淘汰0
cache.put(11, 11);
StringBuilder sb2 = new StringBuilder();
for (int i = 0; i < 10; i++) {
sb2.append(cache.get(i));
}
Assert.assertEquals("null123456789", sb2.toString());
}
}

View File

@@ -0,0 +1,51 @@
package cn.hutool.core.cache;
import cn.hutool.core.cache.impl.WeakCache;
import cn.hutool.core.lang.Console;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
public class WeakCacheTest {
@Test
public void removeTest(){
final WeakCache<String, String> cache = new WeakCache<>(-1);
cache.put("abc", "123");
cache.put("def", "456");
Assert.assertEquals(2, cache.size());
// 检查被MutableObj包装的key能否正常移除
cache.remove("abc");
Assert.assertEquals(1, cache.size());
}
@Test
@Ignore
public void removeByGcTest(){
// https://gitee.com/dromara/hutool/issues/I51O7M
WeakCache<String, String> cache = new WeakCache<>(-1);
cache.put("a", "1");
cache.put("b", "2");
// 监听
Assert.assertEquals(2, cache.size());
cache.setListener(Console::log);
// GC测试
int i=0;
while(true){
if(2 == cache.size()){
i++;
Console.log("Object is alive for {} loops - ", i);
System.gc();
}else{
Console.log("Object has been collected.");
Console.log(cache.size());
break;
}
}
}
}

View File

@@ -6,7 +6,7 @@ import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ByteUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.codec.HexUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;

View File

@@ -1,15 +0,0 @@
package cn.hutool.core.img;
import org.junit.Assert;
import org.junit.Test;
import java.awt.Font;
public class FontUtilTest {
@Test
public void createFontTest(){
final Font font = FontUtil.createFont();
Assert.assertNotNull(font);
}
}

View File

@@ -1,99 +0,0 @@
package cn.hutool.core.img;
import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.URLUtil;
import org.junit.Ignore;
import org.junit.Test;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.Rectangle;
import java.io.File;
public class ImgTest {
@Test
@Ignore
public void cutTest1() {
Img.from(FileUtil.file("e:/pic/face.jpg")).cut(0, 0, 200).write(FileUtil.file("e:/pic/face_radis.png"));
}
@Test
@Ignore
public void compressTest() {
Img.from(FileUtil.file("f:/test/4347273249269e3fb272341acc42d4e.jpg")).setQuality(0.8).write(FileUtil.file("f:/test/test_dest.jpg"));
}
@Test
@Ignore
public void writeTest() {
final Img from = Img.from(FileUtil.file("d:/test/81898311-001d6100-95eb-11ea-83c2-a14d7b1010bd.png"));
ImgUtil.write(from.getImg(), FileUtil.file("d:/test/dest.jpg"));
}
@Test
@Ignore
public void roundTest() {
Img.from(FileUtil.file("e:/pic/face.jpg")).round(0.5).write(FileUtil.file("e:/pic/face_round.png"));
}
@Test
@Ignore
public void pressTextTest() {
Img.from(FileUtil.file("d:/test/617180969474805871.jpg"))
.setPositionBaseCentre(false)
.pressText("版权所有", Color.RED, //
new Font("黑体", Font.BOLD, 100), //
0, //
100, //
1f)
.write(FileUtil.file("d:/test/test2_result.png"));
}
@Test
@Ignore
public void pressTextFullScreenTest() {
Img.from(FileUtil.file("d:/test/1435859438434136064.jpg"))
.setTargetImageType(ImgUtil.IMAGE_TYPE_PNG)
.pressTextFull("版权所有 ", Color.LIGHT_GRAY,
new Font("黑体", Font.PLAIN, 30),
4,
30,
0.8f)
.write(FileUtil.file("d:/test/2_result.png"));
}
@Test
@Ignore
public void pressImgTest() {
Img.from(FileUtil.file("d:/test/图片1.JPG"))
.pressImage(ImgUtil.read("d:/test/617180969474805871.jpg"), new Rectangle(0, 0, 800, 800), 1f)
.write(FileUtil.file("d:/test/pressImg_result.jpg"));
}
@Test
@Ignore
public void strokeTest() {
Img.from(FileUtil.file("d:/test/公章3.png"))
.stroke(null, 2f)
.write(FileUtil.file("d:/test/stroke_result.png"));
}
/**
* issue#I49FIU
*/
@Test
@Ignore
public void scaleTest() {
String downloadFile = "d:/test/1435859438434136064.JPG";
File file = FileUtil.file(downloadFile);
File fileScale = FileUtil.file(downloadFile + ".scale." + FileTypeUtil.getType(file));
Image img = ImgUtil.getImage(URLUtil.getURL(file));
ImgUtil.scale(img, fileScale, 0.8f);
}
}

View File

@@ -1,149 +0,0 @@
package cn.hutool.core.img;
import cn.hutool.core.io.FileUtil;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import javax.imageio.ImageIO;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
public class ImgUtilTest {
@Test
@Ignore
public void scaleTest() {
ImgUtil.scale(FileUtil.file("e:/pic/test.jpg"), FileUtil.file("e:/pic/test_result.jpg"), 0.8f);
}
@Test
@Ignore
public void scaleTest2() {
ImgUtil.scale(
FileUtil.file("d:/test/2.png"),
FileUtil.file("d:/test/2_result.jpg"), 600, 337, null);
}
@Test
@Ignore
public void scalePngTest() {
ImgUtil.scale(FileUtil.file("f:/test/test.png"), FileUtil.file("f:/test/test_result.png"), 0.5f);
}
@Test
@Ignore
public void scaleByWidthAndHeightTest() {
ImgUtil.scale(FileUtil.file("f:/test/aaa.jpg"), FileUtil.file("f:/test/aaa_result.jpg"), 100, 400, Color.BLUE);
}
@Test
@Ignore
public void cutTest() {
ImgUtil.cut(FileUtil.file("d:/face.jpg"), FileUtil.file("d:/face_result.jpg"), new Rectangle(200, 200, 100, 100));
}
@Test
@Ignore
public void rotateTest() throws IOException {
Image image = ImgUtil.rotate(ImageIO.read(FileUtil.file("e:/pic/366466.jpg")), 180);
ImgUtil.write(image, FileUtil.file("e:/pic/result.png"));
}
@Test
@Ignore
public void flipTest() {
ImgUtil.flip(FileUtil.file("d:/logo.png"), FileUtil.file("d:/result.png"));
}
@Test
@Ignore
public void pressImgTest() {
ImgUtil.pressImage(
FileUtil.file("d:/test/1435859438434136064.jpg"),
FileUtil.file("d:/test/dest.jpg"),
ImgUtil.read(FileUtil.file("d:/test/qrcodeCustom.png")), 0, 0, 0.9f);
}
@Test
@Ignore
public void pressTextTest() {
ImgUtil.pressText(//
FileUtil.file("d:/test/2.jpg"), //
FileUtil.file("d:/test/2_result.png"), //
"版权所有", Color.RED, //
new Font("黑体", Font.BOLD, 100), //
0, //
0, //
1f);
}
@Test
@Ignore
public void sliceByRowsAndColsTest() {
ImgUtil.sliceByRowsAndCols(FileUtil.file("d:/test/logo.jpg"), FileUtil.file("d:/test/dest"), 1, 5);
}
@Test
@Ignore
public void convertTest() {
ImgUtil.convert(FileUtil.file("e:/test2.png"), FileUtil.file("e:/test2Convert.jpg"));
}
@Test
@Ignore
public void writeTest() {
final byte[] bytes = ImgUtil.toBytes(ImgUtil.read("d:/test/logo_484.png"), "png");
FileUtil.writeBytes(bytes, "d:/test/result.png");
}
@Test
@Ignore
public void compressTest() {
ImgUtil.compress(FileUtil.file("d:/test/dest.png"),
FileUtil.file("d:/test/1111_target.jpg"), 0.1f);
}
@Test
@Ignore
public void copyTest() {
BufferedImage image = ImgUtil.copyImage(ImgUtil.read("f:/pic/test.png"), BufferedImage.TYPE_INT_RGB);
ImgUtil.write(image, FileUtil.file("f:/pic/test_dest.jpg"));
}
@Test
public void toHexTest(){
final String s = ImgUtil.toHex(Color.RED);
Assert.assertEquals("#FF0000", s);
}
@Test
@Ignore
public void backgroundRemovalTest() {
// 图片 背景 换成 透明的
ImgUtil.backgroundRemoval(
"d:/test/617180969474805871.jpg",
"d:/test/2.jpg", 10);
// 图片 背景 换成 红色的
ImgUtil.backgroundRemoval(new File(
"d:/test/617180969474805871.jpg"),
new File("d:/test/3.jpg"),
new Color(200, 0, 0), 10);
}
@Test
public void getMainColor() throws MalformedURLException {
BufferedImage read = ImgUtil.read(new URL("https://pic2.zhimg.com/v2-94f5552f2b142ff575306850c5bab65d_b.png"));
String mainColor = ImgUtil.getMainColor(read, new int[]{64,84,116});
System.out.println(mainColor);
}
}

View File

@@ -1,7 +1,7 @@
package cn.hutool.core.io.checksum;
import cn.hutool.core.io.checksum.crc16.CRC16XModem;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.codec.HexUtil;
import cn.hutool.core.util.StrUtil;
import org.junit.Assert;
import org.junit.Test;

View File

@@ -2,7 +2,7 @@ package cn.hutool.core.lang.hash;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.codec.HexUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import org.junit.Assert;

View File

@@ -1,28 +0,0 @@
package cn.hutool.core.swing;
import cn.hutool.core.lang.Console;
import cn.hutool.core.swing.clipboard.ClipboardUtil;
import org.junit.Ignore;
import org.junit.Test;
public class ClipboardMonitorTest {
@Test
@Ignore
public void monitorTest() {
// 第一个监听
ClipboardUtil.listen((clipboard, contents) -> {
Object object = ClipboardUtil.getStr(contents);
Console.log("1# {}", object);
return contents;
}, false);
// 第二个监听
ClipboardUtil.listen((clipboard, contents) -> {
Object object = ClipboardUtil.getStr(contents);
Console.log("2# {}", object);
return contents;
});
}
}

View File

@@ -1,28 +0,0 @@
package cn.hutool.core.swing;
import org.junit.Assert;
import org.junit.Test;
import cn.hutool.core.swing.clipboard.ClipboardUtil;
/**
* 剪贴板工具类单元测试
*
* @author looly
*
*/
public class ClipboardUtilTest {
@Test
public void setAndGetStrTest() {
try {
ClipboardUtil.setStr("test");
String test = ClipboardUtil.getStr();
Assert.assertEquals("test", test);
} catch (java.awt.HeadlessException e) {
// 忽略 No X11 DISPLAY variable was set, but this program performed an operation which requires it.
// ignore
}
}
}

View File

@@ -1,13 +0,0 @@
package cn.hutool.core.swing;
import org.junit.Ignore;
import org.junit.Test;
public class DesktopUtilTest {
@Test
@Ignore
public void browseTest() {
DesktopUtil.browse("https://www.hutool.club");
}
}

View File

@@ -1,15 +0,0 @@
package cn.hutool.core.swing;
import org.junit.Ignore;
import org.junit.Test;
import cn.hutool.core.io.FileUtil;
public class RobotUtilTest {
@Test
@Ignore
public void captureScreenTest() {
RobotUtil.captureScreen(FileUtil.file("e:/screen.jpg"));
}
}

View File

@@ -0,0 +1,54 @@
package cn.hutool.core.text.bloomfilter;
import cn.hutool.core.map.bitMap.IntMap;
import cn.hutool.core.map.bitMap.LongMap;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
public class BitMapBloomFilterTest {
@Test
public void filterTest() {
BitMapBloomFilter filter = new BitMapBloomFilter(10);
filter.add("123");
filter.add("abc");
filter.add("ddd");
Assert.assertTrue(filter.contains("abc"));
Assert.assertTrue(filter.contains("ddd"));
Assert.assertTrue(filter.contains("123"));
}
@Test
@Ignore
public void testIntMap() {
IntMap intMap = new IntMap();
for (int i = 0; i < 32; i++) {
intMap.add(i);
}
intMap.remove(30);
for (int i = 0; i < 32; i++) {
System.out.println(i + "是否存在-->" + intMap.contains(i));
}
}
@Test
@Ignore
public void testLongMap() {
LongMap longMap = new LongMap();
for (int i = 0; i < 64; i++) {
longMap.add(i);
}
longMap.remove(30);
for (int i = 0; i < 64; i++) {
System.out.println(i + "是否存在-->" + longMap.contains(i));
}
}
}

View File

@@ -1,63 +0,0 @@
package cn.hutool.core.text.csv;
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 {
@Test
public void parseTest1() {
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);
}
@Test
public void parseTest2() {
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);
}
@Test
public void parseTest3() {
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);
}
@Test
public void parseTest4() {
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);
}
@Test
public void parseEscapeTest(){
// https://datatracker.ietf.org/doc/html/rfc4180#section-2
// 第七条规则
StringReader reader = StrUtil.getReader("\"b\"\"bb\"");
CsvParser parser = new CsvParser(reader, null);
CsvRow row = parser.nextRow();
Assert.assertNotNull(row);
Assert.assertEquals(1, row.size());
Assert.assertEquals("b\"bb", row.get(0));
}
}

View File

@@ -1,205 +0,0 @@
package cn.hutool.core.text.csv;
import cn.hutool.core.annotation.Alias;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.CharsetUtil;
import lombok.Data;
import org.junit.Assert;
import org.junit.Test;
import org.junit.Ignore;
import java.util.List;
import java.util.Map;
public class CsvReaderTest {
@Test
public void readTest() {
CsvReader reader = new CsvReader();
CsvData data = reader.read(ResourceUtil.getReader("test.csv", CharsetUtil.CHARSET_UTF_8));
Assert.assertEquals("sss,sss", data.getRow(0).get(0));
Assert.assertEquals(1, data.getRow(0).getOriginalLineNumber());
Assert.assertEquals("性别", data.getRow(0).get(2));
Assert.assertEquals("关注\"对象\"", data.getRow(0).get(3));
}
@Test
public void readMapListTest() {
final CsvReader reader = CsvUtil.getReader();
final List<Map<String, String>> result = reader.readMapList(
ResourceUtil.getUtf8Reader("test_bean.csv"));
Assert.assertEquals("张三", result.get(0).get("姓名"));
Assert.assertEquals("", result.get(0).get("gender"));
Assert.assertEquals("", result.get(0).get("focus"));
Assert.assertEquals("33", result.get(0).get("age"));
Assert.assertEquals("李四", result.get(1).get("姓名"));
Assert.assertEquals("", result.get(1).get("gender"));
Assert.assertEquals("好对象", result.get(1).get("focus"));
Assert.assertEquals("23", result.get(1).get("age"));
Assert.assertEquals("王妹妹", result.get(2).get("姓名"));
Assert.assertEquals("", result.get(2).get("gender"));
Assert.assertEquals("特别关注", result.get(2).get("focus"));
Assert.assertEquals("22", result.get(2).get("age"));
}
@Test
public void readAliasMapListTest() {
final CsvReadConfig csvReadConfig = CsvReadConfig.defaultConfig();
csvReadConfig.addHeaderAlias("姓名", "name");
final CsvReader reader = CsvUtil.getReader(csvReadConfig);
final List<Map<String, String>> result = reader.readMapList(
ResourceUtil.getUtf8Reader("test_bean.csv"));
Assert.assertEquals("张三", result.get(0).get("name"));
Assert.assertEquals("", result.get(0).get("gender"));
Assert.assertEquals("", result.get(0).get("focus"));
Assert.assertEquals("33", result.get(0).get("age"));
Assert.assertEquals("李四", result.get(1).get("name"));
Assert.assertEquals("", result.get(1).get("gender"));
Assert.assertEquals("好对象", result.get(1).get("focus"));
Assert.assertEquals("23", result.get(1).get("age"));
Assert.assertEquals("王妹妹", result.get(2).get("name"));
Assert.assertEquals("", result.get(2).get("gender"));
Assert.assertEquals("特别关注", result.get(2).get("focus"));
Assert.assertEquals("22", result.get(2).get("age"));
}
@Test
public void readBeanListTest() {
final CsvReader reader = CsvUtil.getReader();
final List<TestBean> result = reader.read(
ResourceUtil.getUtf8Reader("test_bean.csv"), TestBean.class);
Assert.assertEquals("张三", result.get(0).getName());
Assert.assertEquals("", result.get(0).getGender());
Assert.assertEquals("", result.get(0).getFocus());
Assert.assertEquals(Integer.valueOf(33), result.get(0).getAge());
Assert.assertEquals("李四", result.get(1).getName());
Assert.assertEquals("", result.get(1).getGender());
Assert.assertEquals("好对象", result.get(1).getFocus());
Assert.assertEquals(Integer.valueOf(23), result.get(1).getAge());
Assert.assertEquals("王妹妹", result.get(2).getName());
Assert.assertEquals("", result.get(2).getGender());
Assert.assertEquals("特别关注", result.get(2).getFocus());
Assert.assertEquals(Integer.valueOf(22), result.get(2).getAge());
}
@Data
private static class TestBean {
@Alias("姓名")
private String name;
private String gender;
private String focus;
private Integer age;
}
@Test
@Ignore
public void readTest2() {
final CsvReader reader = CsvUtil.getReader();
final CsvData read = reader.read(FileUtil.file("d:/test/test.csv"));
for (CsvRow strings : read) {
Console.log(strings);
}
}
@Test
@Ignore
public void readTest3() {
final CsvReadConfig csvReadConfig = CsvReadConfig.defaultConfig();
csvReadConfig.setContainsHeader(true);
final CsvReader reader = CsvUtil.getReader(csvReadConfig);
final CsvData read = reader.read(FileUtil.file("d:/test/ceshi.csv"));
for (CsvRow row : read) {
Console.log(row.getByName("案件ID"));
}
}
@Test
public void lineNoTest() {
CsvReader reader = new CsvReader();
CsvData data = reader.read(ResourceUtil.getReader("test_lines.csv", CharsetUtil.CHARSET_UTF_8));
Assert.assertEquals(1, data.getRow(0).getOriginalLineNumber());
Assert.assertEquals("a,b,c,d", CollUtil.join(data.getRow(0), ","));
Assert.assertEquals(4, data.getRow(2).getOriginalLineNumber());
Assert.assertEquals("q,w,e,r,我是一段\n带换行的内容",
CollUtil.join(data.getRow(2), ",").replace("\r", ""));
// 文件中第3行数据对应原始行号是6从0开始
Assert.assertEquals(6, data.getRow(3).getOriginalLineNumber());
Assert.assertEquals("a,s,d,f", CollUtil.join(data.getRow(3), ","));
}
@Test
public void lineLimitTest() {
// 从原始第2行开始读取
CsvReader reader = new CsvReader(CsvReadConfig.defaultConfig().setBeginLineNo(2));
CsvData data = reader.read(ResourceUtil.getReader("test_lines.csv", CharsetUtil.CHARSET_UTF_8));
Assert.assertEquals(2, data.getRow(0).getOriginalLineNumber());
Assert.assertEquals("1,2,3,4", CollUtil.join(data.getRow(0), ","));
Assert.assertEquals(4, data.getRow(1).getOriginalLineNumber());
Assert.assertEquals("q,w,e,r,我是一段\n带换行的内容",
CollUtil.join(data.getRow(1), ",").replace("\r", ""));
// 文件中第3行数据对应原始行号是6从0开始
Assert.assertEquals(6, data.getRow(2).getOriginalLineNumber());
Assert.assertEquals("a,s,d,f", CollUtil.join(data.getRow(2), ","));
}
@Test
public void lineLimitWithHeaderTest() {
// 从原始第2行开始读取
CsvReader reader = new CsvReader(CsvReadConfig.defaultConfig().setBeginLineNo(2).setContainsHeader(true));
CsvData data = reader.read(ResourceUtil.getReader("test_lines.csv", CharsetUtil.CHARSET_UTF_8));
Assert.assertEquals(4, data.getRow(0).getOriginalLineNumber());
Assert.assertEquals("q,w,e,r,我是一段\n带换行的内容",
CollUtil.join(data.getRow(0), ",").replace("\r", ""));
// 文件中第3行数据对应原始行号是6从0开始
Assert.assertEquals(6, data.getRow(1).getOriginalLineNumber());
Assert.assertEquals("a,s,d,f", CollUtil.join(data.getRow(1), ","));
}
@Test
public void customConfigTest() {
final CsvReader reader = CsvUtil.getReader(
CsvReadConfig.defaultConfig()
.setTextDelimiter('\'')
.setFieldSeparator(';'));
final CsvData csvRows = reader.readFromStr("123;456;'789;0'abc;");
final CsvRow row = csvRows.getRow(0);
Assert.assertEquals("123", row.get(0));
Assert.assertEquals("456", row.get(1));
Assert.assertEquals("'789;0'abc", row.get(2));
}
@Test
public void readDisableCommentTest() {
final CsvReader reader = CsvUtil.getReader(CsvReadConfig.defaultConfig().disableComment());
final CsvData read = reader.read(ResourceUtil.getUtf8Reader("test.csv"));
final CsvRow row = read.getRow(0);
Assert.assertEquals("# 这是一行注释,读取时应忽略", row.get(0));
}
@Test
@Ignore
public void streamTest() {
final CsvReader reader = CsvUtil.getReader(ResourceUtil.getUtf8Reader("test_bean.csv"));
reader.stream().limit(2).forEach(Console::log);
}
}

View File

@@ -1,207 +0,0 @@
package cn.hutool.core.text.csv;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.CharsetUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class CsvUtilTest {
@Test
public void readTest() {
CsvReader reader = CsvUtil.getReader();
//从文件中读取CSV数据
CsvData data = reader.read(FileUtil.file("test.csv"));
List<CsvRow> rows = data.getRows();
final CsvRow row0 = rows.get(0);
Assert.assertEquals("sss,sss", row0.get(0));
Assert.assertEquals("姓名", row0.get(1));
Assert.assertEquals("性别", row0.get(2));
Assert.assertEquals("关注\"对象\"", row0.get(3));
Assert.assertEquals("年龄", row0.get(4));
Assert.assertEquals("", row0.get(5));
Assert.assertEquals("\"", row0.get(6));
}
@Test
public void readTest2() {
CsvReader reader = CsvUtil.getReader();
reader.read(FileUtil.getUtf8Reader("test.csv"), (csvRow)-> {
// 只有一行,所以直接判断
Assert.assertEquals("sss,sss", csvRow.get(0));
Assert.assertEquals("姓名", csvRow.get(1));
Assert.assertEquals("性别", csvRow.get(2));
Assert.assertEquals("关注\"对象\"", csvRow.get(3));
Assert.assertEquals("年龄", csvRow.get(4));
Assert.assertEquals("", csvRow.get(5));
Assert.assertEquals("\"", csvRow.get(6));
});
}
@Test
@Ignore
public void readTest3() {
CsvReader reader = CsvUtil.getReader();
String path = FileUtil.isWindows() ? "d:/test/test.csv" : "~/test/test.csv";
reader.read(FileUtil.getUtf8Reader(path), Console::log);
}
@Test
public void readCsvStr1(){
CsvData data = CsvUtil.getReader().readFromStr("# 这是一行注释,读取时应忽略\n" +
"\"sss,sss\",姓名,\"性别\",关注\"对象\",年龄,\"\",\"\"\"\n");
List<CsvRow> rows = data.getRows();
final CsvRow row0 = rows.get(0);
Assert.assertEquals("sss,sss", row0.get(0));
Assert.assertEquals("姓名", row0.get(1));
Assert.assertEquals("性别", row0.get(2));
Assert.assertEquals("关注\"对象\"", row0.get(3));
Assert.assertEquals("年龄", row0.get(4));
Assert.assertEquals("", row0.get(5));
Assert.assertEquals("\"", row0.get(6));
}
@Test
public void readCsvStr2(){
CsvUtil.getReader().readFromStr("# 这是一行注释,读取时应忽略\n" +
"\"sss,sss\",姓名,\"性别\",关注\"对象\",年龄,\"\",\"\"\"\n",(csvRow)-> {
// 只有一行,所以直接判断
Assert.assertEquals("sss,sss", csvRow.get(0));
Assert.assertEquals("姓名", csvRow.get(1));
Assert.assertEquals("性别", csvRow.get(2));
Assert.assertEquals("关注\"对象\"", csvRow.get(3));
Assert.assertEquals("年龄", csvRow.get(4));
Assert.assertEquals("", csvRow.get(5));
Assert.assertEquals("\"", csvRow.get(6));
});
}
@Test
@Ignore
public void writeTest() {
String path = FileUtil.isWindows() ? "d:/test/testWrite.csv" : "~/test/testWrite.csv";
CsvWriter writer = CsvUtil.getWriter(path, CharsetUtil.CHARSET_UTF_8);
writer.write(
new String[] {"a1", "b1", "c1", "123345346456745756756785656"},
new String[] {"a2", "b2", "c2"},
new String[] {"a3", "b3", "c3"}
);
}
@Test
@Ignore
public void writeBeansTest() {
@Data
class Student {
Integer id;
String name;
Integer age;
}
String path = FileUtil.isWindows() ? "d:/test/testWriteBeans.csv" : "~/test/testWriteBeans.csv";
CsvWriter writer = CsvUtil.getWriter(path, CharsetUtil.CHARSET_UTF_8);
List<Student> students = new ArrayList<>();
Student student1 = new Student();
student1.setId(1);
student1.setName("张三");
student1.setAge(18);
Student student2 = new Student();
student2.setId(2);
student2.setName("李四");
student2.setAge(22);
Student student3 = new Student();
student3.setId(3);
student3.setName("王五");
student3.setAge(31);
students.add(student1);
students.add(student2);
students.add(student3);
writer.writeBeans(students);
writer.close();
}
@Test
@Ignore
public void readLfTest(){
final CsvReader reader = CsvUtil.getReader();
String path = FileUtil.isWindows() ? "d:/test/rw_test.csv" : "~/test/rw_test.csv";
final CsvData read = reader.read(FileUtil.file(path));
for (CsvRow row : read) {
Console.log(row);
}
}
@Test
@Ignore
public void writeWrapTest(){
List<List<Object>> resultList=new ArrayList<>();
List<Object> list =new ArrayList<>();
list.add("\"name\"");
list.add("\"code\"");
resultList.add(list);
list =new ArrayList<>();
list.add("\"wang\"");
list.add(1);
resultList.add(list);
String path = FileUtil.isWindows() ? "d:/test/csvWrapTest.csv" : "~/test/csvWrapTest.csv";
final CsvWriter writer = CsvUtil.getWriter(path, CharsetUtil.CHARSET_UTF_8);
writer.write(resultList);
}
@Test
@Ignore
public void writeDataTest(){
@Data
@AllArgsConstructor
class User {
Integer userId;
String username;
String mobile;
}
List<String> header = ListUtil.of("用户id", "用户名", "手机号");
List<CsvRow> row = new ArrayList<>();
List<User> datas = new ArrayList<>();
datas.add(new User(1, "张三", "18800001111"));
datas.add(new User(2, "李四", "18800001112"));
datas.add(new User(3, "王五", "18800001113"));
datas.add(new User(4, "赵六", "18800001114"));
//可以为null
//Map<String, Integer> headMap = null;
Map<String, Integer> headMap = new HashMap<>();
headMap.put("userId", 0);
headMap.put("username", 1);
headMap.put("mobile", 2);
for (User user : datas) {
// row.size() + 1, 表示从第2行开始第一行是标题栏
row.add(new CsvRow(row.size() + 1, headMap,
BeanUtil.beanToMap(user).values().stream().map(Object::toString).collect(Collectors.toList())));
}
CsvData csvData = new CsvData(header, row);
String path = FileUtil.isWindows() ? "d:/test/csvWriteDataTest.csv" : "~/test/csvWriteDataTest.csv";
final CsvWriter writer = CsvUtil.getWriter(path, CharsetUtil.CHARSET_UTF_8);
writer.write(csvData);
}
}

View File

@@ -1,47 +0,0 @@
package cn.hutool.core.text.csv;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.CharsetUtil;
import org.junit.Ignore;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class CsvWriterTest {
@Test
@Ignore
public void writeWithAliasTest(){
final CsvWriteConfig csvWriteConfig = CsvWriteConfig.defaultConfig()
.addHeaderAlias("name", "姓名")
.addHeaderAlias("gender", "性别");
final CsvWriter writer = CsvUtil.getWriter(
FileUtil.file("d:/test/csvAliasTest.csv"),
CharsetUtil.CHARSET_GBK, false, csvWriteConfig);
writer.writeHeaderLine("name", "gender", "address");
writer.writeLine("张三", "", "XX市XX区");
writer.writeLine("李四", "", "XX市XX区,01号");
writer.close();
}
@Test
@Ignore
public void issue2255Test(){
String fileName = "D:/test/" + new Random().nextInt(100) + "-a.csv";
CsvWriter writer = CsvUtil.getWriter(fileName, CharsetUtil.CHARSET_UTF_8);
List<String> list = new ArrayList<>();
for (int i = 0; i < 10000; i++) {
list.add(i+"");
}
Console.log("{} : {}", fileName, list.size());
for (String s : list) {
writer.writeLine(s);
}
writer.close();
}
}

View File

@@ -0,0 +1,167 @@
package cn.hutool.core.text.dfa;
import cn.hutool.core.collection.CollUtil;
import org.junit.Assert;
import org.junit.Test;
import java.util.List;
/**
* DFA单元测试
*
* @author Looly
*
*/
public class DfaTest {
// 构建被查询的文本,包含停顿词
String text = "我有一颗$大土^豆,刚出锅的";
@Test
public void matchAllTest() {
// 构建查询树
WordTree tree = buildWordTree();
// -----------------------------------------------------------------------------------------------------------------------------------
// 情况一:标准匹配,匹配到最短关键词,并跳过已经匹配的关键词
// 匹配到【大】,就不再继续匹配了,因此【大土豆】不匹配
// 匹配到【刚出锅】,就跳过这三个字了,因此【出锅】不匹配(由于刚首先被匹配,因此长的被匹配,最短匹配只针对第一个字相同选最短)
List<String> matchAll = tree.matchAll(text, -1, false, false);
Assert.assertEquals(matchAll, CollUtil.newArrayList("", "土^豆", "刚出锅"));
}
/**
* 密集匹配原则(最短匹配)测试
*/
@Test
public void densityMatchTest() {
// 构建查询树
WordTree tree = buildWordTree();
// -----------------------------------------------------------------------------------------------------------------------------------
// 情况二:匹配到最短关键词,不跳过已经匹配的关键词
// 【大】被匹配,最短匹配原则【大土豆】被跳过,【土豆继续被匹配】
// 【刚出锅】被匹配,由于不跳过已经匹配的词,【出锅】被匹配
List<String> matchAll = tree.matchAll(text, -1, true, false);
Assert.assertEquals(matchAll, CollUtil.newArrayList("", "土^豆", "刚出锅", "出锅"));
}
/**
* 贪婪非密集匹配原则测试
*/
@Test
public void greedMatchTest() {
// 构建查询树
WordTree tree = buildWordTree();
// -----------------------------------------------------------------------------------------------------------------------------------
// 情况三:匹配到最长关键词,跳过已经匹配的关键词
// 匹配到【大】,由于非密集匹配,因此从下一个字符开始查找,匹配到【土豆】接着被匹配
// 由于【刚出锅】被匹配,由于非密集匹配,【出锅】被跳过
List<String> matchAll = tree.matchAll(text, -1, false, true);
Assert.assertEquals(matchAll, CollUtil.newArrayList("", "土^豆", "刚出锅"));
}
/**
* 密集匹配原则(最长匹配)和贪婪匹配原则测试
*/
@Test
public void densityAndGreedMatchTest() {
// 构建查询树
WordTree tree = buildWordTree();
// -----------------------------------------------------------------------------------------------------------------------------------
// 情况四:匹配到最长关键词,不跳过已经匹配的关键词(最全关键词)
// 匹配到【大】,由于到最长匹配,因此【大土豆】接着被匹配,由于不跳过已经匹配的关键词,土豆继续被匹配
// 【刚出锅】被匹配,由于不跳过已经匹配的词,【出锅】被匹配
List<String> matchAll = tree.matchAll(text, -1, true, true);
Assert.assertEquals(matchAll, CollUtil.newArrayList("", "大土^豆", "土^豆", "刚出锅", "出锅"));
}
@Test
public void densityAndGreedMatchTest2(){
WordTree tree = new WordTree();
tree.addWord("");
tree.addWord("赵阿");
tree.addWord("赵阿三");
final List<FoundWord> result = tree.matchAllWords("赵阿三在做什么", -1, true, true);
Assert.assertEquals(3, result.size());
Assert.assertEquals("", result.get(0).getWord());
Assert.assertEquals(0, result.get(0).getStartIndex().intValue());
Assert.assertEquals(0, result.get(0).getEndIndex().intValue());
Assert.assertEquals("赵阿", result.get(1).getWord());
Assert.assertEquals(0, result.get(1).getStartIndex().intValue());
Assert.assertEquals(1, result.get(1).getEndIndex().intValue());
Assert.assertEquals("赵阿三", result.get(2).getWord());
Assert.assertEquals(0, result.get(2).getStartIndex().intValue());
Assert.assertEquals(2, result.get(2).getEndIndex().intValue());
}
/**
* 停顿词测试
*/
@Test
public void stopWordTest() {
WordTree tree = new WordTree();
tree.addWord("tio");
List<String> all = tree.matchAll("AAAAAAAt-ioBBBBBBB");
Assert.assertEquals(all, CollUtil.newArrayList("t-io"));
}
@Test
public void aTest(){
WordTree tree = new WordTree();
tree.addWord("women");
String text = "a WOMEN todo.".toLowerCase();
List<String> matchAll = tree.matchAll(text, -1, false, false);
Assert.assertEquals("[women]", matchAll.toString());
}
@Test
public void clearTest(){
WordTree tree = new WordTree();
tree.addWord("");
Assert.assertTrue(tree.matchAll("黑大衣").contains(""));
//clear时直接调用Map的clear并没有把endCharacterSet清理掉
tree.clear();
tree.addWords("黑大衣","红色大衣");
//clear() 覆写前 这里想匹配到黑大衣,但是却匹配到了黑
// Assert.assertFalse(tree.matchAll("黑大衣").contains("黑大衣"));
// Assert.assertTrue(tree.matchAll("黑大衣").contains("黑"));
//clear() 覆写后
Assert.assertTrue(tree.matchAll("黑大衣").contains("黑大衣"));
Assert.assertFalse(tree.matchAll("黑大衣").contains(""));
Assert.assertTrue(tree.matchAll("红色大衣").contains("红色大衣"));
//如果不覆写只能通过new出新对象才不会有问题
tree = new WordTree();
tree.addWords("黑大衣","红色大衣");
Assert.assertTrue(tree.matchAll("黑大衣").contains("黑大衣"));
Assert.assertTrue(tree.matchAll("红色大衣").contains("红色大衣"));
}
// ----------------------------------------------------------------------------------------------------------
/**
* 构建查找树
*
* @return 查找树
*/
private WordTree buildWordTree() {
// 构建查询树
WordTree tree = new WordTree();
tree.addWord("");
tree.addWord("大土豆");
tree.addWord("土豆");
tree.addWord("刚出锅");
tree.addWord("出锅");
return tree;
}
}

View File

@@ -0,0 +1,42 @@
package cn.hutool.core.text.dfa;
import cn.hutool.core.collection.ListUtil;
import lombok.Data;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class SensitiveUtilTest {
@Test
public void testSensitiveFilter() {
List<String> wordList = new ArrayList<>();
wordList.add("");
wordList.add("大土豆");
wordList.add("土豆");
wordList.add("刚出锅");
wordList.add("出锅");
TestBean bean = new TestBean();
bean.setStr("我有一颗$大土^豆,刚出锅的");
bean.setNum(100);
SensitiveUtil.init(wordList);
String beanStr = SensitiveUtil.sensitiveFilter(bean.getStr(), true, null);
Assert.assertEquals("我有一颗$*******的", beanStr);
}
@Data
public static class TestBean {
private String str;
private Integer num;
}
@Test
public void issue2126(){
SensitiveUtil.init(ListUtil.of("", "赵阿", "赵阿三"));
String result = SensitiveUtil.sensitiveFilter("赵阿三在做什么。", true, null);
Assert.assertEquals("***在做什么。", result);
}
}

View File

@@ -1,5 +1,6 @@
package cn.hutool.core.util;
import cn.hutool.core.text.escape.EscapeUtil;
import org.junit.Assert;
import org.junit.Test;

View File

@@ -1,5 +1,6 @@
package cn.hutool.core.util;
import cn.hutool.core.codec.HexUtil;
import org.junit.Assert;
import org.junit.Test;

View File

@@ -0,0 +1,39 @@
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() {
CompiledScript script = ScriptUtil.compile("print('Script test!');");
try {
script.eval();
} catch (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

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