*/
package org.apache.poi.xslf.usermodel;
+import java.io.IOException;
import java.io.OutputStream;
import java.util.LinkedHashMap;
import java.util.Map;
-import junit.framework.TestCase;
-
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
-import org.openxmlformats.schemas.drawingml.x2006.chart.*;
-
-/**
- * @author Yegor Kozlov
- */
-public class TestXSLFChart extends TestCase {
+import org.junit.Test;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTChart;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumData;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumVal;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTPieChart;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTPieSer;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTSerTx;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrData;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrVal;
+
+public class TestXSLFChart {
/**
* a modified version from POI-examples
*/
- public void testFillChartTemplate() throws Exception {
+ @Test
+ public void testFillChartTemplate() throws IOException {
String chartTitle = "Apache POI"; // first line is chart title
OutputStream xlsOut = xlsPart.getPackagePart().getOutputStream();
wb.write(xlsOut);
xlsOut.close();
-
+ wb.close();
}
}
\ No newline at end of file
*/
package org.apache.poi.xslf.usermodel;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
import org.junit.Test;
/**
* test common operations on containers of shapes (sheets and groups of shapes)
- *
- * @author Yegor Kozlov
*/
public class TestXSLFShapeContainer {
- @SuppressWarnings("unused")
public void verifyContainer(XSLFShapeContainer container) {
container.clear();
assertEquals(0, container.getShapes().size());
- XSLFGroupShape shape1 = container.createGroup();
+ container.createGroup();
assertEquals(1, container.getShapes().size());
- XSLFTextBox shape2 = container.createTextBox();
+ container.createTextBox();
assertEquals(2, container.getShapes().size());
- XSLFAutoShape shape3 = container.createAutoShape();
+ container.createAutoShape();
assertEquals(3, container.getShapes().size());
- XSLFConnectorShape shape4 = container.createConnector();
+ container.createConnector();
assertEquals(4, container.getShapes().size());
container.clear();
}
@Test
- public void testSheet() {
+ public void testSheet() throws IOException {
XMLSlideShow ppt = new XMLSlideShow();
XSLFSheet sheet = ppt.createSlide();
verifyContainer(sheet);
XSLFGroupShape group = sheet.createGroup();
verifyContainer(group);
+ ppt.close();
}
}
\ No newline at end of file
package org.apache.poi.xssf.usermodel;
-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.assertTrue;
+
+import java.io.IOException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
+import org.junit.Test;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDialogsheet;
-public class TestXSSFDialogSheet extends TestCase {
-
+public class TestXSSFDialogSheet {
- public void testCreateDialogSheet() {
+ @Test
+ public void testCreateDialogSheet() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet dialogsheet = workbook.createDialogsheet("Dialogsheet 1", CTDialogsheet.Factory.newInstance());
assertNotNull(dialogsheet);
+ workbook.close();
}
-
- public void testGetDialog() {
+
+ @Test
+ public void testGetDialog() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFDialogsheet dialogsheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertTrue(dialogsheet.getDialog());
-
+ workbook.close();
}
-
- public void testAddRow() {
+
+ @Test
+ public void testAddRow() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet dialogsheet = workbook.createDialogsheet("Dialogsheet 1", CTDialogsheet.Factory.newInstance());
assertNotNull(dialogsheet);
Row row = dialogsheet.createRow(0);
assertNull(row);
+ workbook.close();
}
-
- public void testGetSetAutoBreaks() {
+
+ @Test
+ public void testGetSetAutoBreaks() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFDialogsheet sheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertTrue(sheet.getAutobreaks());
sheet.setAutobreaks(false);
assertFalse(sheet.getAutobreaks());
+ workbook.close();
}
-
- public void testIsSetFitToPage() {
+
+ @Test
+ public void testIsSetFitToPage() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFDialogsheet sheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertFalse(sheet.getFitToPage());
assertTrue(sheet.getFitToPage());
sheet.setFitToPage(false);
assertFalse(sheet.getFitToPage());
+ workbook.close();
}
-
-
- public void testGetFooter() {
+
+
+ @Test
+ public void testGetFooter() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFDialogsheet sheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertNotNull(sheet.getFooter());
sheet.getFooter().setCenter("test center footer");
assertEquals("test center footer", sheet.getFooter().getCenter());
+ workbook.close();
}
-
- public void testGetAllHeadersFooters() {
+
+ @Test
+ public void testGetAllHeadersFooters() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFDialogsheet sheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertNotNull(sheet);
assertNotNull(sheet.getOddHeader());
assertNotNull(sheet.getEvenHeader());
assertNotNull(sheet.getFirstHeader());
-
+
assertEquals("", sheet.getOddFooter().getLeft());
sheet.getOddFooter().setLeft("odd footer left");
assertEquals("odd footer left", sheet.getOddFooter().getLeft());
-
+
assertEquals("", sheet.getEvenFooter().getLeft());
sheet.getEvenFooter().setLeft("even footer left");
assertEquals("even footer left", sheet.getEvenFooter().getLeft());
-
+
assertEquals("", sheet.getFirstFooter().getLeft());
sheet.getFirstFooter().setLeft("first footer left");
assertEquals("first footer left", sheet.getFirstFooter().getLeft());
-
+
assertEquals("", sheet.getOddHeader().getLeft());
sheet.getOddHeader().setLeft("odd header left");
assertEquals("odd header left", sheet.getOddHeader().getLeft());
-
+
assertEquals("", sheet.getOddHeader().getRight());
sheet.getOddHeader().setRight("odd header right");
assertEquals("odd header right", sheet.getOddHeader().getRight());
-
+
assertEquals("", sheet.getOddHeader().getCenter());
sheet.getOddHeader().setCenter("odd header center");
assertEquals("odd header center", sheet.getOddHeader().getCenter());
-
+ workbook.close();
}
-
- public void testGetSetHorizontallyCentered() {
+
+ @Test
+ public void testGetSetHorizontallyCentered() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFDialogsheet sheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertFalse(sheet.getHorizontallyCenter());
assertTrue(sheet.getHorizontallyCenter());
sheet.setHorizontallyCenter(false);
assertFalse(sheet.getHorizontallyCenter());
+ workbook.close();
}
-
- public void testGetSetVerticallyCentered() {
+
+ @Test
+ public void testGetSetVerticallyCentered() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFDialogsheet sheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertFalse(sheet.getVerticallyCenter());
assertTrue(sheet.getVerticallyCenter());
sheet.setVerticallyCenter(false);
assertFalse(sheet.getVerticallyCenter());
+ workbook.close();
}
-
- public void testIsSetPrintGridlines() {
+
+ @Test
+ public void testIsSetPrintGridlines() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFDialogsheet sheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertFalse(sheet.isPrintGridlines());
sheet.setPrintGridlines(true);
assertTrue(sheet.isPrintGridlines());
+ workbook.close();
}
-
- public void testIsSetDisplayFormulas() {
+
+ @Test
+ public void testIsSetDisplayFormulas() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFDialogsheet sheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertFalse(sheet.isDisplayFormulas());
sheet.setDisplayFormulas(true);
assertTrue(sheet.isDisplayFormulas());
+ workbook.close();
}
-
- public void testIsSetDisplayGridLines() {
+
+ @Test
+ public void testIsSetDisplayGridLines() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFDialogsheet sheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertTrue(sheet.isDisplayGridlines());
sheet.setDisplayGridlines(false);
assertFalse(sheet.isDisplayGridlines());
+ workbook.close();
}
-
- public void testIsSetDisplayRowColHeadings() {
+
+ @Test
+ public void testIsSetDisplayRowColHeadings() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFDialogsheet sheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertTrue(sheet.isDisplayRowColHeadings());
sheet.setDisplayRowColHeadings(false);
assertFalse(sheet.isDisplayRowColHeadings());
+ workbook.close();
}
-
- public void testGetScenarioProtect() {
+
+ @Test
+ public void testGetScenarioProtect() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFDialogsheet sheet = workbook.createDialogsheet("Dialogsheet 1", null);
assertFalse(sheet.getScenarioProtect());
+ workbook.close();
}
-
+
}
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
-import org.junit.Test;
+import java.io.IOException;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.junit.Test;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
}
@Test
- public void testGetOrCreateColumn() {
+ public void testGetOrCreateColumn() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Sheet 1");
ColumnHelper columnHelper = sheet.getColumnHelper();
assertNotNull(columnHelper.getColumn(29, false));
assertNotNull(columnHelper.getColumn1Based(30, false));
assertNull(columnHelper.getColumn(30, false));
+
+ workbook.close();
}
@Test
- public void testGetSetColDefaultStyle() {
+ public void testGetSetColDefaultStyle() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
CTWorksheet ctWorksheet = sheet.getCTWorksheet();
columnHelper.setColDefaultStyle(11, cellStyle);
assertEquals(0, col_2.getStyle());
assertEquals(1, columnHelper.getColDefaultStyle(10));
+
+ workbook.close();
}
private static int countColumns(CTWorksheet worksheet) {
import org.apache.poi.hdgf.extractor.VisioTextExtractor;
import org.apache.poi.hpbf.extractor.PublisherTextExtractor;
import org.apache.poi.hslf.extractor.PowerPointExtractor;
+import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hsmf.MAPIMessage;
import org.apache.poi.hsmf.datatypes.AttachmentChunks;
import org.apache.poi.hsmf.extractor.OutlookTextExtactor;
}
}
- if (poifsDir.hasEntry("PowerPoint Document")) {
+ if (poifsDir.hasEntry(HSLFSlideShow.POWERPOINT_DOCUMENT)) {
return new PowerPointExtractor(poifsDir);
}
import java.nio.charset.Charset;
import org.apache.poi.hslf.record.RecordTypes;
+import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.util.IOUtils;
public final class PPTXMLDump {
private static final int HEADER_SIZE = 8; //size of the record header
private static final int PICT_HEADER_SIZE = 25; //size of the picture header
- private static final String PPDOC_ENTRY = "PowerPoint Document";
private static final String PICTURES_ENTRY = "Pictures";
private static final String CR = System.getProperty("line.separator");
public PPTXMLDump(File ppt) throws IOException {
NPOIFSFileSystem fs = new NPOIFSFileSystem(ppt, true);
try {
- docstream = readEntry(fs, PPDOC_ENTRY);
+ docstream = readEntry(fs, HSLFSlideShow.POWERPOINT_DOCUMENT);
pictstream = readEntry(fs, PICTURES_ENTRY);
} finally {
fs.close();
import org.apache.poi.ddf.EscherTextboxRecord;
import org.apache.poi.hslf.record.HSLFEscherRecordFactory;
import org.apache.poi.hslf.record.RecordTypes;
+import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.util.HexDump;
import org.apache.poi.util.IOUtils;
*/
public SlideShowDumper(NPOIFSFileSystem filesystem, PrintStream out) throws IOException {
// Grab the document stream
- InputStream is = filesystem.createDocumentInputStream("PowerPoint Document");
+ InputStream is = filesystem.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT);
docstream = IOUtils.toByteArray(is);
is.close();
this.out = out;
import org.apache.poi.hslf.record.RecordTypes;
import org.apache.poi.hslf.record.TextBytesAtom;
import org.apache.poi.hslf.record.TextCharsAtom;
+import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hslf.usermodel.HSLFTextParagraph;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.util.IOUtils;
fs = poifs;
// Find the PowerPoint bit, and get out the bytes
- InputStream pptIs = fs.createDocumentInputStream("PowerPoint Document");
+ InputStream pptIs = fs.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT);
pptContents = IOUtils.toByteArray(pptIs);
pptIs.close();
}
* understanding DocSlideList and DocNotesList) - handle Slide creation cleaner
*/
public final class HSLFSlideShow implements SlideShow<HSLFShape,HSLFTextParagraph>, Closeable {
+ /** Powerpoint document entry/stream name */
+ public static final String POWERPOINT_DOCUMENT = "PowerPoint Document";
+
enum LoadSavePhase {
INIT, LOADED
}
protected static Map<String,ClassID> getOleMap() {
Map<String,ClassID> olemap = new HashMap<String,ClassID>();
- olemap.put("PowerPoint Document", ClassID.PPT_SHOW);
+ olemap.put(POWERPOINT_DOCUMENT, ClassID.PPT_SHOW);
olemap.put("Workbook", ClassID.EXCEL97); // as per BIFF8 spec
olemap.put("WORKBOOK", ClassID.EXCEL97); // Typically from third party programs
olemap.put("BOOK", ClassID.EXCEL97); // Typically odd Crystal Reports exports
private void readPowerPointStream() throws IOException {
// Get the main document stream
DocumentEntry docProps =
- (DocumentEntry) getDirectory().getEntry("PowerPoint Document");
+ (DocumentEntry) getDirectory().getEntry(HSLFSlideShow.POWERPOINT_DOCUMENT);
// Grab the document stream
int len = docProps.getSize();
- InputStream is = getDirectory().createDocumentInputStream("PowerPoint Document");
+ InputStream is = getDirectory().createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT);
try {
_docstream = IOUtils.toByteArray(is, len);
} finally {
// Write the PPT stream into the POIFS layer
ByteArrayInputStream bais = new ByteArrayInputStream(_docstream);
- outFS.createOrUpdateDocument(bais, "PowerPoint Document");
- writtenEntries.add("PowerPoint Document");
+ outFS.createOrUpdateDocument(bais, HSLFSlideShow.POWERPOINT_DOCUMENT);
+ writtenEntries.add(HSLFSlideShow.POWERPOINT_DOCUMENT);
currentUser.setEncrypted(encryptedSS.getDocumentEncryptionAtom() != null);
currentUser.writeToFS(outFS);
// Check all of them in turn
for (POIFSFileSystem npf : new POIFSFileSystem[] { npfS, npfF, npfRF }) {
// Check that the "PowerPoint Document" sections have the same size
- DocumentEntry oProps = (DocumentEntry)pfs.getRoot().getEntry("PowerPoint Document");
- DocumentEntry nProps = (DocumentEntry)npf.getRoot().getEntry("PowerPoint Document");
+ DocumentEntry oProps = (DocumentEntry)pfs.getRoot().getEntry(HSLFSlideShow.POWERPOINT_DOCUMENT);
+ DocumentEntry nProps = (DocumentEntry)npf.getRoot().getEntry(HSLFSlideShow.POWERPOINT_DOCUMENT);
assertEquals(oProps.getSize(),nProps.getSize());
// Check that they contain the same data
byte[] _oData = new byte[oProps.getSize()];
byte[] _nData = new byte[nProps.getSize()];
- pfs.createDocumentInputStream("PowerPoint Document").read(_oData);
- npf.createDocumentInputStream("PowerPoint Document").read(_nData);
+ pfs.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT).read(_oData);
+ npf.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT).read(_nData);
for(int i=0; i<_oData.length; i++) {
//System.out.println(i + "\t" + Integer.toHexString(i));
assertEquals(_oData[i], _nData[i]);
POIFSFileSystem npfs = new POIFSFileSystem(bais);
// Check that the "PowerPoint Document" sections have the same size
- DocumentEntry oProps = (DocumentEntry)pfs.getRoot().getEntry("PowerPoint Document");
- DocumentEntry nProps = (DocumentEntry)npfs.getRoot().getEntry("PowerPoint Document");
+ DocumentEntry oProps = (DocumentEntry)pfs.getRoot().getEntry(HSLFSlideShow.POWERPOINT_DOCUMENT);
+ DocumentEntry nProps = (DocumentEntry)npfs.getRoot().getEntry(HSLFSlideShow.POWERPOINT_DOCUMENT);
assertEquals(oProps.getSize(),nProps.getSize());
// Check that they contain the same data
byte[] _oData = new byte[oProps.getSize()];
byte[] _nData = new byte[nProps.getSize()];
- pfs.createDocumentInputStream("PowerPoint Document").read(_oData);
- npfs.createDocumentInputStream("PowerPoint Document").read(_nData);
+ pfs.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT).read(_oData);
+ npfs.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT).read(_nData);
for(int i=0; i<_oData.length; i++) {
if(_oData[i] != _nData[i])
System.out.println(i + "\t" + Integer.toHexString(i));
private PowerPointExtractor assertExtractFromEmbedded(DirectoryNode root, String entryName, String expected)
throws IOException {
DirectoryNode dir = (DirectoryNode)root.getEntry(entryName);
- assertTrue(dir.hasEntry("PowerPoint Document"));
+ assertTrue(dir.hasEntry(HSLFSlideShow.POWERPOINT_DOCUMENT));
// Check the first file
HSLFSlideShowImpl ppt = new HSLFSlideShowImpl(dir);
package org.apache.poi.hslf.model;
import java.awt.Color;
+import java.io.IOException;
+import org.apache.poi.hslf.usermodel.HSLFLine;
import org.apache.poi.hslf.usermodel.HSLFSlide;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
-import org.apache.poi.hslf.usermodel.HSLFLine;
import org.apache.poi.sl.usermodel.StrokeStyle.LineCompound;
import org.apache.poi.sl.usermodel.StrokeStyle.LineDash;
import org.junit.Test;
/**
* Test Line shape.
- *
- * @author Yegor Kozlov
*/
public final class TestLine {
@Test
- public void testCreateLines() {
+ public void testCreateLines() throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow();
HSLFSlide slide = ppt.createSlide();
line.setLineCompound(LineCompound.DOUBLE);
line.setLineWidth(8.0);
slide.addShape(line);
+
+ ppt.close();
}
}
import static org.junit.Assert.assertEquals;
+import java.io.IOException;
+
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.junit.Test;
/**
* Test adding fonts to the presenataion resources
- *
- * @author Yegor Kozlov
*/
public final class TestPPFont {
@Test
- public void testCreate() {
+ public void testCreate() throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow();
assertEquals(1, ppt.getNumberOfFonts());
assertEquals("Arial", ppt.getFont(0).getFontName());
assertEquals(PPFont.WINGDINGS.getFontName(), font3.getFontName());
assertEquals(PPFont.SYMBOL_CHARSET, font3.getCharSet());
assertEquals(PPFont.VARIABLE_PITCH, font3.getPitchAndFamily());
+
+ ppt.close();
}
}
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
+import java.io.IOException;
import java.util.List;
import org.apache.poi.POIDataSamples;
import org.apache.poi.hslf.model.textproperties.CharFlagsTextProp;
import org.apache.poi.hslf.record.Environment;
import org.apache.poi.hslf.record.TextHeaderAtom;
-import org.apache.poi.hslf.usermodel.*;
+import org.apache.poi.hslf.usermodel.HSLFMasterSheet;
+import org.apache.poi.hslf.usermodel.HSLFSlide;
+import org.apache.poi.hslf.usermodel.HSLFSlideMaster;
+import org.apache.poi.hslf.usermodel.HSLFSlideShow;
+import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
+import org.apache.poi.hslf.usermodel.HSLFTextParagraph;
+import org.apache.poi.hslf.usermodel.HSLFTextRun;
+import org.apache.poi.hslf.usermodel.HSLFTitleMaster;
import org.junit.Test;
/**
* Tests for SlideMaster
- *
- * @author Yegor Kozlov
*/
public final class TestSlideMaster {
private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();
* Check we can read their attributes.
*/
@Test
- public void testSlideMaster() throws Exception {
+ public void testSlideMaster() throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("slide_master.ppt"));
Environment env = ppt.getDocumentRecord().getEnvironment();
int b2 = master.get(1).getStyleAttribute(TextHeaderAtom.BODY_TYPE, 0, "bullet.font", false).getValue();
assertEquals("Arial", env.getFontCollection().getFontWithId(b1));
assertEquals("Georgia", env.getFontCollection().getFontWithId(b2));
+
+ ppt.close();
}
/**
* Test we can read default text attributes for a title master sheet
*/
@Test
- public void testTitleMasterTextAttributes() throws Exception {
+ public void testTitleMasterTextAttributes() throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("slide_master.ppt"));
List<HSLFTitleMaster> master = ppt.getTitleMasters();
assertEquals(1, master.size());
assertEquals(true, prop2.getSubValue(CharFlagsTextProp.BOLD_IDX));
assertEquals(false, prop2.getSubValue(CharFlagsTextProp.ITALIC_IDX));
assertEquals(false, prop2.getSubValue(CharFlagsTextProp.UNDERLINE_IDX));
+
+ ppt.close();
}
/**
* Slide 3 has title layout and follows the TitleMaster. Verify that.
*/
@Test
- public void testTitleMaster() throws Exception {
+ public void testTitleMaster() throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("slide_master.ppt"));
HSLFSlide slide = ppt.getSlides().get(2);
HSLFMasterSheet masterSheet = slide.getMasterSheet();
}
}
+ ppt.close();
}
+
/**
* If a style attribute is not set ensure it is read from the master
*/
}
}
+ ppt.close();
}
/**
* Check we can dynamically assign a slide master to a slide.
*/
@Test
- public void testChangeSlideMaster() throws Exception {
+ public void testChangeSlideMaster() throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("slide_master.ppt"));
List<HSLFSlideMaster> master = ppt.getSlideMasters();
List<HSLFSlide> slide = ppt.getSlides();
for (HSLFSlide s : slide) {
assertEquals(sheetNo, s.getMasterSheet()._getSheetNumber());
}
+
+ ppt.close();
}
/**
* (typical for the "bullted body" placeholder)
*/
@Test
- public void testIndentation() throws Exception {
+ public void testIndentation() throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("slide_master.ppt"));
HSLFSlide slide = ppt.getSlides().get(0);
-
+
for (List<HSLFTextParagraph> tparas : slide.getTextParagraphs()) {
HSLFTextParagraph tpara = tparas.get(0);
if (tpara.getRunType() == TextHeaderAtom.TITLE_TYPE){
}
}
}
-
+ ppt.close();
}
-
-}
+}
\ No newline at end of file
package org.apache.poi.hslf.model;
+import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
+import java.io.IOException;
import java.util.List;
import org.apache.poi.POIDataSamples;
-import org.apache.poi.hslf.usermodel.*;
+import org.apache.poi.hslf.usermodel.HSLFSlideShow;
+import org.apache.poi.hslf.usermodel.HSLFTextParagraph;
+import org.apache.poi.hslf.usermodel.HSLFTextRun;
+import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.junit.Before;
* Tests that if we load something up, get a TextRun, set the text
* to be the same as it was before, and write it all back out again,
* that we don't break anything in the process.
- *
- * @author Nick Burch (nick at torchbox dot com)
*/
public final class TestTextRunReWrite {
- // HSLFSlideShow primed on the test data
- private HSLFSlideShowImpl hss;
// HSLFSlideShow primed on the test data
private HSLFSlideShow ss;
- // POIFS primed on the test data
- private POIFSFileSystem pfs;
/**
* Load up a test PPT file with rich data
public void setUp() throws Exception {
POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
String filename = "Single_Coloured_Page_With_Fonts_and_Alignments.ppt";
- pfs = new POIFSFileSystem(slTests.openResourceAsStream(filename));
- hss = new HSLFSlideShowImpl(pfs);
- ss = new HSLFSlideShow(hss);
+ ss = new HSLFSlideShow(slTests.openResourceAsStream(filename));
}
@Test
- public void testWritesOutTheSameNonRich() throws Exception {
+ public void testWritesOutTheSameNonRich() throws IOException {
// Ensure the text lengths are as we'd expect to start with
assertEquals(1, ss.getSlides().size());
assertEquals(2, ss.getSlides().get(0).getTextParagraphs().size());
POIFSFileSystem npfs = new POIFSFileSystem(bais);
// Check that the "PowerPoint Document" sections have the same size
- DocumentEntry oProps = (DocumentEntry)pfs.getRoot().getEntry("PowerPoint Document");
- DocumentEntry nProps = (DocumentEntry)npfs.getRoot().getEntry("PowerPoint Document");
+ DirectoryNode oDir = ss.getSlideShowImpl().getDirectory();
+
+ DocumentEntry oProps = (DocumentEntry)oDir.getEntry(HSLFSlideShow.POWERPOINT_DOCUMENT);
+ DocumentEntry nProps = (DocumentEntry)npfs.getRoot().getEntry(HSLFSlideShow.POWERPOINT_DOCUMENT);
assertEquals(oProps.getSize(),nProps.getSize());
// Check that they contain the same data
byte[] _oData = new byte[oProps.getSize()];
byte[] _nData = new byte[nProps.getSize()];
- pfs.createDocumentInputStream("PowerPoint Document").read(_oData);
- npfs.createDocumentInputStream("PowerPoint Document").read(_nData);
- for(int i=0; i<_oData.length; i++) {
-// System.out.println(i + "\t" + Integer.toHexString(i));
- assertEquals(_oData[i], _nData[i]);
- }
+ oDir.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT).read(_oData);
+ npfs.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT).read(_nData);
+ assertArrayEquals(_oData, _nData);
+
+ npfs.close();
}
@Test
- public void testWritesOutTheSameRich() throws Exception {
+ public void testWritesOutTheSameRich() throws IOException {
// Grab the first text run on the first sheet
List<HSLFTextParagraph> tr1 = ss.getSlides().get(0).getTextParagraphs().get(0);
POIFSFileSystem npfs = new POIFSFileSystem(bais);
// Check that the "PowerPoint Document" sections have the same size
- DocumentEntry oProps = (DocumentEntry)pfs.getRoot().getEntry("PowerPoint Document");
- DocumentEntry nProps = (DocumentEntry)npfs.getRoot().getEntry("PowerPoint Document");
+ DirectoryNode oDir = ss.getSlideShowImpl().getDirectory();
+
+ DocumentEntry oProps = (DocumentEntry)oDir.getEntry(HSLFSlideShow.POWERPOINT_DOCUMENT);
+ DocumentEntry nProps = (DocumentEntry)npfs.getRoot().getEntry(HSLFSlideShow.POWERPOINT_DOCUMENT);
assertEquals(oProps.getSize(),nProps.getSize());
// Check that they contain the same data
byte[] _oData = new byte[oProps.getSize()];
byte[] _nData = new byte[nProps.getSize()];
- pfs.createDocumentInputStream("PowerPoint Document").read(_oData);
- npfs.createDocumentInputStream("PowerPoint Document").read(_nData);
- for(int i=0; i<_oData.length; i++) {
-// System.out.println(i + "\t" + Integer.toHexString(i) + "\t" + _oData[i]);
- assertEquals(_oData[i], _nData[i]);
- }
+
+ oDir.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT).read(_oData);
+ npfs.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT).read(_nData);
+ assertArrayEquals(_oData, _nData);
+
+ npfs.close();
}
}
package org.apache.poi.hslf.record;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
import java.io.ByteArrayOutputStream;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
-import junit.framework.AssertionFailedError;
-import junit.framework.TestCase;
-
+import org.apache.poi.POIDataSamples;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
-import org.apache.poi.POIDataSamples;
+import org.junit.Test;
/**
* Tests that ExHyperlink works properly.
- *
- * @author Nick Burch (nick at torchbox dot com)
*/
-public final class TestExHyperlink extends TestCase {
- // From a real file
- private final byte[] data_a = new byte[] {
- 0x0F, 00, 0xD7-256, 0x0F, 0xA8-256, 00, 00, 00,
-
- 00, 00, 0xD3-256, 0x0F, 04, 00, 00, 00,
- 03, 00, 00, 00,
-
- 00, 00, 0xBA-256, 0x0F, 0x46, 00, 00, 00,
- 0x68, 00, 0x74, 00, 0x74, 00, 0x70, 00,
- 0x3A, 00, 0x2F, 00, 0x2F, 00, 0x6A, 00,
- 0x61, 00, 0x6B, 00, 0x61, 00, 0x72, 00,
- 0x74, 00, 0x61, 00, 0x2E, 00, 0x61, 00,
- 0x70, 00, 0x61, 00, 0x63, 00, 0x68, 00,
- 0x65, 00, 0x2E, 00, 0x6F, 00, 0x72, 00,
- 0x67, 00, 0x2F, 00, 0x70, 00, 0x6F, 00,
- 0x69, 00, 0x2F, 00, 0x68, 00, 0x73, 00,
- 0x73, 00, 0x66, 00, 0x2F, 00,
-
- 0x10, 00, 0xBA-256, 0x0F, 0x46, 00, 00, 00,
- 0x68, 00, 0x74, 00, 0x74, 00, 0x70, 00,
- 0x3A, 00, 0x2F, 00, 0x2F, 00, 0x6A, 00,
- 0x61, 00, 0x6B, 00, 0x61, 00, 0x72, 00,
- 0x74, 00, 0x61, 00, 0x2E, 00, 0x61, 00,
- 0x70, 00, 0x61, 00, 0x63, 00, 0x68, 00,
- 0x65, 00, 0x2E, 00, 0x6F, 00, 0x72, 00,
- 0x67, 00, 0x2F, 00, 0x70, 00, 0x6F, 00,
- 0x69, 00, 0x2F, 00, 0x68, 00, 0x73, 00,
- 0x73, 00, 0x66, 00, 0x2F, 00
- };
-
- public void testRecordType() {
- ExHyperlink eh = new ExHyperlink(data_a, 0, data_a.length);
- assertEquals(4055l, eh.getRecordType());
- }
-
- public void testNumber() {
- ExHyperlink eh = new ExHyperlink(data_a, 0, data_a.length);
- assertEquals(3, eh.getExHyperlinkAtom().getNumber());
- }
-
- public void testLinkURL() {
- ExHyperlink eh = new ExHyperlink(data_a, 0, data_a.length);
- assertEquals("http://jakarta.apache.org/poi/hssf/", eh.getLinkURL());
+public final class TestExHyperlink {
+ @Test
+ public void testReadWrite() throws IOException {
+ // From a real file
+ byte[] exHyperlinkBytes = org.apache.poi.poifs.storage.RawDataUtil.decompress(
+ "H4sIAAAAAAAAAONnuM6/ggEELvOzAElmMHsXvxuQzGAoAcICBisGfSDMYkhkyAbi"+
+ "IqBYIoMeEBcAcTJQVSqQlw8UTweqKgCyMoF0BkMxEKYBWQJUNQ0A/k1x3rAAAAA="
+ );
+ ExHyperlink exHyperlink = new ExHyperlink(exHyperlinkBytes, 0, exHyperlinkBytes.length);
+
+
+ assertEquals(4055l, exHyperlink.getRecordType());
+ assertEquals(3, exHyperlink.getExHyperlinkAtom().getNumber());
+ String expURL = "http://jakarta.apache.org/poi/hssf/";
+ assertEquals(expURL, exHyperlink.getLinkURL());
+ assertEquals(expURL, exHyperlink._getDetailsA());
+ assertEquals(expURL, exHyperlink._getDetailsB());
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ exHyperlink.writeOut(baos);
+ assertArrayEquals(exHyperlinkBytes, baos.toByteArray());
}
- public void testDetails() {
- ExHyperlink eh = new ExHyperlink(data_a, 0, data_a.length);
- assertEquals("http://jakarta.apache.org/poi/hssf/", eh._getDetailsA());
- assertEquals("http://jakarta.apache.org/poi/hssf/", eh._getDetailsB());
- }
-
- public void testWrite() throws Exception {
- ExHyperlink eh = new ExHyperlink(data_a, 0, data_a.length);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- eh.writeOut(baos);
- byte[] b = baos.toByteArray();
- assertEquals(data_a.length, b.length);
- for(int i=0; i<data_a.length; i++) {
- assertEquals(data_a[i],b[i]);
- }
- }
-
- public void testRealFile() throws Exception {
+ @Test
+ public void testRealFile() throws IOException {
POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
HSLFSlideShowImpl hss = new HSLFSlideShowImpl(slTests.openResourceAsStream("WithLinks.ppt"));
HSLFSlideShow ss = new HSLFSlideShow(hss);
exObjList = (ExObjList)rec;
}
}
- if (exObjList == null) {
- throw new AssertionFailedError("exObjList must not be null");
- }
+
+ assertNotNull(exObjList);
// Within that, grab out the Hyperlink atoms
List<ExHyperlink> linksA = new ArrayList<ExHyperlink>();
assertEquals(4, links[3].getExHyperlinkAtom().getNumber());
assertEquals("http://jakarta.apache.org/hslf/", links[3].getLinkURL());
+ ss.close();
}
}
package org.apache.poi.hslf.record;
-import junit.framework.TestCase;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
-import org.apache.poi.hslf.usermodel.HSLFSlideShow;
-import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
import org.apache.poi.POIDataSamples;
+import org.apache.poi.hslf.usermodel.HSLFSlideShow;
+import org.junit.Test;
/**
* Tests that ExObjList works properly.
- *
- * @author Nick Burch (nick at torchbox dot com)
*/
-public class TestExObjList extends TestCase {
- public void testRealFile() throws Exception {
+public class TestExObjList {
+ @Test
+ public void testRealFile() throws Exception {
POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
- HSLFSlideShowImpl hss = new HSLFSlideShowImpl(slTests.openResourceAsStream("WithLinks.ppt"));
- HSLFSlideShow ss = new HSLFSlideShow(hss);
+ HSLFSlideShow ss = new HSLFSlideShow(slTests.openResourceAsStream("WithLinks.ppt"));
// Get the document
Document doc = ss.getDocumentRecord();
assertEquals(4, links[3].getExHyperlinkAtom().getNumber());
assertEquals("http://jakarta.apache.org/hslf/", links[3].getLinkURL());
+ ss.close();
}
}
package org.apache.poi.hslf.record;
import static org.junit.Assert.assertArrayEquals;
-import junit.framework.AssertionFailedError;
-import junit.framework.TestCase;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.IOException;
import org.apache.poi.POIDataSamples;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
+import org.junit.Test;
/**
* Tests Sound-related records: SoundCollection(2020), Sound(2022) and
* SoundData(2023)).
- *
- * @author Yegor Kozlov
*/
-public final class TestSound extends TestCase {
- public void testRealFile() throws Exception {
+public final class TestSound {
+ @Test
+ public void testRealFile() throws IOException {
POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
HSLFSlideShow ppt = new HSLFSlideShow(slTests.openResourceAsStream("sound.ppt"));
break;
}
}
- if (soundCollection == null) {
- throw new AssertionFailedError("soundCollection must not be null");
- }
+ assertNotNull(soundCollection);
Sound sound = null;
Record[] sound_ch = soundCollection.getChildRecords();
k++;
}
}
- if (sound == null) {
- throw new AssertionFailedError("sound must not be null");
- }
+
+ assertNotNull(sound);
assertEquals(1, k);
assertEquals("ringin.wav", sound.getSoundName());
byte[] ref_data = slTests.readFile("ringin.wav");
assertArrayEquals(ref_data, sound.getSoundData());
- }
+
+ ppt.close();
+ }
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
+import java.awt.FontFormatException;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
+import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
// @Ignore2("This fails on some systems because fonts are rendered slightly different")
@Test
- public void bug55902mixedFontWithChineseCharacters() throws Exception {
+ public void bug55902mixedFontWithChineseCharacters() throws IOException, FontFormatException {
// font files need to be downloaded first via
// ant test-scratchpad-download-resources
String fontFiles[][] = {
}
assertArrayEquals("Expected to have matching raster-arrays, but found differences", expectedData, actualData);
+ ss.close();
}
}
package org.apache.poi.hslf.usermodel;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import java.io.IOException;
import java.util.List;
import org.apache.poi.POIDataSamples;
import org.apache.poi.hslf.model.textproperties.TextPFException9;
import org.apache.poi.hslf.model.textproperties.TextPropCollection;
-import org.apache.poi.hslf.record.*;
+import org.apache.poi.hslf.record.EscherTextboxWrapper;
+import org.apache.poi.hslf.record.StyleTextProp9Atom;
+import org.apache.poi.hslf.record.StyleTextPropAtom;
import org.apache.poi.sl.usermodel.AutoNumberingScheme;
import org.junit.Test;
* if a paragraph has autonumber ()
* @see <a href="http://social.msdn.microsoft.com/Forums/mr-IN/os_binaryfile/thread/650888db-fabd-4b95-88dc-f0455f6e2d28">
* PPT: Missing TextAutoNumberScheme structure providing the style of the number bullets</a>
- *
- * @author Alex Nikiforov [mailto:anikif@gmail.com]
*/
public final class TestNumberedList2 {
private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();
@Test
- public void testNumberedList() throws Exception {
+ public void testNumberedList() throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("numbers2.ppt"));
assertTrue("No Exceptions while reading file", true);
assertEquals(2, slides.size());
checkSlide0(slides.get(0));
checkSlide1(slides.get(1));
- }
- private void checkSlide0(final HSLFSlide s) {
+
+ ppt.close();
+ }
+
+ private void checkSlide0(final HSLFSlide s) {
final StyleTextProp9Atom[] numberedListArray = s.getNumberedListInfo();
assertNotNull(numberedListArray);
assertEquals(2, numberedListArray.length);
checkSingleRunWrapper(44, styleAtoms[0]);
checkSingleRunWrapper(130, styleAtoms[1]);
}
+
private void checkSlide1(final HSLFSlide s) {
final StyleTextProp9Atom[] numberedListArray = s.getNumberedListInfo();
assertNotNull(numberedListArray);
checkSingleRunWrapper(67, styleAtoms[1]);
checkSingleRunWrapper(70, styleAtoms[2]);
}
+
private void checkSingleRunWrapper(final int exceptedLength, final EscherTextboxWrapper wrapper) {
final StyleTextPropAtom styleTextPropAtom = wrapper.getStyleTextPropAtom();
final List<TextPropCollection> textProps = styleTextPropAtom.getCharacterStyles();
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
+import java.io.IOException;
import org.apache.poi.POIDataSamples;
+import org.apache.poi.hslf.HSLFTestDataSamples;
import org.junit.Before;
import org.junit.Test;
/**
* Tests that SlideShow can re-order slides properly
- *
- * @author Nick Burch (nick at torchbox dot com)
*/
public final class TestReOrderingSlides {
// A SlideShow with one slide
* Test that we can "re-order" a slideshow with only 1 slide on it
*/
@Test
- public void testReOrder1() throws Exception {
+ public void testReOrder1() throws IOException {
// Has one slide
assertEquals(1, ss_one.getSlides().size());
HSLFSlide s1 = ss_one.getSlides().get(0);
ss_one.reorderSlide(1, 1);
// Write out, and read back in
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- hss_one.write(baos);
- ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
-
- HSLFSlideShowImpl hss_read = new HSLFSlideShowImpl(bais);
- HSLFSlideShow ss_read = new HSLFSlideShow(hss_read);
+ HSLFSlideShow ss_read = HSLFTestDataSamples.writeOutAndReadBack(ss_one);
// Check it still has 1 slide
assertEquals(1, ss_read.getSlides().size());
assertEquals(256, s1._getSheetNumber());
assertEquals(3, s1._getSheetRefId());
assertEquals(1, s1.getSlideNumber());
+
+ ss_read.close();
}
/**
* two slides in it
*/
@Test
- public void testReOrder2() throws Exception {
+ public void testReOrder2() throws IOException {
// Has two slides
assertEquals(2, ss_two.getSlides().size());
HSLFSlide s1 = ss_two.getSlides().get(0);
ss_two.reorderSlide(2, 2);
// Write out, and read back in
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- hss_two.write(baos);
- ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
-
- HSLFSlideShowImpl hss_read = new HSLFSlideShowImpl(bais);
- HSLFSlideShow ss_read = new HSLFSlideShow(hss_read);
+ HSLFSlideShow ss_read = HSLFTestDataSamples.writeOutAndReadBack(ss_two);
// Check it still has 2 slides
assertEquals(2, ss_read.getSlides().size());
assertEquals(257, s2._getSheetNumber());
assertEquals(6, s2._getSheetRefId());
assertEquals(2, s2.getSlideNumber());
- }
+
+ ss_read.close();
+ }
/**
* Test re-ordering slides in a slideshow with 2 slides on it
*/
@Test
- public void testReOrder2swap() throws Exception {
+ public void testReOrder2swap() throws IOException {
// Has two slides
assertEquals(2, ss_two.getSlides().size());
HSLFSlide s1 = ss_two.getSlides().get(0);
ss_two.reorderSlide(2, 1);
// Write out, and read back in
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- hss_two.write(baos);
- ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
-
- HSLFSlideShowImpl hss_read = new HSLFSlideShowImpl(bais);
- HSLFSlideShow ss_read = new HSLFSlideShow(hss_read);
+ HSLFSlideShow ss_read = HSLFTestDataSamples.writeOutAndReadBack(ss_two);
// Check it still has 2 slides
assertEquals(2, ss_read.getSlides().size());
assertEquals(256, s2._getSheetNumber());
assertEquals(4, s2._getSheetRefId());
assertEquals(2, s2.getSlideNumber());
- }
+
+ ss_read.close();
+ }
/**
* Test doing a dummy re-order on a slideshow with
* three slides in it
*/
@Test
- public void testReOrder3() throws Exception {
+ public void testReOrder3() throws IOException {
// Has three slides
assertEquals(3, ss_three.getSlides().size());
HSLFSlide s1 = ss_three.getSlides().get(0);
ss_three.reorderSlide(2, 2);
// Write out, and read back in
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- hss_three.write(baos);
- ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
-
- HSLFSlideShowImpl hss_read = new HSLFSlideShowImpl(bais);
- HSLFSlideShow ss_read = new HSLFSlideShow(hss_read);
+ HSLFSlideShow ss_read = HSLFTestDataSamples.writeOutAndReadBack(ss_three);
// Check it still has 3 slides
assertEquals(3, ss_read.getSlides().size());
assertEquals(257, s3._getSheetNumber());
assertEquals(4, s3._getSheetRefId());
assertEquals(3, s3.getSlideNumber());
- }
+
+ ss_read.close();
+ }
/**
* Test re-ordering slides in a slideshow with 3 slides on it
*/
@Test
- public void testReOrder3swap() throws Exception {
+ public void testReOrder3swap() throws IOException {
// Has three slides
assertEquals(3, ss_three.getSlides().size());
HSLFSlide s1 = ss_three.getSlides().get(0);
assertEquals("Slide 1", ((HSLFTextShape)s3.getShapes().get(0)).getText());
// Write out, and read back in
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- hss_three.write(baos);
- ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
-
- HSLFSlideShowImpl hss_read = new HSLFSlideShowImpl(bais);
- HSLFSlideShow ss_read = new HSLFSlideShow(hss_read);
+ HSLFSlideShow ss_read = HSLFTestDataSamples.writeOutAndReadBack(ss_three);
// Check it still has 3 slides
assertEquals(3, ss_read.getSlides().size());
assertEquals(s3._getSheetNumber(), _s3._getSheetNumber());
assertEquals(s3._getSheetRefId(), _s3._getSheetRefId());
assertEquals(3, s3.getSlideNumber());
+
+ ss_read.close();
}
}
private static void assertMatchesFileC(HSLFSlideShow s) throws IOException {
// Grab the bytes of the file
NPOIFSFileSystem fs = new NPOIFSFileSystem(HSLFTestDataSamples.openSampleFileStream(filenameC));
- InputStream is = fs.createDocumentInputStream("PowerPoint Document");
+ InputStream is = fs.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT);
byte[] raw_file = IOUtils.toByteArray(is);
is.close();
fs.close();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
s.write(baos);
fs = new NPOIFSFileSystem(new ByteArrayInputStream(baos.toByteArray()));
- is = fs.createDocumentInputStream("PowerPoint Document");
+ is = fs.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT);
byte[] raw_ss = IOUtils.toByteArray(is);
is.close();
fs.close();
private final POIDataSamples samples = POIDataSamples.getHSMFInstance();
@Test
- public void testFindsCore() throws Exception {
+ public void testFindsCore() throws IOException, ChunkNotFoundException {
NPOIFSFileSystem simple = new NPOIFSFileSystem(samples.getFile("quick.msg"), true);
-
+
// Check a few core things are present
simple.getRoot().getEntry(
(new StringChunk(MAPIProperty.SUBJECT.id, Types.ASCII_STRING)).getEntryName()
simple.getRoot().getEntry(
(new StringChunk(MAPIProperty.SENDER_NAME.id, Types.ASCII_STRING)).getEntryName()
);
-
+
// Now load the file
MAPIMessage msg = new MAPIMessage(simple);
assertEquals("Kevin Roast", msg.getDisplayTo());
assertEquals("Kevin Roast", msg.getDisplayFrom());
assertEquals("Test the content transformer", msg.getSubject());
-
+
// Check date too
Calendar calExp = LocaleUtil.getLocaleCalendar(2007,5,14,9,42,55);
Calendar calAct = msg.getMessageDate();
assertEquals( calExp, calAct );
+ msg.close();
simple.close();
}
-
+
+ @Test
public void testFindsRecips() throws IOException, ChunkNotFoundException {
NPOIFSFileSystem simple = new NPOIFSFileSystem(samples.getFile("quick.msg"), true);
-
+
simple.getRoot().getEntry("__recip_version1.0_#00000000");
ChunkGroup[] groups = POIFSChunkParser.parse(simple.getRoot());
assertTrue(groups[0] instanceof Chunks);
assertTrue(groups[1] instanceof RecipientChunks);
assertTrue(groups[2] instanceof NameIdChunks);
-
+
RecipientChunks recips = (RecipientChunks)groups[1];
assertEquals("kevin.roast@alfresco.org", recips.recipientSMTPChunk.getValue());
- assertEquals("/O=HOSTEDSERVICE2/OU=FIRST ADMINISTRATIVE GROUP/CN=RECIPIENTS/CN=Kevin.roast@ben",
+ assertEquals("/O=HOSTEDSERVICE2/OU=FIRST ADMINISTRATIVE GROUP/CN=RECIPIENTS/CN=Kevin.roast@ben",
recips.recipientEmailChunk.getValue());
-
+
String search = new String(recips.recipientSearchChunk.getValue(), "ASCII");
assertEquals("CN=KEVIN.ROAST@BEN\0", search.substring(search.length()-19));
-
+
// Now via MAPIMessage
MAPIMessage msg = new MAPIMessage(simple);
assertNotNull(msg.getRecipientDetailsChunks());
assertEquals(1, msg.getRecipientDetailsChunks().length);
-
+
assertEquals("kevin.roast@alfresco.org", msg.getRecipientDetailsChunks()[0].recipientSMTPChunk.getValue());
assertEquals("kevin.roast@alfresco.org", msg.getRecipientDetailsChunks()[0].getRecipientEmailAddress());
assertEquals("Kevin Roast", msg.getRecipientDetailsChunks()[0].getRecipientName());
assertEquals("kevin.roast@alfresco.org", msg.getRecipientEmailAddress());
-
-
+
+
// Try both SMTP and EX files for recipient
assertEquals("EX", msg.getRecipientDetailsChunks()[0].deliveryTypeChunk.getValue());
assertEquals("kevin.roast@alfresco.org", msg.getRecipientDetailsChunks()[0].recipientSMTPChunk.getValue());
- assertEquals("/O=HOSTEDSERVICE2/OU=FIRST ADMINISTRATIVE GROUP/CN=RECIPIENTS/CN=Kevin.roast@ben",
+ assertEquals("/O=HOSTEDSERVICE2/OU=FIRST ADMINISTRATIVE GROUP/CN=RECIPIENTS/CN=Kevin.roast@ben",
msg.getRecipientDetailsChunks()[0].recipientEmailChunk.getValue());
+ msg.close();
simple.close();
-
-
+
+
// Now look at another message
simple = new NPOIFSFileSystem(samples.getFile("simple_test_msg.msg"), true);
msg = new MAPIMessage(simple);
assertNotNull(msg.getRecipientDetailsChunks());
assertEquals(1, msg.getRecipientDetailsChunks().length);
-
+
assertEquals("SMTP", msg.getRecipientDetailsChunks()[0].deliveryTypeChunk.getValue());
assertEquals(null, msg.getRecipientDetailsChunks()[0].recipientSMTPChunk);
assertEquals(null, msg.getRecipientDetailsChunks()[0].recipientNameChunk);
assertEquals("travis@overwrittenstack.com", msg.getRecipientDetailsChunks()[0].recipientEmailChunk.getValue());
assertEquals("travis@overwrittenstack.com", msg.getRecipientEmailAddress());
-
+
+ msg.close();
simple.close();
}
-
+
+ @Test
public void testFindsMultipleRecipients() throws IOException, ChunkNotFoundException {
NPOIFSFileSystem multiple = new NPOIFSFileSystem(samples.getFile("example_received_unicode.msg"), true);
-
+
multiple.getRoot().getEntry("__recip_version1.0_#00000000");
multiple.getRoot().getEntry("__recip_version1.0_#00000001");
multiple.getRoot().getEntry("__recip_version1.0_#00000002");
multiple.getRoot().getEntry("__recip_version1.0_#00000003");
multiple.getRoot().getEntry("__recip_version1.0_#00000004");
multiple.getRoot().getEntry("__recip_version1.0_#00000005");
-
+
ChunkGroup[] groups = POIFSChunkParser.parse(multiple.getRoot());
assertEquals(9, groups.length);
assertTrue(groups[0] instanceof Chunks);
assertTrue(groups[6] instanceof RecipientChunks);
assertTrue(groups[7] instanceof RecipientChunks);
assertTrue(groups[8] instanceof NameIdChunks);
-
+
// In FS order initially
RecipientChunks[] chunks = new RecipientChunks[] {
(RecipientChunks)groups[1],
assertEquals(5, chunks[3].recipientNumber);
assertEquals(3, chunks[4].recipientNumber);
assertEquals(1, chunks[5].recipientNumber);
-
+
// Check
assertEquals("'Ashutosh Dandavate'", chunks[0].getRecipientName());
assertEquals("ashutosh.dandavate@alfresco.com", chunks[0].getRecipientEmailAddress());
assertEquals("nickb@alfresco.com", chunks[4].getRecipientEmailAddress());
assertEquals("'Paul Holmes-Higgin'", chunks[5].getRecipientName());
assertEquals("paul.hh@alfresco.com", chunks[5].getRecipientEmailAddress());
-
+
// Now sort, and re-check
Arrays.sort(chunks, new RecipientChunksSorter());
-
+
assertEquals("'Ashutosh Dandavate'", chunks[0].getRecipientName());
assertEquals("ashutosh.dandavate@alfresco.com", chunks[0].getRecipientEmailAddress());
assertEquals("'Paul Holmes-Higgin'", chunks[1].getRecipientName());
assertEquals("nick.burch@alfresco.com", chunks[4].getRecipientEmailAddress());
assertEquals("'Roy Wetherall'", chunks[5].getRecipientName());
assertEquals("roy.wetherall@alfresco.com", chunks[5].getRecipientEmailAddress());
-
+
// Finally check on message
MAPIMessage msg = new MAPIMessage(multiple);
assertEquals(6, msg.getRecipientEmailAddressList().length);
assertEquals(6, msg.getRecipientNamesList().length);
-
+
assertEquals("'Ashutosh Dandavate'", msg.getRecipientNamesList()[0]);
assertEquals("'Paul Holmes-Higgin'", msg.getRecipientNamesList()[1]);
assertEquals("'Mike Farman'", msg.getRecipientNamesList()[2]);
assertEquals("nickb@alfresco.com", msg.getRecipientNamesList()[3]);
assertEquals("nick.burch@alfresco.com", msg.getRecipientNamesList()[4]);
assertEquals("'Roy Wetherall'", msg.getRecipientNamesList()[5]);
-
+
assertEquals("ashutosh.dandavate@alfresco.com", msg.getRecipientEmailAddressList()[0]);
assertEquals("paul.hh@alfresco.com", msg.getRecipientEmailAddressList()[1]);
assertEquals("mikef@alfresco.com", msg.getRecipientEmailAddressList()[2]);
assertEquals("nickb@alfresco.com", msg.getRecipientEmailAddressList()[3]);
assertEquals("nick.burch@alfresco.com", msg.getRecipientEmailAddressList()[4]);
assertEquals("roy.wetherall@alfresco.com", msg.getRecipientEmailAddressList()[5]);
-
+
+ msg.close();
multiple.close();
}
-
+
+ @Test
public void testFindsNameId() throws IOException {
NPOIFSFileSystem simple = new NPOIFSFileSystem(samples.getFile("quick.msg"), true);
-
+
simple.getRoot().getEntry("__nameid_version1.0");
ChunkGroup[] groups = POIFSChunkParser.parse(simple.getRoot());
assertTrue(groups[0] instanceof Chunks);
assertTrue(groups[1] instanceof RecipientChunks);
assertTrue(groups[2] instanceof NameIdChunks);
-
+
NameIdChunks nameId = (NameIdChunks)groups[2];
assertEquals(10, nameId.getAll().length);
-
+
// Now via MAPIMessage
MAPIMessage msg = new MAPIMessage(simple);
assertNotNull(msg.getNameIdChunks());
assertEquals(10, msg.getNameIdChunks().getAll().length);
-
+
+ msg.close();
simple.close();
}
-
- public void testFindsAttachments() throws Exception {
+
+ @Test
+ public void testFindsAttachments() throws IOException, ChunkNotFoundException {
NPOIFSFileSystem with = new NPOIFSFileSystem(samples.getFile("attachment_test_msg.msg"), true);
NPOIFSFileSystem without = new NPOIFSFileSystem(samples.getFile("quick.msg"), true);
AttachmentChunks attachment;
-
-
+
+
// Check raw details on the one with
with.getRoot().getEntry("__attach_version1.0_#00000000");
with.getRoot().getEntry("__attach_version1.0_#00000001");
POIFSChunkParser.parse(with.getRoot());
-
+
ChunkGroup[] groups = POIFSChunkParser.parse(with.getRoot());
assertEquals(5, groups.length);
assertTrue(groups[0] instanceof Chunks);
assertTrue(groups[2] instanceof AttachmentChunks);
assertTrue(groups[3] instanceof AttachmentChunks);
assertTrue(groups[4] instanceof NameIdChunks);
-
+
attachment = (AttachmentChunks)groups[2];
assertEquals("TEST-U~1.DOC", attachment.getAttachFileName().toString());
assertEquals("test-unicode.doc", attachment.getAttachLongFileName().toString());
assertEquals(24064, attachment.getAttachData().getValue().length);
-
+
attachment = (AttachmentChunks)groups[3];
assertEquals("pj1.txt", attachment.getAttachFileName().toString());
assertEquals("pj1.txt", attachment.getAttachLongFileName().toString());
assertEquals(89, attachment.getAttachData().getValue().length);
-
-
+
+
// Check raw details on one without
assertFalse(without.getRoot().hasEntry("__attach_version1.0_#00000000"));
assertFalse(without.getRoot().hasEntry("__attach_version1.0_#00000001"));
-
+
// One with, from the top
MAPIMessage msgWith = new MAPIMessage(with);
assertEquals(2, msgWith.getAttachmentFiles().length);
assertEquals("TEST-U~1.DOC", attachment.getAttachFileName().toString());
assertEquals("test-unicode.doc", attachment.getAttachLongFileName().toString());
assertEquals(24064, attachment.getAttachData().getValue().length);
-
+
attachment = msgWith.getAttachmentFiles()[1];
assertEquals("pj1.txt", attachment.getAttachFileName().toString());
assertEquals("pj1.txt", attachment.getAttachLongFileName().toString());
assertEquals(89, attachment.getAttachData().getValue().length);
-
+
// Plus check core details are there
assertEquals("'nicolas1.23456@free.fr'", msgWith.getDisplayTo());
assertEquals("Nicolas1 23456", msgWith.getDisplayFrom());
assertEquals("test pi\u00e8ce jointe 1", msgWith.getSubject());
-
+
// One without, from the top
MAPIMessage msgWithout = new MAPIMessage(without);
-
+
// No attachments
assertEquals(0, msgWithout.getAttachmentFiles().length);
-
+
// But has core details
assertEquals("Kevin Roast", msgWithout.getDisplayTo());
assertEquals("Kevin Roast", msgWithout.getDisplayFrom());
assertEquals("Test the content transformer", msgWithout.getSubject());
+ msgWithout.close();
+ msgWith.close();
without.close();
with.close();
}
-
+
/**
* Bugzilla #51873 - Outlook 2002 files created with dragging and
* dropping files to the disk include a non-standard named streams
* such as "Olk10SideProps_0001"
*/
- public void testOlk10SideProps() throws Exception {
+ @Test
+ public void testOlk10SideProps() throws IOException, ChunkNotFoundException {
NPOIFSFileSystem poifs = new NPOIFSFileSystem(samples.getFile("51873.msg"), true);
MAPIMessage msg = new MAPIMessage(poifs);
// Check core details came through
assertEquals("bubba@bubbasmith.com", msg.getDisplayTo());
assertEquals("Test with Olk10SideProps_ Chunk", msg.getSubject());
-
+
+ msg.close();
poifs.close();
}
}
assertContains(txt, "APPLICOLOR");
assertContains(txt, "les meilleurs");
assertContains(txt, "GUY LECOLE");
+ ex.close();
+ doc.close();
}
sb.append(p);
}
assertContains(sb.toString(), "\u043F\u0440\u0438\u0432\u0435\u0442");//Greetings!
+ ex.close();
+ doc.close();
}
@Test
sb.append(p);
}
assertContains(sb.toString(), "4 sk\u00f3re a p\u0159ed 7 lety");//Greetings!
+ ex.close();
+ doc.close();
}
@Test
//TODO: figure out why these two aren't passing
//assertContains(txt, "\u2019\u0078 block2");//make sure smart quote is extracted correctly
//assertContains(txt, "We are able to");//not sure if we can get this easily?
+ ex.close();
+ doc.close();
}
}
package org.apache.poi.hssf.usermodel;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.FileNotFoundException;
-import java.io.InputStream;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
-import junit.framework.TestCase;
+import java.io.IOException;
+import java.io.InputStream;
import org.apache.poi.hssf.HSSFTestDataSamples;
-import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import org.apache.poi.poifs.filesystem.DirectoryNode;
+import org.junit.Test;
/**
* Tests for how HSSFWorkbook behaves with XLS files
* with a WORKBOOK or BOOK directory entry (instead of
* the more usual, Workbook)
*/
-public final class TestNonStandardWorkbookStreamNames extends TestCase {
+public final class TestNonStandardWorkbookStreamNames {
private final String xlsA = "WORKBOOK_in_capitals.xls";
- private final String xlsB = "BOOK_in_capitals.xls";
+ private final String xlsB = "BOOK_in_capitals.xls";
/**
* Test that we can open a file with WORKBOOK
*/
- public void testOpenWORKBOOK() throws Exception {
+ @Test
+ public void testOpenWORKBOOK() throws IOException {
+ // Try to open the workbook
InputStream is = HSSFTestDataSamples.openSampleFileStream(xlsA);
-
- POIFSFileSystem fs = new POIFSFileSystem(is);
+ HSSFWorkbook wb = new HSSFWorkbook(is);
+ is.close();
+ DirectoryNode root = wb.getDirectory();
- // Ensure that we have a WORKBOOK entry
- fs.getRoot().getEntry("WORKBOOK");
- // And a summary
- fs.getRoot().getEntry("\005SummaryInformation");
- assertTrue(true);
+ // Ensure that we have a WORKBOOK entry and a summary
+ assertTrue(root.hasEntry("WORKBOOK"));
+ assertTrue(root.hasEntry("\005SummaryInformation"));
// But not a Workbook one
- try {
- fs.getRoot().getEntry("Workbook");
- fail();
- } catch(FileNotFoundException e) {}
+ assertFalse(root.hasEntry("Workbook"));
- // Try to open the workbook
- HSSFWorkbook wb = new HSSFWorkbook(fs);
+ wb.close();
}
/**
* Test that we can open a file with BOOK
*/
- public void testOpenBOOK() throws Exception {
+ @Test
+ public void testOpenBOOK() throws IOException {
+ // Try to open the workbook
InputStream is = HSSFTestDataSamples.openSampleFileStream(xlsB);
-
- POIFSFileSystem fs = new POIFSFileSystem(is);
+ HSSFWorkbook wb = new HSSFWorkbook(is);
+ is.close();
+ DirectoryNode root = wb.getDirectory();
// Ensure that we have a BOOK entry
- fs.getRoot().getEntry("BOOK");
- assertTrue(true);
-
- // But not a Workbook one
- try {
- fs.getRoot().getEntry("Workbook");
- fail();
- } catch(FileNotFoundException e) {}
- // And not a Summary one
- try {
- fs.getRoot().getEntry("\005SummaryInformation");
- fail();
- } catch(FileNotFoundException e) {}
+ assertTrue(root.hasEntry("BOOK"));
+
+ // But not a Workbook one and not a Summary one
+ assertFalse(root.hasEntry("Workbook"));
+ assertFalse(root.hasEntry("\\005SummaryInformation"));
- // Try to open the workbook
- HSSFWorkbook wb = new HSSFWorkbook(fs);
+ wb.close();
}
/**
* Test that when we write out, we go back to the correct case
*/
- public void testWrite() throws Exception {
+ @Test
+ public void testWrite() throws IOException {
for (String file : new String[] {xlsA, xlsB}) {
- InputStream is = HSSFTestDataSamples.openSampleFileStream(file);
- POIFSFileSystem fs = new POIFSFileSystem(is);
-
- // Open the workbook, not preserving nodes
- HSSFWorkbook wb = new HSSFWorkbook(fs);
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- wb.write(out);
-
- // Check now
- ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
- POIFSFileSystem fs2 = new POIFSFileSystem(in);
-
- // Check that we have the new entries
- fs2.getRoot().getEntry("Workbook");
- try {
- fs2.getRoot().getEntry("BOOK");
- fail();
- } catch(FileNotFoundException e) {}
- try {
- fs2.getRoot().getEntry("WORKBOOK");
- fail();
- } catch(FileNotFoundException e) {}
-
- // And it can be opened
- HSSFWorkbook wb2 = new HSSFWorkbook(fs2);
- }
+ // Open the workbook, not preserving nodes
+ InputStream is = HSSFTestDataSamples.openSampleFileStream(file);
+ HSSFWorkbook wb = new HSSFWorkbook(is, false);
+ is.close();
+
+ // Check now it can be opened
+ HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ wb.close();
+
+ DirectoryNode root = wb2.getDirectory();
+
+ // Check that we have the new entries
+ assertTrue(root.hasEntry("Workbook"));
+ assertFalse(root.hasEntry("BOOK"));
+ assertFalse(root.hasEntry("WORKBOOK"));
+
+ wb2.close();
+ }
}
/**
* Test that when we write out preserving nodes, we go back to the
* correct case
*/
- public void testWritePreserve() throws Exception {
+ @Test
+ public void testWritePreserve() throws IOException {
+ // Open the workbook, not preserving nodes
InputStream is = HSSFTestDataSamples.openSampleFileStream(xlsA);
- POIFSFileSystem fs = new POIFSFileSystem(is);
-
- // Open the workbook, not preserving nodes
- HSSFWorkbook wb = new HSSFWorkbook(fs,true);
-
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- wb.write(out);
-
- // Check now
- ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
- POIFSFileSystem fs2 = new POIFSFileSystem(in);
-
- // Check that we have the new entries
- fs2.getRoot().getEntry("Workbook");
- try {
- fs2.getRoot().getEntry("WORKBOOK");
- fail();
- } catch(FileNotFoundException e) {}
-
- // As we preserved, should also have a few other streams
- fs2.getRoot().getEntry("\005SummaryInformation");
-
- // And it can be opened
- HSSFWorkbook wb2 = new HSSFWorkbook(fs2);
+ HSSFWorkbook wb = new HSSFWorkbook(is,true);
+ is.close();
+
+ // Check now it can be opened
+ HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ wb.close();
+
+ DirectoryNode root = wb2.getDirectory();
+
+ // Check that we have the new entries
+ assertTrue(root.hasEntry("Workbook"));
+ assertFalse(root.hasEntry("BOOK"));
+ assertFalse(root.hasEntry("WORKBOOK"));
+
+ // As we preserved, should also have a few other streams
+ assertTrue(root.hasEntry("\005SummaryInformation"));
+ wb2.close();
}
}
package org.apache.poi.hssf.usermodel;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+
import org.apache.poi.POITestCase;
import org.apache.poi.ddf.EscherContainerRecord;
import org.apache.poi.ddf.EscherSpgrRecord;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.record.EscherAggregate;
+import org.junit.Test;
-import junit.framework.TestCase;
-
-/**
- * @author Evgeniy Berlog
- * @date 29.06.12
- */
-public class TestShapeGroup extends TestCase{
+public class TestShapeGroup {
- public void testSetGetCoordinates(){
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sh = wb.createSheet();
+ @Test
+ public void testSetGetCoordinates() throws IOException {
+ HSSFWorkbook wb1 = new HSSFWorkbook();
+ HSSFSheet sh = wb1.createSheet();
HSSFPatriarch patriarch = sh.createDrawingPatriarch();
HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor());
assertEquals(group.getX1(), 0);
assertEquals(group.getX2(), 3);
assertEquals(group.getY2(), 4);
- wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
- sh = wb.getSheetAt(0);
+ HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
+ wb1.close();
+ sh = wb2.getSheetAt(0);
patriarch = sh.getDrawingPatriarch();
group = (HSSFShapeGroup) patriarch.getChildren().get(0);
assertEquals(group.getY1(), 2);
assertEquals(group.getX2(), 3);
assertEquals(group.getY2(), 4);
+ wb2.close();
}
- public void testAddToExistingFile(){
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sh = wb.createSheet();
+ @Test
+ public void testAddToExistingFile() throws IOException {
+ HSSFWorkbook wb1 = new HSSFWorkbook();
+ HSSFSheet sh = wb1.createSheet();
HSSFPatriarch patriarch = sh.createDrawingPatriarch();
HSSFShapeGroup group1 = patriarch.createGroup(new HSSFClientAnchor());
HSSFShapeGroup group2 = patriarch.createGroup(new HSSFClientAnchor());
group1.setCoordinates(1,2,3,4);
group2.setCoordinates(5,6,7,8);
- wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
- sh = wb.getSheetAt(0);
+ HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
+ wb1.close();
+ sh = wb2.getSheetAt(0);
patriarch = sh.getDrawingPatriarch();
assertEquals(patriarch.getChildren().size(), 2);
HSSFShapeGroup group3 = patriarch.createGroup(new HSSFClientAnchor());
group3.setCoordinates(9,10,11,12);
- wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
- sh = wb.getSheetAt(0);
+ HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2);
+ wb2.close();
+ sh = wb3.getSheetAt(0);
patriarch = sh.getDrawingPatriarch();
assertEquals(patriarch.getChildren().size(), 3);
+ wb3.close();
}
- public void testModify() throws Exception {
- HSSFWorkbook wb = new HSSFWorkbook();
+ @Test
+ public void testModify() throws IOException {
+ HSSFWorkbook wb1 = new HSSFWorkbook();
// create a sheet with a text box
- HSSFSheet sheet = wb.createSheet();
+ HSSFSheet sheet = wb1.createSheet();
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFShapeGroup group1 = patriarch.createGroup(new
textbox1.setString(rt1);
// write, read back and check that our text box is there
- wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
- sheet = wb.getSheetAt(0);
+ HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
+ wb1.close();
+ sheet = wb2.getSheetAt(0);
patriarch = sheet.getDrawingPatriarch();
assertEquals(1, patriarch.getChildren().size());
textbox2.setString(rt2);
assertEquals(2, group1.getChildren().size());
- wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
- sheet = wb.getSheetAt(0);
+ HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2);
+ wb2.close();
+ sheet = wb3.getSheetAt(0);
patriarch = sheet.getDrawingPatriarch();
assertEquals(1, patriarch.getChildren().size());
assertEquals(new HSSFChildAnchor(400, 400, 600, 600),
textbox2.getAnchor());
- wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
- sheet = wb.getSheetAt(0);
+ HSSFWorkbook wb4 = HSSFTestDataSamples.writeOutAndReadBack(wb3);
+ wb3.close();
+ sheet = wb4.getSheetAt(0);
patriarch = sheet.getDrawingPatriarch();
group1 = (HSSFShapeGroup)patriarch.getChildren().get(0);
textbox1 = (HSSFTextbox)group1.getChildren().get(0);
HSSFChildAnchor(400,200, 600, 400));
HSSFRichTextString rt3 = new HSSFRichTextString("Hello, World-3");
textbox3.setString(rt3);
+ wb4.close();
}
- public void testAddShapesToGroup(){
- HSSFWorkbook wb = new HSSFWorkbook();
+ @Test
+ public void testAddShapesToGroup() throws IOException {
+ HSSFWorkbook wb1 = new HSSFWorkbook();
// create a sheet with a text box
- HSSFSheet sheet = wb.createSheet();
+ HSSFSheet sheet = wb1.createSheet();
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor());
- int index = wb.addPicture(new byte[]{1,2,3}, HSSFWorkbook.PICTURE_TYPE_JPEG);
+ int index = wb1.addPicture(new byte[]{1,2,3}, HSSFWorkbook.PICTURE_TYPE_JPEG);
group.createPicture(new HSSFChildAnchor(), index);
HSSFPolygon polygon = group.createPolygon(new HSSFChildAnchor());
polygon.setPoints(new int[]{1,100, 1}, new int[]{1, 50, 100});
group.createTextbox(new HSSFChildAnchor());
group.createShape(new HSSFChildAnchor());
- wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
- sheet = wb.getSheetAt(0);
+ HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
+ wb1.close();
+ sheet = wb2.getSheetAt(0);
patriarch = sheet.getDrawingPatriarch();
assertEquals(1, patriarch.getChildren().size());
HSSFShapeGroup group2 = patriarch.createGroup(new HSSFClientAnchor());
- index = wb.addPicture(new byte[]{2,2,2}, HSSFWorkbook.PICTURE_TYPE_JPEG);
+ index = wb2.addPicture(new byte[]{2,2,2}, HSSFWorkbook.PICTURE_TYPE_JPEG);
group2.createPicture(new HSSFChildAnchor(), index);
polygon = group2.createPolygon(new HSSFChildAnchor());
polygon.setPoints(new int[]{1,100, 1}, new int[]{1, 50, 100});
group2.createShape(new HSSFChildAnchor());
group2.createShape(new HSSFChildAnchor());
- wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
- sheet = wb.getSheetAt(0);
+ HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2);
+ wb2.close();
+ sheet = wb3.getSheetAt(0);
patriarch = sheet.getDrawingPatriarch();
assertEquals(2, patriarch.getChildren().size());
assertTrue(group.getChildren().get(4) instanceof HSSFSimpleShape);
group.getShapeId();
+ wb3.close();
}
- public void testSpgrRecord(){
+ @Test
+ public void testSpgrRecord() throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
// create a sheet with a text box
HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor());
assertSame(((EscherContainerRecord)group.getEscherContainer().getChild(0)).getChildById(EscherSpgrRecord.RECORD_ID), getSpgrRecord(group));
+ wb.close();
}
private static EscherSpgrRecord getSpgrRecord(HSSFShapeGroup group) {
return POITestCase.getFieldValue(HSSFShapeGroup.class, group, EscherSpgrRecord.class, "_spgrRecord");
}
- public void testClearShapes(){
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sheet = wb.createSheet();
+ @Test
+ public void testClearShapes() throws IOException {
+ HSSFWorkbook wb1 = new HSSFWorkbook();
+ HSSFSheet sheet = wb1.createSheet();
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor());
assertEquals(agg.getTailRecords().size(), 0);
assertEquals(group.getChildren().size(), 0);
- wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
- sheet = wb.getSheetAt(0);
+ HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
+ wb1.close();
+ sheet = wb2.getSheetAt(0);
patriarch = sheet.getDrawingPatriarch();
group = (HSSFShapeGroup) patriarch.getChildren().get(0);
assertEquals(agg.getShapeToObjMapping().size(), 1);
assertEquals(agg.getTailRecords().size(), 0);
assertEquals(group.getChildren().size(), 0);
+ wb2.close();
}
}
package org.apache.poi.ss.formula.functions;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.IOException;
+
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.FormulaParseException;
-import org.apache.poi.ss.formula.eval.*;
-
-import junit.framework.TestCase;
-import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.ss.formula.eval.AreaEval;
+import org.apache.poi.ss.formula.eval.ErrorEval;
+import org.apache.poi.ss.formula.eval.NotImplementedException;
+import org.apache.poi.ss.formula.eval.NumberEval;
+import org.apache.poi.ss.formula.eval.ValueEval;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellValue;
+import org.apache.poi.ss.usermodel.FormulaError;
+import org.apache.poi.ss.usermodel.FormulaEvaluator;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.junit.Test;
/**
* Tests for {@link Subtotal}
- *
- * @author Paul Tomlin
*/
-public final class TestSubtotal extends TestCase {
+public final class TestSubtotal {
private static final int FUNCTION_AVERAGE = 1;
private static final int FUNCTION_COUNT = 2;
private static final int FUNCTION_MAX = 4;
assertEquals(expected, ((NumberEval) result).getNumberValue(), 0.0);
}
+ @Test
public void testBasics() {
confirmSubtotal(FUNCTION_SUM, 55.0);
confirmSubtotal(FUNCTION_AVERAGE, 5.5);
confirmSubtotal(FUNCTION_STDEV, 3.0276503540974917);
}
- public void testAvg(){
+ @Test
+ public void testAvg() throws IOException {
Workbook wb = new HSSFWorkbook();
FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
fe.evaluateAll();
- assertEquals(2.0, a3.getNumericCellValue());
- assertEquals(8.0, a6.getNumericCellValue());
- assertEquals(3.0, a7.getNumericCellValue());
- assertEquals(3.0, a8.getNumericCellValue());
+ assertEquals(2.0, a3.getNumericCellValue(), 0);
+ assertEquals(8.0, a6.getNumericCellValue(), 0);
+ assertEquals(3.0, a7.getNumericCellValue(), 0);
+ assertEquals(3.0, a8.getNumericCellValue(), 0);
+ wb.close();
+
}
- public void testSum(){
+ @Test
+ public void testSum() throws IOException {
Workbook wb = new HSSFWorkbook();
FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
fe.evaluateAll();
- assertEquals(4.0, a3.getNumericCellValue());
- assertEquals(26.0, a6.getNumericCellValue());
- assertEquals(12.0, a7.getNumericCellValue());
- assertEquals(12.0, a8.getNumericCellValue());
+ assertEquals(4.0, a3.getNumericCellValue(), 0);
+ assertEquals(26.0, a6.getNumericCellValue(), 0);
+ assertEquals(12.0, a7.getNumericCellValue(), 0);
+ assertEquals(12.0, a8.getNumericCellValue(), 0);
+ wb.close();
}
- public void testCount(){
+ @Test
+ public void testCount() throws IOException {
Workbook wb = new HSSFWorkbook();
fe.evaluateAll();
- assertEquals(2.0, a3.getNumericCellValue());
- assertEquals(6.0, a6.getNumericCellValue());
- assertEquals(2.0, a7.getNumericCellValue());
- assertEquals(2.0, a8.getNumericCellValue());
+ assertEquals(2.0, a3.getNumericCellValue(), 0);
+ assertEquals(6.0, a6.getNumericCellValue(), 0);
+ assertEquals(2.0, a7.getNumericCellValue(), 0);
+ assertEquals(2.0, a8.getNumericCellValue(), 0);
+ wb.close();
}
- public void testCounta(){
+ @Test
+ public void testCounta() throws IOException {
Workbook wb = new HSSFWorkbook();
fe.evaluateAll();
- assertEquals(2.0, a3.getNumericCellValue());
- assertEquals(8.0, a6.getNumericCellValue());
- assertEquals(3.0, a7.getNumericCellValue());
- assertEquals(3.0, a8.getNumericCellValue());
+ assertEquals(2.0, a3.getNumericCellValue(), 0);
+ assertEquals(8.0, a6.getNumericCellValue(), 0);
+ assertEquals(3.0, a7.getNumericCellValue(), 0);
+ assertEquals(3.0, a8.getNumericCellValue(), 0);
+ wb.close();
}
- public void testMax(){
+ @Test
+ public void testMax() throws IOException {
Workbook wb = new HSSFWorkbook();
fe.evaluateAll();
- assertEquals(3.0, a3.getNumericCellValue());
- assertEquals(16.0, a6.getNumericCellValue());
- assertEquals(7.0, a7.getNumericCellValue());
- assertEquals(7.0, a8.getNumericCellValue());
+ assertEquals(3.0, a3.getNumericCellValue(), 0);
+ assertEquals(16.0, a6.getNumericCellValue(), 0);
+ assertEquals(7.0, a7.getNumericCellValue(), 0);
+ assertEquals(7.0, a8.getNumericCellValue(), 0);
+ wb.close();
}
- public void testMin(){
+ @Test
+ public void testMin() throws IOException {
Workbook wb = new HSSFWorkbook();
fe.evaluateAll();
- assertEquals(1.0, a3.getNumericCellValue());
- assertEquals(4.0, a6.getNumericCellValue());
- assertEquals(1.0, a7.getNumericCellValue());
- assertEquals(1.0, a8.getNumericCellValue());
+ assertEquals(1.0, a3.getNumericCellValue(), 0);
+ assertEquals(4.0, a6.getNumericCellValue(), 0);
+ assertEquals(1.0, a7.getNumericCellValue(), 0);
+ assertEquals(1.0, a8.getNumericCellValue(), 0);
+ wb.close();
}
- public void testStdev(){
+ @Test
+ public void testStdev() throws IOException {
Workbook wb = new HSSFWorkbook();
assertEquals(7.65685, a6.getNumericCellValue(), 0.0001);
assertEquals(2.82842, a7.getNumericCellValue(), 0.0001);
assertEquals(2.82842, a8.getNumericCellValue(), 0.0001);
+ wb.close();
}
- public void test50209(){
+ @Test
+ public void test50209() throws IOException {
Workbook wb = new HSSFWorkbook();
Sheet sh = wb.createSheet();
Cell a1 = sh.createRow(1).createCell(1);
FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
fe.evaluateAll();
- assertEquals(1.0, a2.getNumericCellValue());
- assertEquals(1.0, a3.getNumericCellValue());
+ assertEquals(1.0, a2.getNumericCellValue(), 0);
+ assertEquals(1.0, a3.getNumericCellValue(), 0);
+ wb.close();
}
private static void confirmExpectedResult(FormulaEvaluator evaluator, String msg, Cell cell, double expected) {
CellValue value = evaluator.evaluate(cell);
if (value.getErrorValue() != 0)
throw new RuntimeException(msg + ": " + value.formatAsString());
- assertEquals(msg, expected, value.getNumberValue());
+ assertEquals(msg, expected, value.getNumberValue(), 0);
}
- public void testFunctionsFromTestSpreadsheet() {
+ @Test
+ public void testFunctionsFromTestSpreadsheet() throws IOException {
HSSFWorkbook workbook = HSSFTestDataSamples.openSampleWorkbook("SubtotalsNested.xls");
HSSFSheet sheet = workbook.getSheetAt(0);
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
- assertEquals("B2", 10.0, sheet.getRow(1).getCell(1).getNumericCellValue());
- assertEquals("B3", 20.0, sheet.getRow(2).getCell(1).getNumericCellValue());
+ assertEquals("B2", 10.0, sheet.getRow(1).getCell(1).getNumericCellValue(), 0);
+ assertEquals("B3", 20.0, sheet.getRow(2).getCell(1).getNumericCellValue(), 0);
//Test simple subtotal over one area
Cell cellA3 = sheet.getRow(3).getCell(1);
//Test existence of the second area
assertNotNull("C2 must not be null", sheet.getRow(1).getCell(2));
- assertEquals("C2", 7.0, sheet.getRow(1).getCell(2).getNumericCellValue());
+ assertEquals("C2", 7.0, sheet.getRow(1).getCell(2).getNumericCellValue(), 0);
Cell cellC1 = sheet.getRow(1).getCell(3);
Cell cellC2 = sheet.getRow(2).getCell(3);
confirmExpectedResult(evaluator, "SUBTOTAL(SUM;B2:B8;C2:C8)", cellC1, 37.0);
confirmExpectedResult(evaluator, "SUBTOTAL(COUNT;B2:B8,C2:C8)", cellC2, 3.0);
confirmExpectedResult(evaluator, "SUBTOTAL(COUNTA;B2:B8,C2:C8)", cellC3, 5.0);
+
+ workbook.close();
}
- public void testUnimplemented(){
+ @Test
+ public void testUnimplemented() throws IOException {
Workbook wb = new HSSFWorkbook();
FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
Sheet sh = wb.createSheet();
Cell a3 = sh.createRow(3).createCell(1);
- a3.setCellFormula("SUBTOTAL(8,B2:B3)");
-
- try {
- fe.evaluateAll();
- fail("Should catch an NotImplementedFunctionException here, adjust these tests if it was actually implemented");
- } catch (NotImplementedException e) {
- // expected here
- }
-
- a3.setCellFormula("SUBTOTAL(10,B2:B3)");
-
- try {
- fe.evaluateAll();
- fail("Should catch an NotImplementedFunctionException here, adjust these tests if it was actually implemented");
- } catch (NotImplementedException e) {
- // expected here
- }
-
- a3.setCellFormula("SUBTOTAL(11,B2:B3)");
-
- try {
- fe.evaluateAll();
- fail("Should catch an NotImplementedFunctionException here, adjust these tests if it was actually implemented");
- } catch (NotImplementedException e) {
- // expected here
- }
-
- a3.setCellFormula("SUBTOTAL(107,B2:B3)");
-
- try {
- fe.evaluateAll();
- fail("Should catch an NotImplementedFunctionException here, adjust these tests if it was actually implemented");
- } catch (NotImplementedException e) {
- // expected here
- }
-
- a3.setCellFormula("SUBTOTAL(0,B2:B3)");
- fe.evaluateAll();
- assertEquals(FormulaError.VALUE.getCode(), a3.getErrorCellValue());
-
- try {
- a3.setCellFormula("SUBTOTAL(9)");
- fail("Should catch an exception here");
- } catch (FormulaParseException e) {
- // expected here
- }
-
- try {
- a3.setCellFormula("SUBTOTAL()");
- fail("Should catch an exception here");
- } catch (FormulaParseException e) {
- // expected here
+
+ // formula, throws NotImplemnted?
+ String[][] formulas = {
+ { "SUBTOTAL(8,B2:B3)", NotImplementedException.class.getName() },
+ { "SUBTOTAL(10,B2:B3)", NotImplementedException.class.getName() },
+ { "SUBTOTAL(11,B2:B3)", NotImplementedException.class.getName() },
+ { "SUBTOTAL(107,B2:B3)", NotImplementedException.class.getName() },
+ { "SUBTOTAL(0,B2:B3)", null },
+ { "SUBTOTAL(9)", FormulaParseException.class.getName() },
+ { "SUBTOTAL()", FormulaParseException.class.getName() },
+ };
+
+ for (String[] f : formulas) {
+ Exception actualEx = null;
+ try {
+ a3.setCellFormula(f[0]);
+ fe.evaluateAll();
+ assertEquals(FormulaError.VALUE.getCode(), a3.getErrorCellValue());
+ } catch (Exception e) {
+ actualEx = e;
+ }
+ String msg =
+ "Check "+(f[1] == null ? "unexpected exception" : f[1])+" here, "+
+ "adjust these tests if it was actually implemented - "+f[0];
+ assertEquals(msg, f[1], (actualEx == null ? null : actualEx.getClass().getName()));
}
Subtotal subtotal = new Subtotal();
assertEquals(ErrorEval.VALUE_INVALID, subtotal.evaluate(new ValueEval[] {}, 0, 0));
+
+ wb.close();
}
}