-
-
Notifications
You must be signed in to change notification settings - Fork 713
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hasPackage Class assertion (#2019)
- Loading branch information
Showing
9 changed files
with
485 additions
and
4 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
67 changes: 67 additions & 0 deletions
67
src/main/java/org/assertj/core/error/ShouldHavePackage.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,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); | ||
} | ||
|
||
} |
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
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
42 changes: 42 additions & 0 deletions
42
src/test/java/org/assertj/core/api/classes/ClassAssert_hasPackage_with_Package_Test.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,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); | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
src/test/java/org/assertj/core/api/classes/ClassAssert_hasPackage_with_String_Test.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,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); | ||
} | ||
|
||
} |
94 changes: 94 additions & 0 deletions
94
src/test/java/org/assertj/core/error/ShouldHavePackage_create_Test.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,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.")); | ||
} | ||
|
||
} |
Oops, something went wrong.