From 771f0a48090d1d9b41222511c658f7deb5cfa37d Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 9 Feb 2023 19:43:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E2=80=9Csax=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=E8=AF=BB=E5=8F=96excel2003=E7=89=88=E6=9C=AC=EF=BC=8C?= =?UTF-8?q?=E4=BC=9A=E8=B0=83=E7=94=A8=E4=B8=A4=E6=AC=A1doAfterAllAnalysed?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E2=80=9D=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../cn/hutool/core/collection/CollUtil.java | 29 ----------- .../hutool/core/collection/CollUtilTest.java | 49 +++++-------------- .../cn/hutool/poi/excel/ExcelSaxReadTest.java | 35 +++++++------ .../cn/hutool/poi/excel/ExcelUtilTest.java | 45 ++++++----------- 5 files changed, 46 insertions(+), 113 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6130b247a..030515ea9 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ * 【core 】 修复ClassScanner自定义classload无效问题(issue#I68TV2@Gitee) * 【core 】 【重要】删除XmlUtil.readObjectFromXml方法,避免漏洞(issue#2857@Github) * 【core 】 修复Ipv4Util.list()方法的bug(pr#929@Gitee) +* 【poi 】 修复“sax方式读取excel2003版本,会调用两次doAfterAllAnalysed方法”问题。(pr#919@Gitee) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java index 0fcfb39c8..9f46e9254 100755 --- a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java @@ -1659,35 +1659,6 @@ public class CollUtil { return collection == null || collection.isEmpty(); } - /** - * 集合是否为空。 - * 如果集合中所有元素为null或空串,也认为此集合为空。 - * @param collection - * @return - */ - public static boolean isBlank(Collection collection) { - if(isEmpty(collection)){ - return true; - } - - for(Object o: collection){ - if(ObjectUtil.isNotEmpty(o)){ - return false; - } - } - return true; - } - - /** - * 集合是否为非空。 - * 集合长度大于0,且所有元素中至少有一个不为null或空串。 - * @param collection - * @return - */ - public static boolean isNotBlank(Collection collection) { - return false == isBlank(collection); - } - /** * 如果给定集合为空,返回默认集合 * diff --git a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java index 5400cd5fe..75ab23e26 100755 --- a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java @@ -306,7 +306,7 @@ public class CollUtilTest { final List removed = new ArrayList<>(); final ArrayList filtered = CollUtil.filter(list, t -> { - if("a".equals(t)){ + if ("a".equals(t)) { removed.add(t); return false; } @@ -767,7 +767,7 @@ public class CollUtilTest { } @Test - public void mapToMapTest(){ + public void mapToMapTest() { final HashMap oldMap = new HashMap<>(); oldMap.put("a", "1"); oldMap.put("b", "12"); @@ -778,9 +778,9 @@ public class CollUtilTest { Map.Entry::getKey, entry -> Long.parseLong(entry.getValue())); - Assert.assertEquals(1L, (long)map.get("a")); - Assert.assertEquals(12L, (long)map.get("b")); - Assert.assertEquals(134L, (long)map.get("c")); + Assert.assertEquals(1L, (long) map.get("a")); + Assert.assertEquals(12L, (long) map.get("b")); + Assert.assertEquals(134L, (long) map.get("c")); } @Test @@ -834,7 +834,7 @@ public class CollUtilTest { final List result = CollUtil.subtractToList(list1, list2); Assert.assertEquals(1, result.size()); - Assert.assertEquals(1L, (long)result.get(0)); + Assert.assertEquals(1L, (long) result.get(0)); } @Test @@ -845,7 +845,7 @@ public class CollUtilTest { } @Test - public void setValueByMapTest(){ + public void setValueByMapTest() { // https://gitee.com/dromara/hutool/pulls/482 final List people = Arrays.asList( new Person("aa", 12, "man", 1), @@ -886,13 +886,13 @@ public class CollUtilTest { } @Test - public void distinctTest(){ + public void distinctTest() { final ArrayList distinct = CollUtil.distinct(ListUtil.of(5, 3, 10, 9, 0, 5, 10, 9)); Assert.assertEquals(ListUtil.of(5, 3, 10, 9, 0), distinct); } @Test - public void distinctByFunctionTest(){ + public void distinctByFunctionTest() { final List people = Arrays.asList( new Person("aa", 12, "man", 1), new Person("bb", 13, "woman", 2), @@ -941,8 +941,7 @@ public class CollUtilTest { final List list = CollUtil.unionAll(list1, list2, list3); Assert.assertNotNull(list); - @SuppressWarnings("ConfusingArgumentToVarargsMethod") - final List resList2 = CollUtil.unionAll(null, null, null); + @SuppressWarnings("ConfusingArgumentToVarargsMethod") final List resList2 = CollUtil.unionAll(null, null, null); Assert.assertNotNull(resList2); } @@ -973,8 +972,7 @@ public class CollUtilTest { public void unionAllOtherIsNullTest() { final List list1 = CollectionUtil.newArrayList(1, 2, 2, 3, 3); final List list2 = CollectionUtil.newArrayList(1, 2, 3); - @SuppressWarnings("ConfusingArgumentToVarargsMethod") - final List list = CollUtil.unionAll(list1, list2, null); + @SuppressWarnings("ConfusingArgumentToVarargsMethod") final List list = CollUtil.unionAll(list1, list2, null); Assert.assertNotNull(list); Assert.assertArrayEquals( CollectionUtil.newArrayList(1, 2, 2, 3, 3, 1, 2, 3).toArray(), @@ -1042,32 +1040,9 @@ public class CollUtilTest { } @Test - public void getFirstTest(){ + public void getFirstTest() { final List nullList = null; final Object first = CollUtil.getFirst(nullList); Assert.assertNull(first); } - - @Test - public void blankTest() { - List strs = new ArrayList<>(); - strs.add(null); - strs.add(""); - strs.add(""); - - boolean c = CollUtil.isBlank(strs); - Assert.assertEquals(true, c ); - - - List arrs = new ArrayList<>(); - arrs.add(null); - arrs.add(""); - arrs.add(" "); - arrs.add(""); - arrs.add(" a "); - - boolean d = CollUtil.isNotBlank(arrs); - Assert.assertEquals(true, d ); - } - } diff --git a/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelSaxReadTest.java b/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelSaxReadTest.java index a4f9d13c3..afc7e7646 100644 --- a/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelSaxReadTest.java +++ b/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelSaxReadTest.java @@ -51,7 +51,7 @@ public class ExcelSaxReadTest { @Test public void excel03Test() { - Excel03SaxReader reader = new Excel03SaxReader(createRowHandler()); + final Excel03SaxReader reader = new Excel03SaxReader(createRowHandler()); reader.read("aaa.xls", 1); // Console.log("Sheet index: [{}], Sheet name: [{}]", reader.getSheetIndex(), reader.getSheetName()); @@ -60,7 +60,7 @@ public class ExcelSaxReadTest { @Test public void excel03ByNameTest() { - Excel03SaxReader reader = new Excel03SaxReader(createRowHandler()); + final Excel03SaxReader reader = new Excel03SaxReader(createRowHandler()); reader.read("aaa.xls", "校园入学"); reader.read("aaa.xls", "sheetName:校园入学"); } @@ -68,7 +68,7 @@ public class ExcelSaxReadTest { @Test(expected = POIException.class) public void excel03ByNameErrorTest() { // sheet名称不存在则报错 - Excel03SaxReader reader = new Excel03SaxReader(createRowHandler()); + final Excel03SaxReader reader = new Excel03SaxReader(createRowHandler()); reader.read("aaa.xls", "校园入学1"); } @@ -120,12 +120,12 @@ public class ExcelSaxReadTest { ExcelUtil.readBySax("d:/test/test.xlsx", -1, new RowHandler() { @Override - public void handleCell(int sheetIndex, long rowIndex, int cellIndex, Object value, CellStyle xssfCellStyle) { + public void handleCell(final int sheetIndex, final long rowIndex, final int cellIndex, final Object value, final CellStyle xssfCellStyle) { Console.log("{} {} {}", rowIndex, cellIndex, value); } @Override - public void handle(int sheetIndex, long rowIndex, List rowCells) { + public void handle(final int sheetIndex, final long rowIndex, final List rowCells) { } } @@ -133,17 +133,16 @@ public class ExcelSaxReadTest { } @Test - @Ignore public void handle03CellTest() { - ExcelUtil.readBySax("d:/test/test.xls", -1, new RowHandler() { + ExcelUtil.readBySax("test.xls", -1, new RowHandler() { @Override - public void handleCell(int sheetIndex, long rowIndex, int cellIndex, Object value, CellStyle xssfCellStyle) { - Console.log("{} {} {}", rowIndex, cellIndex, value); + public void handleCell(final int sheetIndex, final long rowIndex, final int cellIndex, final Object value, final CellStyle xssfCellStyle) { + //Console.log("{} {} {}", rowIndex, cellIndex, value); } @Override - public void handle(int sheetIndex, long rowIndex, List rowCells) { + public void handle(final int sheetIndex, final long rowIndex, final List rowCells) { } } ); @@ -151,11 +150,11 @@ public class ExcelSaxReadTest { @Test public void formulaRead03Test() { - List rows = new ArrayList<>(); + final List rows = new ArrayList<>(); ExcelUtil.readBySax("data_for_sax_test.xls", -1, (i, i1, list) -> { - if(list.size() > 1){ + if (list.size() > 1) { rows.add(list.get(1)); - } else{ + } else { rows.add(""); } }); @@ -164,7 +163,7 @@ public class ExcelSaxReadTest { @Test public void formulaRead07Test() { - List rows = new ArrayList<>(); + final List rows = new ArrayList<>(); ExcelUtil.readBySax("data_for_sax_test.xlsx", 0, (i, i1, list) -> rows.add(list.get(1))); @@ -174,7 +173,7 @@ public class ExcelSaxReadTest { @Test public void dateReadXlsTest() { - List rows = new ArrayList<>(); + final List rows = new ArrayList<>(); ExcelUtil.readBySax("data_for_sax_test.xls", 0, (i, i1, list) -> rows.add(StrUtil.toString(list.get(0))) ); @@ -188,7 +187,7 @@ public class ExcelSaxReadTest { @Test public void dateReadXlsxTest() { - List rows = new ArrayList<>(); + final List rows = new ArrayList<>(); ExcelUtil.readBySax("data_for_sax_test.xlsx", 0, (i, i1, list) -> rows.add(StrUtil.toString(list.get(0))) ); @@ -211,7 +210,7 @@ public class ExcelSaxReadTest { @Test @Ignore public void readBlankTest() { - File file = new File("D:/test/b.xlsx"); + final File file = new File("D:/test/b.xlsx"); ExcelUtil.readBySax(file, 0, (sheetIndex, rowIndex, rowList) -> rowList.forEach(Console::log)); @@ -220,7 +219,7 @@ public class ExcelSaxReadTest { @Test @Ignore - public void readXlsmTest(){ + public void readXlsmTest() { ExcelUtil.readBySax("d:/test/WhiteListTemplate.xlsm", -1, (sheetIndex, rowIndex, rowlist) -> Console.log("[{}] [{}] {}", sheetIndex, rowIndex, rowlist)); } diff --git a/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelUtilTest.java b/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelUtilTest.java index 9a3dcf1d4..06d0bfc76 100644 --- a/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelUtilTest.java +++ b/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelUtilTest.java @@ -1,19 +1,10 @@ package cn.hutool.poi.excel; -import cn.hutool.core.collection.CollUtil; -import cn.hutool.core.collection.CollectionUtil; -import cn.hutool.core.util.ArrayUtil; -import cn.hutool.core.util.ObjectUtil; -import cn.hutool.core.util.StrUtil; import cn.hutool.poi.excel.cell.CellLocation; import cn.hutool.poi.excel.sax.handler.RowHandler; -import org.apache.poi.ss.usermodel.CellStyle; import org.junit.Assert; import org.junit.Test; -import java.io.FileInputStream; -import java.io.InputStream; -import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; @@ -59,39 +50,35 @@ public class ExcelUtilTest { @Test public void readAndWriteTest() { - ExcelReader reader = ExcelUtil.getReader("aaa.xlsx"); - ExcelWriter writer = reader.getWriter(); + final ExcelReader reader = ExcelUtil.getReader("aaa.xlsx"); + final ExcelWriter writer = reader.getWriter(); writer.writeCellValue(1, 2, "设置值"); writer.close(); } @Test public void getReaderByBookFilePathAndSheetNameTest() { - ExcelReader reader = ExcelUtil.getReader("aaa.xlsx", "12"); - List> list = reader.readAll(); + final ExcelReader reader = ExcelUtil.getReader("aaa.xlsx", "12"); + final List> list = reader.readAll(); reader.close(); Assert.assertEquals(1L, list.get(1).get("鞋码")); } @Test public void doAfterAllAnalysedTest() { - String path = "readBySax.xls"; - AtomicInteger doAfterAllAnalysedTime = new AtomicInteger(0); - try{ - ExcelUtil.readBySax(path, -1, new RowHandler() { - @Override - public void handle(int sheetIndex, long rowIndex, List rowCells) { - System.out.println(StrUtil.format("sheetIndex={};rowIndex={},rowCells={}",sheetIndex,rowIndex,rowCells)); - } + final String path = "readBySax.xls"; + final AtomicInteger doAfterAllAnalysedTime = new AtomicInteger(0); + ExcelUtil.readBySax(path, -1, new RowHandler() { + @Override + public void handle(final int sheetIndex, final long rowIndex, final List rowCells) { + //Console.log("sheetIndex={};rowIndex={},rowCells={}",sheetIndex,rowIndex,rowCells); + } - @Override - public void doAfterAllAnalysed() { - doAfterAllAnalysedTime.addAndGet(1); - } - }); - }catch (Exception ex){ - ex.printStackTrace(); - } + @Override + public void doAfterAllAnalysed() { + doAfterAllAnalysedTime.addAndGet(1); + } + }); //总共2个sheet页,读取所有sheet时,一共执行doAfterAllAnalysed2次。 Assert.assertEquals(2, doAfterAllAnalysedTime.intValue()); }