Skip to content

Commit

Permalink
ShouldNotBeNull(new factory method), Throwables(update of assertHasMe…
Browse files Browse the repository at this point in the history
…ssageFindingMatch()), unit tests created and updated
  • Loading branch information
ebundy committed Dec 25, 2018
1 parent d2f934f commit 0cf4cf8
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/assertj/core/api/AbstractMapAssert.java
Expand Up @@ -25,6 +25,7 @@

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/assertj/core/api/AbstractThrowableAssert.java
Expand Up @@ -238,11 +238,11 @@ public SELF hasMessageMatching(String regex) {
* Throwable throwable = new IllegalArgumentException("Dear John,\n" +
* "it' s a wrong amount");
* // assertion will pass
* assertThat(throwable).hasMessageFindMatching("wrong amount");
* assertThat(throwable).hasMessageFindMatching("Dear John");
* assertThat(throwable).hasMessageFindMatching("wrong amount$");
* assertThat(throwable).hasMessageFindingMatch("wrong amount");
* assertThat(throwable).hasMessageFindingMatch("Dear John");
* assertThat(throwable).hasMessageFindingMatch("wrong amount$");
* // assertion will fail
* assertThat(throwable).hasMessageFindMatching("Dear John$");
* assertThat(throwable).hasMessageFindingMatch("Dear John$");
*
* </code></pre>
*
Expand All @@ -252,8 +252,8 @@ public SELF hasMessageMatching(String regex) {
* @throws AssertionError if the message of the actual {@code Throwable} doesn't contain any sequence matching with the given regular expression
* @throws NullPointerException if the regex is null
*/
public SELF hasMessageFindMatching(String regex) {
throwables.assertHasMessageFindMatching(info, actual, regex);
public SELF hasMessageFindingMatch(String regex) {
throwables.assertHasMessageFindingMatch(info, actual, regex);
return myself;
}

Expand Down
Expand Up @@ -16,7 +16,6 @@
* Creates an error message indicating that an assertion that verifies that a {@code CharSequence} matches given regular
* expression.
*
* @author David Haccoun
*/
public class ShouldHaveMessageFindMatchingRegex extends BasicErrorMessageFactory {

Expand Down
21 changes: 15 additions & 6 deletions src/main/java/org/assertj/core/error/ShouldNotBeNull.java
Expand Up @@ -14,23 +14,32 @@

/**
* Creates an error message that indicates an assertion that verifies that an object is not {@code null} failed.
*
*
* @author Alex Ruiz
*/
public class ShouldNotBeNull extends BasicErrorMessageFactory {

private static final ShouldNotBeNull INSTANCE = new ShouldNotBeNull();
private static final ShouldNotBeNull INSTANCE = new ShouldNotBeNull("%nExpecting actual not to be null");

/**
* Returns the singleton instance of this class.
* @return the singleton instance of this class.
* Returns the default instance of this class.
* @return the default instance of this class.
*/
public static ErrorMessageFactory shouldNotBeNull() {
return INSTANCE;
}

private ShouldNotBeNull() {
super("%nExpecting actual not to be null");
/**
* Create a instance specifying the label of what should not be null
* @param label
* @return the new instance
*/
public static ShouldNotBeNull of(String label) {
return new ShouldNotBeNull(String.format("%nExpecting %s not to be null", label));
}

private ShouldNotBeNull(String label) {
super(label);
}

}
16 changes: 16 additions & 0 deletions src/main/java/org/assertj/core/internal/Objects.java
Expand Up @@ -62,6 +62,7 @@
import java.util.Set;

import org.assertj.core.api.AssertionInfo;
import org.assertj.core.error.ShouldNotBeNull;
import org.assertj.core.internal.DeepDifference.Difference;
import org.assertj.core.util.VisibleForTesting;
import org.assertj.core.util.introspection.FieldSupport;
Expand Down Expand Up @@ -382,6 +383,21 @@ public void assertNotNull(AssertionInfo info, Object actual) {
throw failures.failure(info, shouldNotBeNull());
}

/**
* Asserts that the given object is not {@code null}.
*
* @param info contains information about the assertion.
* @param actual the given object.
* @param label the label to represent actual in the error message
* @throws AssertionError if the given object is {@code null}.
*/
public void assertNotNull(AssertionInfo info, Object actual, String label) {
if (actual != null) {
return;
}
throw failures.failure(info, ShouldNotBeNull.of(label));
}

/**
* Asserts that two objects refer to the same object.
*
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/assertj/core/internal/Throwables.java
Expand Up @@ -186,10 +186,11 @@ public void assertHasMessageMatching(AssertionInfo info, Throwable actual, Strin
* @throws AssertionError if the message of the actual {@code Throwable} doesn't contain any sequence matching with the given regular expression
* @throws NullPointerException if the regex is null
*/
public void assertHasMessageFindMatching(AssertionInfo info, Throwable actual, String regex) {
public void assertHasMessageFindingMatch(AssertionInfo info, Throwable actual, String regex) {
checkNotNull(regex, "regex must not be null");
assertNotNull(info, actual);
if (actual.getMessage() != null && Pattern.compile(regex, Pattern.DOTALL).asPredicate()
Objects.instance().assertNotNull(info, actual);
Objects.instance().assertNotNull(info, actual.getMessage(), "exception message of actual");
if (Pattern.compile(regex, Pattern.DOTALL).asPredicate()
.test(actual.getMessage())) return;
throw failures.failure(info, shouldHaveMessageFindMatchingRegex(actual, regex));
}
Expand Down Expand Up @@ -307,6 +308,7 @@ private static void assertNotNull(AssertionInfo info, Throwable actual) {
Objects.instance().assertNotNull(info, actual);
}


private static boolean compareThrowable(Throwable actual, Throwable expected) {
return areEqual(actual.getMessage(), expected.getMessage())
&& areEqual(actual.getClass(), expected.getClass());
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/assertj/core/api/SoftAssertionsTest.java
Expand Up @@ -557,9 +557,9 @@ public void should_collect_errors_when_using_extractingFromEntries_with_map() {
// THEN
List<Throwable> errorsCollected = softly.errorsCollected();
assertThat(errorsCollected).hasSize(3);
assertThat(errorsCollected.get(0)).hasMessageFindMatching("not found:.*stranger.*not expected:.*david");
assertThat(errorsCollected.get(0)).hasMessageFindingMatch("not found:.*stranger.*not expected:.*david");
assertThat(errorsCollected.get(1)).hasMessage("overridingErrorMessage with extractingFromEntries");
assertThat(errorsCollected.get(2)).hasMessageFindMatching("not found:.*10.*not expected:.*1");
assertThat(errorsCollected.get(2)).hasMessageFindingMatch("not found:.*10.*not expected:.*1");
}

@Test
Expand Down
Expand Up @@ -17,6 +17,7 @@

import org.assertj.core.internal.Failures;
import org.assertj.core.internal.Throwables;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;

Expand Down Expand Up @@ -45,6 +46,12 @@ public void setUp() {
failures = spy(new Failures());
throwables = new Throwables();
throwables.failures = failures;
Objects.instance().failures = failures;
}

}
@AfterEach
public void tearDown() {
Objects.instance().failures = Failures.instance();
}

}
@@ -0,0 +1,88 @@
/*
* 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-2018 the original author or authors.
*/
package org.assertj.core.internal.throwables;

import org.assertj.core.api.AssertionInfo;
import org.assertj.core.error.ShouldNotBeNull;
import org.assertj.core.internal.ThrowablesBaseTest;
import org.junit.Ignore;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
import static org.assertj.core.error.ShouldHaveMessageFindMatchingRegex.shouldHaveMessageFindMatchingRegex;
import static org.assertj.core.error.ShouldNotBeNull.shouldNotBeNull;
import static org.assertj.core.test.TestData.someInfo;
import static org.mockito.Mockito.verify;

/**
*
* @author David Haccoun
*/

public class Throwables_assertHasMessageFindingMatch_Test extends ThrowablesBaseTest {

static final String REGEX = "waiting for Foo";

@Test
public void should_pass_if_throwable_message_matches_given_regex() {
Throwable actual = new RuntimeException("Blablabla...\n" +
"waiting for Foo" +
"...blablabla...\n");
throwables.assertHasMessageFindingMatch(someInfo(), actual, REGEX);
}

@Test
public void should_pass_if_throwable_message_is_empty_and_regex_is_too() {
Throwable actual = new RuntimeException("");
throwables.assertHasMessageFindingMatch(someInfo(), actual, "");
}

@Test
public void should_fail_if_throwable_message_does_not_match_given_regex() {

Throwable actual = new RuntimeException("Blablabla...\n" +
"waiting for Bar" +
"...blablabla...\n");
AssertionInfo info = someInfo();
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> throwables.assertHasMessageFindingMatch(someInfo(), actual, REGEX));
verify(failures).failure(info, shouldHaveMessageFindMatchingRegex(actual, REGEX));
}

@Test
public void should_fail_if_given_regex_is_null() {
assertThatNullPointerException()
.isThrownBy(() -> throwables.assertHasMessageFindingMatch(someInfo(), actual, null))
.withMessage("regex must not be null");
}

@Test
public void should_fail_if_throwable_is_null() {
AssertionInfo info = someInfo();
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> throwables.assertHasMessageFindingMatch(someInfo(), null, REGEX));
verify(failures).failure(info, shouldNotBeNull());
}

@Test
public void should_fail_if_throwable_does_not_have_a_message() {
actual = new RuntimeException();
AssertionInfo info = someInfo();
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> throwables.assertHasMessageFindingMatch(someInfo(), actual, REGEX));
verify(failures).failure(info, ShouldNotBeNull.of("exception message of actual"));
}

}

0 comments on commit 0cf4cf8

Please sign in to comment.