Skip to content

Commit

Permalink
Adds more module tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Jan 17, 2025
1 parent d3bbca3 commit 8f513ac
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Objects;

import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

public class EverythingWorksInTheModularWorldTest {
Expand All @@ -12,7 +13,20 @@ void classCanBeVerified() {
EqualsVerifier.forClass(ClassPoint.class).verify();
}

final class ClassPoint {
@Test
@Disabled("It's impossble to load `equalsverifier-16` in the module world "
+ "because it creates a split path. We can enable this test again "
+ "when EqualsVerifier's baseline becomes Java 17")
void recordCanBeVerified() {
EqualsVerifier.forClass(RecordPoint.class).verify();
}

@Test
void classContainingFieldsFromOtherJdkModulesCanBeVerifier() {
EqualsVerifier.forClass(FieldsFromJdkModulesHaver.class).verify();
}

static final class ClassPoint {
private final int x;
private final int y;

Expand All @@ -31,4 +45,38 @@ public int hashCode() {
return Objects.hash(x, y);
}
}

record RecordPoint(int x, int y) {}

static final class FieldsFromJdkModulesHaver {
private final java.awt.Color desktopAwtColor;
private final java.rmi.server.UID rmiUid;
private final java.sql.Date sqlDate;
private final javax.naming.Reference namingReference;

private FieldsFromJdkModulesHaver(
java.awt.Color c,
java.rmi.server.UID u,
java.sql.Date d,
javax.naming.Reference r) {
this.desktopAwtColor = c;
this.rmiUid = u;
this.sqlDate = d;
this.namingReference = r;
}

@Override
public boolean equals(Object obj) {
return obj instanceof FieldsFromJdkModulesHaver other
&& Objects.equals(desktopAwtColor, other.desktopAwtColor)
&& Objects.equals(rmiUid, other.rmiUid)
&& Objects.equals(sqlDate, other.sqlDate)
&& Objects.equals(namingReference, other.namingReference);
}

@Override
public int hashCode() {
return Objects.hash(desktopAwtColor, rmiUid, sqlDate, namingReference);
}
}
}
60 changes: 60 additions & 0 deletions equalsverifier-test-jpms/src/test/java/it/ModuleErrorsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package it;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.text.AttributedString;
import java.util.Objects;

import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
import org.junit.jupiter.api.Test;

/*
* Let's hope nobody needs prefab values for `java.text.AttributedString`, because we need a class here from je Java
* APIs that doesn't already have prefab values.
*/
public class ModuleErrorsTest {
@Test
void giveProperErrorMessage_whenClassUnderTestIsInaccessible() {
assertThatThrownBy(
() -> EqualsVerifier
.forClass(AttributedString.class)
.suppress(Warning.INHERITED_DIRECTLY_FROM_OBJECT)
.verify())
.isInstanceOf(AssertionError.class)
.hasMessageContaining("The class")
.hasMessageContaining("Consider opening");
}

@Test
void giveProperErrorMessage_whenFieldIsInaccessible() {
assertThatThrownBy(() -> EqualsVerifier.forClass(InaccessibleContainer.class).verify())
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Field x")
.hasMessageContaining("Consider opening")
.hasMessageContaining("add prefab values");
}

static final class InaccessibleContainer {

private final AttributedString x;

public InaccessibleContainer(AttributedString x) {
this.x = x;
}

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

@Override
public int hashCode() {
return Objects.hash(x);
}
}
}
5 changes: 5 additions & 0 deletions equalsverifier-test-jpms/src/test/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@

requires org.junit.jupiter.api;
requires org.assertj.core;

requires java.desktop;
requires java.naming;
requires java.rmi;
requires java.sql;
}

0 comments on commit 8f513ac

Please sign in to comment.