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