Skip to content

Commit

Permalink
JPA Id: adds VERSIONED_ENTITY test
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Dec 21, 2018
1 parent 720dc95 commit 8e26d92
Showing 1 changed file with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ public void succeed_whenIdAndNameFieldsAreNotUsed_givenSocialSecurityAndBirthdat
.verify();
}

@Test
public void succeed_whenIdIsPartOfAProperJpaEntity() {
EqualsVerifier.forClass(JpaIdBusinessKeyPersonEntity.class)
.verify();
}

@Test
public void succeed_whenNaturalIdIsPartOfAProperJpaEntity() {
EqualsVerifier.forClass(NaturalIdBusinessKeyPersonEntity.class)
.verify();
}

@Test
public void succeed_whenEqualsBehavesLikeVersionedEntity_givenIdIsMarkedWithIdAndWarningVersionedEntityIsSuppressed() {
EqualsVerifier.forClass(JpaIdVersionedEntity.class)
.suppress(Warning.IDENTICAL_COPY_FOR_VERSIONED_ENTITY)
.verify();
}

@Test
public void fail_whenIdFieldIsTheOnlyFieldUsed() {
expectFailure("Precondition: you can't use withOnlyTheseFields on a field marked @Id.", "Suppress Warning.SURROGATE_KEY if");
Expand Down Expand Up @@ -156,18 +175,6 @@ public void fail_whenWarningVersionedEntityIsSuppressed_givenWarningSurrogateKey
.verify();
}

@Test
public void succeed_whenIdIsPartOfAProperJpaEntity() {
EqualsVerifier.forClass(JpaIdBusinessKeyPersonEntity.class)
.verify();
}

@Test
public void succeed_whenNaturalIdIsPartOfAProperJpaEntity() {
EqualsVerifier.forClass(NaturalIdBusinessKeyPersonEntity.class)
.verify();
}

@Test
public void fail_whenAnIdAnnotationFromAnotherPackageIsUsed() {
expectFailure("Significant fields");
Expand Down Expand Up @@ -500,4 +507,28 @@ public int hashCode() {
return Objects.hash(socialSecurity);
}
}

public static final class JpaIdVersionedEntity {
@Id
private final long id;
private final String s;

public JpaIdVersionedEntity(long id, String s) { this.id = id; this.s = s; }

@Override
public boolean equals(Object obj) {
if (!(obj instanceof JpaIdVersionedEntity)) {
return false;
}
JpaIdVersionedEntity other = (JpaIdVersionedEntity)obj;
if (id == 0L && other.id == 0L) {
return Objects.equals(s, other.s);
}
return id == other.id;
}

@Override public int hashCode() {
return Float.floatToIntBits(id);
}
}
}

0 comments on commit 8e26d92

Please sign in to comment.