mirror of https://github.com/apache/poi.git
Sonar fixes
add asserts to tests git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1885859 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
904c77e242
commit
54fde895e6
|
@ -133,28 +133,30 @@ public final class DateFormatConverter {
|
|||
result.put( "S", "0" );
|
||||
result.put( "SS", "00" );
|
||||
result.put( "SSS", "000" );
|
||||
result.put( "y", "yyyy" );
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String getPrefixForLocale( Locale locale ) {
|
||||
final String languageTag = locale.toLanguageTag();
|
||||
if ("".equals(languageTag)) {
|
||||
if (Locale.ROOT.equals(locale) || "".equals(languageTag)) {
|
||||
// JDK 8 adds an empty locale-string, see also https://issues.apache.org/jira/browse/LANG-941
|
||||
return "[$-0409]";
|
||||
return "";
|
||||
}
|
||||
|
||||
LocaleID loc = LocaleID.lookupByLanguageTag(languageTag);
|
||||
if (loc == null) {
|
||||
String cmpTag = (languageTag.indexOf('_') > -1) ? languageTag.replace('_','-') : languageTag;
|
||||
String cmpTag = (languageTag.indexOf('_') > -1) ? languageTag.replace('_', '-') : languageTag;
|
||||
int idx = languageTag.length();
|
||||
while (loc == null && (idx = cmpTag.lastIndexOf('-', idx-1)) > 0) {
|
||||
while (loc == null && (idx = cmpTag.lastIndexOf('-', idx - 1)) > 0) {
|
||||
loc = LocaleID.lookupByLanguageTag(languageTag.substring(0, idx));
|
||||
}
|
||||
if (loc == null) {
|
||||
LOG.log( POILogger.ERROR, "Unable to find prefix for Locale '", languageTag, "' or its parent locales." );
|
||||
return "";
|
||||
}
|
||||
|
||||
if (loc == null) {
|
||||
LOG.log(POILogger.ERROR, "Unable to find prefix for Locale '", languageTag, "' or its parent locales.");
|
||||
return "";
|
||||
}
|
||||
|
||||
return String.format(Locale.ROOT, "[$-%04X]", loc.getLcid());
|
||||
|
|
|
@ -17,20 +17,19 @@
|
|||
|
||||
package org.apache.poi.xssf.usermodel;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.poi.ss.usermodel.BaseTestNamedRange;
|
||||
import org.apache.poi.ss.util.CellRangeAddress;
|
||||
import org.apache.poi.xssf.XSSFTestDataSamples;
|
||||
import org.apache.poi.xssf.XSSFITestDataProvider;
|
||||
import org.apache.poi.xssf.XSSFTestDataSamples;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* @author Yegor Kozlov
|
||||
*/
|
||||
public final class TestXSSFName extends BaseTestNamedRange {
|
||||
|
||||
public TestXSSFName() {
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -19,6 +19,7 @@ package org.apache.poi.hdgf.streams;
|
|||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
@ -60,7 +61,8 @@ public final class TestStreamBugs extends StreamTest {
|
|||
@Test
|
||||
void testGetTrailer() {
|
||||
Pointer trailerPointer = ptrFactory.createPointer(contents, 0x24);
|
||||
Stream.createStream(trailerPointer, contents, chunkFactory, ptrFactory);
|
||||
Stream s = Stream.createStream(trailerPointer, contents, chunkFactory, ptrFactory);
|
||||
assertNotNull(s);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
package org.apache.poi.ss.usermodel;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
@ -1318,7 +1319,7 @@ public abstract class BaseTestBugzillaIssues {
|
|||
sheet.setRowGroupCollapsed(0, true);
|
||||
|
||||
sheet.groupColumn(0, 0);
|
||||
sheet.setColumnGroupCollapsed(0, true);
|
||||
assertDoesNotThrow(() -> sheet.setColumnGroupCollapsed(0, true));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1328,7 +1329,7 @@ public abstract class BaseTestBugzillaIssues {
|
|||
void test58648() throws IOException {
|
||||
try (Workbook wb = _testDataProvider.createWorkbook()) {
|
||||
Cell cell = wb.createSheet().createRow(0).createCell(0);
|
||||
cell.setCellFormula("((1 + 1) )");
|
||||
assertDoesNotThrow(() -> cell.setCellFormula("((1 + 1) )"));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1441,9 +1442,11 @@ public abstract class BaseTestBugzillaIssues {
|
|||
return time() - startTimeMillis;
|
||||
}
|
||||
|
||||
@Disabled("bug 59393")
|
||||
@Test
|
||||
void bug59393_commentsCanHaveSameAnchor() throws IOException {
|
||||
// only executed for HSSF currently
|
||||
assumeTrue("xls".equals(_testDataProvider.getStandardFileNameExtension()));
|
||||
|
||||
try (Workbook wb = _testDataProvider.createWorkbook()) {
|
||||
|
||||
Sheet sheet = wb.createSheet();
|
||||
|
@ -1473,14 +1476,13 @@ public abstract class BaseTestBugzillaIssues {
|
|||
Comment comment2 = drawing.createCellComment(anchor);
|
||||
RichTextString richTextString2 = helper.createRichTextString("comment2");
|
||||
comment2.setString(richTextString2);
|
||||
cell2.setCellComment(comment2);
|
||||
assertDoesNotThrow(() -> cell2.setCellComment(comment2));
|
||||
|
||||
// anchor.setCol1(2);
|
||||
Comment comment3 = drawing.createCellComment(anchor);
|
||||
RichTextString richTextString3 = helper.createRichTextString("comment3");
|
||||
comment3.setString(richTextString3);
|
||||
cell3.setCellComment(comment3);
|
||||
|
||||
assertDoesNotThrow(() -> cell3.setCellComment(comment3));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1221,7 +1221,8 @@ public abstract class BaseTestConditionalFormatting {
|
|||
CellRangeAddress.valueOf("C9:D30"), CellRangeAddress.valueOf("C7:C31")
|
||||
};
|
||||
ConditionalFormattingRule rule = sheet.getSheetConditionalFormatting().createConditionalFormattingRule("$A$1>0");
|
||||
sheet.getSheetConditionalFormatting().addConditionalFormatting(ranges, rule);
|
||||
int form = sheet.getSheetConditionalFormatting().addConditionalFormatting(ranges, rule);
|
||||
assertEquals(0, form);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -17,14 +17,14 @@
|
|||
|
||||
package org.apache.poi.ss.usermodel;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.apache.poi.ss.ITestDataProvider;
|
||||
import org.apache.poi.ss.usermodel.DataValidation.ErrorStyle;
|
||||
import org.apache.poi.ss.usermodel.DataValidationConstraint.OperatorType;
|
||||
import org.apache.poi.ss.usermodel.DataValidationConstraint.ValidationType;
|
||||
import org.apache.poi.ss.util.CellRangeAddress;
|
||||
import org.apache.poi.ss.util.CellRangeAddressList;
|
||||
import org.apache.poi.util.POILogFactory;
|
||||
import org.apache.poi.util.POILogger;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
|
@ -35,18 +35,18 @@ import org.junit.jupiter.api.Test;
|
|||
public abstract class BaseTestDataValidation {
|
||||
private final ITestDataProvider _testDataProvider;
|
||||
|
||||
private static final POILogger log = POILogFactory.getLogger(BaseTestDataValidation.class);
|
||||
|
||||
protected BaseTestDataValidation(ITestDataProvider testDataProvider) {
|
||||
_testDataProvider = testDataProvider;
|
||||
}
|
||||
|
||||
/** Convenient access to ERROR_STYLE constants */
|
||||
/**
|
||||
* Convenient access to ERROR_STYLE constants
|
||||
*/
|
||||
protected static final DataValidation.ErrorStyle ES = null;
|
||||
/** Convenient access to OPERATOR constants */
|
||||
/**
|
||||
* Convenient access to OPERATOR constants
|
||||
*/
|
||||
protected static final DataValidationConstraint.ValidationType VT = null;
|
||||
/** Convenient access to OPERATOR constants */
|
||||
protected static final DataValidationConstraint.OperatorType OP = null;
|
||||
|
||||
private static final class ValidationAdder {
|
||||
|
||||
|
@ -66,6 +66,7 @@ public abstract class BaseTestDataValidation {
|
|||
_validationType = validationType;
|
||||
_currentRowIndex = fSheet.getPhysicalNumberOfRows();
|
||||
}
|
||||
|
||||
void addValidation(int operatorType, String firstFormula, String secondFormula,
|
||||
int errorStyle, String ruleDescr, String promptDescr,
|
||||
boolean allowEmpty, boolean inputBox, boolean errorBox) {
|
||||
|
@ -83,9 +84,9 @@ public abstract class BaseTestDataValidation {
|
|||
int rowNum = _currentRowIndex++;
|
||||
|
||||
DataValidationHelper dataValidationHelper = _sheet.getDataValidationHelper();
|
||||
DataValidationConstraint dc = createConstraint(dataValidationHelper,operatorType, firstFormula, secondFormula, explicitListValues);
|
||||
DataValidationConstraint dc = createConstraint(dataValidationHelper, operatorType, firstFormula, secondFormula, explicitListValues);
|
||||
|
||||
DataValidation dv = dataValidationHelper.createValidation(dc,new CellRangeAddressList(rowNum, rowNum, 0, 0));
|
||||
DataValidation dv = dataValidationHelper.createValidation(dc, new CellRangeAddressList(rowNum, rowNum, 0, 0));
|
||||
|
||||
dv.setEmptyCellAllowed(allowEmpty);
|
||||
dv.setErrorStyle(errorStyle);
|
||||
|
@ -107,7 +108,8 @@ public abstract class BaseTestDataValidation {
|
|||
}
|
||||
writeOtherSettings(_sheet, _style_1, promptDescr);
|
||||
}
|
||||
private DataValidationConstraint createConstraint(DataValidationHelper dataValidationHelper,int operatorType, String firstFormula,
|
||||
|
||||
private DataValidationConstraint createConstraint(DataValidationHelper dataValidationHelper, int operatorType, String firstFormula,
|
||||
String secondFormula, String[] explicitListValues) {
|
||||
if (_validationType == ValidationType.LIST) {
|
||||
if (explicitListValues != null) {
|
||||
|
@ -125,17 +127,18 @@ public abstract class BaseTestDataValidation {
|
|||
return dataValidationHelper.createCustomConstraint(firstFormula);
|
||||
}
|
||||
|
||||
if( _validationType == ValidationType.INTEGER) {
|
||||
if (_validationType == ValidationType.INTEGER) {
|
||||
return dataValidationHelper.createIntegerConstraint(operatorType, firstFormula, secondFormula);
|
||||
}
|
||||
if( _validationType == ValidationType.DECIMAL) {
|
||||
if (_validationType == ValidationType.DECIMAL) {
|
||||
return dataValidationHelper.createDecimalConstraint(operatorType, firstFormula, secondFormula);
|
||||
}
|
||||
if( _validationType == ValidationType.TEXT_LENGTH) {
|
||||
if (_validationType == ValidationType.TEXT_LENGTH) {
|
||||
return dataValidationHelper.createTextLengthConstraint(operatorType, firstFormula, secondFormula);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* writes plain text values into cells in a tabular format to form comments readable from within
|
||||
* the spreadsheet.
|
||||
|
@ -161,6 +164,7 @@ public abstract class BaseTestDataValidation {
|
|||
cell.setCellStyle(style_2);
|
||||
setCellValue(cell, ((errorBox) ? "yes" : "no"));
|
||||
}
|
||||
|
||||
private static void writeOtherSettings(Sheet sheet, CellStyle style,
|
||||
String strStettings) {
|
||||
Row row = sheet.getRow(sheet.getPhysicalNumberOfRows() - 1);
|
||||
|
@ -168,6 +172,7 @@ public abstract class BaseTestDataValidation {
|
|||
cell.setCellStyle(style);
|
||||
setCellValue(cell, strStettings);
|
||||
}
|
||||
|
||||
void addListValidation(String[] explicitListValues, String listFormula, String listValsDescr,
|
||||
boolean allowEmpty, boolean suppressDropDown) {
|
||||
String promptDescr = (allowEmpty ? "empty ok" : "not empty")
|
||||
|
@ -177,10 +182,6 @@ public abstract class BaseTestDataValidation {
|
|||
}
|
||||
}
|
||||
|
||||
private static void log(String msg) {
|
||||
log.log(POILogger.INFO, msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Manages the cell styles used for formatting the output spreadsheet
|
||||
*/
|
||||
|
@ -195,9 +196,9 @@ public abstract class BaseTestDataValidation {
|
|||
|
||||
public WorkbookFormatter(Workbook wb) {
|
||||
_wb = wb;
|
||||
_style_1 = createStyle( wb, HorizontalAlignment.LEFT );
|
||||
_style_2 = createStyle( wb, HorizontalAlignment.CENTER );
|
||||
_style_3 = createStyle( wb, HorizontalAlignment.CENTER, IndexedColors.GREY_25_PERCENT.getIndex(), true );
|
||||
_style_1 = createStyle(wb, HorizontalAlignment.LEFT);
|
||||
_style_2 = createStyle(wb, HorizontalAlignment.CENTER);
|
||||
_style_3 = createStyle(wb, HorizontalAlignment.CENTER, IndexedColors.GREY_25_PERCENT.getIndex(), true);
|
||||
_style_4 = createHeaderStyle(wb);
|
||||
}
|
||||
|
||||
|
@ -229,9 +230,10 @@ public abstract class BaseTestDataValidation {
|
|||
private static CellStyle createStyle(Workbook wb, HorizontalAlignment h_align) {
|
||||
return createStyle(wb, h_align, IndexedColors.WHITE.getIndex(), false);
|
||||
}
|
||||
|
||||
private static CellStyle createHeaderStyle(Workbook wb) {
|
||||
Font font = wb.createFont();
|
||||
font.setColor( IndexedColors.WHITE.getIndex() );
|
||||
font.setColor(IndexedColors.WHITE.getIndex());
|
||||
font.setBold(true);
|
||||
|
||||
CellStyle cellStyle = wb.createCellStyle();
|
||||
|
@ -256,10 +258,11 @@ public abstract class BaseTestDataValidation {
|
|||
_currentSheet = _wb.createSheet(sheetName);
|
||||
return _currentSheet;
|
||||
}
|
||||
|
||||
void createDVTypeRow(String strTypeDescription) {
|
||||
Sheet sheet = _currentSheet;
|
||||
Row row = sheet.createRow(sheet.getPhysicalNumberOfRows());
|
||||
sheet.addMergedRegion(new CellRangeAddress(sheet.getPhysicalNumberOfRows()-1, sheet.getPhysicalNumberOfRows()-1, 0, 5));
|
||||
sheet.addMergedRegion(new CellRangeAddress(sheet.getPhysicalNumberOfRows() - 1, sheet.getPhysicalNumberOfRows() - 1, 0, 5));
|
||||
Cell cell = row.createCell(0);
|
||||
setCellValue(cell, strTypeDescription);
|
||||
cell.setCellStyle(_style_3);
|
||||
|
@ -300,8 +303,8 @@ public abstract class BaseTestDataValidation {
|
|||
|
||||
void createDVDescriptionRow(String strTypeDescription) {
|
||||
Sheet sheet = _currentSheet;
|
||||
Row row = sheet.getRow(sheet.getPhysicalNumberOfRows()-1);
|
||||
sheet.addMergedRegion(new CellRangeAddress(sheet.getPhysicalNumberOfRows()-1, sheet.getPhysicalNumberOfRows()-1, 0, 5));
|
||||
Row row = sheet.getRow(sheet.getPhysicalNumberOfRows() - 1);
|
||||
sheet.addMergedRegion(new CellRangeAddress(sheet.getPhysicalNumberOfRows() - 1, sheet.getPhysicalNumberOfRows() - 1, 0, 5));
|
||||
Cell cell = row.createCell(0);
|
||||
setCellValue(cell, strTypeDescription);
|
||||
cell.setCellStyle(_style_3);
|
||||
|
@ -377,7 +380,6 @@ public abstract class BaseTestDataValidation {
|
|||
va.addListValidation(listVals, null, listValsDescr, true, true);
|
||||
|
||||
|
||||
|
||||
wf.createDVTypeRow("Reference lists - list items are taken from others cells");
|
||||
wf.createDVDescriptionRow("Advantage - no restriction regarding the sum of item's length");
|
||||
wf.createHeaderRow();
|
||||
|
@ -429,7 +431,7 @@ public abstract class BaseTestDataValidation {
|
|||
va.addValidation(OperatorType.NOT_BETWEEN, "2004/01/01", "2004/01/06", ErrorStyle.INFO, "Not between 1/2/2004 and 1/6/2004 ", "Error box type = INFO", false, true, true);
|
||||
va.addValidation(OperatorType.EQUAL, "2004/03/02", null, ErrorStyle.WARNING, "Equal to 3/2/2004", "Error box type = WARNING", false, false, true);
|
||||
va.addValidation(OperatorType.NOT_EQUAL, "2004/03/02", null, ErrorStyle.WARNING, "Not equal to 3/2/2004", "-", false, false, false);
|
||||
va.addValidation(OperatorType.GREATER_THAN,"=DATEVALUE(\"4-Jul-2001\")", null, ErrorStyle.WARNING, "Greater than DATEVALUE('4-Jul-2001')", "-", true, false, false);
|
||||
va.addValidation(OperatorType.GREATER_THAN, "=DATEVALUE(\"4-Jul-2001\")", null, ErrorStyle.WARNING, "Greater than DATEVALUE('4-Jul-2001')", "-", true, false, false);
|
||||
va.addValidation(OperatorType.LESS_THAN, "2004/03/02", null, ErrorStyle.WARNING, "Less than 3/2/2004", "-", true, true, false);
|
||||
va.addValidation(OperatorType.GREATER_OR_EQUAL, "2004/03/02", null, ErrorStyle.STOP, "Greater than or equal to 3/2/2004", "Error box type = STOP", true, false, true);
|
||||
va.addValidation(OperatorType.LESS_OR_EQUAL, "2004/03/04", null, ErrorStyle.STOP, "Less than or equal to 3/4/2004", "-", false, true, false);
|
||||
|
@ -443,10 +445,10 @@ public abstract class BaseTestDataValidation {
|
|||
va.addValidation(OperatorType.NOT_BETWEEN, "12:00", "16:00", ErrorStyle.INFO, "Not between 12:00 and 16:00 ", "Error box type = INFO", false, true, true);
|
||||
va.addValidation(OperatorType.EQUAL, "13:35", null, ErrorStyle.WARNING, "Equal to 13:35", "Error box type = WARNING", false, false, true);
|
||||
va.addValidation(OperatorType.NOT_EQUAL, "13:35", null, ErrorStyle.WARNING, "Not equal to 13:35", "-", false, false, false);
|
||||
va.addValidation(OperatorType.GREATER_THAN,"12:00", null, ErrorStyle.WARNING, "Greater than 12:00", "-", true, false, false);
|
||||
va.addValidation(OperatorType.GREATER_THAN, "12:00", null, ErrorStyle.WARNING, "Greater than 12:00", "-", true, false, false);
|
||||
va.addValidation(OperatorType.LESS_THAN, "=1/2", null, ErrorStyle.WARNING, "Less than (1/2) -> 12:00", "-", true, true, false);
|
||||
va.addValidation(OperatorType.GREATER_OR_EQUAL, "14:00", null, ErrorStyle.STOP, "Greater than or equal to 14:00", "Error box type = STOP", true, false, true);
|
||||
va.addValidation(OperatorType.LESS_OR_EQUAL,"14:00", null, ErrorStyle.STOP, "Less than or equal to 14:00", "-", false, true, false);
|
||||
va.addValidation(OperatorType.LESS_OR_EQUAL, "14:00", null, ErrorStyle.STOP, "Less than or equal to 14:00", "-", false, true, false);
|
||||
}
|
||||
|
||||
private static void addTextLengthValidations(WorkbookFormatter wf) {
|
||||
|
@ -466,41 +468,25 @@ public abstract class BaseTestDataValidation {
|
|||
|
||||
@Test
|
||||
void testDataValidation() throws Exception {
|
||||
log("\nTest no. 2 - Test Excel's Data validation mechanism");
|
||||
Workbook wb = _testDataProvider.createWorkbook();
|
||||
WorkbookFormatter wf = new WorkbookFormatter(wb);
|
||||
|
||||
log(" Create sheet for Data Validation's number types ... ");
|
||||
addSimpleNumericValidations(wf);
|
||||
log("done !");
|
||||
|
||||
log(" Create sheet for 'List' Data Validation type ... ");
|
||||
addListValidations(wf, wb);
|
||||
log("done !");
|
||||
|
||||
log(" Create sheet for 'Date' and 'Time' Data Validation types ... ");
|
||||
addDateTimeValidations(wf, wb);
|
||||
log("done !");
|
||||
|
||||
log(" Create sheet for 'Text length' Data Validation type... ");
|
||||
addTextLengthValidations(wf);
|
||||
log("done !");
|
||||
|
||||
try (Workbook wb1 = _testDataProvider.createWorkbook()) {
|
||||
WorkbookFormatter wf1 = new WorkbookFormatter(wb1);
|
||||
addSimpleNumericValidations(wf1);
|
||||
addListValidations(wf1, wb1);
|
||||
addDateTimeValidations(wf1, wb1);
|
||||
addTextLengthValidations(wf1);
|
||||
// Custom Validation type
|
||||
log(" Create sheet for 'Custom' Data Validation type ... ");
|
||||
addCustomValidations(wf);
|
||||
log("done !");
|
||||
addCustomValidations(wf1);
|
||||
try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1)) {
|
||||
Sheet sh = wb2.getSheet("Numbers");
|
||||
assertEquals(16, sh.getDataValidations().size());
|
||||
}
|
||||
|
||||
_testDataProvider.writeOutAndReadBack(wb).close();
|
||||
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* package */ static void setCellValue(Cell cell, String text) {
|
||||
static void setCellValue(Cell cell, String text) {
|
||||
cell.setCellValue(text);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
package org.apache.poi.ss.usermodel;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
@ -25,21 +26,17 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
|||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.poi.hssf.HSSFITestDataProvider;
|
||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||
import org.apache.poi.ss.ITestDataProvider;
|
||||
import org.apache.poi.ss.util.AreaReference;
|
||||
import org.apache.poi.ss.util.CellReference;
|
||||
import org.apache.poi.util.IOUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
/**
|
||||
* Tests of implementations of {@link org.apache.poi.ss.usermodel.Name}.
|
||||
*
|
||||
* @author Yegor Kozlov
|
||||
*/
|
||||
public abstract class BaseTestNamedRange {
|
||||
|
||||
|
@ -103,13 +100,13 @@ public abstract class BaseTestNamedRange {
|
|||
|
||||
@Test
|
||||
public final void testUnicodeNamedRange() throws Exception {
|
||||
try (HSSFWorkbook wb1 = new HSSFWorkbook()) {
|
||||
try (Workbook wb1 = _testDataProvider.createWorkbook()) {
|
||||
wb1.createSheet("Test");
|
||||
Name name = wb1.createName();
|
||||
name.setNameName("\u03B1");
|
||||
name.setRefersToFormula("Test!$D$3:$E$8");
|
||||
|
||||
try (HSSFWorkbook wb2 = HSSFITestDataProvider.instance.writeOutAndReadBack(wb1)) {
|
||||
try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1)) {
|
||||
Name name2 = wb2.getName("\u03B1");
|
||||
|
||||
assertNotNull(name2);
|
||||
|
@ -637,71 +634,64 @@ public abstract class BaseTestNamedRange {
|
|||
|
||||
// bug 56781: name validation only checks for first character's validity and presence of spaces
|
||||
// bug 60246: validate name does not allow DOT in named ranges
|
||||
@Test
|
||||
void testValid() throws IOException {
|
||||
Workbook wb = _testDataProvider.createWorkbook();
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"Hello", "number1", "_underscore", "underscore_", "p.e.r.o.i.d.s", "\\Backslash", "Backslash\\"})
|
||||
void testValid(String valid) throws IOException {
|
||||
try (Workbook wb = _testDataProvider.createWorkbook()) {
|
||||
Name name = wb.createName();
|
||||
for (String valid : Arrays.asList(
|
||||
"Hello",
|
||||
"number1",
|
||||
"_underscore",
|
||||
"underscore_",
|
||||
"p.e.r.o.i.d.s",
|
||||
"\\Backslash",
|
||||
"Backslash\\"
|
||||
)) {
|
||||
name.setNameName(valid);
|
||||
assertDoesNotThrow(() -> name.setNameName(valid));
|
||||
}
|
||||
}
|
||||
|
||||
wb.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testInvalid() {
|
||||
Workbook wb = _testDataProvider.createWorkbook();
|
||||
|
||||
Name name = wb.createName();
|
||||
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> name.setNameName(""));
|
||||
assertEquals("Name cannot be blank", e.getMessage());
|
||||
|
||||
for (String invalid : Arrays.asList(
|
||||
"1number",
|
||||
"Sheet1!A1",
|
||||
"Exclamation!",
|
||||
"Has Space",
|
||||
"Colon:",
|
||||
"A-Minus",
|
||||
"A+Plus",
|
||||
"Dollar$",
|
||||
".periodAtBeginning",
|
||||
"R", //special shorthand
|
||||
"C", //special shorthand
|
||||
"A1", // A1-style cell reference
|
||||
"R1C1", // R1C1-style cell reference
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"1number", "Sheet1!A1", "Exclamation!", "Has Space", "Colon:", "A-Minus", "A+Plus", "Dollar$", ".periodAtBeginning",
|
||||
//special shorthand
|
||||
"R", "C",
|
||||
// A1-style cell reference
|
||||
"A1",
|
||||
// R1C1-style cell reference
|
||||
"R1C1",
|
||||
"NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters..."+
|
||||
"NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters..."+
|
||||
"NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters..."+
|
||||
"NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters..."+
|
||||
"NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters"
|
||||
)) {
|
||||
})
|
||||
void testInvalid(String invalid) {
|
||||
Workbook wb = _testDataProvider.createWorkbook();
|
||||
|
||||
Name name = wb.createName();
|
||||
IllegalArgumentException e;
|
||||
e = assertThrows(IllegalArgumentException.class, () -> name.setNameName(""));
|
||||
assertEquals("Name cannot be blank", e.getMessage());
|
||||
|
||||
e = assertThrows(IllegalArgumentException.class, () -> name.setNameName(invalid));
|
||||
assertTrue(e.getMessage().startsWith("Invalid name: '"+invalid+"'"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// bug 60260: renaming a sheet with a named range referring to a unicode (non-ASCII) sheet name
|
||||
@Test
|
||||
void renameSheetWithNamedRangeReferringToUnicodeSheetName() {
|
||||
Workbook wb = _testDataProvider.createWorkbook();
|
||||
wb.createSheet("Sheet\u30FB1");
|
||||
void renameSheetWithNamedRangeReferringToUnicodeSheetName() throws IOException {
|
||||
String unicodeName = "Sheet\u30FB201";
|
||||
String asciiName = "Sheet 1";
|
||||
String rangeName = "test_named_range";
|
||||
try (Workbook wb1 = _testDataProvider.createWorkbook()) {
|
||||
wb1.createSheet(unicodeName);
|
||||
|
||||
Name name = wb.createName();
|
||||
name.setNameName("test_named_range");
|
||||
name.setRefersToFormula("'Sheet\u30FB201'!A1:A6");
|
||||
Name name1 = wb1.createName();
|
||||
name1.setNameName(rangeName);
|
||||
name1.setRefersToFormula("'"+unicodeName+"'!A1:A6");
|
||||
|
||||
wb.setSheetName(0, "Sheet 1");
|
||||
IOUtils.closeQuietly(wb);
|
||||
wb1.setSheetName(0, asciiName);
|
||||
assertEquals(asciiName, name1.getSheetName());
|
||||
|
||||
try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1)) {
|
||||
Name name2 = wb2.getName(rangeName);
|
||||
assertNotNull(name2);
|
||||
// Eventually this will be updated, but currently we don't update the sheet name
|
||||
assertEquals(asciiName, name2.getSheetName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
package org.apache.poi.ss.usermodel;
|
||||
|
||||
import static org.apache.poi.POITestCase.assertBetween;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
@ -39,6 +40,7 @@ import java.util.Map.Entry;
|
|||
import java.util.Set;
|
||||
|
||||
import org.apache.poi.common.usermodel.HyperlinkType;
|
||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||
import org.apache.poi.ss.ITestDataProvider;
|
||||
import org.apache.poi.ss.SpreadsheetVersion;
|
||||
import org.apache.poi.ss.util.CellAddress;
|
||||
|
@ -1039,7 +1041,7 @@ public abstract class BaseTestSheet {
|
|||
|
||||
// here we can only verify that setting some zoom values works,
|
||||
// range-checking is different between the implementations
|
||||
sheet.setZoom(75);
|
||||
assertDoesNotThrow(() -> sheet.setZoom(75));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1048,6 +1050,10 @@ public abstract class BaseTestSheet {
|
|||
try (Workbook wb = _testDataProvider.createWorkbook()) {
|
||||
Sheet sheet = wb.createSheet();
|
||||
sheet.showInPane(2, 3);
|
||||
if (wb instanceof HSSFWorkbook) {
|
||||
assertEquals(2, sheet.getTopRow());
|
||||
assertEquals(3, sheet.getLeftCol());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -22,22 +22,24 @@ package org.apache.poi.ss.util;
|
|||
import static java.text.DateFormat.getDateInstance;
|
||||
import static java.text.DateFormat.getDateTimeInstance;
|
||||
import static java.text.DateFormat.getTimeInstance;
|
||||
import static org.apache.poi.ss.util.DateFormatConverter.getPrefixForLocale;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||
|
@ -49,7 +51,6 @@ import org.apache.poi.ss.usermodel.Sheet;
|
|||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.apache.poi.util.LocaleID;
|
||||
import org.apache.poi.util.NullOutputStream;
|
||||
import org.apache.poi.util.TempFile;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
@ -74,50 +75,54 @@ final class TestDateFormatConverter {
|
|||
"false, true, " + DateFormat.FULL + ", Full"
|
||||
})
|
||||
void testJavaDateFormatsInExcel(boolean dates, boolean times, int style, String styleName ) throws Exception {
|
||||
try (Workbook workbook = new HSSFWorkbook()) {
|
||||
String sheetName = (dates) ? ((times) ? "DateTimes" : "Dates") : "Times";
|
||||
String[] headers = {
|
||||
"locale", "DisplayName", "Excel " + styleName, "java.text.DateFormat",
|
||||
"Equals", "Java pattern", "Excel pattern"
|
||||
};
|
||||
|
||||
Locale[] locales = Arrays.stream(DateFormat.getAvailableLocales())
|
||||
// only use locale with known LocaleIDs
|
||||
.filter(l -> !getPrefixForLocale(l).isEmpty() || Locale.ROOT.equals(l) || l.toLanguageTag().isEmpty())
|
||||
.sorted(Comparator.comparing(Locale::toString))
|
||||
.toArray(Locale[]::new);
|
||||
|
||||
|
||||
try (Workbook workbook = new HSSFWorkbook()) {
|
||||
Sheet sheet = workbook.createSheet(sheetName);
|
||||
Row header = sheet.createRow(0);
|
||||
header.createCell(0).setCellValue("locale");
|
||||
header.createCell(1).setCellValue("DisplayName");
|
||||
header.createCell(2).setCellValue("Excel " + styleName);
|
||||
header.createCell(3).setCellValue("java.text.DateFormat");
|
||||
header.createCell(4).setCellValue("Equals");
|
||||
header.createCell(5).setCellValue("Java pattern");
|
||||
header.createCell(6).setCellValue("Excel pattern");
|
||||
IntStream.range(0, headers.length).forEach(i -> header.createCell(i).setCellValue(headers[i]));
|
||||
|
||||
int rowNum = 1;
|
||||
for (Locale locale : DateFormat.getAvailableLocales()) {
|
||||
Row row = sheet.createRow(rowNum++);
|
||||
|
||||
row.createCell(0).setCellValue(locale.toString());
|
||||
row.createCell(1).setCellValue(locale.getDisplayName(Locale.ROOT));
|
||||
final Cell[] cell = new Cell[7];
|
||||
final Date date = new Date();
|
||||
|
||||
for (Locale locale : locales) {
|
||||
DateFormat dateFormat = (dates)
|
||||
? (times ? getDateTimeInstance(style, style, locale) : getDateInstance(style, locale))
|
||||
: getTimeInstance(style, locale);
|
||||
|
||||
Cell cell = row.createCell(2);
|
||||
Date date = new Date();
|
||||
cell.setCellValue(date);
|
||||
CellStyle cellStyle = row.getSheet().getWorkbook().createCellStyle();
|
||||
|
||||
String javaDateFormatPattern = ((SimpleDateFormat) dateFormat).toPattern();
|
||||
String excelFormatPattern = DateFormatConverter.convert(locale, javaDateFormatPattern);
|
||||
|
||||
DataFormat poiFormat = row.getSheet().getWorkbook().createDataFormat();
|
||||
Row row = sheet.createRow(rowNum++);
|
||||
IntStream.range(0, headers.length).forEach(i -> cell[i] = row.createCell(i));
|
||||
CellStyle cellStyle = workbook.createCellStyle();
|
||||
DataFormat poiFormat = workbook.createDataFormat();
|
||||
cellStyle.setDataFormat(poiFormat.getFormat(excelFormatPattern));
|
||||
row.createCell(3).setCellValue(dateFormat.format(date));
|
||||
|
||||
cell.setCellStyle(cellStyle);
|
||||
cell[0].setCellValue(locale.toString());
|
||||
cell[1].setCellValue(locale.getDisplayName(Locale.ROOT));
|
||||
cell[2].setCellValue(date);
|
||||
cell[2].setCellStyle(cellStyle);
|
||||
cell[3].setCellValue(dateFormat.format(date));
|
||||
|
||||
// the formula returns TRUE is the formatted date in column C equals to the string in column D
|
||||
row.createCell(4).setCellFormula("TEXT(C" + rowNum + ",G" + rowNum + ")=D" + rowNum);
|
||||
row.createCell(5).setCellValue(javaDateFormatPattern);
|
||||
row.createCell(6).setCellValue(excelFormatPattern);
|
||||
cell[4].setCellFormula("TEXT(C" + rowNum + ",G" + rowNum + ")=D" + rowNum);
|
||||
cell[5].setCellValue(javaDateFormatPattern);
|
||||
cell[6].setCellValue(excelFormatPattern);
|
||||
}
|
||||
|
||||
workbook.write(new NullOutputStream());
|
||||
assertDoesNotThrow(() -> workbook.write(new NullOutputStream()));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -125,7 +130,7 @@ final class TestDateFormatConverter {
|
|||
void testJDK8EmptyLocale() {
|
||||
// JDK 8 seems to add an empty locale-string to the list returned via DateFormat.getAvailableLocales()
|
||||
// therefore we now cater for this special locale as well
|
||||
String prefix = DateFormatConverter.getPrefixForLocale(new Locale(""));
|
||||
String prefix = getPrefixForLocale(new Locale(""));
|
||||
assertEquals("", prefix);
|
||||
}
|
||||
|
||||
|
@ -163,7 +168,7 @@ final class TestDateFormatConverter {
|
|||
continue;
|
||||
}
|
||||
|
||||
String prefix = DateFormatConverter.getPrefixForLocale(loc);
|
||||
String prefix = getPrefixForLocale(loc);
|
||||
assertNotNull(prefix, "Prefix not found - language tag: "+partTag);
|
||||
assertNotEquals("", prefix, "Prefix not found - language tag: "+partTag);
|
||||
Matcher m = p.matcher(prefix);
|
||||
|
|
Loading…
Reference in New Issue