Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use BaseVersion for artifacts version checking - fix #499 #534

Merged
merged 1 commit into from
Apr 11, 2024
Merged
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 @@ -18,6 +18,7 @@
import java.util.Locale;

import lombok.Getter;
import lombok.ToString;
import org.apache.maven.artifact.Artifact;

/**
Expand All @@ -28,6 +29,7 @@
* @author Slawomir Jaranowski.
*/
@Getter
@ToString
class ArtifactData {

private final String groupId;
Expand All @@ -39,6 +41,6 @@ class ArtifactData {
groupId = artifact.getGroupId().toLowerCase(Locale.US);
artifactId = artifact.getArtifactId().toLowerCase(Locale.US);
type = artifact.getType().toLowerCase(Locale.US);
version = artifact.getVersion().toLowerCase(Locale.US);
version = artifact.getBaseVersion().toLowerCase(Locale.US);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.regex.PatternSyntaxException;

import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
Expand All @@ -32,24 +33,32 @@
* @author Slawomir Jaranowski.
*/
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@ToString(onlyExplicitlyIncluded = true)
class ArtifactPattern {

private static final Pattern DOT_REPLACE = Pattern.compile("\\.");

private static final Pattern STAR_REPLACE = Pattern.compile("\\*");

private static final Pattern PACKAGING = Pattern.compile("^[a-zA-Z]+$");

private static final String VERSION_REGEX_PREFIX = "~";

private static final String NOT_VERSION_REGEX_PREFIX = "!~";

/**
* Original pattern from keysMap. Used to compare if object is equal to another.
*/
@EqualsAndHashCode.Include
@ToString.Include
private final String pattern;

private final Pattern groupIdPattern;

private final Pattern artifactIdPattern;

private final Pattern packagingPattern;

private final Function<String, Boolean> versionMatch;

public ArtifactPattern(String pattern) {
Expand All @@ -76,7 +85,6 @@ public ArtifactPattern(String pattern) {
packaging = "";
}


try {
groupIdPattern = Pattern.compile(patternPrepare(groupId));
artifactIdPattern = Pattern.compile(patternPrepare(artifactId));
Expand Down Expand Up @@ -155,6 +163,12 @@ private static String versionSpecPrepare(String versionSpec) throws InvalidVersi
return versionSpec;
}

/**
* Check if given artifact match a pattern.
*
* @param artifact an artifact to test
* @return result of verification
*/
public boolean isMatch(ArtifactData artifact) {

return isMatchPattern(groupIdPattern, artifact.getGroupId())
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/simplify4u/plugins/keysmap/KeyItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Arrays;
import java.util.List;

import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.simplify4u.plugins.pgp.KeyInfo;

Expand All @@ -28,6 +29,7 @@
* @author Slawomir Jaranowski.
*/
@Slf4j
@ToString
class KeyItems {

private final List<KeyItem> keys = new ArrayList<>();
Expand Down Expand Up @@ -100,6 +102,11 @@ private void addKey(KeyItem keyItem, KeysMapContext keysMapContext) {
}
}

/**
* Check if key match
* @param keyInfo a key to test
* @return a result
*/
public boolean isKeyMatch(KeyInfo keyInfo) {
return keys.stream().anyMatch(keyInfoItem -> keyInfoItem.isKeyMatch(keyInfo));
}
Expand Down
32 changes: 27 additions & 5 deletions src/test/java/org/simplify4u/plugins/keysmap/KeysMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import static org.simplify4u.plugins.TestUtils.aKeyInfo;
import static org.simplify4u.plugins.TestUtils.aKeysMapLocationConfig;

import org.apache.maven.artifact.Artifact;
import org.codehaus.plexus.resource.ResourceManager;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand Down Expand Up @@ -235,16 +236,18 @@ void properLogShouldBeGeneratedForProcessingItems() throws Exception {

keysMap.load(aKeysMapLocationConfig("/keysMap.list"));

assertThat(keysMap.size()).isEqualTo(10);
assertThat(keysMap.size()).isEqualTo(11);

verify(loggerKeysMap)
.debug(eq("Existing artifact pattern: {} - only update key items in {}"), anyString(), any(KeysMapContext.class));
.debug(eq("Existing artifact pattern: {} - only update key items in {}"), anyString(),
any(KeysMapContext.class));
verifyNoMoreInteractions(loggerKeysMap);

verify(loggerKeyItems, times(3))
.warn(eq("Duplicate key item: {} in: {}"), any(KeyItem.class), any(KeysMapContext.class));
verify(loggerKeyItems)
.warn(eq("Empty value for key is deprecated - please provide some value - now assume as noSig in: {}"), any(KeysMapContext.class));
.warn(eq("Empty value for key is deprecated - please provide some value - now assume as noSig in: {}"),
any(KeysMapContext.class));
verifyNoMoreInteractions(loggerKeyItems);
}

Expand All @@ -260,7 +263,7 @@ void onlyIncludedItemsFromMapByValue() throws Exception {

keysMap.load(config);

assertThat(keysMap.size()).isEqualTo(3);
assertThat(keysMap.size()).isEqualTo(4);

assertThat(keysMap.isNoSignature(testArtifact().groupId("noSig").artifactId("test3").build())).isTrue();
assertThat(keysMap.isKeyMissing(testArtifact().groupId("noKey").build())).isFalse();
Expand Down Expand Up @@ -301,10 +304,29 @@ void excludeItemsFromMap() throws Exception {

keysMap.load(config);

assertThat(keysMap.size()).isEqualTo(9);
assertThat(keysMap.size()).isEqualTo(10);

assertThat(keysMap.isNoSignature(testArtifact().groupId("noSig").artifactId("test2").build())).isFalse();
assertThat(keysMap.isKeyMissing(testArtifact().groupId("noKey").build())).isTrue();
}

@Test
void baseVersionFromArtifactShouldBeUsed() throws Exception {
doAnswer(invocation -> getClass().getResourceAsStream(invocation.getArgument(0)))
.when(resourceManager).getResourceAsInputStream(anyString());

keysMap.load(aKeysMapLocationConfig("/keysMap.list"));

Artifact snapshot = testArtifact()
.groupId("noSig")
.artifactId("test-snapshot")
.version("1.20.1-R0.1-20240410.215944-178")
.build();

assertThat(snapshot.isSnapshot()).isTrue();
assertThat(snapshot.getBaseVersion()).isEqualTo("1.20.1-R0.1-SNAPSHOT");

assertThat(keysMap.isNoSignature(snapshot)).isTrue();
}

}
2 changes: 2 additions & 0 deletions src/test/resources/keysMap.list
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ noKey:test = noKey

# duplicate artifact pattern with duplicate value
noKey:test = noKey

noSig:test-snapshot:1.20.1-R0.1-SNAPSHOT = noSig