sonar fixes and closing resources after use

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1706937 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2015-10-06 00:04:12 +00:00
parent 28ccb12153
commit 62561d5f53
16 changed files with 239 additions and 260 deletions

View File

@ -66,7 +66,7 @@ public class InCellLists {
* @param outputFilename A String that encapsulates the name of and path to * @param outputFilename A String that encapsulates the name of and path to
* the Excel spreadsheet file this code will create. * the Excel spreadsheet file this code will create.
*/ */
public void demonstrateMethodCalls(String outputFilename) { public void demonstrateMethodCalls(String outputFilename) throws IOException {
HSSFWorkbook workbook = null; HSSFWorkbook workbook = null;
HSSFSheet sheet = null; HSSFSheet sheet = null;
HSSFRow row = null; HSSFRow row = null;
@ -75,7 +75,6 @@ public class InCellLists {
FileOutputStream fos = null; FileOutputStream fos = null;
ArrayList<MultiLevelListItem> multiLevelListItems = null; ArrayList<MultiLevelListItem> multiLevelListItems = null;
ArrayList<String> listItems = null; ArrayList<String> listItems = null;
String listItem = null;
try { try {
workbook = new HSSFWorkbook(); workbook = new HSSFWorkbook();
sheet = workbook.createSheet("In Cell Lists"); sheet = workbook.createSheet("In Cell Lists");
@ -188,13 +187,11 @@ public class InCellLists {
ioEx.printStackTrace(System.out); ioEx.printStackTrace(System.out);
} }
finally { finally {
if(fos != null) { if (workbook != null) {
try { workbook.close();
fos.close(); }
} if (fos != null) {
catch(IOException ioEx) { fos.close();
}
} }
} }
} }
@ -503,8 +500,8 @@ public class InCellLists {
* *
* @param args the command line arguments. * @param args the command line arguments.
*/ */
public static void main(String[] args) { public static void main(String[] args) throws IOException {
new InCellLists().demonstrateMethodCalls("C:/temp/Latest In Cell List.xls"); new InCellLists().demonstrateMethodCalls("Latest In Cell List.xls");
} }
/** /**

View File

@ -17,53 +17,55 @@
package org.apache.poi.hssf.usermodel.examples; package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.HSSFSheet; import java.io.Closeable;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
/** /**
* Creates outlines. * Creates outlines.
*
* @author Glen Stampoultzis (glens at apache.org)
*/ */
public class Outlines { public class Outlines implements Closeable {
public static void main(String[] args) throws IOException { public static void main(String[] args)
createCase1( "outline1.xls" ); throws IOException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
System.out.println( "outline1.xls written. Two expanded groups." ); POILogger LOGGER = POILogFactory.getLogger(Outlines.class);
createCase2( "outline2.xls" ); for (int i=1; i<=13; i++) {
System.out.println( "outline2.xls written. Two groups. Inner group collapsed." ); Outlines o = new Outlines();
createCase3( "outline3.xls" ); try {
System.out.println( "outline3.xls written. Two groups. Both collapsed." ); String log = (String)Outlines.class.getDeclaredMethod("test"+i).invoke(o);
createCase4( "outline4.xls" ); String filename = "outline"+i+".xls";
System.out.println( "outline4.xls written. Two groups. Collapsed then inner group expanded." ); o.writeOut(filename);
createCase5( "outline5.xls" ); LOGGER.log(POILogger.INFO, filename+" written. "+log);
System.out.println( "outline5.xls written. Two groups. Collapsed then reexpanded." ); } finally {
createCase6( "outline6.xls" ); o.close();
System.out.println( "outline6.xls written. Two groups with matching end points. Second group collapsed." ); }
createCase7( "outline7.xls" ); }
System.out.println( "outline7.xls written. Row outlines." );
createCase8( "outline8.xls" );
System.out.println( "outline8.xls written. Row outlines. Inner group collapsed." );
createCase9( "outline9.xls" );
System.out.println( "outline9.xls written. Row outlines. Both collapsed." );
createCase10( "outline10.xls" );
System.out.println( "outline10.xls written. Row outlines. Collapsed then inner group expanded." );
createCase11( "outline11.xls" );
System.out.println( "outline11.xls written. Row outlines. Collapsed then expanded." );
createCase12( "outline12.xls" );
System.out.println( "outline12.xls written. Row outlines. Two row groups with matching end points. Second group collapsed." );
createCase13( "outline13.xls" );
System.out.println( "outline13.xls written. Mixed bag." );
} }
private static void createCase1(String filename) throws IOException { private final HSSFWorkbook wb = new HSSFWorkbook();
HSSFWorkbook wb = new HSSFWorkbook(); private final HSSFSheet sheet1 = wb.createSheet("new sheet");
HSSFSheet sheet1 = wb.createSheet("new sheet");
public void writeOut(String filename) throws IOException {
FileOutputStream fileOut = new FileOutputStream(filename);
try {
wb.write(fileOut);
} finally {
fileOut.close();
}
}
public void close() throws IOException {
wb.close();
}
public String test1() {
sheet1.groupColumn(4, 7); sheet1.groupColumn(4, 7);
for (int row = 0; row < 200; row++) { for (int row = 0; row < 200; row++) {
@ -73,59 +75,35 @@ public class Outlines {
c.setCellValue(column); c.setCellValue(column);
} }
} }
return "Two expanded groups.";
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
} }
private static void createCase2(String filename) throws IOException { public String test2() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.groupColumn(2, 10); sheet1.groupColumn(2, 10);
sheet1.groupColumn(4, 7); sheet1.groupColumn(4, 7);
sheet1.setColumnGroupCollapsed(4, true); sheet1.setColumnGroupCollapsed(4, true);
return "Two groups. Inner group collapsed.";
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
} }
private static void createCase3(String filename) throws IOException { public String test3() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.groupColumn(2, 10); sheet1.groupColumn(2, 10);
sheet1.groupColumn(4, 7); sheet1.groupColumn(4, 7);
sheet1.setColumnGroupCollapsed(4, true); sheet1.setColumnGroupCollapsed(4, true);
sheet1.setColumnGroupCollapsed(2, true); sheet1.setColumnGroupCollapsed(2, true);
return "Two groups. Both collapsed.";
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
} }
private static void createCase4(String filename) throws IOException { public String test4() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.groupColumn(2, 10); sheet1.groupColumn(2, 10);
sheet1.groupColumn(4, 7); sheet1.groupColumn(4, 7);
sheet1.setColumnGroupCollapsed(4, true); sheet1.setColumnGroupCollapsed(4, true);
sheet1.setColumnGroupCollapsed(2, true); sheet1.setColumnGroupCollapsed(2, true);
sheet1.setColumnGroupCollapsed(4, false); sheet1.setColumnGroupCollapsed(4, false);
return "Two groups. Collapsed then inner group expanded.";
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
} }
private static void createCase5(String filename) throws IOException { public String test5() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.groupColumn(2, 10); sheet1.groupColumn(2, 10);
sheet1.groupColumn(4, 7); sheet1.groupColumn(4, 7);
sheet1.setColumnGroupCollapsed(4, true); sheet1.setColumnGroupCollapsed(4, true);
@ -133,117 +111,69 @@ public class Outlines {
sheet1.setColumnGroupCollapsed(4, false); sheet1.setColumnGroupCollapsed(4, false);
sheet1.setColumnGroupCollapsed(3, false); sheet1.setColumnGroupCollapsed(3, false);
return "Two groups. Collapsed then reexpanded.";
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
} }
private static void createCase6(String filename) throws IOException { public String test6() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.groupColumn(2, 10); sheet1.groupColumn(2, 10);
sheet1.groupColumn(4, 10); sheet1.groupColumn(4, 10);
sheet1.setColumnGroupCollapsed(4, true); sheet1.setColumnGroupCollapsed(4, true);
sheet1.setColumnGroupCollapsed(2, true); sheet1.setColumnGroupCollapsed(2, true);
sheet1.setColumnGroupCollapsed(3, false); sheet1.setColumnGroupCollapsed(3, false);
return "Two groups with matching end points. Second group collapsed.";
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
} }
private static void createCase7(String filename) throws IOException { public String test7() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.groupRow(5, 14); sheet1.groupRow(5, 14);
sheet1.groupRow(7, 10); sheet1.groupRow(7, 10);
return "Row outlines.";
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
} }
private static void createCase8(String filename) throws IOException { public String test8() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.groupRow(5, 14); sheet1.groupRow(5, 14);
sheet1.groupRow(7, 10); sheet1.groupRow(7, 10);
sheet1.setRowGroupCollapsed(7, true); sheet1.setRowGroupCollapsed(7, true);
return "Row outlines. Inner group collapsed.";
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
} }
private static void createCase9(String filename) throws IOException { public String test9() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.groupRow(5, 14); sheet1.groupRow(5, 14);
sheet1.groupRow(7, 10); sheet1.groupRow(7, 10);
sheet1.setRowGroupCollapsed(7, true); sheet1.setRowGroupCollapsed(7, true);
sheet1.setRowGroupCollapsed(5, true); sheet1.setRowGroupCollapsed(5, true);
return "Row outlines. Both collapsed.";
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
} }
private static void createCase10(String filename) throws IOException { public String test10() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.groupRow(5, 14); sheet1.groupRow(5, 14);
sheet1.groupRow(7, 10); sheet1.groupRow(7, 10);
sheet1.setRowGroupCollapsed(7, true); sheet1.setRowGroupCollapsed(7, true);
sheet1.setRowGroupCollapsed(5, true); sheet1.setRowGroupCollapsed(5, true);
sheet1.setRowGroupCollapsed(8, false); sheet1.setRowGroupCollapsed(8, false);
return "Row outlines. Collapsed then inner group expanded.";
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
} }
private static void createCase11(String filename) throws IOException { public String test11() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.groupRow(5, 14); sheet1.groupRow(5, 14);
sheet1.groupRow(7, 10); sheet1.groupRow(7, 10);
sheet1.setRowGroupCollapsed(7, true); sheet1.setRowGroupCollapsed(7, true);
sheet1.setRowGroupCollapsed(5, true); sheet1.setRowGroupCollapsed(5, true);
sheet1.setRowGroupCollapsed(8, false); sheet1.setRowGroupCollapsed(8, false);
sheet1.setRowGroupCollapsed(14, false); sheet1.setRowGroupCollapsed(14, false);
return "Row outlines. Collapsed then expanded.";
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
} }
private static void createCase12(String filename) throws IOException { public String test12() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.groupRow(5, 14); sheet1.groupRow(5, 14);
sheet1.groupRow(7, 14); sheet1.groupRow(7, 14);
sheet1.setRowGroupCollapsed(7, true); sheet1.setRowGroupCollapsed(7, true);
sheet1.setRowGroupCollapsed(5, true); sheet1.setRowGroupCollapsed(5, true);
sheet1.setRowGroupCollapsed(6, false); sheet1.setRowGroupCollapsed(6, false);
return "Row outlines. Two row groups with matching end points. Second group collapsed.";
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
} }
private static void createCase13(String filename) throws IOException { public String test13() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.groupRow(5, 14); sheet1.groupRow(5, 14);
sheet1.groupRow(7, 14); sheet1.groupRow(7, 14);
sheet1.groupRow(16, 19); sheet1.groupRow(16, 19);
@ -251,9 +181,6 @@ public class Outlines {
sheet1.groupColumn(4, 7); sheet1.groupColumn(4, 7);
sheet1.groupColumn(9, 12); sheet1.groupColumn(9, 12);
sheet1.groupColumn(10, 11); sheet1.groupColumn(10, 11);
return "Mixed bag.";
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
} }
} }

