aboutsummaryrefslogtreecommitdiffstats
path: root/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java
diff options
context:
space:
mode:
authorJosh Micich <josh@apache.org>2008-04-07 03:02:03 +0000
committerJosh Micich <josh@apache.org>2008-04-07 03:02:03 +0000
commitbf756d96784b527c3cb77d455d9f0c322311d091 (patch)
tree08b9b03e4820164076827a8cbd0de2aca09275bf /src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java
parentb938b6c97f47f085757b330d1bce90293c863afb (diff)
downloadpoi-bf756d96784b527c3cb77d455d9f0c322311d091.tar.gz
poi-bf756d96784b527c3cb77d455d9f0c322311d091.zip
refactored all hssf junits to get test sample data in the in one place
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@645348 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java')
-rw-r--r--src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java670
1 files changed, 307 insertions, 363 deletions
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java b/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java
index 9eb12bd4e1..80bab8a445 100644
--- a/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java
@@ -1,4 +1,3 @@
-
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@@ -15,7 +14,6 @@
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
-
package org.apache.poi.hssf.usermodel;
@@ -27,6 +25,7 @@ import java.util.Date;
import junit.framework.TestCase;
+import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.util.TempFile;
@@ -34,64 +33,62 @@ import org.apache.poi.util.TempFile;
* @author Andrew C. Oliver (acoliver at apache dot org)
* @author Avik Sengupta
*/
+public final class TestFormulas extends TestCase {
-public class TestFormulas
-extends TestCase {
- public TestFormulas(String s) {
- super(s);
+ private static HSSFWorkbook openSample(String sampleFileName) {
+ return HSSFTestDataSamples.openSampleWorkbook(sampleFileName);
}
-
+
/**
* Add 1+1 -- WHoohoo!
*/
-
+
public void testBasicAddIntegers()
throws Exception {
-
- short rownum = 0;
+
File file = TempFile.createTempFile("testFormula",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
HSSFRow r = null;
HSSFCell c = null;
-
+
//get our minimum values
r = s.createRow((short)1);
c = r.createCell((short)1);
c.setCellFormula(1 + "+" + 1);
-
+
wb.write(out);
out.close();
-
+
FileInputStream in = new FileInputStream(file);
wb = new HSSFWorkbook(in);
s = wb.getSheetAt(0);
r = s.getRow((short)1);
c = r.getCell((short)1);
-
+
assertTrue("Formula is as expected",("1+1".equals(c.getCellFormula())));
in.close();
}
-
+
/**
* Add various integers
*/
-
+
public void testAddIntegers()
throws Exception {
binomialOperator("+");
}
-
+
/**
* Multiply various integers
*/
-
+
public void testMultplyIntegers()
throws Exception {
binomialOperator("*");
}
-
+
/**
* Subtract various integers
*/
@@ -99,7 +96,7 @@ extends TestCase {
throws Exception {
binomialOperator("-");
}
-
+
/**
* Subtract various integers
*/
@@ -107,7 +104,7 @@ extends TestCase {
throws Exception {
binomialOperator("/");
}
-
+
/**
* Exponentialize various integers;
*/
@@ -119,36 +116,36 @@ extends TestCase {
/**
* Concatinate two numbers 1&2 = 12
*/
- public void testConcatIntegers()
+ public void testConcatIntegers()
throws Exception {
binomialOperator("&");
}
-
+
/**
* tests 1*2+3*4
*/
- public void testOrderOfOperationsMultiply()
+ public void testOrderOfOperationsMultiply()
throws Exception {
orderTest("1*2+3*4");
}
-
+
/**
* tests 1*2+3^4
*/
- public void testOrderOfOperationsPower()
+ public void testOrderOfOperationsPower()
throws Exception {
orderTest("1*2+3^4");
}
-
+
/**
* Tests that parenthesis are obeyed
*/
- public void testParenthesis()
+ public void testParenthesis()
throws Exception {
orderTest("(1*3)+2+(1+2)*(3^4)^5");
}
-
- public void testReferencesOpr()
+
+ public void testReferencesOpr()
throws Exception {
String[] operation = new String[] {
"+", "-", "*", "/", "^", "&"
@@ -157,7 +154,7 @@ extends TestCase {
operationRefTest(operation[k]);
}
}
-
+
/**
* Tests creating a file with floating point in a formula.
*
@@ -167,32 +164,31 @@ extends TestCase {
floatTest("*");
floatTest("/");
}
-
+
private void floatTest(String operator)
throws Exception {
- short rownum = 0;
File file = TempFile.createTempFile("testFormulaFloat",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
HSSFRow r = null;
HSSFCell c = null;
-
+
//get our minimum values
-
+
r = s.createRow((short)0);
c = r.createCell((short)1);
c.setCellFormula(""+Float.MIN_VALUE + operator + Float.MIN_VALUE);
-
+
for (short x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2) ) {
r = s.createRow((short) x);
-
+
for (short y = 1; y < 256 && y > 0; y= (short) (y +2)) {
-
+
c = r.createCell((short) y);
c.setCellFormula("" + x+"."+y + operator + y +"."+x);
-
-
+
+
}
}
if (s.getLastRowNum() < Short.MAX_VALUE) {
@@ -205,21 +201,20 @@ extends TestCase {
assertTrue("file exists",file.exists());
out=null;wb=null; //otherwise we get out of memory error!
floatVerify(operator,file);
-
+
}
-
+
private void floatVerify(String operator, File file)
throws Exception {
- short rownum = 0;
-
+
FileInputStream in = new FileInputStream(file);
HSSFWorkbook wb = new HSSFWorkbook(in);
HSSFSheet s = wb.getSheetAt(0);
HSSFRow r = null;
HSSFCell c = null;
-
- // dont know how to check correct result .. for the moment, we just verify that the file can be read.
-
+
+ // dont know how to check correct result .. for the moment, we just verify that the file can be read.
+
for (short x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) {
r = s.getRow((short) x);
@@ -227,40 +222,40 @@ extends TestCase {
c = r.getCell((short) y);
assertTrue("got a formula",c.getCellFormula()!=null);
-
+
assertTrue("loop Formula is as expected "+x+"."+y+operator+y+"."+x+"!="+c.getCellFormula(),(
(""+x+"."+y+operator+y+"."+x).equals(c.getCellFormula()) ));
-
+
}
}
-
+
in.close();
assertTrue("file exists",file.exists());
}
-
- public void testAreaSum()
+
+ public void testAreaSum()
throws Exception {
areaFunctionTest("SUM");
}
-
- public void testAreaAverage()
+
+ public void testAreaAverage()
throws Exception {
areaFunctionTest("AVERAGE");
}
-
- public void testRefArraySum()
+
+ public void testRefArraySum()
throws Exception {
refArrayFunctionTest("SUM");
}
-
- public void testAreaArraySum()
+
+ public void testAreaArraySum()
throws Exception {
refAreaArrayFunctionTest("SUM");
}
-
-
- private void operationRefTest(String operator)
+
+
+ private void operationRefTest(String operator)
throws Exception {
File file = TempFile.createTempFile("testFormula",".xls");
FileOutputStream out = new FileOutputStream(file);
@@ -268,17 +263,17 @@ extends TestCase {
HSSFSheet s = wb.createSheet();
HSSFRow r = null;
HSSFCell c = null;
-
+
//get our minimum values
r = s.createRow((short)0);
c = r.createCell((short)1);
c.setCellFormula("A2" + operator + "A3");
-
+
for (short x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) {
r = s.createRow((short) x);
- for (short y = 1; y < 256 && y > 0; y++) {
-
+ for (short y = 1; y < 256 && y > 0; y++) {
+
String ref=null;
String ref2=null;
short refx1=0;
@@ -292,7 +287,7 @@ extends TestCase {
refx1=(short)(x-4);
refx2=(short)(x-3);
}
-
+
if (y+50 < 255) {
refy1=(short)(y+50);
refy2=(short)(y+49);
@@ -300,7 +295,7 @@ extends TestCase {
refy1=(short)(y-4);
refy2=(short)(y-3);
}
-
+
c = r.getCell((short) y);
CellReference cr= new CellReference(refx1,refy1, false, false);
ref=cr.formatAsString();
@@ -309,39 +304,38 @@ extends TestCase {
c = r.createCell((short) y);
c.setCellFormula("" + ref + operator + ref2);
-
-
+
+
}
}
-
+
//make sure we do the maximum value of the Int operator
if (s.getLastRowNum() < Short.MAX_VALUE) {
r = s.createRow((short)0);
c = r.createCell((short)0);
c.setCellFormula("" + "B1" + operator + "IV255");
}
-
+
wb.write(out);
out.close();
assertTrue("file exists",file.exists());
operationalRefVerify(operator,file);
}
-
+
/**
* Opens the sheet we wrote out by binomialOperator and makes sure the formulas
* all match what we expect (x operator y)
*/
private void operationalRefVerify(String operator, File file)
throws Exception {
- short rownum = 0;
-
+
FileInputStream in = new FileInputStream(file);
HSSFWorkbook wb = new HSSFWorkbook(in);
HSSFSheet s = wb.getSheetAt(0);
HSSFRow r = null;
HSSFCell c = null;
-
+
//get our minimum values
r = s.getRow((short)0);
c = r.getCell((short)1);
@@ -350,12 +344,12 @@ extends TestCase {
( ("A2"+operator+"A3").equals(c.getCellFormula())
));
-
+
for (short x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) {
r = s.getRow((short) x);
- for (short y = 1; y < 256 && y > 0; y++) {
-
+ for (short y = 1; y < 256 && y > 0; y++) {
+
String ref=null;
String ref2=null;
short refx1=0;
@@ -369,7 +363,7 @@ extends TestCase {
refx1=(short)(x-4);
refx2=(short)(x-3);
}
-
+
if (y+50 < 255) {
refy1=(short)(y+50);
refy2=(short)(y+49);
@@ -383,36 +377,36 @@ extends TestCase {
ref=cr.formatAsString();
cr=new CellReference(refx2,refy2, false, false);
ref2=cr.formatAsString();
-
-
+
+
assertTrue("loop Formula is as expected "+ref+operator+ref2+"!="+c.getCellFormula(),(
(""+ref+operator+ref2).equals(c.getCellFormula())
)
);
-
-
+
+
}
}
-
+
//test our maximum values
r = s.getRow((short)0);
c = r.getCell((short)0);
-
+
assertTrue("maxval Formula is as expected",(
("B1"+operator+"IV255").equals(c.getCellFormula())
)
);
-
+
in.close();
assertTrue("file exists",file.exists());
}
-
-
+
+
/**
* tests order wrting out == order writing in for a given formula
- */
- private void orderTest(String formula)
+ */
+ private void orderTest(String formula)
throws Exception {
File file = TempFile.createTempFile("testFormula",".xls");
FileOutputStream out = new FileOutputStream(file);
@@ -420,12 +414,12 @@ extends TestCase {
HSSFSheet s = wb.createSheet();
HSSFRow r = null;
HSSFCell c = null;
-
+
//get our minimum values
r = s.createRow((short)0);
c = r.createCell((short)1);
c.setCellFormula(formula);
-
+
wb.write(out);
out.close();
assertTrue("file exists",file.exists());
@@ -433,17 +427,17 @@ extends TestCase {
FileInputStream in = new FileInputStream(file);
wb = new HSSFWorkbook(in);
s = wb.getSheetAt(0);
-
+
//get our minimum values
r = s.getRow((short)0);
c = r.getCell((short)1);
assertTrue("minval Formula is as expected",
formula.equals(c.getCellFormula())
);
-
+
in.close();
}
-
+
/**
* All multi-binomial operator tests use this to create a worksheet with a
* huge set of x operator y formulas. Next we call binomialVerify and verify
@@ -451,19 +445,18 @@ extends TestCase {
*/
private void binomialOperator(String operator)
throws Exception {
- short rownum = 0;
File file = TempFile.createTempFile("testFormula",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
HSSFRow r = null;
HSSFCell c = null;
-
+
//get our minimum values
r = s.createRow((short)0);
c = r.createCell((short)1);
c.setCellFormula(1 + operator + 1);
-
+
for (short x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) {
r = s.createRow((short) x);
@@ -471,45 +464,44 @@ extends TestCase {
c = r.createCell((short) y);
c.setCellFormula("" + x + operator + y);
-
+
}
}
-
+
//make sure we do the maximum value of the Int operator
if (s.getLastRowNum() < Short.MAX_VALUE) {
r = s.createRow((short)0);
c = r.createCell((short)0);
c.setCellFormula("" + Short.MAX_VALUE + operator + Short.MAX_VALUE);
}
-
+
wb.write(out);
out.close();
assertTrue("file exists",file.exists());
-
+
binomialVerify(operator,file);
}
-
+
/**
* Opens the sheet we wrote out by binomialOperator and makes sure the formulas
* all match what we expect (x operator y)
*/
private void binomialVerify(String operator, File file)
throws Exception {
- short rownum = 0;
-
+
FileInputStream in = new FileInputStream(file);
HSSFWorkbook wb = new HSSFWorkbook(in);
HSSFSheet s = wb.getSheetAt(0);
HSSFRow r = null;
HSSFCell c = null;
-
+
//get our minimum values
r = s.getRow((short)0);
c = r.getCell((short)1);
assertTrue("minval Formula is as expected 1"+operator+"1 != "+c.getCellFormula(),
( ("1"+operator+"1").equals(c.getCellFormula())
));
-
+
for (short x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) {
r = s.getRow((short) x);
@@ -521,35 +513,34 @@ extends TestCase {
(""+x+operator+y).equals(c.getCellFormula())
)
);
-
-
+
+
}
}
-
+
//test our maximum values
r = s.getRow((short)0);
c = r.getCell((short)0);
-
-
+
+
assertTrue("maxval Formula is as expected",(
(""+Short.MAX_VALUE+operator+Short.MAX_VALUE).equals(c.getCellFormula())
)
);
-
+
in.close();
assertTrue("file exists",file.exists());
}
-
-
+
+
/**
* Writes a function then tests to see if its correct
*
*/
- public void areaFunctionTest(String function)
+ public void areaFunctionTest(String function)
throws Exception {
-
- short rownum = 0;
+
File file = TempFile.createTempFile("testFormulaAreaFunction"+function,".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
@@ -567,27 +558,26 @@ extends TestCase {
wb.write(out);
out.close();
assertTrue("file exists",file.exists());
-
+
FileInputStream in = new FileInputStream(file);
wb = new HSSFWorkbook(in);
s = wb.getSheetAt(0);
r = s.getRow(0);
c = r.getCell((short)0);
-
+
assertTrue("function ="+function+"(A2:A3)",
( (function+"(A2:A3)").equals((function+"(A2:A3)")) )
);
in.close();
}
-
+
/**
* Writes a function then tests to see if its correct
*
*/
- public void refArrayFunctionTest(String function)
+ public void refArrayFunctionTest(String function)
throws Exception {
-
- short rownum = 0;
+
File file = TempFile.createTempFile("testFormulaArrayFunction"+function,".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
@@ -605,28 +595,27 @@ extends TestCase {
wb.write(out);
out.close();
assertTrue("file exists",file.exists());
-
+
FileInputStream in = new FileInputStream(file);
wb = new HSSFWorkbook(in);
s = wb.getSheetAt(0);
r = s.getRow(0);
c = r.getCell((short)0);
-
+
assertTrue("function ="+function+"(A2,A3)",
( (function+"(A2,A3)").equals(c.getCellFormula()) )
);
in.close();
}
-
-
+
+
/**
* Writes a function then tests to see if its correct
*
*/
- public void refAreaArrayFunctionTest(String function)
+ public void refAreaArrayFunctionTest(String function)
throws Exception {
-
- short rownum = 0;
+
File file = TempFile.createTempFile("testFormulaAreaArrayFunction"+function,".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
@@ -645,26 +634,26 @@ extends TestCase {
wb.write(out);
out.close();
assertTrue("file exists",file.exists());
-
+
FileInputStream in = new FileInputStream(file);
wb = new HSSFWorkbook(in);
s = wb.getSheetAt(0);
r = s.getRow(0);
c = r.getCell((short)0);
-
+
assertTrue("function ="+function+"(A2:A4,B2:B4)",
( (function+"(A2:A4,B2:B4)").equals(c.getCellFormula()) )
);
-
+
c=r.getCell((short) 1);
assertTrue("function ="+function+"($A$2:$A4,B$2:B4)",
( (function+"($A$2:$A4,B$2:B4)").equals(c.getCellFormula()) )
);
in.close();
}
-
-
-
+
+
+
public void testAbsRefs() throws Exception {
File file = TempFile.createTempFile("testFormulaAbsRef",".xls");
FileOutputStream out = new FileOutputStream(file);
@@ -686,11 +675,11 @@ extends TestCase {
c.setCellFormula("$A$3+$A$2");
c=r.createCell( (short) 4);
c.setCellFormula("SUM($A$3,$A$2)");
-
+
wb.write(out);
out.close();
assertTrue("file exists",file.exists());
-
+
FileInputStream in = new FileInputStream(file);
wb = new HSSFWorkbook(in);
s = wb.getSheetAt(0);
@@ -707,12 +696,10 @@ extends TestCase {
assertTrue("SUM($A$3,$A$2)", ("SUM($A$3,$A$2)").equals(c.getCellFormula()));
in.close();
}
-
+
public void testSheetFunctions()
throws IOException
{
- String filename = System.getProperty("HSSF.testdata.path");
-
File file = TempFile.createTempFile("testSheetFormula",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
@@ -722,7 +709,7 @@ extends TestCase {
r = s.createRow((short)0);
c = r.createCell((short)0);c.setCellValue(1);
c = r.createCell((short)1);c.setCellValue(2);
-
+
s = wb.createSheet("B");
r = s.createRow((short)0);
c=r.createCell((short)0); c.setCellFormula("AVERAGE(A!A1:B1)");
@@ -730,9 +717,9 @@ extends TestCase {
c=r.createCell((short)2); c.setCellFormula("A!$A$1+A!$B1");
wb.write(out);
out.close();
-
+
assertTrue("file exists",file.exists());
-
+
FileInputStream in = new FileInputStream(file);
wb = new HSSFWorkbook(in);
s = wb.getSheet("B");
@@ -743,7 +730,7 @@ extends TestCase {
assertTrue("expected: A!A1+A!B1 got: "+c.getCellFormula(), ("A!A1+A!B1").equals(c.getCellFormula()));
in.close();
}
-
+
public void testRVAoperands() throws Exception {
File file = TempFile.createTempFile("testFormulaRVA",".xls");
FileOutputStream out = new FileOutputStream(file);
@@ -767,26 +754,24 @@ extends TestCase {
c.setCellFormula("POWER(A2,A3)");
c=r.createCell( (short) 5);
c.setCellFormula("SIN(A2)");
-
+
c=r.createCell( (short) 6);
c.setCellFormula("SUM(A2:A3)");
-
+
c=r.createCell( (short) 7);
c.setCellFormula("SUM(A2,A3)");
-
+
r = s.createRow((short) 1);c=r.createCell( (short) 0); c.setCellValue(2.0);
r = s.createRow((short) 2);c=r.createCell( (short) 0); c.setCellValue(3.0);
-
+
wb.write(out);
out.close();
assertTrue("file exists",file.exists());
}
-
+
public void testStringFormulas()
throws IOException
{
- String readFilename = System.getProperty("HSSF.testdata.path");
-
File file = TempFile.createTempFile("testStringFormula",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
@@ -797,25 +782,21 @@ extends TestCase {
c=r.createCell((short)1); c.setCellFormula("UPPER(\"abc\")");
c=r.createCell((short)2); c.setCellFormula("LOWER(\"ABC\")");
c=r.createCell((short)3); c.setCellFormula("CONCATENATE(\" my \",\" name \")");
-
+
wb.write(out);
out.close();
-
- assertTrue("file exists",file.exists());
-
- FileInputStream in = new FileInputStream(readFilename+File.separator+"StringFormulas.xls");
- wb = new HSSFWorkbook(in);
+
+ wb = openSample("StringFormulas.xls");
s = wb.getSheetAt(0);
r = s.getRow(0);
c = r.getCell((short)0);
assertTrue("expected: UPPER(\"xyz\") got "+c.getCellFormula(), ("UPPER(\"xyz\")").equals(c.getCellFormula()));
//c = r.getCell((short)1);
//assertTrue("expected: A!A1+A!B1 got: "+c.getCellFormula(), ("A!A1+A!B1").equals(c.getCellFormula()));
- in.close();
}
-
-
-
+
+
+
public void testLogicalFormulas()
throws IOException
{
@@ -829,26 +810,24 @@ extends TestCase {
r = s.createRow((short)0);
c=r.createCell((short)1); c.setCellFormula("IF(A1<A2,B1,B2)");
-
+
wb.write(out);
out.close();
-
+
assertTrue("file exists",file.exists());
-
+
FileInputStream in = new FileInputStream(file);
wb = new HSSFWorkbook(in);
s = wb.getSheetAt(0);
r = s.getRow(0);
c = r.getCell((short)1);
assertEquals("Formula in cell 1 ","IF(A1<A2,B1,B2)",c.getCellFormula());
- in.close();
+ in.close();
}
-
+
public void testDateFormulas()
throws IOException
{
- String readFilename = System.getProperty("HSSF.testdata.path");
-
File file = TempFile.createTempFile("testDateFormula",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
@@ -866,27 +845,25 @@ extends TestCase {
// assertEquals("Checking hour = " + hour, date.getTime().getTime(),
// HSSFDateUtil.getJavaDate(excelDate).getTime());
-
- for (int k=1; k < 100; k++) {
+
+ for (int k=1; k < 100; k++) {
r=s.createRow((short)k);
- c=r.createCell((short)0);
+ c=r.createCell((short)0);
c.setCellFormula("A"+(k)+"+1");
c.setCellStyle(cellStyle);
}
wb.write(out);
out.close();
-
+
assertTrue("file exists",file.exists());
-
+
}
-
+
public void testIfFormulas()
throws IOException
{
- String readFilename = System.getProperty("HSSF.testdata.path");
-
File file = TempFile.createTempFile("testIfFormula",".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
@@ -898,24 +875,22 @@ extends TestCase {
c=r.createCell((short)2); c.setCellValue(2);
c=r.createCell((short)3); c.setCellFormula("MAX(A1:B1)");
c=r.createCell((short)4); c.setCellFormula("IF(A1=D1,\"A1\",\"B1\")");
-
+
wb.write(out);
out.close();
-
+
assertTrue("file exists",file.exists());
-
+
FileInputStream in = new FileInputStream(file);
wb = new HSSFWorkbook(in);
s = wb.getSheetAt(0);
r = s.getRow(0);
c = r.getCell((short)4);
-
- assertTrue("expected: IF(A1=D1,\"A1\",\"B1\") got "+c.getCellFormula(), ("IF(A1=D1,\"A1\",\"B1\")").equals(c.getCellFormula()));
+
+ assertTrue("expected: IF(A1=D1,\"A1\",\"B1\") got "+c.getCellFormula(), ("IF(A1=D1,\"A1\",\"B1\")").equals(c.getCellFormula()));
in.close();
-
-
- in = new FileInputStream(readFilename+File.separator+"IfFormulaTest.xls");
- wb = new HSSFWorkbook(in);
+
+ wb = openSample("IfFormulaTest.xls");
s = wb.getSheetAt(0);
r = s.getRow(3);
c = r.getCell((short)0);
@@ -923,118 +898,107 @@ extends TestCase {
//c = r.getCell((short)1);
//assertTrue("expected: A!A1+A!B1 got: "+c.getCellFormula(), ("A!A1+A!B1").equals(c.getCellFormula()));
in.close();
-
- File simpleIf = TempFile.createTempFile("testSimpleIfFormulaWrite",".xls");
- out = new FileOutputStream(simpleIf);
- wb = new HSSFWorkbook();
- s = wb.createSheet("testSheet1");
- r = null;
- c = null;
- r = s.createRow((short)0);
- c=r.createCell((short)0); c.setCellFormula("IF(1=1,0,1)");
-
- wb.write(out);
- out.close();
- assertTrue("file exists", simpleIf.exists());
-
- assertTrue("length of simpleIf file is zero", (simpleIf.length()>0));
-
- File nestedIf = TempFile.createTempFile("testNestedIfFormula",".xls");
- out = new FileOutputStream(nestedIf);
- wb = new HSSFWorkbook();
- s = wb.createSheet("testSheet1");
- r = null;
- c = null;
- r = s.createRow((short)0);
- c=r.createCell((short)0);
- c.setCellValue(1);
-
- c=r.createCell((short)1);
- c.setCellValue(3);
-
-
- HSSFCell formulaCell=r.createCell((short)3);
-
- r = s.createRow((short)1);
- c=r.createCell((short)0);
- c.setCellValue(3);
-
- c=r.createCell((short)1);
- c.setCellValue(7);
-
- formulaCell.setCellFormula("IF(A1=B1,AVERAGE(A1:B1),AVERAGE(A2:B2))");
-
-
- wb.write(out);
- out.close();
- assertTrue("file exists", nestedIf.exists());
-
- assertTrue("length of nestedIf file is zero", (nestedIf.length()>0));
+
+ File simpleIf = TempFile.createTempFile("testSimpleIfFormulaWrite",".xls");
+ out = new FileOutputStream(simpleIf);
+ wb = new HSSFWorkbook();
+ s = wb.createSheet("testSheet1");
+ r = null;
+ c = null;
+ r = s.createRow((short)0);
+ c=r.createCell((short)0); c.setCellFormula("IF(1=1,0,1)");
+
+ wb.write(out);
+ out.close();
+ assertTrue("file exists", simpleIf.exists());
+
+ assertTrue("length of simpleIf file is zero", (simpleIf.length()>0));
+
+ File nestedIf = TempFile.createTempFile("testNestedIfFormula",".xls");
+ out = new FileOutputStream(nestedIf);
+ wb = new HSSFWorkbook();
+ s = wb.createSheet("testSheet1");
+ r = null;
+ c = null;
+ r = s.createRow((short)0);
+ c=r.createCell((short)0);
+ c.setCellValue(1);
+
+ c=r.createCell((short)1);
+ c.setCellValue(3);
+
+
+ HSSFCell formulaCell=r.createCell((short)3);
+
+ r = s.createRow((short)1);
+ c=r.createCell((short)0);
+ c.setCellValue(3);
+
+ c=r.createCell((short)1);
+ c.setCellValue(7);
+
+ formulaCell.setCellFormula("IF(A1=B1,AVERAGE(A1:B1),AVERAGE(A2:B2))");
+
+
+ wb.write(out);
+ out.close();
+ assertTrue("file exists", nestedIf.exists());
+
+ assertTrue("length of nestedIf file is zero", (nestedIf.length()>0));
}
- public void testSumIf()
- throws IOException
- {
- String readFilename = System.getProperty("HSSF.testdata.path");
- String function ="SUMIF(A1:A5,\">4000\",B1:B5)";
-
- File inFile = new File(readFilename+"/sumifformula.xls");
- FileInputStream in = new FileInputStream(inFile);
- HSSFWorkbook wb = new HSSFWorkbook(in);
- in.close();
-
- HSSFSheet s = wb.getSheetAt(0);
- HSSFRow r = s.getRow(0);
- HSSFCell c = r.getCell((short)2);
- assertEquals(function, c.getCellFormula());
-
-
- File file = TempFile.createTempFile("testSumIfFormula",".xls");
- FileOutputStream out = new FileOutputStream(file);
- wb = new HSSFWorkbook();
- s = wb.createSheet();
-
- r = s.createRow((short)0);
- c=r.createCell((short)0); c.setCellValue((double)1000);
- c=r.createCell((short)1); c.setCellValue((double)1);
-
-
- r = s.createRow((short)1);
- c=r.createCell((short)0); c.setCellValue((double)2000);
- c=r.createCell((short)1); c.setCellValue((double)2);
-
- r = s.createRow((short)2);
- c=r.createCell((short)0); c.setCellValue((double)3000);
- c=r.createCell((short)1); c.setCellValue((double)3);
-
- r = s.createRow((short)3);
- c=r.createCell((short)0); c.setCellValue((double)4000);
- c=r.createCell((short)1); c.setCellValue((double)4);
-
- r = s.createRow((short)4);
- c=r.createCell((short)0); c.setCellValue((double)5000);
- c=r.createCell((short)1); c.setCellValue((double)5);
-
- r = s.getRow(0);
- c=r.createCell((short)2); c.setCellFormula(function);
-
- wb.write(out);
- out.close();
-
- assertTrue("sumif file doesnt exists", (file.exists()));
- assertTrue("sumif == 0 bytes", file.length() > 0);
- }
-
- public void testSquareMacro() throws IOException {
- File dir = new File(System.getProperty("HSSF.testdata.path"));
- File xls = new File(dir, "SquareMacro.xls");
- FileInputStream in = new FileInputStream(xls);
- HSSFWorkbook w;
- try {
- w = new HSSFWorkbook(in);
- } finally {
- in.close();
- }
+ public void testSumIf()
+ throws IOException
+ {
+ String function ="SUMIF(A1:A5,\">4000\",B1:B5)";
+
+ HSSFWorkbook wb = openSample("sumifformula.xls");
+
+ HSSFSheet s = wb.getSheetAt(0);
+ HSSFRow r = s.getRow(0);
+ HSSFCell c = r.getCell((short)2);
+ assertEquals(function, c.getCellFormula());
+
+
+ File file = TempFile.createTempFile("testSumIfFormula",".xls");
+ FileOutputStream out = new FileOutputStream(file);
+ wb = new HSSFWorkbook();
+ s = wb.createSheet();
+
+ r = s.createRow((short)0);
+ c=r.createCell((short)0); c.setCellValue((double)1000);
+ c=r.createCell((short)1); c.setCellValue((double)1);
+
+
+ r = s.createRow((short)1);
+ c=r.createCell((short)0); c.setCellValue((double)2000);
+ c=r.createCell((short)1); c.setCellValue((double)2);
+
+ r = s.createRow((short)2);
+ c=r.createCell((short)0); c.setCellValue((double)3000);
+ c=r.createCell((short)1); c.setCellValue((double)3);
+
+ r = s.createRow((short)3);
+ c=r.createCell((short)0); c.setCellValue((double)4000);
+ c=r.createCell((short)1); c.setCellValue((double)4);
+
+ r = s.createRow((short)4);
+ c=r.createCell((short)0); c.setCellValue((double)5000);
+ c=r.createCell((short)1); c.setCellValue((double)5);
+
+ r = s.getRow(0);
+ c=r.createCell((short)2); c.setCellFormula(function);
+
+ wb.write(out);
+ out.close();
+
+ assertTrue("sumif file doesnt exists", (file.exists()));
+ assertTrue("sumif == 0 bytes", file.length() > 0);
+ }
+
+ public void testSquareMacro() {
+ HSSFWorkbook w = openSample("SquareMacro.xls");
+
HSSFSheet s0 = w.getSheetAt(0);
HSSFRow[] r = {s0.getRow(0), s0.getRow(1)};
@@ -1071,78 +1035,58 @@ extends TestCase {
assertEquals(4d, d2.getNumericCellValue(), 1e-9);
}
- public void testStringFormulaRead() throws IOException {
- File dir = new File(System.getProperty("HSSF.testdata.path"));
- File xls = new File(dir, "StringFormulas.xls");
- FileInputStream in = new FileInputStream(xls);
- HSSFWorkbook w;
- try {
- w = new HSSFWorkbook(in);
- } finally {
- in.close();
- }
+ public void testStringFormulaRead() {
+ HSSFWorkbook w = openSample("StringFormulas.xls");
HSSFCell c = w.getSheetAt(0).getRow(0).getCell((short)0);
- assertEquals("String Cell value","XYZ",c.getStringCellValue());
+ assertEquals("String Cell value","XYZ",c.getRichStringCellValue().getString());
}
-
+
/** test for bug 34021*/
public void testComplexSheetRefs () throws IOException {
- HSSFWorkbook sb = new HSSFWorkbook();
- HSSFSheet s1 = sb.createSheet("Sheet a.1");
- HSSFSheet s2 = sb.createSheet("Sheet.A");
- s2.createRow(1).createCell((short) 2).setCellFormula("'Sheet a.1'!A1");
- s1.createRow(1).createCell((short) 2).setCellFormula("'Sheet.A'!A1");
- File file = TempFile.createTempFile("testComplexSheetRefs",".xls");
- sb.write(new FileOutputStream(file));
+ HSSFWorkbook sb = new HSSFWorkbook();
+ HSSFSheet s1 = sb.createSheet("Sheet a.1");
+ HSSFSheet s2 = sb.createSheet("Sheet.A");
+ s2.createRow(1).createCell((short) 2).setCellFormula("'Sheet a.1'!A1");
+ s1.createRow(1).createCell((short) 2).setCellFormula("'Sheet.A'!A1");
+ File file = TempFile.createTempFile("testComplexSheetRefs",".xls");
+ sb.write(new FileOutputStream(file));
+ }
+
+ /*Unknown Ptg 3C*/
+ public void test27272_1() throws Exception {
+ HSSFWorkbook wb = openSample("27272_1.xls");
+ wb.getSheetAt(0);
+ assertEquals("Reference for named range ", "#REF!",wb.getNameAt(0).getReference());
+ File outF = File.createTempFile("bug27272_1",".xls");
+ wb.write(new FileOutputStream(outF));
+ System.out.println("Open "+outF.getAbsolutePath()+" in Excel");
+ }
+ /*Unknown Ptg 3D*/
+ public void test27272_2() throws Exception {
+ HSSFWorkbook wb = openSample("27272_2.xls");
+ assertEquals("Reference for named range ", "#REF!",wb.getNameAt(0).getReference());
+ File outF = File.createTempFile("bug27272_2",".xls");
+ wb.write(new FileOutputStream(outF));
+ System.out.println("Open "+outF.getAbsolutePath()+" in Excel");
}
-
- /*Unknown Ptg 3C*/
- public void test27272_1() throws Exception {
- String readFilename = System.getProperty("HSSF.testdata.path");
- File inFile = new File(readFilename+"/27272_1.xls");
- FileInputStream in = new FileInputStream(inFile);
- HSSFWorkbook wb = new HSSFWorkbook(in);
- wb.getSheetAt(0);
- assertEquals("Reference for named range ", "#REF!",wb.getNameAt(0).getReference());
- File outF = File.createTempFile("bug27272_1",".xls");
- wb.write(new FileOutputStream(outF));
- System.out.println("Open "+outF.getAbsolutePath()+" in Excel");
- }
- /*Unknown Ptg 3D*/
- public void test27272_2() throws Exception {
- String readFilename = System.getProperty("HSSF.testdata.path");
- File inFile = new File(readFilename+"/27272_2.xls");
- FileInputStream in = new FileInputStream(inFile);
- HSSFWorkbook wb = new HSSFWorkbook(in);
- assertEquals("Reference for named range ", "#REF!",wb.getNameAt(0).getReference());
- File outF = File.createTempFile("bug27272_2",".xls");
- wb.write(new FileOutputStream(outF));
- System.out.println("Open "+outF.getAbsolutePath()+" in Excel");
- }
-
- /* MissingArgPtg */
- public void testMissingArgPtg() throws Exception {
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFCell cell = wb.createSheet("Sheet1").createRow(4).createCell((short) 0);
- cell.setCellFormula("IF(A1=\"A\",1,)");
- }
-
- public void testSharedFormula() throws Exception {
- String readFilename = System.getProperty("HSSF.testdata.path");
- File inFile = new File(readFilename+"/SharedFormulaTest.xls");
- FileInputStream fis = new FileInputStream(inFile);
- HSSFWorkbook wb = new HSSFWorkbook(fis);
-
- assertEquals("A$1*2", wb.getSheetAt(0).getRow(1).getCell((short)1).toString());
- assertEquals("$A11*2", wb.getSheetAt(0).getRow(11).getCell((short)1).toString());
- assertEquals("DZ2*2", wb.getSheetAt(0).getRow(1).getCell((short)128).toString());
- assertEquals("B32770*2", wb.getSheetAt(0).getRow(32768).getCell((short)1).toString());
- }
-
+
+ /* MissingArgPtg */
+ public void testMissingArgPtg() throws Exception {
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFCell cell = wb.createSheet("Sheet1").createRow(4).createCell((short) 0);
+ cell.setCellFormula("IF(A1=\"A\",1,)");
+ }
+
+ public void testSharedFormula() {
+ HSSFWorkbook wb = openSample("SharedFormulaTest.xls");
+
+ assertEquals("A$1*2", wb.getSheetAt(0).getRow(1).getCell((short)1).toString());
+ assertEquals("$A11*2", wb.getSheetAt(0).getRow(11).getCell((short)1).toString());
+ assertEquals("DZ2*2", wb.getSheetAt(0).getRow(1).getCell((short)128).toString());
+ assertEquals("B32770*2", wb.getSheetAt(0).getRow(32768).getCell((short)1).toString());
+ }
+
public static void main(String [] args) {
- System.out
- .println("Testing org.apache.poi.hssf.usermodel.TestFormulas");
junit.textui.TestRunner.run(TestFormulas.class);
}
-
}