Skip to content

Commit dc478dc

Browse files
committed
Refactor LDIF reader tests
Signed-off-by: banseok1216 <bansuk1216@naver.com>
1 parent c121edd commit dc478dc

7 files changed

Lines changed: 654 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2026 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.infrastructure.item.ldif;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import org.springframework.batch.infrastructure.item.ExecutionContext;
21+
import org.springframework.batch.infrastructure.item.ldif.support.LdifReaderTestSupport;
22+
23+
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
import static org.junit.jupiter.api.Assertions.assertThrows;
25+
26+
/**
27+
* @author Banseok Kim
28+
*/
29+
class LdifReaderTests extends LdifReaderTestSupport {
30+
31+
@Test
32+
void readRecordsUsingSetters() throws Exception {
33+
LdifReader reader = new LdifReader(ldifResource);
34+
reader.setName("ldif");
35+
reader.setStrict(true);
36+
reader.afterPropertiesSet();
37+
38+
verify(reader, expectedDns());
39+
}
40+
41+
@Test
42+
void missingResourceInStrictModeShouldFailOnOpen() throws Exception {
43+
LdifReader reader = new LdifReader(missingResource());
44+
reader.setName("ldif");
45+
reader.setStrict(true);
46+
reader.afterPropertiesSet();
47+
48+
assertThrows(Exception.class, () -> reader.open(new ExecutionContext()));
49+
}
50+
51+
@Test
52+
void skippedRecordsCallbackIsInvoked() throws Exception {
53+
StringBuilder callback = new StringBuilder();
54+
55+
LdifReader reader = new LdifReader(ldifResource);
56+
reader.setName("ldif");
57+
reader.setRecordsToSkip(1);
58+
reader.setSkippedRecordsCallback(attributes -> callback.append(attributes.getName().toString()));
59+
reader.afterPropertiesSet();
60+
61+
reader.open(new ExecutionContext());
62+
reader.read();
63+
reader.close();
64+
65+
assertEquals("cn=Barbara Jensen,ou=Product Development,dc=airius,dc=com", callback.toString());
66+
}
67+
68+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2026 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.infrastructure.item.ldif;
17+
18+
import org.jspecify.annotations.Nullable;
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.batch.infrastructure.item.ExecutionContext;
22+
import org.springframework.batch.infrastructure.item.ldif.support.MappingLdifReaderTestSupport;
23+
import org.springframework.ldap.core.LdapAttributes;
24+
25+
import static org.junit.jupiter.api.Assertions.assertThrows;
26+
27+
/**
28+
* @author Banseok Kim
29+
*/
30+
class MappingLdifReaderTests extends MappingLdifReaderTestSupport {
31+
32+
@Test
33+
void readRecordsMappingToDnStringUsingSetters() throws Exception {
34+
MappingLdifReader<String> reader = new MappingLdifReader<>(ldifResource);
35+
reader.setName("ldif");
36+
reader.setRecordMapper(new StringMapper());
37+
reader.setStrict(true);
38+
reader.afterPropertiesSet();
39+
40+
verify(reader, expectedDns());
41+
}
42+
43+
@Test
44+
void missingResourceInStrictModeShouldFailOnOpen() throws Exception {
45+
MappingLdifReader<String> reader = new MappingLdifReader<>(missingResource());
46+
reader.setName("ldif");
47+
reader.setRecordMapper(new StringMapper());
48+
reader.setStrict(true);
49+
reader.afterPropertiesSet();
50+
51+
assertThrows(Exception.class, () -> reader.open(new ExecutionContext()));
52+
}
53+
54+
private static class StringMapper implements RecordMapper<String> {
55+
56+
@Override
57+
public @Nullable String mapRecord(@Nullable LdapAttributes attributes) {
58+
if (attributes == null) {
59+
return null;
60+
}
61+
return attributes.getName().toString();
62+
}
63+
64+
}
65+
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
* Copyright 2026 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.infrastructure.item.ldif.builder;
17+
18+
import org.junit.jupiter.api.Assertions;
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.batch.infrastructure.item.ExecutionContext;
22+
import org.springframework.batch.infrastructure.item.ItemStreamException;
23+
import org.springframework.batch.infrastructure.item.ldif.LdifReader;
24+
import org.springframework.batch.infrastructure.item.ldif.RecordCallbackHandler;
25+
import org.springframework.batch.infrastructure.item.ldif.support.LdifReaderTestSupport;
26+
import org.springframework.ldap.core.LdapAttributes;
27+
28+
import static org.junit.jupiter.api.Assertions.assertEquals;
29+
import static org.junit.jupiter.api.Assertions.assertNull;
30+
import static org.junit.jupiter.api.Assertions.assertThrows;
31+
32+
/**
33+
* @author Banseok Kim
34+
*/
35+
class LdifReaderBuilderTests extends LdifReaderTestSupport {
36+
37+
private String callbackDn;
38+
39+
@Test
40+
void itemReaderBasicRead() throws Exception {
41+
LdifReader reader = new LdifReaderBuilder().name("ldif").resource(ldifResource).build();
42+
43+
verify(reader, expectedDns());
44+
}
45+
46+
@Test
47+
void recordsToSkipSkipsFirstRecord() throws Exception {
48+
LdifReader reader = new LdifReaderBuilder().name("ldif").resource(ldifResource).recordsToSkip(1).build();
49+
50+
reader.open(new ExecutionContext());
51+
LdapAttributes item = reader.read();
52+
assertEquals("cn=Bjorn Jensen,ou=Accounting,dc=airius,dc=com", item.getName().toString());
53+
reader.close();
54+
}
55+
56+
@Test
57+
void currentItemCountStartsFromThirdRecord() throws Exception {
58+
LdifReader reader = new LdifReaderBuilder().name("ldif").resource(ldifResource).currentItemCount(2).build();
59+
60+
reader.open(new ExecutionContext());
61+
LdapAttributes item = reader.read();
62+
assertEquals("cn=Gern Jensen,ou=Product Testing,dc=airius,dc=com", item.getName().toString());
63+
reader.close();
64+
}
65+
66+
@Test
67+
void currentItemCountAtEndReturnsNull() throws Exception {
68+
LdifReader reader = new LdifReaderBuilder().name("ldif").resource(ldifResource).currentItemCount(3).build();
69+
70+
reader.open(new ExecutionContext());
71+
Assertions.assertNull(reader.read());
72+
reader.close();
73+
}
74+
75+
@Test
76+
void maxItemCountLimitsReads() throws Exception {
77+
LdifReader reader = new LdifReaderBuilder().name("ldif").resource(ldifResource).maxItemCount(1).build();
78+
79+
reader.open(new ExecutionContext());
80+
LdapAttributes first = reader.read();
81+
LdapAttributes second = reader.read();
82+
assertEquals("cn=Barbara Jensen,ou=Product Development,dc=airius,dc=com", first.getName().toString());
83+
assertNull(second);
84+
reader.close();
85+
}
86+
87+
@Test
88+
void skippedRecordsCallbackIsInvoked() throws Exception {
89+
this.callbackDn = null;
90+
91+
LdifReader reader = new LdifReaderBuilder().name("ldif")
92+
.resource(ldifResource)
93+
.recordsToSkip(1)
94+
.skippedRecordsCallback(new TestCallback())
95+
.build();
96+
97+
reader.open(new ExecutionContext());
98+
reader.read();
99+
assertEquals("cn=Barbara Jensen,ou=Product Development,dc=airius,dc=com", this.callbackDn);
100+
reader.close();
101+
}
102+
103+
@Test
104+
void saveStateUpdatesExecutionContext() throws Exception {
105+
LdifReader reader = new LdifReaderBuilder().name("foo").resource(ldifResource).build();
106+
107+
ExecutionContext ec = new ExecutionContext();
108+
reader.open(ec);
109+
reader.read();
110+
reader.update(ec);
111+
112+
assertEquals(1, ec.getInt("foo.read.count"));
113+
reader.close();
114+
}
115+
116+
@Test
117+
void strictTrueThrowsOnMissingResource() throws Exception {
118+
LdifReader reader = new LdifReaderBuilder().name("ldif").resource(missingResource()).strict(true).build();
119+
120+
ItemStreamException ex = assertThrows(ItemStreamException.class, () -> reader.open(new ExecutionContext()));
121+
assertEquals("Failed to initialize the reader", ex.getMessage());
122+
}
123+
124+
@Test
125+
void strictFalseDoesNotThrowOnMissingResource() throws Exception {
126+
LdifReader reader = new LdifReaderBuilder().name("ldif").resource(missingResource()).strict(false).build();
127+
128+
reader.open(new ExecutionContext());
129+
reader.close();
130+
}
131+
132+
@Test
133+
void itemReaderWithNoResourceShouldFail() {
134+
assertThrows(IllegalArgumentException.class, () -> new LdifReaderBuilder().name("ldif").build());
135+
}
136+
137+
@Test
138+
void itemReaderWithNoNameAndDefaultSaveStateShouldFail() {
139+
assertThrows(IllegalArgumentException.class, () -> new LdifReaderBuilder().resource(ldifResource).build());
140+
}
141+
142+
@Test
143+
void itemReaderWithNoNameButSaveStateFalseShouldSucceed() throws Exception {
144+
LdifReader reader = new LdifReaderBuilder().resource(ldifResource).saveState(false).build();
145+
146+
verify(reader, expectedDns());
147+
}
148+
149+
private class TestCallback implements RecordCallbackHandler {
150+
151+
@Override
152+
public void handleRecord(LdapAttributes attributes) {
153+
callbackDn = attributes.getName().toString();
154+
}
155+
156+
}
157+
158+
}

0 commit comments

Comments
 (0)