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