Skip to content

Commit

Permalink
Adds tests for Jakarta lazy fields
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Jul 6, 2023
1 parent 0fdbd43 commit 053eefd
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private boolean fieldIsLazy(FieldAccessor fieldAccessor) {
annotationCache.hasFieldAnnotation(
type,
fieldAccessor.getFieldName(),
SupportedAnnotations.JPA_COLLECTION_FIELD
SupportedAnnotations.JPA_LINKED_FIELD
) ||
annotationCache.hasFieldAnnotation(
type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public void postProcess(Set<Class<?>> types, AnnotationCache annotationCache) {
}
},

JPA_COLLECTION_FIELD(
JPA_LINKED_FIELD(
true,
"javax.persistence.OneToOne",
"javax.persistence.OneToMany",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import java.util.Arrays;
import java.util.Objects;
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
Expand All @@ -29,58 +30,106 @@ public void gettersAreUsed() {
@Test
public void basicGetterNotUsed_givenEagerLoading() {
EqualsVerifier
.forClass(IncorrectBasicJakartaEagerFieldContainer.class)
.forClass(CorrectBasicJakartaEagerFieldContainer.class)
.suppress(Warning.NONFINAL_FIELDS)
.verify();
}

@Test
public void basicGetterNotUsed_givenCorrespondingFieldIgnored() {
EqualsVerifier
.forClass(IncorrectBasicJakartaIgnoredLazyFieldContainer.class)
.forClass(CorrectBasicJakartaIgnoredLazyFieldContainer.class)
.withIgnoredFields("basic")
.suppress(Warning.NONFINAL_FIELDS)
.verify();
}

@Test
public void basicGetterUsed_givenAnnotationIsOnGetter() {
getterNotUsed(CorrectBasicJakartaLazyGetterContainer.class, "equals");
getterNotUsed(IncorrectBasicJakartaLazyGetterContainer.class, "equals");
getterNotUsed_warningSuppressed(IncorrectBasicJakartaLazyGetterContainer.class);
}

@Test
public void basicGetterNotUsedInHashCode() {
getterNotUsed(IncorrectBasicJakartaLazyFieldContainerHashCode.class, "hashCode");
getterNotUsed_warningSuppressed(IncorrectBasicJakartaLazyFieldContainerHashCode.class);
}

@Test
public void basicGetterNotUsed() {
getterNotUsed(IncorrectBasicJakartaLazyFieldContainer.class, "equals");
getterNotUsed_warningSuppressed(IncorrectBasicJakartaLazyFieldContainer.class);
}

@Test
public void oneToOneGetterNotUsed() {
getterNotUsed(IncorrectOneToOneJakartaLazyFieldContainer.class, "equals");
getterNotUsed_warningSuppressed(IncorrectOneToOneJakartaLazyFieldContainer.class);
}

@Test
public void oneToManyGetterNotUsed() {
getterNotUsed(IncorrectOneToManyJakartaLazyFieldContainer.class, "equals");
getterNotUsed_warningSuppressed(IncorrectOneToManyJakartaLazyFieldContainer.class);
}

@Test
public void manyToOneGetterNotUsed() {
getterNotUsed(IncorrectManyToOneJakartaLazyFieldContainer.class, "equals");
getterNotUsed_warningSuppressed(IncorrectManyToOneJakartaLazyFieldContainer.class);
}

@Test
public void manyToManyGetterNotUsed() {
getterNotUsed(IncorrectManyToManyJakartaLazyFieldContainer.class, "equals");
getterNotUsed_warningSuppressed(IncorrectManyToManyJakartaLazyFieldContainer.class);
}

@Test
public void elementCollectionGetterNotUsed() {
getterNotUsed(IncorrectElementCollectionJakartaLazyFieldContainer.class, "equals");
getterNotUsed_warningSuppressed(IncorrectElementCollectionJakartaLazyFieldContainer.class);
}

@Test
public void lazyGettersPickedUpInSuper() {
EqualsVerifier.forClass(LazyGetterContainer.class).usingGetClass().verify();
EqualsVerifier.forClass(ChildOfLazyGetterContainer.class).usingGetClass().verify();
}

@Test
public void differentCodingStyle_single() {
EqualsVerifier
.forClass(DifferentCodingStyleContainer.class)
.suppress(Warning.NONFINAL_FIELDS)
.withFieldnameToGetterConverter(fn ->
"get" + Character.toUpperCase(fn.charAt(2)) + fn.substring(3)
)
.verify();
}

@Test
public void differentCodingStyle_configured() {
EqualsVerifier
.configure()
.suppress(Warning.NONFINAL_FIELDS)
.withFieldnameToGetterConverter(fn ->
"get" + Character.toUpperCase(fn.charAt(2)) + fn.substring(3)
)
.forClass(DifferentCodingStyleContainer.class)
.verify();
}

@Test
public void differentCodingStyle_multiple() {
EqualsVerifier
.forClasses(Arrays.asList(DifferentCodingStyleContainer.class))
.suppress(Warning.NONFINAL_FIELDS)
.withFieldnameToGetterConverter(fn ->
"get" + Character.toUpperCase(fn.charAt(2)) + fn.substring(3)
)
.verify();
}

private void getterNotUsed(Class<?> type, String method) {
Expand All @@ -90,6 +139,13 @@ private void getterNotUsed(Class<?> type, String method) {
.assertMessageContains("JPA Entity", method, "direct reference");
}

private void getterNotUsed_warningSuppressed(Class<?> type) {
EqualsVerifier
.forClass(type)
.suppress(Warning.JPA_GETTER, Warning.NONFINAL_FIELDS)
.verify();
}

@Entity
static class CorrectJakartaLazyFieldContainer {

Expand Down Expand Up @@ -165,7 +221,7 @@ public int hashCode() {
}

@Entity
static class IncorrectBasicJakartaEagerFieldContainer {
static class CorrectBasicJakartaEagerFieldContainer {

@Basic
private String basic;
Expand All @@ -176,11 +232,11 @@ public String getBasic() {

@Override
public boolean equals(Object obj) {
if (!(obj instanceof IncorrectBasicJakartaEagerFieldContainer)) {
if (!(obj instanceof CorrectBasicJakartaEagerFieldContainer)) {
return false;
}
IncorrectBasicJakartaEagerFieldContainer other =
(IncorrectBasicJakartaEagerFieldContainer) obj;
CorrectBasicJakartaEagerFieldContainer other =
(CorrectBasicJakartaEagerFieldContainer) obj;
return Objects.equals(basic, other.basic);
}

Expand All @@ -191,7 +247,7 @@ public int hashCode() {
}

@Entity
static class IncorrectBasicJakartaIgnoredLazyFieldContainer {
static class CorrectBasicJakartaIgnoredLazyFieldContainer {

private String somethingElse;

Expand All @@ -204,11 +260,11 @@ public String getBasic() {

@Override
public boolean equals(Object obj) {
if (!(obj instanceof IncorrectBasicJakartaIgnoredLazyFieldContainer)) {
if (!(obj instanceof CorrectBasicJakartaIgnoredLazyFieldContainer)) {
return false;
}
IncorrectBasicJakartaIgnoredLazyFieldContainer other =
(IncorrectBasicJakartaIgnoredLazyFieldContainer) obj;
CorrectBasicJakartaIgnoredLazyFieldContainer other =
(CorrectBasicJakartaIgnoredLazyFieldContainer) obj;
return Objects.equals(somethingElse, other.somethingElse);
}

Expand All @@ -219,7 +275,7 @@ public int hashCode() {
}

@Entity
static class CorrectBasicJakartaLazyGetterContainer {
static class IncorrectBasicJakartaLazyGetterContainer {

private String basic;

Expand All @@ -230,11 +286,11 @@ public String getBasic() {

@Override
public boolean equals(Object obj) {
if (!(obj instanceof CorrectBasicJakartaLazyGetterContainer)) {
if (!(obj instanceof IncorrectBasicJakartaLazyGetterContainer)) {
return false;
}
CorrectBasicJakartaLazyGetterContainer other =
(CorrectBasicJakartaLazyGetterContainer) obj;
IncorrectBasicJakartaLazyGetterContainer other =
(IncorrectBasicJakartaLazyGetterContainer) obj;
return Objects.equals(basic, other.basic);
}

Expand Down Expand Up @@ -425,4 +481,84 @@ public int hashCode() {
return Objects.hash(getElementCollection());
}
}

@Entity
static class LazyGetterContainer {

@Basic(fetch = FetchType.LAZY)
private String s;

public String getS() {
return s;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
return Objects.equals(getS(), ((LazyGetterContainer) obj).getS());
}

@Override
public int hashCode() {
return Objects.hash(getS());
}
}

@Entity
static class ChildOfLazyGetterContainer extends LazyGetterContainer {

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
return super.equals(obj);
}
}

@Entity
static class DifferentCodingStyleContainer {

// CHECKSTYLE OFF: MemberName
@OneToMany(fetch = FetchType.LAZY)
private String m_oneToMany;

@ManyToOne(fetch = FetchType.LAZY)
private String m_manyToOne;

// CHECKSTYLE ON: MemberName

public String getOneToMany() {
return m_oneToMany;
}

public String getManyToOne() {
return m_manyToOne;
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof DifferentCodingStyleContainer)) {
return false;
}
DifferentCodingStyleContainer other = (DifferentCodingStyleContainer) obj;
return (
Objects.equals(getOneToMany(), other.getOneToMany()) &&
Objects.equals(getManyToOne(), other.getManyToOne())
);
}

@Override
public int hashCode() {
return Objects.hash(getOneToMany(), getManyToOne());
}
}
}

0 comments on commit 053eefd

Please sign in to comment.