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;
18  
19  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.net.URL;
24  import java.util.Collection;
25  import java.util.stream.Collectors;
26  
27  import org.apache.rat.configuration.builders.AbstractBuilder;
28  import org.apache.rat.configuration.builders.AllBuilder;
29  import org.apache.rat.configuration.builders.AnyBuilder;
30  import org.apache.rat.configuration.builders.CopyrightBuilder;
31  import org.apache.rat.configuration.builders.MatcherRefBuilder;
32  import org.apache.rat.configuration.builders.NotBuilder;
33  import org.apache.rat.configuration.builders.RegexBuilder;
34  import org.apache.rat.configuration.builders.SpdxBuilder;
35  import org.apache.rat.configuration.builders.TextBuilder;
36  import org.junit.jupiter.api.Test;
37  
38  public class ConfigurationReaderTest {
39  
40      public static final String[] EXPECTED_IDS = { "AL", "BSD-3", "CDDL1", "GEN", "GPL1", "GPL2", "GPL3", "MIT", "OASIS",
41              "W3C", "W3CD" };
42  
43      public static final String[] EXPECTED_LICENSES = { "AL", "ASL", "BSD-3", "CDDL1", "DOJO", "GEN", "GPL1", "GPL2",
44              "GPL3", "ILLUMOS", "MIT", "OASIS", "TMF", "W3C", "W3CD" };
45  
46      @Test
47      public void approvedLicenseIdTest() {
48          XMLConfigurationReader reader = new XMLConfigurationReader();
49          URL url = ConfigurationReaderTest.class.getResource("/org/apache/rat/default.xml");
50          reader.read(url);
51  
52          Collection<String> readCategories = reader.approvedLicenseId();
53  
54          assertArrayEquals(EXPECTED_IDS, readCategories.toArray(new String[readCategories.size()]));
55      }
56  
57      @Test
58      public void LicensesTest() {
59          XMLConfigurationReader reader = new XMLConfigurationReader();
60          URL url = ConfigurationReaderTest.class.getResource("/org/apache/rat/default.xml");
61          reader.read(url);
62  
63          Collection<String> readCategories = reader.readLicenses().stream().map(x -> x.getId())
64                  .collect(Collectors.toList());
65          assertArrayEquals(EXPECTED_LICENSES, readCategories.toArray(new String[readCategories.size()]));
66      }
67  
68      @Test
69      public void LicenseFamiliesTest() {
70          XMLConfigurationReader reader = new XMLConfigurationReader();
71          URL url = ConfigurationReaderTest.class.getResource("/org/apache/rat/default.xml");
72          reader.read(url);
73  
74          Collection<String> readCategories = reader.readFamilies().stream().map(x -> x.getFamilyCategory().trim())
75                  .collect(Collectors.toList());
76          assertArrayEquals(EXPECTED_IDS, readCategories.toArray(new String[readCategories.size()]));
77      }
78  
79      private void checkMatcher(String name, Class<? extends AbstractBuilder> clazz) {
80          AbstractBuilder builder = MatcherBuilderTracker.getMatcherBuilder(name);
81          assertNotNull(builder);
82          assertTrue(clazz.isAssignableFrom(builder.getClass()),()->name + " is not an instanceof " + clazz.getName() );
83      }
84  
85      @Test
86      public void checkSystemMatcherTest() {
87          XMLConfigurationReader reader = new XMLConfigurationReader();
88          URL url = ConfigurationReaderTest.class.getResource("/org/apache/rat/default.xml");
89          reader.read(url);
90          reader.readMatcherBuilders();
91          checkMatcher("all", AllBuilder.class);
92          checkMatcher("any", AnyBuilder.class);
93          checkMatcher("copyright", CopyrightBuilder.class);
94          checkMatcher("matcherRef", MatcherRefBuilder.class);
95          checkMatcher("not", NotBuilder.class);
96          checkMatcher("regex", RegexBuilder.class);
97          checkMatcher("spdx", SpdxBuilder.class);
98          checkMatcher("text", TextBuilder.class);
99      }
100 
101 }