common: Add a checkNotNullContents() for arrays
[deliverable/tracecompass.git] / common / org.eclipse.tracecompass.common.core.tests / src / org / eclipse / tracecompass / common / core / tests / NonNullUtilsTest.java
CommitLineData
04ec39a6
AM
1/*******************************************************************************
2 * Copyright (c) 2015 EfficiOS Inc., Alexandre Montplaisir
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9
10package org.eclipse.tracecompass.common.core.tests;
11
7fb07f6f 12import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
04ec39a6 13import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNullContents;
7fb07f6f 14import static org.junit.Assert.assertArrayEquals;
04ec39a6 15import static org.junit.Assert.assertEquals;
7fb07f6f 16import static org.junit.Assert.assertNotNull;
04ec39a6
AM
17
18import java.util.ArrayList;
19import java.util.List;
20import java.util.stream.Collectors;
7fb07f6f 21import java.util.stream.Stream;
04ec39a6
AM
22
23import org.eclipse.jdt.annotation.NonNull;
24import org.eclipse.jdt.annotation.Nullable;
25import org.eclipse.tracecompass.common.core.NonNullUtils;
26import org.junit.Test;
27
28/**
29 * Tests for {@link NonNullUtils}.
30 */
31public class NonNullUtilsTest {
32
33 /**
7fb07f6f
AM
34 * Test code to ensure the null-annotations on
35 * {@link NonNullUtils#checkNotNullContents(Stream)} work correctly.
36 *
37 * Not mean to be run as a {@link Test}, the code should simply not produce
38 * any compilation errors or warnings.
39 */
40 public void testAnnotationsCheckNotNullContentsStream() {
41 Stream<String> a = (new ArrayList<String>()).stream();
42 Stream<@Nullable String> b = (new ArrayList<@Nullable String>()).stream();
43 @Nullable Stream<String> c = (new ArrayList<String>()).stream();
44 @Nullable Stream<@Nullable String> d = (new ArrayList<@Nullable String>()).stream();
45 Stream<@Nullable String> e = (new ArrayList<@Nullable String>()).stream();
46 @NonNull Stream<String> f = checkNotNull((new ArrayList<String>()).stream());
47 @NonNull Stream<@NonNull String> g = checkNotNull((new ArrayList<@NonNull String>()).stream());
48
49 @NonNull Stream<@NonNull String> checkedA = checkNotNullContents(a);
50 @NonNull Stream<@NonNull String> checkedB = checkNotNullContents(b);
51 @NonNull Stream<@NonNull String> checkedC = checkNotNullContents(c);
52 @NonNull Stream<@NonNull String> checkedD = checkNotNullContents(d);
53 @NonNull Stream<@NonNull String> checkedE = checkNotNullContents(e);
54 @NonNull Stream<@NonNull String> checkedF = checkNotNullContents(f);
55 @NonNull Stream<@NonNull String> checkedG = checkNotNullContents(g);
56
57 assertNotNull(checkedA);
58 assertNotNull(checkedB);
59 assertNotNull(checkedC);
60 assertNotNull(checkedD);
61 assertNotNull(checkedE);
62 assertNotNull(checkedF);
63 assertNotNull(checkedG);
64 }
65
66 /**
67 * Test code to ensure the null-annotations on
68 * {@link NonNullUtils#checkNotNullContents(Object[])} work correctly.
69 *
70 * Not mean to be run as a {@link Test}, the code should simply not produce
71 * any compilation errors or warnings.
72 */
73 public void testAnnotationsCheckNotNullContentsArray() {
74 String[] a = new String[] {};
75 @Nullable String[] b = new @Nullable String[] {};
76 String @Nullable [] c = new String[] {};
77 @Nullable String @Nullable [] d = new @Nullable String[] {};
78 @NonNull String[] e = new @NonNull String[] {};
79 String @NonNull [] f = new String[] {};
80 @NonNull String @NonNull [] g = new @NonNull String[] {};
81
82 @NonNull String @NonNull [] checkedA = checkNotNullContents(a);
83 @NonNull String @NonNull [] checkedB = checkNotNullContents(b);
84 @NonNull String @NonNull [] checkedC = checkNotNullContents(c);
85 @NonNull String @NonNull [] checkedD = checkNotNullContents(d);
86 @NonNull String @NonNull [] checkedE = checkNotNullContents(e);
87 @NonNull String @NonNull [] checkedF = checkNotNullContents(f);
88 @NonNull String @NonNull [] checkedG = checkNotNullContents(g);
89
90 assertNotNull(checkedA);
91 assertNotNull(checkedB);
92 assertNotNull(checkedC);
93 assertNotNull(checkedD);
94 assertNotNull(checkedE);
95 assertNotNull(checkedF);
96 assertNotNull(checkedG);
97 }
98
99 /**
100 * Test {@link NonNullUtils#checkNotNullContents(Stream)} for a stream
101 * containing no null elements.
04ec39a6
AM
102 */
103 @Test
7fb07f6f 104 public void testCheckContentsStream() {
04ec39a6
AM
105 List<@Nullable String> list = new ArrayList<>();
106 list.add("a");
107 list.add("b");
108 list.add("c");
109 list.add("d");
110
111 List<@NonNull String> out = checkNotNullContents(list.stream()).collect(Collectors.toList());
112 assertEquals(list, out);
113 }
114
115 /**
7fb07f6f
AM
116 * Test {@link NonNullUtils#checkNotNullContents(Stream)} with a null
117 * reference (should fail immediately).
04ec39a6
AM
118 */
119 @Test(expected = NullPointerException.class)
7fb07f6f
AM
120 public void testCheckContentsStreamNullRef() {
121 checkNotNullContents((Stream<@Nullable ?>) null);
04ec39a6
AM
122 }
123
124 /**
7fb07f6f
AM
125 * Test {@link NonNullUtils#checkNotNullContents(Stream)} with a stream
126 * containing a null value.
04ec39a6
AM
127 */
128 @Test(expected = NullPointerException.class)
7fb07f6f 129 public void testCheckContentsStreamNullElement() {
04ec39a6
AM
130 List<@Nullable String> list = new ArrayList<>();
131 list.add("a");
132 list.add("b");
133 list.add(null);
134 list.add("d");
135
136 /*
137 * Should fail.
138 *
139 * Don't forget we need a terminal operation to process the contents of
140 * the stream!
141 */
142 checkNotNullContents(list.stream()).collect(Collectors.toList());
143 }
7fb07f6f
AM
144
145 /**
146 * Test {@link NonNullUtils#checkNotNullContents(Object[])} for an array
147 * containing no null elements.
148 */
149 @Test
150 public void testCheckContentsArray() {
151 @Nullable String[] array = new @Nullable String[3];
152 array[0] = "a";
153 array[1] = "b";
154 array[2] = "c";
155
156 @NonNull String[] out = checkNotNullContents(array);
157 assertArrayEquals(array, out);
158 }
159
160 /**
161 * Test {@link NonNullUtils#checkNotNullContents(Object[])} with a null
162 * reference (should fail immediately).
163 */
164 @Test(expected = NullPointerException.class)
165 public void testCheckContentsArrayNullRef() {
166 checkNotNullContents((@Nullable Object[]) null);
167 }
168
169 /**
170 * Test {@link NonNullUtils#checkNotNullContents(Object[])} with an array
171 * containing a null value.
172 */
173 @Test(expected = NullPointerException.class)
174 public void testCheckContentsArrayNullElement() {
175 @Nullable String[] array = new @Nullable String[3];
176 array[0] = "a";
177 array[1] = null;
178 array[2] = "c";
179
180 /* Should fail */
181 checkNotNullContents(array);
182 }
04ec39a6 183}
This page took 0.0449 seconds and 5 git commands to generate.