Skip to content

Commit b358fb6

Browse files
BillyYcccjoel-costigliola
authored andcommitted
Add assertContainsOnlyNulls iterable/array assertion
1 parent 58d70fc commit b358fb6

15 files changed

+551
-6
lines changed

src/main/java/org/assertj/core/api/AbstractIterableAssert.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,15 @@ public SELF containsOnlyOnce(@SuppressWarnings("unchecked") ELEMENT... values) {
207207
return myself;
208208
}
209209

210+
/**
211+
* {@inheritDoc}
212+
*/
213+
@Override
214+
public SELF containsOnlyNulls() {
215+
iterables.assertContainsOnlyNulls(info, actual);
216+
return myself;
217+
}
218+
210219
/**
211220
* {@inheritDoc}
212221
*/
@@ -368,7 +377,7 @@ public SELF endsWith(ELEMENT first, @SuppressWarnings("unchecked") ELEMENT... re
368377
* {@inheritDoc}
369378
*/
370379
@Override
371-
public SELF endsWith(@SuppressWarnings("unchecked") ELEMENT[] sequence) {
380+
public SELF endsWith(ELEMENT[] sequence) {
372381
iterables.assertEndsWith(info, actual, sequence);
373382
return myself;
374383
}

src/main/java/org/assertj/core/api/AbstractObjectArrayAssert.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,31 @@ public SELF containsOnlyElementsOf(Iterable<? extends ELEMENT> iterable) {
270270
return containsOnly(toArray(iterable));
271271
}
272272

273+
/**
274+
* Verifies that the actual array contains only null elements.
275+
* <p>
276+
* Example :
277+
* <pre><code class='java'> Person[] persons1 = {null, null, null};
278+
* Person[] persons2 = {null, null, person};
279+
*
280+
* // assertion will pass
281+
* assertThat(persons1).containsOnlyNulls();
282+
*
283+
* // assertions will fail
284+
* assertThat(persons2).containsOnlyNulls();
285+
* assertThat(new Person[0]).containsOnlyNulls();</code></pre>
286+
*
287+
* @return {@code this} assertion object.
288+
* @throws AssertionError if the actual array is {@code null}.
289+
* @throws AssertionError if the actual array is empty or contains a non null element
290+
* @since 2.9.0 / 3.9.0
291+
*/
292+
@Override
293+
public SELF containsOnlyNulls() {
294+
arrays.assertContainsOnlyNulls(info, actual);
295+
return myself;
296+
}
297+
273298
/**
274299
* An alias of {@link #containsOnlyElementsOf(Iterable)} : verifies that actual contains all elements of the
275300
* given {@code Iterable} and nothing else, <b>in any order</b>.

src/main/java/org/assertj/core/api/AtomicReferenceArrayAssert.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import static org.assertj.core.internal.CommonValidations.checkSubsequenceIsNotNull;
2323
import static org.assertj.core.util.Arrays.array;
2424
import static org.assertj.core.util.Arrays.isArray;
25-
import static org.assertj.core.util.Arrays.prepend;
2625
import static org.assertj.core.util.IterableUtil.toArray;
2726
import static org.assertj.core.util.Lists.newArrayList;
2827
import static org.assertj.core.util.Preconditions.checkNotNull;
@@ -328,6 +327,33 @@ public AtomicReferenceArrayAssert<T> containsOnlyElementsOf(Iterable<? extends T
328327
return containsOnly(toArray(iterable));
329328
}
330329

330+
/**
331+
* Verifies that the actual AtomicReferenceArray contains only null elements and nothing else.
332+
* <p>
333+
* Example :
334+
* <pre><code class='java'> // assertion will pass
335+
* AtomicReferenceArray&lt;String&gt; items = new AtomicReferenceArray&lt;&gt;(new String[]{null, null, null});
336+
* assertThat(items).containsOnlyNulls();
337+
*
338+
* // assertion will fail because items2 contains not null element
339+
* AtomicReferenceArray&lt;String&gt; items2 = new AtomicReferenceArray&lt;&gt;(new String[]{null, null, "notNull"});
340+
* assertThat(items2).containsOnlyNulls();
341+
*
342+
* // assertion will fail since an empty array does not contain any element and therefore no null ones.
343+
* AtomicReferenceArray&lt;String&gt; empty = new AtomicReferenceArray&lt;&gt;(new String[0]);
344+
* assertThat(empty).containsOnlyNulls();</code></pre>
345+
*
346+
* @return {@code this} assertion object.
347+
* @throws AssertionError if the actual AtomicReferenceArray is {@code null}.
348+
* @throws AssertionError if the actual AtomicReferenceArray is empty or contains non null elements.
349+
* @since 2.9.0 / 3.9.0
350+
*/
351+
@Override
352+
public AtomicReferenceArrayAssert<T> containsOnlyNulls() {
353+
arrays.assertContainsOnlyNulls(info, array);
354+
return myself;
355+
}
356+
331357
/**
332358
* An alias of {@link #containsOnlyElementsOf(Iterable)} : verifies that actual contains all elements of the
333359
* given {@code Iterable} and nothing else, <b>in any order</b>.

src/main/java/org/assertj/core/api/ObjectEnumerableAssert.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,29 @@ public interface ObjectEnumerableAssert<SELF extends ObjectEnumerableAssert<SELF
8181
*/
8282
SELF containsOnly(@SuppressWarnings("unchecked") ELEMENT... values);
8383

84+
/**
85+
* Verifies that the actual group contains only null elements and nothing else.
86+
* <p>
87+
* Example :
88+
* <pre><code class='java'> // assertion will pass
89+
* Iterable&lt;String&gt; items = Arrays.asList(null, null, null);
90+
* assertThat(items).containsOnlyNulls();
91+
*
92+
* // assertion will fail because items2 contains a not null element
93+
* Iterable&lt;String&gt; items2 = Arrays.asList(null, null, "notNull");
94+
* assertThat(items2).containsOnlyNulls();
95+
*
96+
* // assertion will fail since an empty iterable does not contain any element and therefore no null ones.
97+
* Iterable&lt;String&gt; empty = new ArrayList&lt;&gt;();
98+
* assertThat(empty).containsOnlyNulls();</code></pre>
99+
*
100+
* @return {@code this} assertion object.
101+
* @throws AssertionError if the actual group is {@code null}.
102+
* @throws AssertionError if the actual group is empty or contains non null elements.
103+
* @since 2.9.0 / 3.9.0
104+
*/
105+
SELF containsOnlyNulls();
106+
84107
/**
85108
* Verifies that the actual group contains the given values only once.
86109
* <p>
@@ -491,7 +514,7 @@ public interface ObjectEnumerableAssert<SELF extends ObjectEnumerableAssert<SELF
491514
* @throws AssertionError if the actual group is {@code null}.
492515
* @throws AssertionError if the actual group does not end with the given sequence of objects.
493516
*/
494-
SELF endsWith(@SuppressWarnings("unchecked") ELEMENT[] sequence);
517+
SELF endsWith(ELEMENT[] sequence);
495518

496519
/**
497520
* Verifies that the actual group contains at least a null element.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2012-2017 the original author or authors.
12+
*/
13+
package org.assertj.core.error;
14+
15+
import static org.assertj.core.error.ShouldContainOnlyNulls.ErrorType.EMPTY;
16+
import static org.assertj.core.error.ShouldContainOnlyNulls.ErrorType.NON_NULL_ELEMENTS;
17+
18+
/**
19+
* Creates an error message indicating that an assertion that verifies a group of elements contains only null elements failed. A group
20+
* of elements can be a collection or an array.
21+
*
22+
* @author Billy Yuan
23+
*/
24+
public class ShouldContainOnlyNulls extends BasicErrorMessageFactory {
25+
26+
/**
27+
* Creates a new <code>{@link ShouldContainOnlyNulls}</code>.
28+
* @param actual the actual value in the failed assertion.
29+
* @param nonNullElements the values that are not null elements in the failed assertion.
30+
* @return the created {@code ErrorMessageFactory}.
31+
*/
32+
public static ErrorMessageFactory shouldContainOnlyNulls(Object actual) {
33+
return new ShouldContainOnlyNulls(actual, EMPTY, null);
34+
}
35+
36+
public static ErrorMessageFactory shouldContainOnlyNulls(Object actual, Iterable<?> nonNullElements) {
37+
return new ShouldContainOnlyNulls(actual, NON_NULL_ELEMENTS, nonNullElements);
38+
}
39+
40+
private ShouldContainOnlyNulls(Object actual, ErrorType errorType, Iterable<?> notExpected) {
41+
super("%n" +
42+
"Expecting:%n" +
43+
" <%s>%n" +
44+
"to contain only null elements but " + describe(errorType),
45+
actual, notExpected);
46+
}
47+
48+
private static String describe(ErrorType errorType) {
49+
switch (errorType) {
50+
case EMPTY:
51+
return "it was empty";
52+
case NON_NULL_ELEMENTS:
53+
default:
54+
return "some elements were not:%n <%s>";
55+
}
56+
}
57+
58+
public enum ErrorType {
59+
EMPTY, NON_NULL_ELEMENTS
60+
}
61+
}

src/main/java/org/assertj/core/internal/Arrays.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import static org.assertj.core.error.ShouldContainExactlyInAnyOrder.shouldContainExactlyInAnyOrder;
4242
import static org.assertj.core.error.ShouldContainNull.shouldContainNull;
4343
import static org.assertj.core.error.ShouldContainOnly.shouldContainOnly;
44+
import static org.assertj.core.error.ShouldContainOnlyNulls.shouldContainOnlyNulls;
4445
import static org.assertj.core.error.ShouldContainSequence.shouldContainSequence;
4546
import static org.assertj.core.error.ShouldContainSubsequence.shouldContainSubsequence;
4647
import static org.assertj.core.error.ShouldContainsOnlyOnce.shouldContainsOnlyOnce;
@@ -214,6 +215,18 @@ void assertContainsOnly(AssertionInfo info, Failures failures, Object actual, Ob
214215
comparisonStrategy));
215216
}
216217

