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.tiff;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  
21  import java.io.File;
22  
23  import org.apache.commons.imaging.ImageInfo;
24  import org.apache.commons.imaging.Imaging;
25  import org.apache.commons.imaging.ImagingTestConstants;
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Performs a test on the ImageInfo returned from TIFF to confirm that it contains the specified value for a particular target. This test is used to confirm
30   * that the TiffImageParser is correctly interpreting the ImageInfo values.
31   */
32  public class TiffReadImageInfoTest extends TiffBaseTest {
33  
34      // The form of the test set is
35      // 0. target file name
36      // 1. Parameter field in ImageInfo
37      // 2. Expected value
38      static final String[][] testSet = { { "1/matthew2.tif", "Color Type", "Black and White" },
39              { "7/Oregon Scientific DS6639 - DSC_0307 - small - CMYK.tiff", "Color Type", "CMYK" }, { "10/Imaging247.TIFF", "Uses Palette", "true" },
40              { "12/TransparencyTestStripAssociated.tif", "Is Transparent", "true" }, { "14/TestJpegStrips.tiff", "Color Type", "YCbCr" } };
41  
42      private File getTiffFile(final String name) {
43          final File tiffFolder = new File(ImagingTestConstants.TEST_IMAGE_FOLDER, "tiff");
44          return new File(tiffFolder, name);
45      }
46  
47      /**
48       * Gets the value for the target data field within the ImageInfo string. This method expects data fields to be given in the form: parameter name, colon,
49       * value, end-of-line
50       *
51       * @param info   a valid instance obtained from TiffImageParser
52       * @param target the target data field string
53       * @return the value
54       */
55      private String getValue(final ImageInfo info, final String target) {
56          final String s = info.toString();
57          final int i = s.indexOf(target);
58          if (i < 0) {
59              return "";
60          }
61          final int j = s.indexOf(':', i);
62          if (j < 0) {
63              return "";
64          }
65          final int k = s.indexOf('\n', j);
66          if (k < j) {
67              return "";
68          }
69          return s.substring(j + 1, k).trim();
70      }
71  
72      @Test
73      public void testImageInfoElements() throws Exception {
74          for (final String[] testTarget : testSet) {
75              final File targetFile = getTiffFile(testTarget[0]);
76              final ImageInfo info = Imaging.getImageInfo(targetFile);
77              final String value = getValue(info, testTarget[1]);
78              final String identifier = targetFile.getName() + ": " + testTarget[1];
79              assertEquals(value.toLowerCase(), testTarget[2].toLowerCase(), identifier);
80          }
81      }
82  
83  }