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 org.apache.commons.io.IOUtils;
20 import org.apache.tools.ant.BuildFileRule;
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.junit.Rule;
24
25 import java.io.File;
26 import java.io.FileReader;
27 import java.io.IOException;
28 import java.util.regex.Pattern;
29
30 public abstract class AbstractRatAntTaskTest {
31 private static final File tempDir = new File("target/anttasks");
32 @Rule
33 public final BuildFileRule buildRule = new BuildFileRule();
34
35 protected abstract File getAntFile();
36
37 protected File getTempDir() {
38 return tempDir;
39 }
40
41 @Before
42 public void setUp() {
43 buildRule.configureProject(getAntFile().getPath());
44 buildRule.getProject().setProperty("output.dir", tempDir.getAbsolutePath());
45 buildRule.getProject().setProperty("resource.dir", getAntFile().getParent());
46 }
47
48 protected void assertLogDoesNotMatch(String pPattern) {
49 final String log = buildRule.getLog();
50 Assert.assertFalse("Log matches the pattern: " + pPattern + ", got " + log,
51 isMatching(pPattern, log));
52 }
53
54 protected void assertLogMatches(String pPattern) {
55 final String log = buildRule.getLog();
56 Assert.assertTrue("Log doesn't match string: " + pPattern + ", got " + log,
57 isMatching(pPattern, log));
58 }
59
60 private boolean isMatching(final String pPattern, final String pValue) {
61 return Pattern.compile(pPattern).matcher(pValue).find();
62 }
63
64 private String load(File pFile) throws IOException {
65 FileReader fr = new FileReader(pFile);
66 try {
67 final StringBuilder sb = new StringBuilder();
68 char[] buffer = new char[1024];
69 for (;;) {
70 int res = fr.read(buffer);
71 if (res == -1) {
72 fr.close();
73 fr = null;
74 return sb.toString();
75 }
76 if (res > 0) {
77 sb.append(buffer, 0, res);
78 }
79 }
80 } finally {
81 IOUtils.closeQuietly(fr);
82 }
83 }
84
85 protected void assertFileMatches(File pFile, String pPattern)
86 throws IOException {
87 final String content = load(pFile);
88 Assert.assertTrue("File " + pFile
89 + " doesn't match the pattern " + pPattern
90 + ", got " + content,
91 isMatching(pPattern, content));
92 }
93 }