Skip to content

Commit 02b8a55

Browse files
Copilotozlerhakan
andcommitted
Add comprehensive tests for record support to improve code coverage
Co-authored-by: ozlerhakan <[email protected]>
1 parent 8757a1d commit 02b8a55

File tree

3 files changed

+243
-0
lines changed

3 files changed

+243
-0
lines changed

src/test/java/com/poiji/deserialize/RecordDeserializationTest.java

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,184 @@ public void shouldMapExcelToRecordWithExcelCell() {
110110
assertThat(calculation.toDate().toString(), is("2018-06-30"));
111111
}
112112
}
113+
114+
@Test
115+
public void shouldMapExcelToRecordFromInputStream() {
116+
// Test with InputStream
117+
try (java.io.FileInputStream stream = new java.io.FileInputStream(
118+
new File("src/test/resources/employees.xlsx"))) {
119+
List<EmployeeRecord> employees = Poiji.fromExcel(
120+
stream,
121+
com.poiji.exception.PoijiExcelType.XLSX,
122+
EmployeeRecord.class
123+
);
124+
125+
assertThat(employees, notNullValue());
126+
assertThat(employees.size(), is(3));
127+
128+
EmployeeRecord firstEmployee = employees.get(0);
129+
assertThat(firstEmployee.employeeId(), is(123923L));
130+
assertThat(firstEmployee.name(), is("Joe"));
131+
} catch (Exception e) {
132+
throw new RuntimeException(e);
133+
}
134+
}
135+
136+
@Test
137+
public void shouldMapExcelToRecordFromInputStreamWithOptions() {
138+
// Test with InputStream and options
139+
PoijiOptions options = PoijiOptions.PoijiOptionsBuilder.settings()
140+
.headerStart(0)
141+
.build();
142+
143+
try (java.io.FileInputStream stream = new java.io.FileInputStream(
144+
new File("src/test/resources/employees.xlsx"))) {
145+
List<EmployeeRecord> employees = Poiji.fromExcel(
146+
stream,
147+
com.poiji.exception.PoijiExcelType.XLSX,
148+
EmployeeRecord.class,
149+
options
150+
);
151+
152+
assertThat(employees, notNullValue());
153+
assertThat(employees.size(), is(3));
154+
} catch (Exception e) {
155+
throw new RuntimeException(e);
156+
}
157+
}
158+
159+
@Test
160+
public void shouldMapXLSFromInputStream() {
161+
// Test XLS with InputStream
162+
try (java.io.FileInputStream stream = new java.io.FileInputStream(
163+
new File("src/test/resources/employees.xls"))) {
164+
List<EmployeeRecord> employees = Poiji.fromExcel(
165+
stream,
166+
com.poiji.exception.PoijiExcelType.XLS,
167+
EmployeeRecord.class
168+
);
169+
170+
assertThat(employees, notNullValue());
171+
assertThat(employees.size(), is(3));
172+
173+
EmployeeRecord firstEmployee = employees.get(0);
174+
assertThat(firstEmployee.employeeId(), is(123923L));
175+
} catch (Exception e) {
176+
throw new RuntimeException(e);
177+
}
178+
}
179+
180+
@Test
181+
public void shouldHandleRecordWithConsumer() {
182+
// Test with consumer interface
183+
final java.util.List<EmployeeRecord> collectedEmployees = new java.util.ArrayList<>();
184+
185+
Poiji.fromExcel(
186+
new File("src/test/resources/employees.xlsx"),
187+
EmployeeRecord.class,
188+
collectedEmployees::add
189+
);
190+
191+
assertThat(collectedEmployees.size(), is(3));
192+
assertThat(collectedEmployees.get(0).name(), is("Joe"));
193+
}
194+
195+
@Test
196+
public void shouldHandleRecordWithConsumerAndOptions() {
197+
// Test with consumer interface and options
198+
PoijiOptions options = PoijiOptions.PoijiOptionsBuilder.settings()
199+
.headerStart(0)
200+
.build();
201+
202+
final java.util.List<EmployeeRecord> collectedEmployees = new java.util.ArrayList<>();
203+
204+
Poiji.fromExcel(
205+
new File("src/test/resources/employees.xlsx"),
206+
EmployeeRecord.class,
207+
options,
208+
collectedEmployees::add
209+
);
210+
211+
assertThat(collectedEmployees.size(), is(3));
212+
}
213+
214+
@Test
215+
public void shouldMapExcelToRecordFromSheet() {
216+
// Test with Sheet API
217+
try {
218+
org.apache.poi.ss.usermodel.Workbook workbook = org.apache.poi.ss.usermodel.WorkbookFactory.create(
219+
new File("src/test/resources/employees.xlsx"));
220+
org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(0);
221+
222+
List<EmployeeRecord> employees = Poiji.fromExcel(
223+
sheet,
224+
EmployeeRecord.class
225+
);
226+
227+
assertThat(employees, notNullValue());
228+
assertThat(employees.size(), is(3));
229+
assertThat(employees.get(0).name(), is("Joe"));
230+
231+
workbook.close();
232+
} catch (Exception e) {
233+
throw new RuntimeException(e);
234+
}
235+
}
236+
237+
@Test
238+
public void shouldMapExcelToRecordFromSheetWithOptions() {
239+
// Test with Sheet API and options
240+
PoijiOptions options = PoijiOptions.PoijiOptionsBuilder.settings()
241+
.headerStart(0)
242+
.build();
243+
244+
try {
245+
org.apache.poi.ss.usermodel.Workbook workbook = org.apache.poi.ss.usermodel.WorkbookFactory.create(
246+
new File("src/test/resources/employees.xlsx"));
247+
org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(0);
248+
249+
List<EmployeeRecord> employees = Poiji.fromExcel(
250+
sheet,
251+
EmployeeRecord.class,
252+
options
253+
);
254+
255+
assertThat(employees, notNullValue());
256+
assertThat(employees.size(), is(3));
257+
258+
workbook.close();
259+
} catch (Exception e) {
260+
throw new RuntimeException(e);
261+
}
262+
}
263+
264+
@Test
265+
public void shouldMapExcelToRecordFromSheetWithConsumer() {
266+
// Test with Sheet API and consumer
267+
PoijiOptions options = PoijiOptions.PoijiOptionsBuilder.settings()
268+
.headerStart(0)
269+
.build();
270+
271+
final java.util.List<EmployeeRecord> collectedEmployees = new java.util.ArrayList<>();
272+
273+
try {
274+
org.apache.poi.ss.usermodel.Workbook workbook = org.apache.poi.ss.usermodel.WorkbookFactory.create(
275+
new File("src/test/resources/employees.xlsx"));
276+
org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(0);
277+
278+
Poiji.fromExcel(
279+
sheet,
280+
EmployeeRecord.class,
281+
options,
282+
collectedEmployees::add
283+
);
284+
285+
assertThat(collectedEmployees.size(), is(3));
286+
assertThat(collectedEmployees.get(0).name(), is("Joe"));
287+
288+
workbook.close();
289+
} catch (Exception e) {
290+
throw new RuntimeException(e);
291+
}
292+
}
113293
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.poiji.util;
2+
3+
import com.poiji.exception.PoijiInstantiationException;
4+
import org.junit.Test;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
import static org.hamcrest.CoreMatchers.is;
10+
import static org.hamcrest.MatcherAssert.assertThat;
11+
import static org.junit.Assert.assertFalse;
12+
import static org.junit.Assert.fail;
13+
14+
/**
15+
* Tests for ReflectUtil record support
16+
*/
17+
public class ReflectUtilRecordTest {
18+
19+
// Simple POJO for testing
20+
public static class SimplePojo {
21+
private String name;
22+
23+
public SimplePojo() {}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
}
33+
34+
@Test
35+
public void shouldReturnFalseForNonRecordClass() {
36+
// Test that isRecord returns false for regular classes
37+
assertFalse(ReflectUtil.isRecord(SimplePojo.class));
38+
assertFalse(ReflectUtil.isRecord(String.class));
39+
assertFalse(ReflectUtil.isRecord(Integer.class));
40+
}
41+
42+
@Test
43+
public void shouldThrowExceptionWhenCreatingRecordInstanceFromNonRecord() {
44+
// Test that newRecordInstance throws exception for non-record types
45+
Map<String, Object> values = new HashMap<>();
46+
values.put("name", "test");
47+
48+
try {
49+
ReflectUtil.newRecordInstance(SimplePojo.class, values);
50+
fail("Expected PoijiInstantiationException");
51+
} catch (PoijiInstantiationException e) {
52+
// Expected
53+
assertThat(e.getMessage().contains("is not a record"), is(true));
54+
}
55+
}
56+
57+
@Test
58+
public void shouldCreatePojoInstanceWithNewInstanceOf() {
59+
// Test that regular class instantiation still works
60+
SimplePojo pojo = ReflectUtil.newInstanceOf(SimplePojo.class);
61+
assertThat(pojo != null, is(true));
62+
}
63+
}

src/test/resources/employees.xlsx

7 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)