Skip to content

Commit b58c842

Browse files
committed
Add utility test class for single item reading scenarios
1 parent a6a53c4 commit b58c842

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2025-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.core.step.item;
17+
18+
import org.jspecify.annotations.Nullable;
19+
20+
import org.springframework.batch.infrastructure.item.ExecutionContext;
21+
import org.springframework.batch.infrastructure.item.ItemStreamException;
22+
import org.springframework.batch.infrastructure.item.ItemStreamReader;
23+
24+
/**
25+
* An {@link ItemStreamReader} that reads a single item. Useful for testing purposes.
26+
*
27+
* @param <T> the type of item to be read
28+
* @author Mahmoud Ben Hassine
29+
*/
30+
public class SingleItemStreamReader<T> implements ItemStreamReader<T> {
31+
32+
private T item;
33+
34+
private final T initialValue;
35+
36+
/**
37+
* Create a new {@link SingleItemStreamReader} with the given initial value.
38+
* @param initialValue the initial value to be read
39+
*/
40+
public SingleItemStreamReader(T initialValue) {
41+
this.initialValue = initialValue;
42+
}
43+
44+
@Override
45+
public @Nullable T read() throws Exception {
46+
T returnedItem = this.item;
47+
this.item = null;
48+
return returnedItem;
49+
}
50+
51+
@Override
52+
public void open(ExecutionContext executionContext) throws ItemStreamException {
53+
this.item = this.initialValue;
54+
}
55+
56+
@Override
57+
public void update(ExecutionContext executionContext) throws ItemStreamException {
58+
this.item = null;
59+
}
60+
61+
@Override
62+
public void close() throws ItemStreamException {
63+
this.item = null;
64+
}
65+
66+
}

0 commit comments

Comments
 (0)