Skip to content

Commit

Permalink
fix #294
Browse files Browse the repository at this point in the history
  • Loading branch information
ozlerhakan committed Sep 24, 2023
1 parent 60ce106 commit 4c4688e
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 89 deletions.
33 changes: 23 additions & 10 deletions src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,29 @@ private <T> T tailSetFieldValue(Row currentRow, Class<? super T> type, T instanc
throw new PoijiMultiRowException("Problem(s) occurred while reading data", errors);
}

Map<String, String> excelUnknownCellsMap = StreamSupport
.stream(Spliterators.spliteratorUnknownSize(currentRow.cellIterator(), Spliterator.ORDERED), false)
.filter(cell -> indexToTitle.size() != 0)
.filter(cell -> !mappedColumnIndices.contains(cell.getColumnIndex()))
.filter(cell -> !cell.toString().isEmpty())
.collect(Collectors.toMap(
cell -> indexToTitle.get(cell.getColumnIndex()),
Object::toString));

unknownCells.forEach(field -> setFieldData(instance, field, excelUnknownCellsMap));
if (unknownCells.isEmpty()) {
return instance;
}

if (!indexToTitle.isEmpty()) {
Map<String, String> excelUnknownCellsMap = StreamSupport
.stream(Spliterators.spliteratorUnknownSize(currentRow.cellIterator(), Spliterator.ORDERED), false)
.filter(cell -> !mappedColumnIndices.contains(cell.getColumnIndex()))
.filter(cell -> !cell.toString().isEmpty())
.collect(Collectors.toMap(
cell -> indexToTitle.get(cell.getColumnIndex()),
Object::toString));
unknownCells.forEach(field -> setFieldData(instance, field, excelUnknownCellsMap));
} else {
Map<String, String> excelUnknownCellsMap = StreamSupport
.stream(Spliterators.spliteratorUnknownSize(currentRow.cellIterator(), Spliterator.ORDERED), false)
.filter(cell -> !mappedColumnIndices.contains(cell.getColumnIndex()))
.filter(cell -> !cell.toString().isEmpty())
.collect(Collectors.toMap(
cell -> String.valueOf(cell.getColumnIndex()),
Object::toString));
unknownCells.forEach(field -> setFieldData(instance, field, excelUnknownCellsMap));
}

