aboutsummaryrefslogtreecommitdiffstats
path: root/src/scratchpad/testcases/org/apache
diff options
context:
space:
mode:
authorAndreas Beeker <kiwiwings@apache.org>2018-08-26 11:55:00 +0000
committerAndreas Beeker <kiwiwings@apache.org>2018-08-26 11:55:00 +0000
commit96d5fab4836225c0aebb0d71bff4a04ae98858e0 (patch)
tree9d00b62a855d26156f70ed40ee0805083b999e1b /src/scratchpad/testcases/org/apache
parent636d9495a52686df0f570db57d56a72f12274e3c (diff)
downloadpoi-96d5fab4836225c0aebb0d71bff4a04ae98858e0.tar.gz
poi-96d5fab4836225c0aebb0d71bff4a04ae98858e0.zip
#62649 - Remove OPOIFS
Fix HPSF UTF-8 encoding error with Bug52311.doc while refactoring CopyCompare/EntryUtils git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1839201 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/scratchpad/testcases/org/apache')
-rw-r--r--src/scratchpad/testcases/org/apache/poi/hslf/extractor/TestExtractor.java38
-rw-r--r--src/scratchpad/testcases/org/apache/poi/hwpf/extractor/TestWordExtractor.java116
-rw-r--r--src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestHWPFWrite.java35
3 files changed, 61 insertions, 128 deletions
diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/extractor/TestExtractor.java b/src/scratchpad/testcases/org/apache/poi/hslf/extractor/TestExtractor.java
index 1f709d969b..5e42300f8b 100644
--- a/src/scratchpad/testcases/org/apache/poi/hslf/extractor/TestExtractor.java
+++ b/src/scratchpad/testcases/org/apache/poi/hslf/extractor/TestExtractor.java
@@ -27,7 +27,6 @@ import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
@@ -35,12 +34,10 @@ import java.util.List;
import org.apache.poi.POIDataSamples;
import org.apache.poi.hslf.usermodel.HSLFObjectShape;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
-import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
-import org.apache.poi.poifs.filesystem.OPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.sl.extractor.SlideShowExtractor;
import org.apache.poi.sl.usermodel.ObjectShape;
@@ -68,18 +65,7 @@ public final class TestExtractor {
*/
private static POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
-// @Before
-// public void setUp() throws Exception {
-// ppe = new PowerPointExtractor(slTests.getFile("basic_test_ppt_file.ppt").getCanonicalPath());
-// ppe2 = new PowerPointExtractor(slTests.getFile("with_textbox.ppt").getCanonicalPath());
-// }
-
-// @After
-// public void closeResources() throws Exception {
-// ppe2.close();
-// ppe.close();
-// }
-
+ @SuppressWarnings("unchecked")
private SlideShowExtractor<?,?> openExtractor(String fileName) throws IOException {
try (InputStream is = slTests.openResourceAsStream(fileName)) {
return new SlideShowExtractor(SlideShowFactory.create(is));
@@ -151,8 +137,6 @@ public final class TestExtractor {
/**
* Test that when presented with a PPT file missing the odd
* core record, we can still get the rest of the text out
- *
- * @throws Exception
*/
@Test
public void testMissingCoreRecords() throws IOException {
@@ -191,7 +175,7 @@ public final class TestExtractor {
assertTrue(dir.hasEntry(HSLFSlideShow.POWERPOINT_DOCUMENT));
try (final SlideShow<?,?> ppt = SlideShowFactory.create(dir);
- final SlideShowExtractor<?,?> ppe = new SlideShowExtractor(ppt)) {
+ final SlideShowExtractor<?,?> ppe = new SlideShowExtractor<>(ppt)) {
assertEquals(TEST_SET[i+1], ppe.getText());
}
}
@@ -297,7 +281,7 @@ public final class TestExtractor {
}
private void testHeaderFooterInner(final HSLFSlideShow ppt) throws IOException {
- try (final SlideShowExtractor<?,?> ppe = new SlideShowExtractor(ppt)) {
+ try (final SlideShowExtractor<?,?> ppe = new SlideShowExtractor<>(ppt)) {
String text = ppe.getText();
assertFalse("Header shouldn't be there by default\n" + text, text.contains("testdoc"));
assertFalse("Header shouldn't be there by default\n" + text, text.contains("test phrase"));
@@ -399,19 +383,11 @@ public final class TestExtractor {
public void testDifferentPOIFS() throws IOException {
// Open the two filesystems
File pptFile = slTests.getFile("basic_test_ppt_file.ppt");
- try (final InputStream is1 = new FileInputStream(pptFile);
- final NPOIFSFileSystem npoifs = new NPOIFSFileSystem(pptFile)) {
-
- final OPOIFSFileSystem opoifs = new OPOIFSFileSystem(is1);
-
- DirectoryNode[] files = {opoifs.getRoot(), npoifs.getRoot()};
-
+ try (final NPOIFSFileSystem npoifs = new NPOIFSFileSystem(pptFile, true)) {
// Open directly
- for (DirectoryNode dir : files) {
- try (SlideShow<?,?> ppt = SlideShowFactory.create(dir);
- SlideShowExtractor<?,?> extractor = new SlideShowExtractor(ppt)) {
- assertEquals(expectText, extractor.getText());
- }
+ try (SlideShow<?,?> ppt = SlideShowFactory.create(npoifs.getRoot());
+ SlideShowExtractor<?,?> extractor = new SlideShowExtractor<>(ppt)) {
+ assertEquals(expectText, extractor.getText());
}
}
}
diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/extractor/TestWordExtractor.java b/src/scratchpad/testcases/org/apache/poi/hwpf/extractor/TestWordExtractor.java
index 3cfec195d1..8c8dfa78e0 100644
--- a/src/scratchpad/testcases/org/apache/poi/hwpf/extractor/TestWordExtractor.java
+++ b/src/scratchpad/testcases/org/apache/poi/hwpf/extractor/TestWordExtractor.java
@@ -24,7 +24,6 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -36,7 +35,6 @@ import org.apache.poi.hwpf.OldWordFileFormatException;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.Entry;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
-import org.apache.poi.poifs.filesystem.OPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.StringUtil;
import org.junit.Test;
@@ -48,7 +46,7 @@ public final class TestWordExtractor {
private static POIDataSamples docTests = POIDataSamples.getDocumentInstance();
- public static void assertEqualsTrim( String expected, String actual )
+ private static void assertEqualsTrim( String expected, String actual )
{
String newExpected = expected.replaceAll( "\r\n", "\n" )
.replaceAll( "\r", "\n" ).trim();
@@ -188,7 +186,8 @@ public final class TestWordExtractor {
HWPFDocument doc1 = HWPFTestDataSamples.openSampleFile("ThreeColHeadFoot.doc");
WordExtractor extractor1 = new WordExtractor(doc1);
- assertEquals("First header column!\tMid header Right header!\n", extractor1.getHeaderText());
+ //noinspection deprecation
+ assertEquals("First header column!\tMid header Right header!\n", extractor1.getHeaderText());
assertContains(extractor1.getText(), "First header column!");
extractor1.close();
doc1.close();
@@ -197,7 +196,8 @@ public final class TestWordExtractor {
HWPFDocument doc2 = HWPFTestDataSamples.openSampleFile("HeaderFooterUnicode.doc");
WordExtractor extractor2 = new WordExtractor(doc2);
- assertEquals("This is a simple header, with a \u20ac euro symbol in it.\n\n", extractor2.getHeaderText());
+ //noinspection deprecation
+ assertEquals("This is a simple header, with a \u20ac euro symbol in it.\n\n", extractor2.getHeaderText());
assertContains(extractor2.getText(), "This is a simple header");
extractor2.close();
doc2.close();
@@ -209,7 +209,8 @@ public final class TestWordExtractor {
HWPFDocument doc1 = HWPFTestDataSamples.openSampleFile("ThreeColHeadFoot.doc");
WordExtractor extractor1 = new WordExtractor(doc1);
- assertEquals("Footer Left\tFooter Middle Footer Right\n", extractor1.getFooterText());
+ //noinspection deprecation
+ assertEquals("Footer Left\tFooter Middle Footer Right\n", extractor1.getFooterText());
assertContains(extractor1.getText(), "Footer Left");
extractor1.close();
doc1.close();
@@ -218,7 +219,8 @@ public final class TestWordExtractor {
HWPFDocument doc2 = HWPFTestDataSamples.openSampleFile("HeaderFooterUnicode.doc");
WordExtractor extractor2 = new WordExtractor(doc2);
- assertEquals("The footer, with Moli\u00e8re, has Unicode in it.\n", extractor2.getFooterText());
+ //noinspection deprecation
+ assertEquals("The footer, with Moli\u00e8re, has Unicode in it.\n", extractor2.getFooterText());
assertContains(extractor2.getText(), "The footer, with");
extractor2.close();
doc2.close();
@@ -279,6 +281,7 @@ public final class TestWordExtractor {
assertContains(text, "Paragraph 3. Has some RED text and some BLUE BOLD text in it");
assertContains(text, "Last (4th) paragraph");
+ @SuppressWarnings("deprecation")
String[] tp = w6e.getParagraphText();
assertEquals(7, tp.length);
assertEquals("The quick brown fox jumps over the lazy dog\r\n", tp[0]);
@@ -299,17 +302,17 @@ public final class TestWordExtractor {
@Test
public void testWord6() throws Exception {
- InputStream is = docTests.openResourceAsStream("Word6.doc");
- Word6Extractor w6e = new Word6Extractor(is);
- is.close();
- String text = w6e.getText();
-
- assertContains(text, "The quick brown fox jumps over the lazy dog");
-
- String[] tp = w6e.getParagraphText();
- assertEquals(1, tp.length);
- assertEquals("The quick brown fox jumps over the lazy dog\r\n", tp[0]);
- w6e.close();
+ try (InputStream is = docTests.openResourceAsStream("Word6.doc");
+ Word6Extractor w6e = new Word6Extractor(is)) {
+ String text = w6e.getText();
+
+ assertContains(text, "The quick brown fox jumps over the lazy dog");
+
+ @SuppressWarnings("deprecation")
+ String[] tp = w6e.getParagraphText();
+ assertEquals(1, tp.length);
+ assertEquals("The quick brown fox jumps over the lazy dog\r\n", tp[0]);
+ }
}
@Test
@@ -341,30 +344,23 @@ public final class TestWordExtractor {
public void testDifferentPOIFS() throws Exception {
// Open the two filesystems
File file = docTests.getFile("test2.doc");
- InputStream is = new FileInputStream(file);
- OPOIFSFileSystem opoifs = new OPOIFSFileSystem(is);
- is.close();
- NPOIFSFileSystem npoifs = new NPOIFSFileSystem(file);
-
- DirectoryNode[] files = { opoifs.getRoot(), npoifs.getRoot() };
-
- // Open directly
- for(DirectoryNode dir : files) {
- @SuppressWarnings("resource")
- WordExtractor extractor = new WordExtractor(dir);
- assertEqualsTrim(p_text1_block, extractor.getText());
- // extractor.close();
- }
+ try (NPOIFSFileSystem npoifs = new NPOIFSFileSystem(file, true)) {
+
+ DirectoryNode dir = npoifs.getRoot();
+
+ // Open directly
+ @SuppressWarnings("resource")
+ WordExtractor extractor1 = new WordExtractor(dir);
+ assertEqualsTrim(p_text1_block, extractor1.getText());
+ // extractor.close();
+
+ // Open via a HWPFDocument
+ try (HWPFDocument doc = new HWPFDocument(dir);
+ WordExtractor extractor2 = new WordExtractor(doc)) {
+ assertEqualsTrim(p_text1_block, extractor2.getText());
+ }
- // Open via a HWPFDocument
- for(DirectoryNode dir : files) {
- HWPFDocument doc = new HWPFDocument(dir);
- WordExtractor extractor = new WordExtractor(doc);
- assertEqualsTrim(p_text1_block, extractor.getText());
- extractor.close();
}
-
- npoifs.close();
}
/**
@@ -381,11 +377,8 @@ public final class TestWordExtractor {
for (Entry entry : fs.getRoot()) {
if ("WordDocument".equals(entry.getName())) {
- WordExtractor ex = new WordExtractor(fs);
- try {
+ try (WordExtractor ex = new WordExtractor(fs)) {
text = ex.getText();
- } finally {
- ex.close();
}
}
}
@@ -396,35 +389,22 @@ public final class TestWordExtractor {
@Test
public void testExtractorFromWord6Extractor() throws Exception {
- InputStream is = POIDataSamples.getHPSFInstance().openResourceAsStream("TestMickey.doc");
- POIFSFileSystem fs = new POIFSFileSystem(is);
- is.close();
- Word6Extractor wExt = new Word6Extractor(fs);
- try {
- POITextExtractor ext = wExt.getMetadataTextExtractor();
- try {
- // Now overall
- String text = ext.getText();
- assertContains(text, "TEMPLATE = Normal");
- assertContains(text, "SUBJECT = sample subject");
- assertContains(text, "MANAGER = sample manager");
- assertContains(text, "COMPANY = sample company");
- } finally {
- ext.close();
- }
- } finally {
- wExt.close();
- fs.close();
+ try (InputStream is = POIDataSamples.getHPSFInstance().openResourceAsStream("TestMickey.doc");
+ POIFSFileSystem fs = new POIFSFileSystem(is);
+ Word6Extractor wExt = new Word6Extractor(fs);
+ POITextExtractor ext = wExt.getMetadataTextExtractor()) {
+ // Now overall
+ String text = ext.getText();
+ assertContains(text, "TEMPLATE = Normal");
+ assertContains(text, "SUBJECT = sample subject");
+ assertContains(text, "MANAGER = sample manager");
+ assertContains(text, "COMPANY = sample company");
}
}
private WordExtractor openExtractor(String fileName) throws IOException {
- InputStream is = docTests.openResourceAsStream(fileName);
- try {
+ try (InputStream is = docTests.openResourceAsStream(fileName)) {
return new WordExtractor(is);
- } finally {
- is.close();
}
-
}
}
diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestHWPFWrite.java b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestHWPFWrite.java
index 76c71bab07..ea9cf893dd 100644
--- a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestHWPFWrite.java
+++ b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestHWPFWrite.java
@@ -32,7 +32,6 @@ import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.HWPFTestCase;
import org.apache.poi.hwpf.HWPFTestDataSamples;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
-import org.apache.poi.poifs.filesystem.OPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.TempFile;
@@ -99,16 +98,9 @@ public final class TestHWPFWrite extends HWPFTestCase {
public void testInPlaceWrite() throws Exception {
// Setup as a copy of a known-good file
final File file = TempFile.createTempFile("TestDocument", ".doc");
- InputStream inputStream = SAMPLES.openResourceAsStream("SampleDoc.doc");
- try {
- FileOutputStream outputStream = new FileOutputStream(file);
- try {
- IOUtils.copy(inputStream, outputStream);
- } finally {
- outputStream.close();
- }
- } finally {
- inputStream.close();
+ try (InputStream inputStream = SAMPLES.openResourceAsStream("SampleDoc.doc");
+ FileOutputStream outputStream = new FileOutputStream(file)) {
+ IOUtils.copy(inputStream, outputStream);
}
// Open from the temp file in read-write mode
@@ -136,29 +128,14 @@ public final class TestHWPFWrite extends HWPFTestCase {
@Test(expected=IllegalStateException.class)
public void testInvalidInPlaceWriteInputStream() throws IOException {
// Can't work for InputStream opened files
- InputStream is = SAMPLES.openResourceAsStream("SampleDoc.doc");
- HWPFDocument doc = new HWPFDocument(is);
- is.close();
- try {
+
+ try (InputStream is = SAMPLES.openResourceAsStream("SampleDoc.doc");
+ HWPFDocument doc = new HWPFDocument(is)) {
doc.write();
- } finally {
- doc.close();
}
}
@Test(expected=IllegalStateException.class)
- public void testInvalidInPlaceWriteOPOIFS() throws Exception {
- // Can't work for OPOIFS
- OPOIFSFileSystem ofs = new OPOIFSFileSystem(SAMPLES.openResourceAsStream("SampleDoc.doc"));
- HWPFDocument doc = new HWPFDocument(ofs.getRoot());
- try {
- doc.write();
- } finally {
- doc.close();
- }
- }
-
- @Test(expected=IllegalStateException.class)
public void testInvalidInPlaceWriteNPOIFS() throws Exception {
// Can't work for Read-Only files
NPOIFSFileSystem fs = new NPOIFSFileSystem(SAMPLES.getFile("SampleDoc.doc"), true);