/
RetrolambdaTest.java
93 lines (72 loc) · 3.17 KB
/
RetrolambdaTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright © 2013-2015 Esko Luontola <www.orfjackal.net>
// This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0
package net.orfjackal.retrolambda;
import org.junit.*;
import org.junit.rules.TemporaryFolder;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertTrue;
public class RetrolambdaTest {
@Rule
public final TemporaryFolder tempDir = new TemporaryFolder();
private Path inputDir;
private Path outputDir;
private final List<Path> visitedFiles = new ArrayList<>();
private final FileVisitor<Path> visitor = new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
visitedFiles.add(file);
return FileVisitResult.CONTINUE;
}
};
private Path file1;
private Path file2;
private Path fileInSubdir;
private Path outsider;
@Before
public void setup() throws IOException {
inputDir = tempDir.newFolder("inputDir").toPath();
outputDir = tempDir.newFolder("outputDir").toPath();
file1 = Files.createFile(inputDir.resolve("file1.txt"));
file2 = Files.createFile(inputDir.resolve("file2.txt"));
Path subdir = inputDir.resolve("subdir");
Files.createDirectory(subdir);
fileInSubdir = Files.createFile(subdir.resolve("file.txt"));
outsider = tempDir.newFile("outsider.txt").toPath();
}
@Test
public void by_default_visits_all_files_recursively() throws IOException {
Retrolambda.visitFiles(inputDir, null, visitor);
assertThat(visitedFiles, containsInAnyOrder(file1, file2, fileInSubdir));
}
@Test
public void when_included_files_is_set_then_visits_only_those_files() throws IOException {
List<Path> includedFiles = Arrays.asList(file1, fileInSubdir);
Retrolambda.visitFiles(inputDir, includedFiles, visitor);
assertThat(visitedFiles, containsInAnyOrder(file1, fileInSubdir));
}
@Test
public void ignores_included_files_that_are_outside_the_input_directory() throws IOException {
List<Path> includedFiles = Arrays.asList(file1, outsider);
Retrolambda.visitFiles(inputDir, includedFiles, visitor);
assertThat(visitedFiles, containsInAnyOrder(file1));
}
@Test
public void copies_resources_to_output_directory() throws Throwable {
Properties p = new Properties();
p.setProperty(SystemPropertiesConfig.INPUT_DIR, inputDir.toString());
p.setProperty(SystemPropertiesConfig.OUTPUT_DIR, outputDir.toString());
p.setProperty(SystemPropertiesConfig.CLASSPATH, "");
Retrolambda.run(new SystemPropertiesConfig(p));
assertIsFile(outputDir.resolve("file1.txt"));
assertIsFile(outputDir.resolve("subdir/file.txt"));
}
private static void assertIsFile(Path path) {
assertTrue("Expected " + path + " to be a file", Files.isRegularFile(path));
}
}