From a3acf25e923620cde5b9368345a9caf69570d269 Mon Sep 17 00:00:00 2001 From: Guillermo Calvo Date: Sat, 17 Feb 2024 13:11:25 +0100 Subject: [PATCH] Extract string constants from `SemverOut` so they can be reused --- .../test/output/SemverOutIntegTest.java | 18 +++++++++--------- .../incompatible/IncompatibleErrorOutput.java | 12 ++++++------ .../java/japicmp/output/semver/SemverOut.java | 13 +++++++++---- .../japicmp/output/semver/SemverOutTest.java | 2 +- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/japicmp-testbase/japicmp-test/src/test/java/japicmp/test/output/SemverOutIntegTest.java b/japicmp-testbase/japicmp-test/src/test/java/japicmp/test/output/SemverOutIntegTest.java index 8df5a1917..3c7de75ea 100644 --- a/japicmp-testbase/japicmp-test/src/test/java/japicmp/test/output/SemverOutIntegTest.java +++ b/japicmp-testbase/japicmp-test/src/test/java/japicmp/test/output/SemverOutIntegTest.java @@ -24,63 +24,63 @@ public class SemverOutIntegTest { public void testSemver001_implementation_of_method_changes() { String lastPackage = "semver001.a"; String string = getSemverDiff(lastPackage); - assertEquals("0.0.1", string); + assertEquals(SemverOut.SEMVER_PATCH, string); } @Test public void testSemver010_added_deprecated_annotation() { String lastPackage = "semver010.a"; String string = getSemverDiff(lastPackage); - assertEquals("0.1.0", string); + assertEquals(SemverOut.SEMVER_MINOR, string); } @Test public void testSemver100_change_method() { String lastPackage = "semver100.a"; String string = getSemverDiff(lastPackage); - assertEquals("1.0.0", string); + assertEquals(SemverOut.SEMVER_MAJOR, string); } @Test public void testSemver100_reduce_class_visibility() { String lastPackage = "semver100.b"; String string = getSemverDiff(lastPackage); - assertEquals("1.0.0", string); + assertEquals(SemverOut.SEMVER_MAJOR, string); } @Test public void testSemver100_reduce_method_visibility() { String lastPackage = "semver100.c"; String string = getSemverDiff(lastPackage); - assertEquals("1.0.0", string); + assertEquals(SemverOut.SEMVER_MAJOR, string); } @Test public void testSemver100_superclass_with_field() { String lastPackage = "semver100.d"; String string = getSemverDiff(lastPackage); - assertEquals("1.0.0", string); + assertEquals(SemverOut.SEMVER_MAJOR, string); } @Test public void testSemver_class_with_private_final_field() { String lastPackage = "semver.finalfield"; String string = getSemverDiff(lastPackage); - assertEquals("0.0.1", string); + assertEquals(SemverOut.SEMVER_PATCH, string); } @Test public void testSemver_class_with_public_final_method() { String lastPackage = "semver.finalpublicmethod"; String string = getSemverDiff(lastPackage); - assertEquals("1.0.0", string); + assertEquals(SemverOut.SEMVER_MAJOR, string); } @Test public void testSemver_private_inner_class_changes() { String lastPackage = "semver.privateinnerclass"; String string = getSemverDiff(lastPackage); - assertEquals("1.0.0", string); // private inner class becomes package protected -> change is binary incompatible + assertEquals(SemverOut.SEMVER_MAJOR, string); // private inner class becomes package protected -> change is binary incompatible } private String getSemverDiff(String lastPackage) { diff --git a/japicmp/src/main/java/japicmp/output/incompatible/IncompatibleErrorOutput.java b/japicmp/src/main/java/japicmp/output/incompatible/IncompatibleErrorOutput.java index 2d6a59f10..b8d74174d 100644 --- a/japicmp/src/main/java/japicmp/output/incompatible/IncompatibleErrorOutput.java +++ b/japicmp/src/main/java/japicmp/output/incompatible/IncompatibleErrorOutput.java @@ -142,22 +142,22 @@ public void onChange(JApiCompatibility change, JApiSemanticVersionLevel semantic }); String semver = semverOut.generate(); - if (changeType == SemanticVersion.ChangeType.MINOR && semver.equals("1.0.0")) { + if (changeType == SemanticVersion.ChangeType.MINOR && semver.equals(SemverOut.SEMVER_MAJOR)) { throw new JApiCmpException(JApiCmpException.Reason.IncompatibleChange, "Versions of archives indicate a minor change but binary incompatible changes found."); } - if (changeType == SemanticVersion.ChangeType.PATCH && semver.equals("1.0.0")) { + if (changeType == SemanticVersion.ChangeType.PATCH && semver.equals(SemverOut.SEMVER_MAJOR)) { throw new JApiCmpException(JApiCmpException.Reason.IncompatibleChange, "Versions of archives indicate a patch change but binary incompatible changes found."); } - if (changeType == SemanticVersion.ChangeType.PATCH && semver.equals("0.1.0")) { + if (changeType == SemanticVersion.ChangeType.PATCH && semver.equals(SemverOut.SEMVER_MINOR)) { throw new JApiCmpException(JApiCmpException.Reason.IncompatibleChange, "Versions of archives indicate a patch change but binary compatible changes found."); } - if (changeType == SemanticVersion.ChangeType.UNCHANGED && semver.equals("1.0.0")) { + if (changeType == SemanticVersion.ChangeType.UNCHANGED && semver.equals(SemverOut.SEMVER_MAJOR)) { throw new JApiCmpException(JApiCmpException.Reason.IncompatibleChange, "Versions of archives indicate no API changes but binary incompatible changes found."); } - if (changeType == SemanticVersion.ChangeType.UNCHANGED && semver.equals("0.1.0")) { + if (changeType == SemanticVersion.ChangeType.UNCHANGED && semver.equals(SemverOut.SEMVER_MINOR)) { throw new JApiCmpException(JApiCmpException.Reason.IncompatibleChange, "Versions of archives indicate no API changes but binary compatible changes found."); } - if (changeType == SemanticVersion.ChangeType.UNCHANGED && semver.equals("0.0.1")) { + if (changeType == SemanticVersion.ChangeType.UNCHANGED && semver.equals(SemverOut.SEMVER_PATCH)) { throw new JApiCmpException(JApiCmpException.Reason.IncompatibleChange, "Versions of archives indicate no API changes but found API changes."); } } else { diff --git a/japicmp/src/main/java/japicmp/output/semver/SemverOut.java b/japicmp/src/main/java/japicmp/output/semver/SemverOut.java index a543c949b..57bb24fc3 100644 --- a/japicmp/src/main/java/japicmp/output/semver/SemverOut.java +++ b/japicmp/src/main/java/japicmp/output/semver/SemverOut.java @@ -28,6 +28,11 @@ public class SemverOut extends OutputGenerator { + public static final String SEMVER_MAJOR = "1.0.0"; + public static final String SEMVER_MINOR = "0.1.0"; + public static final String SEMVER_PATCH = "0.0.1"; + public static final String SEMVER_COMPATIBLE = "0.0.0"; + private Listener m_Listener = Listener.NULL; public SemverOut(Options options, List jApiClasses) { @@ -80,13 +85,13 @@ public void visit(JApiSuperclass jApiSuperclass) { }); ImmutableSet build = builder.build(); if (build.contains(JApiSemanticVersionLevel.MAJOR)) { - return "1.0.0"; + return SEMVER_MAJOR; } else if (build.contains(JApiSemanticVersionLevel.MINOR)) { - return "0.1.0"; + return SEMVER_MINOR; } else if (build.contains(JApiSemanticVersionLevel.PATCH)) { - return "0.0.1"; + return SEMVER_PATCH; } else if (build.isEmpty()) { - return "0.0.0"; + return SEMVER_COMPATIBLE; } else { return "N/A"; } diff --git a/japicmp/src/test/java/japicmp/output/semver/SemverOutTest.java b/japicmp/src/test/java/japicmp/output/semver/SemverOutTest.java index b7ee3bfaf..be2a9f17d 100644 --- a/japicmp/src/test/java/japicmp/output/semver/SemverOutTest.java +++ b/japicmp/src/test/java/japicmp/output/semver/SemverOutTest.java @@ -33,6 +33,6 @@ public List createNewClasses(ClassPool classPool) throws Exception { Options options = Options.newDefault(); SemverOut semverOut = new SemverOut(options, jApiClasses); String output = semverOut.generate(); - assertThat(output, is("0.0.0")); + assertThat(output, is(SemverOut.SEMVER_COMPATIBLE)); } }