forked from assertj/assertj
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add assertions for java.lang.reflect. Fix assertj#3130
This changes adds assertions for classes: * Field * Method * Constructor
- Loading branch information
William Bakker
committed
Jul 31, 2023
1 parent
a3bca4e
commit 82bddaf
Showing
46 changed files
with
2,709 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
assertj-core/src/main/java/org/assertj/core/api/AbstractConstructorAssert.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "<a href="http://bit.ly/1IZIRcY" | ||
* target="_blank">Emulating 'self types' using Java Generics to simplify fluent API implementation</a>" | ||
* 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); | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
assertj-core/src/main/java/org/assertj/core/api/AbstractExecutableAssert.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "<a href="http://bit.ly/1IZIRcY" | ||
* target="_blank">Emulating 'self types' using Java Generics to simplify fluent API implementation</a>" | ||
* 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; | ||
} | ||
} |
178 changes: 178 additions & 0 deletions
178
assertj-core/src/main/java/org/assertj/core/api/AbstractFieldAssert.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "<a href="http://bit.ly/1IZIRcY" | ||
* target="_blank">Emulating 'self types' using Java Generics to simplify fluent API implementation</a>" | ||
* 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 field='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 field='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 field='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 field='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)); | ||
} | ||
} |
Oops, something went wrong.