Skip to content

Commit

Permalink
Tweaks ValidationTest to use ExpectedException
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Nov 6, 2024
1 parent 9534b24 commit d0a8a18
Showing 1 changed file with 33 additions and 103 deletions.
Original file line number Diff line number Diff line change
@@ -1,98 +1,67 @@
package nl.jqno.equalsverifier.internal.util;

import static nl.jqno.equalsverifier.internal.testhelpers.Util.coverThePrivateConstructor;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import nl.jqno.equalsverifier.internal.testhelpers.ExpectedException;
import org.junit.jupiter.api.Test;

/** Tests for {@link Validations} */
class ValidationsTest {

@Test
void coverTheConstructor() {
public void coverTheConstructor() {
coverThePrivateConstructor(Validations.class);
}

/**
* {@link Validations#validateFieldTypeMatches(Class, String, Class)} should
* throw if the field type does not match the given type.
*/
@Test
void validateFieldTypeMatches_ShouldFailOnWrongType() {
public void validateFieldTypeMatches_shouldFailOnWrongType() {
assertAll(
() -> {
Exception exc = assertThrows(
IllegalStateException.class,
() ->
ExpectedException
.when(() ->
Validations.validateFieldTypeMatches(
TestContainer.class,
"listField",
HashSet.class
),
"Should not allow HashSet as a List"
);
assertThat(
exc.getMessage(),
allOf(
containsString("should be of type List"),
containsString("but are HashSet")
)
)
);
.assertThrows(IllegalStateException.class)
.assertMessageContains("should be of type List", "but are HashSet");
},
() -> {
Exception exc = assertThrows(
IllegalStateException.class,
() ->
ExpectedException
.when(() ->
Validations.validateFieldTypeMatches(
TestContainer.class,
"objectField",
int.class
),
"Should not allow primitives as an Object, because primitives are not a subtype of Object"
);
assertThat(
exc.getMessage(),
allOf(containsString("should be of type Object"), containsString("but are int"))
);
)
)
.assertThrows(IllegalStateException.class)
.assertMessageContains("should be of type Object", "but are int");
},
() -> {
Exception exc = assertThrows(
IllegalStateException.class,
() ->
ExpectedException
.when(() ->
Validations.validateFieldTypeMatches(
TestContainer.class,
"charsField",
Character.class
),
"Should not allow Character as a CharSequence"
);
assertThat(
exc.getMessage(),
allOf(
containsString("should be of type CharSequence"),
containsString("but are Character")
)
)
);
.assertThrows(IllegalStateException.class)
.assertMessageContains("should be of type CharSequence", "but are Character");
}
);
}

/**
* {@link Validations#validateFieldTypeMatches(Class, String, Class)} should not
* throw if the field type is a super-type or interface of the given type.
*/
@Test
void validateFieldTypeMatches_ShouldAllowSubTypes() {
public void validateFieldTypeMatches_shouldAllowSubTypes() {
assertAll(
() ->
assertDoesNotThrow(
Expand Down Expand Up @@ -127,80 +96,41 @@ void validateFieldTypeMatches_ShouldAllowSubTypes() {
);
}

/**
* {@link Validations#validateFieldTypeMatches(Class, String, Class)} should not
* throw if the field type is a sub-type of the given type or interface.
*/
@Test
void validateFieldTypeMatches_ShouldFailOnSuperTypes() {
public void validateFieldTypeMatches_shouldFailOnSuperTypes() {
assertAll(
() -> {
Exception exc = assertThrows(
IllegalStateException.class,
() ->
ExpectedException
.when(() ->
Validations.validateFieldTypeMatches(
TestContainer.class,
"listField",
Collection.class
),
"Should not allow Collection as a List"
);
assertThat(
exc.getMessage(),
allOf(
containsString("should be of type List"),
containsString("but are Collection")
)
)
);
.assertThrows(IllegalStateException.class)
.assertMessageContains("should be of type List", "but are Collection");
},
() -> {
Exception exc = assertThrows(
IllegalStateException.class,
() ->
ExpectedException
.when(() ->
Validations.validateFieldTypeMatches(
TestContainer.class,
"charsField",
Object.class
),
"Should not allow Object as a CharSequence"
);
assertThat(
exc.getMessage(),
allOf(
containsString("should be of type CharSequence"),
containsString("but are Object")
)
)
);
.assertThrows(IllegalStateException.class)
.assertMessageContains("should be of type CharSequence", "but are Object");
}
);
}

/** Test class for type checking. */
private static class TestContainer {
@SuppressWarnings("unused")
private static final class TestContainer {

List<String> listField;
Object objectField;
CharSequence charsField;

@Override
public int hashCode() {
return Objects.hash(charsField, listField, objectField);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof TestContainer)) {
return false;
}
TestContainer other = (TestContainer) obj;
return (
Objects.equals(charsField, other.charsField) &&
Objects.equals(listField, other.listField) &&
Objects.equals(objectField, other.objectField)
);
}
}
}

0 comments on commit d0a8a18

Please sign in to comment.