Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 68677dd4982d2eee597bd814f4d3962a7f08c7bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package org.eclipse.jdt.core.tests.compiler.regression;

import java.util.Map;

import org.eclipse.jdt.core.JavaCore;

import junit.framework.Test;

public class NullChecksTests extends AbstractNullAnnotationTest {

	public NullChecksTests(String name) {
		super(name);
	}

	// Static initializer to specify tests subset using TESTS_* static variables
	// All specified tests which do not belong to the class are skipped...
	static {
//			TESTS_NAMES = new String[] { "testAssertNonNull1" };
//			TESTS_NUMBERS = new int[] { 561 };
//			TESTS_RANGE = new int[] { 1, 2049 };
	}

	public static Test suite() {
		return buildMinimalComplianceTestSuite(testClass(), F_1_8);
	}

	public static Class<NullChecksTests> testClass() {
		return NullChecksTests.class;
	}

	public void testAssertNonNull1() {
		runConformTestWithLibs(
			new String[] {
				"X.java",
				"import org.eclipse.jdt.annotation.*;\n" +
				"import static org.eclipse.jdt.annotation.Checks.*;\n" +
				"public class X {\n" +
				"	@SuppressWarnings(\"null\")\n" +
				"	static @NonNull String hide(String some) {\n" +
				"		return some;\n" +
				"	}\n" +
				"	public static void main(String... args) {\n" +
				"		@NonNull String myHiddenNull = hide(null);\n" +
				"		try {\n" +
				"			assertNonNull(\"foo\", myHiddenNull);\n" +
				"		} catch (NullPointerException npe) {\n" +
				"			System.out.println(npe.getMessage());\n" +
				"		}\n" +
				"		try {\n" +
				"			assertNonNullWithMessage(\"Shouldn't!\", \"foo\", myHiddenNull);\n" +
				"		} catch (NullPointerException npe) {\n" +
				"			System.out.println(npe.getMessage());\n" +
				"		}\n" +
				"	}\n" +
				"}\n"
			},
			getCompilerOptions(),
			"",
			"Value in position 1 must not be null\n" + 
			"Shouldn\'t!");
	}

	public void testAssertNonNullElements() {
		runConformTestWithLibs(
			new String[] {
				"X.java",
				"import org.eclipse.jdt.annotation.*;\n" +
				"import static org.eclipse.jdt.annotation.Checks.*;\n" +
				"import java.util.*;\n" +
				"public class X {\n" +
				"	@SuppressWarnings(\"null\")\n" +
				"	static @NonNull String hide(String some) {\n" +
				"		return some;\n" +
				"	}\n" +
				"	public static void main(String... args) {\n" +
				"		@NonNull List<String> myList = new ArrayList<>();\n" +
				"		myList.add(\"foo\");\n" +
				"		myList.add(null);\n" +
				"		try {\n" +
				"			assertNonNullElements(myList);\n" +
				"		} catch (NullPointerException npe) {\n" +
				"			System.out.println(npe.getMessage());\n" +
				"		}\n" +
				"		@NonNull List<@NonNull String> myList2 = new ArrayList<>();\n" +
				"		myList2.add(\"foo\");\n" +
				"		myList2.add(hide(null));\n" +
				"		try {\n" +
				"			assertNonNullElements(myList2, \"Shouldn't!\");\n" +
				"		} catch (NullPointerException npe) {\n" +
				"			System.out.println(npe.getMessage());\n" +
				"		}\n" +
				"	}\n" +
				"}\n"
			},
			getCompilerOptions(),
			"",
			"Value in position 1 must not be null\n" + 
			"Shouldn\'t!");
	}