218+
void assertContainsOnlyNulls(AssertionInfo info, Failures failures, Object[] actual) {
219+
assertNotNull(info, actual);
220+
// empty => no null elements => failure
221+
if (actual.length == 0) throw failures.failure(info, shouldContainOnlyNulls(actual));
222+
// look for any non null elements
223+
List<Object> nonNullElements = new ArrayList<>();
224+
for (Object element : actual) {
225+
if (element != null) nonNullElements.add(element);
226+
}
227+
if (nonNullElements.size() > 0) throw failures.failure(info, shouldContainOnlyNulls(actual, nonNullElements));
228+
}
229+
217230
void assertContainsExactly(AssertionInfo info, Failures failures, Object actual, Object values) {
218231
if (commonChecks(info, actual, values)) return;
219232
assertIsArray(info, actual);

src/main/java/org/assertj/core/internal/Iterables.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import static org.assertj.core.error.ShouldContainExactlyInAnyOrder.shouldContainExactlyInAnyOrder;
3636
import static org.assertj.core.error.ShouldContainNull.shouldContainNull;
3737
import static org.assertj.core.error.ShouldContainOnly.shouldContainOnly;
38+
import static org.assertj.core.error.ShouldContainOnlyNulls.shouldContainOnlyNulls;
3839
import static org.assertj.core.error.ShouldContainSequence.shouldContainSequence;
3940
import static org.assertj.core.error.ShouldContainSubsequence.shouldContainSubsequence;
4041
import static org.assertj.core.error.ShouldContainsOnlyOnce.shouldContainsOnlyOnce;
@@ -65,6 +66,7 @@
6566
import static org.assertj.core.util.Lists.newArrayList;
6667
import static org.assertj.core.util.Sets.newTreeSet;
6768

69+
import java.util.ArrayList;
6870
import java.util.Comparator;
6971
import java.util.Iterator;
7072
import java.util.LinkedHashSet;
@@ -304,6 +306,27 @@ public void assertContainsOnlyOnce(AssertionInfo info, Iterable<?> actual, Objec
304306
// assertion succeeded
305307
}
306308

309+
/**
310+
* Asserts that the given {@code Iterable} contains only null elements and nothing else.
311+
*
312+
* @param info contains information about the assertion.
313+
* @param actual the given {@code Iterable}.
314+
* @throws AssertionError if the given {@code Iterable} is {@code null}.
315+
* @throws AssertionError if the given {@code Iterable} does not contain at least a null element or if the given
316+
* {@code Iterable} contains values that are not null elements.
317+
*/
318+
public void assertContainsOnlyNulls(AssertionInfo info, Iterable<?> actual) {
319+
assertNotNull(info, actual);
320+
// empty => no null elements => failure
321+
if (sizeOf(actual) == 0) throw failures.failure(info, shouldContainOnlyNulls(actual));
322+
// look for any non null elements
323+
List<Object> nonNullElements = new ArrayList<>();
324+
for (Object element : actual) {
325+
if (element != null) nonNullElements.add(element);
326+
}
327+
if (nonNullElements.size() > 0) throw failures.failure(info, shouldContainOnlyNulls(actual, nonNullElements));
328+
}
329+
307330
/**
308331
* Verifies that the given <code>{@link Iterable}</code> contains the given sequence of objects, without any other
309332
* objects between them.
@@ -829,7 +852,7 @@ private <E> boolean conditionIsSatisfiedNTimes(Iterable<? extends E> actual, Con
829852
int times) {
830853
List<E> satisfiesCondition = satisfiesCondition(actual, condition);
831854
return satisfiesCondition.size() == times;
832-
}
855+
}
833856

834857
/**
835858
* An alias method of {@link #assertAreAtLeast(AssertionInfo, Iterable, int, Condition)} to provide a richer fluent

src/main/java/org/assertj/core/internal/ObjectArrays.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,19 @@ public void assertContainsOnlyOnce(AssertionInfo info, Object[] actual, Object[]
246246
arrays.assertContainsOnlyOnce(info, failures, actual, values);
247247
}
248248

249+
/**
250+
* Asserts that the given array contains only null elements.
251+
*
252+
* @param info contains information about the assertion
253+
* @param actual the given array
254+
* @throws AssertionError if the given array is {@code null}.
255+
* @throws AssertionError if the given array does not contain at least a null element
256+
* or contains values that are not null elements.
257+
*/
258+
public void assertContainsOnlyNulls(AssertionInfo info, Object[] actual) {
259+
arrays.assertContainsOnlyNulls(info, failures, actual);
260+
}
261+
249262
/**
250263
* Verifies that the given array contains the given sequence of objects, without any other objects between them.
251264
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2012-2017 the original author or authors.
12+
*/
13+
package org.assertj.core.api.atomic.referencearray;
14+
15+
import org.assertj.core.api.AtomicReferenceArrayAssert;
16+
import org.assertj.core.api.AtomicReferenceArrayAssertBaseTest;
17+
18+
import static org.mockito.Mockito.verify;
19+
20+
/**
21+
* Tests for <code>{@link AtomicReferenceArrayAssert#containsOnlyNulls()}</code>.
22+
*
23+
* @author Billy Yuan
24+
*/
25+
public class AtomicReferenceArrayAssert_containsOnlyNulls_Test extends AtomicReferenceArrayAssertBaseTest {
26+
@Override
27+
protected AtomicReferenceArrayAssert<Object> invoke_api_method() {
28+
return assertions.containsOnlyNulls();
29+
}
30+
31+
@Override
32+
protected void verify_internal_effects() {
33+
verify(arrays).assertContainsOnlyNulls(info(), internalArray());
34+
}
35+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2012-2017 the original author or authors.
12+
*/
13+
package org.assertj.core.api.iterable;
14+
15+
import org.assertj.core.api.AbstractIterableAssert;
16+
import org.assertj.core.api.ConcreteIterableAssert;
17+
import org.assertj.core.api.IterableAssertBaseTest;
18+
19+
import static org.mockito.Mockito.verify;
20+
21+
/**
22+
* Tests for <code>{@link AbstractIterableAssert#containsOnlyNulls()}</code>.
23+
*
24+
* @author Billy Yuan
25+
*/
26+
public class IterableAssert_containsOnlyNulls_Test extends IterableAssertBaseTest {
27+
@Override
28+
protected ConcreteIterableAssert<Object> invoke_api_method() {
29+
return assertions.containsOnlyNulls();
30+
}
31+
32+
@Override
33+
protected void verify_internal_effects() {
34+
verify(iterables).assertContainsOnlyNulls(getInfo(assertions), getActual(assertions));
35+
}
36+
}

0 commit comments

Comments
 (0)