2 /* ====================================================================
3 Licensed to the Apache Software Foundation (ASF) under one or more
4 contributor license agreements. See the NOTICE file distributed with
5 this work for additional information regarding copyright ownership.
6 The ASF licenses this file to You under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with
8 the License. You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================== */
19 package org
.apache
.poi
.hwpf
;
21 import java
.io
.ByteArrayOutputStream
;
22 import java
.io
.FileInputStream
;
23 import java
.util
.List
;
25 import org
.apache
.poi
.hwpf
.model
.PicturesTable
;
26 import org
.apache
.poi
.hwpf
.usermodel
.Picture
;
28 import junit
.framework
.TestCase
;
31 * Test picture support in HWPF
34 public class TestHWPFPictures
extends TestCase
{
35 private String docAFile
;
36 private String docBFile
;
37 private String docCFile
;
38 private String docDFile
;
40 private String imgAFile
;
41 private String imgBFile
;
42 private String imgCFile
;
43 private String imgDFile
;
45 protected void setUp() throws Exception
{
46 String dirname
= System
.getProperty("HWPF.testdata.path");
48 docAFile
= dirname
+ "/testPictures.doc";
49 docBFile
= dirname
+ "/two_images.doc";
50 docCFile
= dirname
+ "/vector_image.doc";
51 docDFile
= dirname
+ "/GaiaTest.doc";
53 imgAFile
= dirname
+ "/simple_image.jpg";
54 imgBFile
= dirname
+ "/simple_image.png";
55 imgCFile
= dirname
+ "/vector_image.emf";
56 imgDFile
= dirname
+ "/GaiaTestImg.png";
60 * Test just opening the files
62 public void testOpen() throws Exception
{
63 HWPFDocument docA
= new HWPFDocument(new FileInputStream(docAFile
));
64 HWPFDocument docB
= new HWPFDocument(new FileInputStream(docBFile
));
68 * Test that we have the right numbers of images in each file
70 public void testImageCount() throws Exception
{
71 HWPFDocument docA
= new HWPFDocument(new FileInputStream(docAFile
));
72 HWPFDocument docB
= new HWPFDocument(new FileInputStream(docBFile
));
74 assertNotNull(docA
.getPicturesTable());
75 assertNotNull(docB
.getPicturesTable());
77 PicturesTable picA
= docA
.getPicturesTable();
78 PicturesTable picB
= docB
.getPicturesTable();
80 List picturesA
= picA
.getAllPictures();
81 List picturesB
= picB
.getAllPictures();
83 assertEquals(7, picturesA
.size());
84 assertEquals(2, picturesB
.size());
88 * Test that we have the right images in at least one file
90 public void testImageData() throws Exception
{
91 HWPFDocument docB
= new HWPFDocument(new FileInputStream(docBFile
));
92 PicturesTable picB
= docB
.getPicturesTable();
93 List picturesB
= picB
.getAllPictures();
95 assertEquals(2, picturesB
.size());
97 Picture pic1
= (Picture
)picturesB
.get(0);
98 Picture pic2
= (Picture
)picturesB
.get(1);
104 byte[] pic1B
= readFile(imgAFile
);
105 byte[] pic2B
= readFile(imgBFile
);
107 assertEquals(pic1B
.length
, pic1
.getContent().length
);
108 assertEquals(pic2B
.length
, pic2
.getContent().length
);
110 assertBytesSame(pic1B
, pic1
.getContent());
111 assertBytesSame(pic2B
, pic2
.getContent());
115 * Test that compressed image data is correctly returned.
117 public void testCompressedImageData() throws Exception
{
118 HWPFDocument docC
= new HWPFDocument(new FileInputStream(docCFile
));
119 PicturesTable picC
= docC
.getPicturesTable();
120 List picturesC
= picC
.getAllPictures();
122 assertEquals(1, picturesC
.size());
124 Picture pic
= (Picture
)picturesC
.get(0);
128 byte[] picBytes
= readFile(imgCFile
);
130 assertEquals(picBytes
.length
, pic
.getContent().length
);
131 assertBytesSame(picBytes
, pic
.getContent());
135 * Pending the missing files being uploaded to
138 public void BROKENtestEscherDrawing() throws Exception
140 HWPFDocument docD
= new HWPFDocument(new FileInputStream(docDFile
));
141 List allPictures
= docD
.getPicturesTable().getAllPictures();
143 assertEquals(1, allPictures
.size());
145 Picture pic
= (Picture
) allPictures
.get(0);
147 byte[] picD
= readFile(imgDFile
);
149 assertEquals(picD
.length
, pic
.getContent().length
);
151 assertBytesSame(picD
, pic
.getContent());
154 private void assertBytesSame(byte[] a
, byte[] b
) {
155 assertEquals(a
.length
, b
.length
);
156 for(int i
=0; i
<a
.length
; i
++) {
157 assertEquals(a
[i
],b
[i
]);
161 private byte[] readFile(String file
) throws Exception
{
162 ByteArrayOutputStream baos
= new ByteArrayOutputStream();
163 FileInputStream fis
= new FileInputStream(file
);
164 byte[] buffer
= new byte[1024];
168 read
= fis
.read(buffer
);
170 baos
.write(buffer
,0,read
);
174 return baos
.toByteArray();