View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   */
19  package org.apache.rat;
20  
21  
22  
23  import static org.junit.jupiter.api.Assertions.assertNotNull;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import java.io.File;
27  import java.io.FilenameFilter;
28  import java.io.IOException;
29  import java.io.PrintStream;
30  import java.nio.charset.StandardCharsets;
31  import java.util.Arrays;
32  
33  import org.apache.commons.cli.CommandLine;
34  import org.apache.commons.cli.DefaultParser;
35  import org.apache.commons.cli.ParseException;
36  import org.apache.commons.io.FileUtils;
37  import org.junit.jupiter.api.Disabled;
38  import org.junit.jupiter.api.Test;
39  
40  public class ReportTest {
41      @Test
42      public void parseExclusionsForCLIUsage() {
43          final FilenameFilter filter = Report
44                  .parseExclusions(Arrays.asList("", " # foo/bar", "foo", "##", " ./foo/bar"));
45          assertNotNull(filter);
46      }
47      
48      @Test
49      public void testDefaultConfiguration() throws ParseException, IOException {
50          String[] empty = {};
51          CommandLine cl = new DefaultParser().parse(Report.buildOptions(), empty);
52          ReportConfiguration config = Report.createConfiguration("", cl);
53          ReportConfigurationTest.validateDefault(config);
54      }
55  
56      @Test
57      public void testOutputOption() throws Exception {
58          CommandLine cl = new DefaultParser().parse(Report.buildOptions(), new String[]{ "-o", "target/test" });
59          ReportConfiguration config = Report.createConfiguration("target/test-classes/elements", cl);
60          Reporter.report(config);
61          File output = new File("target/test");
62          assertTrue(output.exists());
63          String content = FileUtils.readFileToString(output, StandardCharsets.UTF_8);
64          assertTrue(content.contains("2 Unknown Licenses"));
65          assertTrue(content.contains("target/test-classes/elements/Source.java"));
66          assertTrue(content.contains("target/test-classes/elements/sub/Empty.txt"));
67      }
68  
69      @Test
70      public void testDefaultOutput() throws Exception {
71          
72          PrintStream origin = System.out;
73          try {
74              File output = new File("target/sysout");
75              System.setOut(new PrintStream(output));
76              CommandLine cl = new DefaultParser().parse(Report.buildOptions(), new String[]{});
77              ReportConfiguration config = Report.createConfiguration("target/test-classes/elements", cl);
78              Reporter.report(config);
79              assertTrue(output.exists());
80              String content = FileUtils.readFileToString(output, StandardCharsets.UTF_8);
81              assertTrue(content.contains("2 Unknown Licenses"));
82              assertTrue(content.contains("target/test-classes/elements/Source.java"));
83              assertTrue(content.contains("target/test-classes/elements/sub/Empty.txt"));
84          } finally {
85              System.setOut(origin);
86          }
87      }
88  
89  }