Skip to content

Commit

Permalink
Add packaging to GAV in key map
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Mar 17, 2020
1 parent c1a4750 commit 05dec12
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 7 deletions.
19 changes: 19 additions & 0 deletions src/it/sigOkKeysMapWithPackage/keysmap.list
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#
# Copyright 2020 Slawomir Jaranowski
#
# Licensed 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.
#

commons-chain:commons-chain:jar:1.2 = 0xD196A5E3E70732EEB2E5007F1861C322C56014B2
commons-chain:commons-chain:pom:1.2 = 0x6E13156C0EE653F0B984663AB95BBD3FA43C4492

59 changes: 59 additions & 0 deletions src/it/sigOkKeysMapWithPackage/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2020 Slawomir Jaranowski
~
~ Licensed 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>test</groupId>
<artifactId>it-test-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/>
</parent>

<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<dependencies>
<dependency>
<groupId>commons-chain</groupId>
<artifactId>commons-chain</artifactId>
<version>1.2</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.simplify4u.plugins</groupId>
<artifactId>pgpverify-maven-plugin</artifactId>
<version>@project.version@</version>
<configuration>
<keysMapLocation>${project.basedir}/keysmap.list</keysMapLocation>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
23 changes: 23 additions & 0 deletions src/it/sigOkKeysMapWithPackage/postbuild.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2020 Slawomir Jaranowski
*
* Licensed 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.
*/
def buildLog = new File( basedir, 'build.log' )

assert buildLog.text.contains('[INFO] commons-chain:commons-chain:jar:1.2 PGP Signature OK')
assert buildLog.text.contains('KeyId: 0xD196A5E3E70732EEB2E5007F1861C322C56014B2')
assert buildLog.text.contains('[INFO] commons-chain:commons-chain:pom:1.2 PGP Signature OK')
assert buildLog.text.contains('KeyId: 0x6E13156C0EE653F0B984663AB95BBD3FA43C4492')

assert buildLog.text.contains('[INFO] BUILD SUCCESS')
32 changes: 29 additions & 3 deletions src/main/java/org/simplify4u/plugins/ArtifactInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,41 @@ public class ArtifactInfo {

private final Pattern groupIdPattern;
private final Pattern artifactIdPattern;
private final Pattern packagingPattern;
private final VersionRange versionRange;

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]+$");

public ArtifactInfo(String strArtifact, KeyInfo keyInfo) {

String[] split = strArtifact.split(":");
String groupId = split.length > 0 ? split[0].trim().toLowerCase(Locale.US) : "";
String artifactId = split.length > 1 ? split[1].trim().toLowerCase(Locale.US) : "";
String version = split.length > 2 ? split[2].trim().toLowerCase(Locale.US) : "";

String packaging = "";
String version = "";

if (split.length == 3) {
String item = split[2].trim().toLowerCase(Locale.US);
if (PACKAGING.matcher(item).matches()) {
packaging = item;
} else {
version = item;
}
} else if (split.length == 4) {
packaging = split[2].trim().toLowerCase(Locale.US);
version = split[3].trim().toLowerCase(Locale.US);
} else {
packaging = "";
}


try {
groupIdPattern = Pattern.compile(patternPrepare(groupId));
artifactIdPattern = Pattern.compile(patternPrepare(artifactId));
packagingPattern = Pattern.compile(patternPrepare(packaging));
versionRange = VersionRange.createFromVersionSpec(versionSpecPrepare(version));
} catch (InvalidVersionSpecificationException | PatternSyntaxException e) {
throw new IllegalArgumentException("Invalid artifact definition: " + strArtifact, e);
Expand All @@ -64,8 +84,13 @@ private String patternPrepare(String str) {
return ".*";
}

String ret = DOT_REPLACE.matcher(str).replaceAll("\\\\.");
ret = STAR_REPLACE.matcher(ret).replaceAll(".*");
String ret;
if (str.endsWith(".*")) {
ret = str.substring(0, str.length() - 2) + "(\\..+)?$";
} else {
ret = DOT_REPLACE.matcher(str).replaceAll("\\\\.");
ret = STAR_REPLACE.matcher(ret).replaceAll(".*");
}
return ret;
}

Expand All @@ -87,6 +112,7 @@ public boolean isMatch(Artifact artifact) {

return isMatchPattern(groupIdPattern, artifact.getGroupId())
&& isMatchPattern(artifactIdPattern, artifact.getArtifactId())
&& isMatchPattern(packagingPattern, artifact.getType())
&& isMatchVersion(artifact.getVersion());
}

Expand Down
18 changes: 14 additions & 4 deletions src/test/java/org/simplify4u/plugins/ArtifactInfoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package org.simplify4u.plugins;

import static org.assertj.core.api.Assertions.assertThat;
import static org.simplify4u.plugins.TestArtifactBuilder.testArtifact;
import static org.testng.Assert.assertTrue;

import org.apache.maven.artifact.Artifact;
import org.testng.annotations.DataProvider;
Expand All @@ -34,11 +34,21 @@ public Object[][] artifactsList() {
return new Object[][]{
{"test.group:test:*", testArtifact().build(), true},
{"test.group:test:1.1.1", testArtifact().build(), true},
{"test.group:test:jar:1.1.1", testArtifact().build(), true},
{"test.group:test:pom:1.1.1", testArtifact().build(), false},
{"test.group:test:[1.1,2.0)", testArtifact().build(), true},
{"test.group:test:pom:[1.1,2.0)", testArtifact().build(), false},
{"test.group:test:[1.1,2.0)", testArtifact().version("2.0").build(), false},
{"test.group:test:1.1.1", testArtifact().version("1.1.2").build(), false},
{"test.group:test", testArtifact().build(), true},
{"test.group:test", testArtifact().build(), true},
{"test.group:*:jar", testArtifact().artifactId("test2").build(), true},
{"test.group:*:pom", testArtifact().artifactId("test2").build(), false},
{"test.group.*:test", testArtifact().build(), true},
{"test.group.*:test", testArtifact().groupId("test.group.next").build(), true},
{"test.group.*:test", testArtifact().groupId("test.groupnext").build(), false},
{"test.group:test", testArtifact().packaging("pom").build(), true},
{"test.group:test:pom", testArtifact().packaging("pom").build(), true},
{"test.group:test:jar", testArtifact().packaging("pom").build(), false},
{"test.*:test", testArtifact().build(), true},
{"test.*", testArtifact().build(), true},
};
Expand All @@ -48,8 +58,8 @@ public Object[][] artifactsList() {
public void testMatchArtifact(String pattern, Artifact artifact, boolean match) {

ArtifactInfo artifactInfo = new ArtifactInfo(pattern, ANY_KEY);
assertTrue(artifactInfo.isMatch(artifact) == match);
assertTrue(artifactInfo.isKeyMatch(null, null));
assertThat(artifactInfo.isMatch(artifact)).isEqualTo(match);
assertThat(artifactInfo.isKeyMatch(null, null)).isTrue();
}

@Test(expectedExceptions = IllegalArgumentException.class,
Expand Down

0 comments on commit 05dec12

Please sign in to comment.