View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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  }