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.policy;
20  
21  import org.apache.rat.Defaults;
22  import org.apache.rat.api.Document;
23  import org.apache.rat.api.MetaData;
24  import org.apache.rat.document.MockLocation;
25  import org.apache.rat.license.ILicenseFamily;
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  import java.util.ArrayList;
30  
31  import static org.junit.Assert.assertEquals;
32  
33  
34  public class DefaultPolicyTest {
35      private static final int NUMBER_OF_DEFAULT_ACCEPTED_LICENSES = Defaults.DEFAULT_LICENSE_FAMILIES.size();
36  
37      private static final ILicenseFamily JUST_A_TEST_LIC_FAMILY = new ILicenseFamily() {
38          public String getFamilyName() {
39              return "justATest";
40          }
41      };
42  
43      private Document subject;
44      private DefaultPolicy policy;
45  
46      @Before
47      public void setUp() throws Exception {
48          policy = new DefaultPolicy();
49          subject = new MockLocation("subject");
50      }
51  
52      @SuppressWarnings("boxing") // OK in test code
53      private void assertApproval(boolean pApproved) {
54          assertEquals(pApproved, MetaData.RAT_APPROVED_LICENSE_VALUE_TRUE.equals(subject.getMetaData().value(MetaData.RAT_URL_APPROVED_LICENSE)));
55      }
56  
57      @Test
58      public void testALFamily() throws Exception {
59          assertEquals(NUMBER_OF_DEFAULT_ACCEPTED_LICENSES, policy.getApprovedLicenseNames().size());
60          subject.getMetaData().set(MetaData.RAT_LICENSE_FAMILY_NAME_DATUM_APACHE_LICENSE_VERSION_2_0);
61          policy.analyse(subject);
62          assertApproval(true);
63      }
64  
65      @Test
66      public void testOASISFamily() throws Exception {
67          assertEquals(NUMBER_OF_DEFAULT_ACCEPTED_LICENSES, policy.getApprovedLicenseNames().size());
68          subject.getMetaData().set(MetaData.RAT_LICENSE_FAMILY_NAME_DATUM_OASIS_OPEN_LICENSE);
69          policy.analyse(subject);
70          assertApproval(true);
71      }
72  
73      @Test
74      public void testW3CFamily() throws Exception {
75          assertEquals(NUMBER_OF_DEFAULT_ACCEPTED_LICENSES, policy.getApprovedLicenseNames().size());
76          subject.getMetaData().set(MetaData.RAT_LICENSE_FAMILY_NAME_DATUM_W3C_SOFTWARE_COPYRIGHT);
77          policy.analyse(subject);
78          assertApproval(true);
79      }
80  
81      @Test
82      public void testW3CDocFamily() throws Exception {
83          assertEquals(NUMBER_OF_DEFAULT_ACCEPTED_LICENSES, policy.getApprovedLicenseNames().size());
84          subject.getMetaData().set(MetaData.RAT_LICENSE_FAMILY_NAME_DATUM_W3C_DOCUMENT_COPYRIGHT);
85          policy.analyse(subject);
86          assertApproval(true);
87      }
88  
89      @Test
90      public void testModifiedBSDFamily() throws Exception {
91          assertEquals(NUMBER_OF_DEFAULT_ACCEPTED_LICENSES, policy.getApprovedLicenseNames().size());
92          subject.getMetaData().set(MetaData.RAT_LICENSE_FAMILY_NAME_DATUM_MODIFIED_BSD_LICENSE);
93          policy.analyse(subject);
94          assertApproval(true);
95      }
96  
97      @Test
98      public void testMITFamily() throws Exception {
99          assertEquals(NUMBER_OF_DEFAULT_ACCEPTED_LICENSES, policy.getApprovedLicenseNames().size());
100         subject.getMetaData().set(MetaData.RAT_LICENSE_FAMILY_NAME_DATUM_MIT);
101         policy.analyse(subject);
102         assertApproval(true);
103     }
104 
105     @Test
106     public void testCDDL1Family() throws Exception {
107         assertEquals(NUMBER_OF_DEFAULT_ACCEPTED_LICENSES, policy.getApprovedLicenseNames().size());
108         subject.getMetaData().set(MetaData.RAT_LICENSE_FAMILY_NAME_DATUM_CDDL1);
109         policy.analyse(subject);
110         assertApproval(true);
111     }
112 
113     @Test
114     public void testUnknownFamily() throws Exception {
115         assertEquals(NUMBER_OF_DEFAULT_ACCEPTED_LICENSES, policy.getApprovedLicenseNames().size());
116         subject.getMetaData().set(MetaData.RAT_LICENSE_FAMILY_NAME_DATUM_UNKNOWN);
117         policy.analyse(subject);
118         assertApproval(false);
119     }
120 
121     @Test
122     public void testNullAsMarkerOfDefaults() {
123         // with defaults
124         for (DefaultPolicy policy : new DefaultPolicy[]{//
125                 new DefaultPolicy(), //
126                 new DefaultPolicy(new ArrayList<String>(0), true),//
127                 new DefaultPolicy(new ILicenseFamily[]{}, true),
128         }) {
129             assertEquals("Did you add new license defaults?", NUMBER_OF_DEFAULT_ACCEPTED_LICENSES, policy.getApprovedLicenseNames().size());
130         }
131 
132         // without defaults and no additions == 0
133         for (DefaultPolicy policy : new DefaultPolicy[]{//
134                 new DefaultPolicy(new ArrayList<String>(0), false),//
135                 new DefaultPolicy(new ILicenseFamily[]{}, false),
136         }) {
137             assertEquals(0, policy.getApprovedLicenseNames().size());
138         }
139     }
140 
141     @Test
142     public void testAddNewApprovedLicenseAndDefaults() {
143         assertEquals("justATest", new DefaultPolicy(new ILicenseFamily[]{JUST_A_TEST_LIC_FAMILY}, false).getApprovedLicenseNames().get(0));
144         assertEquals("Did not properly merge approved licenses with default", 1, new DefaultPolicy(new ILicenseFamily[]{JUST_A_TEST_LIC_FAMILY}, false).getApprovedLicenseNames().size());
145     }
146 
147     @Test
148     public void testAddNewApprovedLicenseNoDefaults() {
149         assertEquals("justATest", new DefaultPolicy(new ILicenseFamily[]{JUST_A_TEST_LIC_FAMILY}, false).getApprovedLicenseNames().get(0));
150         assertEquals("Did not properly merge approved licenses with default", NUMBER_OF_DEFAULT_ACCEPTED_LICENSES + 1, new DefaultPolicy(new ILicenseFamily[]{JUST_A_TEST_LIC_FAMILY}, true).getApprovedLicenseNames().size());
151     }
152 }