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.analysis;
20  
21  import org.apache.rat.api.Document;
22  import org.apache.rat.document.IDocumentAnalyser;
23  import org.apache.rat.document.impl.MonolithicFileDocument;
24  import org.apache.rat.report.claim.impl.xml.SimpleXmlClaimReporter;
25  import org.apache.rat.report.xml.writer.impl.base.XmlWriter;
26  import org.apache.rat.test.utils.Resources;
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  import java.io.StringWriter;
31  
32  import static org.junit.Assert.assertEquals;
33  
34  public class AnalyserFactoryTest {
35  
36      private static final IHeaderMatcher MATCHES_NOTHING_MATCHER = new IHeaderMatcher() {
37          public boolean match(Document subject, String line) throws RatHeaderAnalysisException {
38              return false;
39          }
40  
41          public void reset() {
42          }
43      };
44  
45      private StringWriter out;
46      private SimpleXmlClaimReporter reporter;
47      private IDocumentAnalyser analyser;
48  
49      @Before
50      public void setUp() throws Exception {
51          out = new StringWriter();
52          final XmlWriter writer = new XmlWriter(out);
53          reporter = new SimpleXmlClaimReporter(writer);
54          analyser = DefaultAnalyserFactory.createDefaultAnalyser(MATCHES_NOTHING_MATCHER);
55      }
56  
57      @Test
58      public void standardTypeAnalyser() throws Exception {
59          final MonolithicFileDocument document = new MonolithicFileDocument(Resources.getResourceFile("/elements/Text.txt"));
60          analyser.analyse(document);
61          reporter.report(document);
62          assertEquals("Open standard element", //
63                  "<resource name='src/test/resources/elements/Text.txt'><header-sample>/*\n" +
64                          " * Licensed to the Apache Software Foundation (ASF) under one\n" +
65                          " * or more contributor license agreements.  See the NOTICE file\n" +
66                          " * distributed with this work for additional information\n" +
67                          " * regarding copyright ownership.  The ASF licenses this file\n" +
68                          " * to you under the Apache License, Version 2.0 (the \"License\");\n" +
69                          " * you may not use this file except in compliance with the License.\n" +
70                          " * You may obtain a copy of the License at\n" +
71                          " *\n" +
72                          " *    http://www.apache.org/licenses/LICENSE-2.0\n" +
73                          " *\n" +
74                          " * Unless required by applicable law or agreed to in writing,\n" +
75                          " * software distributed under the License is distributed on an\n" +
76                          " * \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n" +
77                          " * KIND, either express or implied.  See the License for the\n" +
78                          " * specific language governing permissions and limitations\n" +
79                          " * under the License.    \n" +
80                          " */\n" +
81                          "\n" +
82                          "            \n" +
83                          "</header-sample><header-type name='?????'/><license-family name='?????'/><type name='standard'/>", out.toString());
84      }
85  
86      @Test
87      public void noteTypeAnalyser() throws Exception {
88          final MonolithicFileDocument document = new MonolithicFileDocument(Resources.getResourceFile("/elements/LICENSE"));
89          analyser.analyse(document);
90          reporter.report(document);
91          assertEquals("Open note element", "<resource name='src/test/resources/elements/LICENSE'><type name='notice'/>", out.toString());
92      }
93  
94      @Test
95      public void binaryTypeAnalyser() throws Exception {
96          final MonolithicFileDocument document = new MonolithicFileDocument(Resources.getResourceFile("/elements/Image.png"));
97          analyser.analyse(document);
98          reporter.report(document);
99          assertEquals("Open binary element", "<resource name='src/test/resources/elements/Image.png'><type name='binary'/>", out.toString());
100     }
101 
102     @Test
103     public void archiveTypeAnalyser() throws Exception {
104         final MonolithicFileDocument document = new MonolithicFileDocument(Resources.getResourceFile("/elements/dummy.jar"));
105         analyser.analyse(document);
106         reporter.report(document);
107         assertEquals("Open archive element", "<resource name='src/test/resources/elements/dummy.jar'><type name='archive'/>", out.toString());
108     }
109 
110     @Test
111     public void archiveTypeAnalyserIntelliJ() throws Exception {
112         final MonolithicFileDocument document = new MonolithicFileDocument(Resources.getResourceFile("/elements/dummy.jar"));
113         analyser.analyse(document);
114         reporter.report(document);
115         assertEquals("Open archive element", "<resource name='src/test/resources/elements/dummy.jar'><type name='archive'/>", out.toString());
116     }
117 }