Skip to content

Commit

Permalink
Extract string constants from SemverOut so they can be reused
Browse files Browse the repository at this point in the history
  • Loading branch information
guillermocalvo committed Feb 17, 2024
1 parent 858ca2d commit a3acf25
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 9 additions & 4 deletions japicmp/src/main/java/japicmp/output/semver/SemverOut.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@

public class SemverOut extends OutputGenerator<String> {

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<JApiClass> jApiClasses) {
Expand Down Expand Up @@ -80,13 +85,13 @@ public void visit(JApiSuperclass jApiSuperclass) {
});
ImmutableSet<JApiSemanticVersionLevel> 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";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public List<CtClass> 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));
}
}

0 comments on commit a3acf25

Please sign in to comment.