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