From 41a65d80dd10a75332343bfe82c6f89c5c851c55 Mon Sep 17 00:00:00 2001 From: Greg Gibeling Date: Wed, 1 May 2024 11:51:49 -0700 Subject: [PATCH] Support & unit test for parsing effective POMs with multiple projects --- .../g2forge/reassert/maven/MavenSystem.java | 16 ++++++-- .../maven/model/MavenEffectivePOM.java | 23 +++++++++++ .../reassert/maven/model/TestMavenPOM.java | 20 +++++++++- .../maven/model/test-effective-pom.xml.txt | 40 +++++++++++++++++++ 4 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 re-maven/src/main/java/com/g2forge/reassert/maven/model/MavenEffectivePOM.java create mode 100644 re-maven/src/test/java/com/g2forge/reassert/maven/model/test-effective-pom.xml.txt diff --git a/re-maven/src/main/java/com/g2forge/reassert/maven/MavenSystem.java b/re-maven/src/main/java/com/g2forge/reassert/maven/MavenSystem.java index 2531b13..2d6b38a 100644 --- a/re-maven/src/main/java/com/g2forge/reassert/maven/MavenSystem.java +++ b/re-maven/src/main/java/com/g2forge/reassert/maven/MavenSystem.java @@ -12,6 +12,7 @@ import com.ctc.wstx.api.WstxInputProperties; import com.ctc.wstx.stax.WstxInputFactory; +import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.dataformat.xml.XmlFactory; import com.fasterxml.jackson.dataformat.xml.XmlMapper; @@ -24,6 +25,7 @@ import com.g2forge.reassert.core.api.described.IDescriber; import com.g2forge.reassert.core.api.module.IContext; import com.g2forge.reassert.core.api.system.ISystem; +import com.g2forge.reassert.maven.model.MavenEffectivePOM; import com.g2forge.reassert.maven.model.MavenPOM; import com.g2forge.reassert.maven.model.convert.MavenXmlModule; @@ -80,12 +82,12 @@ public IDescriber getCoordinateDescriber() { return MavenCoordinatesDescriber.create(); } - public MavenPOM parse(IDataSource source) { + protected T parse(IDataSource source, Class type) { final XmlMapper mapper = getMapper(); try (final InputStream stream = source.getStream(ITypeRef.of(InputStream.class))) { - return mapper.readValue(stream, MavenPOM.class); + return mapper.readValue(stream, type); } catch (IOException e) { - throw new RuntimeIOException("Failed to parse Maven POM from " + source, e); + throw new RuntimeIOException("Failed to parse " + type.getSimpleName() + " from " + source, e); } } @@ -97,6 +99,14 @@ public MavenCoordinates parse(String string) { return new MavenCoordinates(this, matcher.group(1), matcher.group(2), matcher.group(3), packaging); } + public MavenEffectivePOM parseEffectivePOM(IDataSource source) { + return parse(source, MavenEffectivePOM.class); + } + + public MavenPOM parsePOM(IDataSource source) { + return parse(source, MavenPOM.class); + } + @Override public MavenCoordinates withSystem(MavenCoordinates coordinates) { if (!isValid(coordinates)) throw new IllegalArgumentException(); diff --git a/re-maven/src/main/java/com/g2forge/reassert/maven/model/MavenEffectivePOM.java b/re-maven/src/main/java/com/g2forge/reassert/maven/model/MavenEffectivePOM.java new file mode 100644 index 0000000..e20eedc --- /dev/null +++ b/re-maven/src/main/java/com/g2forge/reassert/maven/model/MavenEffectivePOM.java @@ -0,0 +1,23 @@ +package com.g2forge.reassert.maven.model; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; + +import lombok.Builder; +import lombok.Data; +import lombok.RequiredArgsConstructor; +import lombok.Singular; + +@Data +@Builder(toBuilder = true) +@RequiredArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class MavenEffectivePOM { + @JacksonXmlElementWrapper(useWrapping = false) + @JsonProperty("project") + @Singular + protected final List projects; +} diff --git a/re-maven/src/test/java/com/g2forge/reassert/maven/model/TestMavenPOM.java b/re-maven/src/test/java/com/g2forge/reassert/maven/model/TestMavenPOM.java index 9b85b4e..fb1f46d 100644 --- a/re-maven/src/test/java/com/g2forge/reassert/maven/model/TestMavenPOM.java +++ b/re-maven/src/test/java/com/g2forge/reassert/maven/model/TestMavenPOM.java @@ -12,10 +12,26 @@ public class TestMavenPOM { @Test - public void test() { + public void parseEffectivePom() { final MavenSystem mavenSystem = new MavenSystem(Context.getContext()); - final MavenPOM actual = mavenSystem.parse(new ResourceDataSource(new Resource(getClass(), "test-pom.xml.txt"))); + final MavenEffectivePOM actual = mavenSystem.parseEffectivePOM(new ResourceDataSource(new Resource(getClass(), "test-effective-pom.xml.txt"))); + HAssert.assertEquals(1, actual.getProjects().size()); + final MavenPOM project = HCollection.getOne(actual.getProjects()); + + HAssert.assertEquals(new MavenCoordinates(mavenSystem, "Group", "Artifact", "Version", null), project.getCoordinates()); + HAssert.assertNull(project.getParent()); + HAssert.assertNull(project.getLicenses()); + HAssert.assertNull(project.getProperties()); + HAssert.assertEquals(HCollection.asList(new MavenDependency(new MavenCoordinates(mavenSystem, "Dependencies", "One", "1.0.0", null), null, false), new MavenDependency(new MavenCoordinates(mavenSystem, "Dependencies", "Two", "2.0.0", null), MavenScope.Test, false)), project.getDependencies()); + HAssert.assertEquals(HCollection.asList("ModuleA", "ModuleB"), project.getModules()); + HAssert.assertEquals(HCollection.asList(new MavenProfile("Profile1", null, null, HCollection.asList("ModuleC"))), project.getProfiles()); + } + + @Test + public void parsePom() { + final MavenSystem mavenSystem = new MavenSystem(Context.getContext()); + final MavenPOM actual = mavenSystem.parsePOM(new ResourceDataSource(new Resource(getClass(), "test-pom.xml.txt"))); HAssert.assertEquals(new MavenCoordinates(mavenSystem, "Group", "Artifact", "Version", null), actual.getCoordinates()); HAssert.assertNull(actual.getParent()); HAssert.assertNull(actual.getLicenses()); diff --git a/re-maven/src/test/java/com/g2forge/reassert/maven/model/test-effective-pom.xml.txt b/re-maven/src/test/java/com/g2forge/reassert/maven/model/test-effective-pom.xml.txt new file mode 100644 index 0000000..1150985 --- /dev/null +++ b/re-maven/src/test/java/com/g2forge/reassert/maven/model/test-effective-pom.xml.txt @@ -0,0 +1,40 @@ + + + + 4.0.0 + + Group + Artifact + Version + + + + Dependencies + One + 1.0.0 + + + Dependencies + Two + 2.0.0 + test + + + + + ModuleA + ModuleB + + + + + Profile1 + + ModuleC + + + + + \ No newline at end of file