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  
18  package org.apache.commons.imaging.formats.icns;
19  
20  import org.apache.commons.imaging.ImageInfo;
21  import org.apache.commons.imaging.ImageReadException;
22  import org.apache.commons.imaging.Imaging;
23  import org.junit.jupiter.api.Assertions;
24  import org.junit.jupiter.api.Disabled;
25  import org.junit.jupiter.params.ParameterizedTest;
26  import org.junit.jupiter.params.provider.Arguments;
27  import org.junit.jupiter.params.provider.MethodSource;
28  
29  import java.awt.image.BufferedImage;
30  import java.io.File;
31  import java.io.IOException;
32  import java.util.Arrays;
33  import java.util.List;
34  import java.util.stream.Stream;
35  
36  import static org.junit.jupiter.api.Assertions.assertEquals;
37  import static org.junit.jupiter.api.Assertions.assertNotNull;
38  
39  public class IcnsReadTest extends IcnsBaseTest {
40  
41      public static Stream<File> data() throws Exception {
42          return getIcnsImages().stream();
43      }
44  
45      /**
46       * Provide data for tests of ICNS images with mono and/or JPEG png data. The
47       * logo in the issue IMAGING-248 was the groovy.icns. But the Python project
48       * also provides an image that contains a ic09 image.
49       *
50       * <p>icns2png was used to retrieve the number of images (i.e. icns2png -l
51       * image_file.icns, then counted the number of ico entries, ignoring the masks).</p>
52       * @return stream of test arguments
53       */
54      public static Stream<Arguments> provideIcnsImagesWithMonoAndJpegPngData() {
55          return Arrays
56                  .asList(
57                          Arguments.of("/images/icns/IMAGING-248/python.icns", 7),
58                          Arguments.of("/images/icns/IMAGING-248/groovy.icns", 3))
59                  .stream();
60      }
61  
62      @Disabled(value = "RoundtripTest has to be fixed befor implementation can throw UnsupportedOperationException")
63      @ParameterizedTest
64      @MethodSource("data")
65      public void testImageMetadata(final File imageFile) {
66          Assertions.assertThrows(UnsupportedOperationException.class, () -> Imaging.getMetadata(imageFile));
67      }
68  
69      @ParameterizedTest
70      @MethodSource("data")
71      public void testImageInfo(final File imageFile) throws Exception {
72          final ImageInfo imageInfo = Imaging.getImageInfo(imageFile);
73          assertNotNull(imageInfo);
74      }
75  
76      @ParameterizedTest
77      @MethodSource("data")
78      public void testBufferedImage(final File imageFile) throws Exception {
79          final BufferedImage image = Imaging.getBufferedImage(imageFile);
80          assertNotNull(image);
81          // TODO assert more
82      }
83  
84      /**
85       * Test ICNS types such as mono (ICON) and some types for either JPEG2000 or PNG
86       * (icp4, icp5, ic11, etc). For IMAGING-248.
87       * @throws IOException if it fails to read the input stream
88       * @throws ImageReadException if the image is corrupted or invalid
89       */
90      @ParameterizedTest()
91      @MethodSource("provideIcnsImagesWithMonoAndJpegPngData")
92      public void testIcnsElementMonoPngJpeg(final String file, final int numberOfImages) throws ImageReadException, IOException {
93          final File testFile = new File(IcnsReadTest.class.getResource(file).getFile());
94          final List<BufferedImage> images = new IcnsImageParser().getAllBufferedImages(testFile);
95          assertEquals(numberOfImages, images.size());
96      }
97  }