From 6cff59c5bd95ccc30ca1c0968f3b90e165e0cfd5 Mon Sep 17 00:00:00 2001 From: Tom Page Date: Tue, 14 Dec 2021 16:34:13 +0000 Subject: [PATCH] Add test to check for duplicate libraries in Search Services. --- .../org/alfresco/ThirdPartyLicensesIT.java | 88 ++++++++++++++----- 1 file changed, 65 insertions(+), 23 deletions(-) diff --git a/search-services/packaging/src/test/java/org/alfresco/ThirdPartyLicensesIT.java b/search-services/packaging/src/test/java/org/alfresco/ThirdPartyLicensesIT.java index 17bb08092..929e585be 100644 --- a/search-services/packaging/src/test/java/org/alfresco/ThirdPartyLicensesIT.java +++ b/search-services/packaging/src/test/java/org/alfresco/ThirdPartyLicensesIT.java @@ -31,14 +31,18 @@ import static java.util.stream.Collectors.toSet; import static org.junit.Assert.fail; +import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Collection; import java.util.List; import java.util.Set; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; +import com.google.common.collect.HashMultimap; +import com.google.common.collect.Multimap; import com.google.common.collect.Sets; import org.junit.Test; @@ -54,6 +58,8 @@ public class ThirdPartyLicensesIT private static final String SPRING_SURF_PREFIX = "spring-surf-"; /** Start of the name of the zip file. */ private static final String ALFRESCO_SEARCH_SERVICES = "alfresco-search-services"; + /** The path to the target directory. */ + private static final Path TARGET_PATH = Paths.get(ThirdPartyLicensesIT.class.getProtectionDomain().getCodeSource().getLocation().getPath(), ".."); /** * Test that the dependencies in notice.txt match the actual dependencies, to ensure we've included third party @@ -64,11 +70,8 @@ public class ThirdPartyLicensesIT @Test public void testLicensesDeclared() throws Exception { - // Get the path to the target directory. - Path targetPath = Paths.get(getClass().getProtectionDomain().getCodeSource().getLocation().getPath(), ".."); - // Try to find the zip file in the target directory. - Path zipPath = Files.find(targetPath, 1, + Path zipPath = Files.find(TARGET_PATH, 1, (path, attribute) -> path.getFileName().toString().startsWith(ALFRESCO_SEARCH_SERVICES + "-") && path.toString().endsWith(".zip")) .findFirst() @@ -88,25 +91,7 @@ public class ThirdPartyLicensesIT .filter(name -> !name.startsWith(SPRING_SURF_PREFIX)) .collect(toSet()); } - - // Get the dependencies referenced in notice.txt. - Path noticePath = Paths.get(targetPath.toString(), "classes", "licenses", "notice.txt"); - List lines = Files.readAllLines(noticePath); - // Skip the header, which is all lines before the first "=== License Type ===" line. - int headerSize = 0; - for (String line : lines) - { - if (line.startsWith("===")) - { - break; - } - headerSize++; - } - Set declared = lines.stream() - .skip(headerSize) - .filter(line -> !line.isEmpty() && !line.startsWith("===")) - .map(line -> line.split(" ")[0]) - .collect(toSet()); + Set declared = getJarsFromNoticeFile(); // If the two lists don't match then fail the test and provide information about what's wrong. if (!jars.equals(declared)) @@ -119,4 +104,61 @@ public class ThirdPartyLicensesIT + "Jars declared but not found: " + onlyInNotice); } } + + /** + * Get the dependencies referenced in notice.txt. + * + * @return The set of jar files. + * @throws IOException Unexpected + */ + private Set getJarsFromNoticeFile() throws IOException + { + Path noticePath = Paths.get(TARGET_PATH.toString(), "classes", "licenses", "notice.txt"); + List lines = Files.readAllLines(noticePath); + // Skip the header, which is all lines before the first "=== License Type ===" line. + int headerSize = 0; + for (String line : lines) + { + if (line.startsWith("===")) + { + break; + } + headerSize++; + } + return lines.stream() + .skip(headerSize) + .filter(line -> !line.isEmpty() && !line.startsWith("===")) + .map(line -> line.split(" ")[0]) + .collect(toSet()); + } + + /** Check that we don't have two different versions of the same artifact. */ + @Test + public void testNoDuplicateArtifacts() throws Exception + { + Set declared = getJarsFromNoticeFile(); + Multimap artifactToVersions = HashMultimap.create(); + for (String jar : declared) + { + String artifact = jar.replaceAll("^([^-]*)(-[^0-9][^-]*)*-[0-9].*$", "$1$2"); + String version = jar.replaceAll("^([^-]*)(-[^0-9][^-]*)*-([0-9].*).jar$", "$3"); + artifactToVersions.put(artifact, version); + } + + boolean pass = true; + for (String artifact : artifactToVersions.keySet()) + { + Collection versions = artifactToVersions.get(artifact); + if (versions.size() > 1) + { + System.out.println("There are multiple versions of " + artifact + ": " + versions.toString()); + pass = false; + } + } + + if (!pass) + { + fail("Found duplicate libraries in the notice.txt file."); + } + } }