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.assertTrue;
20  
21  import java.lang.reflect.Field;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  import java.util.Set;
26  import java.util.TreeSet;
27  
28  import org.apache.commons.imaging.AbstractImagingTest;
29  import org.apache.commons.imaging.formats.tiff.constants.AdobePageMaker6TagConstants;
30  import org.apache.commons.imaging.formats.tiff.constants.AdobePhotoshopTagConstants;
31  import org.apache.commons.imaging.formats.tiff.constants.AliasSketchbookProTagConstants;
32  import org.apache.commons.imaging.formats.tiff.constants.DcfTagConstants;
33  import org.apache.commons.imaging.formats.tiff.constants.DngTagConstants;
34  import org.apache.commons.imaging.formats.tiff.constants.ExifTagConstants;
35  import org.apache.commons.imaging.formats.tiff.constants.GdalLibraryTagConstants;
36  import org.apache.commons.imaging.formats.tiff.constants.GeoTiffTagConstants;
37  import org.apache.commons.imaging.formats.tiff.constants.GpsTagConstants;
38  import org.apache.commons.imaging.formats.tiff.constants.MicrosoftHdPhotoTagConstants;
39  import org.apache.commons.imaging.formats.tiff.constants.MicrosoftTagConstants;
40  import org.apache.commons.imaging.formats.tiff.constants.MolecularDynamicsGelTagConstants;
41  import org.apache.commons.imaging.formats.tiff.constants.OceScanjobTagConstants;
42  import org.apache.commons.imaging.formats.tiff.constants.Rfc2301TagConstants;
43  import org.apache.commons.imaging.formats.tiff.constants.Tiff4TagConstants;
44  import org.apache.commons.imaging.formats.tiff.constants.TiffEpTagConstants;
45  import org.apache.commons.imaging.formats.tiff.constants.TiffTagConstants;
46  import org.apache.commons.imaging.formats.tiff.constants.WangTagConstants;
47  import org.apache.commons.imaging.formats.tiff.taginfos.TagInfo;
48  import org.junit.jupiter.api.Test;
49  
50  public class TiffTagIntegrityTest extends AbstractImagingTest {
51  
52      @Test
53      public void testTagIntegrity() {
54          verifyFields(AdobePageMaker6TagConstants.class, AdobePageMaker6TagConstants.ALL_ADOBE_PAGEMAKER_6_TAGS);
55          verifyFields(AdobePhotoshopTagConstants.class, AdobePhotoshopTagConstants.ALL_ADOBE_PHOTOSHOP_TAGS);
56          verifyFields(AliasSketchbookProTagConstants.class, AliasSketchbookProTagConstants.ALL_ALIAS_SKETCHBOOK_PRO_TAGS);
57          verifyFields(DcfTagConstants.class, DcfTagConstants.ALL_DCF_TAGS);
58          verifyFields(DngTagConstants.class, DngTagConstants.ALL_DNG_TAGS);
59          verifyFields(ExifTagConstants.class, ExifTagConstants.ALL_EXIF_TAGS);
60          verifyFields(GeoTiffTagConstants.class, GeoTiffTagConstants.ALL_GEO_TIFF_TAGS);
61          verifyFields(GdalLibraryTagConstants.class, GdalLibraryTagConstants.ALL_GDAL_LIBRARY_TAGS);
62          verifyFields(GpsTagConstants.class, GpsTagConstants.ALL_GPS_TAGS);
63          verifyFields(MolecularDynamicsGelTagConstants.class, MolecularDynamicsGelTagConstants.ALL_MOLECULAR_DYNAMICS_GEL_TAGS);
64          verifyFields(MicrosoftTagConstants.class, MicrosoftTagConstants.ALL_MICROSOFT_TAGS);
65          verifyFields(MicrosoftHdPhotoTagConstants.class, MicrosoftHdPhotoTagConstants.ALL_MICROSOFT_HD_PHOTO_TAGS);
66          verifyFields(OceScanjobTagConstants.class, OceScanjobTagConstants.ALL_OCE_SCANJOB_TAGS);
67          verifyFields(Rfc2301TagConstants.class, Rfc2301TagConstants.ALL_RFC_2301_TAGS);
68          verifyFields(Tiff4TagConstants.class, Tiff4TagConstants.ALL_TIFF_4_TAGS);
69          verifyFields(TiffEpTagConstants.class, TiffEpTagConstants.ALL_TIFF_EP_TAGS);
70          verifyFields(TiffTagConstants.class, TiffTagConstants.ALL_TIFF_TAGS);
71          verifyFields(WangTagConstants.class, WangTagConstants.ALL_WANG_TAGS);
72      }
73  
74      private void verifyFields(final Class<?> cls, final List<TagInfo> allTags) {
75          final ArrayList<Integer> fieldTags = new ArrayList<>();
76          for (final Field field : cls.getFields()) {
77              field.setAccessible(true);
78              Object obj = null;
79              try {
80                  obj = field.get(null);
81              } catch (final IllegalAccessException illegalAccess) {
82              }
83              if (obj == null) {
84                  continue;
85              }
86              if (!(obj instanceof TagInfo)) {
87                  continue;
88              }
89              final TagInfo src = (TagInfo) obj;
90              if (src.tag == -1) {
91                  // Skip TiffTagConstants.TIFF_TAG_UNKNOWN
92                  continue;
93              }
94              fieldTags.add(src.tag);
95          }
96          Collections.sort(fieldTags);
97          final Set<Integer> allTagSet = new TreeSet<>();
98          for (final TagInfo tagInfo : allTags) {
99              assertTrue(Collections.binarySearch(fieldTags, tagInfo.tag) >= 0);
100             allTagSet.add(tagInfo.tag);
101         }
102         for (final Integer tag : fieldTags) {
103             assertTrue(allTagSet.contains(tag));
104         }
105     }
106 }