Skip to content
Draft
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
16 changes: 8 additions & 8 deletions src/main/java/com/solubris/enforcer/VersionPropertyRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
@SuppressWarnings("unused")
@Inject
public VersionPropertyRule(MavenSession session) {
this(modelFrom(session), session.getCurrentProject().getModel());
}

Check warning on line 50 in src/main/java/com/solubris/enforcer/VersionPropertyRule.java

View workflow job for this annotation

GitHub Actions / Autograding GitHub Action

Not covered lines

Lines 49-50 are not covered by tests

protected VersionPropertyRule(Model originalModel, Model effectiveModel) {
this.originalModel = originalModel;
Expand All @@ -56,8 +56,8 @@

@Override
public void execute() throws EnforcerRuleException {
throwViolations(scan(), "Found {0} version violation(s):");
}

Check warning on line 60 in src/main/java/com/solubris/enforcer/VersionPropertyRule.java

View workflow job for this annotation

GitHub Actions / Autograding GitHub Action

Not covered lines

Lines 59-60 are not covered by tests

@SuppressWarnings("PMD.CollapsibleIfStatements")
protected Stream<String> scan() {
Expand All @@ -65,8 +65,8 @@
.collect(groupingBy(Artifact::fullKey, toList()));
Map<String, List<Artifact>> usages = scanModel(originalModel)
.map(a -> a.withEffectiveVersion(fetchVersion(a, byKey)))
.filter(a -> a.getEffectiveVersion() != null)

Check warning on line 68 in src/main/java/com/solubris/enforcer/VersionPropertyRule.java

View workflow job for this annotation

GitHub Actions / Autograding GitHub Action

Partially covered line

Line 68 is only partially covered, one branch is missing
.filter(artifact -> !isExcluded(artifact.getVersion()))

Check warning on line 69 in src/main/java/com/solubris/enforcer/VersionPropertyRule.java

View workflow job for this annotation

GitHub Actions / Autograding GitHub Action

Partially covered line

Line 69 is only partially covered, one branch is missing
.collect(groupingBy(Artifact::getEffectiveVersion, toList()));

// byVersion maps from property to list of artifacts
Expand All @@ -76,14 +76,14 @@
.map(e -> {
String effectiveVersion = e.getKey();
List<Artifact> artifacts = e.getValue();
long propertyCount = artifacts.stream()
long implicitVersionCount = artifacts.stream()
.filter(Artifact::hasImplicitVersion)
.count();
if (artifacts.size() > 1) {
if (propertyCount == 0) return missingProperty(effectiveVersion, artifacts);
if (propertyCount < artifacts.size()) return unusedProperty(effectiveVersion, artifacts);
if (implicitVersionCount == 0) return missingProperty(effectiveVersion, artifacts);
if (implicitVersionCount < artifacts.size()) return unusedProperty(effectiveVersion, artifacts);

Check warning on line 84 in src/main/java/com/solubris/enforcer/VersionPropertyRule.java

View workflow job for this annotation

GitHub Actions / Autograding GitHub Action

Partially covered line

Line 84 is only partially covered, one branch is missing
} else if (artifacts.size() == 1) {

Check warning on line 85 in src/main/java/com/solubris/enforcer/VersionPropertyRule.java

View workflow job for this annotation

GitHub Actions / Autograding GitHub Action

Partially covered line

Line 85 is only partially covered, one branch is missing
if (propertyCount == 1) return redundantProperty(artifacts.get(0));
if (implicitVersionCount == 1) return redundantProperty(artifacts.get(0));

Check warning

Code scanning / SonarQube

Merge this if statement with the enclosing one.

Merge this if statement with the enclosing one.
}
return null;
}).filter(Objects::nonNull);
Expand All @@ -91,9 +91,9 @@

private String fetchVersion(Artifact a, Map<String, List<Artifact>> byKey) {
List<Artifact> artifacts = byKey.getOrDefault(a.fullKey(), List.of());
if (!artifacts.isEmpty()) return artifacts.get(0).getVersion();

Check warning on line 94 in src/main/java/com/solubris/enforcer/VersionPropertyRule.java

View workflow job for this annotation

GitHub Actions / Autograding GitHub Action

Partially covered line

Line 94 is only partially covered, one branch is missing
getLog().warn("Could not find artifact in effectiveModel for " + a.fullKey());
return null;

Check warning on line 96 in src/main/java/com/solubris/enforcer/VersionPropertyRule.java

View workflow job for this annotation

GitHub Actions / Autograding GitHub Action

Not covered lines

Lines 95-96 are not covered by tests
}

private String redundantProperty(Artifact artifact) {
Expand All @@ -114,31 +114,31 @@
* That's possible due to coincidental properties - another edge case to consider.
*/
private static String unusedProperty(String effectiveVersion, List<Artifact> artifacts) {
String unused = artifacts.stream()
String explicitVersionLocations = artifacts.stream()
.filter(Artifact::hasExplicitVersion)
.map(Artifact::key)
.collect(joining(", "));
String propertyName = artifacts.stream()
.filter(Artifact::hasImplicitVersion)
.map(Artifact::getVersion)
.map(PropertyUtil::fromPlaceHolder)
.findAny().orElse("unknown");
return String.format(

Check warning on line 126 in src/main/java/com/solubris/enforcer/VersionPropertyRule.java

View workflow job for this annotation

GitHub Actions / Autograding GitHub Action

Not covered lines

Lines 117-126 are not covered by tests
"Version property %s=%s exists but is not used everywhere. Unused locations: %s",
propertyName, effectiveVersion, unused);
propertyName, effectiveVersion, explicitVersionLocations);
}

private String missingProperty(String version, List<Artifact> artifacts) {
if (!requirePropertiesForDuplicates) return null;

String unused = artifacts.stream()
String locationsWithoutProperty = artifacts.stream()
.filter(a -> Objects.equals(a.getVersion(), a.getEffectiveVersion()))
.map(Artifact::key)
.collect(joining(", "));
return String.format(
"Version '%s' exists in multiple locations, please extract a version property. " +
"Unused locations: %s",
version, unused);
version, locationsWithoutProperty);
}

/**
Expand All @@ -149,11 +149,11 @@
*/
@SuppressWarnings("checkstyle:InvertedControlFlow")
private boolean isExcluded(String version) {
if (version == null) return true;

Check warning on line 152 in src/main/java/com/solubris/enforcer/VersionPropertyRule.java

View workflow job for this annotation

GitHub Actions / Autograding GitHub Action

Partially covered line

Line 152 is only partially covered, one branch is missing
if (excludeVersions.contains(version)) return true;

Check warning on line 153 in src/main/java/com/solubris/enforcer/VersionPropertyRule.java

View workflow job for this annotation

GitHub Actions / Autograding GitHub Action

Partially covered line

Line 153 is only partially covered, one branch is missing

if (ignoreParentVersion && Objects.equals(version, "${project.parent.version}")) {

Check warning on line 155 in src/main/java/com/solubris/enforcer/VersionPropertyRule.java

View workflow job for this annotation

GitHub Actions / Autograding GitHub Action

Partially covered line

Line 155 is only partially covered, 2 branches are missing
return true;

Check warning on line 156 in src/main/java/com/solubris/enforcer/VersionPropertyRule.java

View workflow job for this annotation

GitHub Actions / Autograding GitHub Action

Not covered line

Line 156 is not covered by tests
}

// Match version ranges like [1.0,2.0) or (1.0,2.0]
Expand Down
Loading