aboutsummaryrefslogtreecommitdiffstats
path: root/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPictureData.java
blob: 1a330f0b38d56e536b8b9cd90e4b0019c8d3d2c8 (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
/* ====================================================================
   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.hssf.usermodel;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.imageio.IIOException;
import javax.imageio.ImageIO;

import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.junit.BeforeClass;

/**
 * Test <code>HSSFPictureData</code>.
 * The code to retrieve images from a workbook provided by Trejkaz (trejkaz at trypticon dot org) in Bug 41223.
 *
 * @author Yegor Kozlov (yegor at apache dot org)
 * @author Trejkaz (trejkaz at trypticon dot org)
 */
public final class TestHSSFPictureData extends TestCase{
    @BeforeClass
    public static void setUpClass() {
        final String tmpDirProperty = System.getProperty("java.io.tmpdir");
        if(tmpDirProperty == null || "".equals(tmpDirProperty)) {
            return;
        }
        // ensure that temp-dir exists because ImageIO requires it
        final File tmpDir = new File(tmpDirProperty);
        if(!tmpDir.exists() && !tmpDir.mkdirs()) {
            throw new IllegalStateException("Could not create temporary directory " + tmpDirProperty + ", full path " + tmpDir.getAbsolutePath());
        }
        ImageIO.setCacheDirectory(tmpDir);
    }

	public void testPictures() throws IOException {
        HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("SimpleWithImages.xls");

        // TODO - add getFormat() to interface PictureData and genericise wb.getAllPictures()
        List<HSSFPictureData> lst = wb.getAllPictures();
        //assertEquals(2, lst.size());

        try {
            for (final HSSFPictureData pict : lst) {
                String ext = pict.suggestFileExtension();
                byte[] data = pict.getData();
                if (ext.equals("jpeg")) {
                    //try to read image data using javax.imageio.* (JDK 1.4+)
                    BufferedImage jpg = ImageIO.read(new ByteArrayInputStream(data));
                    assertNotNull(jpg);
                    assertEquals(192, jpg.getWidth());
                    assertEquals(176, jpg.getHeight());
                    assertEquals(HSSFWorkbook.PICTURE_TYPE_JPEG, pict.getFormat());
                    assertEquals("image/jpeg", pict.getMimeType());
                } else if (ext.equals("png")) {
                    //try to read image data using javax.imageio.* (JDK 1.4+)
                    BufferedImage png = ImageIO.read(new ByteArrayInputStream(data));
                    assertNotNull(png);
                    assertEquals(300, png.getWidth());
                    assertEquals(300, png.getHeight());
                    assertEquals(HSSFWorkbook.PICTURE_TYPE_PNG, pict.getFormat());
                    assertEquals("image/png", pict.getMimeType());
            /*} else {
                //TODO: test code for PICT, WMF and EMF*/
                }
            }
        } catch (IIOException e) {
            assertFalse(e.getMessage(), e.getMessage().contains("Can't create cache file"));
        }
    }

	public void testMacPicture() throws IOException {
        HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("53446.xls");

        try{
            List<HSSFPictureData> lst = wb.getAllPictures();
            assertEquals(1, lst.size());

            HSSFPictureData pict = lst.get(0);
            String ext = pict.suggestFileExtension();
            assertEquals("Expected a PNG.", "png", ext);

            //try to read image data using javax.imageio.* (JDK 1.4+)
            byte[] data = pict.getData();
            BufferedImage png = ImageIO.read(new ByteArrayInputStream(data));
            assertNotNull(png);
            assertEquals(78, png.getWidth());
            assertEquals(76, png.getHeight());
            assertEquals(HSSFWorkbook.PICTURE_TYPE_PNG, pict.getFormat());
            assertEquals("image/png", pict.getMimeType());
        } catch (IIOException e) {
            assertFalse(e.getMessage(), e.getMessage().contains("Can't create cache file"));
        }
    }

    public void testNotNullPictures() {

        HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("SheetWithDrawing.xls");

        // TODO - add getFormat() to interface PictureData and genericise wb.getAllPictures()
        List<HSSFPictureData> lst = wb.getAllPictures();
        for(HSSFPictureData pict : lst){
            assertNotNull(pict);
        }
    }
}