From af0bc31fbb6f8084d1aeb1eff45530414db1fce9 Mon Sep 17 00:00:00 2001 From: Alexander Morozov Date: Tue, 30 Sep 2014 14:53:42 +0700 Subject: Exclude IDEA files. --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index b6cd1d6..27cc7a3 100755 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ target/ .classpath .project .settings +.idea/ +*.iml \ No newline at end of file -- cgit v1.2.3 From a94794c432801d0dded3ee0591d1b40ef45a5788 Mon Sep 17 00:00:00 2001 From: Alexander Morozov Date: Wed, 1 Oct 2014 13:03:10 +0700 Subject: Fix testing for snapshot regression. --- api/src/main/java/org/semver/Version.java | 2 +- api/src/test/java/org/semver/VersionTest.java | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/org/semver/Version.java b/api/src/main/java/org/semver/Version.java index 4f73e4f..4cb54db 100755 --- a/api/src/main/java/org/semver/Version.java +++ b/api/src/main/java/org/semver/Version.java @@ -46,7 +46,7 @@ public final class Version implements Comparable { private static final Pattern DIGITS_ONLY = Pattern.compile("\\d+"); - private static final String SNAPSHOT_VERSION_SUFFIX = "-SNAPSHOT"; + private static final String SNAPSHOT_VERSION_SUFFIX = "SNAPSHOT"; private final int major; private final int minor; diff --git a/api/src/test/java/org/semver/VersionTest.java b/api/src/test/java/org/semver/VersionTest.java index bdefa5a..6fbcf47 100755 --- a/api/src/test/java/org/semver/VersionTest.java +++ b/api/src/test/java/org/semver/VersionTest.java @@ -96,6 +96,11 @@ public class VersionTest { Assert.assertFalse(Version.parse("0.1.1").isStable()); } + @Test + public void shouldBeSnapshotVersion() { + Assert.assertTrue(Version.parse("1.5.30-SNAPSHOT").isSnapshot()); + } + @Test public void isNewer() { Assert.assertTrue(Version.parse("3.2.3").compareTo(Version.parse("3.2-M1-SNAPSHOT")) > 0); -- cgit v1.2.3 From b39561c4ade1279eeae42156c741c92529bb2d80 Mon Sep 17 00:00:00 2001 From: Alexander Morozov Date: Wed, 1 Oct 2014 13:23:29 +0700 Subject: Make code more readable. --- .../org/semver/enforcer/AbstractEnforcerRule.java | 78 ++++++++++++---------- 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/enforcer-rule/src/main/java/org/semver/enforcer/AbstractEnforcerRule.java b/enforcer-rule/src/main/java/org/semver/enforcer/AbstractEnforcerRule.java index 38c2517..098a0e7 100755 --- a/enforcer-rule/src/main/java/org/semver/enforcer/AbstractEnforcerRule.java +++ b/enforcer-rule/src/main/java/org/semver/enforcer/AbstractEnforcerRule.java @@ -99,53 +99,39 @@ public abstract class AbstractEnforcerRule implements EnforcerRule { @Override public void execute(final EnforcerRuleHelper helper) throws EnforcerRuleException { - final MavenProject project; - try { - project = (MavenProject) helper.evaluate("${project}"); - } catch (ExpressionEvaluationException e) { - throw new EnforcerRuleException("Failed to access ${project} variable", e); - } - final String type = project.getArtifact().getType(); - if (!AbstractEnforcerRule.JAR_ARTIFACT_TYPE.equals(type) && - !AbstractEnforcerRule.BUNDLE_ARTIFACT_TYPE.equals(type)) { + final MavenProject project = getMavenProject(helper); + if (shouldSkipRuleExecution(project)) { helper.getLog().debug("Skipping non "+AbstractEnforcerRule.JAR_ARTIFACT_TYPE+ " or " + BUNDLE_ARTIFACT_TYPE + " artifact."); return; } final Artifact previousArtifact; - final Artifact currentArtifact = project.getArtifact(); - validateArtifact(currentArtifact); + final Artifact currentArtifact = validateArtifact(project.getArtifact()); final Version current = Version.parse(currentArtifact.getVersion()); - final File currentJar = currentArtifact.getFile(); try { final ArtifactRepository localRepository = (ArtifactRepository) helper.evaluate("${localRepository}"); final String version; + if (this.previousVersion != null) { version = this.previousVersion; - helper.getLog().info("Version specified as <"+version+">"); } else { final ArtifactMetadataSource artifactMetadataSource = (ArtifactMetadataSource) helper.getComponent(ArtifactMetadataSource.class); final List availableVersions = getAvailableReleasedVersions(artifactMetadataSource, project, localRepository); final List availablePreviousVersions = filterNonPreviousVersions(availableVersions, current); - if (availablePreviousVersions.isEmpty()) { helper.getLog().warn("No previously released version. Backward compatibility check not performed."); - return; } - version = availablePreviousVersions.iterator().next().toString(); - helper.getLog().info("Version deduced as <"+version+"> (among all availables: "+availablePreviousVersions+")"); } final ArtifactFactory artifactFactory = (ArtifactFactory) helper.getComponent(ArtifactFactory.class); - previousArtifact = artifactFactory.createArtifact(project.getGroupId(), project.getArtifactId(), version, null, type); + previousArtifact = artifactFactory.createArtifact(project.getGroupId(), project.getArtifactId(), version, null, project.getArtifact().getType()); final ArtifactResolver resolver = (ArtifactResolver) helper.getComponent(ArtifactResolver.class ); resolver.resolve(previousArtifact, project.getRemoteArtifactRepositories(), localRepository); - validateArtifact(previousArtifact); } catch (Exception e) { helper.getLog().warn("Exception while accessing artifacts; skipping check.", e); @@ -154,36 +140,25 @@ public abstract class AbstractEnforcerRule implements EnforcerRule { final Version previous = Version.parse(previousArtifact.getVersion()); final File previousJar = previousArtifact.getFile(); - - helper.getLog().info("Using <"+previousJar+"> as previous JAR"); - helper.getLog().info("Using <"+currentJar+"> as current JAR"); - - try { - final DiffCriteria diffCriteria = publicOnly ? new PublicDiffCriteria() : new SimpleDiffCriteria(); - final Comparer comparer = new Comparer(diffCriteria, previousJar, currentJar, extractFilters(this.includes), extractFilters(this.excludes)); - final Delta delta = comparer.diff(); - - enforce(helper, delta, previous, current); - } catch (IOException e) { - throw new EnforcerRuleException("Exception while checking compatibility: "+e.toString(), e); - } + final File currentJar = currentArtifact.getFile(); + compareJars(helper, previous, previousJar, current, currentJar); } protected abstract void enforce(final EnforcerRuleHelper helper, final Delta delta, final Version previous, final Version current) throws EnforcerRuleException; - + protected final void fail(final Delta delta, final String message) throws EnforcerRuleException { if (this.dumpDetails) { Dumper.dump(delta); } throw new EnforcerRuleException(message); } - + /** * @param artifactMetadataSource * @param project * @param localRepository * @return all available versions from most recent to oldest - * @throws ArtifactMetadataRetrievalException + * @throws ArtifactMetadataRetrievalException */ protected final List getAvailableReleasedVersions(final ArtifactMetadataSource artifactMetadataSource, final MavenProject project, final ArtifactRepository localRepository) throws ArtifactMetadataRetrievalException { final List availableVersions = artifactMetadataSource.retrieveAvailableVersions(project.getArtifact(), localRepository, project.getRemoteArtifactRepositories()); @@ -210,14 +185,45 @@ public abstract class AbstractEnforcerRule implements EnforcerRule { return versions; } + private static MavenProject getMavenProject(EnforcerRuleHelper helper) throws EnforcerRuleException { + final MavenProject project; + try { + project = (MavenProject) helper.evaluate("${project}"); + } catch (ExpressionEvaluationException e) { + throw new EnforcerRuleException("Failed to access ${project} variable", e); + } + return project; + } + + private static boolean shouldSkipRuleExecution(MavenProject project) { + return !AbstractEnforcerRule.JAR_ARTIFACT_TYPE.equals(project.getArtifact().getType()) && + !AbstractEnforcerRule.BUNDLE_ARTIFACT_TYPE.equals(project.getArtifact().getType()); + } + + private void compareJars(final EnforcerRuleHelper helper, final Version previous, final File previousJar, final Version current, + final File currentJar) throws EnforcerRuleException { + helper.getLog().info("Using <" + previousJar + "> as previous JAR"); + helper.getLog().info("Using <" + currentJar + "> as current JAR"); + try { + final DiffCriteria diffCriteria = publicOnly ? new PublicDiffCriteria() : new SimpleDiffCriteria(); + final Comparer comparer = + new Comparer(diffCriteria, previousJar, currentJar, extractFilters(this.includes), extractFilters(this.excludes)); + final Delta delta = comparer.diff(); + enforce(helper, delta, previous, current); + } catch (IOException e) { + throw new EnforcerRuleException("Exception while checking compatibility: " + e.toString(), e); + } + } + /** * Validates that specified {@link Artifact} is a file. * @param artifact */ - private void validateArtifact(final Artifact artifact) { + private static Artifact validateArtifact(final Artifact artifact) { if (!artifact.getFile().isFile()) { throw new IllegalArgumentException("<"+artifact.getFile()+"> is not a file"); } + return artifact; } @Override -- cgit v1.2.3 From 95232052f900ac565917246b29b56b058cc060da Mon Sep 17 00:00:00 2001 From: Alexander Morozov Date: Wed, 1 Oct 2014 13:41:42 +0700 Subject: Add version compatibility checking. AbstractEnforcerRule will skip non-compatible artifacts in case of auto-detection of previous version . --- api/src/main/java/org/semver/Version.java | 8 ++++++++ api/src/test/java/org/semver/VersionTest.java | 14 ++++++++++++++ .../java/org/semver/enforcer/AbstractEnforcerRule.java | 3 ++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/org/semver/Version.java b/api/src/main/java/org/semver/Version.java index 4cb54db..83765da 100755 --- a/api/src/main/java/org/semver/Version.java +++ b/api/src/main/java/org/semver/Version.java @@ -168,6 +168,14 @@ public final class Version implements Comparable { return this.special != null && this.special.isSnapshot(); } + /** + * @param version version to check with + * @return {@code true}, if supplied version is compatible with this version, {@code false} - otherwise + */ + public boolean isCompatible(Version version) { + return version != null && this.major == version.major; + } + @Override public int hashCode() { int hash = 5; diff --git a/api/src/test/java/org/semver/VersionTest.java b/api/src/test/java/org/semver/VersionTest.java index 6fbcf47..625c3ac 100755 --- a/api/src/test/java/org/semver/VersionTest.java +++ b/api/src/test/java/org/semver/VersionTest.java @@ -101,6 +101,20 @@ public class VersionTest { Assert.assertTrue(Version.parse("1.5.30-SNAPSHOT").isSnapshot()); } + @Test + public void shouldBeCompatible() { + Assert.assertTrue(Version.parse("1.0.0").isCompatible(Version.parse("1.2.3-SNAPSHOT"))); + Assert.assertTrue(Version.parse("1.0.0").isCompatible(Version.parse("1.0.1"))); + Assert.assertTrue(Version.parse("1.0.0").isCompatible(Version.parse("1.1.0"))); + } + + @Test + public void shouldBeIncompatible() { + Assert.assertFalse(Version.parse("0.0.1-SNAPSHOT").isCompatible(null)); + Assert.assertFalse(Version.parse("1.0.1").isCompatible(Version.parse("2.0.0"))); + Assert.assertFalse(Version.parse("1.1.0-rc3").isCompatible(Version.parse("3.1.0-SNAPSHOT"))); + } + @Test public void isNewer() { Assert.assertTrue(Version.parse("3.2.3").compareTo(Version.parse("3.2-M1-SNAPSHOT")) > 0); diff --git a/enforcer-rule/src/main/java/org/semver/enforcer/AbstractEnforcerRule.java b/enforcer-rule/src/main/java/org/semver/enforcer/AbstractEnforcerRule.java index 098a0e7..f0ce4cf 100755 --- a/enforcer-rule/src/main/java/org/semver/enforcer/AbstractEnforcerRule.java +++ b/enforcer-rule/src/main/java/org/semver/enforcer/AbstractEnforcerRule.java @@ -178,7 +178,8 @@ public abstract class AbstractEnforcerRule implements EnforcerRule { protected final List filterNonPreviousVersions(final List availableVersions, final Version version) { final List versions = new ArrayList(); for (final ArtifactVersion artifactVersion : availableVersions) { - if (version.compareTo(Version.parse(artifactVersion.toString())) > 0) { + Version parsedVersion = Version.parse(artifactVersion.toString()); + if (version.isCompatible(parsedVersion) && version.compareTo(parsedVersion) > 0) { versions.add(artifactVersion); } } -- cgit v1.2.3