1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.rat.anttasks;
18
19 import java.util.regex.Matcher;
20 import org.apache.commons.io.IOUtils;
21 import org.apache.tools.ant.BuildFileRule;
22
23 import java.io.File;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.util.regex.Pattern;
27 import org.junit.jupiter.api.BeforeEach;
28
29 import static org.assertj.core.api.Assertions.assertThat;
30
31 public abstract class AbstractRatAntTaskTest {
32 private static final File tempDir = new File("target/anttasks");
33
34 public final BuildFileRule buildRule = new BuildFileRule();
35
36 protected abstract File getAntFile();
37
38 protected File getTempDir() {
39 return tempDir;
40 }
41
42 @BeforeEach
43 public void setUp() {
44 buildRule.configureProject(getAntFile().getPath());
45 buildRule.getProject().setProperty("output.dir", tempDir.getAbsolutePath());
46 buildRule.getProject().setProperty("resource.dir", getAntFile().getParent());
47 }
48
49 protected void assertLogDoesNotMatch(String pPattern) {
50 final String log = buildRule.getLog();
51 final Matcher matcher = Pattern.compile(pPattern).matcher(log);
52
53 assertThat(matcher.find()).describedAs("Log matches the pattern: {}",pPattern).isFalse();
54 }
55
56 protected void assertLogMatches(String pPattern) {
57 final String log = buildRule.getLog();
58 final Matcher matcher = Pattern.compile(pPattern).matcher(log);
59 assertThat(matcher.find()).describedAs("Log does not the pattern: {}",pPattern).isTrue();
60 }
61
62 private String load(File pFile) throws IOException {
63 FileReader fr = new FileReader(pFile);
64 try {
65 final StringBuilder sb = new StringBuilder();
66 char[] buffer = new char[1024];
67 for (;;) {
68 int res = fr.read(buffer);
69 if (res == -1) {
70 fr.close();
71 fr = null;
72 return sb.toString();
73 }
74 if (res > 0) {
75 sb.append(buffer, 0, res);
76 }
77 }
78 } finally {
79 IOUtils.closeQuietly(fr);
80 }
81 }
82
83 protected void assertFileMatches(File pFile, String pPattern)
84 throws IOException {
85 final String content = load(pFile);
86 final Matcher matcher = Pattern.compile(pPattern).matcher(content);
87 assertThat(matcher.find()).describedAs("File {} doesn't match the pattern:{} ", pFile, pPattern).isTrue();
88 }
89 }