Skip to content

Commit

Permalink
Add class/method visibility assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
epeee authored and joel-costigliola committed Feb 26, 2017
1 parent ea0d04a commit 1a0c2a6
Show file tree
Hide file tree
Showing 25 changed files with 1,236 additions and 29 deletions.
162 changes: 162 additions & 0 deletions src/main/java/org/assertj/core/api/AbstractClassAssert.java
Expand Up @@ -178,6 +178,51 @@ public SELF isNotFinal() {
return myself;
}

/**
* Verifies that the actual {@code Class} is public (has {@code public} modifier).
*
* <pre><code class='java'> protected class MyClass { }
*
* // These assertions succeed:
* assertThat(String.class).isPublic();
* assertThat(Math.class).isPublic();
*
* // This assertion fails:
* assertThat(MyClass.class).isPublic();</code></pre>
*
* @throws AssertionError if {@code actual} is {@code null}.
* @throws AssertionError if the actual {@code Class} is not public.
*
* @since 2.7.0 / 3.7.0
*/
public SELF isPublic() {
classes.assertIsPublic(info, actual);
return myself;
}

/**
* Verifies that the actual {@code Class} is protected (has {@code protected} modifier).
*
* <pre><code class='java'> public class MyClass { }
*
* // This assertion succeeds:
* assertThat(MyClass.class).isProtected();
*
* // These assertions fail:
* assertThat(String.class).isProtected();
* assertThat(Math.class).isProtected();</code></pre>
*
* @throws AssertionError if {@code actual} is {@code null}.
* @throws AssertionError if the actual {@code Class} is not protected.
*
* @since 2.7.0 / 3.7.0
*/
public SELF isProtected() {
classes.assertIsProtected(info, actual);
return myself;
}


/**
* Verifies that the actual {@code Class} has the given {@code Annotation}s.
*
Expand Down Expand Up @@ -284,4 +329,121 @@ public SELF hasDeclaredFields(String... fields) {
classes.assertHasDeclaredFields(info, actual, fields);
return myself;
}

/**
* Verifies that the actual {@code Class} has the given methods.
*
* <pre><code class='java'> class MyClass {
* public void methodOne() {}
* private void methodTwo() {}
* }
*
* // This assertion succeeds:
* assertThat(MyClass.class).hasMethods("methodOne");
*
* // These assertions fail:
* assertThat(MyClass.class).hasMethods("methodTwo");
* assertThat(MyClass.class).hasMethods("methodThree");</code></pre>
*
* @param methodNames the method names which must be in the class.
* @throws AssertionError if {@code actual} is {@code null}.
* @throws AssertionError if the actual {@code Class} doesn't contains all of the method names.
*
* @since 2.7.0 / 3.7.0
*/
public SELF hasMethods(String... methodNames) {
classes.assertHasMethods(info, actual, methodNames);
return myself;
}

/**
* Verifies that the actual {@code Class} has the given declared methods.
*
* <pre><code class='java'> class MySuperClass {
* public void superMethod() {}
* }
*
* class MyClass extends MySuperClass {
* public void methodOne() {}
* private void methodTwo() {}
* }
*
* // This assertion succeeds:
* assertThat(MyClass.class).hasDeclaredMethods("methodOne", "methodTwo");
*
* // These assertions fail:
* assertThat(MyClass.class).hasDeclaredMethods("superMethod");
* assertThat(MyClass.class).hasDeclaredMethods("methodThree");</code></pre>
*
* @param methodNames the method names which must be declared in the class.
* @throws AssertionError if {@code actual} is {@code null}.
* @throws AssertionError if the actual {@code Class} doesn't contains all of the method names.
*
* @since 2.7.0 / 3.7.0
*/
public SELF hasDeclaredMethods(String... methodNames) {
classes.assertHasDeclaredMethods(info, actual, methodNames);
return myself;
}

/**
* Verifies that the actual {@code Class} has the given public methods.
*
* <pre><code class='java'> class MyClass {
* public void methodOne() {}
* public void methodTwo() {}
* protected void methodThree() {}
* }
*
* // These assertions succeed:
* assertThat(MyClass.class).hasPublicMethods("methodOne");
* assertThat(MyClass.class).hasPublicMethods("methodOne", "methodTwo");
*
* // These assertions fail:
* assertThat(MyClass.class).hasPublicMethods("methodOne", "methodThree");
* assertThat(MyClass.class).hasPublicMethods("methodThree");</code></pre>
*
* @param methodNames the public method names which must be in the class.
* @throws AssertionError if {@code actual} is {@code null}.
* @throws AssertionError if the actual {@code Class} doesn't contains all of the public method names.
*
* @since 2.7.0 / 3.7.0
*/
public SELF hasPublicMethods(String... methodNames) {
classes.assertHasPublicMethods(info, actual, methodNames);
return myself;
}

/**
* Verifies that the actual {@code Class} has the given declared public methods.
*
* <pre><code class='java'> class MySuperClass {
* public void superMethod() {}
* }
*
* class MyClass extends MySuperClass{
* public void methodOne() {}
* public void methodTwo() {}
* protected void methodThree() {}
* }
*
* // These assertions succeed:
* assertThat(MyClass.class).hasDeclaredPublicMethods("methodOne");
* assertThat(MyClass.class).hasDeclaredPublicMethods("methodOne", "methodTwo");
*
* // These assertions fail:
* assertThat(MyClass.class).hasDeclaredPublicMethods("superMethod");
* assertThat(MyClass.class).hasDeclaredPublicMethods("methodOne", "methodThree");
* assertThat(MyClass.class).hasDeclaredPublicMethods("methodThree");</code></pre>
*
* @param methodNames the public method names which must be declared in the class.
* @throws AssertionError if {@code actual} is {@code null}.
* @throws AssertionError if the actual {@code Class} doesn't contains all of the public method names.
*
* @since 2.7.0 / 3.7.0
*/
public SELF hasDeclaredPublicMethods(String... methodNames) {
classes.assertHasDeclaredPublicMethods(info, actual, methodNames);
return myself;
}
}
Expand Up @@ -12,34 +12,57 @@
*/
package org.assertj.core.error;

