Skip to content

Commit

Permalink
Allows withOnlyTheseFields if Warning.SURROGATE_OR_BUSINESS_KEY is su…
Browse files Browse the repository at this point in the history
…ppressed
  • Loading branch information
jqno committed Mar 20, 2024
1 parent 19c44ab commit d8488c0
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- When `Warning.SURROGATE_OR_BUSINESS_KEY` is suppressed, it is now possible to use `#withOnlyTheseFields`, and the fields may include both `@Id` fields and regular fields. ([Issue 934](https://github.com/jqno/equalsverifier/issues/934))

## [3.15.8] - 2024-03-01

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public static void validateProcessedAnnotations(
Set<String> excludedFields
) {
validateClassAnnotations(type, cache, warnings, includedFields, excludedFields);
validateFieldAnnotations(type, cache, includedFields);
validateFieldAnnotations(type, cache, warnings, includedFields);
}

// CHECKSTYLE OFF: VariableDeclarationUsageDistance
Expand Down Expand Up @@ -187,22 +187,25 @@ private static void validateClassAnnotations(
private static void validateFieldAnnotations(
Class<?> type,
AnnotationCache cache,
Set<Warning> warnings,
Set<String> includedFields
) {
FieldIterable
.of(type)
.forEach(f -> validateFieldAnnotation(type, f, cache, includedFields));
.forEach(f -> validateFieldAnnotation(type, f, cache, warnings, includedFields));
}

private static void validateFieldAnnotation(
Class<?> type,
Field f,
AnnotationCache cache,
Set<Warning> warnings,
Set<String> includedFields
) {
validate(
includedFields.contains(f.getName()) &&
cache.hasFieldAnnotation(type, f.getName(), SupportedAnnotations.ID),
cache.hasFieldAnnotation(type, f.getName(), SupportedAnnotations.ID) &&
!warnings.contains(Warning.SURROGATE_OR_BUSINESS_KEY),
"you can't use withOnlyTheseFields on a field marked @Id or @EmbeddedId.\n" +
"Suppress Warning.SURROGATE_KEY and remove withOnlyTheseFields " +
"if you want to use only the @Id or @EmbeddedId fields in equals."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,15 @@ public void succeed_whenEqualsIsImplementedInSuperclass_givenWarningSurrogateKey
EqualsVerifier.forClass(SubclassEntity.class).suppress(Warning.SURROGATE_KEY).verify();
}

@Test
public void succeed_whenEqualsUsesIdsAndNonIds_givenWarningSurrogateOrBusinessKeyIsSuppressed() {
EqualsVerifier
.forClass(JakartaIdDirtyTrackingPerson.class)
.suppress(Warning.SURROGATE_OR_BUSINESS_KEY)
.withOnlyTheseFields("id", "version", "isDirty")
.verify();
}

static class JakartaIdBusinessKeyPerson {

@jakarta.persistence.Id
Expand Down Expand Up @@ -1000,4 +1009,36 @@ public SubclassEntity(UUID id, String name) {
this.name = name;
}
}

@jakarta.persistence.Entity
static class JakartaIdDirtyTrackingPerson {

@jakarta.persistence.Id
private UUID id;

private int version;
private boolean isDirty;

private String socialSecurity;
private String name;
private LocalDate birthdate;

@Override
public final boolean equals(Object obj) {
if (!(obj instanceof JakartaIdDirtyTrackingPerson)) {
return false;
}
JakartaIdDirtyTrackingPerson other = (JakartaIdDirtyTrackingPerson) obj;
return (
Objects.equals(id, other.id) &&
Objects.equals(version, other.version) &&
Objects.equals(isDirty, other.isDirty)
);
}

@Override
public final int hashCode() {
return Objects.hash(id, version, isDirty);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,15 @@ public void succeed_whenEqualsIsImplementedInSuperclass_givenWarningSurrogateKey
EqualsVerifier.forClass(SubclassEntity.class).suppress(Warning.SURROGATE_KEY).verify();
}

@Test
public void succeed_whenEqualsUsesIdsAndNonIds_givenWarningSurrogateOrBusinessKeyIsSuppressed() {
EqualsVerifier
.forClass(JpaIdDirtyTrackingPerson.class)
.suppress(Warning.SURROGATE_OR_BUSINESS_KEY)
.withOnlyTheseFields("id", "version", "isDirty")
.verify();
}

static class JpaIdBusinessKeyPerson {

@Id
Expand Down Expand Up @@ -1092,4 +1101,36 @@ public SubclassEntity(UUID id, String name) {
this.name = name;
}
}

@Entity
static class JpaIdDirtyTrackingPerson {

@Id
private UUID id;

private int version;
private boolean isDirty;

private String socialSecurity;
private String name;
private LocalDate birthdate;

@Override
public final boolean equals(Object obj) {
if (!(obj instanceof JpaIdDirtyTrackingPerson)) {
return false;
}
JpaIdDirtyTrackingPerson other = (JpaIdDirtyTrackingPerson) obj;
return (
Objects.equals(id, other.id) &&
Objects.equals(version, other.version) &&
Objects.equals(isDirty, other.isDirty)
);
}

@Override
public final int hashCode() {
return Objects.hash(id, version, isDirty);
}
}
}

0 comments on commit d8488c0

Please sign in to comment.