aboutsummaryrefslogtreecommitdiffstats
path: root/src/scratchpad/testcases/org/apache
diff options
context:
space:
mode:
authorAndreas Beeker <kiwiwings@apache.org>2018-09-14 21:37:37 +0000
committerAndreas Beeker <kiwiwings@apache.org>2018-09-14 21:37:37 +0000
commita6a55a6ca514f9e76a784aadbec0d6d774ba8b9a (patch)
tree2308c1b5ab0f95d0ffdaf6666d1031e3e9538261 /src/scratchpad/testcases/org/apache
parent33a4b2f3fbc4b83e844dd0e33becbc03635afd67 (diff)
downloadpoi-a6a55a6ca514f9e76a784aadbec0d6d774ba8b9a.tar.gz
poi-a6a55a6ca514f9e76a784aadbec0d6d774ba8b9a.zip
#60656 - Support export file that contains emf and render it correctly
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/hemf@1840956 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/scratchpad/testcases/org/apache')
-rw-r--r--src/scratchpad/testcases/org/apache/poi/hemf/extractor/HemfExtractorTest.java198
-rw-r--r--src/scratchpad/testcases/org/apache/poi/hemf/hemfplus/extractor/HemfPlusExtractorTest.java32
-rw-r--r--src/scratchpad/testcases/org/apache/poi/hemf/usermodel/HemfPictureTest.java201
-rw-r--r--src/scratchpad/testcases/org/apache/poi/hwmf/TestHwmfParsing.java8
4 files changed, 219 insertions, 220 deletions
diff --git a/src/scratchpad/testcases/org/apache/poi/hemf/extractor/HemfExtractorTest.java b/src/scratchpad/testcases/org/apache/poi/hemf/extractor/HemfExtractorTest.java
deleted file mode 100644
index 8d32b21a5e..0000000000
--- a/src/scratchpad/testcases/org/apache/poi/hemf/extractor/HemfExtractorTest.java
+++ /dev/null
@@ -1,198 +0,0 @@
-/* ====================================================================
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-==================================================================== */
-
-
-package org.apache.poi.hemf.extractor;
-
-import static org.apache.poi.POITestCase.assertContains;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.InputStream;
-import java.util.HashSet;
-import java.util.Set;
-
-import org.apache.poi.POIDataSamples;
-import org.apache.poi.hemf.record.AbstractHemfComment;
-import org.apache.poi.hemf.record.HemfCommentPublic;
-import org.apache.poi.hemf.record.HemfCommentRecord;
-import org.apache.poi.hemf.record.HemfHeader;
-import org.apache.poi.hemf.record.HemfRecord;
-import org.apache.poi.hemf.record.HemfRecordType;
-import org.apache.poi.hemf.record.HemfText;
-import org.apache.poi.util.IOUtils;
-import org.apache.poi.util.RecordFormatException;
-import org.junit.Test;
-
-public class HemfExtractorTest {
-
- @Test
- public void testBasicWindows() throws Exception {
- InputStream is = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SimpleEMF_windows.emf");
- HemfExtractor ex = new HemfExtractor(is);
- HemfHeader header = ex.getHeader();
- assertEquals(27864, header.getBytes());
- assertEquals(31, header.getRecords());
- assertEquals(3, header.getHandles());
- assertEquals(346000, header.getMicrometersX());
- assertEquals(194000, header.getMicrometersY());
-
- int records = 0;
- for (HemfRecord record : ex) {
- records++;
- }
-
- assertEquals(header.getRecords() - 1, records);
- }
-
- @Test
- public void testBasicMac() throws Exception {
- InputStream is =
- POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SimpleEMF_mac.emf");
- HemfExtractor ex = new HemfExtractor(is);
- HemfHeader header = ex.getHeader();
-
- int records = 0;
- boolean extractedData = false;
- for (HemfRecord record : ex) {
- if (record.getRecordType() == HemfRecordType.comment) {
- AbstractHemfComment comment = ((HemfCommentRecord) record).getComment();
- if (comment instanceof HemfCommentPublic.MultiFormats) {
- for (HemfCommentPublic.HemfMultiFormatsData d : ((HemfCommentPublic.MultiFormats) comment).getData()) {
- byte[] data = d.getData();
- //make sure header starts at 0
- assertEquals('%', data[0]);
- assertEquals('P', data[1]);
- assertEquals('D', data[2]);
- assertEquals('F', data[3]);
-
- //make sure byte array ends at EOF\n
- assertEquals('E', data[data.length - 4]);
- assertEquals('O', data[data.length - 3]);
- assertEquals('F', data[data.length - 2]);
- assertEquals('\n', data[data.length - 1]);
- extractedData = true;
- }
- }
- }
- records++;
- }
- assertTrue(extractedData);
- assertEquals(header.getRecords() - 1, records);
- }
-
- @Test
- public void testMacText() throws Exception {
- InputStream is =
- POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SimpleEMF_mac.emf");
- HemfExtractor ex = new HemfExtractor(is);
-
- long lastY = -1;
- long lastX = -1;
- long fudgeFactorX = 1000;//derive this from the font information!
- StringBuilder sb = new StringBuilder();
- for (HemfRecord record : ex) {
- if (record.getRecordType().equals(HemfRecordType.exttextoutw)) {
- HemfText.ExtTextOutW extTextOutW = (HemfText.ExtTextOutW) record;
- if (lastY > -1 && lastY != extTextOutW.getY()) {
- sb.append("\n");
- lastX = -1;
- }
- if (lastX > -1 && extTextOutW.getX() - lastX > fudgeFactorX) {
- sb.append(" ");
- }
- sb.append(extTextOutW.getText());
- lastY = extTextOutW.getY();
- lastX = extTextOutW.getX();
- }
- }
- String txt = sb.toString();
- assertContains(txt, "Tika http://incubator.apache.org");
- assertContains(txt, "Latest News\n");
- }
-
- @Test
- public void testWindowsText() throws Exception {
- InputStream is = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SimpleEMF_windows.emf");
- HemfExtractor ex = new HemfExtractor(is);
- long lastY = -1;
- long lastX = -1;
- long fudgeFactorX = 1000;//derive this from the font or frame/bounds information
- StringBuilder sb = new StringBuilder();
- Set<String> expectedParts = new HashSet<>();
- expectedParts.add("C:\\Users\\tallison\\");
- expectedParts.add("testPDF.pdf");
- int foundExpected = 0;
- for (HemfRecord record : ex) {
- if (record.getRecordType().equals(HemfRecordType.exttextoutw)) {
- HemfText.ExtTextOutW extTextOutW = (HemfText.ExtTextOutW) record;
- if (lastY > -1 && lastY != extTextOutW.getY()) {
- sb.append("\n");
- lastX = -1;
- }
- if (lastX > -1 && extTextOutW.getX() - lastX > fudgeFactorX) {
- sb.append(" ");
- }
- String txt = extTextOutW.getText();
- if (expectedParts.contains(txt)) {
- foundExpected++;
- }
- sb.append(txt);
- lastY = extTextOutW.getY();
- lastX = extTextOutW.getX();
- }
- }
- String txt = sb.toString();
- assertContains(txt, "C:\\Users\\tallison\\\n");
- assertContains(txt, "asf2-git-1.x\\tika-\n");
- assertEquals(expectedParts.size(), foundExpected);
- }
-
- @Test(expected = RecordFormatException.class)
- public void testInfiniteLoopOnFile() throws Exception {
- InputStream is = null;
- try {
- is = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("61294.emf");
-
- HemfExtractor ex = new HemfExtractor(is);
- for (HemfRecord record : ex) {
-
- }
- } finally {
- IOUtils.closeQuietly(is);
- }
- }
-
- @Test(expected = RecordFormatException.class)
- public void testInfiniteLoopOnByteArray() throws Exception {
- InputStream is = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("61294.emf");
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- IOUtils.copy(is, bos);
- is.close();
-
- HemfExtractor ex = new HemfExtractor(new ByteArrayInputStream(bos.toByteArray()));
- for (HemfRecord record : ex) {
-
- }
- }
-
- /*
- govdocs1 064213.doc-0.emf contains an example of extextouta
- */
-} \ No newline at end of file
diff --git a/src/scratchpad/testcases/org/apache/poi/hemf/hemfplus/extractor/HemfPlusExtractorTest.java b/src/scratchpad/testcases/org/apache/poi/hemf/hemfplus/extractor/HemfPlusExtractorTest.java
index f3bdbf7b4f..5811accac5 100644
--- a/src/scratchpad/testcases/org/apache/poi/hemf/hemfplus/extractor/HemfPlusExtractorTest.java
+++ b/src/scratchpad/testcases/org/apache/poi/hemf/hemfplus/extractor/HemfPlusExtractorTest.java
@@ -25,13 +25,13 @@ import java.util.ArrayList;
import java.util.List;
import org.apache.poi.POIDataSamples;
-import org.apache.poi.hemf.extractor.HemfExtractor;
-import org.apache.poi.hemf.hemfplus.record.HemfPlusHeader;
-import org.apache.poi.hemf.hemfplus.record.HemfPlusRecord;
-import org.apache.poi.hemf.hemfplus.record.HemfPlusRecordType;
-import org.apache.poi.hemf.record.HemfCommentEMFPlus;
-import org.apache.poi.hemf.record.HemfCommentRecord;
-import org.apache.poi.hemf.record.HemfRecord;
+import org.apache.poi.hemf.record.emf.HemfComment.EmfComment;
+import org.apache.poi.hemf.record.emf.HemfComment.EmfCommentDataPlus;
+import org.apache.poi.hemf.record.emf.HemfRecord;
+import org.apache.poi.hemf.record.emfplus.HemfPlusHeader;
+import org.apache.poi.hemf.record.emfplus.HemfPlusRecord;
+import org.apache.poi.hemf.record.emfplus.HemfPlusRecordType;
+import org.apache.poi.hemf.usermodel.HemfPicture;
import org.junit.Test;
public class HemfPlusExtractorTest {
@@ -39,7 +39,7 @@ public class HemfPlusExtractorTest {
@Test
public void testBasic() throws Exception {
//test header
- HemfCommentEMFPlus emfPlus = getCommentRecord("SimpleEMF_windows.emf", 0);
+ EmfCommentDataPlus emfPlus = getCommentRecord("SimpleEMF_windows.emf", 0);
List<HemfPlusRecord> records = emfPlus.getRecords();
assertEquals(1, records.size());
assertEquals(HemfPlusRecordType.header, records.get(0).getRecordType());
@@ -72,24 +72,20 @@ public class HemfPlusExtractorTest {
}
- private HemfCommentEMFPlus getCommentRecord(String testFileName, int recordIndex) throws Exception {
- InputStream is = null;
- HemfCommentEMFPlus returnRecord = null;
+ private EmfCommentDataPlus getCommentRecord(String testFileName, int recordIndex) throws Exception {
+ EmfCommentDataPlus returnRecord = null;
- try {
- is = POIDataSamples.getSpreadSheetInstance().openResourceAsStream(testFileName);
- HemfExtractor ex = new HemfExtractor(is);
+ try (InputStream is = POIDataSamples.getSpreadSheetInstance().openResourceAsStream(testFileName)) {
+ HemfPicture ex = new HemfPicture(is);
int i = 0;
for (HemfRecord record : ex) {
if (i == recordIndex) {
- HemfCommentRecord commentRecord = ((HemfCommentRecord) record);
- returnRecord = (HemfCommentEMFPlus) commentRecord.getComment();
+ EmfComment commentRecord = ((EmfComment) record);
+ returnRecord = (EmfCommentDataPlus) commentRecord.getCommentData();
break;
}
i++;
}
- } finally {
- is.close();
}
return returnRecord;
}
diff --git a/src/scratchpad/testcases/org/apache/poi/hemf/usermodel/HemfPictureTest.java b/src/scratchpad/testcases/org/apache/poi/hemf/usermodel/HemfPictureTest.java
new file mode 100644
index 0000000000..f0e259df4d
--- /dev/null
+++ b/src/scratchpad/testcases/org/apache/poi/hemf/usermodel/HemfPictureTest.java
@@ -0,0 +1,201 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+
+package org.apache.poi.hemf.usermodel;
+
+import static org.apache.poi.POITestCase.assertContains;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.awt.geom.Point2D;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.poi.POIDataSamples;
+import org.apache.poi.hemf.record.emf.HemfComment;
+import org.apache.poi.hemf.record.emf.HemfComment.EmfComment;
+import org.apache.poi.hemf.record.emf.HemfComment.EmfCommentDataFormat;
+import org.apache.poi.hemf.record.emf.HemfComment.EmfCommentDataMultiformats;
+import org.apache.poi.hemf.record.emf.HemfHeader;
+import org.apache.poi.hemf.record.emf.HemfRecord;
+import org.apache.poi.hemf.record.emf.HemfRecordType;
+import org.apache.poi.hemf.record.emf.HemfText;
+import org.apache.poi.util.IOUtils;
+import org.apache.poi.util.RecordFormatException;
+import org.junit.Test;
+
+public class HemfPictureTest {
+
+ private POIDataSamples samples = POIDataSamples.getSpreadSheetInstance();
+
+ @Test
+ public void testBasicWindows() throws Exception {
+ try (InputStream is = samples.openResourceAsStream("SimpleEMF_windows.emf")) {
+ HemfPicture pic = new HemfPicture(is);
+ HemfHeader header = pic.getHeader();
+ assertEquals(27864, header.getBytes());
+ assertEquals(31, header.getRecords());
+ assertEquals(3, header.getHandles());
+ assertEquals(346000, header.getMicrometersX());
+ assertEquals(194000, header.getMicrometersY());
+
+ List<HemfRecord> records = pic.getRecords();
+
+ assertEquals(31, records.size());
+ }
+ }
+
+ @Test
+ public void testBasicMac() throws Exception {
+ try (InputStream is = samples.openResourceAsStream("SimpleEMF_mac.emf")) {
+ HemfPicture pic = new HemfPicture(is);
+ HemfHeader header = pic.getHeader();
+
+ int records = 0;
+ boolean extractedData = false;
+ for (HemfRecord record : pic) {
+ if (record.getEmfRecordType() == HemfRecordType.comment) {
+ HemfComment.EmfCommentData comment = ((EmfComment) record).getCommentData();
+ if (comment instanceof EmfCommentDataMultiformats) {
+ for (EmfCommentDataFormat d : ((EmfCommentDataMultiformats) comment).getFormats()) {
+ byte[] data = d.getRawData();
+ //make sure header starts at 0
+ assertEquals('%', data[0]);
+ assertEquals('P', data[1]);
+ assertEquals('D', data[2]);
+ assertEquals('F', data[3]);
+
+ //make sure byte array ends at EOF\n
+ assertEquals('E', data[data.length - 4]);
+ assertEquals('O', data[data.length - 3]);
+ assertEquals('F', data[data.length - 2]);
+ assertEquals('\n', data[data.length - 1]);
+ extractedData = true;
+ }
+ }
+ }
+ records++;
+ }
+ assertTrue(extractedData);
+ assertEquals(header.getRecords(), records);
+ }
+ }
+
+ @Test
+ public void testMacText() throws Exception {
+ try (InputStream is = samples.openResourceAsStream("SimpleEMF_mac.emf")) {
+ HemfPicture pic = new HemfPicture(is);
+
+ double lastY = -1;
+ double lastX = -1;
+ //derive this from the font information!
+ long fudgeFactorX = 1000;
+ StringBuilder sb = new StringBuilder();
+ for (HemfRecord record : pic) {
+ if (record.getEmfRecordType().equals(HemfRecordType.exttextoutw)) {
+ HemfText.ExtTextOutW extTextOutW = (HemfText.ExtTextOutW) record;
+ Point2D reference = extTextOutW.getTextObject().getReference();
+ if (lastY > -1 && lastY != reference.getY()) {
+ sb.append("\n");
+ lastX = -1;
+ }
+ if (lastX > -1 && reference.getX() - lastX > fudgeFactorX) {
+ sb.append(" ");
+ }
+ sb.append(extTextOutW.getText());
+ lastY = reference.getY();
+ lastX = reference.getX();
+ }
+ }
+ String txt = sb.toString();
+ assertContains(txt, "Tika http://incubator.apache.org");
+ assertContains(txt, "Latest News\n");
+ }
+ }
+
+ @Test
+ public void testWindowsText() throws Exception {
+ try (InputStream is = samples.openResourceAsStream("SimpleEMF_windows.emf")) {
+ HemfPicture pic = new HemfPicture(is);
+ double lastY = -1;
+ double lastX = -1;
+ long fudgeFactorX = 1000;//derive this from the font or frame/bounds information
+ StringBuilder sb = new StringBuilder();
+ Set<String> expectedParts = new HashSet<>();
+ expectedParts.add("C:\\Users\\tallison\\");
+ expectedParts.add("testPDF.pdf");
+ int foundExpected = 0;
+ for (HemfRecord record : pic) {
+ if (record.getEmfRecordType().equals(HemfRecordType.exttextoutw)) {
+ HemfText.ExtTextOutW extTextOutW = (HemfText.ExtTextOutW) record;
+ Point2D reference = extTextOutW.getTextObject().getReference();
+ if (lastY > -1 && lastY != reference.getY()) {
+ sb.append("\n");
+ lastX = -1;
+ }
+ if (lastX > -1 && reference.getX() - lastX > fudgeFactorX) {
+ sb.append(" ");
+ }
+ String txt = extTextOutW.getText();
+ if (expectedParts.contains(txt)) {
+ foundExpected++;
+ }
+ sb.append(txt);
+ lastY = reference.getY();
+ lastX = reference.getX();
+ }
+ }
+ String txt = sb.toString();
+ assertContains(txt, "C:\\Users\\tallison\\\n");
+ assertContains(txt, "asf2-git-1.x\\tika-\n");
+ assertEquals(expectedParts.size(), foundExpected);
+ }
+ }
+
+ @Test(expected = RecordFormatException.class)
+ public void testInfiniteLoopOnFile() throws Exception {
+ try (InputStream is = samples.openResourceAsStream("61294.emf")) {
+ HemfPicture pic = new HemfPicture(is);
+ for (HemfRecord record : pic) {
+
+ }
+ }
+ }
+
+ @Test(expected = RecordFormatException.class)
+ public void testInfiniteLoopOnByteArray() throws Exception {
+ try (InputStream is = samples.openResourceAsStream("61294.emf")) {
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ IOUtils.copy(is, bos);
+ is.close();
+
+ HemfPicture pic = new HemfPicture(new ByteArrayInputStream(bos.toByteArray()));
+ for (HemfRecord record : pic) {
+
+ }
+ }
+ }
+
+ /*
+ govdocs1 064213.doc-0.emf contains an example of extextouta
+ */
+} \ No newline at end of file
diff --git a/src/scratchpad/testcases/org/apache/poi/hwmf/TestHwmfParsing.java b/src/scratchpad/testcases/org/apache/poi/hwmf/TestHwmfParsing.java
index 1667b67b49..df084cdad3 100644
--- a/src/scratchpad/testcases/org/apache/poi/hwmf/TestHwmfParsing.java
+++ b/src/scratchpad/testcases/org/apache/poi/hwmf/TestHwmfParsing.java
@@ -222,11 +222,11 @@ public class TestHwmfParsing {
//this happens to work on this test file, but you need to
//do what Graphics does by maintaining the stack, etc.!
for (HwmfRecord r : wmf.getRecords()) {
- if (r.getRecordType().equals(HwmfRecordType.createFontIndirect)) {
+ if (r.getWmfRecordType().equals(HwmfRecordType.createFontIndirect)) {
HwmfFont font = ((HwmfText.WmfCreateFontIndirect)r).getFont();
charset = (font.getCharset().getCharset() == null) ? LocaleUtil.CHARSET_1252 : font.getCharset().getCharset();
}
- if (r.getRecordType().equals(HwmfRecordType.extTextOut)) {
+ if (r.getWmfRecordType().equals(HwmfRecordType.extTextOut)) {
HwmfText.WmfExtTextOut textOut = (HwmfText.WmfExtTextOut)r;
sb.append(textOut.getText(charset)).append("\n");
}
@@ -250,11 +250,11 @@ public class TestHwmfParsing {
//this happens to work on this test file, but you need to
//do what Graphics does by maintaining the stack, etc.!
for (HwmfRecord r : wmf.getRecords()) {
- if (r.getRecordType().equals(HwmfRecordType.createFontIndirect)) {
+ if (r.getWmfRecordType().equals(HwmfRecordType.createFontIndirect)) {
HwmfFont font = ((HwmfText.WmfCreateFontIndirect)r).getFont();
charset = (font.getCharset().getCharset() == null) ? LocaleUtil.CHARSET_1252 : font.getCharset().getCharset();
}
- if (r.getRecordType().equals(HwmfRecordType.extTextOut)) {
+ if (r.getWmfRecordType().equals(HwmfRecordType.extTextOut)) {
HwmfText.WmfExtTextOut textOut = (HwmfText.WmfExtTextOut)r;
sb.append(textOut.getText(charset)).append("\n");
}