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.testhelpers;
20  
21  import org.apache.commons.lang3.StringUtils;
22  import org.apache.rat.analysis.IHeaderMatcher;
23  import org.apache.rat.license.ILicense;
24  import org.apache.rat.license.ILicenseFamily;
25  
26  /**
27   * A class to quickly build testing licenses.
28   */
29  public class TestingLicense implements ILicense {
30  
31      private final ILicenseFamily family;
32      private IHeaderMatcher matcher;
33      private String derivedFrom;
34      private String notes;
35      private String name;
36      private String id;
37  
38      /**
39       * Creates a testing license named "DfltTst" with category of "DfltT" and a default 
40       * TestingMatcher.
41       * @see TestingMatcher
42       */
43      public TestingLicense() {
44          this("DfltTst", new TestingMatcher());
45      }
46      
47      /**
48       * Creates a testing license with the specified id and a default TestingMatcher
49       * @param id The ID to use.
50       * @see TestingMatcher
51       */
52      public TestingLicense(String id) {
53          this(id, new TestingMatcher());
54      }
55  
56      /**
57       * Creates a testing license wit the specified id and matcher.
58       * @param id the ID to use
59       * @param matcher the matcher to execute.
60       */
61      public TestingLicense(String id, IHeaderMatcher matcher) {
62          this(matcher, ILicenseFamily.builder().setLicenseFamilyCategory(id)
63                  .setLicenseFamilyName("TestingLicense: " + id).build());
64      }
65      
66      /**
67       * Creates a testing license with the specified matcher and family.
68       * @param matcher the matcher to use.
69       * @param family the family for this license.
70       */
71      public TestingLicense(IHeaderMatcher matcher, ILicenseFamily family) {
72          this.family = family;
73          this.matcher = matcher;
74          this.derivedFrom = null;
75          this.notes = null;
76      }
77      
78      /**
79       * Create a testing license for the specified family using a default TestingMatcher
80       * @param family the family for the license.
81       * @see TestingMatcher
82       */
83      public TestingLicense(ILicenseFamily family) {
84          this(new TestingMatcher(), family );
85      }
86  
87      @Override
88      public String toString() {
89          return family.toString();
90      }
91  
92      /**
93       * Gets the family from  the license
94       * @return the license family.
95       */
96      public ILicenseFamily getFamily() {
97          return family;
98      }
99  
100     /**
101      * Gets the matcher from the license
102      * @return the matcher.
103      */
104     public IHeaderMatcher getMatcher() {
105         return matcher;
106     }
107 
108     /**
109      * Sets the derived from value for this license.
110      * @param derivedFrom the license this license is derived from.
111      */
112     public void setDerivedFrom(String derivedFrom) {
113         this.derivedFrom = derivedFrom;
114     }
115     
116     /**
117      * Sets the name from value for this license.
118      * @param name the name of this license.
119      */
120     public void setName(String name) {
121         this.name = name;
122     }
123 
124     public void setId(String id) {
125         this.id = id;
126     }
127     @Override
128     public String getId() {
129         return StringUtils.defaultIfBlank(id, family.getFamilyCategory().trim());
130     }
131 
132     @Override
133     public void reset() {
134         matcher.reset();
135     }
136 
137     @Override
138     public State matches(String line) {
139         return matcher.matches(line);
140     }
141 
142     @Override
143     public State finalizeState() {
144         return matcher.finalizeState();
145     }
146 
147     @Override
148     public State currentState() {
149         return matcher.currentState();
150     }
151 
152     @Override
153     public ILicenseFamily getLicenseFamily() {
154         return family;
155     }
156 
157     @Override
158     public int compareTo(ILicense other) {
159         return ILicense.getComparator().compare(this, other);
160     }
161 
162     @Override
163     public String getNotes() {
164         return notes;
165     }
166 
167     @Override
168     public String getName() {
169         return StringUtils.defaultIfBlank(name, family.getFamilyName());
170     }
171     
172     @Override
173     public String derivedFrom() {
174         return derivedFrom;
175     }
176 
177 }