	public void testRequireNonNull() {
		runConformTestWithLibs(
			new String[] {
				"X.java",
				"import org.eclipse.jdt.annotation.*;\n" +
				"import static org.eclipse.jdt.annotation.Checks.*;\n" +
				"public class X {\n" +
				"	@SuppressWarnings(\"null\")\n" +
				"	static @NonNull String hide(String some) {\n" +
				"		return some;\n" +
				"	}\n" +
				"	static void test(@Nullable String str, @Nullable X x) {\n" +
				"		@NonNull String nnStr;\n" +
				"		@NonNull X nnX;\n" +
				"		try {\n" +
				"			nnStr = requireNonNull(str);\n" +
				"			nnX = requireNonNull(null, \"Shouldn\'t!\");\n" +
				"		} catch (NullPointerException npe) {\n" +
				"			System.out.println(npe.getMessage());\n" +
				"		}\n" +
				"	}\n" +
				"	public static void main(String... args) {\n" +
				"		test(\"foo\", null);\n" +
				"	}\n" +
				"}\n"
			},
			getCompilerOptions(),
			"",
			"Shouldn\'t!");
	}

	public void testRequireNonEmptyString() {
		runConformTestWithLibs(
			new String[] {
				"X.java",
				"import org.eclipse.jdt.annotation.*;\n" +
				"import static org.eclipse.jdt.annotation.Checks.*;\n" +
				"public class X {\n" +
				"	@SuppressWarnings(\"null\")\n" +
				"	static @NonNull String hide(String some) {\n" +
				"		return some;\n" +
				"	}\n" +
				"	static void test(@Nullable String str1, @Nullable String str2) {\n" +
				"		@NonNull String nnStr;\n" +
				"		try {\n" +
				"			nnStr = requireNonEmpty(str1);\n" +
				"		} catch (NullPointerException npe) {\n" +
				"			System.out.println(\"npe:\"+npe.getMessage());\n" +
				"		}\n" +
				"		try {\n" +
				"			nnStr = requireNonEmpty(str2, \"Shouldn't!\");\n" +
				"		} catch (NullPointerException npe) {\n" +
				"			System.out.println(\"npe\"+npe.getMessage());\n" +
				"		} catch (IllegalArgumentException iae) {\n" +
				"			System.out.println(iae.getMessage());\n" +
				"		}\n" +
				"	}\n" +
				"	public static void main(String... args) {\n" +
				"		test(null, \"\");\n" +
				"	}\n" +
				"}\n"
			},
			getCompilerOptions(),
			"",
			"npe:null\n" +
			"Shouldn\'t!");
	}

	public void testRequireNonEmptyCollection() {
		runConformTestWithLibs(
			new String[] {
				"X.java",
				"import java.util.*;\n" +
				"import org.eclipse.jdt.annotation.*;\n" +
				"import static org.eclipse.jdt.annotation.Checks.*;\n" +
				"public class X {\n" +
				"	static void test(@Nullable Collection<String> strs, @Nullable Collection<String> strs1, Collection<String> strs2) {\n" +
				"		@NonNull Collection<String> nnStrs;\n" +
				"		try {\n" +
				"			nnStrs = requireNonEmpty(strs);\n" +
				"		} catch (NullPointerException npe) {\n" +
				"			System.out.println(\"NPE:\"+npe.getMessage());\n" +
				"		}\n" +
				"		try {\n" +
				"			nnStrs = requireNonEmpty(strs1);\n" +
				"		} catch (NullPointerException npe) {\n" +
				"			System.out.println(\"npe:\"+npe.getMessage());\n" +
				"		}\n" +
				"		try {\n" +
				"			nnStrs = requireNonEmpty(strs2, \"Shouldn't!\");\n" +
				"		} catch (NullPointerException npe) {\n" +
				"			System.out.println(\"npe\"+npe.getMessage());\n" +
				"		} catch (IllegalArgumentException iae) {\n" +
				"			System.out.println(iae.getMessage());\n" +
				"		}\n" +
				"	}\n" +
				"	public static void main(String... args) {\n" +
				"		test(Collections.singletonList(\"good\"), null, Collections.emptyList());\n" +
				"	}\n" +
				"}\n"
			},
			getCompilerOptions(),
			"",
			"npe:null\n" +
			"Shouldn\'t!");
	}
	
