001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hwpf;
019:
020: import java.io.ByteArrayOutputStream;
021: import java.io.FileInputStream;
022: import java.util.List;
023:
024: import org.apache.poi.hwpf.model.PicturesTable;
025: import org.apache.poi.hwpf.usermodel.Picture;
026:
027: import junit.framework.TestCase;
028:
029: /**
030: * Test picture support in HWPF
031: * @author nick
032: */
033: public class TestHWPFPictures extends TestCase {
034: private String docAFile;
035: private String docBFile;
036: private String docCFile;
037:
038: private String imgAFile;
039: private String imgBFile;
040: private String imgCFile;
041:
042: protected void setUp() throws Exception {
043: String dirname = System.getProperty("HWPF.testdata.path");
044:
045: docAFile = dirname + "/testPictures.doc";
046: docBFile = dirname + "/two_images.doc";
047: docCFile = dirname + "/vector_image.doc";
048:
049: imgAFile = dirname + "/simple_image.jpg";
050: imgBFile = dirname + "/simple_image.png";
051: imgCFile = dirname + "/vector_image.emf";
052: }
053:
054: /**
055: * Test just opening the files
056: */
057: public void testOpen() throws Exception {
058: HWPFDocument docA = new HWPFDocument(new FileInputStream(
059: docAFile));
060: HWPFDocument docB = new HWPFDocument(new FileInputStream(
061: docBFile));
062: }
063:
064: /**
065: * Test that we have the right numbers of images in each file
066: */
067: public void testImageCount() throws Exception {
068: HWPFDocument docA = new HWPFDocument(new FileInputStream(
069: docAFile));
070: HWPFDocument docB = new HWPFDocument(new FileInputStream(
071: docBFile));
072:
073: assertNotNull(docA.getPicturesTable());
074: assertNotNull(docB.getPicturesTable());
075:
076: PicturesTable picA = docA.getPicturesTable();
077: PicturesTable picB = docB.getPicturesTable();
078:
079: List picturesA = picA.getAllPictures();
080: List picturesB = picB.getAllPictures();
081:
082: assertEquals(7, picturesA.size());
083: assertEquals(2, picturesB.size());
084: }
085:
086: /**
087: * Test that we have the right images in at least one file
088: */
089: public void testImageData() throws Exception {
090: HWPFDocument docB = new HWPFDocument(new FileInputStream(
091: docBFile));
092: PicturesTable picB = docB.getPicturesTable();
093: List picturesB = picB.getAllPictures();
094:
095: assertEquals(2, picturesB.size());
096:
097: Picture pic1 = (Picture) picturesB.get(0);
098: Picture pic2 = (Picture) picturesB.get(1);
099:
100: assertNotNull(pic1);
101: assertNotNull(pic2);
102:
103: // Check the same
104: byte[] pic1B = readFile(imgAFile);
105: byte[] pic2B = readFile(imgBFile);
106:
107: assertEquals(pic1B.length, pic1.getContent().length);
108: assertEquals(pic2B.length, pic2.getContent().length);
109:
110: assertBytesSame(pic1B, pic1.getContent());
111: assertBytesSame(pic2B, pic2.getContent());
112: }
113:
114: /**
115: * Test that compressed image data is correctly returned.
116: */
117: public void testCompressedImageData() throws Exception {
118: HWPFDocument docC = new HWPFDocument(new FileInputStream(
119: docCFile));
120: PicturesTable picC = docC.getPicturesTable();
121: List picturesC = picC.getAllPictures();
122:
123: assertEquals(1, picturesC.size());
124:
125: Picture pic = (Picture) picturesC.get(0);
126: assertNotNull(pic);
127:
128: // Check the same
129: byte[] picBytes = readFile(imgCFile);
130:
131: assertEquals(picBytes.length, pic.getContent().length);
132: assertBytesSame(picBytes, pic.getContent());
133: }
134:
135: private void assertBytesSame(byte[] a, byte[] b) {
136: assertEquals(a.length, b.length);
137: for (int i = 0; i < a.length; i++) {
138: assertEquals(a[i], b[i]);
139: }
140: }
141:
142: private byte[] readFile(String file) throws Exception {
143: ByteArrayOutputStream baos = new ByteArrayOutputStream();
144: FileInputStream fis = new FileInputStream(file);
145: byte[] buffer = new byte[1024];
146:
147: int read = 0;
148: while (read > -1) {
149: read = fis.read(buffer);
150: if (read > 0) {
151: baos.write(buffer, 0, read);
152: }
153: }
154:
155: return baos.toByteArray();
156: }
157: }
|