View File

@ -20,6 +20,7 @@ package org.apache.poi.hwpf;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.Writer; import java.io.Writer;
@ -215,19 +216,14 @@ public final class Word2Forrest
} }
public static void main(String[] args) public static void main(String[] args) throws IOException {
{ InputStream is = new FileInputStream(args[0]);
try OutputStream out = new FileOutputStream("test.xml");
{ try {
OutputStream out = new FileOutputStream("c:\\test.xml"); new Word2Forrest(new HWPFDocument(is), out);
} finally {
new Word2Forrest(new HWPFDocument(new FileInputStream(args[0])), out); out.close();
out.close(); is.close();
}
} }
catch (Exception t)
{
t.printStackTrace();
}
}
} }

View File

@ -18,20 +18,21 @@
package org.apache.poi.xssf.streaming.examples; package org.apache.poi.xssf.streaming.examples;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.streaming.SXSSFWorkbook;
public class Outlining { public class Outlining {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws IOException {
Outlining o = new Outlining(); Outlining o = new Outlining();
o.collapseRow(); o.collapseRow();
} }
private void collapseRow() throws Exception { private void collapseRow() throws IOException {
SXSSFWorkbook wb2 = new SXSSFWorkbook(100); SXSSFWorkbook wb2 = new SXSSFWorkbook(100);
SXSSFSheet sheet2 = (SXSSFSheet) wb2.createSheet("new sheet"); SXSSFSheet sheet2 = wb2.createSheet("new sheet");
int rowCount = 20; int rowCount = 20;
for (int i = 0; i < rowCount; i++) { for (int i = 0; i < rowCount; i++) {
@ -44,8 +45,12 @@ public class Outlining {
sheet2.setRowGroupCollapsed(4, true); sheet2.setRowGroupCollapsed(4, true);
FileOutputStream fileOut = new FileOutputStream("outlining_collapsed.xlsx"); FileOutputStream fileOut = new FileOutputStream("outlining_collapsed.xlsx");
wb2.write(fileOut); try {
fileOut.close(); wb2.write(fileOut);
wb2.dispose(); } finally {
fileOut.close();
wb2.dispose();
wb2.close();
}
} }
} }

View File

@ -50,7 +50,7 @@ public final class TabIdRecord extends StandardRecord {
* @param array of tab id's {0,1,2} * @param array of tab id's {0,1,2}
*/ */
public void setTabIdArray(short[] array) { public void setTabIdArray(short[] array) {
_tabids = array; _tabids = array.clone();
} }
public String toString() { public String toString() {

View File

@ -53,7 +53,7 @@ public enum CipherAlgorithm {
this.jceId = jceId; this.jceId = jceId;
this.ecmaId = ecmaId; this.ecmaId = ecmaId;
this.defaultKeySize = defaultKeySize; this.defaultKeySize = defaultKeySize;
this.allowedKeySize = allowedKeySize; this.allowedKeySize = allowedKeySize.clone();
this.blockSize = blockSize; this.blockSize = blockSize;
this.encryptedVerifierHashLength = encryptedVerifierHashLength; this.encryptedVerifierHashLength = encryptedVerifierHashLength;
this.xmlId = xmlId; this.xmlId = xmlId;

View File

@ -41,7 +41,7 @@ abstract class BlockListImpl implements BlockList {
*/ */
protected void setBlocks(final ListManagedBlock [] blocks) protected void setBlocks(final ListManagedBlock [] blocks)
{ {
_blocks = blocks; _blocks = blocks.clone();
} }
/** /**

View File

@ -365,11 +365,11 @@ public class DrawTextParagraph implements Drawable {
Double leftMargin = paragraph.getLeftMargin(); Double leftMargin = paragraph.getLeftMargin();
if (leftMargin == null) { if (leftMargin == null) {
// if the marL attribute is omitted, then a value of 347663 is implied // if the marL attribute is omitted, then a value of 347663 is implied
leftMargin = Units.toPoints(347663*(indentLevel+1)); leftMargin = Units.toPoints(347663L*(indentLevel+1));
} }
Double indent = paragraph.getIndent(); Double indent = paragraph.getIndent();
if (indent == null) { if (indent == null) {
indent = Units.toPoints(347663*indentLevel); indent = Units.toPoints(347663L*indentLevel);
} }
Double rightMargin = paragraph.getRightMargin(); Double rightMargin = paragraph.getRightMargin();
if (rightMargin == null) { if (rightMargin == null) {

View File

@ -60,7 +60,7 @@ public final class ArrayPtg extends Ptg {
_reserved2Byte = reserved2; _reserved2Byte = reserved2;
_nColumns = nColumns; _nColumns = nColumns;
_nRows = nRows; _nRows = nRows;
_arrayValues = arrayValues; _arrayValues = arrayValues.clone();
} }
/** /**
* @param values2d array values arranged in rows * @param values2d array values arranged in rows

View File

@ -117,7 +117,7 @@ public final class SSCellRange<K extends Cell> implements CellRange<K> {
private int _index; private int _index;
public ArrayIterator(D[] array) { public ArrayIterator(D[] array) {
_array = array; _array = array.clone();
_index = 0; _index = 0;
} }
public boolean hasNext() { public boolean hasNext() {

View File

@ -17,14 +17,23 @@
package org.apache.poi.xssf.usermodel; package org.apache.poi.xssf.usermodel;
import junit.framework.AssertionFailedError; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import org.apache.poi.ss.usermodel.*; import java.io.IOException;
import org.apache.poi.ss.usermodel.BaseTestSheetUpdateArrayFormulas;
import org.apache.poi.ss.usermodel.CellRange;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.XSSFITestDataProvider; import org.apache.poi.xssf.XSSFITestDataProvider;
import org.junit.Test;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellFormulaType; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellFormulaType;
import junit.framework.AssertionFailedError;
/** /**
* Test array formulas in XSSF * Test array formulas in XSSF
* *
@ -39,8 +48,8 @@ public final class TestXSSFSheetUpdateArrayFormulas extends BaseTestSheetUpdateA
// Test methods common with HSSF are in superclass // Test methods common with HSSF are in superclass
// Local methods here test XSSF-specific details of updating array formulas // Local methods here test XSSF-specific details of updating array formulas
@Test
public void testXSSFSetArrayFormula_singleCell() { public void testXSSFSetArrayFormula_singleCell() throws IOException {
CellRange<XSSFCell> cells; CellRange<XSSFCell> cells;
XSSFWorkbook workbook = new XSSFWorkbook(); XSSFWorkbook workbook = new XSSFWorkbook();
@ -58,9 +67,12 @@ public final class TestXSSFSheetUpdateArrayFormulas extends BaseTestSheetUpdateA
//retrieve the range and check it is the same //retrieve the range and check it is the same
assertEquals(range.formatAsString(), firstCell.getArrayFormulaRange().formatAsString()); assertEquals(range.formatAsString(), firstCell.getArrayFormulaRange().formatAsString());
confirmArrayFormulaCell(firstCell, "C3", formula1, "C3"); confirmArrayFormulaCell(firstCell, "C3", formula1, "C3");
workbook.close();
} }
public void testXSSFSetArrayFormula_multiCell() { @Test
public void testXSSFSetArrayFormula_multiCell() throws IOException {
CellRange<XSSFCell> cells; CellRange<XSSFCell> cells;
String formula2 = "456"; String formula2 = "456";
@ -84,6 +96,7 @@ public final class TestXSSFSheetUpdateArrayFormulas extends BaseTestSheetUpdateA
confirmArrayFormulaCell(cells.getCell(2, 0), "C6"); confirmArrayFormulaCell(cells.getCell(2, 0), "C6");
assertSame(firstCell, sheet.getFirstCellInArrayFormula(firstCell)); assertSame(firstCell, sheet.getFirstCellInArrayFormula(firstCell));
workbook.close();
} }
private static void confirmArrayFormulaCell(XSSFCell c, String cellRef) { private static void confirmArrayFormulaCell(XSSFCell c, String cellRef) {

View File

@ -31,7 +31,6 @@ import org.apache.poi.hslf.exceptions.OldPowerPointFormatException;
import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.DocumentEntry; import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger; import org.apache.poi.util.POILogger;
@ -112,13 +111,7 @@ public class CurrentUserAtom
isEncrypted = false; isEncrypted = false;
} }
/**
* Find the Current User in the filesystem, and create from that
* @deprecated Use {@link #CurrentUserAtom(DirectoryNode)} instead
*/
public CurrentUserAtom(POIFSFileSystem fs) throws IOException {
this(fs.getRoot());
}
/** /**
* Find the Current User in the filesystem, and create from that * Find the Current User in the filesystem, and create from that
*/ */
@ -162,14 +155,6 @@ public class CurrentUserAtom
init(); init();
} }
/**
* Create things from the bytes
*/
public CurrentUserAtom(byte[] b) {
_contents = b;
init();
}
/** /**
* Actually do the creation from a block of bytes * Actually do the creation from a block of bytes
*/ */

View File

@ -48,7 +48,7 @@ public final class TextBytesAtom extends RecordAtom
/** Updates the text in the Atom. Must be 8 bit ascii */ /** Updates the text in the Atom. Must be 8 bit ascii */
public void setText(byte[] b) { public void setText(byte[] b) {
// Set the text // Set the text
_text = b; _text = b.clone();
// Update the size (header bytes 5-8) // Update the size (header bytes 5-8)
LittleEndian.putInt(_header,4,_text.length); LittleEndian.putInt(_header,4,_text.length);

View File

@ -17,6 +17,7 @@
package org.apache.poi.hslf.record; package org.apache.poi.hslf.record;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -44,47 +45,48 @@ public final class TestCurrentUserAtom {
@Test @Test
public void readNormal() throws Exception { public void readNormal() throws Exception {
POIFSFileSystem fs = new POIFSFileSystem( POIFSFileSystem fs = new POIFSFileSystem(_slTests.getFile(normalFile));
_slTests.openResourceAsStream(normalFile)
);
CurrentUserAtom cu = new CurrentUserAtom(fs); CurrentUserAtom cu = new CurrentUserAtom(fs.getRoot());
fs.close();
// Check the contents // Check the contents
assertEquals("Hogwarts", cu.getLastEditUsername()); assertEquals("Hogwarts", cu.getLastEditUsername());
assertEquals(0x2942, cu.getCurrentEditOffset()); assertEquals(0x2942, cu.getCurrentEditOffset());
// Round trip // Round trip
ByteArrayOutputStream baos = new ByteArrayOutputStream(); POIFSFileSystem poifs = new POIFSFileSystem();
cu.writeOut(baos); cu.writeToFS(poifs);
CurrentUserAtom cu2 = new CurrentUserAtom(baos.toByteArray()); CurrentUserAtom cu2 = new CurrentUserAtom(poifs.getRoot());
assertEquals("Hogwarts", cu2.getLastEditUsername()); assertEquals("Hogwarts", cu2.getLastEditUsername());
assertEquals(0x2942, cu2.getCurrentEditOffset()); assertEquals(0x2942, cu2.getCurrentEditOffset());
poifs.close();
} }
@Test(expected = EncryptedPowerPointFileException.class) @Test(expected = EncryptedPowerPointFileException.class)
public void readEnc() throws Exception { public void readEnc() throws Exception {
POIFSFileSystem fs = new POIFSFileSystem( POIFSFileSystem fs = new POIFSFileSystem(_slTests.getFile(encFile));
_slTests.openResourceAsStream(encFile)
);
new CurrentUserAtom(fs); new CurrentUserAtom(fs.getRoot());
assertTrue(true); // not yet failed assertTrue(true); // not yet failed
new HSLFSlideShowImpl(fs); new HSLFSlideShowImpl(fs);
fs.close();
} }
@Test @Test
public void writeNormal() throws Exception { public void writeNormal() throws Exception {
// Get raw contents from a known file // Get raw contents from a known file
POIFSFileSystem fs = new POIFSFileSystem( POIFSFileSystem fs = new POIFSFileSystem(_slTests.getFile(normalFile));
_slTests.openResourceAsStream(normalFile)
);
DocumentEntry docProps = (DocumentEntry)fs.getRoot().getEntry("Current User"); DocumentEntry docProps = (DocumentEntry)fs.getRoot().getEntry("Current User");
byte[] contents = new byte[docProps.getSize()]; byte[] contents = new byte[docProps.getSize()];
InputStream in = fs.getRoot().createDocumentInputStream("Current User"); InputStream in = fs.getRoot().createDocumentInputStream("Current User");
in.read(contents); in.read(contents);
in.close();
fs.close();
// Now build up a new one // Now build up a new one
CurrentUserAtom cu = new CurrentUserAtom(); CurrentUserAtom cu = new CurrentUserAtom();
@ -96,9 +98,6 @@ public final class TestCurrentUserAtom {
cu.writeOut(baos); cu.writeOut(baos);
byte[] out = baos.toByteArray(); byte[] out = baos.toByteArray();
assertEquals(contents.length, out.length); assertArrayEquals(contents, out);
for(int i=0; i<contents.length; i++) {
assertEquals("Byte " + i, contents[i], out[i]);
}
} }
} }

View File

@ -17,7 +17,12 @@
package org.apache.poi.hssf.usermodel; package org.apache.poi.hssf.usermodel;
import junit.framework.AssertionFailedError; 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 java.io.IOException;
import org.apache.poi.hssf.HSSFITestDataProvider; import org.apache.poi.hssf.HSSFITestDataProvider;
import org.apache.poi.hssf.record.ArrayRecord; import org.apache.poi.hssf.record.ArrayRecord;
@ -27,8 +32,10 @@ import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
import org.apache.poi.hssf.record.aggregates.RowRecordsAggregate; import org.apache.poi.hssf.record.aggregates.RowRecordsAggregate;
import org.apache.poi.hssf.record.aggregates.SharedValueManager; import org.apache.poi.hssf.record.aggregates.SharedValueManager;
import org.apache.poi.hssf.record.aggregates.TestSharedValueManager; import org.apache.poi.hssf.record.aggregates.TestSharedValueManager;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.BaseTestSheetUpdateArrayFormulas;
import org.apache.poi.ss.usermodel.CellRange;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.junit.Test;
/** /**
* Test array formulas in HSSF * Test array formulas in HSSF
@ -44,8 +51,8 @@ public final class TestHSSFSheetUpdateArrayFormulas extends BaseTestSheetUpdateA
// Test methods common with XSSF are in superclass // Test methods common with XSSF are in superclass
// Local methods here test HSSF-specific details of updating array formulas // Local methods here test HSSF-specific details of updating array formulas
@Test
public void testHSSFSetArrayFormula_singleCell() { public void testHSSFSetArrayFormula_singleCell() throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook(); HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sheet1"); HSSFSheet sheet = workbook.createSheet("Sheet1");
@ -65,12 +72,15 @@ public final class TestHSSFSheetUpdateArrayFormulas extends BaseTestSheetUpdateA
FormulaRecordAggregate agg = (FormulaRecordAggregate)cell.getCellValueRecord(); FormulaRecordAggregate agg = (FormulaRecordAggregate)cell.getCellValueRecord();
assertEquals(range.formatAsString(), agg.getArrayFormulaRange().formatAsString()); assertEquals(range.formatAsString(), agg.getArrayFormulaRange().formatAsString());
assertTrue(agg.isPartOfArrayFormula()); assertTrue(agg.isPartOfArrayFormula());
workbook.close();
} }
/** /**
* Makes sure the internal state of HSSFSheet is consistent after removing array formulas * Makes sure the internal state of HSSFSheet is consistent after removing array formulas
*/ */
public void testAddRemoveArrayFormulas_recordUpdates() { @Test
public void testAddRemoveArrayFormulas_recordUpdates() throws IOException {
HSSFWorkbook wb = new HSSFWorkbook(); HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet("Sheet1"); HSSFSheet s = wb.createSheet("Sheet1");
@ -96,14 +106,16 @@ public final class TestHSSFSheetUpdateArrayFormulas extends BaseTestSheetUpdateA
RowRecordsAggregate rra = s.getSheet().getRowsAggregate(); RowRecordsAggregate rra = s.getSheet().getRowsAggregate();
SharedValueManager svm = TestSharedValueManager.extractFromRRA(rra); SharedValueManager svm = TestSharedValueManager.extractFromRRA(rra);
if (svm.getArrayRecord(4, 1) != null) { if (svm.getArrayRecord(4, 1) != null) {
throw new AssertionFailedError("Array record was not cleaned up properly."); fail("Array record was not cleaned up properly.");
} }
wb.close();
} }
private static void confirmRecordClass(Record[] recs, int index, Class<? extends Record> cls) { private static void confirmRecordClass(Record[] recs, int index, Class<? extends Record> cls) {
if (recs.length <= index) { if (recs.length <= index) {
throw new AssertionFailedError("Expected (" + cls.getName() + ") at index " fail("Expected (" + cls.getName() + ") at index "
+ index + " but array length is " + recs.length + "."); + index + " but array length is " + recs.length + ".");
} }
assertEquals(cls, recs[index].getClass()); assertEquals(cls, recs[index].getClass());
} }

View File

@ -18,12 +18,21 @@
package org.apache.poi.ss.usermodel; package org.apache.poi.ss.usermodel;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import junit.framework.TestCase; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException;
import org.apache.poi.ss.ITestDataProvider; import org.apache.poi.ss.ITestDataProvider;
import org.apache.poi.ss.formula.FormulaParseException; import org.apache.poi.ss.formula.FormulaParseException;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.CellReference;
import org.junit.Test;
/** /**
* Common superclass for testing usermodel API for array formulas.<br/> * Common superclass for testing usermodel API for array formulas.<br/>
@ -32,14 +41,15 @@ import org.apache.poi.ss.util.CellReference;
* @author Yegor Kozlov * @author Yegor Kozlov
* @author Josh Micich * @author Josh Micich
*/ */
public abstract class BaseTestSheetUpdateArrayFormulas extends TestCase { public abstract class BaseTestSheetUpdateArrayFormulas {
protected final ITestDataProvider _testDataProvider; protected final ITestDataProvider _testDataProvider;
protected BaseTestSheetUpdateArrayFormulas(ITestDataProvider testDataProvider) { protected BaseTestSheetUpdateArrayFormulas(ITestDataProvider testDataProvider) {
_testDataProvider = testDataProvider; _testDataProvider = testDataProvider;
} }
public final void testAutoCreateOtherCells() { @Test
public final void testAutoCreateOtherCells() throws IOException {
Workbook workbook = _testDataProvider.createWorkbook(); Workbook workbook = _testDataProvider.createWorkbook();
Sheet sheet = workbook.createSheet("Sheet1"); Sheet sheet = workbook.createSheet("Sheet1");
@ -55,11 +65,14 @@ public abstract class BaseTestSheetUpdateArrayFormulas extends TestCase {
assertNotNull(row2); assertNotNull(row2);
assertEquals(formula, row2.getCell(0).getCellFormula()); assertEquals(formula, row2.getCell(0).getCellFormula());
assertEquals(formula, row2.getCell(1).getCellFormula()); assertEquals(formula, row2.getCell(1).getCellFormula());
workbook.close();
} }
/** /**
* Set single-cell array formula * Set single-cell array formula
*/ */
public final void testSetArrayFormula_singleCell() { @Test
public final void testSetArrayFormula_singleCell() throws IOException {
Cell[] cells; Cell[] cells;
Workbook workbook = _testDataProvider.createWorkbook(); Workbook workbook = _testDataProvider.createWorkbook();
@ -88,12 +101,15 @@ public abstract class BaseTestSheetUpdateArrayFormulas extends TestCase {
assertEquals(range.formatAsString(), cell.getArrayFormulaRange().formatAsString()); assertEquals(range.formatAsString(), cell.getArrayFormulaRange().formatAsString());
//check the formula //check the formula
assertEquals("SUM(C11:C12*D11:D12)", cell.getCellFormula()); assertEquals("SUM(C11:C12*D11:D12)", cell.getCellFormula());
workbook.close();
} }
/** /**
* Set multi-cell array formula * Set multi-cell array formula
*/ */
public final void testSetArrayFormula_multiCell() { @Test
public final void testSetArrayFormula_multiCell() throws IOException {
Workbook workbook = _testDataProvider.createWorkbook(); Workbook workbook = _testDataProvider.createWorkbook();
Sheet sheet = workbook.createSheet(); Sheet sheet = workbook.createSheet();
@ -119,13 +135,16 @@ public abstract class BaseTestSheetUpdateArrayFormulas extends TestCase {
//retrieve the range and check it is the same //retrieve the range and check it is the same
assertEquals(range.formatAsString(), acell.getArrayFormulaRange().formatAsString()); assertEquals(range.formatAsString(), acell.getArrayFormulaRange().formatAsString());
} }
workbook.close();
} }
/** /**
* Passing an incorrect formula to sheet.setArrayFormula * Passing an incorrect formula to sheet.setArrayFormula
* should throw FormulaParseException * should throw FormulaParseException
*/ */
public final void testSetArrayFormula_incorrectFormula() { @Test
public final void testSetArrayFormula_incorrectFormula() throws IOException {
Workbook workbook = _testDataProvider.createWorkbook(); Workbook workbook = _testDataProvider.createWorkbook();
Sheet sheet = workbook.createSheet(); Sheet sheet = workbook.createSheet();
@ -136,13 +155,16 @@ public abstract class BaseTestSheetUpdateArrayFormulas extends TestCase {
} catch (FormulaParseException e){ } catch (FormulaParseException e){
//expected exception //expected exception
} }
workbook.close();
} }
/** /**
* Calls of cell.getArrayFormulaRange and sheet.removeArrayFormula * Calls of cell.getArrayFormulaRange and sheet.removeArrayFormula
* on a not-array-formula cell throw IllegalStateException * on a not-array-formula cell throw IllegalStateException
*/ */
public final void testArrayFormulas_illegalCalls() { @Test
public final void testArrayFormulas_illegalCalls() throws IOException {
Workbook workbook = _testDataProvider.createWorkbook(); Workbook workbook = _testDataProvider.createWorkbook();
Sheet sheet = workbook.createSheet(); Sheet sheet = workbook.createSheet();
@ -161,12 +183,15 @@ public abstract class BaseTestSheetUpdateArrayFormulas extends TestCase {
} catch (IllegalArgumentException e){ } catch (IllegalArgumentException e){
assertEquals("Cell A1 is not part of an array formula.", e.getMessage()); assertEquals("Cell A1 is not part of an array formula.", e.getMessage());
} }
workbook.close();
} }
/** /**
* create and remove array formulas * create and remove array formulas
*/ */
public final void testRemoveArrayFormula() { @Test
public final void testRemoveArrayFormula() throws IOException {
Workbook workbook = _testDataProvider.createWorkbook(); Workbook workbook = _testDataProvider.createWorkbook();
Sheet sheet = workbook.createSheet(); Sheet sheet = workbook.createSheet();
@ -196,28 +221,32 @@ public abstract class BaseTestSheetUpdateArrayFormulas extends TestCase {
assertEquals("Cell "+ref+" is not part of an array formula.", e.getMessage()); assertEquals("Cell "+ref+" is not part of an array formula.", e.getMessage());
} }
} }
workbook.close();
} }
/** /**
* Test that when reading a workbook from input stream, array formulas are recognized * Test that when reading a workbook from input stream, array formulas are recognized
*/ */
public final void testReadArrayFormula() { @Test
public final void testReadArrayFormula() throws IOException {
Cell[] cells; Cell[] cells;
Workbook workbook = _testDataProvider.createWorkbook(); Workbook workbook1 = _testDataProvider.createWorkbook();
Sheet sheet1 = workbook.createSheet(); Sheet sheet1 = workbook1.createSheet();
cells = sheet1.setArrayFormula("SUM(A1:A3*B1:B3)", CellRangeAddress.valueOf("C4:C6")).getFlattenedCells(); cells = sheet1.setArrayFormula("SUM(A1:A3*B1:B3)", CellRangeAddress.valueOf("C4:C6")).getFlattenedCells();
assertEquals(3, cells.length); assertEquals(3, cells.length);
cells = sheet1.setArrayFormula("MAX(A1:A3*B1:B3)", CellRangeAddress.valueOf("A4:A6")).getFlattenedCells(); cells = sheet1.setArrayFormula("MAX(A1:A3*B1:B3)", CellRangeAddress.valueOf("A4:A6")).getFlattenedCells();
assertEquals(3, cells.length); assertEquals(3, cells.length);
Sheet sheet2 = workbook.createSheet(); Sheet sheet2 = workbook1.createSheet();
cells = sheet2.setArrayFormula("MIN(A1:A3*B1:B3)", CellRangeAddress.valueOf("D2:D4")).getFlattenedCells(); cells = sheet2.setArrayFormula("MIN(A1:A3*B1:B3)", CellRangeAddress.valueOf("D2:D4")).getFlattenedCells();
assertEquals(3, cells.length); assertEquals(3, cells.length);
workbook = _testDataProvider.writeOutAndReadBack(workbook); Workbook workbook2 = _testDataProvider.writeOutAndReadBack(workbook1);
sheet1 = workbook.getSheetAt(0); workbook1.close();
sheet1 = workbook2.getSheetAt(0);
for(int rownum=3; rownum <= 5; rownum++) { for(int rownum=3; rownum <= 5; rownum++) {
Cell cell1 = sheet1.getRow(rownum).getCell(2); Cell cell1 = sheet1.getRow(rownum).getCell(2);
assertTrue( cell1.isPartOfArrayFormulaGroup()); assertTrue( cell1.isPartOfArrayFormulaGroup());
@ -226,17 +255,20 @@ public abstract class BaseTestSheetUpdateArrayFormulas extends TestCase {
assertTrue( cell2.isPartOfArrayFormulaGroup()); assertTrue( cell2.isPartOfArrayFormulaGroup());
} }
sheet2 = workbook.getSheetAt(1); sheet2 = workbook2.getSheetAt(1);
for(int rownum=1; rownum <= 3; rownum++) { for(int rownum=1; rownum <= 3; rownum++) {
Cell cell1 = sheet2.getRow(rownum).getCell(3); Cell cell1 = sheet2.getRow(rownum).getCell(3);
assertTrue( cell1.isPartOfArrayFormulaGroup()); assertTrue( cell1.isPartOfArrayFormulaGroup());
} }
workbook2.close();
} }
/** /**
* Test that we can set pre-calculated formula result for array formulas * Test that we can set pre-calculated formula result for array formulas
*/ */
public void testModifyArrayCells_setFormulaResult(){ @Test
public void testModifyArrayCells_setFormulaResult() throws IOException {
Workbook workbook = _testDataProvider.createWorkbook(); Workbook workbook = _testDataProvider.createWorkbook();
Sheet sheet = workbook.createSheet(); Sheet sheet = workbook.createSheet();
@ -245,23 +277,25 @@ public abstract class BaseTestSheetUpdateArrayFormulas extends TestCase {
sheet.setArrayFormula("SUM(A4:A6,B4:B6)", CellRangeAddress.valueOf("B5")); sheet.setArrayFormula("SUM(A4:A6,B4:B6)", CellRangeAddress.valueOf("B5"));
Cell scell = srange.getTopLeftCell(); Cell scell = srange.getTopLeftCell();
assertEquals(Cell.CELL_TYPE_FORMULA, scell.getCellType()); assertEquals(Cell.CELL_TYPE_FORMULA, scell.getCellType());
assertEquals(0.0, scell.getNumericCellValue()); assertEquals(0.0, scell.getNumericCellValue(), 0);
scell.setCellValue(1.1); scell.setCellValue(1.1);
assertEquals(1.1, scell.getNumericCellValue()); assertEquals(1.1, scell.getNumericCellValue(), 0);
//multi-cell array formula //multi-cell array formula
CellRange<? extends Cell> mrange = CellRange<? extends Cell> mrange =
sheet.setArrayFormula("A1:A3*B1:B3", CellRangeAddress.valueOf("C1:C3")); sheet.setArrayFormula("A1:A3*B1:B3", CellRangeAddress.valueOf("C1:C3"));
for(Cell mcell : mrange){ for(Cell mcell : mrange){
assertEquals(Cell.CELL_TYPE_FORMULA, mcell.getCellType()); assertEquals(Cell.CELL_TYPE_FORMULA, mcell.getCellType());
assertEquals(0.0, mcell.getNumericCellValue()); assertEquals(0.0, mcell.getNumericCellValue(), 0);
double fmlaResult = 1.2; double fmlaResult = 1.2;
mcell.setCellValue(fmlaResult); mcell.setCellValue(fmlaResult);
assertEquals(fmlaResult, mcell.getNumericCellValue()); assertEquals(fmlaResult, mcell.getNumericCellValue(), 0);
} }
workbook.close();
} }
public void testModifyArrayCells_setCellType(){ @Test
public void testModifyArrayCells_setCellType() throws IOException {
Workbook workbook = _testDataProvider.createWorkbook(); Workbook workbook = _testDataProvider.createWorkbook();
Sheet sheet = workbook.createSheet(); Sheet sheet = workbook.createSheet();
@ -271,7 +305,7 @@ public abstract class BaseTestSheetUpdateArrayFormulas extends TestCase {
sheet.setArrayFormula("SUM(A4:A6,B4:B6)", CellRangeAddress.valueOf("B5")); sheet.setArrayFormula("SUM(A4:A6,B4:B6)", CellRangeAddress.valueOf("B5"));
Cell scell = srange.getTopLeftCell(); Cell scell = srange.getTopLeftCell();
assertEquals(Cell.CELL_TYPE_FORMULA, scell.getCellType()); assertEquals(Cell.CELL_TYPE_FORMULA, scell.getCellType());
assertEquals(0.0, scell.getNumericCellValue()); assertEquals(0.0, scell.getNumericCellValue(), 0);
scell.setCellType(Cell.CELL_TYPE_STRING); scell.setCellType(Cell.CELL_TYPE_STRING);
assertEquals(Cell.CELL_TYPE_STRING, scell.getCellType()); assertEquals(Cell.CELL_TYPE_STRING, scell.getCellType());
scell.setCellValue("string cell"); scell.setCellValue("string cell");
@ -295,9 +329,11 @@ public abstract class BaseTestSheetUpdateArrayFormulas extends TestCase {
assertEquals(Cell.CELL_TYPE_FORMULA, mcell.getCellType()); assertEquals(Cell.CELL_TYPE_FORMULA, mcell.getCellType());
assertTrue(mcell.isPartOfArrayFormulaGroup()); assertTrue(mcell.isPartOfArrayFormulaGroup());
} }
workbook.close();
} }
public void testModifyArrayCells_setCellFormula(){ @Test
public void testModifyArrayCells_setCellFormula() throws IOException {
Workbook workbook = _testDataProvider.createWorkbook(); Workbook workbook = _testDataProvider.createWorkbook();
Sheet sheet = workbook.createSheet(); Sheet sheet = workbook.createSheet();
@ -313,9 +349,9 @@ public abstract class BaseTestSheetUpdateArrayFormulas extends TestCase {
assertFalse(scell.isPartOfArrayFormulaGroup()); assertFalse(scell.isPartOfArrayFormulaGroup());
assertEquals(Cell.CELL_TYPE_FORMULA, scell.getCellType()); assertEquals(Cell.CELL_TYPE_FORMULA, scell.getCellType());
//check that setting formula result works //check that setting formula result works
assertEquals(0.0, scell.getNumericCellValue()); assertEquals(0.0, scell.getNumericCellValue(), 0);
scell.setCellValue(33.0); scell.setCellValue(33.0);
assertEquals(33.0, scell.getNumericCellValue()); assertEquals(33.0, scell.getNumericCellValue(), 0);
//multi-cell array formula //multi-cell array formula
CellRange<? extends Cell> mrange = CellRange<? extends Cell> mrange =
@ -336,9 +372,11 @@ public abstract class BaseTestSheetUpdateArrayFormulas extends TestCase {
assertEquals("A1:A3*B1:B3", mcell.getCellFormula()); assertEquals("A1:A3*B1:B3", mcell.getCellFormula());
assertTrue(mcell.isPartOfArrayFormulaGroup()); assertTrue(mcell.isPartOfArrayFormulaGroup());
} }
workbook.close();
} }
public void testModifyArrayCells_removeCell(){ @Test
public void testModifyArrayCells_removeCell() throws IOException {
Workbook workbook = _testDataProvider.createWorkbook(); Workbook workbook = _testDataProvider.createWorkbook();
Sheet sheet = workbook.createSheet(); Sheet sheet = workbook.createSheet();
@ -378,9 +416,12 @@ public abstract class BaseTestSheetUpdateArrayFormulas extends TestCase {
assertTrue(mcell.isPartOfArrayFormulaGroup()); assertTrue(mcell.isPartOfArrayFormulaGroup());
assertEquals(Cell.CELL_TYPE_FORMULA, mcell.getCellType()); assertEquals(Cell.CELL_TYPE_FORMULA, mcell.getCellType());
} }
workbook.close();
} }
public void testModifyArrayCells_removeRow(){ @Test
public void testModifyArrayCells_removeRow() throws IOException {
Workbook workbook = _testDataProvider.createWorkbook(); Workbook workbook = _testDataProvider.createWorkbook();
Sheet sheet = workbook.createSheet(); Sheet sheet = workbook.createSheet();
@ -411,8 +452,8 @@ public abstract class BaseTestSheetUpdateArrayFormulas extends TestCase {
sheet.removeRow(mrow); sheet.removeRow(mrow);
fail("expected exception"); fail("expected exception");
} catch (IllegalStateException e){ } catch (IllegalStateException e){
String msg = "Row[rownum="+mrow.getRowNum()+"] contains cell(s) included in a multi-cell array formula. You cannot change part of an array."; // String msg = "Row[rownum="+mrow.getRowNum()+"] contains cell(s) included in a multi-cell array formula. You cannot change part of an array.";
//assertEquals(msg, e.getMessage()); // assertEquals(msg, e.getMessage());
} }
// a failed invocation of Row.removeCell leaves the row // a failed invocation of Row.removeCell leaves the row
// in the state that it was in prior to the invocation // in the state that it was in prior to the invocation
@ -421,9 +462,12 @@ public abstract class BaseTestSheetUpdateArrayFormulas extends TestCase {
assertTrue(mcell.isPartOfArrayFormulaGroup()); assertTrue(mcell.isPartOfArrayFormulaGroup());
assertEquals(Cell.CELL_TYPE_FORMULA, mcell.getCellType()); assertEquals(Cell.CELL_TYPE_FORMULA, mcell.getCellType());
} }
workbook.close();
} }
public void testModifyArrayCells_mergeCells(){ @Test
public void testModifyArrayCells_mergeCells() throws IOException {
Workbook workbook = _testDataProvider.createWorkbook(); Workbook workbook = _testDataProvider.createWorkbook();
Sheet sheet = workbook.createSheet(); Sheet sheet = workbook.createSheet();
assertEquals(0, sheet.getNumMergedRegions()); assertEquals(0, sheet.getNumMergedRegions());
@ -439,8 +483,7 @@ public abstract class BaseTestSheetUpdateArrayFormulas extends TestCase {
assertEquals(1, sheet.getNumMergedRegions()); assertEquals(1, sheet.getNumMergedRegions());
//we cannot merge cells included in an array formula //we cannot merge cells included in an array formula
CellRange<? extends Cell> mrange = sheet.setArrayFormula("A1:A3*B1:B3", CellRangeAddress.valueOf("C1:C3"));
sheet.setArrayFormula("A1:A3*B1:B3", CellRangeAddress.valueOf("C1:C3"));
CellRangeAddress cra = CellRangeAddress.valueOf("C1:C3"); CellRangeAddress cra = CellRangeAddress.valueOf("C1:C3");
try { try {
sheet.addMergedRegion(cra); sheet.addMergedRegion(cra);
@ -451,9 +494,11 @@ public abstract class BaseTestSheetUpdateArrayFormulas extends TestCase {
} }
//the number of merged regions remains the same //the number of merged regions remains the same
assertEquals(1, sheet.getNumMergedRegions()); assertEquals(1, sheet.getNumMergedRegions());
workbook.close();
} }
public void testModifyArrayCells_shiftRows(){ @Test
public void testModifyArrayCells_shiftRows() throws IOException {
Workbook workbook = _testDataProvider.createWorkbook(); Workbook workbook = _testDataProvider.createWorkbook();
Sheet sheet = workbook.createSheet(); Sheet sheet = workbook.createSheet();
@ -466,8 +511,7 @@ public abstract class BaseTestSheetUpdateArrayFormulas extends TestCase {
sheet.shiftRows(0, 1, 1); sheet.shiftRows(0, 1, 1);
//we cannot set individual formulas for cells included in an array formula //we cannot set individual formulas for cells included in an array formula
CellRange<? extends Cell> mrange = sheet.setArrayFormula("A1:A3*B1:B3", CellRangeAddress.valueOf("C1:C3"));
sheet.setArrayFormula("A1:A3*B1:B3", CellRangeAddress.valueOf("C1:C3"));
try { try {
sheet.shiftRows(0, 0, 1); sheet.shiftRows(0, 0, 1);
@ -491,5 +535,6 @@ public abstract class BaseTestSheetUpdateArrayFormulas extends TestCase {
} }
*/ */
workbook.close();
} }
} }