Skip to content

Commit

Permalink
add tests for jakarta.persistence's entity and Id annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahli authored and jqno committed Jan 25, 2022
1 parent 67f398b commit 7db5d14
Show file tree
Hide file tree
Showing 3 changed files with 1,109 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@
<version>2.2.600</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>

<!-- ArchUnit dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package nl.jqno.equalsverifier.integration.extra_features;

import static nl.jqno.equalsverifier.testhelpers.Util.defaultHashCode;

import java.util.Objects;
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.testhelpers.ExpectedException;
import org.junit.jupiter.api.Test;

public class JakartaEntityTest {

@Test
public void succeed_whenClassIsNonFinalAndFieldsAreMutable_givenClassHasJpaEntityAnnotation() {
EqualsVerifier.forClass(EntityByJakartaAnnotation.class).verify();
}

@Test
public void fail_whenClassIsNonFinalAndFieldsAreMutable_givenSuperclassHasJpaEntityAnnotationButThisClassDoesnt() {
ExpectedException
.when(() -> EqualsVerifier.forClass(SubclassEntityByJakartaAnnotation.class).verify())
.assertFailure()
.assertMessageContains("Subclass");
}

@Test
public void succeed_whenFieldsAreMutable_givenClassHasJpaEmbeddableAnnotation() {
EqualsVerifier.forClass(EmbeddableByJakartaAnnotation.class).verify();
}

@Test
public void fail_whenFieldsAreMutable_givenSuperclassHasJpaEmbeddableAnnotationButThisClassDoesnt() {
ExpectedException
.when(() ->
EqualsVerifier.forClass(SubclassEmbeddableByJakartaAnnotation.class).verify()
)
.assertFailure()
.assertMessageContains("Subclass");
}

@Test
public void succeed_whenFieldsAreMutable_givenClassHasJpaMappedSuperclassAnnotation() {
EqualsVerifier.forClass(MappedSuperclassByJakartaAnnotation.class).verify();
}

@Test
public void fail_whenFieldsAreMutable_givenSuperclassHasJpaMappedSuperclassAnnotationButThisClassDoesnt() {
ExpectedException
.when(() ->
EqualsVerifier.forClass(SubclassMappedSuperclassByJakartaAnnotation.class).verify()
)
.assertFailure()
.assertMessageContains("Subclass");
}

@jakarta.persistence.Entity
static class EntityByJakartaAnnotation {

private int i;
private String s;

public void setI(int value) {
this.i = value;
}

public void setS(String value) {
this.s = value;
}

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

@Override
public int hashCode() {
return defaultHashCode(this);
}
}

static class SubclassEntityByJakartaAnnotation extends EntityByJakartaAnnotation {}

@jakarta.persistence.Embeddable
static class EmbeddableByJakartaAnnotation {

private int i;
private String s;

public void setI(int value) {
this.i = value;
}

public void setS(String value) {
this.s = value;
}

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

@Override
public int hashCode() {
return defaultHashCode(this);
}
}

static class SubclassEmbeddableByJakartaAnnotation extends EmbeddableByJakartaAnnotation {}

@jakarta.persistence.MappedSuperclass
abstract static class MappedSuperclassByJakartaAnnotation {

private int i;
private String s;

public void setI(int value) {
this.i = value;
}

public void setS(String value) {
this.s = value;
}

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

@Override
public int hashCode() {
return defaultHashCode(this);
}
}

static class SubclassMappedSuperclassByJakartaAnnotation
extends MappedSuperclassByJakartaAnnotation {}
}
Loading

0 comments on commit 7db5d14

Please sign in to comment.