From 941b50273914ef531472b6856358b4c8629e9081 Mon Sep 17 00:00:00 2001 From: Juul Hobert Date: Fri, 29 Mar 2024 10:01:11 +0100 Subject: [PATCH] [MNG-7344] Unit tests for updatedWithImportedFrom --- ...faultDependencyManagementImporterTest.java | 161 ++++++++++-------- 1 file changed, 90 insertions(+), 71 deletions(-) diff --git a/maven-model-builder/src/test/java/org/apache/maven/model/composition/DefaultDependencyManagementImporterTest.java b/maven-model-builder/src/test/java/org/apache/maven/model/composition/DefaultDependencyManagementImporterTest.java index 2fbb2089f576..fdf39ab4b17f 100644 --- a/maven-model-builder/src/test/java/org/apache/maven/model/composition/DefaultDependencyManagementImporterTest.java +++ b/maven-model-builder/src/test/java/org/apache/maven/model/composition/DefaultDependencyManagementImporterTest.java @@ -18,87 +18,106 @@ */ package org.apache.maven.model.composition; +import org.apache.maven.api.model.Dependency; +import org.apache.maven.api.model.DependencyManagement; +import org.apache.maven.api.model.InputLocation; +import org.apache.maven.api.model.InputSource; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + class DefaultDependencyManagementImporterTest { - // TODO Need to rewrite tests as soon as the SUT is a bit more stable - /* @Test - public void testUpdateDependencyHierarchy_SameSource() { - InputSource source = new InputSource(); - source.setModelId("SINGLE_SOURCE"); - Dependency dependency = new Dependency(); - dependency.setLocation("", new InputLocation(1, 1, source)); - DependencyManagement bom = new DependencyManagement(); - bom.setLocation("", new InputLocation(2, 2, source)); - - DefaultDependencyManagementImporter.updateDependencyImports(dependency, bom); - - assertSame(source, dependency.getLocation("").getSource()); - assertSame(source, bom.getLocation("").getSource()); - assertNull(source.getImportedBy()); + void testUpdateWithImportedFrom_dependencyLocationAndBomLocationAreNull_dependencyReturned() { + final Dependency dependency = Dependency.newBuilder().build(); + final DependencyManagement depMgmt = DependencyManagement.newBuilder().build(); + final Dependency result = DefaultDependencyManagementImporter.updateWithImportedFrom(dependency, depMgmt); + + assertThat(dependency).isEqualTo(result); + } + + @Test + void testUpdateWithImportedFrom_bomAndDependencyHaveSameSource_dependencyRootFound() { + final InputSource source = new InputSource("SINGLE_SOURCE", ""); + final Dependency dependency = Dependency.newBuilder() + .location("", new InputLocation(1, 1, source)) + .build(); + final DependencyManagement bom = DependencyManagement.newBuilder() + .location("", new InputLocation(1, 1, source)) + .build(); + + final Dependency result = DefaultDependencyManagementImporter.updateWithImportedFrom(dependency, bom); + + assertThat(result).isNotNull(); + assertThat(result.getImportedFrom()).isEqualTo(bom.getLocation("")); } @Test - public void testUpdateDependencyHierarchy_SingleLevel() { - InputSource dependencySource = new InputSource(); - dependencySource.setModelId("DEPENDENCY"); - InputSource bomSource = new InputSource(); - bomSource.setModelId("BOM"); - Dependency dependency = new Dependency(); - dependency.setLocation("", new InputLocation(1, 1, dependencySource)); - DependencyManagement bom = new DependencyManagement(); - bom.setLocation("", new InputLocation(2, 2, bomSource)); - - DefaultDependencyManagementImporter.updateDependencyImports(dependency, bom); - - assertSame(dependencySource, dependency.getLocation("").getSource()); - assertSame(bomSource, bom.getLocation("").getSource()); - assertEquals(bomSource, dependencySource.getImportedBy()); + public void testUpdateWithImportedFrom_singleLevel_importedFromSet() { + // Arrange + final InputSource dependencySource = new InputSource("DEPENDENCY", "DEPENDENCY"); + final InputSource bomSource = new InputSource("BOM", "BOM"); + final Dependency dependency = Dependency.newBuilder() + .location("", new InputLocation(1, 1, dependencySource)) + .build(); + final DependencyManagement bom = DependencyManagement.newBuilder() + .location("", new InputLocation(2, 2, bomSource)) + .build(); + + // Act + final Dependency result = DefaultDependencyManagementImporter.updateWithImportedFrom(dependency, bom); + + // Assert + assertThat(result).isNotNull(); + assertThat(result.getImportedFrom().toString()) + .isEqualTo(bom.getLocation("").toString()); } @Test - public void testUpdateDependencyHierarchy_MultiLevel() { - InputSource intermediateSource = new InputSource(); - intermediateSource.setModelId("INTERMEDIATE"); - InputSource dependencySource = new InputSource(); - dependencySource.setModelId("DEPENDENCY"); - dependencySource.setImportedBy(intermediateSource); - InputSource bomSource = new InputSource(); - bomSource.setModelId("BOM"); - Dependency dependency = new Dependency(); - dependency.setLocation("", new InputLocation(1, 1, dependencySource)); - DependencyManagement bom = new DependencyManagement(); - bom.setLocation("", new InputLocation(2, 2, bomSource)); - - DefaultDependencyManagementImporter.updateDependencyImports(dependency, bom); - - assertSame(dependencySource, dependency.getLocation("").getSource()); - assertSame(bomSource, bom.getLocation("").getSource()); - assertEquals(intermediateSource, dependencySource.getImportedBy()); - assertEquals(bomSource, intermediateSource.getImportedBy()); + public void testUpdateWithImportedFrom_multiLevel_importedFromSetChanged() { + // Arrange + final InputSource bomSource = new InputSource("BOM", "BOM"); + final InputSource intermediateSource = + new InputSource("INTERMEDIATE", "INTERMEDIATE").importedFrom(new InputLocation(bomSource)); + final InputSource dependencySource = + new InputSource("DEPENDENCY", "DEPENDENCY").importedFrom(new InputLocation(intermediateSource)); + final InputLocation bomLocation = new InputLocation(2, 2, bomSource); + final Dependency dependency = Dependency.newBuilder() + .location("", new InputLocation(1, 1, dependencySource)) + .importedFrom(bomLocation) + .build(); + final DependencyManagement bom = + DependencyManagement.newBuilder().location("", bomLocation).build(); + + // Act + final Dependency result = DefaultDependencyManagementImporter.updateWithImportedFrom(dependency, bom); + + // Assert + assertThat(result.getImportedFrom().toString()) + .isEqualTo(bom.getLocation("").toString()); } @Test - public void testUpdateDependencyHierarchy_PresentSource() { - InputSource bomSource = new InputSource(); - bomSource.setModelId("BOM"); - InputSource intermediateSource = new InputSource(); - intermediateSource.setModelId("INTERMEDIATE"); - intermediateSource.setImportedBy(bomSource); - InputSource dependencySource = new InputSource(); - dependencySource.setModelId("DEPENDENCY"); - dependencySource.setImportedBy(intermediateSource); - Dependency dependency = new Dependency(); - dependency.setLocation("", new InputLocation(1, 1, dependencySource)); - DependencyManagement bom = new DependencyManagement(); - bom.setLocation("", new InputLocation(2, 2, bomSource)); - - DefaultDependencyManagementImporter.updateDependencyImports(dependency, bom); - - assertSame(dependencySource, dependency.getLocation("").getSource()); - assertSame(bomSource, bom.getLocation("").getSource()); - assertEquals(intermediateSource, dependencySource.getImportedBy()); - assertEquals(bomSource, intermediateSource.getImportedBy()); - assertNull(bomSource.getImportedBy()); + public void testUpdateWithImportedFrom_multiLevelAlreadyFoundInDifferentSource_importedFromSetMaintained() { + // Arrange + final InputSource bomSource = new InputSource("BOM", "BOM"); + final InputSource intermediateSource = + new InputSource("INTERMEDIATE", "INTERMEDIATE").importedFrom(new InputLocation(bomSource)); + final InputSource dependencySource = + new InputSource("DEPENDENCY", "DEPENDENCY").importedFrom(new InputLocation(intermediateSource)); + final Dependency dependency = Dependency.newBuilder() + .location("", new InputLocation(1, 1, dependencySource)) + .build(); + final DependencyManagement differentSource = DependencyManagement.newBuilder() + .location("", new InputLocation(2, 2, new InputSource("BOM2", "BOM2"))) + .build(); + + // Act + final Dependency result = + DefaultDependencyManagementImporter.updateWithImportedFrom(dependency, differentSource); + + // Assert + assertThat(result.getImportedFrom().toString()) + .isEqualTo(differentSource.getLocation("").toString()); } - */ }