View Javadoc
1   /**
2    * Copyright 2011, Aiki IT, FotoRenamer
3    * <p>
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * <p>
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * <p>
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.aikiit.fotorenamer.image;
17  
18  import de.aikiit.fotorenamer.TestConstants;
19  import org.apache.commons.imaging.formats.tiff.constants.ExifTagConstants;
20  import org.apache.log4j.Logger;
21  import org.junit.jupiter.api.Test;
22  
23  import java.io.File;
24  import java.util.regex.Matcher;
25  
26  import static org.junit.jupiter.api.Assertions.assertThrows;
27  import static org.junit.jupiter.api.Assertions.assertTrue;
28  
29  /**
30   * Read properties from test images and verify exif-metadata extraction.
31   *
32   * @author hirsch
33   */
34  class MetaDataExtractorTest {
35      private static final Logger LOG = Logger.getLogger(MetaDataExtractorTest.class);
36  
37      @Test
38      void makeSurePatternMatchesMultipleRenamedFiles() {
39          Matcher matcher = TestConstants.IS_TEST_FILE.matcher("123123123123_20110130_131102_IMG_7559_mini.JpG");
40          assertTrue(matcher.find());
41      }
42  
43      @Test
44      void ensureGivenExampleFileIsRenamed() throws Exception {
45          File f = new File(TestConstants.FULLPATH_TEST_IMG);
46  
47          int minLength = TestConstants.PLAIN_FILE_NAME.length();
48  
49          if (!f.exists()) {
50              LOG.debug("Switching to renamed file, since another test may have touched it before.");
51              f = new File(TestConstants.FULLPATH_TEST_IMG_RENAMED);
52              minLength = f.getName().length();
53          }
54  
55          LOG.debug("Extracting metadata from " + f.getAbsolutePath());
56  
57          String renamedFile =
58                  MetaDataExtractor
59                          .generateCreationDateInCorrectFormat(f);
60          assertTrue(renamedFile.endsWith(TestConstants.PLAIN_FILE_NAME));
61          assertTrue(renamedFile.length() >= minLength);
62      }
63  
64      /**
65       * Checks assertion failure with null parameter.
66       */
67      @Test
68      void checkAssertionErrors() {
69          assertThrows(AssertionError.class, () -> MetaDataExtractor.getExifMetadata(null, ExifTagConstants.EXIF_TAG_BRIGHTNESS));
70      }
71  
72      /**
73       * Checks assertion failure with null parameter.
74       */
75      @Test
76      void checkAssertionErrorTag() {
77          assertThrows(AssertionError.class, () -> MetaDataExtractor.getExifMetadata(new File(TestConstants.FULLPATH_TEST_IMG), null));
78      }
79  
80  }