Bug 57355: Fix ArrayIndexOutOfBoundsException by removing two unnecessary lines in XSSFSheet.getColumnWidthInPixels()

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1645900 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2014-12-16 10:40:02 +00:00
parent 93372f4468
commit 51308cd422
4 changed files with 71 additions and 18 deletions

View File

@ -714,8 +714,6 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
*/
@Override
public float getColumnWidthInPixels(int columnIndex) {
int styleIdx = getColumnHelper().getColDefaultStyle(columnIndex);
CellStyle cs = getWorkbook().getStylesSource().getStyleAt(styleIdx);
float widthIn256 = getColumnWidth(columnIndex);
return (float)(widthIn256/256.0*XSSFWorkbook.DEFAULT_CHARACTER_WIDTH);
}

View File

@ -17,25 +17,15 @@
package org.apache.poi.xssf.usermodel;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFPicture;
import org.apache.poi.hssf.usermodel.HSSFShape;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.BaseTestPicture;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.xssf.XSSFITestDataProvider;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.junit.Ignore;
import org.junit.Test;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTTwoCellAnchor;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.STEditAs;
@ -169,5 +159,4 @@ public final class TestXSSFPicture extends BaseTestPicture {
wb.close();
}
}

View File

@ -20,8 +20,6 @@ package org.apache.poi.hssf.usermodel;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
@ -36,7 +34,6 @@ import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;
import org.junit.Ignore;
/**
* Test <code>HSSFPicture</code>.

View File

@ -18,13 +18,21 @@
package org.apache.poi.ss.usermodel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.awt.Dimension;
import java.io.FileOutputStream;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.poi.ss.ITestDataProvider;
import org.apache.poi.ss.util.ImageUtils;
import org.apache.poi.util.Units;
import org.apache.poi.xssf.XSSFITestDataProvider;
import org.junit.Test;
/**
* @author Yegor Kozlov
@ -65,4 +73,65 @@ public abstract class BaseTestPicture {
assertEquals("the image width differs", imgDim.getWidth(), inpDim.getWidth()/emuPX, 1);
}
@Test
public void testResizeNoColumns() throws IOException {
Workbook wb = _testDataProvider.createWorkbook();
try {
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(0);
handleResize(wb, sheet, row);
} finally {
wb.close();
}
}
@Test
public void testResizeWithColumns() throws IOException {
Workbook wb = _testDataProvider.createWorkbook();
try {
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(0);
row.createCell(0);
handleResize(wb, sheet, row);
} finally {
wb.close();
}
}
private void handleResize(Workbook wb, Sheet sheet, Row row) throws IOException {
Drawing drawing = sheet.createDrawingPatriarch();
CreationHelper createHelper = wb.getCreationHelper();
final byte[] bytes = XSSFITestDataProvider.instance.getTestDataFileContent("logoKarmokar4.png");
row.setHeightInPoints(getImageSize(bytes).y);
int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
//add a picture shape
ClientAnchor anchor = createHelper.createClientAnchor();
//set top-left corner of the picture,
//subsequent call of Picture#resize() will operate relative to it
anchor.setCol1(0);
anchor.setRow1(0);
Picture pict = drawing.createPicture(anchor, pictureIdx);
//auto-size picture relative to its top-left corner
pict.resize();
}
private static Point getImageSize( byte [] image) throws IOException {
BufferedImage img = ImageIO.read(new ByteArrayInputStream(image));
assertNotNull(img);
return new Point(img.getWidth(), img.getHeight());
}
}