|
1 | 1 | /*
|
2 |
| - * Copyright 2006-2023 the original author or authors. |
| 2 | + * Copyright 2006-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
15 | 15 | */
|
16 | 16 | package org.springframework.batch.item;
|
17 | 17 |
|
| 18 | +import java.io.Serializable; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.DisplayName; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import org.springframework.util.SerializationUtils; |
| 27 | + |
18 | 28 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
19 | 29 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
20 | 30 | import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
| 31 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
21 | 32 | import static org.junit.jupiter.api.Assertions.assertNull;
|
22 | 33 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
23 | 34 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
24 | 35 |
|
25 |
| -import java.io.Serializable; |
26 |
| - |
27 |
| -import org.junit.jupiter.api.Test; |
28 |
| -import org.springframework.util.SerializationUtils; |
29 |
| - |
30 | 36 | /**
|
31 | 37 | * @author Lucas Ward
|
32 | 38 | * @author Mahmoud Ben Hassine
|
@@ -196,4 +202,62 @@ public boolean equals(Object obj) {
|
196 | 202 |
|
197 | 203 | }
|
198 | 204 |
|
| 205 | + @DisplayName("testGetByType") |
| 206 | + @Test |
| 207 | + void givenAList_whenGettingAccordingToListType_thenReturnCorrectObject() { |
| 208 | + // given - a list |
| 209 | + String key = "aListObject"; |
| 210 | + List<String> value = List.of("value1", "value2"); |
| 211 | + context.put(key, value); |
| 212 | + // when - getting according to list type |
| 213 | + @SuppressWarnings("unchecked") |
| 214 | + List<String> result = (List<String>) context.get(key, List.class); |
| 215 | + // then - return the correct list |
| 216 | + assertEquals(result, value); |
| 217 | + assertEquals(result.get(0), value.get(0)); |
| 218 | + assertEquals(result.get(1), value.get(1)); |
| 219 | + } |
| 220 | + |
| 221 | + @DisplayName("testGetNullByDefaultParam") |
| 222 | + @Test |
| 223 | + void givenANonExistingKey_whenGettingTheNullList_thenReturnNull() { |
| 224 | + // given - a non existing key |
| 225 | + String key = "aListObjectButNull"; |
| 226 | + // when - getting according to the key |
| 227 | + @SuppressWarnings("unchecked") |
| 228 | + List<String> result = (List<String>) context.get(key, List.class, null); |
| 229 | + List<String> result2 = (List<String>) context.get(key, List.class); |
| 230 | + // then - return the defined null list |
| 231 | + assertNull(result); |
| 232 | + assertNull(result2); |
| 233 | + } |
| 234 | + |
| 235 | + @DisplayName("testGetNullByNotNullDefaultParam") |
| 236 | + @Test |
| 237 | + void givenAnNullList_whenGettingNullWithNonNullDefault_thenReturnDefinedDefaultValue() { |
| 238 | + // given - a non existing key |
| 239 | + String key = "aListObjectButNull"; |
| 240 | + List<String> defaultValue = new ArrayList<>(); |
| 241 | + defaultValue.add("value1"); |
| 242 | + @SuppressWarnings("unchecked") |
| 243 | + // when - getting according to list type and default value |
| 244 | + List<String> result = (List<String>) context.get(key, List.class, defaultValue); |
| 245 | + // then - return defined default value |
| 246 | + assertNotNull(result); |
| 247 | + assertEquals(result, defaultValue); |
| 248 | + assertEquals(result.get(0), defaultValue.get(0)); |
| 249 | + } |
| 250 | + |
| 251 | + @DisplayName("testGetWithWrongType") |
| 252 | + @Test |
| 253 | + void givenAList_whenGettingWithWrongType_thenThrowClassCastException() { |
| 254 | + // given - another normal list |
| 255 | + String key = "anotherListObject"; |
| 256 | + List<String> value = List.of("value1", "value2", "value3"); |
| 257 | + context.put(key, value); |
| 258 | + // when - getting according to map type |
| 259 | + // then - throw exception |
| 260 | + assertThrows(ClassCastException.class, () -> context.get(key, Map.class)); |
| 261 | + } |
| 262 | + |
199 | 263 | }
|
0 commit comments