children = patriarchBack.getChildren();
assertEquals(4, children.size());
HSSFShape hssfShape = children.get(0);
@@ -843,7 +847,7 @@ public class TestDrawingShapes {
assertEquals(2, cAnchor.getCol2());
assertEquals(2, cAnchor.getRow1());
assertEquals(2, cAnchor.getRow2());
-
+
wbBack.close();
}
}
diff --git a/src/testcases/org/apache/poi/hssf/model/TestEscherRecordFactory.java b/src/testcases/org/apache/poi/hssf/model/TestEscherRecordFactory.java
index a3776e5bdc..72e9e55941 100644
--- a/src/testcases/org/apache/poi/hssf/model/TestEscherRecordFactory.java
+++ b/src/testcases/org/apache/poi/hssf/model/TestEscherRecordFactory.java
@@ -17,17 +17,18 @@
package org.apache.poi.hssf.model;
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import static org.apache.poi.ddf.DefaultEscherRecordFactory.isContainer;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Random;
-import org.apache.poi.ddf.DefaultEscherRecordFactory;
import org.apache.poi.ddf.EscherContainerRecord;
import org.apache.poi.ddf.EscherTextboxRecord;
import org.apache.poi.hssf.HSSFTestDataSamples;
@@ -37,7 +38,7 @@ import org.apache.poi.hssf.record.RecordBase;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFTestHelper;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
public class TestEscherRecordFactory {
@@ -57,30 +58,30 @@ public class TestEscherRecordFactory {
@Test
public void testDetectContainer() {
Random rnd = new Random();
- assertTrue(DefaultEscherRecordFactory.isContainer((short) 0x0, EscherContainerRecord.DG_CONTAINER));
- assertTrue(DefaultEscherRecordFactory.isContainer((short) 0x0, EscherContainerRecord.SOLVER_CONTAINER));
- assertTrue(DefaultEscherRecordFactory.isContainer((short) 0x0, EscherContainerRecord.SP_CONTAINER));
- assertTrue(DefaultEscherRecordFactory.isContainer((short) 0x0, EscherContainerRecord.DGG_CONTAINER));
- assertTrue(DefaultEscherRecordFactory.isContainer((short) 0x0, EscherContainerRecord.BSTORE_CONTAINER));
- assertTrue(DefaultEscherRecordFactory.isContainer((short) 0x0, EscherContainerRecord.SPGR_CONTAINER));
+ assertTrue(isContainer((short) 0x0, EscherContainerRecord.DG_CONTAINER));
+ assertTrue(isContainer((short) 0x0, EscherContainerRecord.SOLVER_CONTAINER));
+ assertTrue(isContainer((short) 0x0, EscherContainerRecord.SP_CONTAINER));
+ assertTrue(isContainer((short) 0x0, EscherContainerRecord.DGG_CONTAINER));
+ assertTrue(isContainer((short) 0x0, EscherContainerRecord.BSTORE_CONTAINER));
+ assertTrue(isContainer((short) 0x0, EscherContainerRecord.SPGR_CONTAINER));
- for (Short i=EscherContainerRecord.DGG_CONTAINER; i<= EscherContainerRecord.SOLVER_CONTAINER; i++){
- assertTrue(DefaultEscherRecordFactory.isContainer(Integer.valueOf(rnd.nextInt(Short.MAX_VALUE)).shortValue(), i));
+ for (short i=EscherContainerRecord.DGG_CONTAINER; i<= EscherContainerRecord.SOLVER_CONTAINER; i++){
+ assertTrue(isContainer(Integer.valueOf(rnd.nextInt(Short.MAX_VALUE)).shortValue(), i));
}
- assertFalse(DefaultEscherRecordFactory.isContainer((short) 0x0, Integer.valueOf(EscherContainerRecord.DGG_CONTAINER - 1).shortValue()));
- assertFalse(DefaultEscherRecordFactory.isContainer((short) 0x0, Integer.valueOf(EscherContainerRecord.SOLVER_CONTAINER + 1).shortValue()));
+ assertFalse(isContainer((short) 0x0, Integer.valueOf(EscherContainerRecord.DGG_CONTAINER - 1).shortValue()));
+ assertFalse(isContainer((short) 0x0, Integer.valueOf(EscherContainerRecord.SOLVER_CONTAINER + 1).shortValue()));
- assertTrue(DefaultEscherRecordFactory.isContainer((short) 0x000F, Integer.valueOf(EscherContainerRecord.DGG_CONTAINER - 1).shortValue()));
- assertTrue(DefaultEscherRecordFactory.isContainer((short) 0xFFFF, Integer.valueOf(EscherContainerRecord.DGG_CONTAINER - 1).shortValue()));
- assertFalse(DefaultEscherRecordFactory.isContainer((short) 0x000C, Integer.valueOf(EscherContainerRecord.DGG_CONTAINER - 1).shortValue()));
- assertFalse(DefaultEscherRecordFactory.isContainer((short) 0xCCCC, Integer.valueOf(EscherContainerRecord.DGG_CONTAINER - 1).shortValue()));
- assertFalse(DefaultEscherRecordFactory.isContainer((short) 0x000F, EscherTextboxRecord.RECORD_ID));
- assertFalse(DefaultEscherRecordFactory.isContainer((short) 0xCCCC, EscherTextboxRecord.RECORD_ID));
+ assertTrue(isContainer((short) 0x000F, Integer.valueOf(EscherContainerRecord.DGG_CONTAINER - 1).shortValue()));
+ assertTrue(isContainer((short) 0xFFFF, Integer.valueOf(EscherContainerRecord.DGG_CONTAINER - 1).shortValue()));
+ assertFalse(isContainer((short) 0x000C, Integer.valueOf(EscherContainerRecord.DGG_CONTAINER - 1).shortValue()));
+ assertFalse(isContainer((short) 0xCCCC, Integer.valueOf(EscherContainerRecord.DGG_CONTAINER - 1).shortValue()));
+ assertFalse(isContainer((short) 0x000F, EscherTextboxRecord.RECORD_ID));
+ assertFalse(isContainer((short) 0xCCCC, EscherTextboxRecord.RECORD_ID));
}
@Test
- public void testDgContainerMustBeRootOfHSSFSheetEscherRecords() throws IOException {
+ public void testDgContainerMustBeRootOfHSSFSheetEscherRecords() {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("47251.xls");
HSSFSheet sh = wb.getSheetAt(0);
InternalSheet ish = HSSFTestHelper.getSheetForTest(sh);
@@ -90,12 +91,14 @@ public class TestEscherRecordFactory {
byte[] dgBytes = toByteArray(dgRecords);
sh.getDrawingPatriarch();
EscherAggregate agg = (EscherAggregate) ish.findFirstRecordBySid(EscherAggregate.sid);
+ assertNotNull(agg);
assertTrue(agg.getEscherRecords().get(0) instanceof EscherContainerRecord);
assertEquals(EscherContainerRecord.DG_CONTAINER, agg.getEscherRecords().get(0).getRecordId());
assertEquals((short) 0x0, agg.getEscherRecords().get(0).getOptions());
agg = (EscherAggregate) ish.findFirstRecordBySid(EscherAggregate.sid);
+ assertNotNull(agg);
byte[] dgBytesAfterSave = agg.serialize();
- assertEquals("different size of drawing data before and after save", dgBytes.length, dgBytesAfterSave.length);
- assertArrayEquals("drawing data before and after save is different", dgBytes, dgBytesAfterSave);
+ assertEquals(dgBytes.length, dgBytesAfterSave.length, "different size of drawing data before and after save");
+ assertArrayEquals(dgBytes, dgBytesAfterSave, "drawing data before and after save is different");
}
}
diff --git a/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java b/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java
index 5a88f460b8..db7f6f1862 100644
--- a/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java
+++ b/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java
@@ -17,11 +17,14 @@
package org.apache.poi.hssf.model;
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
import java.io.IOException;
import java.util.Locale;
@@ -48,7 +51,7 @@ import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.util.HexRead;
import org.apache.poi.util.LittleEndianByteArrayInputStream;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
/**
* Test the low level formula parser functionality. High level tests are to
@@ -61,7 +64,7 @@ public final class TestFormulaParser {
*/
/* package */ static Ptg[] parseFormula(String formula) {
Ptg[] result = HSSFFormulaParser.parse(formula, null);
- assertNotNull("Ptg array should not be null", result);
+ assertNotNull(result, "Ptg array should not be null");
return result;
}
private static String toFormulaString(Ptg[] ptgs) {
@@ -279,18 +282,18 @@ public final class TestFormulaParser {
String value = " hi ";
Ptg[] ptgs = parseFormula("\"" + value + "\"");
confirmTokenClasses(ptgs, StringPtg.class);
- assertEquals("ptg0 contains exact value", ((StringPtg) ptgs[0]).getValue(), value);
+ assertEquals(((StringPtg) ptgs[0]).getValue(), value, "ptg0 contains exact value");
}
@Test
public void testLookupAndMatchFunctionArgs() {
Ptg[] ptgs = parseFormula("lookup(A1, A3:A52, B3:B52)");
confirmTokenClasses(ptgs, RefPtg.class, AreaPtg.class, AreaPtg.class, FuncVarPtg.class);
- assertEquals("ptg0 has Value class", ptgs[0].getPtgClass(), Ptg.CLASS_VALUE);
+ assertEquals(ptgs[0].getPtgClass(), Ptg.CLASS_VALUE, "ptg0 has Value class");
ptgs = parseFormula("match(A1, A3:A52)");
confirmTokenClasses(ptgs, RefPtg.class, AreaPtg.class, FuncVarPtg.class);
- assertEquals("ptg0 has Value class", ptgs[0].getPtgClass(), Ptg.CLASS_VALUE);
+ assertEquals(ptgs[0].getPtgClass(), Ptg.CLASS_VALUE, "ptg0 has Value class");
}
/** bug 33160*/
@@ -394,63 +397,63 @@ public final class TestFormulaParser {
cell.setCellFormula("1.3E21/3");
formula = cell.getCellFormula();
- assertEquals("Exponential formula string", "1.3E+21/3", formula);
+ assertEquals("1.3E+21/3", formula);
cell.setCellFormula("-1.3E21/3");
formula = cell.getCellFormula();
- assertEquals("Exponential formula string", "-1.3E+21/3", formula);
+ assertEquals("-1.3E+21/3", formula);
cell.setCellFormula("1322E21/3");
formula = cell.getCellFormula();
- assertEquals("Exponential formula string", "1.322E+24/3", formula);
+ assertEquals("1.322E+24/3", formula);
cell.setCellFormula("-1322E21/3");
formula = cell.getCellFormula();
- assertEquals("Exponential formula string", "-1.322E+24/3", formula);
+ assertEquals("-1.322E+24/3", formula);
cell.setCellFormula("1.3E1/3");
formula = cell.getCellFormula();
- assertEquals("Exponential formula string", "13/3", formula);
+ assertEquals("13/3", formula);
cell.setCellFormula("-1.3E1/3");
formula = cell.getCellFormula();
- assertEquals("Exponential formula string", "-13/3", formula);
+ assertEquals("-13/3", formula);
cell.setCellFormula("1.3E-4/3");
formula = cell.getCellFormula();
- assertEquals("Exponential formula string", "0.00013/3", formula);
+ assertEquals("0.00013/3", formula);
cell.setCellFormula("-1.3E-4/3");
formula = cell.getCellFormula();
- assertEquals("Exponential formula string", "-0.00013/3", formula);
+ assertEquals("-0.00013/3", formula);
cell.setCellFormula("13E-15/3");
formula = cell.getCellFormula();
- assertEquals("Exponential formula string", "0.000000000000013/3", formula);
+ assertEquals("0.000000000000013/3", formula);
cell.setCellFormula("-13E-15/3");
formula = cell.getCellFormula();
- assertEquals("Exponential formula string", "-0.000000000000013/3", formula);
+ assertEquals("-0.000000000000013/3", formula);
cell.setCellFormula("1.3E3/3");
formula = cell.getCellFormula();
- assertEquals("Exponential formula string", "1300/3", formula);
+ assertEquals("1300/3", formula);
cell.setCellFormula("-1.3E3/3");
formula = cell.getCellFormula();
- assertEquals("Exponential formula string", "-1300/3", formula);
+ assertEquals("-1300/3", formula);
cell.setCellFormula("1300000000000000/3");
formula = cell.getCellFormula();
- assertEquals("Exponential formula string", "1300000000000000/3", formula);
+ assertEquals("1300000000000000/3", formula);
cell.setCellFormula("-1300000000000000/3");
formula = cell.getCellFormula();
- assertEquals("Exponential formula string", "-1300000000000000/3", formula);
+ assertEquals("-1300000000000000/3", formula);
cell.setCellFormula("-10E-1/3.1E2*4E3/3E4");
formula = cell.getCellFormula();
- assertEquals("Exponential formula string", "-1/310*4000/30000", formula);
+ assertEquals("-1/310*4000/30000", formula);
wb.close();
}
@@ -653,11 +656,10 @@ public final class TestFormulaParser {
private static void confirmTokenClasses(Ptg[] ptgs, Class>...expectedClasses) {
assertEquals(expectedClasses.length, ptgs.length);
for (int i = 0; i < expectedClasses.length; i++) {
- if(expectedClasses[i] != ptgs[i].getClass()) {
- fail("difference at token[" + i + "]: expected ("
- + expectedClasses[i].getName() + ") but got ("
- + ptgs[i].getClass().getName() + ")");
- }
+ assertEquals(expectedClasses[i], ptgs[i].getClass(),
+ "difference at token[" + i + "]: expected ("
+ + expectedClasses[i].getName() + ") but got ("
+ + ptgs[i].getClass().getName() + ")");
}
}
@@ -763,9 +765,7 @@ public final class TestFormulaParser {
HSSFCell cell = row.createCell(0);
cell.setCellFormula("right(\"test\"\"ing\", 3)");
String actualCellFormula = cell.getCellFormula();
- if ("RIGHT(\"test\"ing\",3)".equals(actualCellFormula)) {
- fail("Identified bug 28754b");
- }
+ assertNotEquals("RIGHT(\"test\"ing\",3)", actualCellFormula, "Identified bug 28754b");
assertEquals("RIGHT(\"test\"\"ing\",3)", actualCellFormula);
}
}
@@ -819,13 +819,8 @@ public final class TestFormulaParser {
}
private static void parseExpectedException(String formula) {
- try {
- parseFormula(formula);
- fail("Expected FormulaParseException: " + formula);
- } catch (FormulaParseException e) {
- // expected during successful test
- assertNotNull(e.getMessage());
- }
+ FormulaParseException e = assertThrows(FormulaParseException.class, () -> parseFormula(formula));
+ assertNotNull(e.getMessage());
}
@Test
@@ -838,9 +833,7 @@ public final class TestFormulaParser {
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellFormula("SUM(A32769:A32770)");
- if("SUM(A-32767:A-32766)".equals(cell.getCellFormula())) {
- fail("Identified bug 44539");
- }
+ assertNotEquals("SUM(A-32767:A-32766)", cell.getCellFormula(), "Identified bug 44539");
assertEquals("SUM(A32769:A32770)", cell.getCellFormula());
wb.close();
@@ -883,14 +876,10 @@ public final class TestFormulaParser {
new IntPtg(1),
DividePtg.instance,
};
- try {
- toFormulaString(ptgs);
- fail("Expected exception was not thrown");
- } catch (IllegalStateException e) {
- // expected during successful test
- assertTrue(e.getMessage().startsWith("Too few arguments supplied to operation"));
- }
+ IllegalStateException e = assertThrows(IllegalStateException.class, () -> toFormulaString(ptgs));
+ assertTrue(e.getMessage().startsWith("Too few arguments supplied to operation"));
}
+
/**
* Make sure that POI uses the right Func Ptg when encoding formulas. Functions with variable
* number of args should get FuncVarPtg, functions with fixed args should get FuncPtg.
@@ -904,9 +893,7 @@ public final class TestFormulaParser {
Ptg[] ptgs = parseFormula("countif(A1:A2, 1)");
assertEquals(3, ptgs.length);
- if(ptgs[2] instanceof FuncVarPtg) {
- fail("Identified bug 44675");
- }
+ assertFalse(ptgs[2] instanceof FuncVarPtg, "Identified bug 44675");
confirmTokenClasses(ptgs, AreaPtg.class, IntPtg.class, FuncPtg.class);
confirmTokenClasses("sin(1)", IntPtg.class, FuncPtg.class);
@@ -921,34 +908,20 @@ public final class TestFormulaParser {
}
private static void confirmArgCountMsg(String formula, String expectedMessage) throws IOException {
- HSSFWorkbook book = new HSSFWorkbook();
- try {
- HSSFFormulaParser.parse(formula, book);
- fail("Didn't get parse exception as expected");
- } catch (FormulaParseException e) {
+ try (HSSFWorkbook book = new HSSFWorkbook()) {
+ FormulaParseException e = assertThrows(FormulaParseException.class, () -> HSSFFormulaParser.parse(formula, book));
confirmParseException(e, expectedMessage);
}
- book.close();
}
@Test
public void testParseErrorExpectedMsg() {
+ FormulaParseException e;
+ e = assertThrows(FormulaParseException.class, () -> parseFormula("round(3.14;2)"));
+ confirmParseException(e, "Parse error near char 10 ';' in specified formula 'round(3.14;2)'. Expected ',' or ')'");
- try {
- parseFormula("round(3.14;2)");
- fail("Didn't get parse exception as expected");
- } catch (FormulaParseException e) {
- confirmParseException(e,
- "Parse error near char 10 ';' in specified formula 'round(3.14;2)'. Expected ',' or ')'");
- }
-
- try {
- parseFormula(" =2+2");
- fail("Didn't get parse exception as expected");
- } catch (FormulaParseException e) {
- confirmParseException(e,
- "The specified formula ' =2+2' starts with an equals sign which is not allowed.");
- }
+ e = assertThrows(FormulaParseException.class, () -> parseFormula(" =2+2"));
+ confirmParseException(e, "The specified formula ' =2+2' starts with an equals sign which is not allowed.");
}
/**
@@ -972,36 +945,30 @@ public final class TestFormulaParser {
@Test
public void testNamedRangeThatLooksLikeCell() throws IOException {
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sheet = wb.createSheet("Sheet1");
- HSSFName name = wb.createName();
- name.setRefersToFormula("Sheet1!B1");
- name.setNameName("pfy1");
+ try (HSSFWorkbook wb = new HSSFWorkbook()) {
+ HSSFSheet sheet = wb.createSheet("Sheet1");
+ HSSFName name = wb.createName();
+ name.setRefersToFormula("Sheet1!B1");
+ name.setNameName("pfy1");
- Ptg[] ptgs;
- try {
- ptgs = HSSFFormulaParser.parse("count(pfy1)", wb);
- } catch (IllegalArgumentException e) {
- if (e.getMessage().equals("Specified colIx (1012) is out of range")) {
- fail("Identified bug 45354");
+ Ptg[] ptgs;
+ try {
+ ptgs = HSSFFormulaParser.parse("count(pfy1)", wb);
+ } catch (IllegalArgumentException e) {
+ if (e.getMessage().equals("Specified colIx (1012) is out of range")) {
+ fail("Identified bug 45354");
+ }
+ throw e;
}
- wb.close();
- throw e;
- }
- confirmTokenClasses(ptgs, NamePtg.class, FuncVarPtg.class);
+ confirmTokenClasses(ptgs, NamePtg.class, FuncVarPtg.class);
- HSSFCell cell = sheet.createRow(0).createCell(0);
- cell.setCellFormula("count(pfy1)");
- assertEquals("COUNT(pfy1)", cell.getCellFormula());
- try {
- cell.setCellFormula("count(pf1)");
- fail("Expected formula parse execption");
- } catch (FormulaParseException e) {
- confirmParseException(e,
- "Specified named range 'pf1' does not exist in the current workbook.");
+ HSSFCell cell = sheet.createRow(0).createCell(0);
+ cell.setCellFormula("count(pfy1)");
+ assertEquals("COUNT(pfy1)", cell.getCellFormula());
+ FormulaParseException e = assertThrows(FormulaParseException.class, () -> cell.setCellFormula("count(pf1)"));
+ confirmParseException(e, "Specified named range 'pf1' does not exist in the current workbook.");
+ cell.setCellFormula("count(fp1)"); // plain cell ref, col is in range
}
- cell.setCellFormula("count(fp1)"); // plain cell ref, col is in range
- wb.close();
}
@Test
@@ -1014,9 +981,7 @@ public final class TestFormulaParser {
ptgs = HSSFFormulaParser.parse("Sheet1!A10:A40000", book);
aptg = (AreaI) ptgs[0];
- if (aptg.getLastRow() == -25537) {
- fail("Identified bug 45358");
- }
+ assertNotEquals(-25537, aptg.getLastRow(), "Identified bug 45358");
assertEquals(39999, aptg.getLastRow());
ptgs = HSSFFormulaParser.parse("Sheet1!A10:A65536", book);
@@ -1050,10 +1015,8 @@ public final class TestFormulaParser {
ptgs = parseFormula("MAX({\"5\"},3)");
confirmTokenClasses(ptgs, ArrayPtg.class, IntPtg.class, FuncVarPtg.class);
Object element = ((ArrayPtg)ptgs[0]).getTokenArrayValues()[0][0];
- if (element instanceof UnicodeString) {
- // this would cause ClassCastException below
- fail("Wrong encoding of array element value");
- }
+ // this would cause ClassCastException below
+ assertFalse(element instanceof UnicodeString, "Wrong encoding of array element value");
assertEquals(String.class, element.getClass());
// make sure the formula encodes OK
@@ -1354,28 +1317,27 @@ public final class TestFormulaParser {
@Test
public void testRangeFuncOperand_bug46951() throws IOException {
- HSSFWorkbook wb = new HSSFWorkbook();
- Ptg[] ptgs;
- try {
- ptgs = HSSFFormulaParser.parse("SUM(C1:OFFSET(C1,0,B1))", wb);
- } catch (RuntimeException e) {
- if (e.getMessage().equals("Specified named range 'OFFSET' does not exist in the current workbook.")) {
- fail("Identified bug 46951");
+ try (HSSFWorkbook wb = new HSSFWorkbook()) {
+ Ptg[] ptgs;
+ try {
+ ptgs = HSSFFormulaParser.parse("SUM(C1:OFFSET(C1,0,B1))", wb);
+ } catch (RuntimeException e) {
+ if (e.getMessage().equals("Specified named range 'OFFSET' does not exist in the current workbook.")) {
+ fail("Identified bug 46951");
+ }
+ throw e;
}
- wb.close();
- throw e;
+ confirmTokenClasses(ptgs,
+ MemFuncPtg.class, // [len=23]
+ RefPtg.class, // [C1]
+ RefPtg.class, // [C1]
+ IntPtg.class, // [0]
+ RefPtg.class, // [B1]
+ FuncVarPtg.class, // [OFFSET nArgs=3]
+ RangePtg.class, //
+ AttrPtg.class // [sum ]
+ );
}
- confirmTokenClasses(ptgs,
- MemFuncPtg.class, // [len=23]
- RefPtg.class, // [C1]
- RefPtg.class, // [C1]
- IntPtg.class, // [0]
- RefPtg.class, // [B1]
- FuncVarPtg.class, // [OFFSET nArgs=3]
- RangePtg.class, //
- AttrPtg.class // [sum ]
- );
- wb.close();
}
@Test
@@ -1507,13 +1469,8 @@ public final class TestFormulaParser {
}
private static void confirmParseError(HSSFWorkbook wb, String formula, String expectedMessage) {
-
- try {
- HSSFFormulaParser.parse(formula, wb);
- fail("Expected formula parse execption");
- } catch (FormulaParseException e) {
- confirmParseException(e, expectedMessage);
- }
+ FormulaParseException e = assertThrows(FormulaParseException.class, () -> HSSFFormulaParser.parse(formula, wb));
+ confirmParseException(e, expectedMessage);
}
/**
@@ -1524,29 +1481,27 @@ public final class TestFormulaParser {
public void testParseComplexName() throws IOException {
// Mock up a spreadsheet to match the critical details of the sample
- HSSFWorkbook wb = new HSSFWorkbook();
- wb.createSheet("Sheet1");
- HSSFName definedName = wb.createName();
- definedName.setNameName("foo");
- definedName.setRefersToFormula("Sheet1!B2");
+ try (HSSFWorkbook wb = new HSSFWorkbook()) {
+ wb.createSheet("Sheet1");
+ HSSFName definedName = wb.createName();
+ definedName.setNameName("foo");
+ definedName.setRefersToFormula("Sheet1!B2");
- // Set the complex flag - POI doesn't usually manipulate this flag
- NameRecord nameRec = TestHSSFName.getNameRecord(definedName);
- nameRec.setOptionFlag((short)0x10); // 0x10 -> complex
+ // Set the complex flag - POI doesn't usually manipulate this flag
+ NameRecord nameRec = TestHSSFName.getNameRecord(definedName);
+ nameRec.setOptionFlag((short) 0x10); // 0x10 -> complex
- Ptg[] result;
- try {
- result = HSSFFormulaParser.parse("1+foo", wb);
- } catch (FormulaParseException e) {
- if (e.getMessage().equals("Specified name 'foo' is not a range as expected.")) {
- fail("Identified bug 47078c");
+ Ptg[] result;
+ try {
+ result = HSSFFormulaParser.parse("1+foo", wb);
+ } catch (FormulaParseException e) {
+ if (e.getMessage().equals("Specified name 'foo' is not a range as expected.")) {
+ fail("Identified bug 47078c");
+ }
+ throw e;
}
- wb.close();
- throw e;
+ confirmTokenClasses(result, IntPtg.class, NamePtg.class, AddPtg.class);
}
- confirmTokenClasses(result, IntPtg.class, NamePtg.class, AddPtg.class);
-
- wb.close();
}
/**
@@ -1562,23 +1517,16 @@ public final class TestFormulaParser {
String leadingZeroCellRef = "B000001"; // this should get parsed as "B1"
HSSFWorkbook wb = new HSSFWorkbook();
- try {
- HSSFFormulaParser.parse(badCellRef, wb);
- fail("Identified bug 47312b - Shouldn't be able to parse cell ref '"
- + badCellRef + "'.");
- } catch (FormulaParseException e) {
- // expected during successful test
- confirmParseException(e, "Specified named range '"
- + badCellRef + "' does not exist in the current workbook.");
- }
+ FormulaParseException e = assertThrows(FormulaParseException.class, () -> HSSFFormulaParser.parse(badCellRef, wb),
+ "Identified bug 47312b - Shouldn't be able to parse cell ref '" + badCellRef + "'.");
+ confirmParseException(e, "Specified named range '" + badCellRef + "' does not exist in the current workbook.");
Ptg[] ptgs;
try {
ptgs = HSSFFormulaParser.parse(leadingZeroCellRef, wb);
assertEquals("B1", ptgs[0].toFormulaString());
- } catch (FormulaParseException e) {
- confirmParseException(e, "Specified named range '"
- + leadingZeroCellRef + "' does not exist in the current workbook.");
+ } catch (FormulaParseException e2) {
+ confirmParseException(e2, "Specified named range '" + leadingZeroCellRef + "' does not exist in the current workbook.");
// close but no cigar
fail("Identified bug 47312c - '" + leadingZeroCellRef + "' should parse as 'B1'.");
}
@@ -1601,7 +1549,7 @@ public final class TestFormulaParser {
public void test57196_Formula() throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
Ptg[] ptgs = HSSFFormulaParser.parse("DEC2HEX(HEX2DEC(O8)-O2+D2)", wb, FormulaType.CELL, -1);
- assertNotNull("Ptg array should not be null", ptgs);
+ assertNotNull(ptgs, "Ptg array should not be null");
confirmTokenClasses(ptgs,
NameXPtg.class, // ??
diff --git a/src/testcases/org/apache/poi/hssf/model/TestFormulaParserEval.java b/src/testcases/org/apache/poi/hssf/model/TestFormulaParserEval.java
index aba91ad718..d948695620 100644
--- a/src/testcases/org/apache/poi/hssf/model/TestFormulaParserEval.java
+++ b/src/testcases/org/apache/poi/hssf/model/TestFormulaParserEval.java
@@ -17,7 +17,7 @@
package org.apache.poi.hssf.model;
-import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
@@ -30,7 +30,7 @@ import org.apache.poi.ss.formula.ptg.NamePtg;
import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
/**
* Test the low level formula parser functionality,
@@ -70,7 +70,7 @@ public final class TestFormulaParserEval {
*/
private static void confirmParseFormula(HSSFWorkbook workbook) {
Ptg[] ptgs = HSSFFormulaParser.parse("SUM(testName)", workbook);
- assertEquals("two tokens expected, got " + ptgs.length, 2, ptgs.length);
+ assertEquals(2, ptgs.length, "two tokens expected, got " + ptgs.length);
assertEquals(NamePtg.class, ptgs[0].getClass());
assertEquals(AttrPtg.class, ptgs[1].getClass());
}
diff --git a/src/testcases/org/apache/poi/hssf/model/TestFormulaParserIf.java b/src/testcases/org/apache/poi/hssf/model/TestFormulaParserIf.java
index 6897ccb88c..42f650a549 100644
--- a/src/testcases/org/apache/poi/hssf/model/TestFormulaParserIf.java
+++ b/src/testcases/org/apache/poi/hssf/model/TestFormulaParserIf.java
@@ -17,8 +17,8 @@
package org.apache.poi.hssf.model;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import org.apache.poi.ss.formula.ptg.AddPtg;
import org.apache.poi.ss.formula.ptg.AttrPtg;
@@ -33,7 +33,7 @@ import org.apache.poi.ss.formula.ptg.NotEqualPtg;
import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.ss.formula.ptg.RefPtg;
import org.apache.poi.ss.formula.ptg.StringPtg;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
/**
* Tests FormulaParser specifically with respect to IF() functions
@@ -49,7 +49,7 @@ public final class TestFormulaParserIf {
private static void confirmAttrData(Ptg[] ptgs, int i, int expectedData) {
Ptg ptg = ptgs[i];
- assertTrue("Token[" + i + "] was not AttrPtg as expected", ptg instanceof AttrPtg);
+ assertTrue(ptg instanceof AttrPtg, "Token[" + i + "] was not AttrPtg as expected");
AttrPtg attrPtg = (AttrPtg) ptg;
assertEquals(expectedData, attrPtg.getData());
}
@@ -164,24 +164,24 @@ public final class TestFormulaParserIf {
Ptg[] ptgs = parseFormula("IF(3>=1,\"*\",IF(4<>1,\"first\",\"second\"))");
assertEquals(17, ptgs.length);
- assertEquals("6th Ptg is not a goto (Attr) ptg",AttrPtg.class,ptgs[5].getClass());
- assertEquals("9th Ptg is not a not equal ptg",NotEqualPtg.class,ptgs[8].getClass());
- assertEquals("15th Ptg is not the inner IF variable function ptg",FuncVarPtg.class,ptgs[14].getClass());
+ assertEquals(AttrPtg.class, ptgs[5].getClass(), "6th Ptg is not a goto (Attr) ptg");
+ assertEquals(NotEqualPtg.class, ptgs[8].getClass(), "9th Ptg is not a not equal ptg");
+ assertEquals(FuncVarPtg.class, ptgs[14].getClass(), "15th Ptg is not the inner IF variable function ptg");
}
@Test
public void testSimpleLogical() {
Ptg[] ptgs = parseFormula("IF(A1OperandClassTransformer.
@@ -41,7 +41,7 @@ public final class TestOperandClassTransformer {
private static Ptg[] parseFormula(String formula) {
Ptg[] result = HSSFFormulaParser.parse(formula, null);
- assertNotNull("Ptg array should not be null", result);
+ assertNotNull(result, "Ptg array should not be null");
return result;
}
@@ -79,7 +79,7 @@ public final class TestOperandClassTransformer {
* This test has been added but disabled in order to document this issue.
*/
@Test
- @Ignore
+ @Disabled
public void testIndexPi1() {
String formula = "INDEX(PI(),1)";
Ptg[] ptgs = parseFormula(formula);
@@ -144,7 +144,7 @@ public final class TestOperandClassTransformer {
private void confirmTokenClass(Ptg[] ptgs, int i, byte operandClass) {
Ptg ptg = ptgs[i];
- assertFalse("ptg[" + i + "] is a base token", ptg.isBaseToken());
+ assertFalse(ptg.isBaseToken(), "ptg[" + i + "] is a base token");
assertEquals(operandClass, ptg.getPtgClass());
}
}
diff --git a/src/testcases/org/apache/poi/hssf/model/TestRVA.java b/src/testcases/org/apache/poi/hssf/model/TestRVA.java
index 20bc884622..8ca28bb6fc 100644
--- a/src/testcases/org/apache/poi/hssf/model/TestRVA.java
+++ b/src/testcases/org/apache/poi/hssf/model/TestRVA.java
@@ -17,12 +17,13 @@
package org.apache.poi.hssf.model;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.fail;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
import java.util.ArrayList;
-import java.util.Collection;
import java.util.List;
+import java.util.stream.Stream;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.FormulaExtractor;
@@ -34,19 +35,16 @@ import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.formula.ptg.AttrPtg;
import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.ss.usermodel.CellType;
-import org.junit.AfterClass;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameter;
-import org.junit.runners.Parameterized.Parameters;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
/**
* Tests 'operand class' transformation performed by
* OperandClassTransformer by comparing its results with those
* directly produced by Excel (in a sample spreadsheet).
*/
-@RunWith(Parameterized.class)
public final class TestRVA {
private static final String NEW_LINE = System.getProperty("line.separator");
@@ -54,25 +52,19 @@ public final class TestRVA {
private static HSSFWorkbook workbook;
- @Parameter(value = 0)
- public HSSFCell formulaCell;
- @Parameter(value = 1)
- public String formula;
-
- @AfterClass
+ @AfterAll
public static void closeResource() throws Exception {
workbook.close();
poifs.close();
}
- @Parameters(name="{1}")
- public static Collection