Skip to content

Commit

Permalink
Add hasPackage Class assertion (#2019)
Browse files Browse the repository at this point in the history
  • Loading branch information
polarene authored Oct 24, 2020
1 parent 9774849 commit 1873ff0
Show file tree
Hide file tree
Showing 9 changed files with 485 additions and 4 deletions.
60 changes: 60 additions & 0 deletions src/main/java/org/assertj/core/api/AbstractClassAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -647,4 +647,64 @@ public SELF hasPublicMethods(String... methodNames) {
classes.assertHasPublicMethods(info, actual, methodNames);
return myself;
}

/**
* Verifies that the actual {@code Class} has the given package name (as in {@link Class#getPackage()}).
*
* <p>
* Example:
* <pre><code class='java'> package one.two;
*
* class MyClass {}
*
* // this assertions succeeds:
* assertThat(MyClass.class).hasPackage("one.two");
*
* // these assertions fail:
* assertThat(MyClass.class).hasPackage("one");
* assertThat(MyClass.class).hasPackage("");
* assertThat(MyClass.class).hasPackage("java.lang");</code></pre>
*
* @param packageName the package name 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(String packageName) {
classes.assertHasPackage(info, actual, packageName);
return myself;
}

/**
* Verifies that the actual {@code Class} has the given package (as in {@link Class#getPackage()}).
*
* <p>
* Example:
* <pre><code class='java'> package one.two;
*
* class MyClass {}
**
* // these assertions succeed:
* assertThat(MyClass.class).hasPackage(Package.getPackage("one.two"));
* assertThat(MyClass.class).hasPackage(MyClass.class.getPackage());
*
* // these assertions fail:
* assertThat(MyClass.class).hasPackage(Package.getPackage("one"));
* assertThat(MyClass.class).hasPackage(Package.getPackage(""));
* assertThat(MyClass.class).hasPackage(Object.class.getPackage());</code></pre>
*
* @param aPackage 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);
return myself;
}

}
67 changes: 67 additions & 0 deletions src/main/java/org/assertj/core/error/ShouldHavePackage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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-2020 the original author or authors.
*/
package org.assertj.core.error;

import java.util.StringJoiner;

/**
* Creates an error message indicating that a {@link Class} should have a given package.
*
* @author Matteo Mirk
*/
public class ShouldHavePackage extends BasicErrorMessageFactory {
private static final String SHOULD_HAVE_PACKAGE = new StringJoiner("%n", "%n", "").add("Expecting")
.add(" <%s>")
.add("to have package:")
.add(" <%s>")
.toString();
private static final String BUT_HAD_NONE = new StringJoiner("%n", "%n", "").add("but had none.")
.toString();
private static final String BUT_HAD = new StringJoiner("%n", "%n", "").add("but had:")
.add(" <%s>")
.toString();

/**
* Creates a new <code>ShouldHavePackage</code> with a {@link Package} instance.
*
* @param actual the actual value in the failed assertion.
* @param aPackage the expected package
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldHavePackage(Class<?> actual, Package aPackage) {
return shouldHavePackage(actual, aPackage.getName());
}

/**
* Creates a new <code>ShouldHavePackage</code> with a package name.
*
* @param actual the actual value in the failed assertion.
* @param packageName the expected package name
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldHavePackage(Class<?> actual, String packageName) {
final Package actualPackage = actual.getPackage();
return (actualPackage == null)
? new ShouldHavePackage(actual, packageName)
: new ShouldHavePackage(actual, packageName, actualPackage.getName());
}

private ShouldHavePackage(Class<?> actual, String expectedPackage) {
super(SHOULD_HAVE_PACKAGE + BUT_HAD_NONE, actual, expectedPackage);
}

private ShouldHavePackage(Class<?> actual, String expectedPackage, String actualPackage) {
super(SHOULD_HAVE_PACKAGE + BUT_HAD, actual, expectedPackage, actualPackage);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ public class ShouldHaveSuperclass extends BasicErrorMessageFactory {
*/
public static ErrorMessageFactory shouldHaveSuperclass(Class<?> actual, Class<?> superclass) {
Class<?> actualSuperclass = actual.getSuperclass();
if (actualSuperclass == null) {
return new ShouldHaveSuperclass(actual, superclass);
}
return new ShouldHaveSuperclass(actual, superclass, actualSuperclass);
return (actualSuperclass == null)
? new ShouldHaveSuperclass(actual, superclass)
: new ShouldHaveSuperclass(actual, superclass, actualSuperclass);
}

private ShouldHaveSuperclass(Class<?> actual, Class<?> expectedSuperclass) {
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/org/assertj/core/internal/Classes.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import static org.assertj.core.error.ShouldHaveNoFields.shouldHaveNoDeclaredFields;
import static org.assertj.core.error.ShouldHaveNoFields.shouldHaveNoPublicFields;
import static org.assertj.core.error.ShouldHaveNoSuperclass.shouldHaveNoSuperclass;
import static org.assertj.core.error.ShouldHavePackage.shouldHavePackage;
import static org.assertj.core.error.ShouldHaveSuperclass.shouldHaveSuperclass;
import static org.assertj.core.error.ShouldNotBeNull.shouldNotBeNull;
import static org.assertj.core.error.ShouldOnlyHaveFields.shouldOnlyHaveDeclaredFields;
Expand Down Expand Up @@ -581,4 +582,45 @@ private static void assertNotNull(AssertionInfo info, Class<?> actual) {
private static void classParameterIsNotNull(Class<?> clazz) {
requireNonNull(clazz, "The class to compare actual with should not be null");
}

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

if (actualPackage == null || !actualPackage.getName().equals(packageName)) {
throw failures.failure(info, shouldHavePackage(actual, packageName));
}
}

/**
* 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
@@ -0,0 +1,42 @@
/*
* 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-2020 the original author or authors.
*/
package org.assertj.core.api.classes;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import org.assertj.core.api.ClassAssert;
import org.assertj.core.api.ClassAssertBaseTest;
import org.junit.jupiter.api.DisplayName;

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

private static final Package PACKAGE = mock(Package.class);

@Override
protected ClassAssert invoke_api_method() {
return assertions.hasPackage(PACKAGE);
}

@Override
protected void verify_internal_effects() {
verify(classes).assertHasPackage(getInfo(assertions), getActual(assertions), PACKAGE);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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-2020 the original author or authors.
*/
package org.assertj.core.api.classes;

import static org.mockito.Mockito.verify;

import org.assertj.core.api.ClassAssert;
import org.assertj.core.api.ClassAssertBaseTest;
import org.junit.jupiter.api.DisplayName;

/**
* Tests for <code>{@link ClassAssert#hasPackage(String)}</code>.
*
* @author Matteo Mirk
*/
@DisplayName("ClassAssert hasPackage(String)")
class ClassAssert_hasPackage_with_String_Test extends ClassAssertBaseTest {

private static final String PACKAGE = "org.assertj.core.api";

@Override
protected ClassAssert invoke_api_method() {
return assertions.hasPackage(PACKAGE);
}

@Override
protected void verify_internal_effects() {
verify(classes).assertHasPackage(getInfo(assertions), getActual(assertions), PACKAGE);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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-2020 the original author or authors.
*/
package org.assertj.core.error;

import static java.lang.String.format;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.error.ShouldHavePackage.shouldHavePackage;
import static org.assertj.core.presentation.StandardRepresentation.STANDARD_REPRESENTATION;

import java.util.Collection;

import org.assertj.core.description.Description;
import org.assertj.core.internal.TestDescription;
import org.assertj.core.presentation.Representation;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

/**
* Tests for <code>{@link ShouldHavePackage#create(Description, Representation)}</code>.
*
* @author Stefano Cordio
*/
@DisplayName("ShouldHavePackage create")
class ShouldHavePackage_create_Test {

@Test
void should_create_error_message_with_String_if_actual_has_package() {
// WHEN
String message = shouldHavePackage(Object.class, "java.util").create(new TestDescription("TEST"),
STANDARD_REPRESENTATION);
// THEN
then(message).isEqualTo(format("[TEST] %n" +
"Expecting%n" +
" <java.lang.Object>%n" +
"to have package:%n" +
" <\"java.util\">%n" +
"but had:%n" +
" <\"java.lang\">"));
}

@Test
void should_create_error_message_with_String_if_actual_has_no_package() {
// WHEN
String message = shouldHavePackage(Object[].class, "java.util").create(new TestDescription("TEST"),
STANDARD_REPRESENTATION);
// THEN
then(message).isEqualTo(format("[TEST] %n" +
"Expecting%n" +
" <java.lang.Object[]>%n" +
"to have package:%n" +
" <\"java.util\">%n" +
"but had none."));
}

@Test
void should_create_error_message_with_Package_if_actual_has_package() {
// WHEN
String message = shouldHavePackage(Object.class, Collection.class.getPackage()).create(new TestDescription("TEST"),
STANDARD_REPRESENTATION);
// THEN
then(message).isEqualTo(format("[TEST] %n" +
"Expecting%n" +
" <java.lang.Object>%n" +
"to have package:%n" +
" <\"java.util\">%n" +
"but had:%n" +
" <\"java.lang\">"));
}

@Test
void should_create_error_message_with_Package_if_actual_has_no_package() {
// WHEN
String message = shouldHavePackage(Object[].class, Collection.class.getPackage()).create(new TestDescription("TEST"),
STANDARD_REPRESENTATION);
// THEN
then(message).isEqualTo(format("[TEST] %n" +
"Expecting%n" +
" <java.lang.Object[]>%n" +
"to have package:%n" +
" <\"java.util\">%n" +
"but had none."));
}

}
Loading

0 comments on commit 1873ff0

Please sign in to comment.