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;
19  
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import org.apache.commons.imaging.internal.Debug;
29  import org.apache.commons.imaging.test.FileSystemTraversal;
30  
31  public abstract class AbstractImagingTest {
32  
33      public interface ImageFilter {
34          boolean accept(File file) throws IOException, ImagingException;
35      }
36  
37      private static final List<File> ALL_IMAGES = new ArrayList<>();
38  
39      static {
40          File imagesFolder = ImagingTestConstants.TEST_IMAGE_FOLDER;
41  
42          imagesFolder = imagesFolder.getAbsoluteFile();
43  
44          Debug.debug("imagesFolder", imagesFolder);
45          assertTrue(imagesFolder.exists());
46  
47          final FileSystemTraversal.Visitor visitor = (file, progressEstimate) -> {
48              if (!Imaging.hasImageFileExtension(file)) {
49                  return true;
50              }
51              ALL_IMAGES.add(file);
52              return true;
53          };
54          new FileSystemTraversal().traverseFiles(imagesFolder, visitor);
55      }
56  
57      protected static List<File> getTestImages() throws IOException, ImagingException {
58          return getTestImages(null, -1);
59      }
60  
61      protected static List<File> getTestImages(final ImageFilter filter) throws IOException, ImagingException {
62          return getTestImages(filter, -1);
63      }
64  
65      protected static List<File> getTestImages(final ImageFilter filter, final int max) throws IOException, ImagingException {
66          final List<File> images = new ArrayList<>();
67  
68          for (final File file : ALL_IMAGES) {
69              if (!Imaging.hasImageFileExtension(file)) {
70                  continue;
71              }
72  
73              if (file.getParentFile().getName().toLowerCase().equals("@broken")) {
74                  continue;
75              }
76  
77              if (filter != null && !filter.accept(file)) {
78                  continue;
79              }
80  
81              images.add(file);
82  
83              if (max > 0 && images.size() >= max) {
84                  break;
85              }
86          }
87  
88          assertFalse(images.isEmpty());
89  
90          return images;
91      }
92  
93      protected File getTestImage() throws IOException, ImagingException {
94          return getTestImage(null);
95      }
96  
97      protected File getTestImage(final ImageFilter filter) throws IOException, ImagingException {
98          final List<File> images = getTestImages(filter, 1);
99  
100         assertFalse(images.isEmpty());
101 
102         return images.get(0);
103     }
104 
105     protected File getTestImageByName(final String fileName) throws IOException, ImagingException {
106         return getTestImage(file -> file.getName().equals(fileName));
107     }
108 
109     protected boolean isInvalidPngTestFile(final File file) {
110         return file.getParentFile().getName().equalsIgnoreCase("pngsuite") && file.getName().toLowerCase().startsWith("x");
111     }
112 
113     protected boolean isPhilHarveyTestImage(final File file) {
114         return file.getAbsolutePath().startsWith(ImagingTestConstants.PHIL_HARVEY_TEST_IMAGE_FOLDER.getAbsolutePath());
115     }
116 }