fix more LGTM alerts, in tests

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1843557 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Alain Béarez 2018-10-11 14:50:27 +00:00
parent fcbed10cfc
commit 5a53f36391
11 changed files with 438 additions and 380 deletions

View File

@ -17,7 +17,25 @@
package org.apache.poi.ooxml.util; package org.apache.poi.ooxml.util;
import junit.framework.TestCase; import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.security.AccessController;
import java.security.CodeSource;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Pattern;
import org.apache.poi.util.IOUtils; import org.apache.poi.util.IOUtils;
import org.apache.poi.util.StringUtil; import org.apache.poi.util.StringUtil;
import org.apache.poi.util.SuppressForbidden; import org.apache.poi.util.SuppressForbidden;
@ -29,19 +47,7 @@ import org.junit.runner.JUnitCore;
import org.junit.runner.Result; import org.junit.runner.Result;
import org.reflections.Reflections; import org.reflections.Reflections;
import java.io.File; import junit.framework.TestCase;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.security.AccessController;
import java.security.CodeSource;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Pattern;
/** /**
* Build a 'lite' version of the ooxml-schemas.jar * Build a 'lite' version of the ooxml-schemas.jar
@ -87,13 +93,13 @@ public final class OOXMLLite {
for (int i = 0; i < args.length; i++) { for (int i = 0; i < args.length; i++) {
switch (args[i]) { switch (args[i]) {
case "-dest": case "-dest":
dest = args[++i]; dest = args[++i]; // lgtm[java/index-out-of-bounds]
break; break;
case "-test": case "-test":
test = args[++i]; test = args[++i]; // lgtm[java/index-out-of-bounds]
break; break;
case "-ooxml": case "-ooxml":
ooxml = args[++i]; ooxml = args[++i]; // lgtm[java/index-out-of-bounds]
break; break;
} }
} }
@ -286,8 +292,12 @@ public final class OOXMLLite {
String path = arg.getAbsolutePath(); String path = arg.getAbsolutePath();
String prefix = root.getAbsolutePath(); String prefix = root.getAbsolutePath();
String cls = path.substring(prefix.length() + 1).replace(File.separator, "."); String cls = path.substring(prefix.length() + 1).replace(File.separator, ".");
if(!cls.matches(ptrn)) return; if(!cls.matches(ptrn)) {
if (cls.matches(exclude)) return; return;
}
if (cls.matches(exclude)) {
return;
}
//ignore inner classes defined in tests //ignore inner classes defined in tests
if (cls.indexOf('$') != -1) { if (cls.indexOf('$') != -1) {
System.out.println("Inner class " + cls + " not included"); System.out.println("Inner class " + cls + " not included");
@ -319,6 +329,7 @@ public final class OOXMLLite {
// allow JDKs which do not have this field (e.g. IBM JDK) to at least load the class // allow JDKs which do not have this field (e.g. IBM JDK) to at least load the class
// without failing, see https://issues.apache.org/bugzilla/show_bug.cgi?id=56550 // without failing, see https://issues.apache.org/bugzilla/show_bug.cgi?id=56550
final Field _classes = AccessController.doPrivileged(new PrivilegedAction<Field>() { final Field _classes = AccessController.doPrivileged(new PrivilegedAction<Field>() {
@Override
@SuppressForbidden("TODO: Reflection works until Java 8 on Oracle/Sun JDKs, but breaks afterwards (different classloader types, access checks)") @SuppressForbidden("TODO: Reflection works until Java 8 on Oracle/Sun JDKs, but breaks afterwards (different classloader types, access checks)")
public Field run() { public Field run() {
try { try {
@ -339,11 +350,17 @@ public final class OOXMLLite {
for (Class<?> cls : classes) { for (Class<?> cls : classes) {
// e.g. proxy-classes, ... // e.g. proxy-classes, ...
ProtectionDomain pd = cls.getProtectionDomain(); ProtectionDomain pd = cls.getProtectionDomain();
if (pd == null) continue; if (pd == null) {
continue;
}
CodeSource cs = pd.getCodeSource(); CodeSource cs = pd.getCodeSource();
if (cs == null) continue; if (cs == null) {
continue;
}
URL loc = cs.getLocation(); URL loc = cs.getLocation();
if (loc == null) continue; if (loc == null) {
continue;
}
String jar = loc.toString(); String jar = loc.toString();
if (jar.contains(ptrn)) { if (jar.contains(ptrn)) {

View File

@ -30,7 +30,6 @@ import java.io.IOException;
import java.util.Set; import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
import junit.framework.AssertionFailedError;
import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.poi.util.IOUtils; import org.apache.poi.util.IOUtils;
@ -44,125 +43,130 @@ import org.xmlunit.diff.Diff;
import org.xmlunit.diff.DifferenceEvaluator; import org.xmlunit.diff.DifferenceEvaluator;
import org.xmlunit.diff.ElementSelectors; import org.xmlunit.diff.ElementSelectors;
import junit.framework.AssertionFailedError;
/** /**
* Compare the contents of 2 zip files. * Compare the contents of 2 zip files.
*/ */
public final class ZipFileAssert { public final class ZipFileAssert {
private ZipFileAssert() { private ZipFileAssert() {
} }
private static void equals( private static void equals(
TreeMap<String, ByteArrayOutputStream> file1, TreeMap<String, ByteArrayOutputStream> file1,
TreeMap<String, ByteArrayOutputStream> file2) { TreeMap<String, ByteArrayOutputStream> file2) {
Set<String> listFile1 = file1.keySet(); Set<String> listFile1 = file1.keySet();
Assert.assertEquals("not the same number of files in zip:", listFile1.size(), file2.keySet().size()); Assert.assertEquals("not the same number of files in zip:", listFile1.size(), file2.keySet().size());
for (String fileName : listFile1) { for (String fileName : listFile1) {
// extract the contents for both // extract the contents for both
ByteArrayOutputStream contain1 = file1.get(fileName); ByteArrayOutputStream contain1 = file1.get(fileName);
ByteArrayOutputStream contain2 = file2.get(fileName); ByteArrayOutputStream contain2 = file2.get(fileName);
assertNotNull(fileName + " not found in 2nd zip", contain2); assertNotNull(fileName + " not found in 2nd zip", contain2);
// no need to check for contain1. The key come from it // no need to check for contain1. The key come from it
if (fileName.matches(".*\\.(xml|rels)$")) { if (fileName.matches(".*\\.(xml|rels)$")) {
// we have a xml file // we have a xml file
final Diff diff = DiffBuilder. final Diff diff = DiffBuilder.
compare(Input.fromByteArray(contain1.toByteArray())). compare(Input.fromByteArray(contain1.toByteArray())).
withTest(Input.fromByteArray(contain2.toByteArray())). withTest(Input.fromByteArray(contain2.toByteArray())).
ignoreWhitespace(). ignoreWhitespace().
checkForSimilar(). checkForSimilar().
withDifferenceEvaluator(new IgnoreXMLDeclEvaluator()). withDifferenceEvaluator(new IgnoreXMLDeclEvaluator()).
withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndAllAttributes, ElementSelectors.byNameAndText)). withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndAllAttributes, ElementSelectors.byNameAndText)).
build(); build();
assertFalse(fileName+": "+diff.toString(), diff.hasDifferences()); assertFalse(fileName+": "+diff.toString(), diff.hasDifferences());
} else { } else {
// not xml, may be an image or other binary format // not xml, may be an image or other binary format
Assert.assertEquals(fileName + " does not have the same size in both zip:", contain1.size(), contain2.size()); Assert.assertEquals(fileName + " does not have the same size in both zip:", contain1.size(), contain2.size());
assertArrayEquals("contents differ", contain1.toByteArray(), contain2.toByteArray()); assertArrayEquals("contents differ", contain1.toByteArray(), contain2.toByteArray());
} }
} }
} }
private static TreeMap<String, ByteArrayOutputStream> decompress( private static TreeMap<String, ByteArrayOutputStream> decompress(
File filename) throws IOException { File filename) throws IOException {
// store the zip content in memory // store the zip content in memory
// let s assume it is not Go ;-) // let s assume it is not Go ;-)
TreeMap<String, ByteArrayOutputStream> zipContent = new TreeMap<>(); TreeMap<String, ByteArrayOutputStream> zipContent = new TreeMap<>();
/* Open file to decompress */ try (
FileInputStream file_decompress = new FileInputStream(filename); /* Open file to decompress */
FileInputStream file_decompress = new FileInputStream(filename);
/* Create a buffer for the decompressed files */ /* Create a buffer for the decompressed files */
BufferedInputStream buffi = new BufferedInputStream(file_decompress); BufferedInputStream buffi = new BufferedInputStream(file_decompress);
/* Open the file with the buffer */ /* Open the file with the buffer */
ZipArchiveInputStream zis = new ZipArchiveInputStream(buffi); ZipArchiveInputStream zis = new ZipArchiveInputStream(buffi);
) {
/* Processing entries of the zip file */ /* Processing entries of the zip file */
ArchiveEntry entree; ArchiveEntry entree;
while ((entree = zis.getNextEntry()) != null) { while ((entree = zis.getNextEntry()) != null) {
/* Create a array for the current entry */ /* Create a array for the current entry */
ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
IOUtils.copy(zis, byteArray); IOUtils.copy(zis, byteArray);
zipContent.put(entree.getName(), byteArray); zipContent.put(entree.getName(), byteArray);
} }
zis.close(); }
return zipContent; return zipContent;
} }
/** /**
* Asserts that two files are equal. Throws an <tt>AssertionFailedError</tt> * Asserts that two files are equal. Throws an <tt>AssertionFailedError</tt>
* if they are not. * if they are not.
* <p> * <p>
* *
*/ */
public static void assertEquals(File expected, File actual) { public static void assertEquals(File expected, File actual) {
assertNotNull(expected); assertNotNull(expected);
assertNotNull(actual); assertNotNull(actual);
assertTrue("File does not exist [" + expected.getAbsolutePath() assertTrue("File does not exist [" + expected.getAbsolutePath()
+ "]", expected.exists()); + "]", expected.exists());
assertTrue("File does not exist [" + actual.getAbsolutePath() assertTrue("File does not exist [" + actual.getAbsolutePath()
+ "]", actual.exists()); + "]", actual.exists());
assertTrue("Expected file not readable", expected.canRead()); assertTrue("Expected file not readable", expected.canRead());
assertTrue("Actual file not readable", actual.canRead()); assertTrue("Actual file not readable", actual.canRead());
try { try {
TreeMap<String, ByteArrayOutputStream> file1 = decompress(expected); TreeMap<String, ByteArrayOutputStream> file1 = decompress(expected);
TreeMap<String, ByteArrayOutputStream> file2 = decompress(actual); TreeMap<String, ByteArrayOutputStream> file2 = decompress(actual);
equals(file1, file2); equals(file1, file2);
} catch (IOException e) { } catch (IOException e) {
throw new AssertionFailedError(e.toString()); throw new AssertionFailedError(e.toString());
} }
} }
private static class IgnoreXMLDeclEvaluator implements DifferenceEvaluator { private static class IgnoreXMLDeclEvaluator implements DifferenceEvaluator {
public ComparisonResult evaluate(final Comparison comparison, final ComparisonResult outcome) { @Override
if (outcome != ComparisonResult.EQUAL) { public ComparisonResult evaluate(final Comparison comparison, final ComparisonResult outcome) {
// only evaluate differences if (outcome != ComparisonResult.EQUAL) {
switch (comparison.getType()) { // only evaluate differences
case CHILD_NODELIST_SEQUENCE: switch (comparison.getType()) {
case XML_STANDALONE: case CHILD_NODELIST_SEQUENCE:
case NAMESPACE_PREFIX: case XML_STANDALONE:
return ComparisonResult.SIMILAR; case NAMESPACE_PREFIX:
case TEXT_VALUE: return ComparisonResult.SIMILAR;
switch (comparison.getControlDetails().getTarget().getParentNode().getNodeName()) { case TEXT_VALUE:
case "dcterms:created": switch (comparison.getControlDetails().getTarget().getParentNode().getNodeName()) {
case "dc:creator": case "dcterms:created":
return ComparisonResult.SIMILAR; case "dc:creator":
} return ComparisonResult.SIMILAR;
break; }
default: break;
break; default:
} break;
} }
}
return outcome; return outcome;
} }
} }
} }

