Skip to content

Commit

Permalink
Add design & unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed Oct 23, 2023
1 parent 2476bcf commit 97e6315
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 1 deletion.
14 changes: 14 additions & 0 deletions doc/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,20 @@ Covers:

Needs: impl, itest

#### Excluding Implicit Plugins from Dependencies.md

`dsn~dependency.md-file-validator-excludes-implicit-plugins~1`

PK excludes implicit plugins from `dependencies.md`.

Background:

Maven implicitly adds plugins like `org.apache.maven.plugins:maven-clean-plugin` due to the Maven lifecycle. The version of these plugins depends on the Maven version. Each plugin version defines its own name and license which is included in `dependencies.md`. That means the content of `dependencies.md` depends on the Maven version, causing build failures when using a different Maven version, see [issue #436](https://github.com/exasol/project-keeper/issues/436).

One workaround is pinning the Maven version using `maven-enforcer-plugin` but this causes problems on developer's machines. That's why we decided to remove these implicit plugins from `dependencies.md`.

Needs: impl, utest, itest

### Readme.md File Validator

`dsn~readme-validator~1`
Expand Down
5 changes: 5 additions & 0 deletions maven-project-crawler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.exasol</groupId>
<artifactId>maven-plugin-integration-testing</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public ProjectDependencyReader(final MavenModelFromRepositoryReader artifactMode
*/
public ProjectDependencies readDependencies() {
final List<ProjectDependency> dependencies = getDependenciesIncludingPlugins()
.map(dependency -> getLicense(dependency, project)).collect(Collectors.toList());
.map(dependency -> getLicense(dependency, project)) //
.collect(Collectors.toList());
return new ProjectDependencies(dependencies);
}

Expand Down Expand Up @@ -86,6 +87,7 @@ private Stream<Dependency> getPluginDependencies() {
* @param plugin the plugin to check
* @return {@code true} if the plugin is explicitly added to the build
*/
// [impl -> dsn~dependency.md-file-validator-excludes-implicit-plugins~1]
private boolean isExplicitPlugin(final Plugin plugin) {
final String location = plugin.getLocation("").getSource().getLocation();
return location != null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.exasol.projectkeeper.dependencies;

import static java.util.Collections.emptyList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

import java.util.List;

import org.apache.maven.model.*;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import com.exasol.projectkeeper.pom.MavenModelFromRepositoryReader;
import com.exasol.projectkeeper.shared.dependencies.BaseDependency.Type;
import com.exasol.projectkeeper.shared.dependencies.License;
import com.exasol.projectkeeper.shared.dependencies.ProjectDependency;
import com.exasol.projectkeeper.validators.dependencies.ProjectDependencyReader;

@ExtendWith(MockitoExtension.class)
class ProjectDependencyReaderTest {

@Mock
MavenModelFromRepositoryReader artifactModelReaderMock;

private List<ProjectDependency> readDependencies(final List<Plugin> plugins) {
final MavenProject project = new MavenProject(new Model());
project.getModel().setBuild(new Build());
project.getModel().getBuild().setPlugins(plugins);
return new ProjectDependencyReader(artifactModelReaderMock, project).readDependencies().getDependencies();
}

@Test
void noDependencies() {
assertThat(readDependencies(emptyList()), empty());
}

@Test
void explicitPlugin() throws ProjectBuildingException {
simulateDependencies("group", "art", "ver", "website", List.of(mavenLicense("license", "licenseUrl")));

assertThat(readDependencies(List.of(plugin("group", "art", "ver", "no-null"))),
contains(ProjectDependency.builder().type(Type.PLUGIN).name("name:group:art").websiteUrl("website")
.licenses(List.of(new License("license", "licenseUrl"))).build()));
}

// [utest -> dsn~dependency.md-file-validator-excludes-implicit-plugins~1]
@Test
void implicitPlugin() throws ProjectBuildingException {
assertThat(readDependencies(List.of(plugin("group", "art", "ver", null))), empty());
}

private void simulateDependencies(final String groupId, final String artifactId, final String version,
final String url, final List<org.apache.maven.model.License> licenses) throws ProjectBuildingException {
final Model model = new Model();
model.setArtifactId(artifactId);
model.setGroupId(groupId);
model.setVersion(version);
model.setName("name:" + groupId + ":" + artifactId);
model.setUrl(url);
model.setLicenses(licenses);
when(artifactModelReaderMock.readModel(eq(artifactId), eq(groupId), eq(version), anyList())).thenReturn(model);
}

private org.apache.maven.model.License mavenLicense(final String name, final String url) {
final org.apache.maven.model.License license = new org.apache.maven.model.License();
license.setName(name);
license.setUrl(url);
return license;
}

private Plugin plugin(final String groupId, final String artifactId, final String version,
final String sourceLocation) {
final Plugin plugin = new Plugin();
plugin.setGroupId(groupId);
plugin.setArtifactId(artifactId);
plugin.setVersion(version);
final InputSource source = new InputSource();
source.setLocation(sourceLocation);
plugin.setLocation("", new InputLocation(0, 0, source));
return plugin;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ void testWrongContentWithNonDefaultRepository() throws IOException {
assertThat(output, containsString("E-PK-CORE-53: The dependencies.md file has outdated content."));
}

// [itest -> dsn~dependency.md-file-validator-excludes-implicit-plugins~1]
@Test
void testFix() throws IOException {
createExamplePomFile();
Expand Down

0 comments on commit 97e6315

Please sign in to comment.