Inspired by bug #44958 - Record level support for Data Tables. (No formula parser...
[poi.git] / src / scratchpad / testcases / org / apache / poi / hwpf / usermodel / TestPictures.java
blob3656b2ff5f6d4b6d49c71020840a1bdedea402a0
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 package org.apache.poi.hwpf.usermodel;
19 import java.io.ByteArrayOutputStream;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.util.List;
24 import junit.framework.TestCase;
26 import org.apache.poi.hwpf.HWPFDocument;
27 import org.apache.poi.util.LittleEndian;
29 /**
30 * Test the picture handling
32 * @author Nick Burch (nick at torchbox dot com)
34 public class TestPictures extends TestCase {
35 private String dirname = System.getProperty("HWPF.testdata.path");
37 protected void setUp() throws Exception {
40 /**
41 * two jpegs
43 public void testTwoImages() throws Exception {
44 HWPFDocument doc = new HWPFDocument(new FileInputStream(dirname + "/two_images.doc"));
45 List pics = doc.getPicturesTable().getAllPictures();
47 assertNotNull(pics);
48 assertEquals(pics.size(), 2);
49 for(int i=0; i<pics.size(); i++) {
50 Object p = pics.get(i);
51 assertTrue(p instanceof Picture);
53 Picture pic = (Picture)p;
54 assertNotNull(pic.suggestFileExtension());
55 assertNotNull(pic.suggestFullFileName());
58 Picture picA = (Picture)pics.get(0);
59 Picture picB = (Picture)pics.get(1);
60 assertEquals("jpg", picA.suggestFileExtension());
61 assertEquals("jpg", picA.suggestFileExtension());
64 /**
65 * pngs and jpegs
67 public void testDifferentImages() throws Exception {
68 HWPFDocument doc = new HWPFDocument(new FileInputStream(dirname + "/testPictures.doc"));
69 List pics = doc.getPicturesTable().getAllPictures();
71 assertNotNull(pics);
72 assertEquals(7, pics.size());
73 for(int i=0; i<pics.size(); i++) {
74 Object p = pics.get(i);
75 assertTrue(p instanceof Picture);
77 Picture pic = (Picture)p;
78 assertNotNull(pic.suggestFileExtension());
79 assertNotNull(pic.suggestFullFileName());
82 assertEquals("jpg", ((Picture)pics.get(0)).suggestFileExtension());
83 assertEquals("jpg", ((Picture)pics.get(1)).suggestFileExtension());
84 assertEquals("png", ((Picture)pics.get(3)).suggestFileExtension());
85 assertEquals("png", ((Picture)pics.get(4)).suggestFileExtension());
86 assertEquals("wmf", ((Picture)pics.get(5)).suggestFileExtension());
87 assertEquals("jpg", ((Picture)pics.get(6)).suggestFileExtension());
90 /**
91 * emf image, nice and simple
93 public void testEmfImage() throws Exception {
94 HWPFDocument doc = new HWPFDocument(new FileInputStream(dirname + "/vector_image.doc"));
95 List pics = doc.getPicturesTable().getAllPictures();
97 assertNotNull(pics);
98 assertEquals(1, pics.size());
100 Picture pic = (Picture)pics.get(0);
101 assertNotNull(pic.suggestFileExtension());
102 assertNotNull(pic.suggestFullFileName());
103 assertTrue(pic.getSize() > 128);
105 // Check right contents
106 byte[] emf = loadImage("vector_image.emf");
107 byte[] pemf = pic.getContent();
108 assertEquals(emf.length, pemf.length);
109 for(int i=0; i<emf.length; i++) {
110 assertEquals(emf[i], pemf[i]);
115 * emf image, with a crazy offset
117 public void testEmfComplexImage() throws Exception {
120 Commenting out this test case temporarily. The file emf_2003_image does not contain any
121 pictures. Instead it has an office drawing object. Need to rewrite this test after
122 revisiting the implementation of office drawing objects.
124 HWPFDocument doc = new HWPFDocument(new FileInputStream(dirname + "/emf_2003_image.doc"));
125 List pics = doc.getPicturesTable().getAllPictures();
127 assertNotNull(pics);
128 assertEquals(1, pics.size());
130 Picture pic = (Picture)pics.get(0);
131 assertNotNull(pic.suggestFileExtension());
132 assertNotNull(pic.suggestFullFileName());
134 // This one's tricky
135 // TODO: Fix once we've sorted bug #41898
136 assertNotNull(pic.getContent());
137 assertNotNull(pic.getRawContent());
139 // These are probably some sort of offset, need to figure them out
140 assertEquals(4, pic.getSize());
141 assertEquals(0x80000000l, LittleEndian.getUInt(pic.getContent()));
142 assertEquals(0x80000000l, LittleEndian.getUInt(pic.getRawContent()));
146 public void testPicturesWithTable() throws Exception {
147 HWPFDocument doc = new HWPFDocument(new FileInputStream(
148 new File(dirname, "Bug44603.doc")));
150 List pics = doc.getPicturesTable().getAllPictures();
151 assertEquals(pics.size(), 2);
154 private byte[] loadImage(String filename) throws Exception {
155 ByteArrayOutputStream b = new ByteArrayOutputStream();
156 FileInputStream fis = new FileInputStream(dirname + "/" + filename);
158 byte[] buf = new byte[4096];
159 int read = 0;
160 while( (read = fis.read(buf)) > -1 ) {
161 b.write(buf, 0, read);
163 return b.toByteArray();