This is an automated email from the ASF dual-hosted git repository.

bodewig pushed a commit to branch ivy-1642
in repository https://gitbox.apache.org/repos/asf/ant-ivy.git

commit f577ca989c47d4be2c289ecde72a6c85ea64c2d6
Author: Stefan Bodewig <[email protected]>
AuthorDate: Fri Apr 14 10:42:23 2023 +0200

    ensure default artifacts is added to correct configuration
    
    https://issues.apache.org/jira/browse/IVY-1642
---
 .../parser/m2/PomModuleDescriptorBuilder.java      |  6 ++-
 .../parser/m2/PomModuleDescriptorParserTest.java   | 57 ++++++++++++++++------
 ...st-dependencies-with-and-without-classifier.pom | 24 +++++++++
 3 files changed, 72 insertions(+), 15 deletions(-)

diff --git 
a/src/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorBuilder.java 
b/src/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorBuilder.java
index ba28af4e..3c6eff88 100644
--- a/src/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorBuilder.java
+++ b/src/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorBuilder.java
@@ -305,6 +305,8 @@ public class PomModuleDescriptorBuilder {
         // the same dependency mrid could appear twice in the module 
descriptor,
         // so we check if we already have created a dependency descriptor for 
the dependency mrid
         final DependencyDescriptor existing = 
this.ivyModuleDescriptor.depDescriptors.get(moduleRevId);
+        final String[] existingConfigurations = existing == null ? new 
String[0]
+            : existing.getModuleConfigurations();
         final DefaultDependencyDescriptor dd = (existing != null && existing 
instanceof DefaultDependencyDescriptor)
                 ? (DefaultDependencyDescriptor) existing
                 : new PomDependencyDescriptor(dep, ivyModuleDescriptor, 
moduleRevId, !excludeAllTransitiveDeps);
@@ -317,8 +319,10 @@ public class PomModuleDescriptorBuilder {
         final String optionalizedScope = dep.isOptional() ? "optional" : scope;
         if (isNonDefaultArtifact(dep)) {
             if (existing != null && 
existing.getAllDependencyArtifacts().length == 0) {
+                String moduleConfiguration = existingConfigurations.length == 1
+                    ? existingConfigurations[0] : optionalizedScope;
                 // previously added dependency has been the "default artifact"
-                dd.addDependencyArtifact(optionalizedScope, 
createDefaultArtifact(dd));
+                dd.addDependencyArtifact(moduleConfiguration, 
createDefaultArtifact(dd));
             }
             String type = "jar";
             if (dep.getType() != null) {
diff --git 
a/test/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorParserTest.java 
b/test/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorParserTest.java
index 310b21f5..a9673cc0 100644
--- 
a/test/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorParserTest.java
+++ 
b/test/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorParserTest.java
@@ -21,6 +21,7 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertSame;
 import static org.junit.Assert.fail;
 
 import java.io.File;
@@ -357,27 +358,55 @@ public class PomModuleDescriptorParserTest extends 
AbstractModuleDescriptorParse
 
         DependencyDescriptor[] dds = md.getDependencies();
         assertNotNull(dds);
-        assertEquals(3, dds.length);
+        assertEquals(5, dds.length);
+        Map<String, String> extraAtt = Collections.singletonMap("classifier", 
"asl");
         assertEquals(ModuleRevisionId.newInstance("commons-logging", 
"commons-logging", "1.0.4"),
             dds[0].getDependencyRevisionId());
-        Map<String, String> extraAtt = Collections.singletonMap("classifier", 
"asl");
-        assertEquals(2, dds[0].getAllDependencyArtifacts().length);
-        assertEquals(Collections.emptyMap(), 
dds[0].getAllDependencyArtifacts()[0].getExtraAttributes());
-        assertEquals(extraAtt, 
dds[0].getAllDependencyArtifacts()[1].getExtraAttributes());
+        DependencyArtifactDescriptor[] dads = 
dds[0].getAllDependencyArtifacts();
+        assertEquals(2, dads.length);
+        assertEquals(Collections.emptyMap(), dads[0].getExtraAttributes());
+        assertEquals(extraAtt, dads[1].getExtraAttributes());
 
         assertEquals(ModuleRevisionId.newInstance("commons-logging", 
"commons-logging2", "1.0.4"),
             dds[1].getDependencyRevisionId());
-
-        assertEquals(2, dds[1].getAllDependencyArtifacts().length);
-        assertEquals(Collections.emptyMap(), 
dds[1].getAllDependencyArtifacts()[1].getExtraAttributes());
-        assertEquals(extraAtt, 
dds[1].getAllDependencyArtifacts()[0].getExtraAttributes());
+        dads = dds[1].getAllDependencyArtifacts();
+        assertEquals(2, dads.length);
+        assertEquals(Collections.emptyMap(), dads[1].getExtraAttributes());
+        assertEquals(extraAtt, dads[0].getExtraAttributes());
 
         assertEquals(ModuleRevisionId.newInstance("commons-logging", 
"commons-logging3", "1.0.4"),
             dds[2].getDependencyRevisionId());
-        assertEquals(2, dds[2].getAllDependencyArtifacts().length);
-        assertEquals(extraAtt, 
dds[2].getAllDependencyArtifacts()[0].getExtraAttributes());
+        dads = dds[2].getAllDependencyArtifacts();
+        assertEquals(2, dads.length);
+        assertEquals(extraAtt, dads[0].getExtraAttributes());
         assertEquals(Collections.singletonMap("classifier", "foo"),
-                     
dds[2].getAllDependencyArtifacts()[1].getExtraAttributes());
+                     dads[1].getExtraAttributes());
+
+        assertEquals(ModuleRevisionId.newInstance("commons-logging", 
"commons-logging4", "1.0.4"),
+            dds[3].getDependencyRevisionId());
+        dads = dds[3].getAllDependencyArtifacts();
+        assertEquals(2, dads.length);
+        assertEquals(Collections.emptyMap(), dads[0].getExtraAttributes());
+        assertEquals(extraAtt, dads[1].getExtraAttributes());
+        DependencyArtifactDescriptor[] providedDads = 
dds[3].getDependencyArtifacts("provided");
+        assertEquals(1, providedDads.length);
+        assertSame(dads[0], providedDads[0]);
+        DependencyArtifactDescriptor[] compileDads = 
dds[3].getDependencyArtifacts("compile");
+        assertEquals(1, compileDads.length);
+        assertSame(dads[1], compileDads[0]);
+
+        assertEquals(ModuleRevisionId.newInstance("commons-logging", 
"commons-logging5", "1.0.4"),
+            dds[4].getDependencyRevisionId());
+        dads = dds[4].getAllDependencyArtifacts();
+        assertEquals(2, dads.length);
+        assertEquals(extraAtt, dads[0].getExtraAttributes());
+        assertEquals(Collections.emptyMap(), dads[1].getExtraAttributes());
+        providedDads = dds[4].getDependencyArtifacts("provided");
+        assertEquals(1, providedDads.length);
+        assertSame(dads[1], providedDads[0]);
+        compileDads = dds[4].getDependencyArtifacts("compile");
+        assertEquals(1, compileDads.length);
+        assertSame(dads[0], compileDads[0]);
 
         // now we verify the conversion to an Ivy file
         PomModuleDescriptorParser.getInstance().toIvyFile(
@@ -395,10 +424,10 @@ public class PomModuleDescriptorParserTest extends 
AbstractModuleDescriptorParse
         assertNotNull(md2);
         assertEquals(md.getModuleRevisionId(), md2.getModuleRevisionId());
         dds = md2.getDependencies();
-        assertEquals(3, dds.length);
+        assertEquals(5, dds.length);
         for (int i = 0; i < dds.length; i++) {
             assertEquals(2, dds[i].getAllDependencyArtifacts().length);
-            int withExt = i == 0 ? 1: 0;
+            int withExt = i == 0 || i == 3 ? 1: 0;
             assertEquals(extraAtt, 
dds[i].getAllDependencyArtifacts()[withExt].getExtraAttributes());
         }
     }
diff --git 
a/test/java/org/apache/ivy/plugins/parser/m2/test-dependencies-with-and-without-classifier.pom
 
b/test/java/org/apache/ivy/plugins/parser/m2/test-dependencies-with-and-without-classifier.pom
index dce9c431..7cf2ff81 100644
--- 
a/test/java/org/apache/ivy/plugins/parser/m2/test-dependencies-with-and-without-classifier.pom
+++ 
b/test/java/org/apache/ivy/plugins/parser/m2/test-dependencies-with-and-without-classifier.pom
@@ -63,5 +63,29 @@
       <version>1.0.4</version>
       <classifier>foo</classifier>
     </dependency>
+    <dependency>
+      <groupId>commons-logging</groupId>
+      <artifactId>commons-logging4</artifactId>
+      <version>1.0.4</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>commons-logging</groupId>
+      <artifactId>commons-logging4</artifactId>
+      <version>1.0.4</version>
+      <classifier>asl</classifier>
+    </dependency>
+    <dependency>
+      <groupId>commons-logging</groupId>
+      <artifactId>commons-logging5</artifactId>
+      <version>1.0.4</version>
+      <classifier>asl</classifier>
+    </dependency>
+    <dependency>
+      <groupId>commons-logging</groupId>
+      <artifactId>commons-logging5</artifactId>
+      <version>1.0.4</version>
+      <scope>provided</scope>
+    </dependency>
   </dependencies>
 </project>

Reply via email to