View File

@ -81,8 +81,8 @@ public class TestXSLFTextParagraph {
DrawTextParagraphProxy dtp = new DrawTextParagraphProxy(p); DrawTextParagraphProxy dtp = new DrawTextParagraphProxy(p);
Double leftInset = sh.getLeftInset(); double leftInset = sh.getLeftInset();
Double rightInset = sh.getRightInset(); double rightInset = sh.getRightInset();
assertEquals(7.2, leftInset, 0); assertEquals(7.2, leftInset, 0);
assertEquals(7.2, rightInset, 0); assertEquals(7.2, rightInset, 0);

View File

@ -28,7 +28,6 @@ import java.io.InputStream;
import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.TempFile; import org.apache.poi.util.TempFile;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
@ -80,7 +79,9 @@ public class XSSFTestDataSamples {
} }
private static <R extends Workbook> void writeOut(R wb, File file) throws IOException { private static <R extends Workbook> void writeOut(R wb, File file) throws IOException {
IOUtils.write(wb, new FileOutputStream(file)); try (FileOutputStream out = new FileOutputStream(file)) {
wb.write(out);
}
} }
// Anticipates the location of where a workbook will be written to // Anticipates the location of where a workbook will be written to
@ -213,7 +214,7 @@ public class XSSFTestDataSamples {
* Read back a workbook that was written out to a memory buffer with * Read back a workbook that was written out to a memory buffer with
* {@link #writeOut(Workbook)} or {@link #writeOutAndClose(Workbook)}. * {@link #writeOut(Workbook)} or {@link #writeOutAndClose(Workbook)}.
* *
* @param file the workbook file to read * @param out the output stream to read back from
* @return the read back workbook * @return the read back workbook
* @throws IOException * @throws IOException
*/ */

View File

@ -47,15 +47,15 @@ import java.util.TreeMap;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile; import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.poi.POIDataSamples; import org.apache.poi.POIDataSamples;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.ooxml.POIXMLDocumentPart.RelationPart;
import org.apache.poi.ooxml.POIXMLException;
import org.apache.poi.ooxml.POIXMLProperties;
import org.apache.poi.common.usermodel.HyperlinkType; import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.hssf.HSSFITestDataProvider; import org.apache.poi.hssf.HSSFITestDataProvider;
import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator; import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.ooxml.POIXMLDocumentPart.RelationPart;
import org.apache.poi.ooxml.POIXMLException;
import org.apache.poi.ooxml.POIXMLProperties;
import org.apache.poi.ooxml.util.DocumentHelper; import org.apache.poi.ooxml.util.DocumentHelper;
import org.apache.poi.ooxml.util.SAXHelper; import org.apache.poi.ooxml.util.SAXHelper;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
@ -82,7 +82,31 @@ import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.NumberEval; import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.functions.Function; import org.apache.poi.ss.formula.functions.Function;
import org.apache.poi.ss.formula.ptg.Ptg; import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.FormulaError;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.usermodel.PrintSetup;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.SheetConditionalFormatting;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.AreaReference; import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellAddress; import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
@ -2261,7 +2285,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
* problems when deleting columns, conditionally to stop recursion * problems when deleting columns, conditionally to stop recursion
*/ */
private static final String FORMULA1 = private static final String FORMULA1 =
"IF( INDIRECT( ADDRESS( ROW(), COLUMN()-1 ) ) = 0, 0," "IF( INDIRECT( ADDRESS( ROW(), COLUMN()-1 ) ) = 0, 0, "
+ "INDIRECT( ADDRESS( ROW(), COLUMN()-1 ) ) ) + 2"; + "INDIRECT( ADDRESS( ROW(), COLUMN()-1 ) ) ) + 2";
/** /**
@ -2269,7 +2293,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
* problems when deleting rows, conditionally to stop recursion * problems when deleting rows, conditionally to stop recursion
*/ */
private static final String FORMULA2 = private static final String FORMULA2 =
"IF( INDIRECT( ADDRESS( ROW()-1, COLUMN() ) ) = 0, 0," "IF( INDIRECT( ADDRESS( ROW()-1, COLUMN() ) ) = 0, 0, "
+ "INDIRECT( ADDRESS( ROW()-1, COLUMN() ) ) ) + 2"; + "INDIRECT( ADDRESS( ROW()-1, COLUMN() ) ) ) + 2";
/** /**

View File

@ -243,9 +243,9 @@ public class TestXSSFColGrouping {
sheet = wb2.getSheet("test"); sheet = wb2.getSheet("test");
for (int i = 2; i <= 4; i++) { for (int i = 2; i <= 4; i++) {
assertEquals("Unexpected width of column "+ i, 20 * 256, sheet.getColumnWidth(i)); assertEquals("Unexpected width of column "+ i, 20 * 256L, sheet.getColumnWidth(i));
} }
assertEquals("Unexpected width of column "+ 5, sheet.getDefaultColumnWidth() * 256, sheet.getColumnWidth(5)); assertEquals("Unexpected width of column "+ 5, sheet.getDefaultColumnWidth() * 256L, sheet.getColumnWidth(5));
wb2.close(); wb2.close();
wb1.close(); wb1.close();
@ -292,9 +292,9 @@ public class TestXSSFColGrouping {
sheet = wb2.getSheet("test"); sheet = wb2.getSheet("test");
for (int i = 2; i <= 4; i++) { for (int i = 2; i <= 4; i++) {
assertEquals("Unexpected width of column "+ i, 20 * 256, sheet.getColumnWidth(i)); assertEquals("Unexpected width of column "+ i, 20 * 256L, sheet.getColumnWidth(i));
} }
assertEquals("Unexpected width of column "+ 1, sheet.getDefaultColumnWidth() * 256, sheet.getColumnWidth(1)); assertEquals("Unexpected width of column "+ 1, sheet.getDefaultColumnWidth() * 256L, sheet.getColumnWidth(1));
wb2.close(); wb2.close();
wb1.close(); wb1.close();

View File

@ -23,7 +23,6 @@ import java.math.BigDecimal;
import java.util.List; import java.util.List;
import org.apache.poi.ss.formula.DataValidationEvaluator; import org.apache.poi.ss.formula.DataValidationEvaluator;
import org.apache.poi.ss.formula.WorkbookEvaluator;
import org.apache.poi.ss.formula.eval.ValueEval; import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.usermodel.BaseTestDataValidation; import org.apache.poi.ss.usermodel.BaseTestDataValidation;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
@ -140,7 +139,7 @@ public class TestXSSFDataValidation extends BaseTestDataValidation {
cell_10.setCellValue(XSSFDataValidation.operatorTypeMappings.get(operatorType).toString()); cell_10.setCellValue(XSSFDataValidation.operatorTypeMappings.get(operatorType).toString());
Cell cell_11 = row1.createCell(1); Cell cell_11 = row1.createCell(1);
Cell cell_21 = row1.createCell(2); Cell cell_21 = row1.createCell(2);
Cell cell_22 = i==0 && j < 2 ? row2.createCell(2) : null; Cell cell_22 = i==0 && j < 2 ? (row2 == null ? null : row2.createCell(2)) : null;
Cell cell_13 = row1.createCell(3); Cell cell_13 = row1.createCell(3);
@ -170,7 +169,9 @@ public class TestXSSFDataValidation extends BaseTestDataValidation {
assertEquals(++lastKnownNumValidations, ((XSSFSheet) sheet).getDataValidations().size()); assertEquals(++lastKnownNumValidations, ((XSSFSheet) sheet).getDataValidations().size());
cellRangeAddressList = new CellRangeAddressList(); cellRangeAddressList = new CellRangeAddressList();
cellRangeAddressList.addCellRangeAddress(new CellRangeAddress(cell_22.getRowIndex(), cell_22.getRowIndex(), cell_22.getColumnIndex(), cell_22.getColumnIndex())); if (cell_22 != null) {
cellRangeAddressList.addCellRangeAddress(new CellRangeAddress(cell_22.getRowIndex(), cell_22.getRowIndex(), cell_22.getColumnIndex(), cell_22.getColumnIndex()));
}
validation = dataValidationHelper.createValidation(constraint, cellRangeAddressList); validation = dataValidationHelper.createValidation(constraint, cellRangeAddressList);
setOtherValidationParameters( validation); setOtherValidationParameters( validation);
sheet.addValidationData(validation); sheet.addValidationData(validation);
@ -178,7 +179,9 @@ public class TestXSSFDataValidation extends BaseTestDataValidation {
} else if(i==0 && j==1 ){ } else if(i==0 && j==1 ){
cellRangeAddressList = new CellRangeAddressList(); cellRangeAddressList = new CellRangeAddressList();
cellRangeAddressList.addCellRangeAddress(new CellRangeAddress(cell_21.getRowIndex(), cell_21.getRowIndex(), cell_21.getColumnIndex(), cell_21.getColumnIndex())); cellRangeAddressList.addCellRangeAddress(new CellRangeAddress(cell_21.getRowIndex(), cell_21.getRowIndex(), cell_21.getColumnIndex(), cell_21.getColumnIndex()));
cellRangeAddressList.addCellRangeAddress(new CellRangeAddress(cell_22.getRowIndex(), cell_22.getRowIndex(), cell_22.getColumnIndex(), cell_22.getColumnIndex())); if (cell_22 != null) {
cellRangeAddressList.addCellRangeAddress(new CellRangeAddress(cell_22.getRowIndex(), cell_22.getRowIndex(), cell_22.getColumnIndex(), cell_22.getColumnIndex()));
}
validation = dataValidationHelper.createValidation(constraint, cellRangeAddressList); validation = dataValidationHelper.createValidation(constraint, cellRangeAddressList);
setOtherValidationParameters( validation); setOtherValidationParameters( validation);
sheet.addValidationData(validation); sheet.addValidationData(validation);

View File

@ -36,8 +36,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.apache.poi.ooxml.POIXMLException;
import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.ooxml.POIXMLException;
import org.apache.poi.poifs.crypt.CryptoFunctions; import org.apache.poi.poifs.crypt.CryptoFunctions;
import org.apache.poi.poifs.crypt.HashAlgorithm; import org.apache.poi.poifs.crypt.HashAlgorithm;
import org.apache.poi.ss.usermodel.AutoFilter; import org.apache.poi.ss.usermodel.AutoFilter;
@ -801,7 +801,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
//now the span is splitted into 5 individual columns //now the span is splitted into 5 individual columns
assertEquals(5, cols.sizeOfColArray()); assertEquals(5, cols.sizeOfColArray());
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
assertEquals(cw[i]*256, sheet.getColumnWidth(i)); assertEquals(cw[i]*256L, sheet.getColumnWidth(i));
assertEquals(cw[i], cols.getColArray(i).getWidth(), 0.0); assertEquals(cw[i], cols.getColArray(i).getWidth(), 0.0);
} }
@ -812,7 +812,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
cols = sheet.getCTWorksheet().getColsArray(0); cols = sheet.getCTWorksheet().getColsArray(0);
assertEquals(5, cols.sizeOfColArray()); assertEquals(5, cols.sizeOfColArray());
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
assertEquals(cw[i]*256, sheet.getColumnWidth(i)); assertEquals(cw[i]*256L, sheet.getColumnWidth(i));
assertEquals(cw[i], cols.getColArray(i).getWidth(), 0.0); assertEquals(cw[i], cols.getColArray(i).getWidth(), 0.0);
} }
@ -1595,8 +1595,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
final CellCopyPolicy defaultCopyPolicy = new CellCopyPolicy(); final CellCopyPolicy defaultCopyPolicy = new CellCopyPolicy();
sheet.copyRows(0, 3, 8, defaultCopyPolicy); sheet.copyRows(0, 3, 8, defaultCopyPolicy);
@SuppressWarnings("unused") sheet.getRow(0);
final Row srcHeaderRow = sheet.getRow(0);
final Row srcRow1 = sheet.getRow(1); final Row srcRow1 = sheet.getRow(1);
final Row srcRow2 = sheet.getRow(2); final Row srcRow2 = sheet.getRow(2);
final Row srcRow3 = sheet.getRow(3); final Row srcRow3 = sheet.getRow(3);

View File

@ -1044,8 +1044,10 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook {
//assertCloseDoesNotModifyFile(filename, wb); //assertCloseDoesNotModifyFile(filename, wb);
// InputStream // InputStream
wb = new XSSFWorkbook(new FileInputStream(file)); try (FileInputStream is = new FileInputStream(file)) {
assertCloseDoesNotModifyFile(filename, wb); wb = new XSSFWorkbook(is);
assertCloseDoesNotModifyFile(filename, wb);
}
// OPCPackage // OPCPackage
//wb = new XSSFWorkbook(OPCPackage.open(file)); //wb = new XSSFWorkbook(OPCPackage.open(file));

View File

@ -65,7 +65,7 @@ public class TestXSSFChartTitle {
row = sheet.createRow((short) rowIndex); row = sheet.createRow((short) rowIndex);
for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) { for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
cell = row.createCell((short) colIndex); cell = row.createCell((short) colIndex);
cell.setCellValue(colIndex * (rowIndex + 1)); cell.setCellValue(colIndex * (rowIndex + 1L));
} }
} }

View File

@ -17,18 +17,23 @@
package org.apache.poi.xssf.util; package org.apache.poi.xssf.util;
import junit.framework.TestCase;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.util.CellReference;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetData;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType;
import junit.framework.TestCase;
/** /**
* Mixed utilities for testing memory usage in XSSF * Mixed utilities for testing memory usage in XSSF
@ -62,8 +67,11 @@ public class MemoryUsage extends TestCase {
Row row = sh.createRow(i); Row row = sh.createRow(i);
for(int j=0; j < numCols; j++){ for(int j=0; j < numCols; j++){
Cell cell = row.createCell(j); Cell cell = row.createCell(j);
if(j % 2 == 0) cell.setCellValue(j); if(j % 2 == 0) {
else cell.setCellValue(new CellReference(j, i).formatAsString()); cell.setCellValue(j);
} else {
cell.setCellValue(new CellReference(j, i).formatAsString());
}
cnt++; cnt++;
} }
} }
@ -161,7 +169,7 @@ public class MemoryUsage extends TestCase {
rows.add(r); rows.add(r);
} }
} catch (OutOfMemoryError er) { } catch (OutOfMemoryError er) {
System.out.println("Failed at row=" + i); System.out.println("Failed at row=" + i + " from " + rows.size() + " kept.");
} catch (final Exception e) { } catch (final Exception e) {
System.out.println("Unable to reach an OutOfMemoryError"); System.out.println("Unable to reach an OutOfMemoryError");
System.out.println(e.getClass().getName() + ": " + e.getMessage()); System.out.println(e.getClass().getName() + ": " + e.getMessage());
@ -190,7 +198,7 @@ public class MemoryUsage extends TestCase {
rows.add(r); rows.add(r);
} }
} catch (OutOfMemoryError er) { } catch (OutOfMemoryError er) {
System.out.println("Failed at row=" + i); System.out.println("Failed at row=" + i + " from " + rows.size() + " kept.");
} catch (final Exception e) { } catch (final Exception e) {
System.out.println("Unable to reach an OutOfMemoryError"); System.out.println("Unable to reach an OutOfMemoryError");
System.out.println(e.getClass().getName() + ": " + e.getMessage()); System.out.println(e.getClass().getName() + ": " + e.getMessage());