	public void testIsNull() {
		Map<String, String> compilerOptions = getCompilerOptions();
		compilerOptions.put(JavaCore.COMPILER_PB_SUPPRESS_OPTIONAL_ERRORS, JavaCore.ENABLED);
		runConformTestWithLibs(
			new String[] {
				"X.java",
				"import org.eclipse.jdt.annotation.*;\n" +
				"import static org.eclipse.jdt.annotation.Checks.*;\n" +
				"public class X {\n" +
				"	@SuppressWarnings(\"null\")\n" +
				"	static <T> @NonNull T hide(T some) {\n" +
				"		return some;\n" +
				"	}\n" +
				"	static void test(@NonNull X x1, @NonNull X x2, @NonNull X x3) {\n" +
				"		if (isNull(x1))\n" +
				"			System.out.println(\"IS NULL\");\n" +
				"		if (isAnyNull(x2, x1))\n" +
				"			System.out.println(\"IS ANY NULL 1\");\n" +
				"		if (isAnyNull(x2, x3))\n" +
				"			System.out.println(\"IS ANY NULL 2\");\n" +
				"	}\n" +
				"	public static void main(String... args) {\n" +
				"		test(hide(null), new X(), new X());\n" +
				"	}\n" +
				"}\n"					
			},
			compilerOptions,
			"",
			"IS NULL\n" +
			"IS ANY NULL 1");
	}
	
	public void testAsNullable() {
		runConformTestWithLibs(
			new String[] {
				"X.java",
				"import java.util.*;\n" +
				"import static org.eclipse.jdt.annotation.Checks.*;\n" +
				"public class X {\n" +
				"	static void test(Optional<X> xopt) {\n" +
				"		if (xopt != null) {\n" +
				"			X x = asNullable(xopt);\n" +
				"			if (x == null)\n" +
				"				System.out.println(\"NULL\");\n" +
				"		}\n" +
				"	}\n" +
				"	public static void main(String... args) {\n" +
				"		test(Optional.ofNullable(null));\n" +
				"	}\n" +
				"}\n"					
			},
			getCompilerOptions(),
			"",
			"NULL");
	}
	
	public void testNonNullElse() {
		runConformTestWithLibs(
			new String[] {
				"X.java",
				"import java.util.function.*;\n" +
				"import org.eclipse.jdt.annotation.*;\n" +
				"import static org.eclipse.jdt.annotation.Checks.*;\n" +
				"public class X {\n" +
				"	static void test(String str, String noStr, @NonNull Supplier<@NonNull String> prov) {\n" +
				"		System.out.println(nonNullElse(str, \"ELSE1\"));\n" +
				"		System.out.println(nonNullElse(noStr, \"ELSE2\"));\n" +
				"		System.out.println(nonNullElseGet(str, () -> \"ELSE3\"));\n" +
				"		System.out.println(nonNullElseGet(noStr, prov));\n" +
				"	}\n" +
				"	public static void main(String... args) {\n" +
				"		test(\"good\", null, () -> \"ELSE4\");\n" +
				"	}\n" +
				"}\n"					
			},
			getCompilerOptions(),
			"",
			"good\n" +
			"ELSE2\n" +
			"good\n" +
			"ELSE4");
	}
	
	public void _testIfNonNull() { // FIXME: see https://bugs.eclipse.org/489609 - [1.8][null] null annotation on wildcard is dropped during inference
		runConformTestWithLibs(
			new String[] {
				"X.java",
				"import org.eclipse.jdt.annotation.*;\n" +
				"import static org.eclipse.jdt.annotation.Checks.*;\n" +
				"public class X {\n" +
				"	static void test(@Nullable String str) {\n" +
				"		ifNonNull(str, s -> print(s));\n" +
				"	}\n" +
				"	static void print(@NonNull String s) {\n" +
				"		System.out.print(s);\n" +
				"	}\n" +
				"	public static void main(String... args) {\n" +
				"		test(\"good\");\n" +
				"	}\n" +
				"}\n"					
			},
			getCompilerOptions(),
			"",
			"good");
	}
}

Back to the top