Skip to content

Commit

Permalink
#130 multiname: add support in saving
Browse files Browse the repository at this point in the history
  • Loading branch information
vaa25 committed Feb 8, 2020
1 parent 1bb6631 commit 673974b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/main/java/com/poiji/annotation/ExcelCellName.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@

/**
* Delimeter for column multiname.
*
* <p>
* Example: @ExcelCellName(value = "id,identifier", delimerer = ",")
* means that column with name 'id' will be mapped, or if no column 'id', then column 'identifier' will be mapped to this field.
* reading: column with name 'id' will be mapped into field, or if no column 'id',
* then column 'identifier' will be mapped into field.
* writing: field will be saved into column 'id'
*
* @return delimeter for column multiname.
*/
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/poiji/save/MappedFields.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ public MappedFields parseEntity() {
} else {
final ExcelCellName annotation = field.getAnnotation(ExcelCellName.class);
if (annotation != null) {
final String excelName = annotation.value();
final String delimeter = annotation.delimeter();
final String excelName = delimeter.isEmpty()
? annotation.value()
: annotation.value().substring(0, annotation.value().indexOf(delimeter));
final int order = annotation.order();
if (order == ABSENT_ORDER) {
unordered.add(field);
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/com/poiji/deserialize/MultinameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.poiji.deserialize;

import com.poiji.bind.Poiji;
import com.poiji.deserialize.model.ConcurrentEntity;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import static java.util.Collections.singletonList;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

@RunWith(Parameterized.class)
public class MultinameTest {

private final String path;

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

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

@Test
public void read() {
final List<ConcurrentEntity> expected = singletonList(new ConcurrentEntity().setPrimitiveLong(1).setText("1"));

final List<ConcurrentEntity> actual = Poiji.fromExcel(new File(path), ConcurrentEntity.class);

assertThat(actual, equalTo(expected));

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public final class ConcurrentEntity {

@ExcelCell(0)
private long primitiveLong;
@ExcelCellName(value = "TexT")
@ExcelCellName(value = "TexT,other", delimeter = ",")
private String text;

}
Binary file added src/test/resources/multiname.xls
Binary file not shown.
Binary file added src/test/resources/multiname.xlsx
Binary file not shown.

0 comments on commit 673974b

Please sign in to comment.