aboutsummaryrefslogtreecommitdiffstats
path: root/src/testcases/org/apache/poi/hssf/usermodel
diff options
context:
space:
mode:
authorYegor Kozlov <yegor@apache.org>2013-10-13 07:39:40 +0000
committerYegor Kozlov <yegor@apache.org>2013-10-13 07:39:40 +0000
commitf6cb7f14acf47450b536174202a05ce793a2cd52 (patch)
treeebba94f3cbad64f6038ee79826ebd37a70cb6431 /src/testcases/org/apache/poi/hssf/usermodel
parent1ffc66625a01158ff5411d7dcdac45352ca786cc (diff)
downloadpoi-f6cb7f14acf47450b536174202a05ce793a2cd52.tar.gz
poi-f6cb7f14acf47450b536174202a05ce793a2cd52.zip
Bugzilla 55578 - Support embedding OLE1.0 packages in HSSF
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1531623 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases/org/apache/poi/hssf/usermodel')
-rw-r--r--src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPicture.java5
-rw-r--r--src/testcases/org/apache/poi/hssf/usermodel/TestOLE2Embeding.java112
2 files changed, 111 insertions, 6 deletions
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPicture.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPicture.java
index de8ffabf8b..de2b683a7e 100644
--- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPicture.java
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPicture.java
@@ -262,10 +262,7 @@ public final class TestHSSFPicture extends BaseTestPicture {
System.arraycopy(pictureDataWmf, 22, wmfNoHeader, 0, pictureDataWmf.length-22);
pictureDataOut = wb.getAllPictures().get(2).getData();
assertTrue(Arrays.equals(wmfNoHeader, pictureDataOut));
-
- FileOutputStream fos = new FileOutputStream("vect.xls");
- wb.write(fos);
- fos.close();
+
}
}
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestOLE2Embeding.java b/src/testcases/org/apache/poi/hssf/usermodel/TestOLE2Embeding.java
index 661b1d51f1..22700b28cc 100644
--- a/src/testcases/org/apache/poi/hssf/usermodel/TestOLE2Embeding.java
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestOLE2Embeding.java
@@ -17,11 +17,23 @@
package org.apache.poi.hssf.usermodel;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Arrays;
import java.util.List;
import junit.framework.TestCase;
+import org.apache.poi.POIDataSamples;
import org.apache.poi.hssf.HSSFTestDataSamples;
+import org.apache.poi.poifs.filesystem.DirectoryNode;
+import org.apache.poi.poifs.filesystem.Ole10Native;
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import org.apache.poi.ss.usermodel.ClientAnchor;
+import org.apache.poi.ss.usermodel.CreationHelper;
/**
*
@@ -39,7 +51,7 @@ public final class TestOLE2Embeding extends TestCase {
public void testEmbeddedObjects() throws Exception {
HSSFWorkbook workbook = HSSFTestDataSamples.openSampleWorkbook("ole2-embedding.xls");
- List objects = workbook.getAllEmbeddedObjects();
+ List<HSSFObjectData> objects = workbook.getAllEmbeddedObjects();
assertEquals("Wrong number of objects", 2, objects.size());
assertEquals("Wrong name for first object", "MBD06CAB431",
((HSSFObjectData)
@@ -48,5 +60,101 @@ public final class TestOLE2Embeding extends TestCase {
((HSSFObjectData)
objects.get(1)).getDirectory().getName());
}
-}
+
+ public void testReallyEmbedSomething() throws Exception {
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sheet = wb.createSheet();
+ HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
+ byte[] pictureData = HSSFTestDataSamples.getTestDataFileContent("logoKarmokar4.png");
+ byte[] picturePPT = POIDataSamples.getSlideShowInstance().readFile("clock.jpg");
+ int imgIdx = wb.addPicture(pictureData, HSSFWorkbook.PICTURE_TYPE_PNG);
+ POIFSFileSystem pptPoifs = getSamplePPT();
+ int pptIdx = wb.addOlePackage(pptPoifs, "Sample-PPT", "sample.ppt", "sample.ppt");
+ POIFSFileSystem xlsPoifs = getSampleXLS();
+ int imgPPT = wb.addPicture(picturePPT, HSSFWorkbook.PICTURE_TYPE_JPEG);
+ int xlsIdx = wb.addOlePackage(xlsPoifs, "Sample-XLS", "sample.xls", "sample.xls");
+ int txtIdx = wb.addOlePackage(getSampleTXT(), "Sample-TXT", "sample.txt", "sample.txt");
+
+ int rowoffset = 5;
+ int coloffset = 5;
+
+ CreationHelper ch = wb.getCreationHelper();
+ HSSFClientAnchor anchor = (HSSFClientAnchor)ch.createClientAnchor();
+ anchor.setAnchor((short)(2+coloffset), 1+rowoffset, 0, 0, (short)(3+coloffset), 5+rowoffset, 0, 0);
+ anchor.setAnchorType(ClientAnchor.DONT_MOVE_AND_RESIZE);
+
+ patriarch.createObjectData(anchor, pptIdx, imgPPT);
+
+ anchor = (HSSFClientAnchor)ch.createClientAnchor();
+ anchor.setAnchor((short)(5+coloffset), 1+rowoffset, 0, 0, (short)(6+coloffset), 5+rowoffset, 0, 0);
+ anchor.setAnchorType(ClientAnchor.DONT_MOVE_AND_RESIZE);
+
+ patriarch.createObjectData(anchor, xlsIdx, imgIdx);
+
+ anchor = (HSSFClientAnchor)ch.createClientAnchor();
+ anchor.setAnchor((short)(3+coloffset), 10+rowoffset, 0, 0, (short)(5+coloffset), 11+rowoffset, 0, 0);
+ anchor.setAnchorType(ClientAnchor.DONT_MOVE_AND_RESIZE);
+
+ patriarch.createObjectData(anchor, txtIdx, imgIdx);
+
+ anchor = (HSSFClientAnchor)ch.createClientAnchor();
+ anchor.setAnchor((short)(1+coloffset), -2+rowoffset, 0, 0, (short)(7+coloffset), 14+rowoffset, 0, 0);
+ anchor.setAnchorType(ClientAnchor.DONT_MOVE_AND_RESIZE);
+
+ HSSFSimpleShape circle = patriarch.createSimpleShape(anchor);
+ circle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);
+ circle.setNoFill(true);
+
+ if (false) {
+ FileOutputStream fos = new FileOutputStream("embed.xls");
+ wb.write(fos);
+ fos.close();
+ }
+
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ HSSFObjectData od = wb.getAllEmbeddedObjects().get(0);
+ Ole10Native ole10 = Ole10Native.createFromEmbeddedOleObject((DirectoryNode)od.getDirectory());
+ bos.reset();
+ pptPoifs.writeFilesystem(bos);
+ assertTrue(Arrays.equals(ole10.getDataBuffer(), bos.toByteArray()));
+
+ od = wb.getAllEmbeddedObjects().get(1);
+ ole10 = Ole10Native.createFromEmbeddedOleObject((DirectoryNode)od.getDirectory());
+ bos.reset();
+ xlsPoifs.writeFilesystem(bos);
+ assertTrue(Arrays.equals(ole10.getDataBuffer(), bos.toByteArray()));
+
+ od = wb.getAllEmbeddedObjects().get(2);
+ ole10 = Ole10Native.createFromEmbeddedOleObject((DirectoryNode)od.getDirectory());
+ assertTrue(Arrays.equals(ole10.getDataBuffer(), getSampleTXT()));
+
+ }
+
+ static POIFSFileSystem getSamplePPT() throws IOException {
+ // scratchpad classes are not available, so we use something pre-cooked
+ InputStream is = POIDataSamples.getSlideShowInstance().openResourceAsStream("with_textbox.ppt");
+ POIFSFileSystem poifs = new POIFSFileSystem(is);
+ is.close();
+
+ return poifs;
+ }
+
+ static POIFSFileSystem getSampleXLS() throws IOException {
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sheet = wb.createSheet();
+ sheet.createRow(5).createCell(2).setCellValue("yo dawg i herd you like embeddet objekts, so we put a ole in your ole so you can save a file while you save a file");
+
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ wb.write(bos);
+ POIFSFileSystem poifs = new POIFSFileSystem(new ByteArrayInputStream(bos.toByteArray()));
+
+ return poifs;
+ }
+
+ static byte[] getSampleTXT() {
+ return "All your base are belong to us".getBytes();
+ }
+} \ No newline at end of file