Skip to content

Commit

Permalink
add Parent for pom model manipulation (#473)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rainer Drexler authored Jul 27, 2021
1 parent 6b1b708 commit fc22112
Show file tree
Hide file tree
Showing 4 changed files with 283 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright (c) 2020, Mulesoft, LLC. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause License
* license that can be found in the LICENSE.txt file.
*/
package com.mulesoft.tools.migration.project.model.pom;

/**
* Represents a Parent in the pom model. By default its type is jar.
*
* @author Mulesoft Inc.
* @since 1.0.0
*/
public class Parent {

/**
* The ParentBuilder. It builds the Parent Wrapper.
*
* @author Mulesoft Inc.
* @since 1.0.0
*/
public static class ParentBuilder {

private String groupId;
private String artifactId;
private String version;

ParentBuilder withGroupId(String groupId) {
this.groupId = groupId;
return this;
}

public ParentBuilder withArtifactId(String artifactId) {
this.artifactId = artifactId;
return this;
}

public ParentBuilder withVersion(String version) {
this.version = version;
return this;
}

/**
* Builds the a Parent for a PomModel.
*
* @return a Parent
*/
public Parent build() {
Parent parent = new Parent();
parent.setArtifactId(artifactId);
parent.setGroupId(groupId);
parent.setVersion(version);
return parent;
}
}

private final org.apache.maven.model.Parent parent;

protected Parent(org.apache.maven.model.Parent parent) {
this.parent = parent;
}

/**
* Retrieves the parent model represented by a maven core object.
*
* @return the parent inner model
*/
protected org.apache.maven.model.Parent getInnerModel() {
return parent;
}

protected Parent() {
this.parent = new org.apache.maven.model.Parent();
}

/**
* Retrieves the pom parent artifact id.
*
* @return a {@link String}
*/
public String getArtifactId() {
return parent.getArtifactId();
}

/**
* Retrieves the parent group id.
*
* @return a {@link String}
*/
public String getGroupId() {
return parent.getGroupId();
}

/**
* Retrieves the parent version.
*
* @return a {@link String}
*/
public String getVersion() {
return parent.getVersion();
}

/**
* Retrieves the parent relativePath version.
*
* @return a {@link String}
*/
public String getRelativePath() {
return parent.getRelativePath();
}

/**
* Sets the parent artifact id.
*
* @param artifactId
*/
public void setArtifactId(String artifactId) {

parent.setArtifactId(artifactId);
}

/**
* Sets the parent group id.
*
* @param groupId
*/
public void setGroupId(String groupId) {
parent.setGroupId(groupId);
}

/**
* Sets the parent version.
*
* @param version
*/
public void setVersion(String version) {
parent.setVersion(version);
}

/**
* Retrieves the parent relativePath.
*
* @return a {@link String}
*/
public void setRelativePath(String relativePath) {
parent.setRelativePath(relativePath);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@
import static com.mulesoft.tools.migration.project.model.pom.PomModelUtils.buildMinimalMule4ApplicationPom;
import static java.util.stream.Collectors.toList;

import org.apache.maven.model.Build;
import org.apache.maven.model.DistributionManagement;
import org.apache.maven.model.Model;
import org.apache.maven.model.Profile;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
Expand All @@ -25,6 +18,13 @@
import java.util.Properties;
import java.util.function.Predicate;

import org.apache.maven.model.Build;
import org.apache.maven.model.DistributionManagement;
import org.apache.maven.model.Model;
import org.apache.maven.model.Profile;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

/**
* The pom model.
*
Expand Down Expand Up @@ -156,6 +156,24 @@ public void setArtifactId(String artifactId) {
model.setArtifactId(artifactId);
}

/**
* Retrieves the parent declared in the pom.
*
* @return a {@link String}
*/
public Parent getParent() {
return new Parent(model.getParent());
}

/**
* Sets the artifact id in the pom.
*
* @param artifactId
*/
public void setParent(Parent parent) {
model.setParent(parent.getInnerModel());
}

/**
* Retrieves the group id declared in the pom.
*
Expand Down Expand Up @@ -367,6 +385,15 @@ public void addRepository(Repository repository) {
model.addRepository(repository.getInnerModel());
}

/**
* Adds a {@link Repository} to the {@link PomModel} in the repository section.
*
* @param repository
*/
public void removeRepository(Repository repository) {
model.removeRepository(repository.getInnerModel());
}

/**
* Retrieves the list of reapositories plugin repository section.
*/
Expand All @@ -383,6 +410,7 @@ public void addPluginRepository(Repository repository) {
model.addPluginRepository(repository.getInnerModel());
}


/**
* The pom model builder. It builds the pom model based on the pom location in the filesystem.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,35 @@
*/
package com.mulesoft.tools.migration.project.model.pom;

import static com.mulesoft.tools.migration.project.model.pom.PomModelTestCaseUtils.buildDependency;
import static com.mulesoft.tools.migration.project.model.pom.PomModelTestCaseUtils.getPomModelDependencies;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isIn;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.Every.everyItem;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.powermock.api.mockito.PowerMockito.doReturn;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.spy;
import static org.powermock.api.mockito.PowerMockito.verifyPrivate;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.reflect.Whitebox.getInternalState;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.maven.model.DeploymentRepository;
import org.apache.maven.model.DistributionManagement;
import org.apache.maven.model.Model;
Expand All @@ -16,23 +45,6 @@
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

import static com.mulesoft.tools.migration.project.model.pom.PomModelTestCaseUtils.buildDependency;
import static com.mulesoft.tools.migration.project.model.pom.PomModelTestCaseUtils.getPomModelDependencies;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.Every.everyItem;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.powermock.api.mockito.PowerMockito.*;
import static org.powermock.reflect.Whitebox.getInternalState;


@RunWith(Enclosed.class)
public class PomModelTestCase {
Expand Down Expand Up @@ -106,6 +118,35 @@ public void getPackaging() {
assertThat("Packaging is not the expected", model.getPackaging(), equalTo("simple-pom-packaging"));
}

@Test
public void getParentGroupId() {
assertThat("GroupId is not the expected", model.getParent().getGroupId(), equalTo("org.simple.pom.parent"));
}

@Test
public void getParentArtifactId() {
assertThat("ArtifactId is not the expected", model.getParent().getArtifactId(), equalTo("parent-mule-api"));
}

@Test
public void getParentVersion() {
assertThat("Version is not the expected", model.getParent().getVersion(), equalTo("1.0.0"));
}

@Test
public void getParentRelativePath() {
assertThat("RelativePath is not the expected", model.getParent().getRelativePath(), equalTo("../pom.xml"));
}

@Test
public void getParentInnerModel() {

assertThat("InnerModelId is not the expected", model.getParent().getInnerModel().getId(),
equalTo("org.simple.pom.parent:parent-mule-api:pom:1.0.0"));
}



@Test
public void getProperties() {
Properties properties = new Properties();
Expand Down Expand Up @@ -225,5 +266,40 @@ public void setDescription() {

assertThat("Description should exist on the pom model", model.getDescription(), equalTo(value));
}

@Test
public void setParentGroupId() {
model.getParent().setGroupId("new-group-id");
assertThat("GroupId is not the expected", model.getParent().getGroupId(), equalTo("new-group-id"));
}

@Test
public void setParentArtifactId() {
model.getParent().setArtifactId("new-artifact-id");
assertThat("GroupId is not the expected", model.getParent().getArtifactId(), equalTo("new-artifact-id"));
}

@Test
public void setParentVersion() {
model.getParent().setVersion("2.0.0");
assertThat("GroupId is not the expected", model.getParent().getVersion(), equalTo("2.0.0"));
}

@Test
public void setParentRelativePath() {
model.getParent().setRelativePath("new-path");
assertThat("RelativePath is not the expected", model.getParent().getRelativePath(), equalTo("new-path"));
}

@Test
public void setParent() {

final Parent parent =
new Parent.ParentBuilder().withGroupId("myGroupId").withArtifactId("myArtifactId").withVersion("1.0.0").build();
assertNotNull(model.getParent());
model.setParent(parent);
assertNotNull(model.getParent());
assertThat("Parent is not the expected", parent.getInnerModel(), equalTo(model.getParent().getInnerModel()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
<packaging>simple-pom-packaging</packaging>

<name>Simple Pom</name>

<parent>
<groupId>org.simple.pom.parent</groupId>
<artifactId>parent-mule-api</artifactId>
<version>1.0.0</version>
</parent>

<properties>
<key1>value1</key1>
Expand Down

0 comments on commit fc22112

Please sign in to comment.