1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.filter;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertTrue;
21 import static org.junit.jupiter.api.Assertions.fail;
22
23 import java.io.BufferedInputStream;
24 import java.io.BufferedOutputStream;
25 import java.io.File;
26 import java.io.FileFilter;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.nio.file.Files;
30 import java.util.Arrays;
31 import java.util.List;
32 import java.util.Objects;
33 import java.util.zip.ZipEntry;
34 import java.util.zip.ZipOutputStream;
35
36 import org.apache.commons.io.FileUtils;
37 import org.apache.commons.io.function.Uncheck;
38 import org.apache.commons.vfs2.FileObject;
39 import org.apache.commons.vfs2.FileSelectInfo;
40 import org.apache.commons.vfs2.FileSystemException;
41 import org.apache.commons.vfs2.FileSystemManager;
42 import org.apache.commons.vfs2.VFS;
43
44
45
46
47 public abstract class BaseFilterTest {
48
49
50
51
52
53
54
55 protected static void assertContains(final List<?> list, final Object... objects) {
56 for (final Object obj : objects) {
57 assertTrue(list.indexOf(obj) > -1, () -> "Couldn't find " + obj + " in " + Arrays.asList(objects));
58 }
59 }
60
61
62
63
64
65
66
67 protected static void assertContainsOnly(final List<?> list, final Object... objects) {
68 for (final Object obj : objects) {
69 assertTrue(list.indexOf(obj) > -1, () -> "Couldn't find " + obj + " in " + Arrays.asList(objects));
70 }
71 assertEquals(objects.length, list.size());
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85 public static String concatPathAndFileName(final String path, final String fileName, final String separator) {
86
87 if (fileName == null) {
88 throw new IllegalArgumentException("file name cannot be null");
89 }
90 if (fileName.trim().isEmpty()) {
91 throw new IllegalArgumentException("file name cannot be empty");
92 }
93 if (separator == null) {
94 throw new IllegalArgumentException("separator cannot be null");
95 }
96 if (separator.trim().isEmpty()) {
97 throw new IllegalArgumentException("separator cannot be empty");
98 }
99
100 if (path == null) {
101 return fileName;
102 }
103 final String trimmedPath = path.trim();
104 if (trimmedPath.isEmpty()) {
105 return fileName;
106 }
107 final String trimmedFileName = fileName.trim();
108 if (trimmedPath.endsWith(separator)) {
109 return trimmedPath + trimmedFileName;
110 }
111 return trimmedPath + separator + trimmedFileName;
112 }
113
114
115
116
117
118
119
120 protected static FileSelectInfo createFileSelectInfo(final File file) {
121 return Uncheck.get(() -> {
122 final FileObject fileObject = VFS.getManager().toFileObject(file);
123 return new FileSelectInfo() {
124 @Override
125 public FileObject getBaseFolder() {
126 return Uncheck.get(fileObject::getParent);
127 }
128
129 @Override
130 public int getDepth() {
131 return 0;
132 }
133
134 @Override
135 public FileObject getFile() {
136 return fileObject;
137 }
138
139 @Override
140 public String toString() {
141 return Objects.toString(fileObject);
142 }
143 };
144 });
145 }
146
147 protected static void delete(final File file) {
148 if (file != null) {
149 file.delete();
150 }
151 }
152
153
154
155
156
157
158 protected static File getTempDir() {
159 return FileUtils.getTempDirectory();
160 }
161
162
163
164
165
166
167
168 protected static File getTestDir(final String name) {
169 final File file = new File(getTempDir(), name);
170 file.mkdirs();
171 return file;
172 }
173
174
175
176
177
178
179
180
181 protected static FileObject getZipFileObject(final File file) throws FileSystemException {
182 final FileSystemManager fsManager = VFS.getManager();
183 return fsManager.resolveFile("zip:" + file.toURI());
184 }
185
186
187
188
189
190
191
192
193
194 private static File[] listFiles(final File srcDir, final FileFilter filter) {
195 return srcDir.listFiles(filter);
196 }
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 public static void zipDir(final File srcDir, final FileFilter filter, final String destPath, final File destFile)
212 throws IOException {
213 if (srcDir == null) {
214 throw new IllegalArgumentException("srcDir cannot be null");
215 }
216 if (!srcDir.exists()) {
217 throw new IllegalArgumentException("srcDir does not exist");
218 }
219 if (!srcDir.isDirectory()) {
220 throw new IllegalArgumentException("srcDir is not a directory");
221 }
222 if (destFile == null) {
223 throw new IllegalArgumentException("destFile cannot be null");
224 }
225 try (ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(Files.newOutputStream(destFile.toPath())))) {
226 zipDir(srcDir, filter, destPath, out);
227 }
228
229 }
230
231
232
233
234
235
236
237
238
239
240
241
242 private static void zipDir(final File srcDir, final FileFilter filter, final String destPath,
243 final ZipOutputStream out) throws IOException {
244
245 final File[] files = listFiles(srcDir, filter);
246 for (final File file : files) {
247 if (file.isDirectory()) {
248 zipDir(file, filter, concatPathAndFileName(destPath, file.getName(), File.separator), out);
249 } else {
250 zipFile(file, destPath, out);
251 }
252 }
253
254 }
255
256
257
258
259
260
261
262
263
264
265
266
267 public static void zipDir(final File srcDir, final String destPath, final File destFile) throws IOException {
268
269 zipDir(srcDir, null, destPath, destFile);
270 }
271
272
273
274
275
276
277
278
279
280 private static void zipFile(final File srcFile, final String destPath, final ZipOutputStream out)
281 throws IOException {
282
283 final byte[] buf = new byte[1024];
284 try (InputStream in = new BufferedInputStream(Files.newInputStream(srcFile.toPath()))) {
285 final ZipEntry zipEntry = new ZipEntry(concatPathAndFileName(destPath, srcFile.getName(), File.separator));
286 zipEntry.setTime(srcFile.lastModified());
287 out.putNextEntry(zipEntry);
288 int len;
289 while ((len = in.read(buf)) > 0) {
290 out.write(buf, 0, len);
291 }
292 out.closeEntry();
293 }
294 }
295
296
297
298
299
300
301
302 protected void assertContains(final FileObject[] files, final String... fileNames) {
303 for (final String fileName : fileNames) {
304 if (!find(files, fileName)) {
305 fail("File '" + fileName + "' not found in: " + Arrays.asList(files));
306 }
307 }
308 }
309
310 private boolean find(final FileObject[] files, final String fileName) {
311 for (final FileObject file : files) {
312 final String name = file.getName().getBaseName();
313 if (name.equals(fileName)) {
314 return true;
315 }
316 }
317 return false;
318 }
319
320 }