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;
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.StringUtil;
import org.apache.poi.util.SuppressForbidden;
@ -29,19 +47,7 @@ import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.reflections.Reflections;
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.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Pattern;
import junit.framework.TestCase;
/**
* 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++) {
switch (args[i]) {
case "-dest":
dest = args[++i];
dest = args[++i]; // lgtm[java/index-out-of-bounds]
break;
case "-test":
test = args[++i];
test = args[++i]; // lgtm[java/index-out-of-bounds]
break;
case "-ooxml":
ooxml = args[++i];
ooxml = args[++i]; // lgtm[java/index-out-of-bounds]
break;
}
}
@ -286,8 +292,12 @@ public final class OOXMLLite {
String path = arg.getAbsolutePath();
String prefix = root.getAbsolutePath();
String cls = path.substring(prefix.length() + 1).replace(File.separator, ".");
if(!cls.matches(ptrn)) return;
if (cls.matches(exclude)) return;
if(!cls.matches(ptrn)) {
return;
}
if (cls.matches(exclude)) {
return;
}
//ignore inner classes defined in tests
if (cls.indexOf('$') != -1) {
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
// without failing, see https://issues.apache.org/bugzilla/show_bug.cgi?id=56550
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)")
public Field run() {
try {
@ -339,11 +350,17 @@ public final class OOXMLLite {
for (Class<?> cls : classes) {
// e.g. proxy-classes, ...
ProtectionDomain pd = cls.getProtectionDomain();
if (pd == null) continue;
if (pd == null) {
continue;
}
CodeSource cs = pd.getCodeSource();
if (cs == null) continue;
if (cs == null) {
continue;
}
URL loc = cs.getLocation();
if (loc == null) continue;
if (loc == null) {
continue;
}
String jar = loc.toString();
if (jar.contains(ptrn)) {

View File

@ -30,7 +30,6 @@ import java.io.IOException;
import java.util.Set;
import java.util.TreeMap;
import junit.framework.AssertionFailedError;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.poi.util.IOUtils;
@ -44,6 +43,8 @@ import org.xmlunit.diff.Diff;
import org.xmlunit.diff.DifferenceEvaluator;
import org.xmlunit.diff.ElementSelectors;
import junit.framework.AssertionFailedError;
/**
* Compare the contents of 2 zip files.
*/
@ -90,6 +91,7 @@ public final class ZipFileAssert {
// let s assume it is not Go ;-)
TreeMap<String, ByteArrayOutputStream> zipContent = new TreeMap<>();
try (
/* Open file to decompress */
FileInputStream file_decompress = new FileInputStream(filename);
@ -98,6 +100,7 @@ public final class ZipFileAssert {
/* Open the file with the buffer */
ZipArchiveInputStream zis = new ZipArchiveInputStream(buffi);
) {
/* Processing entries of the zip file */
ArchiveEntry entree;
@ -109,7 +112,7 @@ public final class ZipFileAssert {
zipContent.put(entree.getName(), byteArray);
}
zis.close();
}
return zipContent;
}
@ -142,6 +145,7 @@ public final class ZipFileAssert {
}
private static class IgnoreXMLDeclEvaluator implements DifferenceEvaluator {
@Override
public ComparisonResult evaluate(final Comparison comparison, final ComparisonResult outcome) {
if (outcome != ComparisonResult.EQUAL) {
// only evaluate differences

View File

@ -81,8 +81,8 @@ public class TestXSLFTextParagraph {
DrawTextParagraphProxy dtp = new DrawTextParagraphProxy(p);
Double leftInset = sh.getLeftInset();
Double rightInset = sh.getRightInset();
double leftInset = sh.getLeftInset();
double rightInset = sh.getRightInset();
assertEquals(7.2, leftInset, 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.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.TempFile;
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 {
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
@ -213,7 +214,7 @@ public class XSSFTestDataSamples {
* Read back a workbook that was written out to a memory buffer with
* {@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
* @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.ZipFile;
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.hssf.HSSFITestDataProvider;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
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.SAXHelper;
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.functions.Function;
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.CellAddress;
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
*/
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";
/**
@ -2269,7 +2293,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
* problems when deleting rows, conditionally to stop recursion
*/
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";
/**

View File

@ -243,9 +243,9 @@ public class TestXSSFColGrouping {
sheet = wb2.getSheet("test");
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();
wb1.close();
@ -292,9 +292,9 @@ public class TestXSSFColGrouping {
sheet = wb2.getSheet("test");
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();
wb1.close();

View File

@ -23,7 +23,6 @@ import java.math.BigDecimal;
import java.util.List;
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.usermodel.BaseTestDataValidation;
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 cell_11 = row1.createCell(1);
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);
@ -170,7 +169,9 @@ public class TestXSSFDataValidation extends BaseTestDataValidation {
assertEquals(++lastKnownNumValidations, ((XSSFSheet) sheet).getDataValidations().size());
cellRangeAddressList = new CellRangeAddressList();
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);
setOtherValidationParameters( validation);
sheet.addValidationData(validation);
@ -178,7 +179,9 @@ public class TestXSSFDataValidation extends BaseTestDataValidation {
} else if(i==0 && j==1 ){
cellRangeAddressList = new CellRangeAddressList();
cellRangeAddressList.addCellRangeAddress(new CellRangeAddress(cell_21.getRowIndex(), cell_21.getRowIndex(), cell_21.getColumnIndex(), cell_21.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);
setOtherValidationParameters( validation);
sheet.addValidationData(validation);

View File

@ -36,8 +36,8 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.poi.ooxml.POIXMLException;
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.HashAlgorithm;
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
assertEquals(5, cols.sizeOfColArray());
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);
}
@ -812,7 +812,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
cols = sheet.getCTWorksheet().getColsArray(0);
assertEquals(5, cols.sizeOfColArray());
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);
}
@ -1595,8 +1595,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
final CellCopyPolicy defaultCopyPolicy = new CellCopyPolicy();
sheet.copyRows(0, 3, 8, defaultCopyPolicy);
@SuppressWarnings("unused")
final Row srcHeaderRow = sheet.getRow(0);
sheet.getRow(0);
final Row srcRow1 = sheet.getRow(1);
final Row srcRow2 = sheet.getRow(2);
final Row srcRow3 = sheet.getRow(3);

View File

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

View File

@ -65,7 +65,7 @@ public class TestXSSFChartTitle {
row = sheet.createRow((short) rowIndex);
for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; 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;
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.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
@ -62,8 +67,11 @@ public class MemoryUsage extends TestCase {
Row row = sh.createRow(i);
for(int j=0; j < numCols; j++){
Cell cell = row.createCell(j);
if(j % 2 == 0) cell.setCellValue(j);
else cell.setCellValue(new CellReference(j, i).formatAsString());
if(j % 2 == 0) {
cell.setCellValue(j);
} else {
cell.setCellValue(new CellReference(j, i).formatAsString());
}
cnt++;
}
}
@ -161,7 +169,7 @@ public class MemoryUsage extends TestCase {
rows.add(r);
}
} 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) {
System.out.println("Unable to reach an OutOfMemoryError");
System.out.println(e.getClass().getName() + ": " + e.getMessage());
@ -190,7 +198,7 @@ public class MemoryUsage extends TestCase {
rows.add(r);
}
} 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) {
System.out.println("Unable to reach an OutOfMemoryError");
System.out.println(e.getClass().getName() + ": " + e.getMessage());