|
15 | 15 | */
|
16 | 16 | package org.springframework.batch.item;
|
17 | 17 |
|
18 |
| -import static org.junit.jupiter.api.Assertions.assertEquals; |
19 |
| -import static org.junit.jupiter.api.Assertions.assertFalse; |
20 |
| -import static org.junit.jupiter.api.Assertions.assertNotEquals; |
21 |
| -import static org.junit.jupiter.api.Assertions.assertNull; |
22 |
| -import static org.junit.jupiter.api.Assertions.assertThrows; |
23 |
| -import static org.junit.jupiter.api.Assertions.assertTrue; |
24 |
| - |
25 | 18 | import java.io.Serializable;
|
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
26 | 21 |
|
| 22 | +import com.google.gson.reflect.TypeToken; |
| 23 | +import org.assertj.core.api.Assertions; |
| 24 | +import org.junit.jupiter.api.DisplayName; |
27 | 25 | import org.junit.jupiter.api.Test;
|
28 | 26 | import org.springframework.util.SerializationUtils;
|
29 | 27 |
|
| 28 | +import static org.junit.jupiter.api.Assertions.*; |
| 29 | + |
30 | 30 | /**
|
31 | 31 | * @author Lucas Ward
|
32 | 32 | * @author Mahmoud Ben Hassine
|
@@ -196,4 +196,52 @@ public boolean equals(Object obj) {
|
196 | 196 |
|
197 | 197 | }
|
198 | 198 |
|
| 199 | + @DisplayName("testGetByType") |
| 200 | + @Test |
| 201 | + void givenAList_whenGettingAccordingToListType_thenReturnCorrectObject() { |
| 202 | + // given - a list |
| 203 | + String key = "aListObject"; |
| 204 | + List<String> value = List.of("value1", "value2"); |
| 205 | + context.put(key, value); |
| 206 | + // when - getting according to list type |
| 207 | + @SuppressWarnings("unchecked") |
| 208 | + List<String> result = (List<String>) context.get(key, List.class); |
| 209 | + // then - return |
| 210 | + assertEquals(result, value); |
| 211 | + assertEquals(result.get(0), value.get(0)); |
| 212 | + assertEquals(result.get(1), value.get(1)); |
| 213 | + } |
| 214 | + |
| 215 | + @DisplayName("testGetNullByDefaultParam") |
| 216 | + @Test |
| 217 | + void givenAnNullList_whenGettingTheNullList_thenReturnNull() { |
| 218 | + // given - a null list |
| 219 | + String key = "aListObjectButNull"; |
| 220 | + List<String> value = null; |
| 221 | + context.put(key, value); |
| 222 | + // when - getting according to the key |
| 223 | + @SuppressWarnings("unchecked") |
| 224 | + List<String> result = (List<String>) context.get(key, List.class, null); |
| 225 | + // then - return the defined null list |
| 226 | + assertNull(result); |
| 227 | + } |
| 228 | + |
| 229 | + @DisplayName("testGetNullByNotNullDefaultParam") |
| 230 | + @Test |
| 231 | + void givenAnNullList_whenGettingNullWithNonNullDefault_thenReturnDefinedDefaultValue() { |
| 232 | + // given - a null list and expected default value |
| 233 | + String key = "aListObjectButNull"; |
| 234 | + List<String> value = null; |
| 235 | + List<String> defaultValue = new ArrayList<>(); |
| 236 | + defaultValue.add("value1"); |
| 237 | + context.put(key, value); |
| 238 | + @SuppressWarnings("unchecked") |
| 239 | + // when - getting according to list type and default value |
| 240 | + List<String> result = (List<String>) context.get(key, List.class, defaultValue); |
| 241 | + // then - return defined default value |
| 242 | + assertNotNull(result); |
| 243 | + assertEquals(result, defaultValue); |
| 244 | + assertEquals(result.get(0), defaultValue.get(0)); |
| 245 | + } |
| 246 | + |
199 | 247 | }
|
0 commit comments