View Javadoc
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.commons.imaging.formats.webp;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.io.File;
24  
25  import org.apache.commons.imaging.ImageInfo;
26  import org.apache.commons.imaging.bytesource.ByteSource;
27  import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;
28  import org.apache.commons.imaging.formats.tiff.taginfos.TagInfo;
29  import org.apache.commons.imaging.formats.tiff.taginfos.TagInfoAscii;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Tests for the WebP metadata.
34   */
35  public class WebPMetadataTest extends WebPBaseTest {
36  
37      /**
38       * @throws Exception if it cannot open the images.
39       */
40      @Test
41      public void testReadAlpha() throws Exception {
42          final File imageFile = new File(WebPMetadataTest.class.getResource("/images/webp/alpha/alpha.webp").getFile());
43          final WebPImageParser parser = new WebPImageParser();
44          final ImageInfo imageInfo = parser.getImageInfo(ByteSource.file(imageFile), parser.getDefaultParameters());
45          assertNotNull(imageInfo);
46      }
47  
48      /**
49       * @throws Exception if it cannot open the images.
50       */
51      @Test
52      public void testReadMetadata() throws Exception {
53          final File imageFile = new File(WebPMetadataTest.class.getResource("/images/webp/exif/_DSC6099.webp").getFile());
54          final WebPImageParser parser = new WebPImageParser();
55          final WebPImageMetadata metadata = parser.getMetadata(ByteSource.file(imageFile), parser.getDefaultParameters());
56          assertEquals(56, metadata.getItems().size());
57  
58          final TiffImageMetadata tiffImageMetadata = metadata.getExif();
59          assertNotNull(tiffImageMetadata);
60  
61          // Finds by the tag number.
62          final TagInfo make = new TagInfoAscii("", 271, 0, null);
63          final Object field = tiffImageMetadata.getFieldValue(make);
64          assertEquals("sony", field.toString().trim().toLowerCase());
65      }
66  
67      /**
68       * @throws Exception if it cannot open the images.
69       */
70      @Test
71      public void testReadXmp() throws Exception {
72          final File imageFile = new File(WebPMetadataTest.class.getResource("/images/webp/xmp/test.webp").getFile());
73          final WebPImageParser parser = new WebPImageParser();
74  
75          final String xml = parser.getXmpXml(ByteSource.file(imageFile), parser.getDefaultParameters());
76          assertTrue(xml.contains("Apache License"));
77      }
78  }