Skip to content

Commit

Permalink
Move isInterface / isNotInterface implementation to `AbstractClas…
Browse files Browse the repository at this point in the history
…sAssert`
  • Loading branch information
scordio committed Apr 18, 2023
1 parent b26d369 commit ebdc2dd
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import static org.assertj.core.error.ClassModifierShouldBe.shouldNotBeFinal;
import static org.assertj.core.error.ClassModifierShouldBe.shouldNotBeStatic;
import static org.assertj.core.error.ShouldBeAssignableTo.shouldBeAssignableTo;
import static org.assertj.core.error.ShouldBeInterface.shouldBeInterface;
import static org.assertj.core.error.ShouldBeInterface.shouldNotBeInterface;
import static org.assertj.core.error.ShouldBeRecord.shouldBeRecord;
import static org.assertj.core.error.ShouldBeRecord.shouldNotBeRecord;
import static org.assertj.core.error.ShouldHaveNoSuperclass.shouldHaveNoSuperclass;
Expand All @@ -34,7 +36,6 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Collections;
import java.util.Objects;
import java.util.Set;

import org.assertj.core.internal.Classes;
Expand Down Expand Up @@ -112,7 +113,7 @@ public SELF isAssignableTo(Class<?> other) {
}

private void assertIsAssignableTo(Class<?> other) {
Objects.requireNonNull(other, shouldNotBeNull("other")::create);
requireNonNull(other, shouldNotBeNull("other")::create);
if (!other.isAssignableFrom(actual)) throw assertionError(shouldBeAssignableTo(actual, other));
}

