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.analysis.IHeaders;
24  import org.apache.rat.license.ILicense;
25  import org.apache.rat.license.ILicenseFamily;
26  
27  /**
28   * A class to quickly build testing licenses.
29   */
30  public class TestingLicense implements ILicense {
31  
32      private final ILicenseFamily family;
33      private final IHeaderMatcher matcher;
34      private final String note;
35      private String name;
36      private final String id;
37  
38      /**
39       * Creates a testing license with the specified id and a default TestingMatcher
40       * @param id The ID to use.
41       * @see TestingMatcher
42       */
43      public TestingLicense(String id) {
44          this(id, id, new TestingMatcher());
45      }
46  
47      /**
48       * Creates a testing license with the specified id and a default TestingMatcher
49       * @param family the Family id
50       * @param id The ID to use.
51       * @see TestingMatcher
52       */
53      public TestingLicense(String family, String id) {
54          this(family, id, new TestingMatcher());
55      }
56  
57      /**
58       * Creates a testing license wit the specified id and matcher.
59       * @param id the ID to use
60       * @param matcher the matcher to execute.
61       */
62      public TestingLicense(String family, String id, IHeaderMatcher matcher) {
63          this(id, matcher, ILicenseFamily.builder().setLicenseFamilyCategory(family)
64                  .setLicenseFamilyName("TestingLicense: " + family).build());
65      }
66  
67      /**
68       * Creates a testing license with the specified matcher and family.
69       * @param id the license id
70       * @param matcher the matcher to use.
71       * @param family the family for this license.
72       */
73      public TestingLicense(String id, IHeaderMatcher matcher, ILicenseFamily family) {
74          this.family = family;
75          this.matcher = matcher;
76          this.note = null;
77          this.id = id;
78          this.name = id;
79      }
80  
81      /**
82       * Gets the family from the license
83       * @return the license family.
84       */
85      public ILicenseFamily getFamily() {
86          return family;
87      }
88  
89      /**
90       * Gets the matcher from the license
91       * @return the matcher.
92       */
93      @Override
94      public IHeaderMatcher getMatcher() {
95          return matcher;
96      }
97  
98      /**
99       * Sets the name from value for this license.
100      * @param name the name of this license.
101      */
102     public void setName(String name) {
103         this.name = name;
104     }
105 
106     @Override
107     public String getId() {
108         return id;
109     }
110 
111     @Override
112     public void reset() {
113         matcher.reset();
114     }
115 
116     @Override
117     public boolean matches(IHeaders line) {
118         return matcher.matches(line);
119     }
120 
121     @Override
122     public ILicenseFamily getLicenseFamily() {
123         return family;
124     }
125 
126     @Override
127     public String getNote() {
128         return note;
129     }
130 
131     @Override
132     public String getName() {
133         return StringUtils.defaultIfBlank(name, family.getFamilyName());
134     }
135 
136     @Override
137     public boolean equals(Object o) {
138         return ILicense.equals(this, o);
139     }
140 
141     @Override
142     public int hashCode() {
143         return ILicense.hash(this);
144     }
145 
146     @Override
147     public String toString() {
148         return String.format("%s[id='%s', family='%s']", name, id, family);
149     }
150 }