Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private boolean shouldWriteAllEffectivePOMsInReactor() {
* @throws MojoExecutionException if any
*/
private void writeEffectivePom(MavenProject project, XMLWriter writer) throws MojoExecutionException {
Model pom = project.getModel();
Model pom = project.getModel().clone();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how sure are we that clone works here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @elharo, sorry for late reply.

So I think .clone() works here because in the original version the pom in cleanModel(pom) has a reference to the original Model, so cleaning/sorting the properties there overwrites the original ones. That's the issue in #384. Since you mentioned the goal should be read only, I pass a clone() of the Model to cleanModel() instead, so only the copy gets changed.

I am still new to this codebase, so if there is a better way I am happy to change it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My question is more does this return a Cloneable object? What's the contract on Model?

cleanModel(pom);

StringWriter sWriter = new StringWriter();
Expand Down
112 changes: 112 additions & 0 deletions src/test/java/org/apache/maven/plugins/help/EffectivePomMojoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.plugins.help;

import javax.inject.Inject;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Properties;

import org.apache.maven.api.plugin.testing.InjectMojo;
import org.apache.maven.api.plugin.testing.MojoParameter;
import org.apache.maven.api.plugin.testing.MojoTest;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Model;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.project.MavenProject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.apache.maven.api.plugin.testing.MojoExtension.setVariableValueToObject;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.Mockito.when;

/**
* Test class for the effective-pom mojo of the Help Plugin.
*/
@ExtendWith(MockitoExtension.class)
@MojoTest
class EffectivePomMojoTest {

@Inject
private MavenProject project;

@Inject
private MavenSession mavenSession;

@Mock
private MojoExecution mojoExecution;

@TempDir
private Path tempDir;

private final Model model = new Model();

private final Properties originalProperties = new Properties();

@BeforeEach
void setup() throws IOException {
originalProperties.setProperty("b.property", "b-value");
originalProperties.setProperty("a.property", "a-value");

model.setProperties(originalProperties);

when(project.getModel()).thenReturn(model);

Path outputPath = Files.createTempFile(tempDir, "maven-help-plugin-test-", ".xml");
mavenSession.getUserProperties().setProperty("outputPath", outputPath.toString());
}

/**
* The effective-pom goal only displays the model, so it must not modify the project it reads from.
*
* @throws Exception in case of errors.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really needed on test

*/
@Test
@InjectMojo(goal = "effective-pom")
@MojoParameter(name = "output", value = "${outputPath}")
void testExecuteDoesNotModifyProjectModel(EffectivePomMojo mojo) throws Exception {
// snapshot of the contents before the mojo runs, to detect in-place modification
Properties expectedProperties = new Properties();
expectedProperties.putAll(originalProperties);

setVariableValueToObject(mojo, "projects", Collections.singletonList(project));
setVariableValueToObject(mojo, "mojoExecution", mojoExecution);

mojo.execute();

assertSame(
originalProperties,
model.getProperties(),
"effective-pom must not replace the properties of the project model");

assertEquals(
expectedProperties,
model.getProperties(),
"effective-pom must not modify the properties of the project model");
}
}
Loading