Expand All @@ -134,10 +135,15 @@ private void assertIsAssignableTo(Class<?> other) {
* @throws AssertionError if the actual {@code Class} is not an interface.
*/
public SELF isNotInterface() {
classes.assertIsNotInterface(info, actual);
isNotNull();
assertIsNotInterface();
return myself;
}

private void assertIsNotInterface() {
if (actual.isInterface()) throw assertionError(shouldNotBeInterface(actual));
}

/**
* Verifies that the actual {@code Class} is an interface.
* <p>
Expand All @@ -156,10 +162,15 @@ public SELF isNotInterface() {
* @throws AssertionError if the actual {@code Class} is not an interface.
*/
public SELF isInterface() {
classes.assertIsInterface(info, actual);
isNotNull();
assertIsInterface();
return myself;
}

private void assertIsInterface() {
if (!actual.isInterface()) throw assertionError(shouldBeInterface(actual));
}

/**
* Verifies that the actual {@code Class} is abstract (has {@code abstract} modifier).
* <p>
Expand Down
28 changes: 0 additions & 28 deletions assertj-core/src/main/java/org/assertj/core/internal/Classes.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import static org.assertj.core.error.ShouldBeAnnotation.shouldBeAnnotation;
import static org.assertj.core.error.ShouldBeAnnotation.shouldNotBeAnnotation;
import static org.assertj.core.error.ShouldBeAssignableFrom.shouldBeAssignableFrom;
import static org.assertj.core.error.ShouldBeInterface.shouldBeInterface;
import static org.assertj.core.error.ShouldBeInterface.shouldNotBeInterface;
import static org.assertj.core.error.ShouldHaveAnnotations.shouldHaveAnnotations;
import static org.assertj.core.error.ShouldHaveFields.shouldHaveDeclaredFields;
import static org.assertj.core.error.ShouldHaveFields.shouldHaveFields;
Expand Down Expand Up @@ -96,32 +94,6 @@ public void assertIsAssignableFrom(AssertionInfo info, Class<?> actual, Class<?>
if (!missing.isEmpty()) throw failures.failure(info, shouldBeAssignableFrom(actual, expected, missing));
}

/**
* Verifies that the actual {@code Class} is not an interface.
*
* @param info contains information about the assertion.
* @param actual the "actual" {@code Class}.
* @throws AssertionError if {@code actual} is {@code null}.
* @throws AssertionError if the actual {@code Class} is an interface.
*/
public void assertIsNotInterface(AssertionInfo info, Class<?> actual) {
assertNotNull(info, actual);
if (actual.isInterface()) throw failures.failure(info, shouldNotBeInterface(actual));
}

/**
* Verifies that the actual {@code Class} is an interface.
*
* @param info contains information about the assertion.
* @param actual the "actual" {@code Class}.
* @throws AssertionError if {@code actual} is {@code null}.
* @throws AssertionError if the actual {@code Class} is not an interface.
*/
public void assertIsInterface(AssertionInfo info, Class<?> actual) {
assertNotNull(info, actual);
if (!actual.isInterface()) throw failures.failure(info, shouldBeInterface(actual));
}

/**
* Verifies that the actual {@code Class} is abstract.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,45 @@
*/
package org.assertj.core.api.classes;

import static org.mockito.Mockito.verify;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.error.ShouldBeInterface.shouldBeInterface;
import static org.assertj.core.error.ShouldNotBeNull.shouldNotBeNull;
import static org.assertj.core.util.AssertionsUtil.expectAssertionError;

import org.assertj.core.api.ClassAssert;
import org.assertj.core.api.ClassAssertBaseTest;
import org.junit.jupiter.api.Test;

/**
* Tests for <code>{@link org.assertj.core.api.ClassAssert#isInterface()}</code>.
*
* @author William Delanoue
*/
class ClassAssert_isInterface_Test extends ClassAssertBaseTest {
class ClassAssert_isInterface_Test {

@Override
protected ClassAssert invoke_api_method() {
return assertions.isInterface();
@Test
void should_fail_if_actual_is_null() {
// GIVEN
Class<?> actual = null;
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat(actual).isInterface());
// THEN
then(assertionError).hasMessage(shouldNotBeNull().create());
}

@Override
protected void verify_internal_effects() {
verify(classes).assertIsInterface(getInfo(assertions), getActual(assertions));
@Test
void should_fail_if_actual_is_not_interface() {
// GIVEN
Class<?> actual = String.class;
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat(actual).isInterface());
// THEN
then(assertionError).hasMessage(shouldBeInterface(actual).create());
}

@Test
void should_pass_if_actual_is_interface() {
// GIVEN
Class<?> actual = CharSequence.class;
// WHEN/THEN
assertThat(actual).isInterface();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,45 @@
*/
package org.assertj.core.api.classes;

import static org.mockito.Mockito.verify;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.error.ShouldBeInterface.shouldNotBeInterface;
import static org.assertj.core.error.ShouldNotBeNull.shouldNotBeNull;
import static org.assertj.core.util.AssertionsUtil.expectAssertionError;

import org.assertj.core.api.ClassAssert;
import org.assertj.core.api.ClassAssertBaseTest;
import org.junit.jupiter.api.Test;

/**
* Tests for <code>{@link org.assertj.core.api.ClassAssert#isNotInterface()}</code>.
*
* @author William Delanoue
*/
class ClassAssert_isNotInterface_Test extends ClassAssertBaseTest {
class ClassAssert_isNotInterface_Test {

@Override
protected ClassAssert invoke_api_method() {
return assertions.isNotInterface();
@Test
void should_fail_if_actual_is_null() {
// GIVEN
Class<?> actual = null;
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat(actual).isNotInterface());
// THEN
then(assertionError).hasMessage(shouldNotBeNull().create());
}

@Override
protected void verify_internal_effects() {
verify(classes).assertIsNotInterface(getInfo(assertions), getActual(assertions));
@Test
void should_fail_if_actual_is_interface() {
// GIVEN
Class<?> actual = CharSequence.class;
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat(actual).isNotInterface());
// THEN
then(assertionError).hasMessage(shouldNotBeInterface(actual).create());
}

@Test
void should_pass_if_actual_is_not_interface() {
// GIVEN
Class<?> actual = String.class;
// WHEN/THEN
assertThat(actual).isNotInterface();
}

}

This file was deleted.

This file was deleted.

0 comments on commit ebdc2dd

Please sign in to comment.