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: package org.apache.poi.hwpf.usermodel;
018:
019: import java.io.ByteArrayOutputStream;
020: import java.io.FileInputStream;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import org.apache.poi.hwpf.HWPFDocument;
025: import org.apache.poi.hwpf.model.TextPiece;
026: import org.apache.poi.hwpf.usermodel.Paragraph;
027: import org.apache.poi.hwpf.usermodel.Range;
028: import org.apache.poi.util.LittleEndian;
029:
030: import junit.framework.TestCase;
031:
032: /**
033: * Test the picture handling
034: *
035: * @author Nick Burch (nick at torchbox dot com)
036: */
037: public class TestPictures extends TestCase {
038: private String dirname = System.getProperty("HWPF.testdata.path");
039:
040: protected void setUp() throws Exception {
041: }
042:
043: /**
044: * two jpegs
045: */
046: public void testTwoImages() throws Exception {
047: HWPFDocument doc = new HWPFDocument(new FileInputStream(dirname
048: + "/two_images.doc"));
049: List pics = doc.getPicturesTable().getAllPictures();
050:
051: assertNotNull(pics);
052: assertEquals(pics.size(), 2);
053: for (int i = 0; i < pics.size(); i++) {
054: Object p = pics.get(i);
055: assertTrue(p instanceof Picture);
056:
057: Picture pic = (Picture) p;
058: assertNotNull(pic.suggestFileExtension());
059: assertNotNull(pic.suggestFullFileName());
060: }
061:
062: Picture picA = (Picture) pics.get(0);
063: Picture picB = (Picture) pics.get(1);
064: assertEquals("jpg", picA.suggestFileExtension());
065: assertEquals("jpg", picA.suggestFileExtension());
066: }
067:
068: /**
069: * pngs and jpegs
070: */
071: public void testDifferentImages() throws Exception {
072: HWPFDocument doc = new HWPFDocument(new FileInputStream(dirname
073: + "/testPictures.doc"));
074: List pics = doc.getPicturesTable().getAllPictures();
075:
076: assertNotNull(pics);
077: assertEquals(7, pics.size());
078: for (int i = 0; i < pics.size(); i++) {
079: Object p = pics.get(i);
080: assertTrue(p instanceof Picture);
081:
082: Picture pic = (Picture) p;
083: assertNotNull(pic.suggestFileExtension());
084: assertNotNull(pic.suggestFullFileName());
085: }
086:
087: assertEquals("jpg", ((Picture) pics.get(0))
088: .suggestFileExtension());
089: assertEquals("jpg", ((Picture) pics.get(1))
090: .suggestFileExtension());
091: assertEquals("png", ((Picture) pics.get(3))
092: .suggestFileExtension());
093: assertEquals("png", ((Picture) pics.get(4))
094: .suggestFileExtension());
095: assertEquals("wmf", ((Picture) pics.get(5))
096: .suggestFileExtension());
097: assertEquals("jpg", ((Picture) pics.get(6))
098: .suggestFileExtension());
099: }
100:
101: /**
102: * emf image, nice and simple
103: */
104: public void testEmfImage() throws Exception {
105: HWPFDocument doc = new HWPFDocument(new FileInputStream(dirname
106: + "/vector_image.doc"));
107: List pics = doc.getPicturesTable().getAllPictures();
108:
109: assertNotNull(pics);
110: assertEquals(1, pics.size());
111:
112: Picture pic = (Picture) pics.get(0);
113: assertNotNull(pic.suggestFileExtension());
114: assertNotNull(pic.suggestFullFileName());
115: assertTrue(pic.getSize() > 128);
116:
117: // Check right contents
118: byte[] emf = loadImage("vector_image.emf");
119: byte[] pemf = pic.getContent();
120: assertEquals(emf.length, pemf.length);
121: for (int i = 0; i < emf.length; i++) {
122: assertEquals(emf[i], pemf[i]);
123: }
124: }
125:
126: /**
127: * emf image, with a crazy offset
128: */
129: public void testEmfComplexImage() throws Exception {
130: HWPFDocument doc = new HWPFDocument(new FileInputStream(dirname
131: + "/emf_2003_image.doc"));
132: List pics = doc.getPicturesTable().getAllPictures();
133:
134: assertNotNull(pics);
135: assertEquals(1, pics.size());
136:
137: Picture pic = (Picture) pics.get(0);
138: assertNotNull(pic.suggestFileExtension());
139: assertNotNull(pic.suggestFullFileName());
140:
141: // This one's tricky
142: // TODO: Fix once we've sorted bug #41898
143: assertNotNull(pic.getContent());
144: assertNotNull(pic.getRawContent());
145:
146: // These are probably some sort of offset, need to figure them out
147: assertEquals(4, pic.getSize());
148: assertEquals(0x80000000l, LittleEndian
149: .getUInt(pic.getContent()));
150: assertEquals(0x80000000l, LittleEndian.getUInt(pic
151: .getRawContent()));
152: }
153:
154: private byte[] loadImage(String filename) throws Exception {
155: ByteArrayOutputStream b = new ByteArrayOutputStream();
156: FileInputStream fis = new FileInputStream(dirname + "/"
157: + filename);
158:
159: byte[] buf = new byte[4096];
160: int read = 0;
161: while ((read = fis.read(buf)) > -1) {
162: b.write(buf, 0, read);
163: }
164: return b.toByteArray();
165: }
166: }
|