Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assertions for java.lang.reflect. Fix #3130 #3131

Open
wants to merge 4 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
* @author Mikhail Mazursky
*/
public abstract class AbstractClassAssert<SELF extends AbstractClassAssert<SELF>>
extends AbstractAssert<SELF, Class<?>> {
extends AbstractAssert<SELF, Class<?>>
implements AnnotatedElementAssert<SELF, Class<?>> {

Classes classes = Classes.instance();

Expand Down Expand Up @@ -595,36 +596,8 @@ 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>
* Example:
* <pre><code class='java'> &#64;Target(ElementType.TYPE)
* &#64;Retention(RetentionPolicy.RUNTIME)
* private static @interface Force { }
*
* &#64;Target(ElementType.TYPE)
* &#64;Retention(RetentionPolicy.RUNTIME)
* private static @interface Hero { }
*
* &#64;Target(ElementType.TYPE)
* &#64;Retention(RetentionPolicy.RUNTIME)
* private static @interface DarkSide { }
*
* &#64;Hero &#64;Force
* class Jedi implements Jedi {}
*
* // this assertion succeeds:
* assertThat(Jedi.class).containsAnnotations(Force.class, Hero.class);
*
* // this assertion fails:
* assertThat(Jedi.class).containsAnnotations(Force.class, DarkSide.class);</code></pre>
*
* @param annotations annotations who must be attached to the class
* @return {@code this} assertions object
* @throws AssertionError if {@code actual} is {@code null}.
* @throws AssertionError if the actual {@code Class} doesn't contains all of these annotations.
*/
/** {@inheritDoc} */
@Override
@SafeVarargs
public final SELF hasAnnotations(Class<? extends Annotation>... annotations) {
return hasAnnotationsForProxy(annotations);
Expand All @@ -638,27 +611,8 @@ protected SELF hasAnnotationsForProxy(Class<? extends Annotation>[] annotations)
return myself;
}

/**
* Verifies that the actual {@code Class} has the given {@code Annotation}.
* <p>
* Example:
* <pre><code class='java'> &#64;Target(ElementType.TYPE)
* &#64;Retention(RetentionPolicy.RUNTIME)
* private static @interface Force { }
* &#64;Force
* class Jedi implements Jedi {}
*
* // this assertion succeeds:
* assertThat(Jedi.class).containsAnnotation(Force.class);
*
* // this assertion fails:
* assertThat(Jedi.class).containsAnnotation(DarkSide.class);</code></pre>
*
* @param annotation annotations who must be attached to the class
* @return {@code this} assertions object
* @throws AssertionError if {@code actual} is {@code null}.
* @throws AssertionError if the actual {@code Class} doesn't contains all of these annotations.
*/
/** {@inheritDoc} */
@Override
public SELF hasAnnotation(Class<? extends Annotation> annotation) {
classes.assertContainsAnnotations(info, actual, array(annotation));
return myself;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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-2023 the original author or authors.
*/
package org.assertj.core.api;

import java.lang.reflect.Constructor;

/**
* Base class for all implementations of assertions for {@link Constructor}s.
*
* @param <SELF> the "self" type of this assertion class. Please read &quot;<a href="http://bit.ly/1IZIRcY"
* target="_blank">Emulating 'self types' using Java Generics to simplify fluent API implementation</a>&quot;
* for more details.
*
* @author William Bakker
*/
public abstract class AbstractConstructorAssert<SELF extends AbstractConstructorAssert<SELF, ACTUAL>, ACTUAL extends Constructor<?>>
extends AbstractExecutableAssert<SELF, Constructor<?>> {

protected AbstractConstructorAssert(Constructor<?> actual, Class<?> selfType) {
super(actual, selfType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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-2023 the original author or authors.
*/
package org.assertj.core.api;

import org.assertj.core.internal.Executables;

import java.lang.annotation.Annotation;
import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import static org.assertj.core.error.MemberModifierShouldBe.shouldBePackagePrivate;
import static org.assertj.core.error.MemberModifierShouldBe.shouldBeProtected;
import static org.assertj.core.error.MemberModifierShouldBe.shouldBePublic;
import static org.assertj.core.util.Arrays.array;

/**
* Base class for all implementations of assertions for {@link Method}s.
*
* @param <SELF> the "self" type of this assertion class. Please read &quot;<a href="http://bit.ly/1IZIRcY"
* target="_blank">Emulating 'self types' using Java Generics to simplify fluent API implementation</a>&quot;
* for more details.
*
* @author William Bakker
*/
public abstract class AbstractExecutableAssert<SELF extends AbstractExecutableAssert<SELF, ACTUAL>, ACTUAL extends Executable>
extends AbstractAssert<SELF, ACTUAL> implements ExecutableAssert<SELF, ACTUAL> {

Executables executables = Executables.instance();

protected AbstractExecutableAssert(ACTUAL actual, Class<?> selfType) {
super(actual, selfType);
}

/** {@inheritDoc} */
@Override
public SELF isPublic() {
isNotNull();
assertIsPublic();
return myself;
}

private void assertIsPublic() {
if (!Modifier.isPublic(actual.getModifiers())) throw assertionError(shouldBePublic(actual));
}

/** {@inheritDoc} */
@Override
public SELF isProtected() {
isNotNull();
assertIsProtected();
return myself;
}

private void assertIsProtected() {
if (!Modifier.isProtected(actual.getModifiers())) throw assertionError(shouldBeProtected(actual));
}

/** {@inheritDoc} */
@Override
public SELF isPackagePrivate() {
isNotNull();
assertIsPackagePrivate();
return myself;
}

private void assertIsPackagePrivate() {
final int modifiers = actual.getModifiers();
if (Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers) || Modifier.isPrivate(modifiers)) {
throw assertionError(shouldBePackagePrivate(actual));
}
}

/** {@inheritDoc} */
@SafeVarargs
@Override
public final SELF hasAnnotations(Class<? extends Annotation>... annotations) {
return hasAnnotationsForProxy(annotations);
}

// This method is protected in order to be proxied for SoftAssertions / Assumptions.
// The public method for it (the one not ending with "ForProxy") is marked as final and annotated with @SafeVarargs
// in order to avoid compiler warning in user code
protected SELF hasAnnotationsForProxy(Class<? extends Annotation>[] annotations) {
executables.assertContainsAnnotations(info, actual, annotations);
return myself;
}

/** {@inheritDoc} */
@Override
public SELF hasAnnotation(Class<? extends Annotation> annotation) {
executables.assertContainsAnnotations(info, actual, array(annotation));
return myself;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* 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-2023 the original author or authors.
*/
package org.assertj.core.api;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

import static org.assertj.core.error.MemberModifierShouldBe.shouldBeFinal;
import static org.assertj.core.error.MemberModifierShouldBe.shouldBePackagePrivate;
import static org.assertj.core.error.MemberModifierShouldBe.shouldBeProtected;
import static org.assertj.core.error.MemberModifierShouldBe.shouldBePublic;
import static org.assertj.core.error.MemberModifierShouldBe.shouldBeStatic;
import static org.assertj.core.error.MemberModifierShouldBe.shouldNotBeFinal;
import static org.assertj.core.error.MemberModifierShouldBe.shouldNotBeStatic;

/**
* Base class for all implementations of assertions for {@link Field}s.
*
* @param <SELF> the "self" type of this assertion class. Please read &quot;<a href="http://bit.ly/1IZIRcY"
* target="_blank">Emulating 'self types' using Java Generics to simplify fluent API implementation</a>&quot;
* for more details.
*
* @author William Bakker
*/
public abstract class AbstractFieldAssert<SELF extends AbstractFieldAssert<SELF>>
extends AbstractAssert<SELF, Field> implements MemberAssert<SELF, Field> {

protected AbstractFieldAssert(Field actual, Class<?> selfType) {
super(actual, selfType);
}

/** {@inheritDoc} */
@Override
public SELF isPublic() {
isNotNull();
assertIsPublic();
return myself;
}

private void assertIsPublic() {
if (!Modifier.isPublic(actual.getModifiers())) throw assertionError(shouldBePublic(actual));
}

/** {@inheritDoc} */
@Override
public SELF isProtected() {
isNotNull();
assertIsProtected();
return myself;
}

private void assertIsProtected() {
if (!Modifier.isProtected(actual.getModifiers())) throw assertionError(shouldBeProtected(actual));
}

/** {@inheritDoc} */
@Override
public SELF isPackagePrivate() {
isNotNull();
assertIsPackagePrivate();
return myself;
}

private void assertIsPackagePrivate() {
final int modifiers = actual.getModifiers();
if (Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers) || Modifier.isPrivate(modifiers)) {
throw assertionError(shouldBePackagePrivate(actual));
}
}

/**
* Verifies that the actual {@code Field} is final (has {@code final} modifier).
* <p>
* Example:
* <pre><code class='java'> // this assertion succeeds:
* assertThat(Math.class.getDeclaredField("PI")).isFinal();
*
* // this assertion fails:
* assertThat(AtomicLong.class.getDeclaredField("value")).isFinal(); </code></pre>
*
* @return {@code this} assertions object
* @throws AssertionError if {@code actual} is {@code null}.
* @throws AssertionError if the actual {@code Field} is not final.
*/
public SELF isFinal() {
isNotNull();
assertIsFinal();
return myself;
}

private void assertIsFinal() {
if (!Modifier.isFinal(actual.getModifiers())) throw assertionError(shouldBeFinal(actual));
}

/**
* Verifies that the actual {@code Field} is not final (does not have {@code final} modifier).
* <p>
* Example:
* <pre><code class='java'> // this assertion succeeds:
* assertThat(AtomicLong.class.getDeclaredField("value")).isNotFinal();
*
* // this assertion fails:
* assertThat(Math.class.getDeclaredField("PI")).isNotFinal(); </code></pre>
*
* @return {@code this} assertions object
* @throws AssertionError if {@code actual} is {@code null}.
* @throws AssertionError if the actual {@code Field} is final.
*/
public SELF isNotFinal() {
isNotNull();
assertIsNotFinal();
return myself;
}

private void assertIsNotFinal() {
if (Modifier.isFinal(actual.getModifiers())) throw assertionError(shouldNotBeFinal(actual));
}

/**
* Verifies that the actual {@code Field} is static (has {@code static} modifier).
* <p>
* Example:
* <pre><code class='java'> // this assertion succeeds:
* assertThat(Math.class.getDeclaredField("PI")).isStatic();
*
* // this assertion fails:
* assertThat(AtomicLong.class.getDeclaredField("value")).isStatic(); </code></pre>
*
* @return {@code this} assertions object
* @throws AssertionError if {@code actual} is {@code null}.
* @throws AssertionError if the actual {@code Field} is not static.
* @since 3.23.0
*/
public SELF isStatic() {
isNotNull();
assertIsStatic();
return myself;
}

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

/**
* Verifies that the actual {@code Field} is not static (does not have {@code static} modifier).
* <p>
* Example:
* <pre><code class='java'> // this assertion succeeds:
* assertThat(AtomicLong.class.getDeclaredField("value")).isNotStatic();
*
* // this assertion fails:
* assertThat(Math.class.getDeclaredField("PI")).isNotStatic(); </code></pre>
*
* @return {@code this} assertions object
* @throws AssertionError if {@code actual} is {@code null}.
* @throws AssertionError if the actual {@code Field} is static.
* @since 3.23.0
*/
public SELF isNotStatic() {
isNotNull();
assertIsNotStatic();
return myself;
}

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