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;
20  
21  import static org.assertj.core.api.Assertions.assertThat;
22  import static org.assertj.core.api.Assertions.fail;
23  import static org.junit.jupiter.api.Assertions.assertEquals;
24  import static org.junit.jupiter.api.Assertions.assertFalse;
25  import static org.junit.jupiter.api.Assertions.assertThrows;
26  import static org.junit.jupiter.api.Assertions.assertTrue;
27  import static org.mockito.Mockito.mock;
28  import static org.mockito.Mockito.when;
29  
30  import java.io.BufferedReader;
31  import java.io.ByteArrayOutputStream;
32  import java.io.File;
33  import java.io.FilenameFilter;
34  import java.io.IOException;
35  import java.io.InputStream;
36  import java.io.InputStreamReader;
37  import java.io.OutputStream;
38  import java.io.PrintWriter;
39  import java.net.URISyntaxException;
40  import java.net.URL;
41  import java.util.ArrayList;
42  import java.util.Arrays;
43  import java.util.List;
44  import java.util.SortedSet;
45  import java.util.function.Function;
46  
47  import org.apache.commons.io.filefilter.AndFileFilter;
48  import org.apache.commons.io.filefilter.DirectoryFileFilter;
49  import org.apache.commons.io.filefilter.FalseFileFilter;
50  import org.apache.commons.io.function.IOSupplier;
51  import org.apache.rat.ReportConfiguration.NoCloseOutputStream;
52  import org.apache.rat.analysis.IHeaderMatcher;
53  import org.apache.rat.config.AddLicenseHeaders;
54  import org.apache.rat.configuration.ConfigurationReaderTest;
55  import org.apache.rat.license.ILicense;
56  import org.apache.rat.license.ILicenseFamily;
57  import org.apache.rat.license.LicenseSetFactory.LicenseFilter;
58  import org.apache.rat.report.IReportable;
59  import org.apache.rat.testhelpers.TestingLicense;
60  import org.apache.rat.utils.Log;
61  import org.apache.rat.utils.Log.Level;
62  import org.apache.rat.utils.ReportingSet.Options;
63  import org.apache.rat.walker.NameBasedHiddenFileFilter;
64  import org.junit.jupiter.api.BeforeEach;
65  import org.junit.jupiter.api.Test;
66  import org.mockito.Mockito;
67  
68  public class ReportConfigurationTest {
69  
70      private ReportConfiguration underTest;
71      private LoggingCapture log;
72  
73      @BeforeEach
74      public void setup() {
75          log = new LoggingCapture();
76          underTest = new ReportConfiguration(log);
77      }
78  
79      @Test
80      public void testAddAndRemoveApproveLicenseCategories() {
81          List<String> expected = new ArrayList<>();
82          underTest.addLicense( new TestingLicense("Unapproved"));
83  
84          assertThat(underTest.getApprovedLicenseCategories()).isEmpty();
85  
86          TestingLicense license = new TestingLicense("TheCat");
87          underTest.addLicense(license);
88          underTest.addApprovedLicenseCategory(license.getFamily());
89          expected.add("TheCa");
90          SortedSet<String> result = underTest.getApprovedLicenseCategories();
91          assertThat(expected).hasSize(result.size()).containsAll(result);
92          SortedSet<ILicenseFamily> families = underTest.getLicenseFamilies(LicenseFilter.approved);
93          assertThat(expected).hasSize(families.size());
94          SortedSet<ILicense> licenses = underTest.getLicenses(LicenseFilter.approved);
95          assertThat(expected).hasSize(licenses.size());
96  
97          underTest.addLicense(new TestingLicense("ACat"));
98          underTest.addApprovedLicenseCategory("ACat");
99          expected.add("ACat ");
100         result = underTest.getApprovedLicenseCategories();
101         assertThat(expected).hasSize(result.size()).containsAll(result);
102         families = underTest.getLicenseFamilies(LicenseFilter.approved);
103         assertThat(expected).hasSize(families.size());
104         licenses = underTest.getLicenses(LicenseFilter.approved);
105         assertThat(expected).hasSize(licenses.size());
106 
107         String[] cats = { "Spot ", "Felix" };
108         underTest.addLicense(new TestingLicense("Spot"));
109         underTest.addLicense(new TestingLicense("Felix"));
110         underTest.addApprovedLicenseCategories(Arrays.asList(cats));
111         expected.addAll(Arrays.asList(cats));
112         result = underTest.getApprovedLicenseCategories();
113         assertThat(expected).hasSize(result.size()).containsAll(result);
114         families = underTest.getLicenseFamilies(LicenseFilter.approved);
115         assertThat(expected).hasSize(families.size());
116         licenses = underTest.getLicenses(LicenseFilter.approved);
117         assertThat(expected).hasSize(licenses.size());
118         
119         underTest.removeApprovedLicenseCategory("Spot ");
120         expected.remove("Spot ");
121         result = underTest.getApprovedLicenseCategories();
122         assertThat(expected).hasSize(result.size()).containsAll(result);
123         families = underTest.getLicenseFamilies(LicenseFilter.approved);
124         assertThat(expected).hasSize(families.size());
125         licenses = underTest.getLicenses(LicenseFilter.approved);
126         assertThat(expected).hasSize(licenses.size());
127 
128         cats[0] = "TheCa";
129         underTest.removeApprovedLicenseCategories(Arrays.asList(cats));
130         expected.removeAll(Arrays.asList(cats));
131         result = underTest.getApprovedLicenseCategories();
132         assertThat(expected).hasSize(result.size()).containsAll(result);
133         families = underTest.getLicenseFamilies(LicenseFilter.approved);
134         assertThat(expected).hasSize(families.size());
135         licenses = underTest.getLicenses(LicenseFilter.approved);
136         assertThat(expected).hasSize(licenses.size());    }
137 
138     @Test
139     public void testRemoveBeforeAddApproveLicenseCategories() {
140         underTest.addLicense( new TestingLicense("TheCat"));
141         assertThat(underTest.getApprovedLicenseCategories()).isEmpty();
142         assertThat(underTest.getLicenseFamilies(LicenseFilter.approved)).isEmpty();
143         assertThat(underTest.getLicenses(LicenseFilter.approved)).isEmpty();
144         
145         underTest.removeApprovedLicenseCategory("TheCat");
146         assertThat(underTest.getApprovedLicenseCategories()).isEmpty();
147         assertThat(underTest.getLicenseFamilies(LicenseFilter.approved)).isEmpty();
148         assertThat(underTest.getLicenses(LicenseFilter.approved)).isEmpty();
149 
150         underTest.addApprovedLicenseCategory("TheCat");
151         assertThat(underTest.getApprovedLicenseCategories()).isEmpty();
152         assertThat(underTest.getLicenseFamilies(LicenseFilter.approved)).isEmpty();
153         assertThat(underTest.getLicenses(LicenseFilter.approved)).isEmpty();
154     }
155 
156     private ILicense testingLicense(String category, String name) {
157         ILicenseFamily family = ILicenseFamily.builder().setLicenseFamilyCategory(category).setLicenseFamilyName(name)
158                 .build();
159         return new TestingLicense( family );
160     }
161 
162     @Test
163     public void testAddLicense() {
164 
165         List<ILicense> expected = new ArrayList<>();
166         assertThat(underTest.getLicenses(LicenseFilter.all)).isEmpty();
167 
168         ILicense lic1 = testingLicense("TheCat", "TheName");
169         expected.add(lic1);
170         underTest.addLicense(lic1);
171         SortedSet<ILicense> result = underTest.getLicenses(LicenseFilter.all);
172         assertThat(expected).hasSize(result.size()).containsAll(result);
173 
174         ILicense[] lics = { testingLicense("Spot", "Data's cat"), testingLicense("Felix", "Cartoon cat") };
175         expected.addAll(Arrays.asList(lics));
176         underTest.addLicenses(Arrays.asList(lics));
177         result = underTest.getLicenses(LicenseFilter.all);
178         assertThat(expected).hasSize(result.size()).containsAll(result);
179     }
180 
181     @Test
182     public void copyrightMessageTest() {
183         assertThat(underTest.getCopyrightMessage()).isNull();
184         underTest.setCopyrightMessage("This is the message");
185         assertThat(underTest.getCopyrightMessage()).isEqualTo("This is the message");
186     }
187 
188     @Test
189     public void inputFileFilterTest() {
190         FilenameFilter filter = mock(FilenameFilter.class);
191         assertThat(underTest.getInputFileFilter()).isNull();
192         underTest.setInputFileFilter(filter);
193         assertThat(underTest.getInputFileFilter()).isEqualTo(filter);
194     }
195 
196     @Test
197     public void directoryFilterTest() {
198         assertThat(underTest.getDirectoryFilter()).isNotNull();
199         assertThat(underTest.getDirectoryFilter()).isExactlyInstanceOf(NameBasedHiddenFileFilter.class);
200 
201         underTest.setDirectoryFilter(DirectoryFileFilter.DIRECTORY);
202         underTest.addDirectoryFilter(NameBasedHiddenFileFilter.HIDDEN);
203         assertThat(underTest.getDirectoryFilter()).isExactlyInstanceOf(AndFileFilter.class);
204 
205         underTest.setDirectoryFilter(null);
206         assertThat(underTest.getDirectoryFilter()).isExactlyInstanceOf(FalseFileFilter.class);
207     }
208 
209     @Test
210     public void licenseFamiliesTest() {
211         assertThat(underTest.getLicenseFamilies(LicenseFilter.all)).isEmpty();
212         assertThat(underTest.getLicenseFamilies(LicenseFilter.approved)).isEmpty();
213         assertThat(underTest.getLicenseFamilies(LicenseFilter.none)).isEmpty();
214 
215         ILicense[] lics = { testingLicense("TheCat", "TheName"), testingLicense("Spot", "Data's cat"),
216                 testingLicense("Felix", "Cartoon cat") };
217         underTest.addLicenses(Arrays.asList(lics));
218 
219         assertThat(underTest.getLicenseFamilies(LicenseFilter.all)).hasSize(lics.length);
220         assertThat(underTest.getLicenseFamilies(LicenseFilter.approved)).isEmpty();
221         assertThat(underTest.getLicenseFamilies(LicenseFilter.none)).isEmpty();
222 
223         underTest.addApprovedLicenseCategory(lics[1].getLicenseFamily());
224         assertThat(underTest.getLicenseFamilies(LicenseFilter.all)).hasSize(lics.length);
225         SortedSet<ILicenseFamily> result = underTest.getLicenseFamilies(LicenseFilter.approved);
226         assertThat(result).hasSize(1);
227         assertThat(result.first()).isEqualTo(lics[1].getLicenseFamily());
228         assertThat(underTest.getLicenseFamilies(LicenseFilter.none)).isEmpty();
229     }
230 
231     @Test
232     public void licensesTest() {
233         assertThat(underTest.getLicenses(LicenseFilter.all)).isEmpty();
234         assertThat(underTest.getLicenses(LicenseFilter.approved)).isEmpty();
235         assertThat(underTest.getLicenses(LicenseFilter.none)).isEmpty();
236 
237         ILicense[] lics = { testingLicense("TheCat", "TheName"), testingLicense("Spot", "Data's cat"),
238                 testingLicense("Felix", "Cartoon cat") };
239         underTest.addLicenses(Arrays.asList(lics));
240 
241         assertThat(underTest.getLicenses(LicenseFilter.all)).hasSize(lics.length);
242         assertThat(underTest.getLicenses(LicenseFilter.approved)).isEmpty();
243         assertThat(underTest.getLicenses(LicenseFilter.none)).isEmpty();
244 
245         underTest.addApprovedLicenseCategory(lics[1].getLicenseFamily());
246         assertThat(underTest.getLicenses(LicenseFilter.all)).hasSize(lics.length);
247         SortedSet<ILicense> result = underTest.getLicenses(LicenseFilter.approved);
248         assertThat(result).hasSize(1);
249         assertThat(result.first()).isEqualTo(lics[1]);
250         assertThat(underTest.getLicenseFamilies(LicenseFilter.none)).isEmpty();
251     }
252 
253     @Test
254     public void outputTest() throws IOException {
255         assertThat(underTest.getOutput().get()).isExactlyInstanceOf(NoCloseOutputStream.class);
256         assertThat(underTest.getWriter()).isNotNull();
257 
258         ByteArrayOutputStream stream = new ByteArrayOutputStream();
259         underTest.setOut(() -> stream);
260         assertThat(underTest.getOutput().get()).isEqualTo(stream);
261         PrintWriter writer = underTest.getWriter().get();
262         assertThat(writer).isNotNull();
263         writer.write('a');
264         writer.flush();
265         assertThat(stream.toByteArray()[0]).isEqualTo((byte) 'a');
266     }
267 
268     @Test
269     public void reportableTest() {
270         assertThat(underTest.getReportable()).isNull();
271         IReportable reportable = mock(IReportable.class);
272         underTest.setReportable(reportable);
273         assertThat(underTest.getReportable()).isEqualTo(reportable);
274         underTest.setReportable(null);
275         assertThat(underTest.getReportable()).isNull();
276     }
277 
278     @Test
279     public void stylesheetTest() throws IOException, URISyntaxException {
280         URL url = this.getClass().getResource("ReportConfigurationTestFile");
281 
282         assertThat(underTest.getStyleSheet()).isNull();
283         InputStream stream = mock(InputStream.class);
284         underTest.setStyleSheet(() -> stream);
285         assertThat(underTest.getStyleSheet().get()).isEqualTo(stream);
286         IOSupplier<InputStream> sup = null;
287         underTest.setStyleSheet(sup);
288         assertThat(underTest.getStyleSheet()).isNull();
289         
290         File file = mock(File.class);
291         when(file.toURI()).thenReturn(url.toURI());
292         underTest.setStyleSheet(file);
293         BufferedReader d = new BufferedReader(new InputStreamReader(underTest.getStyleSheet().get()));
294         assertThat(d.readLine()).isEqualTo("/*");
295         assertThat(d.readLine()).isEqualTo(" * Licensed to the Apache Software Foundation (ASF) under one   *");
296     }
297 
298     @Test
299     public void testFlags() {
300         assertThat(underTest.isAddingLicenses()).isFalse();
301         assertThat(underTest.isAddingLicensesForced()).isFalse();
302         assertThat(underTest.isStyleReport()).isTrue();
303 
304         underTest.setAddLicenseHeaders(AddLicenseHeaders.TRUE);
305         assertThat(underTest.isAddingLicenses()).isTrue();
306         assertThat(underTest.isAddingLicensesForced()).isFalse();
307         assertThat(underTest.isStyleReport()).isTrue();
308 
309         underTest.setAddLicenseHeaders(AddLicenseHeaders.FALSE);
310         assertThat(underTest.isAddingLicenses()).isFalse();
311         assertThat(underTest.isAddingLicensesForced()).isFalse();
312         assertThat(underTest.isStyleReport()).isTrue();
313 
314         underTest.setAddLicenseHeaders(AddLicenseHeaders.FORCED);
315         assertThat(underTest.isAddingLicenses()).isTrue();
316         assertThat(underTest.isAddingLicensesForced()).isTrue();
317         assertThat(underTest.isStyleReport()).isTrue();
318 
319         underTest.setAddLicenseHeaders(AddLicenseHeaders.FALSE);
320         underTest.setStyleReport(false);
321         assertThat(underTest.isAddingLicenses()).isFalse();
322         assertThat(underTest.isAddingLicensesForced()).isFalse();
323         assertThat(underTest.isStyleReport()).isFalse();
324 
325         underTest.setStyleReport(true);
326         assertThat(underTest.isAddingLicenses()).isFalse();
327         assertThat(underTest.isAddingLicensesForced()).isFalse();
328         assertThat(underTest.isStyleReport()).isTrue();
329     }
330 
331     @Test
332     public void testValidate() {
333         final StringBuilder sb = new StringBuilder();
334         try {
335             underTest.validate(s -> sb.append(s));
336             fail("should have thrown ConfigurationException");
337         } catch (ConfigurationException e) {
338             assertThat(e.getMessage()).isEqualTo("Reportable may not be null");
339             assertThat(sb.length()).isEqualTo(0);
340         }
341 
342         underTest.setReportable(mock(IReportable.class));
343         try {
344             underTest.validate(s -> sb.append(s));
345             fail("should have thrown ConfigurationException");
346         } catch (ConfigurationException e) {
347             assertThat(e.getMessage()).isEqualTo("You must specify at least one license");
348             assertThat(sb.length()).isEqualTo(0);
349         }
350 
351         underTest.addLicense(testingLicense("valid", "Validation testing license"));
352         try {
353             underTest.validate(s -> sb.append(s));
354             fail("should have thrown ConfigurationException");
355         } catch (ConfigurationException e) {
356             assertThat(e.getMessage()).isEqualTo("Stylesheet must be specified if report styling is selected");
357             assertThat(sb.length()).isEqualTo(0);
358         }
359 
360         underTest.setStyleSheet(()->mock(InputStream.class));
361         underTest.setStyleReport(false);
362         underTest.validate(s -> sb.append(s));
363         assertThat(sb.toString()).isEqualTo("Ignoring stylesheet because styling is not selected");
364 
365         final StringBuilder sb2 = new StringBuilder();
366         underTest.setStyleReport(true);
367         underTest.validate(s -> sb2.append(s));
368         assertThat(sb2.length()).isEqualTo(0);
369     }
370     
371     @Test
372     public void testSetOut() throws IOException {
373         ReportConfiguration config = new ReportConfiguration(log);
374         try (OutputStreamIntercepter osi = new OutputStreamIntercepter()) {
375 			config.setOut(() -> osi);
376 			assertThat(osi.closeCount).isEqualTo(0);
377 			try (OutputStream os = config.getOutput().get()) {
378 			    assertThat(osi.closeCount).isEqualTo(0);
379 			}
380 			assertThat(osi.closeCount).isEqualTo(1);
381 			try (OutputStream os = config.getOutput().get()) {
382 			    assertThat(osi.closeCount).isEqualTo(1);
383 			}
384 			assertThat(osi.closeCount).isEqualTo(2);
385 		}
386     }
387     
388     @Test
389     public void logFamilyCollisionTest() {
390         // setup
391         underTest.addFamily(ILicenseFamily.builder().setLicenseFamilyCategory("CAT").setLicenseFamilyName("name"));
392         assertFalse(log.captured.toString().contains("CAT"));
393        
394         // verify default collision logs WARNING
395         underTest.addFamily(ILicenseFamily.builder().setLicenseFamilyCategory("CAT").setLicenseFamilyName("name2"));
396         assertTrue(log.captured.toString().contains("WARN"), ()->"default value not WARN");
397         assertTrue(log.captured.toString().contains("CAT"), ()->"'CAT' not found");
398         
399         // verify level setting works.
400         for (Level l : Level.values()) {
401         log.clear();
402         underTest.logFamilyCollisions(l);
403         underTest.addFamily(ILicenseFamily.builder().setLicenseFamilyCategory("CAT").setLicenseFamilyName("name2"));
404         assertTrue(log.captured.toString().contains("CAT"), ()->"'CAT' not found");
405         assertTrue(log.captured.toString().contains(l.name()), ()->"logging not set to "+l);
406         }
407 
408     }
409     
410     @Test
411     public void familyDuplicateOptionsTest() {
412         underTest.addFamily(ILicenseFamily.builder().setLicenseFamilyCategory("CAT").setLicenseFamilyName("name"));
413         assertFalse(log.captured.toString().contains("CAT"));
414         
415         // verify default second setting ignores change
416         underTest.addFamily(ILicenseFamily.builder().setLicenseFamilyCategory("CAT").setLicenseFamilyName("name2"));
417         assertTrue(log.captured.toString().contains("CAT"));
418         assertEquals("name", underTest.getLicenseFamilies(LicenseFilter.all).stream()
419                 .filter(s -> s.getFamilyCategory().equals("CAT  ")).map(s -> s.getFamilyName()).findFirst().get());
420         
421         underTest.familyDuplicateOption(Options.OVERWRITE);
422         // verify second setting ignores change
423         underTest.addFamily(ILicenseFamily.builder().setLicenseFamilyCategory("CAT").setLicenseFamilyName("name2"));
424         assertTrue(log.captured.toString().contains("CAT"));
425         assertEquals("name2", underTest.getLicenseFamilies(LicenseFilter.all).stream()
426                 .filter(s -> s.getFamilyCategory().equals("CAT  ")).map(s -> s.getFamilyName()).findFirst().get());
427 
428         // verify fail throws exception
429         underTest.familyDuplicateOption(Options.FAIL);
430         assertThrows( IllegalArgumentException.class, ()->underTest.addFamily(ILicenseFamily.builder().setLicenseFamilyCategory("CAT").setLicenseFamilyName("name2")));
431   
432         underTest.familyDuplicateOption(Options.IGNORE);
433     }
434     
435     
436     @Test
437     public void logLicenseCollisionTest() {
438         // setup
439         ILicenseFamily family = ILicenseFamily.builder().setLicenseFamilyCategory("CAT").setLicenseFamilyName("family name").build();
440         IHeaderMatcher matcher = Mockito.mock(IHeaderMatcher.class);
441         when(matcher.getId()).thenReturn("Macher ID");
442         underTest.addFamily(family);
443         underTest.addLicense(ILicense.builder().setId("ID").setName("license name").setLicenseFamilyCategory(family.getFamilyCategory())
444                 .setMatcher( matcher ).build(underTest.getLicenseFamilies(LicenseFilter.all)));
445         
446         // verify default collistion logs WARN
447         underTest.addLicense(ILicense.builder().setId("ID").setName("license name2").setLicenseFamilyCategory(family.getFamilyCategory())
448                 .setMatcher( matcher ).build(underTest.getLicenseFamilies(LicenseFilter.all)));
449         assertTrue(log.captured.toString().contains("WARN"));
450         
451         log.clear();
452         underTest.logLicenseCollisions(Level.ERROR);
453         
454         // verify second setting changes logs issue
455         underTest.addLicense(ILicense.builder().setId("ID").setName("license name2").setLicenseFamilyCategory(family.getFamilyCategory())
456                 .setMatcher( matcher ).build(underTest.getLicenseFamilies(LicenseFilter.all)));
457         assertTrue(log.captured.toString().contains("ERROR"));
458 
459     }
460     
461     @Test
462     public void licenseDuplicateOptionsTest() {
463         // setup
464         ILicenseFamily family = ILicenseFamily.builder().setLicenseFamilyCategory("CAT").setLicenseFamilyName("family name").build();
465         IHeaderMatcher matcher = Mockito.mock(IHeaderMatcher.class);
466         when(matcher.getId()).thenReturn("Macher ID");
467         underTest.addFamily(family);
468         Function<String,ILicense> makeLicense = s -> ILicense.builder().setId("ID").setName(s).setLicenseFamilyCategory(family.getFamilyCategory())
469                 .setMatcher( matcher ).build(underTest.getLicenseFamilies(LicenseFilter.all));
470                 
471         underTest.addLicense(makeLicense.apply("license name"));
472         
473         // verify default second setting ignores change
474         underTest.addLicense(makeLicense.apply("license name2"));
475         assertTrue(log.captured.toString().contains("WARN"));
476         assertEquals("license name",
477                 underTest.getLicenses(LicenseFilter.all).stream().map(ILicense::getName).findFirst().get());
478         
479         underTest.licenseDuplicateOption(Options.OVERWRITE);
480         underTest.addLicense(makeLicense.apply("license name2"));
481         assertEquals("license name2",
482                 underTest.getLicenses(LicenseFilter.all).stream().map(ILicense::getName).findFirst().get());
483         
484          
485         // verify fail throws exception
486         underTest.licenseDuplicateOption(Options.FAIL);
487         assertThrows( IllegalArgumentException.class, ()-> underTest.addLicense(makeLicense.apply("another name")));
488 
489     }
490     
491     /**
492      * Validates that the configuration contains the default approved licenses.
493      * @param config The configuration to test.
494      */
495     public static void validateDefaultApprovedLicenses(ReportConfiguration config) {
496         validateDefaultApprovedLicenses(config, 0);
497     }
498     
499     /**
500      * Validates that the configuration contains the default approved licenses.
501      * @param config The configuration to test.
502      */
503     public static void validateDefaultApprovedLicenses(ReportConfiguration config, int additionalIdCount) {
504         assertThat(config.getApprovedLicenseCategories()).hasSize(ConfigurationReaderTest.EXPECTED_IDS.length + additionalIdCount);
505         for (String s : ConfigurationReaderTest.EXPECTED_IDS) {
506             assertThat(config.getApprovedLicenseCategories()).contains(ILicenseFamily.makeCategory(s));
507         }
508     }
509     
510 
511     /**
512      * Validates that the configruation contains the default license families.
513      * @param config the configuration to test.
514      */
515     public static void validateDefaultLicenseFamilies(ReportConfiguration config, String...additionalIds) {
516         assertThat(config.getLicenseFamilies(LicenseFilter.all)).hasSize(ConfigurationReaderTest.EXPECTED_IDS.length + additionalIds.length);
517         List<String> expected = new ArrayList<>();
518         expected.addAll(Arrays.asList(ConfigurationReaderTest.EXPECTED_IDS));
519         expected.addAll(Arrays.asList(additionalIds));
520         for (ILicenseFamily family : config.getLicenseFamilies(LicenseFilter.all)) {
521             assertThat(expected).contains(family.getFamilyCategory().trim());
522         }
523     }
524 
525     /**
526      * Validates that the configuration contains the default licenses.
527      * @param config the configuration to test.
528      */
529     public static void validateDefaultLicenses(ReportConfiguration config, String...additionalLicenses) {
530         assertThat(config.getLicenses(LicenseFilter.all)).hasSize(ConfigurationReaderTest.EXPECTED_LICENSES.length + additionalLicenses.length);
531         List<String> expected = new ArrayList<>();
532         expected.addAll(Arrays.asList(ConfigurationReaderTest.EXPECTED_LICENSES));
533         expected.addAll(Arrays.asList(additionalLicenses));
534         for (ILicense license : config.getLicenses(LicenseFilter.all)) {
535             assertThat(expected).contains(license.getId());
536         }
537     }
538     
539     /**
540      * Validates that the configuration matches the default.
541      * @param config The configuration to test.
542      */
543     public static void validateDefault(ReportConfiguration config) {
544         assertThat(config.isAddingLicenses()).isFalse();
545         assertThat(config.isAddingLicensesForced()).isFalse();
546         assertThat(config.getCopyrightMessage()).isNull();
547         assertThat(config.getInputFileFilter()).isNull();
548         assertThat(config.isStyleReport()).isTrue();
549         assertThat(config.getStyleSheet()).isNotNull().withFailMessage("Stylesheet should not be null");
550         assertThat(config.getDirectoryFilter()).isNotNull().withFailMessage("Directory filter should not be null");
551         assertThat(config.getDirectoryFilter()).isExactlyInstanceOf(NameBasedHiddenFileFilter.class);
552         
553         validateDefaultApprovedLicenses(config);
554         validateDefaultLicenseFamilies(config);
555         validateDefaultLicenses(config);
556     }
557     
558     private class LoggingCapture implements Log {
559 
560         StringBuilder captured = new StringBuilder();
561         
562         public void clear() {
563             captured = new StringBuilder();
564         }
565         
566         @Override
567         public void log(Level level, String msg) {
568             captured.append( String.format("%s: %s%n", level, msg));
569         }
570         
571     }
572     
573     static class OutputStreamIntercepter extends OutputStream {
574         
575         int closeCount = 0;
576 
577         @Override
578         public void write(int arg0) throws IOException {
579             throw new UnsupportedOperationException();
580         }
581         
582         @Override
583         public void close() {
584             ++closeCount;
585         }
586     }
587 }