Sonar fixes

add asserts to tests

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1885819 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2021-01-22 23:00:51 +00:00
parent 1b55a7930e
commit 0e5f513830
12 changed files with 294 additions and 307 deletions

View File

@ -20,9 +20,6 @@ package org.apache.poi.xssf.usermodel;
import org.apache.poi.ss.usermodel.BaseTestSheetAutosizeColumn;
import org.apache.poi.xssf.XSSFITestDataProvider;
/**
* @author Yegor Kozlov
*/
public final class TestXSSFSheetAutosizeColumn extends BaseTestSheetAutosizeColumn {
public TestXSSFSheetAutosizeColumn(){

View File

@ -172,8 +172,7 @@ class TestXWPFTableRow {
@Test
void testBug62174() throws IOException {
try (XWPFDocument doc = XWPFTestDataSamples
.openSampleDocument("Bug60337.docx")) {
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug60337.docx")) {
XWPFTable table = doc.getTables().get(0);
XWPFTableRow tr = table.getRow(0);

View File

@ -20,6 +20,9 @@ package org.apache.poi.hdgf;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.POIDataSamples;
import org.apache.poi.hdgf.extractor.VisioTextExtractor;
import org.apache.poi.hdgf.streams.PointerContainingStream;
@ -30,49 +33,40 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public final class TestHDGFCore {
private static POIDataSamples _dgTests = POIDataSamples.getDiagramInstance();
private POIFSFileSystem fs;
private HDGFDiagram hdgf;
private VisioTextExtractor textExtractor;
@BeforeEach
void setUp() throws Exception {
fs = new POIFSFileSystem(_dgTests.openResourceAsStream("Test_Visio-Some_Random_Text.vsd"));
}
@AfterEach
void tearDown() throws Exception {
if (textExtractor != null) textExtractor.close();
if (hdgf != null) hdgf.close();
}
private static final POIDataSamples SAMPLES = POIDataSamples.getDiagramInstance();
@Test
void testCreate() throws Exception {
hdgf = new HDGFDiagram(fs);
try (POIFSFileSystem fs = openFS("Test_Visio-Some_Random_Text.vsd");
HDGFDiagram hdgf = new HDGFDiagram(fs)) {
assertNotNull(hdgf);
}
}
@Test
void testTrailer() throws Exception {
hdgf = new HDGFDiagram(fs);
assertNotNull(hdgf);
assertNotNull(hdgf.getTrailerStream());
try (POIFSFileSystem fs = openFS("Test_Visio-Some_Random_Text.vsd");
HDGFDiagram hdgf = new HDGFDiagram(fs)) {
assertNotNull(hdgf);
assertNotNull(hdgf.getTrailerStream());
// Check it has what we'd expect
TrailerStream trailer = hdgf.getTrailerStream();
assertEquals(0x8a94, trailer.getPointer().getOffset());
// Check it has what we'd expect
TrailerStream trailer = hdgf.getTrailerStream();
assertEquals(0x8a94, trailer.getPointer().getOffset());
assertNotNull(trailer.getPointedToStreams());
assertEquals(20, trailer.getPointedToStreams().length);
assertNotNull(trailer.getPointedToStreams());
assertEquals(20, trailer.getPointedToStreams().length);
assertEquals(20, hdgf.getTopLevelStreams().length);
assertEquals(20, hdgf.getTopLevelStreams().length);
// 9th one should have children
assertNotNull(trailer.getPointedToStreams()[8]);
assertNotNull(trailer.getPointedToStreams()[8].getPointer());
PointerContainingStream ps8 = (PointerContainingStream)
// 9th one should have children
assertNotNull(trailer.getPointedToStreams()[8]);
assertNotNull(trailer.getPointedToStreams()[8].getPointer());
PointerContainingStream ps8 = (PointerContainingStream)
trailer.getPointedToStreams()[8];
assertNotNull(ps8.getPointedToStreams());
assertEquals(8, ps8.getPointedToStreams().length);
assertNotNull(ps8.getPointedToStreams());
assertEquals(8, ps8.getPointedToStreams().length);
}
}
/**
@ -81,15 +75,16 @@ public final class TestHDGFCore {
*/
@Test
void testNegativeChunkLength() throws Exception {
fs = new POIFSFileSystem(_dgTests.openResourceAsStream("NegativeChunkLength.vsd"));
hdgf = new HDGFDiagram(fs);
assertNotNull(hdgf);
try (POIFSFileSystem fs = openFS("NegativeChunkLength.vsd");
HDGFDiagram hdgf = new HDGFDiagram(fs)) {
assertNotNull(hdgf);
}
// And another file
fs = new POIFSFileSystem(_dgTests.openResourceAsStream("NegativeChunkLength2.vsd"));
hdgf = new HDGFDiagram(fs);
assertNotNull(hdgf);
try (POIFSFileSystem fs = openFS("NegativeChunkLength2.vsd");
HDGFDiagram hdgf = new HDGFDiagram(fs)) {
assertNotNull(hdgf);
}
}
/**
@ -98,49 +93,55 @@ public final class TestHDGFCore {
* chunk commands.
*/
@Test
void DISABLEDtestAIOOB() throws Exception {
fs = new POIFSFileSystem(_dgTests.openResourceAsStream("44501.vsd"));
hdgf = new HDGFDiagram(fs);
assertNotNull(hdgf);
void testAIOOB() throws Exception {
try (POIFSFileSystem fs = openFS("44501.vsd");
HDGFDiagram hdgf = new HDGFDiagram(fs)) {
assertNotNull(hdgf);
}
}
@Test
void testV5() throws Exception {
fs = new POIFSFileSystem(_dgTests.openResourceAsStream("v5_Connection_Types.vsd"));
try (POIFSFileSystem fs = openFS("v5_Connection_Types.vsd");
HDGFDiagram hdgf = new HDGFDiagram(fs)) {
assertNotNull(hdgf);
hdgf = new HDGFDiagram(fs);
assertNotNull(hdgf);
textExtractor = new VisioTextExtractor(hdgf);
String text = textExtractor.getText().replace("\u0000", "").trim();
assertEquals("Static to Static\nDynamic to Static\nDynamic to Dynamic", text);
try (VisioTextExtractor textExtractor = new VisioTextExtractor(hdgf)) {
String text = textExtractor.getText().replace("\u0000", "").trim();
assertEquals("Static to Static\nDynamic to Static\nDynamic to Dynamic", text);
}
}
}
@Test
void testV6NonUtf16LE() throws Exception {
fs = new POIFSFileSystem(_dgTests.openResourceAsStream("v6-non-utf16le.vsd"));
try (POIFSFileSystem fs = openFS("v6-non-utf16le.vsd");
HDGFDiagram hdgf = new HDGFDiagram(fs)) {
assertNotNull(hdgf);
hdgf = new HDGFDiagram(fs);
assertNotNull(hdgf);
textExtractor = new VisioTextExtractor(hdgf);
String text = textExtractor.getText().replace("\u0000", "").trim();
assertEquals("Table\n\n\nPropertySheet\n\n\n\nPropertySheetField", text);
try (VisioTextExtractor textExtractor = new VisioTextExtractor(hdgf)) {
String text = textExtractor.getText().replace("\u0000", "").trim();
assertEquals("Table\n\n\nPropertySheet\n\n\n\nPropertySheetField", text);
}
}
}
@Test
void testUtf16LE() throws Exception {
fs = new POIFSFileSystem(_dgTests.openResourceAsStream("Test_Visio-Some_Random_Text.vsd"));
try (POIFSFileSystem fs = openFS("Test_Visio-Some_Random_Text.vsd");
HDGFDiagram hdgf = new HDGFDiagram(fs)) {
assertNotNull(hdgf);
hdgf = new HDGFDiagram(fs);
assertNotNull(hdgf);
try (VisioTextExtractor textExtractor = new VisioTextExtractor(hdgf)) {
String text = textExtractor.getText().trim();
assertEquals("text\nView\nTest View\nI am a test view\nSome random text, on a page", text);
}
}
}
textExtractor = new VisioTextExtractor(hdgf);
String text = textExtractor.getText().trim();
assertEquals("text\nView\nTest View\nI am a test view\nSome random text, on a page", text);
private POIFSFileSystem openFS(String file) throws IOException {
try (InputStream is = SAMPLES.openResourceAsStream(file)) {
return new POIFSFileSystem(is);
}
}
}

View File

@ -20,32 +20,26 @@
==================================================================== */
package org.apache.poi.hdgf.dev;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import java.io.File;
import java.io.PrintStream;
import org.apache.poi.POIDataSamples;
import org.apache.poi.util.NullPrintStream;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
public class TestVSDDumper {
private static PrintStream oldStdOut;
@BeforeAll
public static void muteStdout() {
oldStdOut = System.out;
System.setOut(new NullPrintStream());
}
@AfterAll
public static void restoreStdout() {
System.setOut(oldStdOut);
}
@Test
void main() throws Exception {
File file = POIDataSamples.getDiagramInstance().getFile("Test_Visio-Some_Random_Text.vsd");
VSDDumper.main(new String[] { file.getAbsolutePath() });
void main() {
PrintStream oldStdOut = System.out;
System.setOut(new NullPrintStream());
try {
File file = POIDataSamples.getDiagramInstance().getFile("Test_Visio-Some_Random_Text.vsd");
String[] args = { file.getAbsolutePath() };
assertDoesNotThrow(() -> VSDDumper.main(args));
} finally {
System.setOut(oldStdOut);
}
}
}

View File

@ -27,6 +27,8 @@ import org.apache.poi.POIDataSamples;
import org.apache.poi.hdgf.HDGFDiagram;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
public final class TestVisioExtractor {
private static final POIDataSamples _dgTests = POIDataSamples.getDiagramInstance();
@ -39,70 +41,61 @@ public final class TestVisioExtractor {
*/
@Test
void testCreation() throws IOException {
VisioTextExtractor extractor1 = openExtractor(defFilename);
assertNotNull(extractor1);
assertNotNull(extractor1.getAllText());
assertEquals(defTextChunks, extractor1.getAllText().length);
extractor1.close();
try (VisioTextExtractor extractor1 = openExtractor(defFilename)) {
assertNotNull(extractor1);
assertNotNull(extractor1.getAllText());
assertEquals(defTextChunks, extractor1.getAllText().length);
}
InputStream is2 = _dgTests.openResourceAsStream(defFilename);
POIFSFileSystem poifs2 = new POIFSFileSystem(is2);
is2.close();
VisioTextExtractor extractor2 = new VisioTextExtractor(poifs2);
assertNotNull(extractor2);
assertNotNull(extractor2.getAllText());
assertEquals(defTextChunks, extractor2.getAllText().length);
extractor2.close();
poifs2.close();
try (InputStream is2 = _dgTests.openResourceAsStream(defFilename);
POIFSFileSystem poifs2 = new POIFSFileSystem(is2);
VisioTextExtractor extractor2 = new VisioTextExtractor(poifs2)) {
assertNotNull(extractor2);
assertNotNull(extractor2.getAllText());
assertEquals(defTextChunks, extractor2.getAllText().length);
}
InputStream is3 = _dgTests.openResourceAsStream(defFilename);
POIFSFileSystem poifs3 = new POIFSFileSystem(is3);
is3.close();
HDGFDiagram hdgf3 = new HDGFDiagram(poifs3);
VisioTextExtractor extractor3 = new VisioTextExtractor(hdgf3);
assertNotNull(extractor3);
assertNotNull(extractor3.getAllText());
assertEquals(defTextChunks, extractor3.getAllText().length);
extractor3.close();
hdgf3.close();
poifs3.close();
try (InputStream is3 = _dgTests.openResourceAsStream(defFilename);
POIFSFileSystem poifs3 = new POIFSFileSystem(is3);
HDGFDiagram hdgf3 = new HDGFDiagram(poifs3);
VisioTextExtractor extractor3 = new VisioTextExtractor(hdgf3)) {
assertNotNull(extractor3);
assertNotNull(extractor3.getAllText());
assertEquals(defTextChunks, extractor3.getAllText().length);
}
}
@Test
void testExtraction() throws Exception {
VisioTextExtractor extractor = openExtractor(defFilename);
try (VisioTextExtractor extractor = openExtractor(defFilename)) {
// Check the array fetch
String[] text = extractor.getAllText();
assertNotNull(text);
assertEquals(defTextChunks, text.length);
// Check the array fetch
String[] text = extractor.getAllText();
assertNotNull(text);
assertEquals(defTextChunks, text.length);
assertEquals("text\n", text[0]);
assertEquals("View\n", text[1]);
assertEquals("Test View\n", text[2]);
assertEquals("I am a test view\n", text[3]);
assertEquals("Some random text, on a page\n", text[4]);
assertEquals("text\n", text[0]);
assertEquals("View\n", text[1]);
assertEquals("Test View\n", text[2]);
assertEquals("I am a test view\n", text[3]);
assertEquals("Some random text, on a page\n", text[4]);
// And the all-in fetch
String textS = extractor.getText();
assertEquals("text\nView\nTest View\nI am a test view\nSome random text, on a page\n", textS);
extractor.close();
// And the all-in fetch
String textS = extractor.getText();
assertEquals("text\nView\nTest View\nI am a test view\nSome random text, on a page\n", textS);
}
}
@Test
void testProblemFiles() throws Exception {
String[] files = {
"44594.vsd", "44594-2.vsd",
"ShortChunk1.vsd", "ShortChunk2.vsd", "ShortChunk3.vsd",
"NegativeChunkLength.vsd", "NegativeChunkLength2.vsd"
};
for(String file : files){
VisioTextExtractor ex = openExtractor(file);
ex.getText();
ex.close();
}
@ParameterizedTest
@ValueSource(strings = {
"44594.vsd", "44594-2.vsd",
"ShortChunk1.vsd", "ShortChunk2.vsd", "ShortChunk3.vsd",
"NegativeChunkLength.vsd", "NegativeChunkLength2.vsd"
})
void testProblemFiles(String file) throws Exception {
try (VisioTextExtractor ex = openExtractor(file)) {
assertNotNull(ex.getText());
}
}
private VisioTextExtractor openExtractor(String fileName) throws IOException {

View File

@ -17,6 +17,9 @@
package org.apache.poi.hdgf.streams;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.IOException;
import java.io.InputStream;
@ -44,14 +47,14 @@ public final class TestStreamBugs extends StreamTest {
ptrFactory = new PointerFactory(11);
chunkFactory = new ChunkFactory(11);
InputStream is = POIDataSamples.getDiagramInstance().openResourceAsStream("44594.vsd");
filesystem = new POIFSFileSystem(is);
is.close();
try (InputStream is = POIDataSamples.getDiagramInstance().openResourceAsStream("44594.vsd")) {
filesystem = new POIFSFileSystem(is);
}
// Grab the document stream
InputStream is2 = filesystem.createDocumentInputStream("VisioDocument");
contents = IOUtils.toByteArray(is2);
is2.close();
try (InputStream is2 = filesystem.createDocumentInputStream("VisioDocument")) {
contents = IOUtils.toByteArray(is2);
}
}
@Test
@ -80,22 +83,19 @@ public final class TestStreamBugs extends StreamTest {
// Get with recursing into chunks
for (Pointer ptr : ptrs) {
Stream stream =
Stream.createStream(ptr, contents, chunkFactory, ptrFactory);
Stream stream = Stream.createStream(ptr, contents, chunkFactory, ptrFactory);
if(stream instanceof ChunkStream) {
ChunkStream cStream = (ChunkStream)stream;
cStream.findChunks();
assertDoesNotThrow(cStream::findChunks);
}
}
// Get with recursing into chunks and pointers
for (Pointer ptr : ptrs) {
Stream stream =
Stream.createStream(ptr, contents, chunkFactory, ptrFactory);
Stream stream = Stream.createStream(ptr, contents, chunkFactory, ptrFactory);
if(stream instanceof PointerContainingStream) {
PointerContainingStream pStream =
(PointerContainingStream)stream;
pStream.findChildren(contents);
PointerContainingStream pStream = (PointerContainingStream)stream;
assertDoesNotThrow(() -> pStream.findChildren(contents));
}
}
@ -104,6 +104,8 @@ public final class TestStreamBugs extends StreamTest {
@Test
void testOpen() throws IOException {
new HDGFDiagram(filesystem).close();
try (HDGFDiagram dia = new HDGFDiagram(filesystem)) {
assertEquals(20, dia.getTopLevelStreams().length);
}
}
}

View File

@ -21,6 +21,7 @@
package org.apache.poi.hmef.dev;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.File;
@ -39,13 +40,13 @@ public class TestHMEFDumper {
@Test
void main() throws Exception {
File file = POIDataSamples.getHMEFInstance().getFile("quick-winmail.dat");
doMain(file.getAbsolutePath());
assertDoesNotThrow(() -> doMain(file.getAbsolutePath()));
}
@Test
void mainFull() throws Exception {
File file = POIDataSamples.getHMEFInstance().getFile("quick-winmail.dat");
doMain("--full", file.getAbsolutePath());
assertDoesNotThrow(() -> doMain("--full", file.getAbsolutePath()));
}
private static void doMain(String... args) throws Exception {

View File

@ -17,6 +17,9 @@
package org.apache.poi.hslf;
import static org.apache.poi.hslf.usermodel.HSLFSlideShow.POWERPOINT_DOCUMENT;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@ -26,121 +29,89 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.POIDataSamples;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.TempFile;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
/**
* Tests that HSLFSlideShow writes the powerpoint bit of data back out
* correctly. Currently, that means being the same as what it read in
*
* @author Nick Burch (nick at torchbox dot com)
*/
public final class TestReWrite {
// HSLFSlideShow primed on the test data
private HSLFSlideShowImpl hssA;
private HSLFSlideShowImpl hssB;
private HSLFSlideShowImpl hssC;
// POIFS primed on the test data
private POIFSFileSystem pfsA;
private POIFSFileSystem pfsB;
private POIFSFileSystem pfsC;
private static final POIDataSamples SAMPLES = POIDataSamples.getSlideShowInstance();
@BeforeEach
void setUp() throws Exception {
@ParameterizedTest
@ValueSource(strings = { "basic_test_ppt_file.ppt", "ParagraphStylesShorterThanCharStyles.ppt" })
void testWritesOutTheSame(String testfile) throws Exception {
try (POIFSFileSystem pfs = new POIFSFileSystem(SAMPLES.openResourceAsStream(testfile));
HSLFSlideShowImpl hss = new HSLFSlideShowImpl(pfs)) {
POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
// Write out to a byte array, and to a temp file
ByteArrayOutputStream baos = new ByteArrayOutputStream();
hss.write(baos);
pfsA = new POIFSFileSystem(slTests.openResourceAsStream("basic_test_ppt_file.ppt"));
hssA = new HSLFSlideShowImpl(pfsA);
pfsB = new POIFSFileSystem(slTests.openResourceAsStream("ParagraphStylesShorterThanCharStyles.ppt"));
hssB = new HSLFSlideShowImpl(pfsB);
pfsC = new POIFSFileSystem(slTests.openResourceAsStream("WithMacros.ppt"));
hssC = new HSLFSlideShowImpl(pfsC);
}
@Test
void testWritesOutTheSame() throws Exception {
assertWritesOutTheSame(hssA, pfsA);
assertWritesOutTheSame(hssB, pfsB);
}
void assertWritesOutTheSame(HSLFSlideShowImpl hss, POIFSFileSystem pfs) throws Exception {
// Write out to a byte array, and to a temp file
ByteArrayOutputStream baos = new ByteArrayOutputStream();
hss.write(baos);
final File file = TempFile.createTempFile("TestHSLF", ".ppt");
final File file2 = TempFile.createTempFile("TestHSLF", ".ppt");
hss.write(file);
hss.write(file2);
final File file = TempFile.createTempFile("TestHSLF", ".ppt");
final File file2 = TempFile.createTempFile("TestHSLF", ".ppt");
hss.write(file);
hss.write(file2);
// Build an input stream of it, and read back as a POIFS from the stream
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
POIFSFileSystem npfS = new POIFSFileSystem(bais);
// Build an input stream of it, and read back as a POIFS from the stream
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
try (POIFSFileSystem npfS = new POIFSFileSystem(bais);
// And the same on the temp file
POIFSFileSystem npfF = new POIFSFileSystem(file)) {
// And the same on the temp file
POIFSFileSystem npfF = new POIFSFileSystem(file);
// And another where we do an in-place write
POIFSFileSystem npfRF = new POIFSFileSystem(file2, false);
HSLFSlideShowImpl hssRF = new HSLFSlideShowImpl(npfRF);
hssRF.write();
hssRF.close();
npfRF = new POIFSFileSystem(file2);
// 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(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(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]);
// And another where we do an in-place write
try (POIFSFileSystem npfRF = new POIFSFileSystem(file2, false);
HSLFSlideShowImpl hssRF = new HSLFSlideShowImpl(npfRF)) {
hssRF.write();
}
try (POIFSFileSystem npfRF = new POIFSFileSystem(file2)) {
// Check all of them in turn
for (POIFSFileSystem npf : new POIFSFileSystem[]{npfS, npfF, npfRF}) {
assertSame(pfs, npf);
}
}
}
npf.close();
}
}
@Test
void testWithMacroStreams() throws IOException {
// Check that they're apparently the same
assertSlideShowWritesOutTheSame(hssC, pfsC);
try (POIFSFileSystem pfsC = new POIFSFileSystem(SAMPLES.openResourceAsStream("WithMacros.ppt"));
HSLFSlideShowImpl hssC = new HSLFSlideShowImpl(pfsC)) {
// Check that they're apparently the same
assertSlideShowWritesOutTheSame(hssC, pfsC);
// Currently has a Macros stream
assertNotNull( pfsC.getRoot().getEntry("Macros") );
// Currently has a Macros stream
assertNotNull(pfsC.getRoot().getEntry("Macros"));
// Write out normally, will loose the macro stream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
hssC.write(baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
POIFSFileSystem pfsNew = new POIFSFileSystem(bais);
assertFalse(pfsNew.getRoot().hasEntry("Macros"));
pfsNew.close();
// Write out normally, will loose the macro stream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
hssC.write(baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
try (POIFSFileSystem pfsNew = new POIFSFileSystem(bais)) {
assertFalse(pfsNew.getRoot().hasEntry("Macros"));
}
// But if we write out with nodes preserved, will be there
baos.reset();
hssC.write(baos, true);
bais = new ByteArrayInputStream(baos.toByteArray());
pfsNew = new POIFSFileSystem(bais);
assertTrue( pfsNew.getRoot().hasEntry("Macros") );
pfsNew.close();
// But if we write out with nodes preserved, will be there
baos.reset();
hssC.write(baos, true);
bais = new ByteArrayInputStream(baos.toByteArray());
try (POIFSFileSystem pfsNew = new POIFSFileSystem(bais)) {
assertTrue(pfsNew.getRoot().hasEntry("Macros"));
}
}
}
/**
@ -149,7 +120,10 @@ public final class TestReWrite {
*/
@Test
void testSlideShowWritesOutTheSame() throws Exception {
assertSlideShowWritesOutTheSame(hssA, pfsA);
try (POIFSFileSystem pfsA = new POIFSFileSystem(SAMPLES.openResourceAsStream("basic_test_ppt_file.ppt"));
HSLFSlideShowImpl hssA = new HSLFSlideShowImpl(pfsA)) {
assertSlideShowWritesOutTheSame(hssA, pfsA);
}
// Some bug in StyleTextPropAtom rewriting means this will fail
// We need to identify and fix that first
@ -160,8 +134,8 @@ public final class TestReWrite {
// Create a slideshow covering it
@SuppressWarnings("resource")
HSLFSlideShow ss = new HSLFSlideShow(hss);
ss.getSlides();
ss.getNotes();
assertDoesNotThrow(ss::getSlides);
assertDoesNotThrow(ss::getNotes);
// Now write out to a byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
@ -171,40 +145,46 @@ public final class TestReWrite {
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
// Use POIFS to query that lot
POIFSFileSystem npfs = new POIFSFileSystem(bais);
try (POIFSFileSystem npfs = new POIFSFileSystem(bais)) {
assertSame(pfs, npfs);
}
}
private void assertSame(POIFSFileSystem origPFS, POIFSFileSystem newPFS) throws IOException {
// Check that the "PowerPoint Document" sections have the same size
DocumentEntry oProps = (DocumentEntry)pfs.getRoot().getEntry(HSLFSlideShow.POWERPOINT_DOCUMENT);
DocumentEntry nProps = (DocumentEntry)npfs.getRoot().getEntry(HSLFSlideShow.POWERPOINT_DOCUMENT);
assertEquals(oProps.getSize(),nProps.getSize());
DocumentEntry oProps = (DocumentEntry) origPFS.getRoot().getEntry(POWERPOINT_DOCUMENT);
DocumentEntry nProps = (DocumentEntry) newPFS.getRoot().getEntry(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(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));
assertEquals(_oData[i], _nData[i]);
try (InputStream os = origPFS.createDocumentInputStream(POWERPOINT_DOCUMENT);
InputStream ns = newPFS.createDocumentInputStream(POWERPOINT_DOCUMENT)) {
byte[] _oData = IOUtils.toByteArray(os, oProps.getSize());
byte[] _nData = IOUtils.toByteArray(ns, nProps.getSize());
assertArrayEquals(_oData, _nData);
}
npfs.close();
}
@Test
void test48593() throws IOException {
HSLFSlideShow ppt1 = new HSLFSlideShow();
ppt1.createSlide();
HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1);
ppt2.createSlide();
HSLFSlideShow ppt3 = HSLFTestDataSamples.writeOutAndReadBack(ppt2);
ppt3.createSlide();
HSLFSlideShow ppt4 = HSLFTestDataSamples.writeOutAndReadBack(ppt3);
ppt4.createSlide();
HSLFTestDataSamples.writeOutAndReadBack(ppt4).close();
ppt4.close();
ppt3.close();
ppt2.close();
ppt1.close();
try (HSLFSlideShow ppt1 = new HSLFSlideShow()) {
ppt1.createSlide();
try (HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1)) {
ppt2.createSlide();
try (HSLFSlideShow ppt3 = HSLFTestDataSamples.writeOutAndReadBack(ppt2)) {
ppt3.createSlide();
try (HSLFSlideShow ppt4 = HSLFTestDataSamples.writeOutAndReadBack(ppt3)) {
ppt4.createSlide();
try (HSLFSlideShow ppt5 = HSLFTestDataSamples.writeOutAndReadBack(ppt4)) {
assertEquals(4, ppt5.getSlides().size());
}
}
}
}
}
}
}

View File

@ -29,15 +29,20 @@ import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.util.BitSet;
import java.util.List;
import com.zaxxer.sparsebits.SparseBitSet;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.poi.POIDataSamples;
import org.apache.poi.hslf.usermodel.HSLFObjectShape;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.poifs.crypt.CryptoFunctions;
import org.apache.poi.poifs.crypt.HashAlgorithm;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.sl.extractor.SlideShowExtractor;
@ -45,6 +50,7 @@ import org.apache.poi.sl.usermodel.ObjectShape;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.sl.usermodel.SlideShowFactory;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.NullOutputStream;
import org.junit.jupiter.api.Test;
/**
@ -231,9 +237,15 @@ public final class TestExtractor {
@Test
void test52991() throws IOException {
try (SlideShowExtractor<?,?> ppe = openExtractor("badzip.ppt")) {
for (ObjectShape<?,?> shape : ppe.getOLEShapes()) {
IOUtils.copy(shape.getObjectData().getInputStream(), new ByteArrayOutputStream());
List<? extends ObjectShape<?, ?>> shapes = ppe.getOLEShapes();
assertEquals(1, shapes.size());
MessageDigest sha2 = CryptoFunctions.getMessageDigest(HashAlgorithm.sha256);
try (InputStream is = shapes.get(0).getObjectData().getInputStream()) {
sha2.update(IOUtils.toByteArray(is));
}
String exp = "lIRRfGMin6B4++WR4XvA82usdQ3ijeHBHU85j523sKY=";
String act = Base64.encodeBase64String(sha2.digest());
assertEquals(exp, act);
}
}

View File

@ -20,6 +20,7 @@ package org.apache.poi.hslf.usermodel;
import static org.apache.poi.POITestCase.assertContains;
import static org.apache.poi.POITestCase.assertStartsWith;
import static org.apache.poi.hslf.HSLFTestDataSamples.writeOutAndReadBack;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@ -446,7 +447,7 @@ public final class TestBugs {
/* Iterate over slides and extract text */
for (HSLFSlide slide : ppt.getSlides()) {
HeadersFooters hf = slide.getHeadersFooters();
hf.isHeaderVisible(); // exception happens here
assertDoesNotThrow(hf::isHeaderVisible);
}
}
}

View File

@ -22,8 +22,6 @@ import org.apache.poi.ss.usermodel.BaseTestSheetAutosizeColumn;
/**
* Test auto-sizing columns in HSSF
*
* @author Yegor Kozlov
*/
final class TestHSSFSheetAutosizeColumn extends BaseTestSheetAutosizeColumn {

View File

@ -25,6 +25,7 @@ import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.ITestDataProvider;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.LocaleUtil;
@ -292,41 +293,47 @@ public abstract class BaseTestSheetAutosizeColumn {
*/
@Test
void largeRowNumbers() throws Exception {
Workbook workbook = _testDataProvider.createWorkbook();
Sheet sheet = workbook.createSheet();
trackColumnsForAutoSizingIfSXSSF(sheet);
try (Workbook workbook = _testDataProvider.createWorkbook()) {
boolean isHssf = workbook instanceof HSSFWorkbook;
Sheet sheet = workbook.createSheet();
trackColumnsForAutoSizingIfSXSSF(sheet);
Row r0 = sheet.createRow(0);
r0.createCell(0).setCellValue("I am ROW 0");
Row r200 = sheet.createRow(200);
r200.createCell(0).setCellValue("I am ROW 200");
Row r0 = sheet.createRow(0);
r0.createCell(0).setCellValue("I am ROW 0");
Row r200 = sheet.createRow(200);
r200.createCell(0).setCellValue("I am ROW 200");
// This should work fine
sheet.autoSizeColumn(0);
// This should work fine
sheet.autoSizeColumn(0);
assertEquals(isHssf ? 3645 : 3545, sheet.getColumnWidth(0));
// Get close to 32767
Row r32765 = sheet.createRow(32765);
r32765.createCell(0).setCellValue("Nearly there...");
sheet.autoSizeColumn(0);
// Get close to 32767
Row r32765 = sheet.createRow(32765);
r32765.createCell(0).setCellValue("Nearly there...");
sheet.autoSizeColumn(0);
assertEquals(isHssf ? 3645 : 3554, sheet.getColumnWidth(0), 2);
// To it
Row r32767 = sheet.createRow(32767);
r32767.createCell(0).setCellValue("At the boundary");
sheet.autoSizeColumn(0);
// To it
Row r32767 = sheet.createRow(32767);
r32767.createCell(0).setCellValue("At the boundary");
sheet.autoSizeColumn(0);
assertEquals(isHssf ? 3875 : 4001, sheet.getColumnWidth(0));
// And passed it
Row r32768 = sheet.createRow(32768);
r32768.createCell(0).setCellValue("Passed");
Row r32769 = sheet.createRow(32769);
r32769.createCell(0).setCellValue("More Passed");
sheet.autoSizeColumn(0);
// And passed it
Row r32768 = sheet.createRow(32768);
r32768.createCell(0).setCellValue("Passed");
Row r32769 = sheet.createRow(32769);
r32769.createCell(0).setCellValue("More Passed");
sheet.autoSizeColumn(0);
assertEquals(isHssf ? 3875 : 4001, sheet.getColumnWidth(0));
// Long way passed
Row r60708 = sheet.createRow(60708);
r60708.createCell(0).setCellValue("Near the end");
sheet.autoSizeColumn(0);
// Long way passed
Row r60708 = sheet.createRow(60708);
r60708.createCell(0).setCellValue("Near the end");
sheet.autoSizeColumn(0);
assertEquals(isHssf ? 3875 : 4001, sheet.getColumnWidth(0));
workbook.close();
}
}
// TODO should we have this stuff in the FormulaEvaluator?
@ -348,6 +355,7 @@ public abstract class BaseTestSheetAutosizeColumn {
@Test
void testExcelExporter() throws IOException {
try (final Workbook wb = _testDataProvider.createWorkbook()) {
boolean isHssf = wb instanceof HSSFWorkbook;
final Sheet sheet = wb.createSheet("test");
trackColumnsForAutoSizingIfSXSSF(sheet);
final Row row = sheet.createRow(0);
@ -360,6 +368,7 @@ public abstract class BaseTestSheetAutosizeColumn {
cell.setCellStyle(csDateTime);
sheet.autoSizeColumn(0);
assertEquals(isHssf ? 3249 : 3262, sheet.getColumnWidth(0));
}
}
}