Skip to content

Commit

Permalink
RAT-98: converted to assertj (#438)
Browse files Browse the repository at this point in the history
* converted to assertj

* Disabled gitignore tests

---------

Co-authored-by: P. Ottlinger <[email protected]>
  • Loading branch information
Claudenw and ottlinger authored Feb 7, 2025
1 parent cd9aeaf commit 5066aa0
Show file tree
Hide file tree
Showing 22 changed files with 270 additions and 280 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
package org.apache.rat;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -89,12 +86,15 @@ public void cleanup() {

@Test
public void testAddIncludedFilter() {
DocumentName dirName = DocumentName.builder(tempDir).build();
underTest.addExcludedFilter(DirectoryFileFilter.INSTANCE);
DocumentNameMatcher exlcuder = underTest.getDocumentExcluder(DocumentName.builder(new File(File.separator)).build());
assertEquals("not(DirectoryFileFilter)", exlcuder.toString());
assertFalse(exlcuder.matches(DocumentName.builder(tempDir).build()));
DocumentNameMatcher excluder = underTest.getDocumentExcluder(dirName);

assertThat(excluder.toString()).isEqualTo("not(DirectoryFileFilter)");
assertThat(excluder.matches(DocumentName.builder(tempDir).build())).isFalse();

File f = new File(tempDir, "foo.txt");
assertTrue(exlcuder.matches(DocumentName.builder(f).build()));
assertThat(excluder.matches(DocumentName.builder(f).build())).isTrue();
}

@Test
Expand All @@ -103,17 +103,16 @@ public void testAddFamilies() {
ILicenseFamily fam2 = ILicenseFamily.builder().setLicenseFamilyCategory("BAR").setLicenseFamilyName("big and round").build();
underTest.addFamilies(Arrays.asList(fam1, fam2));
SortedSet<String> result = underTest.getLicenseIds(LicenseFilter.ALL);
assertTrue(result.contains(ILicenseFamily.makeCategory("FOO")), "Missing FOO");
assertTrue(result.contains(ILicenseFamily.makeCategory("BAR")), "Missing BAR");
assertEquals(2, result.size());
assertThat(result).contains(ILicenseFamily.makeCategory("FOO"));
assertThat(result).contains(ILicenseFamily.makeCategory("BAR"));
assertThat(result).hasSize(2);
}

@Test
public void testAddApprovedLicenseId() {
underTest.addApprovedLicenseId("FOO");
SortedSet<String> result = underTest.getLicenseIds(LicenseFilter.APPROVED);
assertTrue(result.contains("FOO"));
assertEquals(1, result.size());
assertThat(result).hasSize(1).contains("FOO");
}
@Test
public void testAddAndRemoveApproveLicenseCategories() {
Expand Down Expand Up @@ -366,27 +365,28 @@ DocumentName mkDocumentName(File f) {
@Test
public void exclusionTest() {
DocumentName baseDir = DocumentName.builder(tempDir).build();
assertTrue(underTest.getDocumentExcluder(baseDir).matches(mkDocumentName(new File(tempDir,"foo"))));
assertTrue(underTest.getDocumentExcluder(baseDir).matches(mkDocumentName(new File("foo"))));
DocumentName foo = mkDocumentName(new File(tempDir,"foo"));
assertThat(underTest.getDocumentExcluder(baseDir).matches(foo)).isTrue();

underTest.setFrom(Defaults.builder().build());

File f = new File(tempDir, ".hiddenDir");
assertTrue(f.mkdir(), () -> "Could not create directory " + f);

assertFalse(underTest.getDocumentExcluder(baseDir).matches(mkDocumentName(new File(tempDir, ".hiddenDir"))));
assertThat(f.mkdir()).as(() -> "Could not create directory " + f).isTrue();
DocumentName hiddenDir = mkDocumentName(new File(tempDir, ".hiddenDir"));
DocumentNameMatcher excluder = underTest.getDocumentExcluder(baseDir);
assertThat(excluder.matches(hiddenDir)).isFalse();

underTest.addIncludedCollection(StandardCollection.HIDDEN_DIR);
assertTrue(underTest.getDocumentExcluder(baseDir).matches(mkDocumentName(new File(tempDir, ".hiddenDir"))));
assertThat(underTest.getDocumentExcluder(baseDir).matches(hiddenDir)).isTrue();

underTest.addExcludedCollection(StandardCollection.HIDDEN_DIR);
assertTrue(underTest.getDocumentExcluder(baseDir).matches(mkDocumentName(new File(tempDir, ".hiddenDir"))));
assertThat(underTest.getDocumentExcluder(baseDir).matches(hiddenDir)).isTrue();

underTest.addExcludedFilter(DirectoryFileFilter.DIRECTORY);

File file = new File(tempDir, "newDir");
assertTrue(file.mkdirs(), () -> "Could not create directory " + file);
assertFalse(underTest.getDocumentExcluder(baseDir).matches(mkDocumentName(file)));
assertThat(file.mkdirs()).as(() -> "Could not create directory " + file).isTrue();
assertThat(underTest.getDocumentExcluder(baseDir).matches(mkDocumentName(file))).isFalse();
}

@Test
Expand Down Expand Up @@ -471,8 +471,8 @@ public void reportableTest() {
IReportable reportable = mock(IReportable.class);
underTest.addSource(reportable);
assertThat(underTest.hasSource()).isTrue();
Exception thrown = assertThrows(ConfigurationException.class, () -> underTest.addSource((IReportable)null));
assertThat(thrown.getMessage()).contains("Reportable may not be null.");
assertThatThrownBy(() -> underTest.addSource((IReportable)null)).isExactlyInstanceOf(ConfigurationException.class)
.hasMessageContaining("Reportable may not be null.");
}

@Test
Expand Down Expand Up @@ -517,18 +517,17 @@ public void testFlags() {
public void testValidate() {
final StringBuilder sb = new StringBuilder();
String msg = "At least one source must be specified";
Exception thrown = assertThrows(ConfigurationException.class,
() -> underTest.validate(sb::append));
assertThat(thrown.getMessage()).isEqualTo(msg);
assertThatThrownBy(() -> underTest.validate(sb::append)).isExactlyInstanceOf(ConfigurationException.class)
.hasMessageContaining(msg);
assertThat(sb.toString()).isEqualTo(msg);


sb.setLength(0);
msg = "You must specify at least one license";
underTest.addSource(mock(IReportable.class));
thrown = assertThrows(ConfigurationException.class,
() -> underTest.validate(sb::append));
assertThat(thrown.getMessage()).isEqualTo(msg);

assertThatThrownBy(() -> underTest.validate(sb::append)).isExactlyInstanceOf(ConfigurationException.class)
.hasMessageContaining(msg);
assertThat(sb.toString()).isEqualTo(msg);

sb.setLength(0);
Expand All @@ -544,10 +543,12 @@ public void testSetOut() throws IOException {
config.setOut(() -> osi);
assertThat(osi.closeCount).isEqualTo(0);
try (OutputStream os = config.getOutput().get()) {
assertThat(os).isNotNull();
assertThat(osi.closeCount).isEqualTo(0);
}
assertThat(osi.closeCount).isEqualTo(1);
try (OutputStream os = config.getOutput().get()) {
assertThat(os).isNotNull();
assertThat(osi.closeCount).isEqualTo(1);
}
assertThat(osi.closeCount).isEqualTo(2);
Expand All @@ -558,45 +559,48 @@ public void testSetOut() throws IOException {
public void logFamilyCollisionTest() {
// setup
underTest.addFamily(ILicenseFamily.builder().setLicenseFamilyCategory("CAT").setLicenseFamilyName("name"));
assertFalse(log.getCaptured().contains("CAT"));
assertThat(log.getCaptured()).doesNotContain("CAT");

// verify default collision logs WARNING
underTest.addFamily(ILicenseFamily.builder().setLicenseFamilyCategory("CAT").setLicenseFamilyName("name2"));
assertTrue(log.getCaptured().contains("WARN"), "default value not WARN");
assertTrue(log.getCaptured().contains("CAT"), "'CAT' not found");
assertThat(log.getCaptured().contains("WARN")).as("default value not WARN").isTrue();
assertThat(log.getCaptured().contains("CAT")).as("'CAT' not found").isTrue();

// verify level setting works.
for (Level l : Level.values()) {
log.clear();
underTest.logFamilyCollisions(l);
underTest.addFamily(ILicenseFamily.builder().setLicenseFamilyCategory("CAT").setLicenseFamilyName("name2"));
assertTrue(log.getCaptured().contains("CAT"), "'CAT' not found");
assertTrue(log.getCaptured().contains(l.name()), "logging not set to "+l);
assertThat(log.getCaptured().contains("CAT")).as("'CAT' not found").isTrue();
assertThat(log.getCaptured().contains(l.name())).as("logging not set to "+l).isTrue();
}
}

@Test
public void familyDuplicateOptionsTest() {
underTest.addFamily(ILicenseFamily.builder().setLicenseFamilyCategory("CAT").setLicenseFamilyName("name"));
assertFalse(log.getCaptured().contains("CAT"));
assertThat(log.getCaptured()).doesNotContain("CAT");

// verify default second setting ignores change
underTest.addFamily(ILicenseFamily.builder().setLicenseFamilyCategory("CAT").setLicenseFamilyName("name2"));
assertTrue(log.getCaptured().contains("CAT"));
assertEquals("name", underTest.getLicenseFamilies(LicenseFilter.ALL).stream()
.filter(s -> s.getFamilyCategory().equals("CAT ")).map(ILicenseFamily::getFamilyName).findFirst().get());
assertThat(log.getCaptured()).contains("CAT");
assertThat(underTest.getLicenseFamilies(LicenseFilter.ALL).stream()
.filter(s -> s.getFamilyCategory().equals("CAT ")).map(ILicenseFamily::getFamilyName).findFirst())
.contains("name");

underTest.familyDuplicateOption(Options.OVERWRITE);
// verify second setting ignores change
underTest.addFamily(ILicenseFamily.builder().setLicenseFamilyCategory("CAT").setLicenseFamilyName("name2"));
assertTrue(log.getCaptured().contains("CAT"));
assertEquals("name2", underTest.getLicenseFamilies(LicenseFilter.ALL).stream()
.filter(s -> s.getFamilyCategory().equals("CAT ")).map(ILicenseFamily::getFamilyName).findFirst().get());
assertThat(log.getCaptured()).contains("CAT");
assertThat(underTest.getLicenseFamilies(LicenseFilter.ALL).stream()
.filter(s -> s.getFamilyCategory().equals("CAT ")).map(ILicenseFamily::getFamilyName).findFirst())
.contains("name2");

// verify fail throws exception
underTest.familyDuplicateOption(Options.FAIL);
assertThrows( IllegalArgumentException.class, ()->underTest.addFamily(ILicenseFamily.builder().setLicenseFamilyCategory("CAT").setLicenseFamilyName("name2")));

assertThatThrownBy(()->underTest.addFamily(ILicenseFamily.builder().setLicenseFamilyCategory("CAT").setLicenseFamilyName("name2")))
.isExactlyInstanceOf(IllegalArgumentException.class);

underTest.familyDuplicateOption(Options.IGNORE);
}

Expand All @@ -616,7 +620,7 @@ public void logLicenseCollisionTest() {
underTest.addLicense(ILicense.builder().setId("ID").setName("license name2").setFamily(family.getFamilyCategory())
.setMatcher( matcher ).setLicenseFamilies(underTest.getLicenseFamilies(LicenseFilter.ALL))
.build());
assertTrue(log.getCaptured().contains("WARN"));
assertThat(log.getCaptured()).contains("WARN");

log.clear();
underTest.logLicenseCollisions(Level.ERROR);
Expand All @@ -625,7 +629,7 @@ public void logLicenseCollisionTest() {
underTest.addLicense(ILicense.builder().setId("ID").setName("license name2").setFamily(family.getFamilyCategory())
.setMatcher( matcher ).setLicenseFamilies(underTest.getLicenseFamilies(LicenseFilter.ALL))
.build());
assertTrue(log.getCaptured().contains("ERROR"));
assertThat(log.getCaptured()).contains("ERROR");
}

@Test
Expand All @@ -643,18 +647,19 @@ public void licenseDuplicateOptionsTest() {

// verify default second setting ignores change
underTest.addLicense(makeLicense.apply("license name2"));
assertTrue(log.getCaptured().contains("WARN"));
assertEquals("license name",
underTest.getLicenses(LicenseFilter.ALL).stream().map(ILicense::getName).findFirst().get());
assertThat(log.getCaptured()).contains("WARN");
assertThat(underTest.getLicenses(LicenseFilter.ALL).stream().map(ILicense::getName).findFirst())
.contains("license name");

underTest.licenseDuplicateOption(Options.OVERWRITE);
underTest.addLicense(makeLicense.apply("license name2"));
assertEquals("license name2",
underTest.getLicenses(LicenseFilter.ALL).stream().map(ILicense::getName).findFirst().get());
assertThat(underTest.getLicenses(LicenseFilter.ALL).stream().map(ILicense::getName).findFirst())
.contains("license name2");

// verify fail throws exception
underTest.licenseDuplicateOption(Options.FAIL);
assertThrows( IllegalArgumentException.class, ()-> underTest.addLicense(makeLicense.apply("another name")));
assertThatThrownBy(()-> underTest.addLicense(makeLicense.apply("another name")))
.isExactlyInstanceOf(IllegalArgumentException.class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,50 @@
*/
package org.apache.rat.document;

import java.nio.file.Path;
import org.apache.commons.io.FileUtils;
import org.apache.rat.api.Document;
import org.apache.rat.test.utils.Resources;
import org.assertj.core.util.Files;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
import java.io.File;
import java.io.Reader;
import org.junit.jupiter.api.io.TempDir;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.assertj.core.api.Assertions.assertThat;

public class FileDocumentTest {
private Document document;
private File file;


@TempDir
private static Path tempDir;

@BeforeEach
public void setUp() throws Exception {
File basedir = new File(Files.currentFolder(), Resources.SRC_TEST_RESOURCES);
file = Resources.getExampleResource("exampleData/Source.java");
document = new FileDocument(DocumentName.builder(basedir).build(), file, DocumentNameMatcher.MATCHES_ALL);

File basedir = new File(tempDir.toFile(), Resources.SRC_TEST_RESOURCES);
basedir.mkdirs();
File sourceData = Resources.getExampleResource("exampleData/Source.java");
File file = new File(basedir, "Source.java");
FileUtils.copyFile(sourceData, file);
assertThat(file).exists();

DocumentName docName = DocumentName.builder(basedir).build();
document = new FileDocument(docName, file, DocumentNameMatcher.MATCHES_ALL);
}

@Test
public void reader() throws Exception {
Reader reader = document.reader();
assertNotNull(reader, "Reader should be returned");
assertEquals("package elements;",
new BufferedReader(reader).readLine(), "First file line expected");
assertThat(reader).isNotNull();
assertThat(new BufferedReader(reader).readLine()).isEqualTo("package elements;");
}

@Test
public void getName() {
final DocumentName name = document.getName();
assertNotNull(name, "Name is set");
assertThat(name).isNotNull();
}
}
Loading

0 comments on commit 5066aa0

Please sign in to comment.