Skip to content

Commit

Permalink
Add isPrivate to Class assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
scordio committed Apr 12, 2024
1 parent d1e4bbf commit a1e7669
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 14 deletions.
Expand Up @@ -15,6 +15,7 @@
import static java.util.Objects.requireNonNull;
import static org.assertj.core.error.ClassModifierShouldBe.shouldBeFinal;
import static org.assertj.core.error.ClassModifierShouldBe.shouldBePackagePrivate;
import static org.assertj.core.error.ClassModifierShouldBe.shouldBePrivate;
import static org.assertj.core.error.ClassModifierShouldBe.shouldBeProtected;
import static org.assertj.core.error.ClassModifierShouldBe.shouldBePublic;
import static org.assertj.core.error.ClassModifierShouldBe.shouldBeStatic;
Expand Down Expand Up @@ -508,7 +509,7 @@ private void assertIsProtected() {
}

/**
* Verifies that the actual {@code Class} is package-private (has no modifier).
* Verifies that the actual {@code Class} is package-private (i.e., has no explicit access level modifier).
* <p>
* Example:
* <pre><code class='java'> class MyClass { }
Expand Down Expand Up @@ -539,6 +540,37 @@ private void assertIsPackagePrivate() {
}
}

/**
* Verifies that the actual {@code Class} is private (has {@code private} modifier).
* <p>
* Example:
* <pre><code class='java'> class EnclosingClass {
* private static class PrivateClass { }
* }
*
* // these assertions succeed:
* assertThat(PrivateClass.class).isPrivate();
* assertThat(Class.forName(EnclosingClass.class.getName() + "$PrivateClass")).isPrivate();
*
* // This assertion fails:
* assertThat(String.class).isPrivate();</code></pre>
*
* @return {@code this} assertions object
* @throws AssertionError if {@code actual} is {@code null}.
* @throws AssertionError if the actual {@code Class} is not private.
*
* @since 3.26.0
*/
public SELF isPrivate() {
isNotNull();
assertIsPrivate();
return myself;
}

private void assertIsPrivate() {
if (!Modifier.isPrivate(actual.getModifiers())) throw assertionError(shouldBePrivate(actual));
}

/**
* Verifies that the actual {@code Class} is static (has {@code static} modifier).
* <p>
Expand Down
Expand Up @@ -83,6 +83,16 @@ public static ErrorMessageFactory shouldBePackagePrivate(Class<?> actual) {
return new ClassModifierShouldBe(actual, true, PACKAGE_PRIVATE);
}

/**
* Creates a new instance for a positive check of the {@code private} modifier.
*
* @param actual the actual value in the failed assertion.
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldBePrivate(Class<?> actual) {
return new ClassModifierShouldBe(actual, true, Modifier.toString(Modifier.PRIVATE));
}

/**
* Creates a new instance for a positive check of the {@code static} modifier.
*
Expand Down
@@ -0,0 +1,64 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2024 the original author or authors.
*/
package org.assertj.core.api.classes;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.error.ClassModifierShouldBe.shouldBePrivate;
import static org.assertj.core.error.ShouldNotBeNull.shouldNotBeNull;
import static org.assertj.core.util.AssertionsUtil.expectAssertionError;

import java.util.stream.Stream;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

class ClassAssert_isPrivate_Test {

@Test
void should_fail_if_actual_is_null() {
// GIVEN
Class<?> actual = null;
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat(actual).isPrivate());
// THEN
then(assertionError).hasMessage(shouldNotBeNull().create());
}

@Test
void should_fail_if_actual_is_not_private() {
// GIVEN
Class<?> actual = String.class;
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat(actual).isPrivate());
// THEN
then(assertionError).hasMessage(shouldBePrivate(actual).create());
}

@ParameterizedTest
@MethodSource("parameters")
void should_pass_if_actual_is_private(Class<?> actual) {
// WHEN/THEN
assertThat(actual).isPrivate();
}

static Stream<Class<?>> parameters() throws ClassNotFoundException {
return Stream.of(PrivateClass.class,
Class.forName(ClassAssert_isPrivate_Test.class.getName() + "$PrivateClass"));
}

private static class PrivateClass {
}

}
Expand Up @@ -15,6 +15,7 @@
import static java.lang.String.format;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.error.ClassModifierShouldBe.shouldBeFinal;
import static org.assertj.core.error.ClassModifierShouldBe.shouldBePrivate;
import static org.assertj.core.error.ClassModifierShouldBe.shouldBePublic;
import static org.assertj.core.error.ClassModifierShouldBe.shouldNotBeFinal;

Expand All @@ -24,7 +25,7 @@
class ClassModifierShouldBe_create_Test {

@Test
void should_create_error_message_for_is_final() {
void should_create_error_message_for_shouldBeFinal() {
// GIVEN
Class<?> nonFinalClass = Object.class;
// WHEN
Expand All @@ -37,7 +38,7 @@ void should_create_error_message_for_is_final() {
}

@Test
void should_create_error_message_for_is_not_final() {
void should_create_error_message_for_shouldNotBeFinal() {
// GIVEN
Class<?> finalClass = String.class;
// WHEN
Expand All @@ -50,32 +51,29 @@ void should_create_error_message_for_is_not_final() {
}

@Test
void should_create_clear_error_message_when_actual_is_package_private_enum() {
void should_create_error_message_for_shouldBePublic() {
// GIVEN
Class<?> packagePrivateEnum = PackagePrivateEnum.class;
Class<?> packagePrivateClass = PackagePrivateClass.class;
// WHEN
String error = shouldBePublic(packagePrivateEnum).create(new TestDescription("TEST"));
String error = shouldBePublic(packagePrivateClass).create(new TestDescription("TEST"));
// THEN
then(error).isEqualTo(format("[TEST] %n" +
"Expecting actual:%n" +
" org.assertj.core.error.ClassModifierShouldBe_create_Test.PackagePrivateEnum%n" +
"to be a \"public\" class but was \"package-private static final\"."));
" org.assertj.core.error.ClassModifierShouldBe_create_Test.PackagePrivateClass%n" +
"to be a \"public\" class but was \"package-private\"."));
}

@Test
void should_create_clear_error_message_when_actual_is_only_package_private() {
void should_create_error_message_for_shouldBePrivate() {
// GIVEN
Class<?> packagePrivateClass = PackagePrivateClass.class;
// WHEN
String error = shouldBePublic(packagePrivateClass).create(new TestDescription("TEST"));
String error = shouldBePrivate(packagePrivateClass).create(new TestDescription("TEST"));
// THEN
then(error).isEqualTo(format("[TEST] %n" +
"Expecting actual:%n" +
" org.assertj.core.error.ClassModifierShouldBe_create_Test.PackagePrivateClass%n" +
"to be a \"public\" class but was \"package-private\"."));
}

enum PackagePrivateEnum {
"to be a \"private\" class but was \"package-private\"."));
}

class PackagePrivateClass {
Expand Down

0 comments on commit a1e7669

Please sign in to comment.