From dca98b258201f7102ff074f44231f3ae1218c0b4 Mon Sep 17 00:00:00 2001 From: hades Date: Wed, 18 Mar 2020 19:54:45 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-28383=20=E8=AE=BE=E8=AE=A1=E5=99=A8?= =?UTF-8?q?=E8=8F=9C=E5=8D=95=E6=A0=8F-=E6=96=87=E4=BB=B6-=E8=BE=93?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fr/file/filter/ChooseFileFilter.java | 28 ++++++++-------- .../java/com/fr/file/FILEChooserPaneTest.java | 10 ++++++ .../fr/file/filter/ChooseFileFilterTest.java | 33 +++++++++++++++++++ 3 files changed, 58 insertions(+), 13 deletions(-) create mode 100644 designer-base/src/test/java/com/fr/file/filter/ChooseFileFilterTest.java diff --git a/designer-base/src/main/java/com/fr/file/filter/ChooseFileFilter.java b/designer-base/src/main/java/com/fr/file/filter/ChooseFileFilter.java index a25ebe7b6e..147e1946d4 100644 --- a/designer-base/src/main/java/com/fr/file/filter/ChooseFileFilter.java +++ b/designer-base/src/main/java/com/fr/file/filter/ChooseFileFilter.java @@ -4,6 +4,8 @@ import com.fr.base.extension.FileExtension; import com.fr.file.FILE; import com.fr.file.FileFILE; import com.fr.general.ComparatorUtils; +import com.fr.stable.CoreConstants; +import com.fr.stable.StringUtils; import javax.swing.filechooser.FileFilter; import java.io.File; @@ -275,25 +277,25 @@ public class ChooseFileFilter extends FileFilter implements FILEFilter, java.io. } /** + * 默认情况下 返回首位的后缀名 * get extention. */ public String getExtensionString() { - if (filters == null) { - return ""; - } - - StringBuffer extsb = new StringBuffer("."); - -// Enumeration keys = filters.keys(); -// while (keys.hasMoreElements()) { -// extsb.append((String) keys.nextElement()); -// } + return getExtensionString(0); + } - for (int i = 0; i < filters.size(); i++) { - extsb.append(filters.get(i)); + public String getExtensionString(int index) { + if (filters == null || filters.isEmpty() || index >= filters.size()) { + return StringUtils.EMPTY; } + return CoreConstants.DOT + filters.get(index); + } - return extsb.toString(); + public int getExtensionCount() { + if (filters == null || filters.isEmpty()) { + return 0; + } + return filters.size(); } /** diff --git a/designer-base/src/test/java/com/fr/file/FILEChooserPaneTest.java b/designer-base/src/test/java/com/fr/file/FILEChooserPaneTest.java index 79a3ea0f2e..064cd05cec 100644 --- a/designer-base/src/test/java/com/fr/file/FILEChooserPaneTest.java +++ b/designer-base/src/test/java/com/fr/file/FILEChooserPaneTest.java @@ -39,6 +39,16 @@ public class FILEChooserPaneTest { String result6 = Reflect.on(chooserPane).call("calProperFileName", "WorkBook1.xls", chooseFileFilter6).get(); Assert.assertEquals("WorkBook1.xls.cpt", result6); + ChooseFileFilter chooseFileFilter7 = new ChooseFileFilter(FileExtension.XLS, StringUtils.EMPTY); + chooseFileFilter7.addExtension(".xlsx"); + String result7 = Reflect.on(chooserPane).call("calProperFileName", "WorkBook1", chooseFileFilter7).get(); + Assert.assertEquals("WorkBook1.xls", result7); + + ChooseFileFilter chooseFileFilter8 = new ChooseFileFilter(FileExtension.XLSX, StringUtils.EMPTY); + chooseFileFilter8.addExtension(".xls"); + String result8 = Reflect.on(chooserPane).call("calProperFileName", "WorkBook1", chooseFileFilter8).get(); + Assert.assertEquals("WorkBook1.xlsx", result8); + } } diff --git a/designer-base/src/test/java/com/fr/file/filter/ChooseFileFilterTest.java b/designer-base/src/test/java/com/fr/file/filter/ChooseFileFilterTest.java new file mode 100644 index 0000000000..ec6779bab0 --- /dev/null +++ b/designer-base/src/test/java/com/fr/file/filter/ChooseFileFilterTest.java @@ -0,0 +1,33 @@ +package com.fr.file.filter; + +import com.fr.base.extension.FileExtension; +import com.fr.stable.CoreConstants; +import com.fr.stable.StringUtils; +import junit.framework.TestCase; +import org.junit.Assert; + + +/** + * @author hades + * @version 10.0 + * Created by hades on 2020/3/18 + */ +public class ChooseFileFilterTest extends TestCase { + + public void testGetExtensionString() { + ChooseFileFilter chooseFileFilter = new ChooseFileFilter(FileExtension.XLSX, StringUtils.EMPTY); + chooseFileFilter.addExtension(FileExtension.XLS.getExtension()); + Assert.assertEquals(CoreConstants.DOT + FileExtension.XLSX.getExtension(), chooseFileFilter.getExtensionString()); + Assert.assertEquals(CoreConstants.DOT + FileExtension.XLS.getExtension(), chooseFileFilter.getExtensionString(1)); + ChooseFileFilter chooseFileFilter1 = new ChooseFileFilter(); + Assert.assertEquals(StringUtils.EMPTY, chooseFileFilter1.getExtensionString()); + Assert.assertEquals(StringUtils.EMPTY, chooseFileFilter1.getExtensionString(1)); + + } + + public void testGetExtensionCount() { + ChooseFileFilter chooseFileFilter = new ChooseFileFilter(FileExtension.CPT, StringUtils.EMPTY); + chooseFileFilter.addExtension(FileExtension.CPTX.getExtension()); + Assert.assertEquals(2, chooseFileFilter.getExtensionCount()); + } +} \ No newline at end of file