Skip to content

Commit

Permalink
Move isStatic / isNotStatic implementation to AbstractClassAssert
Browse files Browse the repository at this point in the history
  • Loading branch information
scordio committed Apr 13, 2023
1 parent 18af162 commit 410bb5a
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import static org.assertj.core.error.ClassModifierShouldBe.shouldBePackagePrivate;
import static org.assertj.core.error.ClassModifierShouldBe.shouldBeProtected;
import static org.assertj.core.error.ClassModifierShouldBe.shouldBePublic;
import static org.assertj.core.error.ClassModifierShouldBe.shouldBeStatic;
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.ShouldBeRecord.shouldBeRecord;
import static org.assertj.core.error.ShouldBeRecord.shouldNotBeRecord;
Expand Down Expand Up @@ -510,10 +512,15 @@ private void assertIsPackagePrivate() {
* @since 3.23.0
*/
public SELF isStatic() {
classes.assertIsStatic(info, actual);
isNotNull();
assertIsStatic();
return myself;
}

private void assertIsStatic() {
if (!Modifier.isStatic(actual.getModifiers())) throw assertionError(shouldBeStatic(actual));
}

/**
* Verifies that the actual {@code Class} is not static (does not have {@code static} modifier).
* <p>
Expand All @@ -534,10 +541,15 @@ public SELF isStatic() {
* @since 3.23.0
*/
public SELF isNotStatic() {
classes.assertIsNotStatic(info, actual);
isNotNull();
assertIsNotStatic();
return myself;
}

private void assertIsNotStatic() {
if (Modifier.isStatic(actual.getModifiers())) throw assertionError(shouldNotBeStatic(actual));
}

/**
* Verifies that the actual {@code Class} has the given {@code Annotation}s.
* <p>
Expand Down
32 changes: 0 additions & 32 deletions assertj-core/src/main/java/org/assertj/core/internal/Classes.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toCollection;
import static org.assertj.core.error.ClassModifierShouldBe.shouldBeStatic;
import static org.assertj.core.error.ClassModifierShouldBe.shouldNotBeStatic;
import static org.assertj.core.error.ShouldBeAbstract.shouldBeAbstract;
import static org.assertj.core.error.ShouldBeAnnotation.shouldBeAnnotation;
import static org.assertj.core.error.ShouldBeAnnotation.shouldNotBeAnnotation;
Expand Down Expand Up @@ -165,36 +163,6 @@ public void assertIsAnnotation(AssertionInfo info, Class<?> actual) {
if (!actual.isAnnotation()) throw failures.failure(info, shouldBeAnnotation(actual));
}

/**
* Verifies that the actual {@code Class} is static.
*
* @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 static.
*/
public void assertIsStatic(AssertionInfo info, Class<?> actual) {
assertNotNull(info, actual);
if (!Modifier.isStatic(actual.getModifiers())) {
throw failures.failure(info, shouldBeStatic(actual));
}
}

/**
* Verifies that the actual {@code Class} is not static.
*
* @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 static.
*/
public void assertIsNotStatic(AssertionInfo info, Class<?> actual) {
assertNotNull(info, actual);
if (Modifier.isStatic(actual.getModifiers())) {
throw failures.failure(info, shouldNotBeStatic(actual));
}
}

/**
* Verifies that the actual {@code Class} contains the given {@code Annotation}s.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +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.ClassModifierShouldBe.shouldNotBeStatic;
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;

class ClassAssert_isNotStatic_Test extends ClassAssertBaseTest {
class ClassAssert_isNotStatic_Test {

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

@Override
protected void verify_internal_effects() {
verify(classes).assertIsNotStatic(getInfo(assertions), getActual(assertions));
@Test
void should_fail_if_actual_is_static() {
// GIVEN
Class<?> actual = StaticClass.class;
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat(actual).isNotStatic());
// THEN
then(assertionError).hasMessage(shouldNotBeStatic(actual).create());
}

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

private static class StaticClass {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +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.ClassModifierShouldBe.shouldBeStatic;
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;

class ClassAssert_isStatic_Test extends ClassAssertBaseTest {
class ClassAssert_isStatic_Test {

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

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

@Test
void should_pass_if_actual_is_static() {
// GIVEN
Class<?> actual = StaticClass.class;
// WHEN/THEN
assertThat(actual).isStatic();
}

private static class StaticClass {
}

}

This file was deleted.

This file was deleted.

0 comments on commit 410bb5a

Please sign in to comment.