1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 }