Skip to content

Commit

Permalink
Move hasPackage implementations to AbstractClassAssert
Browse files Browse the repository at this point in the history
  • Loading branch information
scordio committed May 20, 2023
1 parent 5700800 commit d69cd04
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static org.assertj.core.error.ShouldBeRecord.shouldBeRecord;
import static org.assertj.core.error.ShouldBeRecord.shouldNotBeRecord;
import static org.assertj.core.error.ShouldHaveNoSuperclass.shouldHaveNoSuperclass;
import static org.assertj.core.error.ShouldHavePackage.shouldHavePackage;
import static org.assertj.core.error.ShouldHaveRecordComponents.shouldHaveRecordComponents;
import static org.assertj.core.error.ShouldHaveSuperclass.shouldHaveSuperclass;
import static org.assertj.core.error.ShouldNotBeNull.shouldNotBeNull;
Expand Down Expand Up @@ -989,10 +990,19 @@ public SELF hasPublicMethods(String... methodNames) {
* @since 3.18.0
*/
public SELF hasPackage(String packageName) {
classes.assertHasPackage(info, actual, packageName);
isNotNull();
assertHasPackage(packageName);
return myself;
}

private void assertHasPackage(String packageName) {
requireNonNull(packageName, shouldNotBeNull("packageName")::create);
Package actualPackage = actual.getPackage();
if (actualPackage == null || !actualPackage.getName().equals(packageName)) {
throw assertionError(shouldHavePackage(actual, packageName));
}
}

/**
* Verifies that the actual {@code Class} has the given package (as in {@link Class#getPackage()}).
*
Expand All @@ -1011,16 +1021,22 @@ public SELF hasPackage(String packageName) {
* assertThat(MyClass.class).hasPackage(Package.getPackage(""));
* assertThat(MyClass.class).hasPackage(Object.class.getPackage());</code></pre>
*
* @param aPackage the package the class should have
* @param expected the package the class should have
* @return {@code this} assertions object
* @throws AssertionError if {@code actual} is {@code null}.
* @throws AssertionError if the actual {@code Class} does not have the given package.
*
* @since 3.18.0
*/
public SELF hasPackage(Package aPackage) {
classes.assertHasPackage(info, actual, aPackage);
public SELF hasPackage(Package expected) {
isNotNull();
assertHasPackage(expected);
return myself;
}

private void assertHasPackage(Package expected) {
requireNonNull(expected, shouldNotBeNull("expected")::create);
if (!expected.equals(actual.getPackage())) throw assertionError(shouldHavePackage(actual, expected));
}

}
20 changes: 0 additions & 20 deletions assertj-core/src/main/java/org/assertj/core/internal/Classes.java
Original file line number Diff line number Diff line change
Expand Up @@ -423,24 +423,4 @@ public void assertHasPackage(AssertionInfo info, Class<?> actual, String package
}
}

/**
* Verifies that the actual {@code Class} has the given {@code Package}.
*
* @param info contains information about the assertion.
* @param actual the "actual" {@code Class}.
* @param aPackage the package that must be declared in the class.
* @throws NullPointerException if {@code aPackage} is {@code null}.
* @throws AssertionError if {@code actual} is {@code null}.
* @throws AssertionError if {@code actual} does not have the given package.
*/
public void assertHasPackage(AssertionInfo info, Class<?> actual, Package aPackage) {
assertNotNull(info, actual);
requireNonNull(aPackage, shouldNotBeNull("aPackage")::create);
Package actualPackage = actual.getPackage();

if (!aPackage.equals(actualPackage)) {
throw failures.failure(info, shouldHavePackage(actual, aPackage));
}
}

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

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.error.ShouldHavePackage.shouldHavePackage;
import static org.assertj.core.error.ShouldNotBeNull.shouldNotBeNull;
import static org.assertj.core.util.AssertionsUtil.expectAssertionError;
import static org.assertj.core.util.FailureMessages.actualIsNull;

import org.assertj.core.api.ClassAssert;
import org.assertj.core.api.ClassAssertBaseTest;
import java.util.Collection;

import org.junit.jupiter.api.Test;

/**
* Tests for <code>{@link ClassAssert#hasPackage(Package)}</code>.
*
* @author Matteo Mirk
*/
class ClassAssert_hasPackage_with_Package_Test extends ClassAssertBaseTest {
class ClassAssert_hasPackage_with_Package_Test {

private static final Package PACKAGE = mock(Package.class);
@Test
void should_fail_if_actual_is_null() {
// GIVEN
Class<?> actual = null;
Package expected = Object.class.getPackage();
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat(actual).hasPackage(expected));
// THEN
then(assertionError).hasMessage(actualIsNull());
}

@Test
void should_pass_if_expected_package_is_null() {
// GIVEN
Class<?> actual = Integer.class;
Package expected = null;
// WHEN
Throwable thrown = catchThrowable(() -> assertThat(actual).hasPackage(expected));
// THEN
then(thrown).isInstanceOf(NullPointerException.class).hasMessage(shouldNotBeNull("expected").create());
}

@Override
protected ClassAssert invoke_api_method() {
return assertions.hasPackage(PACKAGE);
@Test
void should_fail_if_actual_has_not_expected_package() {
// GIVEN
Class<?> actual = Object.class;
Package expected = Collection.class.getPackage();
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat(actual).hasPackage(expected));
// THEN
then(assertionError).hasMessage(shouldHavePackage(actual, expected).create());
}

@Override
protected void verify_internal_effects() {
verify(classes).assertHasPackage(getInfo(assertions), getActual(assertions), PACKAGE);
@Test
void should_pass_if_actual_has_expected_package() {
// GIVEN
Class<?> actual = Object.class;
Package expected = Object.class.getPackage();
// WHEN/THEN
assertThat(actual).hasPackage(expected);
}

}

This file was deleted.

0 comments on commit d69cd04

Please sign in to comment.