1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.tika.parser.image;
18  
19  import java.io.InputStream;
20  
21  import org.apache.tika.metadata.Metadata;
22  import org.apache.tika.parser.Parser;
23  import org.xml.sax.helpers.DefaultHandler;
24  
25  import junit.framework.TestCase;
26  
27  public class ImageParserTest extends TestCase {
28  
29      private final Parser parser = new ImageParser();
30  
31      public void testBMP() throws Exception {
32          Metadata metadata = new Metadata();
33          metadata.set(Metadata.CONTENT_TYPE, "image/bmp");
34          InputStream stream =
35              getClass().getResourceAsStream("/test-documents/testBMP.bmp");
36          parser.parse(stream, new DefaultHandler(), metadata);
37  
38          assertEquals("75", metadata.get("height"));
39          assertEquals("100", metadata.get("width"));
40      }
41  
42      public void testGIF() throws Exception {
43          Metadata metadata = new Metadata();
44          metadata.set(Metadata.CONTENT_TYPE, "image/gif");
45          InputStream stream =
46              getClass().getResourceAsStream("/test-documents/testGIF.gif");
47          parser.parse(stream, new DefaultHandler(), metadata);
48  
49          assertEquals("75", metadata.get("height"));
50          assertEquals("100", metadata.get("width"));
51      }
52  
53      public void testJPEG() throws Exception {
54          Metadata metadata = new Metadata();
55          metadata.set(Metadata.CONTENT_TYPE, "image/jpeg");
56          InputStream stream =
57              getClass().getResourceAsStream("/test-documents/testJPEG.jpg");
58          parser.parse(stream, new DefaultHandler(), metadata);
59  
60          assertEquals("75", metadata.get("height"));
61          assertEquals("100", metadata.get("width"));
62      }
63  
64      public void testPNG() throws Exception {
65          Metadata metadata = new Metadata();
66          metadata.set(Metadata.CONTENT_TYPE, "image/png");
67          InputStream stream =
68              getClass().getResourceAsStream("/test-documents/testPNG.png");
69          parser.parse(stream, new DefaultHandler(), metadata);
70  
71          assertEquals("75", metadata.get("height"));
72          assertEquals("100", metadata.get("width"));
73      }
74  
75  // TODO: Add TIFF support
76  //    public void testTIFF() throws Exception {
77  //        Metadata metadata = new Metadata();
78  //        metadata.set(Metadata.CONTENT_TYPE, "image/tiff");
79  //        InputStream stream =
80  //            getClass().getResourceAsStream("/test-documents/testTIFF.tif");
81  //        parser.parse(stream, new DefaultHandler(), metadata);
82  //
83  //        assertEquals("75", metadata.get("height"));
84  //        assertEquals("100", metadata.get("width"));
85  //    }
86  
87  }