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.io.BufferedReader;
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.IOException;
23  import java.io.InputStreamReader;
24  
25  import org.apache.tools.ant.BuildException;
26  
27  import junit.framework.Assert;
28  
29  public class ReportTest extends AbstractRatAntTaskTest {
30      private static final File antFile = new File("src/test/resources/antunit/report-junit.xml").getAbsoluteFile();
31  
32      protected File getAntFile() {
33          return antFile;
34      }
35  
36      public void testWithReportSentToAnt() throws Exception {
37          executeTarget("testWithReportSentToAnt");
38          assertLogMatches("AL +\\Q" + getAntFileName() + "\\E");
39      }
40  
41      public void testWithReportSentToFile() throws Exception {
42          final File reportFile = new File(getTempDir(), "selftest.report");
43          getTempDir().mkdirs();
44          final String alLine = "AL +\\Q" + getAntFileName() + "\\E";
45          if (reportFile.isFile()  &&  !reportFile.delete()) {
46              throw new IOException("Unable to remove report file " + reportFile);
47          }
48          executeTarget("testWithReportSentToFile");
49          assertLogDoesntMatch(alLine);
50          Assert.assertTrue("Expected report file " + reportFile, reportFile.isFile());
51          assertFileMatches(reportFile, alLine);
52      }
53  
54      public void testWithASLUnknown() throws Exception {
55          executeTarget("testWithASLUnknown");
56          assertLogDoesntMatch("AL +\\Q" + getAntFileName() + "\\E");
57          assertLogMatches("\\!\\?\\?\\?\\?\\? +\\Q" + getAntFileName() + "\\E");
58      }
59  
60      public void testCustomMatcher() throws Exception {
61          executeTarget("testCustomMatcher");
62          assertLogDoesntMatch("AL +\\Q" + getAntFileName() + "\\E");
63          assertLogMatches("EXMPL +\\Q" + getAntFileName() + "\\E");
64      }
65  
66      public void testNoResources() throws Exception {
67          try {
68              executeTarget("testNoResources");
69              fail("Expected Exception");
70          } catch (BuildException e) {
71              final String expect = "You must specify at least one file";
72              assertTrue("Expected " + expect + ", got " + e.getMessage(),
73                         e.getMessage().indexOf(expect) != -1);
74          }
75      }
76  
77      public void testNoLicenseMatchers() throws Exception {
78          try {
79              executeTarget("testNoLicenseMatchers");
80              fail("Expected Exception");
81          } catch (BuildException e) {
82              final String expect = "at least one license";
83              assertTrue("Expected " + expect + ", got " + e.getMessage(),
84                         e.getMessage().indexOf(expect) != -1);
85          }
86      }
87  
88      private String getAntFileName() {
89          return getAntFile().getPath().replace('\\', '/');
90      }
91  
92      private String getFirstLine(File pFile) throws IOException {
93          final FileInputStream fis = new FileInputStream(pFile);
94          final InputStreamReader reader = new InputStreamReader(fis, "UTF8");
95          final BufferedReader breader = new BufferedReader(reader);
96          final String result = breader.readLine();
97          breader.close();
98          return result;
99      }
100 
101     public void testAddLicenseHeaders() throws Exception {
102         executeTarget("testAddLicenseHeaders");
103 
104         final File origFile = new File("target/anttasks/it-sources/index.apt");
105         final String origFirstLine = getFirstLine(origFile);
106         assertTrue(origFirstLine.indexOf("--") != -1);
107         assertTrue(origFirstLine.indexOf("~~") == -1);
108         final File modifiedFile = new File("target/anttasks/it-sources/index.apt.new");
109         final String modifiedFirstLine = getFirstLine(modifiedFile);
110         assertTrue(modifiedFirstLine.indexOf("--") == -1);
111         assertTrue(modifiedFirstLine.indexOf("~~") != -1);
112     }
113 }