From 4cc6e093f4be08c97e42c9e38f4c006441953c93 Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Tue, 13 Dec 2016 00:36:03 +0000 Subject: [PATCH] eclipse warnings - close resources git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1773910 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hslf/examples/DataExtraction.java | 5 +- .../org/apache/poi/hsmf/examples/Msg2txt.java | 8 +- .../usermodel/examples/SimpleDocument.java | 10 +- .../xwpf/usermodel/examples/SimpleImages.java | 13 +- .../apache/poi/stress/HSMFFileHandler.java | 2 +- src/java/org/apache/poi/POIDocument.java | 1 + .../poi/hssf/record/HyperlinkRecord.java | 23 +- .../openxml4j/opc/internal/FileHelper.java | 8 +- .../poi/openxml4j/opc/ZipFileAssert.java | 84 +-- .../OLE2ScratchpadExtractorFactory.java | 8 +- .../extractor/TestPublisherTextExtractor.java | 274 +++---- .../poi/hpbf/model/TestQuillContents.java | 64 +- .../apache/poi/hslf/TestEncryptedFile.java | 60 +- .../hslf/record/TestDocumentEncryption.java | 9 +- .../apache/poi/hslf/usermodel/TestBugs.java | 12 +- .../hsmf/parsers/TestPOIFSChunkParser.java | 24 +- .../hwpf/usermodel/TestHWPFOldDocument.java | 230 +++--- .../poi/hwpf/usermodel/TestHWPFWrite.java | 239 ++++--- .../poi/hwpf/usermodel/TestProblems.java | 674 +++++++++--------- 19 files changed, 913 insertions(+), 835 deletions(-) diff --git a/src/examples/src/org/apache/poi/hslf/examples/DataExtraction.java b/src/examples/src/org/apache/poi/hslf/examples/DataExtraction.java index 514c01ba0..ad71ceff6 100644 --- a/src/examples/src/org/apache/poi/hslf/examples/DataExtraction.java +++ b/src/examples/src/org/apache/poi/hslf/examples/DataExtraction.java @@ -36,8 +36,6 @@ import org.apache.poi.hwpf.usermodel.Range; /** * Demonstrates how you can extract misc embedded data from a ppt file - * - * @author Yegor Kozlov */ public final class DataExtraction { @@ -93,6 +91,7 @@ public final class DataExtraction { FileOutputStream out = new FileOutputStream(name + "-("+(oleIdx)+").doc"); doc.write(out); out.close(); + doc.close(); } else { FileOutputStream out = new FileOutputStream(ole.getProgID() + "-"+(oleIdx+1)+".dat"); InputStream dis = data.getData(); @@ -117,8 +116,8 @@ public final class DataExtraction { out.close(); } } - } + ppt.close(); } private static void usage(){ diff --git a/src/examples/src/org/apache/poi/hsmf/examples/Msg2txt.java b/src/examples/src/org/apache/poi/hsmf/examples/Msg2txt.java index bf018a3e4..9eeba8ce5 100644 --- a/src/examples/src/org/apache/poi/hsmf/examples/Msg2txt.java +++ b/src/examples/src/org/apache/poi/hsmf/examples/Msg2txt.java @@ -131,16 +131,16 @@ public class Msg2txt { */ public void processAttachment(AttachmentChunks attachment, File dir) throws IOException { - String fileName = attachment.attachFileName.toString(); - if(attachment.attachLongFileName != null) { - fileName = attachment.attachLongFileName.toString(); + String fileName = attachment.getAttachFileName().toString(); + if(attachment.getAttachLongFileName() != null) { + fileName = attachment.getAttachLongFileName().toString(); } File f = new File(dir, fileName); OutputStream fileOut = null; try { fileOut = new FileOutputStream(f); - fileOut.write(attachment.attachData.getValue()); + fileOut.write(attachment.getAttachData().getValue()); } finally { if(fileOut != null) { fileOut.close(); diff --git a/src/examples/src/org/apache/poi/xwpf/usermodel/examples/SimpleDocument.java b/src/examples/src/org/apache/poi/xwpf/usermodel/examples/SimpleDocument.java index 1553321cd..4f5487b1f 100644 --- a/src/examples/src/org/apache/poi/xwpf/usermodel/examples/SimpleDocument.java +++ b/src/examples/src/org/apache/poi/xwpf/usermodel/examples/SimpleDocument.java @@ -32,8 +32,6 @@ import org.apache.poi.xwpf.usermodel.XWPFRun; /** * A simple WOrdprocessingML document created by POI XWPF API - * - * @author Yegor Kozlov */ public class SimpleDocument { @@ -71,18 +69,18 @@ public class SimpleDocument { XWPFRun r2 = p2.createRun(); r2.setText("jumped over the lazy dog"); - r2.setStrike(true); + r2.setStrikeThrough(true); r2.setFontSize(20); XWPFRun r3 = p2.createRun(); r3.setText("and went away"); - r3.setStrike(true); + r3.setStrikeThrough(true); r3.setFontSize(20); r3.setSubscript(VerticalAlign.SUPERSCRIPT); XWPFParagraph p3 = doc.createParagraph(); - p3.setWordWrap(true); + p3.setWordWrapped(true); p3.setPageBreak(true); //p3.setAlignment(ParagraphAlignment.DISTRIBUTE); @@ -127,6 +125,6 @@ public class SimpleDocument { FileOutputStream out = new FileOutputStream("simple.docx"); doc.write(out); out.close(); - + doc.close(); } } diff --git a/src/examples/src/org/apache/poi/xwpf/usermodel/examples/SimpleImages.java b/src/examples/src/org/apache/poi/xwpf/usermodel/examples/SimpleImages.java index 7e95c7a7f..638cf91b1 100644 --- a/src/examples/src/org/apache/poi/xwpf/usermodel/examples/SimpleImages.java +++ b/src/examples/src/org/apache/poi/xwpf/usermodel/examples/SimpleImages.java @@ -18,23 +18,23 @@ */ package org.apache.poi.xwpf.usermodel.examples; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; + +import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.util.Units; import org.apache.poi.xwpf.usermodel.BreakType; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; -import java.io.FileInputStream; -import java.io.FileOutputStream; - /** * Demonstrates how to add pictures in a .docx document - * - * @author Yegor Kozlov */ public class SimpleImages { - public static void main(String[] args) throws Exception { + public static void main(String[] args) throws IOException, InvalidFormatException { XWPFDocument doc = new XWPFDocument(); XWPFParagraph p = doc.createParagraph(); @@ -69,6 +69,7 @@ public class SimpleImages { FileOutputStream out = new FileOutputStream("images.docx"); doc.write(out); out.close(); + doc.close(); } diff --git a/src/integrationtest/org/apache/poi/stress/HSMFFileHandler.java b/src/integrationtest/org/apache/poi/stress/HSMFFileHandler.java index c22edf2fc..4f7bd4b5d 100644 --- a/src/integrationtest/org/apache/poi/stress/HSMFFileHandler.java +++ b/src/integrationtest/org/apache/poi/stress/HSMFFileHandler.java @@ -38,7 +38,7 @@ public class HSMFFileHandler extends POIFSFileHandler { for(AttachmentChunks attachment : attachments) { - DirectoryChunk chunkDirectory = attachment.attachmentDirectory; + DirectoryChunk chunkDirectory = attachment.getAttachmentDirectory(); if(chunkDirectory != null) { MAPIMessage attachmentMSG = chunkDirectory.getAsEmbededMessage(); assertNotNull(attachmentMSG); diff --git a/src/java/org/apache/poi/POIDocument.java b/src/java/org/apache/poi/POIDocument.java index afd78a467..ed8e6783e 100644 --- a/src/java/org/apache/poi/POIDocument.java +++ b/src/java/org/apache/poi/POIDocument.java @@ -420,6 +420,7 @@ public abstract class POIDocument implements Closeable { * * @return {@code true} if dummy directory was created, {@code false} otherwise */ + @SuppressWarnings("resource") @Internal protected boolean initDirectory() { if (directory == null) { diff --git a/src/java/org/apache/poi/hssf/record/HyperlinkRecord.java b/src/java/org/apache/poi/hssf/record/HyperlinkRecord.java index 4048255f4..e4096e226 100644 --- a/src/java/org/apache/poi/hssf/record/HyperlinkRecord.java +++ b/src/java/org/apache/poi/hssf/record/HyperlinkRecord.java @@ -17,14 +17,10 @@ package org.apache.poi.hssf.record; -import java.io.ByteArrayOutputStream; -import java.io.DataOutputStream; -import java.io.IOException; - import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.util.HexDump; import org.apache.poi.util.HexRead; -import org.apache.poi.util.LittleEndianByteArrayInputStream; +import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndianInput; import org.apache.poi.util.LittleEndianOutput; import org.apache.poi.util.POILogFactory; @@ -107,15 +103,14 @@ public final class HyperlinkRecord extends StandardRecord implements Cloneable { } public long getD4() { - // - ByteArrayOutputStream baos = new ByteArrayOutputStream(8); - try { - new DataOutputStream(baos).writeLong(_d4); - } catch (IOException e) { - throw new RuntimeException(e); - } - byte[] buf = baos.toByteArray(); - return new LittleEndianByteArrayInputStream(buf).readLong(); + byte[] result = new byte[Long.SIZE/Byte.SIZE]; + long l = _d4; + for (int i = result.length-1; i >= 0; i--) { + result[i] = (byte)(l & 0xFF); + l >>= 8; + } + + return LittleEndian.getLong(result, 0); } public String formatAsString() { diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/FileHelper.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/FileHelper.java index cc41e4352..bf7402ea0 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/FileHelper.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/FileHelper.java @@ -64,11 +64,15 @@ public final class FileHelper { * If an I/O error occur. */ public static void copyFile(File in, File out) throws IOException { - FileChannel sourceChannel = new FileInputStream(in).getChannel(); - FileChannel destinationChannel = new FileOutputStream(out).getChannel(); + FileInputStream fis = new FileInputStream(in); + FileOutputStream fos = new FileOutputStream(out); + FileChannel sourceChannel = fis.getChannel(); + FileChannel destinationChannel = fos.getChannel(); sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel); sourceChannel.close(); destinationChannel.close(); + fos.close(); + fis.close(); } /** diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/ZipFileAssert.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/ZipFileAssert.java index c59be4525..a910acc0e 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/ZipFileAssert.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/ZipFileAssert.java @@ -17,24 +17,26 @@ package org.apache.poi.openxml4j.opc; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.util.Iterator; import java.util.Set; import java.util.TreeMap; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; -import junit.framework.Assert; +import org.junit.Assert; + import junit.framework.AssertionFailedError; /** * Compare the contents of 2 zip files. - * - * @author CDubettier */ public class ZipFileAssert { private ZipFileAssert() { @@ -42,55 +44,31 @@ public class ZipFileAssert { static final int BUFFER_SIZE = 2048; - protected static boolean equals( + protected static void equals( TreeMap file1, TreeMap file2) { - Set listFile1 = file1.keySet(); - if (listFile1.size() == file2.keySet().size()) { - for (Iterator iter = listFile1.iterator(); iter.hasNext();) { - String fileName = (String) iter.next(); - // extract the contents for both - ByteArrayOutputStream contain2 = file2.get(fileName); - ByteArrayOutputStream contain1 = file1.get(fileName); + Set listFile1 = file1.keySet(); + Assert.assertEquals("not the same number of files in zip:", listFile1.size(), file2.keySet().size()); + + for (String fileName : listFile1) { + // extract the contents for both + ByteArrayOutputStream contain2 = file2.get(fileName); + ByteArrayOutputStream contain1 = file1.get(fileName); - if (contain2 == null) { - // file not found in archive 2 - Assert.fail(fileName + " not found in 2nd zip"); - return false; - } - // no need to check for contain1. The key come from it + assertNotNull(fileName + " not found in 2nd zip", contain2); + // no need to check for contain1. The key come from it - if ((fileName.endsWith(".xml")) || fileName.endsWith(".rels")) { - // we have a xml file - // TODO - // YK: the original OpenXML4J version attempted to compare xml using xmlunit (http://xmlunit.sourceforge.net), - // but POI does not depend on this library - } else { - // not xml, may be an image or other binary format - if (contain2.size() != contain1.size()) { - // not the same size - Assert.fail(fileName - + " does not have the same size in both zip:" - + contain2.size() + "!=" + contain1.size()); - return false; - } - byte array1[] = contain1.toByteArray(); - byte array2[] = contain2.toByteArray(); - for (int i = 0; i < array1.length; i++) { - if (array1[i] != array2[i]) { - Assert.fail(fileName + " differ at index:" + i); - return false; - } - } - } + if ((fileName.endsWith(".xml")) || fileName.endsWith(".rels")) { + // we have a xml file + // TODO + // YK: the original OpenXML4J version attempted to compare xml using xmlunit (http://xmlunit.sourceforge.net), + // but POI does not depend on this library + } else { + // not xml, may be an image or other binary format + Assert.assertEquals(fileName + " does not have the same size in both zip:", contain2.size(), contain1.size()); + assertArrayEquals("contents differ", contain1.toByteArray(), contain2.toByteArray()); } - } else { - // not the same number of files -> cannot be equals - Assert.fail("not the same number of files in zip:" - + listFile1.size() + "!=" + file2.keySet().size()); - return false; } - return true; } protected static TreeMap decompress( @@ -139,16 +117,16 @@ public class ZipFileAssert { * */ public static void assertEquals(File expected, File actual) { - Assert.assertNotNull(expected); - Assert.assertNotNull(actual); + assertNotNull(expected); + assertNotNull(actual); - Assert.assertTrue("File does not exist [" + expected.getAbsolutePath() + assertTrue("File does not exist [" + expected.getAbsolutePath() + "]", expected.exists()); - Assert.assertTrue("File does not exist [" + actual.getAbsolutePath() + assertTrue("File does not exist [" + actual.getAbsolutePath() + "]", actual.exists()); - Assert.assertTrue("Expected file not readable", expected.canRead()); - Assert.assertTrue("Actual file not readable", actual.canRead()); + assertTrue("Expected file not readable", expected.canRead()); + assertTrue("Actual file not readable", actual.canRead()); try { TreeMap file1 = decompress(expected); diff --git a/src/scratchpad/src/org/apache/poi/extractor/OLE2ScratchpadExtractorFactory.java b/src/scratchpad/src/org/apache/poi/extractor/OLE2ScratchpadExtractorFactory.java index 115b74a39..429e257ab 100644 --- a/src/scratchpad/src/org/apache/poi/extractor/OLE2ScratchpadExtractorFactory.java +++ b/src/scratchpad/src/org/apache/poi/extractor/OLE2ScratchpadExtractorFactory.java @@ -133,11 +133,11 @@ public class OLE2ScratchpadExtractorFactory { // Stored in the Attachment blocks MAPIMessage msg = ((OutlookTextExtactor)ext).getMAPIMessage(); for (AttachmentChunks attachment : msg.getAttachmentFiles()) { - if (attachment.attachData != null) { - byte[] data = attachment.attachData.getValue(); + if (attachment.getAttachData() != null) { + byte[] data = attachment.getAttachData().getValue(); nonPOIFS.add( new ByteArrayInputStream(data) ); - } else if (attachment.attachmentDirectory != null) { - dirs.add(attachment.attachmentDirectory.getDirectory()); + } else if (attachment.getAttachmentDirectory() != null) { + dirs.add(attachment.getAttachmentDirectory().getDirectory()); } } } diff --git a/src/scratchpad/testcases/org/apache/poi/hpbf/extractor/TestPublisherTextExtractor.java b/src/scratchpad/testcases/org/apache/poi/hpbf/extractor/TestPublisherTextExtractor.java index 63af798f9..35920b577 100644 --- a/src/scratchpad/testcases/org/apache/poi/hpbf/extractor/TestPublisherTextExtractor.java +++ b/src/scratchpad/testcases/org/apache/poi/hpbf/extractor/TestPublisherTextExtractor.java @@ -17,159 +17,171 @@ package org.apache.poi.hpbf.extractor; -import java.io.File; -import java.io.FileInputStream; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; -import junit.framework.TestCase; +import java.io.IOException; +import java.io.InputStream; import org.apache.poi.POIDataSamples; import org.apache.poi.hpbf.HPBFDocument; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; +import org.junit.Test; -public final class TestPublisherTextExtractor extends TestCase { +public final class TestPublisherTextExtractor { private static final POIDataSamples _samples = POIDataSamples.getPublisherInstance(); - + private static final String SAMPLE_TEXT = - "This is some text on the first page\n" + - "It\u2019s in times new roman, font size 10, all normal\n" + - "" + - "This is in bold and italic\n" + - "It\u2019s Arial, 20 point font\n" + - "It\u2019s in the second textbox on the first page\n" + - "" + - "This is the second page\n\n" + - "" + - "It is also times new roman, 10 point\n" + - "" + - "Table on page 2\nTop right\n" + - "P2 table left\nP2 table right\n" + - "Bottom Left\nBottom Right\n" + - "" + - "This text is on page two\n" + - "#This is a link to Apache POI\n" + - "More normal text\n" + - "Link to a file\n" + - "" + - "More text, more hyperlinks\n" + - "email link\n" + - "Final hyperlink\n" + - "Within doc to page 1\n"; + "This is some text on the first page\n" + + "It\u2019s in times new roman, font size 10, all normal\n" + + "" + + "This is in bold and italic\n" + + "It\u2019s Arial, 20 point font\n" + + "It\u2019s in the second textbox on the first page\n" + + "" + + "This is the second page\n\n" + + "" + + "It is also times new roman, 10 point\n" + + "" + + "Table on page 2\nTop right\n" + + "P2 table left\nP2 table right\n" + + "Bottom Left\nBottom Right\n" + + "" + + "This text is on page two\n" + + "#This is a link to Apache POI\n" + + "More normal text\n" + + "Link to a file\n" + + "" + + "More text, more hyperlinks\n" + + "email link\n" + + "Final hyperlink\n" + + "Within doc to page 1\n"; + private static final String SIMPLE_TEXT = - "0123456789\n" + - "0123456789abcdef\n" + - "0123456789abcdef0123456789abcdef\n" + - "0123456789\n" + - "0123456789abcdef\n" + - "0123456789abcdef0123456789abcdef\n" + - "0123456789abcdef0123456789abcdef0123456789abcdef\n"; + "0123456789\n" + + "0123456789abcdef\n" + + "0123456789abcdef0123456789abcdef\n" + + "0123456789\n" + + "0123456789abcdef\n" + + "0123456789abcdef0123456789abcdef\n" + + "0123456789abcdef0123456789abcdef0123456789abcdef\n"; - public void testBasics() throws Exception { - HPBFDocument doc = new HPBFDocument( - _samples.openResourceAsStream("Sample.pub") - ); + @Test + public void testBasics() throws IOException { + InputStream sample = _samples.openResourceAsStream("Sample.pub"); + HPBFDocument doc = new HPBFDocument(sample); + PublisherTextExtractor ext = new PublisherTextExtractor(doc); + assertNotNull(ext.getText()); + ext.close(); + doc.close(); + sample.close(); - PublisherTextExtractor ext = - new PublisherTextExtractor(doc); - ext.getText(); + InputStream simple = _samples.openResourceAsStream("Simple.pub"); + ext = new PublisherTextExtractor(simple); + assertNotNull(ext.getText()); + ext.close(); + simple.close(); + } - ext = new PublisherTextExtractor( - _samples.openResourceAsStream("Simple.pub") - ); - ext.getText(); - } + @Test + public void testContents() throws IOException { + // Check this complicated file using POIFS + InputStream sample = _samples.openResourceAsStream("Sample.pub"); + HPBFDocument docOPOIFS = new HPBFDocument(sample); + PublisherTextExtractor ext = new PublisherTextExtractor(docOPOIFS); + assertEquals(SAMPLE_TEXT, ext.getText()); + ext.close(); + docOPOIFS.close(); + sample.close(); - public void testContents() throws Exception { - PublisherTextExtractor ext; - File sample = _samples.getFile("Sample.pub"); - File simple = _samples.getFile("Simple.pub"); - - // Check this complicated file using POIFS - HPBFDocument docOPOIFS = new HPBFDocument( - new FileInputStream(sample) - ); - ext = new PublisherTextExtractor(docOPOIFS); - assertEquals( SAMPLE_TEXT, ext.getText() ); + // And with NPOIFS + sample = _samples.openResourceAsStream("Sample.pub"); + NPOIFSFileSystem fs = new NPOIFSFileSystem(sample); + HPBFDocument docNPOIFS = new HPBFDocument(fs); + ext = new PublisherTextExtractor(docNPOIFS); + assertEquals(SAMPLE_TEXT, ext.getText()); + ext.close(); + docNPOIFS.close(); + fs.close(); + sample.close(); - // And with NPOIFS - NPOIFSFileSystem fs = new NPOIFSFileSystem(sample); - HPBFDocument docNPOIFS = new HPBFDocument( - fs - ); - ext = new PublisherTextExtractor(docNPOIFS); - assertEquals( SAMPLE_TEXT, ext.getText() ); + // Now a simpler file + InputStream simple = _samples.openResourceAsStream("Simple.pub"); + ext = new PublisherTextExtractor(simple); + assertEquals(SIMPLE_TEXT, ext.getText()); + ext.close(); + } - - // Now a simpler file - ext = new PublisherTextExtractor( - new FileInputStream(simple) - ); - assertEquals( SIMPLE_TEXT, ext.getText() ); - fs.close(); - } + /** + * We have the same file saved for Publisher 98, Publisher 2000 and + * Publisher 2007. Check they all agree. + * + * @throws Exception + */ + @Test + public void testMultipleVersions() throws Exception { + InputStream sample = _samples.openResourceAsStream("Sample.pub"); + HPBFDocument doc = new HPBFDocument(sample); + PublisherTextExtractor ext = new PublisherTextExtractor(doc); + String s2007 = ext.getText(); + ext.close(); + doc.close(); + sample.close(); - /** - * We have the same file saved for Publisher 98, Publisher - * 2000 and Publisher 2007. Check they all agree. - * @throws Exception - */ - public void testMultipleVersions() throws Exception { - File f; - HPBFDocument doc; + InputStream sample2000 = _samples.openResourceAsStream("Sample2000.pub"); + doc = new HPBFDocument(sample2000); + ext = new PublisherTextExtractor(doc); + String s2000 = ext.getText(); + ext.close(); + doc.close(); + sample2000.close(); - doc = new HPBFDocument( - _samples.openResourceAsStream("Sample.pub") - ); - String s2007 = (new PublisherTextExtractor(doc)).getText(); + InputStream sample98 = _samples.openResourceAsStream("Sample98.pub"); + doc = new HPBFDocument(sample98); + ext = new PublisherTextExtractor(doc); + String s98 = ext.getText(); + ext.close(); + doc.close(); + sample98.close(); - doc = new HPBFDocument( - _samples.openResourceAsStream("Sample2000.pub") - ); - String s2000 = (new PublisherTextExtractor(doc)).getText(); + // Check they all agree + assertEquals(s2007, s2000); + assertEquals(s2007, s98); + } - doc = new HPBFDocument( - _samples.openResourceAsStream("Sample98.pub") - ); - String s98 = (new PublisherTextExtractor(doc)).getText(); + /** + * Test that the hyperlink extraction stuff works as well as we can hope it + * to. + */ + @Test + public void testWithHyperlinks() throws Exception { + InputStream linkAt = _samples.openResourceAsStream("LinkAt10.pub"); + HPBFDocument doc = new HPBFDocument(linkAt); - // Check they all agree - assertEquals(s2007, s2000); - assertEquals(s2007, s98); - } + PublisherTextExtractor ext = new PublisherTextExtractor(doc); - /** - * Test that the hyperlink extraction stuff works as well - * as we can hope it to. - */ - public void testWithHyperlinks() throws Exception { - HPBFDocument doc = new HPBFDocument( - _samples.openResourceAsStream("LinkAt10.pub") - ); + // Default is no hyperlinks + assertEquals("1234567890LINK\n", ext.getText()); - PublisherTextExtractor ext = - new PublisherTextExtractor(doc); - ext.getText(); + // Turn on + ext.setHyperlinksByDefault(true); + assertEquals("1234567890LINK\n\n", ext.getText()); + ext.close(); + doc.close(); + linkAt.close(); - // Default is no hyperlinks - assertEquals("1234567890LINK\n", ext.getText()); + // Now a much more complex document + InputStream sample = _samples.openResourceAsStream("Sample.pub"); + ext = new PublisherTextExtractor(sample); + ext.setHyperlinksByDefault(true); + String text = ext.getText(); + ext.close(); + sample.close(); - // Turn on - ext.setHyperlinksByDefault(true); - assertEquals("1234567890LINK\n\n", ext.getText()); - - - // Now a much more complex document - ext = new PublisherTextExtractor( - _samples.openResourceAsStream("Sample.pub") - ); - ext.setHyperlinksByDefault(true); - String text = ext.getText(); - - assertTrue(text.endsWith( - "\n" + - "\n" + - "<>\n" + - "\n" + - "\n" - )); - } + assertTrue(text.endsWith("\n" + + "\n" + + "<>\n" + "\n" + + "\n")); + } } diff --git a/src/scratchpad/testcases/org/apache/poi/hpbf/model/TestQuillContents.java b/src/scratchpad/testcases/org/apache/poi/hpbf/model/TestQuillContents.java index b88e45b02..a251d0527 100644 --- a/src/scratchpad/testcases/org/apache/poi/hpbf/model/TestQuillContents.java +++ b/src/scratchpad/testcases/org/apache/poi/hpbf/model/TestQuillContents.java @@ -17,20 +17,27 @@ package org.apache.poi.hpbf.model; +import static org.junit.Assert.assertEquals; +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.POIDataSamples; import org.apache.poi.hpbf.HPBFDocument; -import org.apache.poi.hpbf.model.qcbits.QCTextBit; -import org.apache.poi.hpbf.model.qcbits.QCPLCBit.Type12; import org.apache.poi.hpbf.model.qcbits.QCPLCBit.Type0; +import org.apache.poi.hpbf.model.qcbits.QCPLCBit.Type12; import org.apache.poi.hpbf.model.qcbits.QCPLCBit.Type4; import org.apache.poi.hpbf.model.qcbits.QCPLCBit.Type8; -import org.apache.poi.POIDataSamples; +import org.apache.poi.hpbf.model.qcbits.QCTextBit; +import org.junit.Test; -import junit.framework.TestCase; - -public final class TestQuillContents extends TestCase { +public final class TestQuillContents { private static final POIDataSamples _samples = POIDataSamples.getPublisherInstance(); - public void testBasics() throws Exception { + @Test + public void testBasics() throws IOException { HPBFDocument doc = new HPBFDocument( _samples.openResourceAsStream("Sample.pub") ); @@ -59,9 +66,12 @@ public final class TestQuillContents extends TestCase { assertEquals("STSH", qc.getBits()[3].getThingType()); assertEquals("STSH", qc.getBits()[3].getBitType()); assertEquals(2, qc.getBits()[3].getOptA()); + + doc.close(); } - public void testText() throws Exception { + @Test + public void testText() throws IOException { HPBFDocument doc = new HPBFDocument( _samples.openResourceAsStream("Sample.pub") ); @@ -73,9 +83,12 @@ public final class TestQuillContents extends TestCase { String t = text.getText(); assertTrue(t.startsWith("This is some text on the first page")); assertTrue(t.endsWith("Within doc to page 1\r")); + + doc.close(); } - public void testPLC() throws Exception { + @Test + public void testPLC() throws IOException { HPBFDocument doc = new HPBFDocument( _samples.openResourceAsStream("Simple.pub") ); @@ -133,9 +146,13 @@ public final class TestQuillContents extends TestCase { assertEquals(0x22000000, plc12.getPlcValB()[0]); assertEquals(0x05, plc12.getPlcValA()[1]); assertEquals(0x04, plc12.getPlcValB()[1]); + + doc.close(); } - public void testComplexPLC() throws Exception { + @SuppressWarnings("unused") + @Test + public void testComplexPLC() throws IOException { HPBFDocument doc = new HPBFDocument( _samples.openResourceAsStream("Sample.pub") ); @@ -234,9 +251,12 @@ public final class TestQuillContents extends TestCase { assertEquals(0x000004, plc16.getPlcValB()[4]); assertEquals(0x000004, plc16.getPlcValA()[5]); assertEquals(0x000004, plc16.getPlcValB()[5]); + + doc.close(); } - public void testNoHyperlinks() throws Exception { + @Test + public void testNoHyperlinks() throws IOException { HPBFDocument doc = new HPBFDocument( _samples.openResourceAsStream("SampleNewsletter.pub") ); @@ -250,9 +270,12 @@ public final class TestQuillContents extends TestCase { assertEquals(0, plc18.getNumberOfHyperlinks()); assertEquals(0, plc18.getTextStartAt(0)); assertEquals(601, plc18.getAllTextEndAt()); + + doc.close(); } - public void testSimpleHyperlink() throws Exception { + @Test + public void testSimpleHyperlink() throws IOException { HPBFDocument doc; QuillContents qc; Type12 hlBit; @@ -270,6 +293,7 @@ public final class TestQuillContents extends TestCase { assertEquals(10, hlBit.getTextStartAt(0)); assertEquals(15, hlBit.getAllTextEndAt()); assertEquals("http://poi.apache.org/", hlBit.getHyperlink(0)); + doc.close(); // Longer link at 10 doc = new HPBFDocument( @@ -284,6 +308,7 @@ public final class TestQuillContents extends TestCase { assertEquals(10, hlBit.getTextStartAt(0)); assertEquals(15, hlBit.getAllTextEndAt()); assertEquals("http://poi.apache.org/hpbf/", hlBit.getHyperlink(0)); + doc.close(); // Link at 20 doc = new HPBFDocument( @@ -298,9 +323,11 @@ public final class TestQuillContents extends TestCase { assertEquals(20, hlBit.getTextStartAt(0)); assertEquals(25, hlBit.getAllTextEndAt()); assertEquals("http://poi.apache.org/", hlBit.getHyperlink(0)); + doc.close(); } - public void testManyHyperlinks() throws Exception { + @Test + public void testManyHyperlinks() throws IOException { HPBFDocument doc; QuillContents qc; Type12 hlBit; @@ -318,10 +345,12 @@ public final class TestQuillContents extends TestCase { assertEquals(10, hlBit.getTextStartAt(0)); assertEquals(15, hlBit.getAllTextEndAt()); assertEquals("http://poi.apache.org/", hlBit.getHyperlink(0)); - + + doc.close(); } - public void testHyperlinkDifferentVersions() throws Exception { + @Test + public void testHyperlinkDifferentVersions() throws IOException { HPBFDocument doc; QuillContents qc; Type12 hlBitA; @@ -354,6 +383,7 @@ public final class TestQuillContents extends TestCase { assertEquals("", hlBitB.getHyperlink(0)); assertEquals("mailto:dev@poi.apache.org?subject=HPBF", hlBitB.getHyperlink(1)); assertEquals("mailto:dev@poi.apache.org?subject=HPBF", hlBitB.getHyperlink(2)); + doc.close(); // 2000 version doc = new HPBFDocument( @@ -382,6 +412,7 @@ public final class TestQuillContents extends TestCase { assertEquals("", hlBitB.getHyperlink(0)); assertEquals("mailto:dev@poi.apache.org?subject=HPBF", hlBitB.getHyperlink(1)); assertEquals("mailto:dev@poi.apache.org?subject=HPBF", hlBitB.getHyperlink(2)); + doc.close(); // 98 version doc = new HPBFDocument( @@ -410,5 +441,6 @@ public final class TestQuillContents extends TestCase { assertEquals("", hlBitB.getHyperlink(0)); assertEquals("mailto:dev@poi.apache.org?subject=HPBF", hlBitB.getHyperlink(1)); assertEquals("mailto:dev@poi.apache.org?subject=HPBF", hlBitB.getHyperlink(2)); - } + doc.close(); + } } diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/TestEncryptedFile.java b/src/scratchpad/testcases/org/apache/poi/hslf/TestEncryptedFile.java index ae4339427..a8060e7d3 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/TestEncryptedFile.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/TestEncryptedFile.java @@ -18,46 +18,58 @@ package org.apache.poi.hslf; -import junit.framework.TestCase; +import static org.junit.Assert.assertNotNull; +import java.io.IOException; +import java.io.InputStream; + +import org.apache.poi.POIDataSamples; import org.apache.poi.hslf.exceptions.EncryptedPowerPointFileException; import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl; -import org.apache.poi.POIDataSamples; +import org.junit.Test; /** * Tests that HSLFSlideShow does the right thing with an encrypted file - * - * @author Nick Burch (nick at torchbox dot com) */ -public final class TestEncryptedFile extends TestCase { - private static POIDataSamples slTests = POIDataSamples.getSlideShowInstance(); - - public void testLoadNonEncrypted() throws Exception { - HSLFSlideShowImpl hss = new HSLFSlideShowImpl(slTests.openResourceAsStream("basic_test_ppt_file.ppt")); +public final class TestEncryptedFile { + private static final POIDataSamples slTests = POIDataSamples.getSlideShowInstance(); + @Test + public void testLoadNonEncrypted() throws IOException { + InputStream is = slTests.openResourceAsStream("basic_test_ppt_file.ppt"); + HSLFSlideShowImpl hss = new HSLFSlideShowImpl(is); assertNotNull(hss); + hss.close(); + is.close(); } - public void testLoadEncrypted() throws Exception { + @Test(expected=EncryptedPowerPointFileException.class) + public void testLoadEncrypted1() throws IOException { + InputStream is = slTests.openResourceAsStream("Password_Protected-hello.ppt"); try { - new HSLFSlideShowImpl(slTests.openResourceAsStream("Password_Protected-hello.ppt")); - fail(); - } catch(EncryptedPowerPointFileException e) { - // Good + new HSLFSlideShowImpl(is).close(); + } finally { + is.close(); } - + } + + @Test(expected=EncryptedPowerPointFileException.class) + public void testLoadEncrypted2() throws IOException { + InputStream is = slTests.openResourceAsStream("Password_Protected-np-hello.ppt"); try { - new HSLFSlideShowImpl(slTests.openResourceAsStream("Password_Protected-np-hello.ppt")); - fail(); - } catch(EncryptedPowerPointFileException e) { - // Good + new HSLFSlideShowImpl(is).close(); + } finally { + is.close(); } - + } + + @Test(expected=EncryptedPowerPointFileException.class) + public void testLoadEncrypted3() throws IOException { + InputStream is = slTests.openResourceAsStream("Password_Protected-56-hello.ppt"); try { - new HSLFSlideShowImpl(slTests.openResourceAsStream("Password_Protected-56-hello.ppt")); - fail(); - } catch(EncryptedPowerPointFileException e) { - // Good + new HSLFSlideShowImpl(is).close(); + } finally { + is.close(); } } } diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/record/TestDocumentEncryption.java b/src/scratchpad/testcases/org/apache/poi/hslf/record/TestDocumentEncryption.java index 80e9f53d0..227f2c464 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/record/TestDocumentEncryption.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/record/TestDocumentEncryption.java @@ -77,7 +77,7 @@ public class TestDocumentEncryption { try { NPOIFSFileSystem fs = new NPOIFSFileSystem(slTests.getFile(pptFile), true); HSLFSlideShowImpl hss = new HSLFSlideShowImpl(fs); - new HSLFSlideShow(hss); + new HSLFSlideShow(hss).close(); fs.close(); } catch (EncryptedPowerPointFileException e) { fail(pptFile+" can't be decrypted"); @@ -99,17 +99,19 @@ public class TestDocumentEncryption { ByteArrayOutputStream bos = new ByteArrayOutputStream(); hss.write(bos); + hss.close(); fs.close(); fs = new NPOIFSFileSystem(new ByteArrayInputStream(bos.toByteArray())); hss = new HSLFSlideShowImpl(fs); List picsActual = hss.getPictureData(); - fs.close(); assertEquals(picsExpected.size(), picsActual.size()); for (int i=0; i -1) { + para.delete(); + deletedLength = text.length(); + } + } - int totalLength = 0, deletedLength = 0; + // check the text length after deletion + int newLength = 0; + range = doc.getRange(); + numParagraphs = range.numParagraphs(); - for (int i = 0; i < numParagraphs; i++) { - Paragraph para = range.getParagraph(i); - String text = para.text(); + for (int i = 0; i < numParagraphs; i++) { + Paragraph para = range.getParagraph(i); + String text = para.text(); - totalLength += text.length(); - if (text.indexOf("{delete me}") > -1) { - para.delete(); - deletedLength = text.length(); - } - } + newLength += text.length(); + } - // check the text length after deletion - int newLength = 0; - range = doc.getRange(); - numParagraphs = range.numParagraphs(); + assertEquals(newLength, totalLength - deletedLength); + + doc.close(); + } - for (int i = 0; i < numParagraphs; i++) { - Paragraph para = range.getParagraph(i); - String text = para.text(); + /** + * With an encrypted file, we should give a suitable exception, and not OOM + */ + @Test(expected=EncryptedDocumentException.class) + public void testEncryptedFile() throws IOException { + HWPFTestDataSamples.openSampleFile("PasswordProtected.doc"); + } - newLength += text.length(); - } + @Test + public void testWriteProperties() throws IOException { + HWPFDocument doc = HWPFTestDataSamples.openSampleFile("SampleDoc.doc"); + assertEquals("Nick Burch", doc.getSummaryInformation().getAuthor()); - assertEquals(newLength, totalLength - deletedLength); - } + // Write and read + HWPFDocument doc2 = writeOutAndRead(doc); + assertEquals("Nick Burch", doc2.getSummaryInformation().getAuthor()); + doc2.close(); + doc.close(); + } - /** - * With an encrypted file, we should give a suitable exception, and not OOM - */ - public void testEncryptedFile() { - try { - HWPFTestDataSamples.openSampleFile("PasswordProtected.doc"); - fail(); - } catch (EncryptedDocumentException e) { - // Good - } - } + /** + * Test for reading paragraphs from Range after replacing some text in this + * Range. Bug #45269 + */ + @Test + public void testReadParagraphsAfterReplaceText() throws IOException { + HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug45269.doc"); + Range range = doc.getRange(); - public void testWriteProperties() { - HWPFDocument doc = HWPFTestDataSamples.openSampleFile("SampleDoc.doc"); - assertEquals("Nick Burch", doc.getSummaryInformation().getAuthor()); + String toFind = "campo1"; + String longer = " foi porraaaaa "; + String shorter = " foi "; - // Write and read - HWPFDocument doc2 = writeOutAndRead(doc); - assertEquals("Nick Burch", doc2.getSummaryInformation().getAuthor()); - } + // check replace with longer text + for (int x = 0; x < range.numParagraphs(); x++) { + Paragraph para = range.getParagraph(x); + int offset = para.text().indexOf(toFind); + if (offset >= 0) { + para.replaceText(toFind, longer, offset); + assertEquals(offset, para.text().indexOf(longer)); + } + } - /** - * Test for reading paragraphs from Range after replacing some - * text in this Range. - * Bug #45269 - */ - public void testReadParagraphsAfterReplaceText()throws Exception{ - HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug45269.doc"); - Range range = doc.getRange(); + doc = HWPFTestDataSamples.openSampleFile("Bug45269.doc"); + range = doc.getRange(); - String toFind = "campo1"; - String longer = " foi porraaaaa "; - String shorter = " foi "; + // check replace with shorter text + for (int x = 0; x < range.numParagraphs(); x++) { + Paragraph para = range.getParagraph(x); + int offset = para.text().indexOf(toFind); + if (offset >= 0) { + para.replaceText(toFind, shorter, offset); + assertEquals(offset, para.text().indexOf(shorter)); + } + } + + doc.close(); + } - //check replace with longer text - for (int x = 0; x < range.numParagraphs(); x++) { - Paragraph para = range.getParagraph(x); - int offset = para.text().indexOf(toFind); - if (offset >= 0) { - para.replaceText(toFind, longer, offset); - assertEquals(offset, para.text().indexOf(longer)); - } - } + /** + * Bug #49936 - Problems with reading the header out of the Header Stories + */ + @SuppressWarnings("deprecation") + @Test + public void testProblemHeaderStories49936() throws IOException { + HWPFDocument doc = HWPFTestDataSamples.openSampleFile("HeaderFooterProblematic.doc"); + HeaderStories hs = new HeaderStories(doc); - doc = HWPFTestDataSamples.openSampleFile("Bug45269.doc"); - range = doc.getRange(); + assertEquals("", hs.getFirstHeader()); + assertEquals("\r", hs.getEvenHeader()); + assertEquals("", hs.getOddHeader()); - //check replace with shorter text - for (int x = 0; x < range.numParagraphs(); x++) { - Paragraph para = range.getParagraph(x); - int offset = para.text().indexOf(toFind); - if (offset >= 0) { - para.replaceText(toFind, shorter, offset); - assertEquals(offset, para.text().indexOf(shorter)); - } - } - } - - /** - * Bug #49936 - Problems with reading the header out of - * the Header Stories - */ - public void testProblemHeaderStories49936() throws Exception { - HWPFDocument doc = HWPFTestDataSamples.openSampleFile("HeaderFooterProblematic.doc"); - HeaderStories hs = new HeaderStories(doc); - - assertEquals("", hs.getFirstHeader()); - assertEquals("\r", hs.getEvenHeader()); - assertEquals("", hs.getOddHeader()); - - assertEquals("", hs.getFirstFooter()); - assertEquals("", hs.getEvenFooter()); - assertEquals("", hs.getOddFooter()); - - WordExtractor ext = new WordExtractor(doc); - assertEquals("\n", ext.getHeaderText()); - assertEquals("", ext.getFooterText()); - } - - /** - * Bug #45877 - problematic PAPX with no parent set - */ - public void testParagraphPAPXNoParent45877() throws Exception { - HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug45877.doc"); - assertEquals(17, doc.getRange().numParagraphs()); - - assertEquals("First paragraph\r", doc.getRange().getParagraph(0).text()); - assertEquals("After Crashing Part\r", doc.getRange().getParagraph(13).text()); - } + assertEquals("", hs.getFirstFooter()); + assertEquals("", hs.getEvenFooter()); + assertEquals("", hs.getOddFooter()); - /** - * Bug #48245 - don't include the text from the - * next cell in the current one - */ - public void testTableIterator() throws Exception { - HWPFDocument doc = HWPFTestDataSamples.openSampleFile("simple-table2.doc"); - Range r = doc.getRange(); + WordExtractor ext = new WordExtractor(doc); + assertEquals("\n", ext.getHeaderText()); + assertEquals("", ext.getFooterText()); + ext.close(); + doc.close(); + } - // Check the text is as we'd expect - assertEquals(13, r.numParagraphs()); - assertEquals("Row 1/Cell 1\u0007", r.getParagraph(0).text()); - assertEquals("Row 1/Cell 2\u0007", r.getParagraph(1).text()); - assertEquals("Row 1/Cell 3\u0007", r.getParagraph(2).text()); - assertEquals("\u0007", r.getParagraph(3).text()); - assertEquals("Row 2/Cell 1\u0007", r.getParagraph(4).text()); - assertEquals("Row 2/Cell 2\u0007", r.getParagraph(5).text()); - assertEquals("Row 2/Cell 3\u0007", r.getParagraph(6).text()); - assertEquals("\u0007", r.getParagraph(7).text()); - assertEquals("Row 3/Cell 1\u0007", r.getParagraph(8).text()); - assertEquals("Row 3/Cell 2\u0007", r.getParagraph(9).text()); - assertEquals("Row 3/Cell 3\u0007", r.getParagraph(10).text()); - assertEquals("\u0007", r.getParagraph(11).text()); - assertEquals("\r", r.getParagraph(12).text()); + /** + * Bug #45877 - problematic PAPX with no parent set + */ + @Test + public void testParagraphPAPXNoParent45877() throws IOException { + HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug45877.doc"); + assertEquals(17, doc.getRange().numParagraphs()); - Paragraph p; + assertEquals("First paragraph\r", + doc.getRange().getParagraph(0).text()); + assertEquals("After Crashing Part\r", + doc.getRange().getParagraph(13).text()); + + doc.close(); + } - // Take a look in detail at the first couple of - // paragraphs - p = r.getParagraph(0); - assertEquals(1, p.numParagraphs()); - assertEquals(0, p.getStartOffset()); - assertEquals(13, p.getEndOffset()); - assertEquals(0, p._parStart); - assertEquals(1, p._parEnd); + /** + * Bug #48245 - don't include the text from the next cell in the current one + */ + @Test + public void testTableIterator() throws IOException { + HWPFDocument doc = HWPFTestDataSamples.openSampleFile("simple-table2.doc"); + Range r = doc.getRange(); - p = r.getParagraph(1); - assertEquals(1, p.numParagraphs()); - assertEquals(13, p.getStartOffset()); - assertEquals(26, p.getEndOffset()); - assertEquals(1, p._parStart); - assertEquals(2, p._parEnd); + // Check the text is as we'd expect + assertEquals(13, r.numParagraphs()); + assertEquals("Row 1/Cell 1\u0007", r.getParagraph(0).text()); + assertEquals("Row 1/Cell 2\u0007", r.getParagraph(1).text()); + assertEquals("Row 1/Cell 3\u0007", r.getParagraph(2).text()); + assertEquals("\u0007", r.getParagraph(3).text()); + assertEquals("Row 2/Cell 1\u0007", r.getParagraph(4).text()); + assertEquals("Row 2/Cell 2\u0007", r.getParagraph(5).text()); + assertEquals("Row 2/Cell 3\u0007", r.getParagraph(6).text()); + assertEquals("\u0007", r.getParagraph(7).text()); + assertEquals("Row 3/Cell 1\u0007", r.getParagraph(8).text()); + assertEquals("Row 3/Cell 2\u0007", r.getParagraph(9).text()); + assertEquals("Row 3/Cell 3\u0007", r.getParagraph(10).text()); + assertEquals("\u0007", r.getParagraph(11).text()); + assertEquals("\r", r.getParagraph(12).text()); - p = r.getParagraph(2); - assertEquals(1, p.numParagraphs()); - assertEquals(26, p.getStartOffset()); - assertEquals(39, p.getEndOffset()); - assertEquals(2, p._parStart); - assertEquals(3, p._parEnd); + Paragraph p; + // Take a look in detail at the first couple of + // paragraphs + p = r.getParagraph(0); + assertEquals(1, p.numParagraphs()); + assertEquals(0, p.getStartOffset()); + assertEquals(13, p.getEndOffset()); + assertEquals(0, p._parStart); + assertEquals(1, p._parEnd); - // Now look at the table - Table table = r.getTable(r.getParagraph(0)); - assertEquals(3, table.numRows()); + p = r.getParagraph(1); + assertEquals(1, p.numParagraphs()); + assertEquals(13, p.getStartOffset()); + assertEquals(26, p.getEndOffset()); + assertEquals(1, p._parStart); + assertEquals(2, p._parEnd); - TableRow row; - TableCell cell; + p = r.getParagraph(2); + assertEquals(1, p.numParagraphs()); + assertEquals(26, p.getStartOffset()); + assertEquals(39, p.getEndOffset()); + assertEquals(2, p._parStart); + assertEquals(3, p._parEnd); + // Now look at the table + Table table = r.getTable(r.getParagraph(0)); + assertEquals(3, table.numRows()); - row = table.getRow(0); - assertEquals(0, row._parStart); - assertEquals(4, row._parEnd); + TableRow row; + TableCell cell; - cell = row.getCell(0); - assertEquals(1, cell.numParagraphs()); - assertEquals(0, cell._parStart); - assertEquals(1, cell._parEnd); - assertEquals(0, cell.getStartOffset()); - assertEquals(13, cell.getEndOffset()); - assertEquals("Row 1/Cell 1\u0007", cell.text()); - assertEquals("Row 1/Cell 1\u0007", cell.getParagraph(0).text()); + row = table.getRow(0); + assertEquals(0, row._parStart); + assertEquals(4, row._parEnd); - cell = row.getCell(1); - assertEquals(1, cell.numParagraphs()); - assertEquals(1, cell._parStart); - assertEquals(2, cell._parEnd); - assertEquals(13, cell.getStartOffset()); - assertEquals(26, cell.getEndOffset()); - assertEquals("Row 1/Cell 2\u0007", cell.text()); - assertEquals("Row 1/Cell 2\u0007", cell.getParagraph(0).text()); + cell = row.getCell(0); + assertEquals(1, cell.numParagraphs()); + assertEquals(0, cell._parStart); + assertEquals(1, cell._parEnd); + assertEquals(0, cell.getStartOffset()); + assertEquals(13, cell.getEndOffset()); + assertEquals("Row 1/Cell 1\u0007", cell.text()); + assertEquals("Row 1/Cell 1\u0007", cell.getParagraph(0).text()); - cell = row.getCell(2); - assertEquals(1, cell.numParagraphs()); - assertEquals(2, cell._parStart); - assertEquals(3, cell._parEnd); - assertEquals(26, cell.getStartOffset()); - assertEquals(39, cell.getEndOffset()); - assertEquals("Row 1/Cell 3\u0007", cell.text()); - assertEquals("Row 1/Cell 3\u0007", cell.getParagraph(0).text()); + cell = row.getCell(1); + assertEquals(1, cell.numParagraphs()); + assertEquals(1, cell._parStart); + assertEquals(2, cell._parEnd); + assertEquals(13, cell.getStartOffset()); + assertEquals(26, cell.getEndOffset()); + assertEquals("Row 1/Cell 2\u0007", cell.text()); + assertEquals("Row 1/Cell 2\u0007", cell.getParagraph(0).text()); + cell = row.getCell(2); + assertEquals(1, cell.numParagraphs()); + assertEquals(2, cell._parStart); + assertEquals(3, cell._parEnd); + assertEquals(26, cell.getStartOffset()); + assertEquals(39, cell.getEndOffset()); + assertEquals("Row 1/Cell 3\u0007", cell.text()); + assertEquals("Row 1/Cell 3\u0007", cell.getParagraph(0).text()); - // Onto row #2 - row = table.getRow(1); - assertEquals(4, row._parStart); - assertEquals(8, row._parEnd); + // Onto row #2 + row = table.getRow(1); + assertEquals(4, row._parStart); + assertEquals(8, row._parEnd); - cell = row.getCell(0); - assertEquals(1, cell.numParagraphs()); - assertEquals(4, cell._parStart); - assertEquals(5, cell._parEnd); - assertEquals(40, cell.getStartOffset()); - assertEquals(53, cell.getEndOffset()); - assertEquals("Row 2/Cell 1\u0007", cell.text()); + cell = row.getCell(0); + assertEquals(1, cell.numParagraphs()); + assertEquals(4, cell._parStart); + assertEquals(5, cell._parEnd); + assertEquals(40, cell.getStartOffset()); + assertEquals(53, cell.getEndOffset()); + assertEquals("Row 2/Cell 1\u0007", cell.text()); - cell = row.getCell(1); - assertEquals(1, cell.numParagraphs()); - assertEquals(5, cell._parStart); - assertEquals(6, cell._parEnd); - assertEquals(53, cell.getStartOffset()); - assertEquals(66, cell.getEndOffset()); - assertEquals("Row 2/Cell 2\u0007", cell.text()); + cell = row.getCell(1); + assertEquals(1, cell.numParagraphs()); + assertEquals(5, cell._parStart); + assertEquals(6, cell._parEnd); + assertEquals(53, cell.getStartOffset()); + assertEquals(66, cell.getEndOffset()); + assertEquals("Row 2/Cell 2\u0007", cell.text()); - cell = row.getCell(2); - assertEquals(1, cell.numParagraphs()); - assertEquals(6, cell._parStart); - assertEquals(7, cell._parEnd); - assertEquals(66, cell.getStartOffset()); - assertEquals(79, cell.getEndOffset()); - assertEquals("Row 2/Cell 3\u0007", cell.text()); + cell = row.getCell(2); + assertEquals(1, cell.numParagraphs()); + assertEquals(6, cell._parStart); + assertEquals(7, cell._parEnd); + assertEquals(66, cell.getStartOffset()); + assertEquals(79, cell.getEndOffset()); + assertEquals("Row 2/Cell 3\u0007", cell.text()); + // Finally row 3 + row = table.getRow(2); + assertEquals(8, row._parStart); + assertEquals(12, row._parEnd); - // Finally row 3 - row = table.getRow(2); - assertEquals(8, row._parStart); - assertEquals(12, row._parEnd); + cell = row.getCell(0); + assertEquals(1, cell.numParagraphs()); + assertEquals(8, cell._parStart); + assertEquals(9, cell._parEnd); + assertEquals(80, cell.getStartOffset()); + assertEquals(93, cell.getEndOffset()); + assertEquals("Row 3/Cell 1\u0007", cell.text()); - cell = row.getCell(0); - assertEquals(1, cell.numParagraphs()); - assertEquals(8, cell._parStart); - assertEquals(9, cell._parEnd); - assertEquals(80, cell.getStartOffset()); - assertEquals(93, cell.getEndOffset()); - assertEquals("Row 3/Cell 1\u0007", cell.text()); - - cell = row.getCell(1); - assertEquals(1, cell.numParagraphs()); - assertEquals(9, cell._parStart); - assertEquals(10, cell._parEnd); - assertEquals(93, cell.getStartOffset()); - assertEquals(106, cell.getEndOffset()); - assertEquals("Row 3/Cell 2\u0007", cell.text()); - - cell = row.getCell(2); - assertEquals(1, cell.numParagraphs()); - assertEquals(10, cell._parStart); - assertEquals(11, cell._parEnd); - assertEquals(106, cell.getStartOffset()); - assertEquals(119, cell.getEndOffset()); - assertEquals("Row 3/Cell 3\u0007", cell.text()); - } + cell = row.getCell(1); + assertEquals(1, cell.numParagraphs()); + assertEquals(9, cell._parStart); + assertEquals(10, cell._parEnd); + assertEquals(93, cell.getStartOffset()); + assertEquals(106, cell.getEndOffset()); + assertEquals("Row 3/Cell 2\u0007", cell.text()); + cell = row.getCell(2); + assertEquals(1, cell.numParagraphs()); + assertEquals(10, cell._parStart); + assertEquals(11, cell._parEnd); + assertEquals(106, cell.getStartOffset()); + assertEquals(119, cell.getEndOffset()); + assertEquals("Row 3/Cell 3\u0007", cell.text()); + + doc.close(); + } }