diff --git a/core/src/main/java/org/jboss/pnc/build/finder/core/DistributionAnalyzer.java b/core/src/main/java/org/jboss/pnc/build/finder/core/DistributionAnalyzer.java index 8f69dabc..9054a17d 100644 --- a/core/src/main/java/org/jboss/pnc/build/finder/core/DistributionAnalyzer.java +++ b/core/src/main/java/org/jboss/pnc/build/finder/core/DistributionAnalyzer.java @@ -662,7 +662,7 @@ private List addLicensesFromJar(FileObject jar, FileObject localFil licenseInfos = addLicensesFromPom(localFile, POM_XML); } else if (LicenseUtils.isManifestMfFileName(localFile)) { licenseInfos = addLicensesFromBundleLicense(localFile); - } else if (LicenseUtils.isLicenseFileName(localFile)) { + } else if (LicenseUtils.isLicenseFileName(localFile.getName().getBaseName())) { licenseInfos = addLicenseFromTextFile(jar, localFile); } else { licenseInfos = Collections.emptyList(); diff --git a/core/src/main/java/org/jboss/pnc/build/finder/core/LicenseUtils.java b/core/src/main/java/org/jboss/pnc/build/finder/core/LicenseUtils.java index 658bcda5..cfa0b35f 100644 --- a/core/src/main/java/org/jboss/pnc/build/finder/core/LicenseUtils.java +++ b/core/src/main/java/org/jboss/pnc/build/finder/core/LicenseUtils.java @@ -52,7 +52,6 @@ import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.vfs2.FileContent; -import org.apache.commons.vfs2.FileName; import org.apache.commons.vfs2.FileObject; import org.spdx.library.InvalidSPDXAnalysisException; import org.spdx.library.model.license.AnyLicenseInfo; @@ -114,7 +113,7 @@ public final class LicenseUtils { private static final String URL_MARKER = ":/"; - private static final List EXTENSIONS_TO_REMOVE = List.of(".html", ".php", ".txt"); + private static final List EXTENSIONS_TO_REMOVE = List.of(".html", ".md", ".php", ".txt"); private static final Pattern NAME_VERSION_PATTERN = Pattern .compile("(?[A-Z-a-z])[Vv]?(?[1-9]+)(\\.(?[0-9]+))?"); @@ -535,7 +534,8 @@ public static boolean isManifestMfFileName(FileObject fileObject) { } /** - * Returns whether the given file object is a license text file. Matches files such as + * Returns whether the given file name is a license text file. Matches files such as + * *
    *
  • LICENSE.md
  • *
  • LICENSE
  • @@ -544,25 +544,16 @@ public static boolean isManifestMfFileName(FileObject fileObject) { *
  • <SPDX-LICENSE-ID>.txt
  • *
* - * @param fileObject the file object - * @return whether the given file object is a license text file + * @param fileName the file name + * @return whether the given file name is a license text file */ - public static boolean isLicenseFileName(FileObject fileObject) { - FileName name = fileObject.getName(); - String fileName = name.getBaseName(); - + public static boolean isLicenseFileName(String fileName) { if (LICENSE_FILE_PATTERN.matcher(fileName).matches()) { return true; } - String extension = FilenameUtils.getExtension(fileName); - - if (StringUtils.equalsAny(extension, "", "txt")) { - String baseName = StringUtils.removeEnd(fileName, extension); - return LicenseUtils.isKnownLicenseId(baseName); - } - - return false; + String baseName = FilenameUtils.removeExtension(fileName); + return LicenseUtils.isKnownLicenseId(baseName); } /** diff --git a/core/src/test/java/org/jboss/pnc/build/finder/core/LicenseUtilsTest.java b/core/src/test/java/org/jboss/pnc/build/finder/core/LicenseUtilsTest.java index ea2206e4..20f5bc3a 100644 --- a/core/src/test/java/org/jboss/pnc/build/finder/core/LicenseUtilsTest.java +++ b/core/src/test/java/org/jboss/pnc/build/finder/core/LicenseUtilsTest.java @@ -44,6 +44,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; import org.spdx.library.InvalidSPDXAnalysisException; import org.spdx.library.model.license.InvalidLicenseStringException; import org.spdx.library.model.license.LicenseInfoFactory; @@ -319,4 +320,10 @@ void testGetMatchingLicense() throws IOException, InvalidSPDXAnalysisException { assertThat(LicenseUtils.getMatchingLicense(fo)).isEqualTo("Apache-2.0"); } } + + @ParameterizedTest + @ValueSource(strings = { "LICENSE", "LICENSE.md", "LICENSE.txt", "MIT-LICENSE", "MIT", "MIT.md", "MIT.txt" }) + void testIsLicenseFileName(String fileName) { + assertThat(LicenseUtils.isLicenseFileName(fileName)).isTrue(); + } }