View Javadoc
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.configuration.builders;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  
22  import java.io.StringReader;
23  import java.util.SortedSet;
24  
25  import org.apache.rat.analysis.IHeaderMatcher;
26  import org.apache.rat.analysis.matchers.FullTextMatcher;
27  import org.apache.rat.analysis.matchers.NotMatcher;
28  import org.apache.rat.configuration.XMLConfigurationReader;
29  import org.apache.rat.license.ILicense;
30  import org.junit.jupiter.api.Test;
31  
32  public class NotBuilderTest {
33  
34      @Test
35      public void xmlTest() {
36          String configStr = "<rat-config>" //
37                  + "        <families>" //
38                  + "            <family id='newFam' name='my new family' />" //
39                  + "        </families>" //
40                  + "        <licenses>" //
41                  + "            <license family='newFam' id='EXAMPLE' name='Example License'>" //
42                  + "                <not>" //
43                  + "                   <text>The text to match</text>" //
44                  + "                </not>" //
45                  + "            </license>" //
46                  + "        </licenses>" //
47                  + "    </rat-config>";
48  
49          XMLConfigurationReader reader = new XMLConfigurationReader();
50          reader.read(new StringReader(configStr));
51          SortedSet<ILicense> licenses = reader.readLicenses();
52          assertEquals(1, licenses.size());
53          IHeaderMatcher matcher = licenses.first().getMatcher();
54          assertEquals(NotMatcher.class, matcher.getClass());
55          NotMatcher result = (NotMatcher) matcher;
56          assertNotNull(result.getEnclosed());
57          assertEquals(FullTextMatcher.class, result.getEnclosed().getClass());
58      }
59  
60      @Test
61      public void xmlIdTest() {
62          String configStr = "<rat-config>" //
63                  + "        <families>" //
64                  + "            <family id='newFam' name='my new family' />" //
65                  + "        </families>" //
66                  + "        <licenses>" //
67                  + "            <license family='newFam' id='EXAMPLE' name='Example License'>" //
68                  + "                <not id='foo'>" //
69                  + "                   <text>The text to match</text>" //
70                  + "                </not>" //
71                  + "            </license>" //
72                  + "        </licenses>" //
73                  + "    </rat-config>";
74  
75          XMLConfigurationReader reader = new XMLConfigurationReader();
76          reader.read(new StringReader(configStr));
77          SortedSet<ILicense> licenses = reader.readLicenses();
78          assertEquals(1, licenses.size());
79          IHeaderMatcher matcher = licenses.first().getMatcher();
80          assertEquals("foo", matcher.getId());
81          assertEquals(NotMatcher.class, matcher.getClass());
82          NotMatcher result = (NotMatcher) matcher;
83          assertNotNull(result.getEnclosed());
84          assertEquals(FullTextMatcher.class, result.getEnclosed().getClass());
85      }
86  }