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;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.List;
24  
25  import org.apache.commons.imaging.ImageInfo.ColorType;
26  import org.apache.commons.imaging.ImageInfo.CompressionAlgorithm;
27  import org.junit.jupiter.api.Test;
28  
29  public class TestImageInfo {
30  
31      @Test
32      public void testColorType() {
33          // ColorType is an enum within ImageInfo. We don't want to test every value
34          // of the enum. Just that one has the right values should be enough.
35          assertEquals("RGB", ColorType.RGB.toString());
36          assertEquals(ColorType.RGB, ColorType.valueOf("RGB"));
37      }
38  
39      @Test
40      public void testCompressionAlgorithm() {
41          assertEquals("LZW", CompressionAlgorithm.LZW.toString());
42          assertEquals(CompressionAlgorithm.LZW, CompressionAlgorithm.valueOf("LZW"));
43      }
44  
45      @Test
46      public void testImageInfo() {
47          final String formatDetails = "image-info-format-details";
48          final int bitsPerPixel = 2;
49          final List<String> comments = Arrays.asList("a", "b", "c");
50          final ImageFormat format = ImageFormats.BMP;
51          final String formatName = format.getName();
52          final int height = 2;
53          final String mimeType = "image-info-mimetype";
54          final int numberOfImages = 2;
55          final int physicalHeightDpi = 2;
56          final float physicalHeightInch = 2.0f;
57          final int physicalWidthDpi = 2;
58          final float physicalWidthInch = 2.0f;
59          final int width = 2;
60          final boolean progressive = true;
61          final boolean transparent = true;
62          final boolean usesPalette = true;
63          final ColorType colorType = ColorType.GRAYSCALE;
64          final CompressionAlgorithm compressionAlgorithm = CompressionAlgorithm.JPEG;
65  
66          final ImageInfo imageInfo = new ImageInfo(formatDetails, bitsPerPixel, comments, format, formatName, height, mimeType, numberOfImages,
67                  physicalHeightDpi, physicalHeightInch, physicalWidthDpi, physicalWidthInch, width, progressive, transparent, usesPalette, colorType,
68                  compressionAlgorithm);
69          assertEquals(formatDetails, imageInfo.getFormatDetails());
70          assertEquals(bitsPerPixel, imageInfo.getBitsPerPixel());
71          assertEquals(comments.toString(), imageInfo.getComments().toString());
72          assertEquals(format, imageInfo.getFormat());
73          assertEquals(formatName, imageInfo.getFormatName());
74          assertEquals(height, imageInfo.getHeight());
75          assertEquals(mimeType, imageInfo.getMimeType());
76          assertEquals(numberOfImages, imageInfo.getNumberOfImages());
77          assertEquals(physicalHeightDpi, imageInfo.getPhysicalHeightDpi());
78          assertEquals(physicalHeightInch, imageInfo.getPhysicalHeightInch());
79          assertEquals(physicalWidthDpi, imageInfo.getPhysicalWidthDpi());
80          assertEquals(physicalWidthInch, imageInfo.getPhysicalWidthInch());
81          assertEquals(width, imageInfo.getWidth());
82          assertEquals(progressive, imageInfo.isProgressive());
83          assertEquals(transparent, imageInfo.isTransparent());
84          assertEquals(usesPalette, imageInfo.usesPalette());
85          assertEquals(colorType, imageInfo.getColorType());
86          assertEquals(compressionAlgorithm, imageInfo.getCompressionAlgorithm());
87      }
88  
89      @Test
90      public void testToStringEmptyComments() {
91          final ImageInfo imageInfo = new ImageInfo(null, 0, new ArrayList<>(), ImageFormats.DCX, null, 0, null, 0, 0, 0.0f, 0, 0.0f, 0, false, false, false,
92                  ColorType.BW, null);
93          final String expected = "Format Details: null\n" + "Bits Per Pixel: 0\n" + "Comments: 0\n" + "Format: DCX\n" + "Format Name: null\n"
94                  + "Compression Algorithm: null\n" + "Height: 0\n" + "MimeType: null\n" + "Number Of Images: 0\n" + "Physical Height Dpi: 0\n"
95                  + "Physical Height Inch: 0.0\n" + "Physical Width Dpi: 0\n" + "Physical Width Inch: 0.0\n" + "Width: 0\n" + "Is Progressive: false\n"
96                  + "Is Transparent: false\n" + "Color Type: Black and White\n" + "Uses Palette: false\n";
97          final String testString = imageInfo.toString().replaceAll("\\r", "");
98          assertEquals(expected, testString);
99      }
100 
101     @Test
102     public void testToStringErrorWhenColorPaletteIsNull() {
103         final ImageInfo imageInfo = new ImageInfo(null, 0, new ArrayList<>(), ImageFormats.DCX, null, 0, null, 0, 0, 0.0f, 0, 0.0f, 0, false, false, false,
104                 null, null);
105         assertEquals("Image Data: Error", imageInfo.toString());
106     }
107 
108     @Test
109     public void testToStringErrorWhenCommentsIsNull() {
110         final ImageInfo imageInfo = new ImageInfo(null, 0, null, null, null, 0, null, 0, 0, 0.0f, 0, 0.0f, 0, false, false, false, null, null);
111         assertEquals("Image Data: Error", imageInfo.toString());
112     }
113 
114     @Test
115     public void testToStringErrorWhenFormatIsNull() {
116         final ImageInfo imageInfo = new ImageInfo(null, 0, new ArrayList<>(), null, null, 0, null, 0, 0, 0.0f, 0, 0.0f, 0, false, false, false, ColorType.BW,
117                 null);
118         assertEquals("Image Data: Error", imageInfo.toString());
119     }
120 
121     @Test
122     public void testToStringWithComments() {
123         final ImageInfo imageInfo = new ImageInfo(null, 0, Arrays.asList("a", "b"), ImageFormats.DCX, null, 0, null, 0, 0, 0.0f, 0, 0.0f, 0, false, false,
124                 false, ColorType.BW, null);
125         final String expected = "Format Details: null\n" + "Bits Per Pixel: 0\n" + "Comments: 2\n" + "\t0: 'a'\n" + "\t1: 'b'\n" + "Format: DCX\n"
126                 + "Format Name: null\n" + "Compression Algorithm: null\n" + "Height: 0\n" + "MimeType: null\n" + "Number Of Images: 0\n"
127                 + "Physical Height Dpi: 0\n" + "Physical Height Inch: 0.0\n" + "Physical Width Dpi: 0\n" + "Physical Width Inch: 0.0\n" + "Width: 0\n"
128                 + "Is Progressive: false\n" + "Is Transparent: false\n" + "Color Type: Black and White\n" + "Uses Palette: false\n";
129         final String testString = imageInfo.toString().replaceAll("\\r", "");
130         assertEquals(expected, testString);
131     }
132 }