Skip to content

Commit 0747d9e

Browse files
Copilotozlerhakan
andcommitted
Add tests for @ExcelUnknownCells with records
Co-authored-by: ozlerhakan <[email protected]>
1 parent f83a6ea commit 0747d9e

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@
235235
<exclude>**/model/byid/CalculationRecord.java</exclude>
236236
<exclude>**/model/byname/EmployeeRecord.java</exclude>
237237
<exclude>**/model/byname/PersonRecord.java</exclude>
238+
<exclude>**/model/byname/OrgWithUnknownCellsRecord.java</exclude>
238239
<exclude>**/RecordDeserializationTest.java</exclude>
239240
</testExcludes>
240241
</configuration>

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.poiji.bind.Poiji;
44
import com.poiji.deserialize.model.byid.CalculationRecord;
55
import com.poiji.deserialize.model.byname.EmployeeRecord;
6+
import com.poiji.deserialize.model.byname.OrgWithUnknownCellsRecord;
67
import com.poiji.deserialize.model.byname.PersonRecord;
78
import com.poiji.option.PoijiOptions;
89
import org.junit.Test;
@@ -291,4 +292,57 @@ public void shouldMapExcelToRecordFromSheetWithConsumer() {
291292
fail(e.getMessage());
292293
}
293294
}
295+
296+
@Test
297+
public void shouldMapExcelToRecordWithUnknownCells() {
298+
// Test with @ExcelUnknownCells annotation
299+
List<OrgWithUnknownCellsRecord> organisations = Poiji.fromExcel(
300+
new File("src/test/resources/unknown-cells.xlsx"),
301+
OrgWithUnknownCellsRecord.class,
302+
PoijiOptions.PoijiOptionsBuilder.settings()
303+
.sheetName("Organisation")
304+
.build()
305+
);
306+
307+
assertThat(organisations, notNullValue());
308+
assertThat(organisations.size(), is(2));
309+
310+
// Test first row - verify unknownCells captures unmapped "Region" column
311+
OrgWithUnknownCellsRecord firstRow = organisations.stream()
312+
.filter(org -> org.id().equals("CrEaTe"))
313+
.findFirst()
314+
.get();
315+
assertThat(firstRow.unknownCells(), notNullValue());
316+
assertThat(firstRow.unknownCells().size(), is(1));
317+
assertThat(firstRow.unknownCells().get("Region"), is("EMEA"));
318+
319+
// Test second row
320+
OrgWithUnknownCellsRecord secondRow = organisations.stream()
321+
.filter(org -> org.id().equals("8d9e6430-8626-4556-8004-079085d2df2d"))
322+
.findFirst()
323+
.get();
324+
assertThat(secondRow.unknownCells(), notNullValue());
325+
assertThat(secondRow.unknownCells().size(), is(1));
326+
assertThat(secondRow.unknownCells().get("Region"), is("NA"));
327+
}
328+
329+
@Test
330+
public void shouldMapXLSToRecordWithUnknownCells() {
331+
// Test XLS format with @ExcelUnknownCells annotation
332+
List<OrgWithUnknownCellsRecord> organisations = Poiji.fromExcel(
333+
new File("src/test/resources/unknown-cells.xls"),
334+
OrgWithUnknownCellsRecord.class,
335+
PoijiOptions.PoijiOptionsBuilder.settings()
336+
.sheetName("Organisation")
337+
.build()
338+
);
339+
340+
assertThat(organisations, notNullValue());
341+
assertThat(organisations.size(), is(2));
342+
343+
// Verify unknownCells are captured correctly
344+
OrgWithUnknownCellsRecord firstRow = organisations.get(0);
345+
assertThat(firstRow.unknownCells(), notNullValue());
346+
assertThat(firstRow.unknownCells().containsKey("Region"), is(true));
347+
}
294348
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.poiji.deserialize.model.byname;
2+
3+
import com.poiji.annotation.ExcelCellName;
4+
import com.poiji.annotation.ExcelRow;
5+
import com.poiji.annotation.ExcelUnknownCells;
6+
7+
import java.util.Map;
8+
9+
/**
10+
* A record class to test @ExcelUnknownCells support
11+
*/
12+
public record OrgWithUnknownCellsRecord(
13+
@ExcelRow
14+
int rowIndex,
15+
16+
@ExcelCellName(OrgWithUnknownCellsByName.HEADER_ORGANISATION_ID)
17+
String id,
18+
19+
@ExcelCellName(OrgWithUnknownCellsByName.HEADER_ORGANISATION_EXTERNAL_ID)
20+
String externalId,
21+
22+
@ExcelUnknownCells
23+
Map<String, String> unknownCells,
24+
25+
@ExcelCellName(OrgWithUnknownCellsByName.HEADER_ORGANISATION_NAME)
26+
String name,
27+
28+
@ExcelCellName(OrgWithUnknownCellsByName.HEADER_CUSTOMER_EXTERNAL_ID)
29+
String customerExternalId
30+
) {
31+
}

src/test/resources/employees.xlsx

0 Bytes
Binary file not shown.

src/test/resources/person.xlsx

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)