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.data;
20  
21  import com.google.common.collect.ImmutableList;
22  import java.util.ArrayList;
23  import java.util.HashSet;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
27  import java.util.TreeMap;
28  import java.util.TreeSet;
29  import java.util.stream.Stream;
30  import org.apache.commons.cli.Option;
31  import org.apache.commons.lang3.tuple.ImmutablePair;
32  import org.apache.rat.commandline.Arg;
33  import org.apache.rat.ui.UIOptionCollection;
34  import org.apache.rat.ui.ArgumentTracker;
35  import org.apache.rat.utils.DefaultLog;
36  
37  /**
38   * Generates a list of TestData to test an implementation.
39   * The tests work by creating a Path Consumer to construct a directory under the test base directory and creating files and/or
40   * directories within that directory. A test validator is created to validate the expected results of the operation and a {@link TestData}
41   * object is created for each test.
42   * <br />
43   * Each {@code TestData} represents a single test of a command line option or set of options.
44   * <br />
45   * Use of this class ensures consistent testing across all available UIs.
46   */
47  public abstract class AbstractTestDataProvider {
48      /** The list of exclude arguments. */
49      static final String[] EXCLUDE_ARGS = {"*.foo", "%regex[[A-Z]\\.bar]", "justbaz"};
50      /** The list of include arguments. */
51      static final String[] INCLUDE_ARGS = {"B.bar", "justbaz"};
52      // Sonar suggests List.of(), but we need an Immutable list.
53      public static final ImmutableList<ImmutablePair<Option, String[]>> NO_OPTIONS = ImmutableList.of(ImmutablePair.nullPair()); // NOSONAR
54  
55      /**
56       * Generates a map of TestData indexed by the testName.
57       * @param optionCollection the collection of options for the UI under test.
58       * @return the map of testName to Test Data.
59       */
60      public final Map<String, TestData> getOptionTestMap(final UIOptionCollection<?> optionCollection) {
61          Map<String, TestData> map = new TreeMap<>();
62          for (TestData test : getOptionTests(optionCollection)) {
63              map.put(test.getTestName(), test);
64          }
65          return map;
66      }
67  
68      /**
69       * Generates a list of test data for Option testing.
70       * This is different from UI testing as this is to test
71       * that the command line is properly parsed into a configuration.
72       * @param optionCollection the collection of options for the UI under test.
73       * @return a set of TestData for the tests.
74       */
75      public final Set<TestData> getOptionTests(final UIOptionCollection<?> optionCollection) {
76          // the optionCollection establishes any changes to the Arg values.
77          List<Option> unsupportedOptions = new ArrayList<>(optionCollection.getUnsupportedOptions().getOptions());
78          Set<TestData> result = new TreeSet<>();
79          for (Arg arg : Arg.values()) {
80              if (!arg.isEmpty()) {
81                  Stream<Option> options = arg.group().getOptions().stream().filter(opt -> !unsupportedOptions.contains(opt));
82                  switch (arg) {
83                      case CONFIGURATION -> options.forEach(opt -> configTest(result, opt));
84                      case CONFIGURATION_NO_DEFAULTS -> options.forEach(opt -> configurationNoDefaultsTest(result, opt));
85                      case COUNTER_MIN -> options.forEach(opt -> counterMinTest(result, opt));
86                      case COUNTER_MAX -> options.forEach(opt -> counterMaxTest(result, opt));
87                      case DRY_RUN -> options.forEach(opt -> dryRunTest(result, opt));
88                      case EDIT_COPYRIGHT -> options.forEach(opt -> editCopyrightTest(result, opt));
89                      case EDIT_ADD -> options.forEach(opt -> editLicenseTest(result, opt));
90                      case EDIT_OVERWRITE -> options.forEach(opt -> editOverwriteTest(result, opt));
91                      case HELP_LICENSES -> options.forEach(opt -> helpLicenses(result, opt));
92                      case EXCLUDE -> options.forEach(opt -> inputExcludeTest(result, opt));
93                      case EXCLUDE_FILE -> options.forEach(opt -> inputExcludeFileTest(result, opt));
94                      case EXCLUDE_PARSE_SCM -> options.forEach(opt -> inputExcludeParsedScmTest(result, opt));
95                      case EXCLUDE_STD -> options.forEach(opt -> inputExcludeStdTest(result, opt));
96                      case EXCLUDE_SIZE -> options.forEach(opt -> inputExcludeSizeTest(result, opt));
97                      case INCLUDE -> options.forEach(opt -> inputIncludeTest(result, opt));
98                      case INCLUDE_FILE -> options.forEach(opt -> inputIncludeFileTest(result, opt));
99                      case INCLUDE_STD -> options.forEach(opt -> inputIncludeStdTest(result, opt));
100                     case SOURCE -> options.forEach(opt -> inputSourceTest(result, opt));
101                     case FAMILIES_APPROVED -> options.forEach(opt -> licenseFamiliesApprovedTest(result, opt));
102                     case FAMILIES_APPROVED_FILE -> options.forEach(opt -> licenseFamiliesApprovedFileTest(result, opt));
103                     case FAMILIES_DENIED -> options.forEach(opt -> licenseFamiliesDeniedTest(result, opt));
104                     case FAMILIES_DENIED_FILE -> options.forEach(opt -> licenseFamiliesDeniedFileTest(result, opt));
105                     case LICENSES_APPROVED -> options.forEach(opt -> licensesApprovedTest(result, opt));
106                     case LICENSES_APPROVED_FILE -> options.forEach(opt -> licensesApprovedFileTest(result, opt));
107                     case LICENSES_DENIED -> options.forEach(opt -> licensesDeniedTest(result, opt));
108                     case LICENSES_DENIED_FILE -> options.forEach(opt -> licensesDeniedFileTest(result, opt));
109                     case LOG_LEVEL -> options.forEach(opt -> logLevelTest(result, opt));
110                     case OUTPUT_ARCHIVE -> options.forEach(opt -> outputArchiveTest(result, opt));
111                     case OUTPUT_FAMILIES -> options.forEach(opt -> outputFamiliesTest(result, opt));
112                     case OUTPUT_FILE -> options.forEach(opt -> outputFileTest(result, opt));
113                     case OUTPUT_LICENSES -> options.forEach(opt -> outputLicensesTest(result, opt));
114                     case OUTPUT_STANDARD -> options.forEach(opt -> outputStandardTest(result, opt));
115                     case OUTPUT_STYLE -> options.forEach(opt -> outputStyleTest(result, opt));
116                     case DIR -> {/* do nothing */}
117                     default -> throw new IllegalStateException("Unrecognized Arg: " + arg.name());
118                 }
119             }
120         }
121         validate(result);
122         return result;
123     }
124 
125     private void validate(Set<TestData> result) {
126         Set<Option> options = new HashSet<>(Arg.getOptions().getOptions());
127         result.forEach(testData -> options.remove(testData.getOption()));
128         // TODO fix this once deprecated options are removed  NOSONAR
129         options.forEach(opt -> DefaultLog.getInstance().warn("Option " + ArgumentTracker.extractKey(opt) + " was not tested."));
130         //assertThat(options).describedAs("All options are not accounted for.").isEmpty(); // NOSONAR
131     }
132 
133     /**
134      * Execute an {@link Arg#EXCLUDE_FILE} tests.
135      * @param result the set of configured tests.
136      * @param option the specific option being tested.
137      */
138     protected abstract void inputExcludeFileTest(final Set<TestData> result, final Option option);
139 
140     /**
141      * Execute an {@link Arg#EXCLUDE} tests.
142      * @param result the set of configured tests.
143      * @param option the specific option being tested.
144      */
145     protected abstract void inputExcludeTest(final Set<TestData> result, final Option option);
146 
147     /**
148      * Execute an {@link Arg#EXCLUDE_STD} tests.
149      * @param result the set of configured tests.
150      * @param option the specific option being tested.
151      */
152     protected abstract void inputExcludeStdTest(final Set<TestData> result, final Option option);
153 
154     /**
155      * Execute an {@link Arg#EXCLUDE_PARSE_SCM} tests.
156      * @param result the set of configured tests.
157      * @param option the specific option being tested.
158      */
159     protected abstract void inputExcludeParsedScmTest(final Set<TestData> result, final Option option);
160 
161     /**
162      * Execute an {@link Arg#EXCLUDE_SIZE} tests.
163      * @param result the set of configured tests.
164      * @param option the specific option being tested.
165      */
166     protected abstract void inputExcludeSizeTest(final Set<TestData> result, final Option option);
167 
168     /**
169      * Execute an {@link Arg#INCLUDE_FILE} tests.
170      * @param result the set of configured tests.
171      * @param option the specific option being tested.
172      */
173     protected abstract void inputIncludeFileTest(final Set<TestData> result, final Option option);
174 
175     /**
176      * Execute an {@link Arg#INCLUDE} tests.
177      * @param result the set of configured tests.
178      * @param option the specific option being tested.
179      */
180     protected abstract void inputIncludeTest(final Set<TestData> result, final Option option);
181 
182     /**
183      * Execute an {@link Arg#INCLUDE_STD} tests.
184      * @param result the set of configured tests.
185      * @param option the specific option being tested.
186      */
187     protected abstract void inputIncludeStdTest(final Set<TestData> result, final Option option);
188 
189     /**
190      * Execute an {@link Arg#SOURCE} tests.
191      * @param result the set of configured tests.
192      * @param option the specific option being tested.
193      */
194     protected abstract void inputSourceTest(final Set<TestData> result, final Option option);
195 
196     /**
197      * Execute an {@link Arg#HELP_LICENSES} tests.
198      * @param result the set of configured tests.
199      * @param option the specific option being tested.
200      */
201     protected abstract void helpLicenses(final Set<TestData> result, final Option option);
202 
203     /**
204      * Execute an {@link Arg#LICENSES_APPROVED_FILE} tests.
205      * @param result the set of configured tests.
206      * @param option the specific option being tested.
207      */
208     protected abstract void licensesApprovedFileTest(final Set<TestData> result, final Option option);
209 
210     /**
211      * Execute an {@link Arg#LICENSES_APPROVED} tests.
212      * @param result the set of configured tests.
213      * @param option the specific option being tested.
214      */
215     protected abstract void licensesApprovedTest(final Set<TestData> result, final Option option);
216 
217     /**
218      * Execute an {@link Arg#LICENSES_DENIED} tests.
219      * @param result the set of configured tests.
220      * @param option the specific option being tested.
221      */
222     protected abstract void licensesDeniedTest(final Set<TestData> result, final Option option);
223 
224     /**
225      * Execute an {@link Arg#LICENSES_DENIED_FILE} tests.
226      * @param result the set of configured tests.
227      * @param option the specific option being tested.
228      */
229     protected abstract void licensesDeniedFileTest(final Set<TestData> result, final Option option);
230 
231     /**
232      * Execute an {@link Arg#FAMILIES_APPROVED_FILE} tests.
233      * @param result the set of configured tests.
234      * @param option the specific option being tested.
235      */
236     protected abstract void licenseFamiliesApprovedFileTest(final Set<TestData> result, final Option option);
237 
238     /**
239      * Execute an {@link Arg#FAMILIES_APPROVED} tests.
240      * @param result the set of configured tests.
241      * @param option the specific option being tested.
242      */
243     protected abstract void licenseFamiliesApprovedTest(final Set<TestData> result, final Option option);
244 
245     /**
246      * Execute an {@link Arg#FAMILIES_DENIED_FILE} tests.
247      * @param result the set of configured tests.
248      * @param option the specific option being tested.
249      */
250     protected abstract void licenseFamiliesDeniedFileTest(final Set<TestData> result, final Option option);
251 
252     /**
253      * Execute an {@link Arg#FAMILIES_DENIED} tests.
254      * @param result the set of configured tests.
255      * @param option the specific option being tested.
256      */
257     protected abstract void licenseFamiliesDeniedTest(final Set<TestData> result, final Option option);
258 
259     /**
260      * Execute an {@link Arg#COUNTER_MAX} tests.
261      * @param result the set of configured tests.
262      * @param option the specific option being tested.
263      */
264     protected abstract void counterMaxTest(final Set<TestData> result, final Option option);
265 
266     /**
267      * Execute an {@link Arg#COUNTER_MIN} tests.
268      * @param result the set of configured tests.
269      * @param option the specific option being tested.
270      */
271     protected abstract void counterMinTest(final Set<TestData> result, final Option option);
272 
273     /**
274      * Execute an {@link Arg#CONFIGURATION} tests.
275      * @param result the set of configured tests.
276      * @param option the specific option being tested.
277      */
278     protected abstract void configTest(final Set<TestData> result, final Option option);
279 
280     /**
281      * Execute an {@link Arg#CONFIGURATION_NO_DEFAULTS} tests.
282      * @param result the set of configured tests.
283      * @param option the specific option being tested.
284      */
285     protected abstract void configurationNoDefaultsTest(final Set<TestData> result, final Option option);
286 
287     /**
288      * Execute an {@link Arg#DRY_RUN} tests.
289      * @param result the set of configured tests.
290      * @param option the specific option being tested.
291      */
292     protected abstract void dryRunTest(final Set<TestData> result, final Option option);
293 
294     /**
295      * Execute an {@link Arg#EDIT_COPYRIGHT} tests.
296      * @param result the set of configured tests.
297      * @param option the specific option being tested.
298      */
299     protected abstract  void editCopyrightTest(final Set<TestData> result, final Option option);
300 
301     /**
302      * Execute an {@link Arg#EDIT_ADD} tests.
303      * @param result the set of configured tests.
304      * @param option the specific option being tested.
305      */
306     protected abstract void editLicenseTest(final Set<TestData> result, final Option option);
307 
308     /**
309      * Execute an {@link Arg#EDIT_OVERWRITE} tests.
310      * @param result the set of configured tests.
311      * @param option the specific option being tested.
312      */
313     protected abstract void editOverwriteTest(final Set<TestData> result, final Option option);
314 
315     /**
316      * Execute an {@link Arg#LOG_LEVEL} tests.
317      * @param result the set of configured tests.
318      * @param option the specific option being tested.
319      */
320     protected abstract void logLevelTest(final Set<TestData> result, final Option option);
321 
322     /**
323      * Execute an {@link Arg#OUTPUT_ARCHIVE} tests.
324      * @param result the set of configured tests.
325      * @param option the specific option being tested.
326      */
327     protected abstract void outputArchiveTest(final Set<TestData> result, final Option option);
328 
329     /**
330      * Execute an {@link Arg#OUTPUT_FAMILIES} tests.
331      * @param result the set of configured tests.
332      * @param option the specific option being tested.
333      */
334     protected abstract void outputFamiliesTest(final Set<TestData> result, final Option option);
335 
336     /**
337      * Execute an {@link Arg#OUTPUT_FILE} tests.
338      * @param result the set of configured tests.
339      * @param option the specific option being tested.
340      */
341     protected abstract void outputFileTest(final Set<TestData> result, final Option option);
342 
343     /**
344      * Execute an {@link Arg#OUTPUT_LICENSES} tests.
345      * @param result the set of configured tests.
346      * @param option the specific option being tested.
347      */
348     protected abstract void outputLicensesTest(final Set<TestData> result, final Option option);
349 
350     /**
351      * Execute an {@link Arg#OUTPUT_STANDARD} tests.
352      * @param result the set of configured tests.
353      * @param option the specific option being tested.
354      */
355     protected abstract void outputStandardTest(final Set<TestData> result, final Option option);
356 
357     /**
358      * Execute an {@link Arg#OUTPUT_STYLE} tests.
359      * @param result the set of configured tests.
360      * @param option the specific option being tested.
361      */
362     protected abstract void outputStyleTest(final Set<TestData> result, final Option option);
363 }