[java-identity-provider] branch dev/JPAR-175 updated: JPAR-175 more tinkering

Rod Widdowson rdw at steadingsoftware.com
Sat Jun 19 14:41:22 UTC 2021


This is an automated email from the git hooks/post-receive script.

rdw pushed a commit to branch dev/JPAR-175
in repository java-identity-provider.

View the commit online:
http://git.shibboleth.net/view/?p=java-identity-provider.git;a=commit;h=05f347207990a6befc2c69f8fa0ba04b0959eccb

The following commit(s) were added to refs/heads/dev/JPAR-175 by this push:
       new  05f347207 JPAR-175 more tinkering
05f347207 is described below

commit 05f347207990a6befc2c69f8fa0ba04b0959eccb
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Sat Jun 19 15:21:50 2021 +0100

    JPAR-175 more tinkering
    
    Source the jars from the idp-war-distribution project
    
    Add the idp-war-distribution project run time dependencies
    to the expected jar files.
    
    Add special cases for weird versions (1.2.3-a-random-string-that-i-just-made-up)
    
    Add special case for orphan artifacts (ones which have no apparant version declaration
    like "jul-to-slf4j".
    
    https://issues.shibboleth.net/jira/browse/JPAR-175
---
 .../idp/dependencies/DependencyTest.java           |  77 ++++++++------
 .../net/shibboleth/idp/dependencies/ParsedPom.java | 115 ++++++++++++++++-----
 2 files changed, 130 insertions(+), 62 deletions(-)

diff --git a/idp-installer/src/test/java/net/shibboleth/idp/dependencies/DependencyTest.java b/idp-installer/src/test/java/net/shibboleth/idp/dependencies/DependencyTest.java
index 83cb83a3a..76df4ea42 100644
--- a/idp-installer/src/test/java/net/shibboleth/idp/dependencies/DependencyTest.java
+++ b/idp-installer/src/test/java/net/shibboleth/idp/dependencies/DependencyTest.java
@@ -49,6 +49,7 @@ import org.apache.maven.shared.invoker.Invoker;
 import org.apache.maven.shared.invoker.MavenInvocationException;
 import org.opensaml.core.testing.OpenSAMLInitBaseTestCase;
 import org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport;
+import org.testng.SkipException;
 import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
@@ -65,14 +66,14 @@ import net.shibboleth.utilities.java.support.xml.XMLParserException;
 public class DependencyTest extends OpenSAMLInitBaseTestCase {
 
     /** Set this up if you want to run the tests from eclipse. */
-    private static String LOCAL_MAVEN_HOME = "C:/Program Files (x86)/apache-maven-3.6.1";
-    
+    private static String LOCAL_MAVEN_HOME = null;
+
+    /** A list of things which get added to real versions. */
+    private static final List<String> extensionGarnish = List.of("-SNAPSHOT", "-GA", "-jre", "-empty-to-avoid-conflict-with-guava");
+
     /** Parse for us to use. */
     private ParserPool parserPool;
     
-    /** If this is set to false we do not do any work which requires maven (which is everything). */
-    private boolean mavenAvailable;
-    
     /** Work space.  Deleted on exit. */
     private Path workingDir;
     
@@ -85,7 +86,6 @@ public class DependencyTest extends OpenSAMLInitBaseTestCase {
     private PrintWriter report;
 
     private PomArtifact parentArtefact;
-
     
     /**  We have as an assumption that the CWD is idp-installer.  Test this.
      * @throws IOException
@@ -94,7 +94,7 @@ public class DependencyTest extends OpenSAMLInitBaseTestCase {
         final Path path = Path.of(".");
         final String myPath = path.toFile().getCanonicalPath();
         final String indirectPath = path.resolve("..").resolve("idp-installer").toFile().getCanonicalPath();
-        
+
         assertTrue(path.resolve("..").resolve("idp-war").toFile().exists());
         assertEquals(myPath, indirectPath);
     }
@@ -105,7 +105,6 @@ public class DependencyTest extends OpenSAMLInitBaseTestCase {
      */
     @BeforeClass public void setupMavenEnvironment() {
         if (System.getProperty("maven.home") != null) {
-            mavenAvailable = true;
             return;
         }
         String home = LOCAL_MAVEN_HOME;
@@ -115,11 +114,11 @@ public class DependencyTest extends OpenSAMLInitBaseTestCase {
         if (home == null) {
             home = System.getenv("_");
         }
-
-        if (home != null) {
-            System.setProperty("maven.home", home);
-            mavenAvailable = true;
+        if (home == null) {
+            throw new SkipException("Maven Not Located");
         }
+
+        System.setProperty("maven.home", home);
     }
     
     /** Set up the environment for running the test.
@@ -132,30 +131,38 @@ public class DependencyTest extends OpenSAMLInitBaseTestCase {
      */
 
     @BeforeClass(dependsOnMethods = {"setupMavenEnvironment", "testWorkingDir"}) public void setup() throws IOException, XMLParserException, MavenInvocationException {
-        if (!mavenAvailable) {
-            return;
-        }
         workingDir = Files.createTempDirectory("dependencyTest");
         parserPool = XMLObjectProviderRegistrySupport.getParserPool();
-        ParsedPom idpParent = new ParsedPom(parserPool, Path.of("../idp-parent/pom.xml"), null);
+        ParsedPom idpParent = new ParsedPom(parserPool, Path.of("../idp-parent/pom.xml"), "idp-parent/pom.xml", null, Collections.emptyList());
         idpArtefact = idpParent.getOurInfo();
         parentArtefact = idpParent.getParent(); 
         assertNotNull(parentArtefact);
         final Path parentPath = downloadPom(parentArtefact);
-        final ParsedPom projectParent = new ParsedPom(parserPool, parentPath, new Properties());
-        idpParent = new ParsedPom(parserPool, Path.of("../idp-parent/pom.xml"), projectParent.getProperties());
+        final ParsedPom projectParent = new ParsedPom(parserPool, parentPath, "parent/pom.xml", new Properties(), Collections.emptyList());
+        idpParent = new ParsedPom(parserPool, Path.of("../idp-parent/pom.xml"), "idp-parent/pom.xml", projectParent.getProperties(), projectParent.getCompileDependencies());
         dependencies.addAll(projectParent.getCompileDependencies());
         dependencies.addAll(idpParent.getCompileDependencies());
         for (final PomArtifact bom : projectParent.getBomDependencies()) {
             final Path bomPath = downloadPom(bom);
-            final ParsedPom bomContents = new ParsedPom(parserPool, bomPath, projectParent.getProperties());
+            final ParsedPom bomContents = new ParsedPom(parserPool, bomPath, bom.getArtifactId()+".pom", projectParent.getProperties(), projectParent.getCompileDependencies());
             dependencies.addAll(bomContents.getCompileDependencies());
         }
         for (final PomArtifact bom : idpParent.getBomDependencies()) {
             final Path bomPath = downloadPom(bom);
-            final ParsedPom bomContents = new ParsedPom(parserPool, bomPath, projectParent.getProperties());
+            final ParsedPom bomContents = new ParsedPom(parserPool, bomPath, bom.getArtifactId()+".pom", projectParent.getProperties(), projectParent.getCompileDependencies());
             dependencies.addAll(bomContents.getCompileDependencies());
         }
+        final Set<PomArtifact> allDeps = new HashSet<>(projectParent.getCompileDependencies().size() +
+                idpParent.getCompileDependencies().size() +
+                projectParent.getRuntimeDependencies().size() +
+                idpParent.getRuntimeDependencies().size() 
+                );
+        allDeps.addAll(projectParent.getCompileDependencies());
+        allDeps.addAll(idpParent.getCompileDependencies());
+        allDeps.addAll(projectParent.getRuntimeDependencies());
+        allDeps.addAll(idpParent.getRuntimeDependencies());
+        final ParsedPom warDist = new ParsedPom(parserPool, Path.of("../idp-war-distribution/pom.xml"), "idp-war-distribution/pom.xml", projectParent.getProperties(), allDeps);
+        dependencies.addAll(warDist.getRuntimeDependencies());
         final File out = new File("target/dependencyReport.txt");
         final FileOutputStream outStream = new FileOutputStream(out);
         report = new PrintWriter(new BufferedOutputStream(outStream));
@@ -172,10 +179,7 @@ public class DependencyTest extends OpenSAMLInitBaseTestCase {
      * @throws MavenInvocationException if we fail to download a pom or a dependency
      */
     @Test public void testDependencies() throws IOException, MavenInvocationException {
-        if (!mavenAvailable) {
-            return;
-        }
-        final Path lib = Path.of("../idp-war/target/idp-war-"+ idpArtefact.getVersion()).resolve("WEB-INF").resolve("lib");
+        final Path lib = Path.of("../idp-war-distribution/target/idp-war-distribution-"+ idpArtefact.getVersion()).resolve("WEB-INF").resolve("lib");
         assertTrue(Files.exists(lib), "idp-war must have been built");
         final Map<String, String> names = new HashMap<>();
         int wrongVersion = 0;
@@ -205,8 +209,10 @@ public class DependencyTest extends OpenSAMLInitBaseTestCase {
                 analyzeChild(dependencySource, artifact);
             } else {
                 report.format("%-22s\t: %-12s\tVersion Mismatch- found %s %s\n", id, ver, version, sourcePomFilename);
-                analyzeChild(dependencySource, artifact);
-                wrongVersion++;
+                analyzeChild(dependencySource, artifact.withVersion(version));
+                if (!ver.equals(PomArtifact.BAD_VERSION)) {
+                    wrongVersion++;
+                }
             }
             last = artifact;
         }
@@ -216,9 +222,9 @@ public class DependencyTest extends OpenSAMLInitBaseTestCase {
         if (similarNames != 0) {
             report.format("\n%d Artifacts with multiple versions\n", similarNames);
         }
-        
+
         report.format("\n%d dependencies, %d found, %d not found, %d mismatched\n\nDependency Sources\n", dependencies.size(), found, nonUsed, wrongVersion);
-        
+
         final List<String> contributedDeps = new ArrayList<>(names.keySet());
         Collections.sort(contributedDeps);
         int noSource = 0, verMismatch = 0;
@@ -293,7 +299,9 @@ public class DependencyTest extends OpenSAMLInitBaseTestCase {
 
         Invoker invoker = new DefaultInvoker();
         invoker.execute( request );
-        Files.list(outputDir).forEach(e -> addDep(dependencySource, outputDir.relativize(e).toString(), artifact));
+        if (Files.exists(outputDir)) {
+            Files.list(outputDir).forEach(e -> addDep(dependencySource, outputDir.relativize(e).toString(), artifact));
+        }
     }
 
     /** Add the artifact as a source of this file.
@@ -391,11 +399,12 @@ public class DependencyTest extends OpenSAMLInitBaseTestCase {
         } else {
             name = inName;
         }
-        final int last;
-        if (name.endsWith("-SNAPSHOT")) {
-            last = name.substring(0, name.length()-9).lastIndexOf("-");
-        } else {
-            last = name.lastIndexOf("-");
+        int last = name.lastIndexOf("-");
+        for (String otherGarnish : extensionGarnish) {
+            if (name.endsWith(otherGarnish)) {
+                last = name.substring(0, name.length()-otherGarnish.length()).lastIndexOf("-");
+                break;
+            }
         }
         final String base = name.substring(0, last);
         String versionExtension = name.substring(last+1);
diff --git a/idp-installer/src/test/java/net/shibboleth/idp/dependencies/ParsedPom.java b/idp-installer/src/test/java/net/shibboleth/idp/dependencies/ParsedPom.java
index 9e276be42..e2ef70836 100644
--- a/idp-installer/src/test/java/net/shibboleth/idp/dependencies/ParsedPom.java
+++ b/idp-installer/src/test/java/net/shibboleth/idp/dependencies/ParsedPom.java
@@ -24,10 +24,15 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.nio.file.Path;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
+import java.util.Objects;
 import java.util.Properties;
 import java.util.Set;
+import java.util.function.Function;
+import java.util.stream.Collectors;
 
 import javax.annotation.Nonnull;
 
@@ -48,12 +53,21 @@ import net.shibboleth.utilities.java.support.xml.XMLParserException;
  *
  */
 public class ParsedPom extends OpenSAMLInitBaseTestCase{
-
+    
     /** Compile dependencies - what we care about. */
     private final List<PomArtifact> compileDependencies = new ArrayList<>();
     
     /** BOM dependencies. */
     private final List<PomArtifact> bomDependencies = new ArrayList<>();
+
+    /** Rumtime dependencies. */
+    private final List<PomArtifact> runtimeDependencies = new ArrayList<>();    
+
+    /** Inherits dependencies. */
+    private final Map<String, PomArtifact> managedDependencies;
+    
+    /** Which the POM.*/
+    @Nonnull private final String sourcePomInfo;
     
     /** Properties. */
     private final Properties properties = new Properties();
@@ -69,17 +83,24 @@ public class ParsedPom extends OpenSAMLInitBaseTestCase{
      *
      * @param parsers a short-cut to let us parse XML
      * @param pom the {@link Path} to the pom.
+     * @param pomName an ID for the pom
      * @param parentPomProperties if present it is properties from the parent (which might be empty), if null we are *only*
      * looking for the parent pom coordinates.
+     * @param managed Managed dependencies from parent
      * @throws IOException from parsing
      * @throws FileNotFoundException if the file doesn't exist
      * @throws XMLParserException from parsing
      */
     public ParsedPom(@Nonnull final ParserPool parsers,
                      @Nonnull final Path pom,
-                     @Nullable final Properties parentPomProperties)
+                     @Nonnull final String pomName,
+                     @Nullable final Properties parentPomProperties, 
+                     @Nonnull final Collection<PomArtifact> managed)
             throws FileNotFoundException, IOException, XMLParserException {
 
+        managedDependencies = managed.stream().collect(Collectors.toMap(e->e.getGroupId()+"+"+e.getArtifactId(), Function.identity()));
+                
+        sourcePomInfo = pomName;
         Document document;
         try (final InputStream stream = new BufferedInputStream(new FileInputStream(pom.toFile()))) {
             document = parsers.parse(stream);
@@ -92,10 +113,10 @@ public class ParsedPom extends OpenSAMLInitBaseTestCase{
         final List<Element> par = ElementSupport.getChildElementsByTagName(el, "parent");
         
         if (!par.isEmpty()) {
-            parseParent(par.get(0), pom.getFileName().toString());
+            parseParent(par.get(0));
         }
 
-        us = new PomArtifact(el, parent, pom.getFileName().toString());
+        us = new PomArtifact(el, parent);
 
         if (parentPomProperties == null) {
             return;
@@ -117,7 +138,11 @@ public class ParsedPom extends OpenSAMLInitBaseTestCase{
         final List<Element> dependencyMgt = ElementSupport.getChildElementsByTagName(el, "dependencyManagement");
         if (!dependencyMgt.isEmpty()) {
             final List<Element> dependencies = ElementSupport.getChildElementsByTagName(dependencyMgt.get(0), "dependencies");
-            parseDependencies(dependencies.get(0), pom.getFileName().toString());
+            parseDependencies(dependencies.get(0));
+        }
+        final List<Element> dependencies = ElementSupport.getChildElementsByTagName(el, "dependencies");
+        if (!dependencies.isEmpty()) {
+            parseDependencies(dependencies.get(0));
         }
     }
 
@@ -134,9 +159,8 @@ public class ParsedPom extends OpenSAMLInitBaseTestCase{
 
     /**
      * @param item
-     * @param pomSource the filename of the POM.
      */
-    private void parseDependencies(Element item, final String pomSource) {
+    private void parseDependencies(Element item) {
         final List<Element> dependencies = ElementSupport.getChildElementsByTagName(item, "dependency");
         
         for (Element dependency : dependencies) {
@@ -144,7 +168,7 @@ public class ParsedPom extends OpenSAMLInitBaseTestCase{
             if (!types.isEmpty()) {
                 final String type = StringSupport.trimOrNull(types.get(0).getTextContent());
                 if ("pom".equals(type)) {
-                    bomDependencies.add(new PomArtifact(dependency,pomSource));
+                    bomDependencies.add(new PomArtifact(dependency));
                     continue;
                 } else if (!"jar".equals(type)) {
                     // not for us
@@ -154,12 +178,16 @@ public class ParsedPom extends OpenSAMLInitBaseTestCase{
             final List<Element> scopes = ElementSupport.getChildElementsByTagName(dependency, "scope");
             if (!scopes.isEmpty()) {
                 final String scope = StringSupport.trimOrNull(scopes.get(0).getTextContent());
+                if ("runtime".equals(scope)) {
+                    runtimeDependencies.add(new PomArtifact(dependency));
+                    continue;
+                }
                 if (!"compile".equals(scope)) {
                     // not for us
                     continue;
                 }
             }
-            final PomArtifact dep = new PomArtifact(dependency,pomSource);
+            final PomArtifact dep = new PomArtifact(dependency);
             compileDependencies.add(dep);
         }
     }
@@ -178,10 +206,9 @@ public class ParsedPom extends OpenSAMLInitBaseTestCase{
 
     /**
      * @param item
-     * @param pomSource the filename of the POM this artifact is being extracted from.
      */
-    private void parseParent(Element item, final String pomSource) {
-        parent = new PomArtifact(item, pomSource);
+    private void parseParent(Element item) {
+        parent = new PomArtifact(item);
     }
     
     /**
@@ -197,6 +224,13 @@ public class ParsedPom extends OpenSAMLInitBaseTestCase{
     public List<PomArtifact> getBomDependencies() {
         return bomDependencies;
     }
+    
+    /**
+     * @return Returns the runtimeDependencies.
+     */
+    public List<PomArtifact> getRuntimeDependencies() {
+        return runtimeDependencies;
+    }
 
     /** Return our artifactInformation.
      * @return us.
@@ -221,10 +255,10 @@ public class ParsedPom extends OpenSAMLInitBaseTestCase{
 
     /** Encapsulation of a <dependency> element. */
     public class PomArtifact implements Comparable<PomArtifact>{
-        
-        /** Which physical POM this artifact is listed in.*/
-        @Nonnull private final String sourcePomFilename;
-        
+
+        /** What version to give if we cannot find the version. */
+        public final static String BAD_VERSION = "VERSION_NOT_DETERMINED"; 
+
         /** <groupId>.*/
         @Nonnull private final String groupId;
 
@@ -237,14 +271,26 @@ public class ParsedPom extends OpenSAMLInitBaseTestCase{
         /** <exclusions>. */
         @Nonnull private final Set<Pair<String, String>> exclusions = new HashSet<>();
 
+        /**
+         * Constructor.
+         *
+         * @param id 
+         * @param group
+         * @param ver
+         */
+        private PomArtifact(final String id, final String group, final String ver) {
+            artifactId = id;
+            groupId = group;
+            version = ver;
+        }
+        
         /**
          * Constructor.
          *
          * @param item element to interrogate.
-         * @param pomSource the filename of the POM this artifact is being extracted from.
          */
-        public PomArtifact(final Element item, final String pomSource) {
-            this(item, null, pomSource);
+        public PomArtifact(final Element item) {
+            this(item, null);
         }
 
         /**
@@ -252,13 +298,9 @@ public class ParsedPom extends OpenSAMLInitBaseTestCase{
          *
          * @param item element to interrogate.
          * @param parentArtifact to inherit from
-         * @param pomSource the filename of the POM this artifact is being extracted from.
          */
-        public PomArtifact(final Element item, final @Nullable PomArtifact parentArtifact, 
-                final String pomSource) {
+        public PomArtifact(final Element item, final @Nullable PomArtifact parentArtifact) {
             
-            sourcePomFilename = pomSource;
-
             final List<Element> grps  = ElementSupport.getChildElementsByTagName(item, "groupId");
             if (grps.size() > 0) {
                 groupId = getElementContent(grps.get(0));
@@ -279,8 +321,12 @@ public class ParsedPom extends OpenSAMLInitBaseTestCase{
             } else if (parentArtifact != null) {
                 version = parentArtifact.getVersion();
             } else {
-                Constraint.isGreaterThan(0, vers.size(), "<version> should exist in dependency");
-                version = null;
+                final PomArtifact inherited = managedDependencies.get(groupId+"+"+artifactId);
+                if (inherited != null) {
+                    version = inherited.getVersion();
+                } else {
+                    version = BAD_VERSION;
+                }
             }
             
             List<Element> excls  = ElementSupport.getChildElementsByTagName(item, "exclusions"); 
@@ -323,9 +369,9 @@ public class ParsedPom extends OpenSAMLInitBaseTestCase{
          * @return the pom source.
          */
         public String getSourcePomFilename() {
-            return sourcePomFilename;
+            return sourcePomInfo;
         }
-
+        
         /**
          * @return Returns the exclusions.
          */
@@ -348,5 +394,18 @@ public class ParsedPom extends OpenSAMLInitBaseTestCase{
             }
             return false;
         }
+        
+        /** {@inheritDoc} */
+        public int hashCode() {
+            return Objects.hash(artifactId, groupId, version);
+        }
+
+        /** return the same artifact but with an amended version.
+         * @param ver the version
+         * @return an amended artifact.
+         */
+        public PomArtifact withVersion(String ver) {
+            return new PomArtifact(artifactId, groupId, ver);
+        }
     }
 }

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list