import java.lang.reflect.Modifier;

/**
* Creates an error message indicating that an assertion that verifies that a class is (or is not) final.
*
* @author Michal Kordas
*/
public class ShouldBeFinal extends BasicErrorMessageFactory {
public class ClassModifierShouldBe extends BasicErrorMessageFactory {

private ShouldBeFinal(Class<?> actual, boolean positive) {
super("%nExpecting:%n <%s>%n" + (positive ? "to" : "not to") + " be a final class.", actual);
private ClassModifierShouldBe(Class<?> actual, boolean positive, String modifier) {
super("%nExpecting:%n <%s>%n" + (positive ? "to" : "not to") + " be a %s class but was %s.", actual, modifier, Modifier.toString(actual.getModifiers()));
}

/**
* Creates a new {@link ShouldBeFinal}.
* Creates a new {@link ClassModifierShouldBe}.
*
* @param actual the actual value in the failed assertion.
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldBeFinal(Class<?> actual) {
return new ShouldBeFinal(actual, true);
return new ClassModifierShouldBe(actual, true, Modifier.toString(Modifier.FINAL));
}

/**
* Creates a new {@link ShouldBeFinal}.
* Creates a new {@link ClassModifierShouldBe}.
*
* @param actual the actual value in the failed assertion.
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldNotBeFinal(Class<?> actual) {
return new ShouldBeFinal(actual, false);
return new ClassModifierShouldBe(actual, false, Modifier.toString(Modifier.FINAL));
}

/**
* Creates a new {@link ClassModifierShouldBe}.
*
* @param actual the actual value in the failed assertion.
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldBePublic(Class<?> actual) {
return new ClassModifierShouldBe(actual, true, Modifier.toString(Modifier.PUBLIC));
}

/**
* Creates a new {@link ClassModifierShouldBe}.
*
* @param actual the actual value in the failed assertion.
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldBeProtected(Class<?> actual) {
return new ClassModifierShouldBe(actual, true, Modifier.toString(Modifier.PROTECTED));
}

}
83 changes: 83 additions & 0 deletions src/main/java/org/assertj/core/error/ShouldHaveMethods.java
@@ -0,0 +1,83 @@
/**
* 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-2017 the original author or authors.
*/
package org.assertj.core.error;

import java.util.Map;
import java.util.Set;
import java.util.SortedSet;

/**
* Creates an error message indicating that an assertion that verifies that a class have methods.
*/
public class ShouldHaveMethods extends BasicErrorMessageFactory {

/**
* Creates a new </code>{@link org.assertj.core.error.ShouldHaveMethods}</code>.
*
* @param actual the actual value in the failed assertion.
* @param declared {@code true} if the expected methods are declared ones, {@code false} otherwise.
* @param expected expected methods for this class
* @param missing missing methods for this class
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldHaveMethods(Class<?> actual, boolean declared, SortedSet<String> expected,
SortedSet<String> missing) {
return new ShouldHaveMethods(actual, expected, missing, declared);
}

public static ErrorMessageFactory shouldHaveMethods(Class<?> actual, boolean declared, SortedSet<String> expected,
String modifier, Map<String, String> nonMatching) {
return new ShouldHaveMethods(actual, expected, modifier, nonMatching, declared);
}

public static ErrorMessageFactory shouldNotHaveMethods(Class<?> actual, String modifier, boolean declared,
SortedSet<String> actualMethodsHavingModifier) {
return new ShouldHaveMethods(actual, modifier, declared, actualMethodsHavingModifier);
}

public static ErrorMessageFactory shouldNotHaveMethods(Class<?> actual, boolean declared,
SortedSet<String> actualMethodsHavingModifier) {
return new ShouldHaveMethods(actual, null, declared, actualMethodsHavingModifier);
}

private ShouldHaveMethods(Class<?> actual, Set<String> expected, Set<String> missing, boolean declared) {
super("%n" +
"Expecting%n" +
" <%s>%n" +
"to have " + (declared ? "declared " : "") + "methods:%n" +
" <%s>%n" +
"but could not find:%n" +
" <%s>", actual, expected, missing);
}

private ShouldHaveMethods(Class<?> actual, Set<String> expected, String modifier, Map<String, String> nonMatching,
boolean declared) {
super("%n" +
"Expecting%n" +
" <%s>%n" +
"to have " + (declared ? "declared " : "") + modifier + " " + "methods:%n" +
" <%s>%n" +
"but the following are not " + modifier + ":%n" +
" <%s>", actual, expected, nonMatching);
}

private ShouldHaveMethods(Class<?> actual, String modifier, boolean declared,
Set<String> actualMethodsHavingModifier) {
super("%n" +
"Expecting%n" +
" <%s>%n" +
"not to have any " + (declared ? "declared " : "")
+ (modifier != null && modifier.length() > 0 ? modifier + " " : "") + "methods but had the following:%n" +
" <%s>", actual, actualMethodsHavingModifier);
}
}

0 comments on commit 1a0c2a6

Please sign in to comment.