Skip to content

Commit

Permalink
Merge pull request #64 from Roche-CSI/ParseEffectivePOM
Browse files Browse the repository at this point in the history
  • Loading branch information
gdgib authored May 1, 2024
2 parents 7e532ed + 41a65d8 commit 489a434
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 5 deletions.
16 changes: 13 additions & 3 deletions re-maven/src/main/java/com/g2forge/reassert/maven/MavenSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -80,12 +82,12 @@ public IDescriber<MavenCoordinates> getCoordinateDescriber() {
return MavenCoordinatesDescriber.create();
}

public MavenPOM parse(IDataSource source) {
protected <T> T parse(IDataSource source, Class<T> 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);
}
}

Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<MavenPOM> projects;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version='1.0' encoding='UTF-8'?>
<projects>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>Group</groupId>
<artifactId>Artifact</artifactId>
<version>Version</version>

<dependencies>
<dependency>
<groupId>Dependencies</groupId>
<artifactId>One</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>Dependencies</groupId>
<artifactId>Two</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<modules>
<module>ModuleA</module>
<module>ModuleB</module>
</modules>

<profiles>
<profile>
<id>Profile1</id>
<modules>
<module>ModuleC</module>
</modules>
</profile>
</profiles>
</project>
</projects>

0 comments on commit 489a434

Please sign in to comment.