aboutsummaryrefslogtreecommitdiffstats
path: root/src/scratchpad/testcases/org/apache/poi/hwpf/TestHWPFPictures.java
blob: 1adb68b809b5eb52a69a1e2d68bc7fdef4d1d2a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* ====================================================================
   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.hwpf;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.util.List;

import org.apache.poi.hwpf.model.PicturesTable;
import org.apache.poi.hwpf.usermodel.Picture;

import junit.framework.TestCase;

/**
 * Test picture support in HWPF
 * @author nick
 */
public final class TestHWPFPictures extends TestCase {
	private String docAFile;
	private String docBFile;
	private String docCFile;
    private String docDFile;

	private String imgAFile;
	private String imgBFile;
	private String imgCFile;
    private String imgDFile;

	protected void setUp() throws Exception {
		String dirname = System.getProperty("HWPF.testdata.path");

		docAFile = dirname + "/testPictures.doc";
		docBFile = dirname + "/two_images.doc";
		docCFile = dirname + "/vector_image.doc";
        docDFile = dirname + "/GaiaTest.doc";

		imgAFile = dirname + "/simple_image.jpg";
		imgBFile = dirname + "/simple_image.png";
		imgCFile = dirname + "/vector_image.emf";
        imgDFile = dirname + "/GaiaTestImg.png";
	}

	/**
	 * Test just opening the files
	 */
	public void testOpen() throws Exception {
		HWPFDocument docA = new HWPFDocument(new FileInputStream(docAFile));
		HWPFDocument docB = new HWPFDocument(new FileInputStream(docBFile));
	}

	/**
	 * Test that we have the right numbers of images in each file
	 */
	public void testImageCount() throws Exception {
		HWPFDocument docA = new HWPFDocument(new FileInputStream(docAFile));
		HWPFDocument docB = new HWPFDocument(new FileInputStream(docBFile));

		assertNotNull(docA.getPicturesTable());
		assertNotNull(docB.getPicturesTable());

		PicturesTable picA = docA.getPicturesTable();
		PicturesTable picB = docB.getPicturesTable();

		List picturesA = picA.getAllPictures();
		List picturesB = picB.getAllPictures();

		assertEquals(7, picturesA.size());
		assertEquals(2, picturesB.size());
	}

	/**
	 * Test that we have the right images in at least one file
	 */
	public void testImageData() throws Exception {
		HWPFDocument docB = new HWPFDocument(new FileInputStream(docBFile));
		PicturesTable picB = docB.getPicturesTable();
		List picturesB = picB.getAllPictures();

		assertEquals(2, picturesB.size());

		Picture pic1 = (Picture)picturesB.get(0);
		Picture pic2 = (Picture)picturesB.get(1);

		assertNotNull(pic1);
		assertNotNull(pic2);

		// Check the same
		byte[] pic1B = readFile(imgAFile);
		byte[] pic2B = readFile(imgBFile);

		assertEquals(pic1B.length, pic1.getContent().length);
		assertEquals(pic2B.length, pic2.getContent().length);

		assertBytesSame(pic1B, pic1.getContent());
		assertBytesSame(pic2B, pic2.getContent());
	}

	/**
	 * Test that compressed image data is correctly returned.
	 */
	public void testCompressedImageData() throws Exception {
		HWPFDocument docC = new HWPFDocument(new FileInputStream(docCFile));
		PicturesTable picC = docC.getPicturesTable();
		List picturesC = picC.getAllPictures();

		assertEquals(1, picturesC.size());

		Picture pic = (Picture)picturesC.get(0);
		assertNotNull(pic);

		// Check the same
		byte[] picBytes = readFile(imgCFile);

		assertEquals(picBytes.length, pic.getContent().length);
		assertBytesSame(picBytes, pic.getContent());
	}

	/**
	 * Pending the missing files being uploaded to
	 *  bug #44937
	 */
    public void BROKENtestEscherDrawing() throws Exception
    {
        HWPFDocument docD = new HWPFDocument(new FileInputStream(docDFile));
        List allPictures = docD.getPicturesTable().getAllPictures();

        assertEquals(1, allPictures.size());

        Picture pic = (Picture) allPictures.get(0);
        assertNotNull(pic);
        byte[] picD = readFile(imgDFile);

        assertEquals(picD.length, pic.getContent().length);

        assertBytesSame(picD, pic.getContent());
    }

	private void assertBytesSame(byte[] a, byte[] b) {
		assertEquals(a.length, b.length);
		for(int i=0; i<a.length; i++) {
			assertEquals(a[i],b[i]);
		}
	}

	private byte[] readFile(String file) throws Exception {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		FileInputStream fis = new FileInputStream(file);
		byte[] buffer = new byte[1024];

		int read = 0;
		while(read > -1) {
			read = fis.read(buffer);
			if(read > 0) {
				baos.write(buffer,0,read);
			}
		}

		return baos.toByteArray();
	}
}