return instance;
}
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/com/poiji/bind/mapping/PoijiHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,15 @@ private boolean setValue(String content, Class<? super T> type, int column) {
} else {
excelUnknownCellsMap = (Map<String, String>) field.get(instance);
}
excelUnknownCellsMap.put(indexToTitle.get(column), content);
String index = indexToTitle.get(column);
if (index == null) {
excelUnknownCellsMap.put(String.valueOf(column), content);
} else {
excelUnknownCellsMap.put(indexToTitle.get(column), content);
}
} catch (IllegalAccessException e) {
throw new IllegalCastException("Could not read content of field " + field.getName() + " on Object {" + instance + "}");
throw new IllegalCastException("Could not read content of field " + field.getName()
+ " on Object {" + instance + "}");
}
}
});
Expand Down Expand Up @@ -167,7 +173,7 @@ private boolean setValue(Field field, int column, String content, Object ins) {
excelCellNameAnnotations.add(excelCellName);
final String titleName = formatting.transform(options, excelCellName.value());
final Integer titleColumn = titleToIndex.get(titleName);
//Fix both columns mapped to name passing this condition below
// Fix both columns mapped to name passing this condition below
if (titleColumn != null && titleColumn == column) {
Object o = casting.castValue(field, content, internalRow, column, options);
ReflectUtil.setFieldData(field, o, ins);
Expand Down Expand Up @@ -234,7 +240,7 @@ private String getTitleNameForMap(String cellContent, int columnIndex) {

@Override
public void headerFooter(String text, boolean isHeader, String tagName) {
//no-op
// no-op
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,79 +19,108 @@
@RunWith(Parameterized.class)
public class UnknownCellsIdenticalHeadersTest {

private String path;

public UnknownCellsIdenticalHeadersTest(String path) {
this.path = path;
}

@Parameterized.Parameters
public static List<String> excel() {
return Arrays.asList(
"src/test/resources/unknown-cells-identical-headers.xlsx",
"src/test/resources/unknown-cells-identical-headers.xls"
);
}

@Test
public void byName() {
List<OrgWithUnknownCellsByName> organisations = Poiji.fromExcel(
new File(path),
OrgWithUnknownCellsByName.class,
PoijiOptions.PoijiOptionsBuilder.settings()
.sheetName("Organisation")
.build()
);

assertThat(organisations, notNullValue());
assertThat(organisations.size(), is(2));

OrgWithUnknownCellsByName firstRow = organisations.stream()
.filter(org -> org.getId().equals("CrEaTe"))
.findFirst()
.get();
assertThat(firstRow.getUnknownCells().size(), is(2));
assertThat(firstRow.getUnknownCells().get("Tag"), is("testTag"));
assertThat(firstRow.getUnknownCells().get("Tag@5"), is("rndTag"));


OrgWithUnknownCellsByName secondRow = organisations.stream()
.filter(org -> org.getId().equals("8d9e6430-8626-4556-8004-079085d2df2d"))
.findFirst()
.get();
assertThat(secondRow.getUnknownCells().size(), is(2));
assertThat(secondRow.getUnknownCells().get("Tag"), is("testTag2"));
assertThat(secondRow.getUnknownCells().get("Tag@5"), is("rndTag2"));
}

@Test
public void byIndex() {
List<OrgWithUnknownCells> organisations = Poiji.fromExcel(
new File(path),
OrgWithUnknownCells.class,
PoijiOptions.PoijiOptionsBuilder.settings()
.sheetName("Organisation")
.build()
);

assertThat(organisations, notNullValue());
assertThat(organisations.size(), is(2));

OrgWithUnknownCells firstRow = organisations.stream()
.filter(org -> org.getId().equals("CrEaTe"))
.findFirst()
.get();
assertThat(firstRow.getUnknownCells().size(), is(2));
assertThat(firstRow.getUnknownCells().get("Tag"), is("testTag"));
assertThat(firstRow.getUnknownCells().get("Tag@5"), is("rndTag"));


OrgWithUnknownCells secondRow = organisations.stream()
.filter(org -> org.getId().equals("8d9e6430-8626-4556-8004-079085d2df2d"))
.findFirst()
.get();
assertThat(secondRow.getUnknownCells().size(), is(2));
assertThat(secondRow.getUnknownCells().get("Tag"), is("testTag2"));
assertThat(secondRow.getUnknownCells().get("Tag@5"), is("rndTag2"));
}
private String path;

public UnknownCellsIdenticalHeadersTest(String path) {
this.path = path;
}

@Parameterized.Parameters
public static List<String> excel() {
return Arrays.asList("src/test/resources/unknown-cells-identical-headers.xlsx",
"src/test/resources/unknown-cells-identical-headers.xls");
}

@Test
public void byName() {
List<OrgWithUnknownCellsByName> organisations = Poiji.fromExcel(
new File(path),
OrgWithUnknownCellsByName.class,
PoijiOptions.PoijiOptionsBuilder.settings()
.sheetName("Organisation")
.build());

assertThat(organisations, notNullValue());
assertThat(organisations.size(), is(2));

OrgWithUnknownCellsByName firstRow = organisations.stream()
.filter(org -> org.getId().equals("CrEaTe"))
.findFirst()
.get();
assertThat(firstRow.getUnknownCells().size(), is(2));
assertThat(firstRow.getUnknownCells().get("Tag"), is("testTag"));
assertThat(firstRow.getUnknownCells().get("Tag@5"), is("rndTag"));

OrgWithUnknownCellsByName secondRow = organisations.stream()
.filter(org -> org.getId().equals("8d9e6430-8626-4556-8004-079085d2df2d"))
.findFirst()
.get();
assertThat(secondRow.getUnknownCells().size(), is(2));
assertThat(secondRow.getUnknownCells().get("Tag"), is("testTag2"));
assertThat(secondRow.getUnknownCells().get("Tag@5"), is("rndTag2"));
}

@Test
public void byIndex() {
List<OrgWithUnknownCells> organisations = Poiji.fromExcel(
new File(path),
OrgWithUnknownCells.class,
PoijiOptions.PoijiOptionsBuilder.settings()
.sheetName("Organisation")
.build());

assertThat(organisations, notNullValue());
assertThat(organisations.size(), is(2));

OrgWithUnknownCells firstRow = organisations.stream()
.filter(org -> org.getId().equals("CrEaTe"))
.findFirst()
.get();
assertThat(firstRow.getUnknownCells().size(), is(2));
assertThat(firstRow.getUnknownCells().get("Tag"), is("testTag"));
assertThat(firstRow.getUnknownCells().get("Tag@5"), is("rndTag"));

OrgWithUnknownCells secondRow = organisations.stream()
.filter(org -> org.getId().equals("8d9e6430-8626-4556-8004-079085d2df2d"))
.findFirst()
.get();
assertThat(secondRow.getUnknownCells().size(), is(2));
assertThat(secondRow.getUnknownCells().get("Tag"), is("testTag2"));
assertThat(secondRow.getUnknownCells().get("Tag@5"), is("rndTag2"));
}

@Test
public void byIndexNoHeader() {
List<OrgWithUnknownCells> organisations = Poiji.fromExcel(
new File(path),
OrgWithUnknownCells.class,
PoijiOptions.PoijiOptionsBuilder.settings()
.sheetName("Organisation")
.skip(1)
.headerCount(0)
.build());

assertThat(organisations, notNullValue());
assertThat(organisations.size(), is(2));

OrgWithUnknownCells firstRow = organisations.stream()
.filter(org -> org.getId().equals("CrEaTe"))
.findFirst()
.get();
assertThat(firstRow.getUnknownCells().size(), is(2));
assertThat(firstRow.getUnknownCells().get("Tag") == null ? firstRow.getUnknownCells().get("4")
: firstRow.getUnknownCells().get("Tag"), is("testTag"));
assertThat(firstRow.getUnknownCells().get("Tag@5") == null ? firstRow.getUnknownCells().get("5")
: firstRow.getUnknownCells().get("Tag@5"), is("rndTag"));

OrgWithUnknownCells secondRow = organisations.stream()
.filter(org -> org.getId().equals("8d9e6430-8626-4556-8004-079085d2df2d"))
.findFirst()
.get();
assertThat(secondRow.getUnknownCells().size(), is(2));
assertThat(secondRow.getUnknownCells().get("Tag") == null ? secondRow.getUnknownCells().get("4")
: secondRow.getUnknownCells().get("Tag"), is("testTag2"));
assertThat(secondRow.getUnknownCells().get("Tag@5") == null ? secondRow.getUnknownCells().get("5")
: secondRow.getUnknownCells().get("Tag@5"), is("rndTag2"));
}
}

0 comments on commit 4c4688e

Please sign in to comment.