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