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 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  }