gpt4 book ai didi

java - 如何从 apache POI dataValidators 正确转换 ExplicitListValues 进行测试?

转载 作者:行者123 更新时间:2023-11-30 01:56:30 27 4
gpt4 key购买 nike

我已将 dataConstraints 添加到我的 Apache POI 工作簿的工作表中。现在我想添加一个单元测试。

我的约束是两个值的简单列表,"is"和“否”。

我当前的尝试:

Assert.assertEquals(
Arrays.toString("yes,no".split(",")),
Arrays.toString(dv.get(0).getValidationConstraint().getExplicitListValues()));
}

失败并显示

expected:<[[yes, no]]> but was:<[["yes, no"]]

我很确定我遗漏了一些关于 java 类型转换逻辑的明显内容,但无法指出具体是什么。

--编辑1:

我尝试将列表创建为

XSSFDataValidationConstraint c =
(XSSFDataValidationConstraint) dvHelper.createExplicitListConstraint(
new String[]{"yes", "no"});

XSSFDataValidationConstraint c =
(XSSFDataValidationConstraint) dvHelper.createExplicitListConstraint(
"yes,no".split(","));

没关系,Arrays.toString( ...getExplicitListValues()) 的输出将始终显示额外的引号!

--编辑2:

为了尝试解决这个问题,我按照 @axel-ichter 建议的方式创建了列表:

XSSFDataValidationConstraint c =
(XSSFDataValidationConstraint) dvHelper.createExplicitListConstraint(
"\"yes,no".split(","));

然后我得到了其他东西:

expected:<[[yes, no]]> but was:<[[""yes, no"]]>

--编辑3

ArrayUtils.contains( ....getExplicitListValues(), "yes") // returns null;

...getExplicitListValues().getClass().getTypeName() // java.lang.String[]

最佳答案

无法重现您的问题。使用apache poi 4.0.1以下代码:

import java.io.FileOutputStream;
import java.io.FileInputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddressList;
import java.util.List;
import java.util.Arrays;

class CreateExcelDataValidationLists {

public static void main(String[] args) throws Exception {

Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
DataValidationHelper dvHelper = sheet.getDataValidationHelper();
String constraint = "yes,no";
DataValidationConstraint dvConstraint = dvHelper.createExplicitListConstraint(constraint.split(",")) ;
CellRangeAddressList addressList = new CellRangeAddressList(0,0,0,0);
DataValidation validation = dvHelper.createValidation(dvConstraint, addressList);
sheet.addValidationData(validation);

FileOutputStream out = new FileOutputStream("CreateExcelDataValidationLists.xlsx");
workbook.write(out);
out.close();
workbook.close();

Thread.sleep(1000);

workbook = WorkbookFactory.create(new FileInputStream("CreateExcelDataValidationLists.xlsx"));
sheet = workbook.getSheetAt(0);

@SuppressWarnings("unchecked")
List<DataValidation> validations = (List<DataValidation>)sheet.getDataValidations();

System.out.println(Arrays.toString(validations.get(0).getValidationConstraint().getExplicitListValues()));

}
}

产品:

[yes, no]
<小时/>

刚刚测试过。使用 apache poi 3.14 的相同代码确实会产生

["yes, no"]

关于java - 如何从 apache POI dataValidators 正确转换 ExplicitListValues 进行测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54318716/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com