Skip to content

Commit

Permalink
Code quality improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisJehan committed Nov 21, 2024
1 parent 05027fc commit 11d7a39
Show file tree
Hide file tree
Showing 12 changed files with 255 additions and 177 deletions.
13 changes: 11 additions & 2 deletions src/main/java/com/github/alexisjehan/mvncheck/core/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,11 @@ public List<BuildFile> findBuildFiles(final Path path, final int maxDepth) throw
public List<BuildFile> filterBuildFiles(final List<BuildFile> buildFiles) {
Ensure.notNullAndNotNullElements("buildFiles", buildFiles);
final var outputDirectories = buildFiles.stream()
.map(buildFile -> buildFile.getFile().resolveSibling(buildFile.getType().getOutputDirectoryName()))
.map(
buildFile -> buildFile.getFile().resolveSibling(
buildFile.getType().getOutputDirectoryName()
)
)
.collect(Collectors.toUnmodifiableList());
return buildFiles.stream()
.filter(buildFile -> outputDirectories.stream().noneMatch(buildFile.getFile()::startsWith))
Expand Down Expand Up @@ -287,7 +291,12 @@ public List<ArtifactUpdateVersion> findArtifactUpdateVersions(
.filter(version -> !ignoreSnapshots || !VersionFilter.SNAPSHOT.accept(version))
.isPresent()
)
.map(artifact -> artifactAvailableVersionsResolver.resolve(artifact, build.getRepositories()))
.map(
artifact -> artifactAvailableVersionsResolver.resolve(
artifact,
build.getRepositories()
)
)
.map(artifactAvailableVersions -> {
final var artifact = artifactAvailableVersions.getArtifact();
final var artifactVersion = artifact.getOptionalVersion().orElseThrow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,28 +167,40 @@ private Build resolve(final BuildFile file, final OutputStream outputStream) thr
final var repositories = parseRepositories(reader);
logger.debug("Parsed repositories:");
repositories.forEach(
repository -> logger.debug("- {}", () -> ToString.toString(repository))
repository -> logger.debug(
"- {}",
() -> ToString.toString(repository)
)
);

logger.trace("Filtering repositories");
final var filteredRepositories = filterRepositories(repositories);
logger.debug("Filtered repositories:");
filteredRepositories.forEach(
filteredRepository -> logger.debug("- {}", () -> ToString.toString(filteredRepository))
filteredRepository -> logger.debug(
"- {}",
() -> ToString.toString(filteredRepository)
)
);

logger.trace("Parsing artifacts");
final var artifacts = parseArtifacts(reader);
logger.debug("Parsed artifacts:");
artifacts.forEach(
artifact -> logger.debug("- {}", () -> ToString.toString(artifact))
artifact -> logger.debug(
"- {}",
() -> ToString.toString(artifact)
)
);

logger.trace("Filtering artifacts");
final var filteredArtifacts = filterArtifacts(artifacts);
logger.debug("Filtered artifacts:");
filteredArtifacts.forEach(
filteredArtifact -> logger.debug("- {}", () -> ToString.toString(filteredArtifact))
filteredArtifact -> logger.debug(
"- {}",
() -> ToString.toString(filteredArtifact)
)
);

return new Build(file, filteredRepositories, filteredArtifacts);
Expand Down Expand Up @@ -280,7 +292,10 @@ static List<Artifact<GradleArtifactType>> parseArtifacts(final BufferedReader bu
}
final var artifactTypeString = Strings.substringBefore(line, " - ");
final var optionalArtifactType = Arrays.stream(GradleArtifactType.values())
.filter(artifactType -> artifactType.dependenciesTaskName().equals(artifactTypeString))
.filter(
artifactType -> artifactType.dependenciesTaskName()
.equals(artifactTypeString)
)
.findAny();
if (optionalArtifactType.isEmpty()) {
while (null != (line = bufferedReader.readLine())) {
Expand Down Expand Up @@ -411,9 +426,16 @@ static List<Artifact<GradleArtifactType>> filterArtifacts(final List<Artifact<Gr
artifacts.remove(artifact.withType(GradleArtifactType.TEST_RUNTIME_CLASSPATH));
});
artifacts.stream()
.filter(artifact -> GradleArtifactType.TEST_COMPILE_CLASSPATH == artifact.getType())
.filter(
artifact ->
GradleArtifactType.TEST_COMPILE_CLASSPATH == artifact.getType()
)
.collect(Collectors.toUnmodifiableList())
.forEach(artifact -> artifacts.remove(artifact.withType(GradleArtifactType.TEST_RUNTIME_CLASSPATH)));
.forEach(
artifact -> artifacts.remove(
artifact.withType(GradleArtifactType.TEST_RUNTIME_CLASSPATH)
)
);
return artifacts.stream()
.map(artifact -> {
switch (artifact.getType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,28 +132,40 @@ public Build resolve(final BuildFile file) {
final var effectiveRepositories = extractRepositories(effectiveModel);
logger.debug("Extracted effective repositories:");
effectiveRepositories.forEach(
effectiveRepository -> logger.debug("- {}", () -> ToString.toString(effectiveRepository))
effectiveRepository -> logger.debug(
"- {}",
() -> ToString.toString(effectiveRepository)
)
);

logger.trace("Extracting effective artifacts");
final var effectiveArtifacts = extractArtifacts(effectiveModel);
logger.debug("Extracted effective artifacts:");
effectiveArtifacts.forEach(
effectiveArtifact -> logger.debug("- {}", () -> ToString.toString(effectiveArtifact))
effectiveArtifact -> logger.debug(
"- {}",
() -> ToString.toString(effectiveArtifact)
)
);

logger.trace("Extracting raw artifacts");
final var rawArtifacts = extractArtifacts(rawModel);
logger.debug("Extracted raw artifacts:");
rawArtifacts.forEach(
rawArtifact -> logger.debug("- {}", () -> ToString.toString(rawArtifact))
rawArtifact -> logger.debug(
"- {}",
() -> ToString.toString(rawArtifact)
)
);

logger.trace("Inheriting artifacts");
final var inheritedArtifacts = inherit(rawArtifacts, effectiveArtifacts);
logger.debug("Inherited artifacts:");
inheritedArtifacts.forEach(
inheritedArtifact -> logger.debug("- {}", () -> ToString.toString(inheritedArtifact))
inheritedArtifact -> logger.debug(
"- {}",
() -> ToString.toString(inheritedArtifact)
)
);

return new Build(file, effectiveRepositories, inheritedArtifacts);
Expand Down Expand Up @@ -299,21 +311,22 @@ private static List<Artifact<MavenArtifactType>> inherit(
final var rawArtifactOptionalVersion = rawArtifact.getOptionalVersion();
return effectiveArtifacts.stream()
.filter(
effectiveArtifact -> rawArtifactType == effectiveArtifact.getType()
&& rawArtifactIdentifier.equals(effectiveArtifact.getIdentifier())
effectiveArtifact ->
rawArtifactType == effectiveArtifact.getType()
&& rawArtifactIdentifier.equals(effectiveArtifact.getIdentifier())
)
.sorted(
Comparator.comparing(
effectiveArtifact -> rawArtifactOptionalVersion.equals(
effectiveArtifact.getOptionalVersion()
),
effectiveArtifact ->
rawArtifactOptionalVersion.equals(
effectiveArtifact.getOptionalVersion()
),
Comparator.reverseOrder()
)
)
.map(
effectiveArtifact -> effectiveArtifact.withVersionInherited(
rawArtifactOptionalVersion.isEmpty()
)
effectiveArtifact ->
effectiveArtifact.withVersionInherited(rawArtifactOptionalVersion.isEmpty())
)
.findAny()
.orElse(rawArtifact);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ void testFindArtifactUpdateVersionsDeprecated1() throws IOException {
.when(
mockedArtifactAvailableVersionsResolver.resolve(
Mockito.argThat(
artifact -> null != artifact && fooIdentifier.equals(artifact.getIdentifier())
artifact -> null != artifact
&& fooIdentifier.equals(artifact.getIdentifier())
),
Mockito.notNull()
)
Expand All @@ -382,7 +383,8 @@ void testFindArtifactUpdateVersionsDeprecated1() throws IOException {
.when(
mockedArtifactAvailableVersionsResolver.resolve(
Mockito.argThat(
artifact -> null != artifact && barIdentifier.equals(artifact.getIdentifier())
artifact -> null != artifact
&& barIdentifier.equals(artifact.getIdentifier())
),
Mockito.notNull()
)
Expand Down Expand Up @@ -432,7 +434,8 @@ void testFindArtifactUpdateVersionsDeprecated2() throws IOException {
.when(
mockedArtifactAvailableVersionsResolver.resolve(
Mockito.argThat(
artifact -> null != artifact && fooIdentifier.equals(artifact.getIdentifier())
artifact -> null != artifact
&& fooIdentifier.equals(artifact.getIdentifier())
),
Mockito.notNull()
)
Expand All @@ -447,7 +450,8 @@ void testFindArtifactUpdateVersionsDeprecated2() throws IOException {
.when(
mockedArtifactAvailableVersionsResolver.resolve(
Mockito.argThat(
artifact -> null != artifact && barIdentifier.equals(artifact.getIdentifier())
artifact -> null != artifact
&& barIdentifier.equals(artifact.getIdentifier())
),
Mockito.notNull()
)
Expand Down Expand Up @@ -517,7 +521,8 @@ void testFindArtifactUpdateVersions() throws IOException {
.when(
mockedArtifactAvailableVersionsResolver.resolve(
Mockito.argThat(
artifact -> null != artifact && fooIdentifier.equals(artifact.getIdentifier())
artifact -> null != artifact
&& fooIdentifier.equals(artifact.getIdentifier())
),
Mockito.notNull()
)
Expand All @@ -532,7 +537,8 @@ void testFindArtifactUpdateVersions() throws IOException {
.when(
mockedArtifactAvailableVersionsResolver.resolve(
Mockito.argThat(
artifact -> null != artifact && barIdentifier.equals(artifact.getIdentifier())
artifact -> null != artifact
&& barIdentifier.equals(artifact.getIdentifier())
),
Mockito.notNull()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,27 @@ void testConstructorInvalid() {
void testEqualsAndHashCodeAndToString() {
assertThat(artifactIdentifier.equals(artifactIdentifier)).isTrue();
assertThat(artifactIdentifier).isNotEqualTo(new Object());
assertThat(new ArtifactIdentifier(GROUP_ID, ARTIFACT_ID)).satisfies(otherArtifactIdentifier -> {
assertThat(otherArtifactIdentifier).isNotSameAs(artifactIdentifier);
assertThat(otherArtifactIdentifier).isEqualTo(artifactIdentifier);
assertThat(otherArtifactIdentifier).hasSameHashCodeAs(artifactIdentifier);
assertThat(otherArtifactIdentifier).hasToString(artifactIdentifier.toString());
});
assertThat(new ArtifactIdentifier(OTHER_GROUP_ID, ARTIFACT_ID)).satisfies(otherArtifactIdentifier -> {
assertThat(otherArtifactIdentifier).isNotSameAs(artifactIdentifier);
assertThat(otherArtifactIdentifier).isNotEqualTo(artifactIdentifier);
assertThat(otherArtifactIdentifier).doesNotHaveSameHashCodeAs(artifactIdentifier);
assertThat(otherArtifactIdentifier).doesNotHaveToString(artifactIdentifier.toString());
});
assertThat(new ArtifactIdentifier(GROUP_ID, OTHER_ARTIFACT_ID)).satisfies(otherArtifactIdentifier -> {
assertThat(otherArtifactIdentifier).isNotSameAs(artifactIdentifier);
assertThat(otherArtifactIdentifier).isNotEqualTo(artifactIdentifier);
assertThat(otherArtifactIdentifier).doesNotHaveSameHashCodeAs(artifactIdentifier);
assertThat(otherArtifactIdentifier).doesNotHaveToString(artifactIdentifier.toString());
});
assertThat(new ArtifactIdentifier(GROUP_ID, ARTIFACT_ID))
.satisfies(otherArtifactIdentifier -> {
assertThat(otherArtifactIdentifier).isNotSameAs(artifactIdentifier);
assertThat(otherArtifactIdentifier).isEqualTo(artifactIdentifier);
assertThat(otherArtifactIdentifier).hasSameHashCodeAs(artifactIdentifier);
assertThat(otherArtifactIdentifier).hasToString(artifactIdentifier.toString());
});
assertThat(new ArtifactIdentifier(OTHER_GROUP_ID, ARTIFACT_ID))
.satisfies(otherArtifactIdentifier -> {
assertThat(otherArtifactIdentifier).isNotSameAs(artifactIdentifier);
assertThat(otherArtifactIdentifier).isNotEqualTo(artifactIdentifier);
assertThat(otherArtifactIdentifier).doesNotHaveSameHashCodeAs(artifactIdentifier);
assertThat(otherArtifactIdentifier).doesNotHaveToString(artifactIdentifier.toString());
});
assertThat(new ArtifactIdentifier(GROUP_ID, OTHER_ARTIFACT_ID))
.satisfies(otherArtifactIdentifier -> {
assertThat(otherArtifactIdentifier).isNotSameAs(artifactIdentifier);
assertThat(otherArtifactIdentifier).isNotEqualTo(artifactIdentifier);
assertThat(otherArtifactIdentifier).doesNotHaveSameHashCodeAs(artifactIdentifier);
assertThat(otherArtifactIdentifier).doesNotHaveToString(artifactIdentifier.toString());
});
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,54 +125,61 @@ void testWithTypeInvalid() {

@Test
void testWithVersionInherited() {
assertThat(artifact.withVersionInherited(VERSION_INHERITED)).satisfies(otherArtifact -> {
assertThat(otherArtifact.getType()).isEqualTo(artifact.getType());
assertThat(otherArtifact.getIdentifier()).isEqualTo(artifact.getIdentifier());
assertThat(otherArtifact.getOptionalVersion()).isEqualTo(artifact.getOptionalVersion());
assertThat(otherArtifact.isVersionInherited()).isEqualTo(VERSION_INHERITED);
});
assertThat(artifact.withVersionInherited(OTHER_VERSION_INHERITED)).satisfies(otherArtifact -> {
assertThat(otherArtifact.getType()).isEqualTo(artifact.getType());
assertThat(otherArtifact.getIdentifier()).isEqualTo(artifact.getIdentifier());
assertThat(otherArtifact.getOptionalVersion()).isEqualTo(artifact.getOptionalVersion());
assertThat(otherArtifact.isVersionInherited()).isEqualTo(OTHER_VERSION_INHERITED);
});
assertThat(artifact.withVersionInherited(VERSION_INHERITED))
.satisfies(otherArtifact -> {
assertThat(otherArtifact.getType()).isEqualTo(artifact.getType());
assertThat(otherArtifact.getIdentifier()).isEqualTo(artifact.getIdentifier());
assertThat(otherArtifact.getOptionalVersion()).isEqualTo(artifact.getOptionalVersion());
assertThat(otherArtifact.isVersionInherited()).isEqualTo(VERSION_INHERITED);
});
assertThat(artifact.withVersionInherited(OTHER_VERSION_INHERITED))
.satisfies(otherArtifact -> {
assertThat(otherArtifact.getType()).isEqualTo(artifact.getType());
assertThat(otherArtifact.getIdentifier()).isEqualTo(artifact.getIdentifier());
assertThat(otherArtifact.getOptionalVersion()).isEqualTo(artifact.getOptionalVersion());
assertThat(otherArtifact.isVersionInherited()).isEqualTo(OTHER_VERSION_INHERITED);
});
}

@Test
void testEqualsAndHashCodeAndToString() {
assertThat(artifact.equals(artifact)).isTrue();
assertThat(artifact).isNotEqualTo(new Object());
assertThat(new Artifact<>(TYPE, IDENTIFIER, VERSION, VERSION_INHERITED)).satisfies(otherArtifact -> {
assertThat(otherArtifact).isNotSameAs(artifact);
assertThat(otherArtifact).isEqualTo(artifact);
assertThat(otherArtifact).hasSameHashCodeAs(artifact);
assertThat(otherArtifact).hasToString(artifact.toString());
});
assertThat(new Artifact<>(OTHER_TYPE, IDENTIFIER, VERSION, VERSION_INHERITED)).satisfies(otherArtifact -> {
assertThat(otherArtifact).isNotSameAs(artifact);
assertThat(otherArtifact).isNotEqualTo(artifact);
assertThat(otherArtifact).doesNotHaveSameHashCodeAs(artifact);
assertThat(otherArtifact).doesNotHaveToString(artifact.toString());
});
assertThat(new Artifact<>(TYPE, OTHER_IDENTIFIER, VERSION, VERSION_INHERITED)).satisfies(otherArtifact -> {
assertThat(otherArtifact).isNotSameAs(artifact);
assertThat(otherArtifact).isNotEqualTo(artifact);
assertThat(otherArtifact).doesNotHaveSameHashCodeAs(artifact);
assertThat(otherArtifact).doesNotHaveToString(artifact.toString());
});
assertThat(new Artifact<>(TYPE, IDENTIFIER, OTHER_VERSION, VERSION_INHERITED)).satisfies(otherArtifact -> {
assertThat(otherArtifact).isNotSameAs(artifact);
assertThat(otherArtifact).isNotEqualTo(artifact);
assertThat(otherArtifact).doesNotHaveSameHashCodeAs(artifact);
assertThat(otherArtifact).doesNotHaveToString(artifact.toString());
});
assertThat(new Artifact<>(TYPE, IDENTIFIER, VERSION, OTHER_VERSION_INHERITED)).satisfies(otherArtifact -> {
assertThat(otherArtifact).isNotSameAs(artifact);
assertThat(otherArtifact).isNotEqualTo(artifact);
assertThat(otherArtifact).doesNotHaveSameHashCodeAs(artifact);
assertThat(otherArtifact).doesNotHaveToString(artifact.toString());
});
assertThat(new Artifact<>(TYPE, IDENTIFIER, VERSION, VERSION_INHERITED))
.satisfies(otherArtifact -> {
assertThat(otherArtifact).isNotSameAs(artifact);
assertThat(otherArtifact).isEqualTo(artifact);
assertThat(otherArtifact).hasSameHashCodeAs(artifact);
assertThat(otherArtifact).hasToString(artifact.toString());
});
assertThat(new Artifact<>(OTHER_TYPE, IDENTIFIER, VERSION, VERSION_INHERITED))
.satisfies(otherArtifact -> {
assertThat(otherArtifact).isNotSameAs(artifact);
assertThat(otherArtifact).isNotEqualTo(artifact);
assertThat(otherArtifact).doesNotHaveSameHashCodeAs(artifact);
assertThat(otherArtifact).doesNotHaveToString(artifact.toString());
});
assertThat(new Artifact<>(TYPE, OTHER_IDENTIFIER, VERSION, VERSION_INHERITED))
.satisfies(otherArtifact -> {
assertThat(otherArtifact).isNotSameAs(artifact);
assertThat(otherArtifact).isNotEqualTo(artifact);
assertThat(otherArtifact).doesNotHaveSameHashCodeAs(artifact);
assertThat(otherArtifact).doesNotHaveToString(artifact.toString());
});
assertThat(new Artifact<>(TYPE, IDENTIFIER, OTHER_VERSION, VERSION_INHERITED))
.satisfies(otherArtifact -> {
assertThat(otherArtifact).isNotSameAs(artifact);
assertThat(otherArtifact).isNotEqualTo(artifact);
assertThat(otherArtifact).doesNotHaveSameHashCodeAs(artifact);
assertThat(otherArtifact).doesNotHaveToString(artifact.toString());
});
assertThat(new Artifact<>(TYPE, IDENTIFIER, VERSION, OTHER_VERSION_INHERITED))
.satisfies(otherArtifact -> {
assertThat(otherArtifact).isNotSameAs(artifact);
assertThat(otherArtifact).isNotEqualTo(artifact);
assertThat(otherArtifact).doesNotHaveSameHashCodeAs(artifact);
assertThat(otherArtifact).doesNotHaveToString(artifact.toString());
});
}

@Test
Expand Down
Loading

0 comments on commit 11d7a39

Please sign in to comment.