embulk-parser-poi_excel 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +35 -0
  3. data/build.gradle +1 -1
  4. data/classpath/embulk-parser-poi_excel-0.1.2.jar +0 -0
  5. data/src/main/java/org/embulk/parser/poi_excel/PoiExcelColumnValueType.java +2 -3
  6. data/src/main/java/org/embulk/parser/poi_excel/PoiExcelParserPlugin.java +34 -50
  7. data/src/main/java/org/embulk/parser/poi_excel/bean/PoiExcelColumnBean.java +300 -0
  8. data/src/main/java/org/embulk/parser/poi_excel/{visitor → bean}/PoiExcelColumnIndex.java +44 -27
  9. data/src/main/java/org/embulk/parser/poi_excel/bean/PoiExcelSheetBean.java +102 -0
  10. data/src/main/java/org/embulk/parser/poi_excel/visitor/AbstractPoiExcelCellAttributeVisitor.java +15 -13
  11. data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelCellCommentVisitor.java +2 -2
  12. data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelCellFontVisitor.java +2 -2
  13. data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelCellStyleVisitor.java +2 -2
  14. data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelCellValueVisitor.java +52 -31
  15. data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelClientAnchorVisitor.java +2 -2
  16. data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelColumnVisitor.java +30 -29
  17. data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelVisitorFactory.java +4 -0
  18. data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelVisitorValue.java +10 -21
  19. data/src/main/java/org/embulk/parser/poi_excel/visitor/embulk/BooleanCellVisitor.java +5 -0
  20. data/src/main/java/org/embulk/parser/poi_excel/visitor/embulk/CellVisitor.java +30 -0
  21. data/src/main/java/org/embulk/parser/poi_excel/visitor/embulk/DoubleCellVisitor.java +13 -1
  22. data/src/main/java/org/embulk/parser/poi_excel/visitor/embulk/LongCellVisitor.java +13 -1
  23. data/src/main/java/org/embulk/parser/poi_excel/visitor/embulk/StringCellVisitor.java +5 -0
  24. data/src/main/java/org/embulk/parser/poi_excel/visitor/embulk/TimestampCellVisitor.java +30 -8
  25. data/src/test/java/org/embulk/parser/poi_excel/TestPoiExcelParserPlugin.java +0 -1
  26. data/src/test/java/org/embulk/parser/poi_excel/TestPoiExcelParserPlugin_cellError.java +184 -0
  27. data/src/test/java/org/embulk/parser/poi_excel/TestPoiExcelParserPlugin_columnNumber.java +3 -5
  28. data/src/test/java/org/embulk/parser/poi_excel/TestPoiExcelParserPlugin_constant.java +54 -0
  29. data/src/test/java/org/embulk/parser/poi_excel/TestPoiExcelParserPlugin_convertError.java +113 -0
  30. data/src/test/resources/org/embulk/parser/poi_excel/test1.xls +0 -0
  31. metadata +9 -4
  32. data/classpath/embulk-parser-poi_excel-0.1.1.jar +0 -0
@@ -7,7 +7,7 @@ import java.util.Map;
7
7
 
8
8
  import org.apache.poi.ss.usermodel.Cell;
9
9
  import org.apache.poi.ss.usermodel.ClientAnchor;
10
- import org.embulk.parser.poi_excel.PoiExcelParserPlugin.ColumnOptionTask;
10
+ import org.embulk.parser.poi_excel.bean.PoiExcelColumnBean;
11
11
  import org.embulk.spi.Column;
12
12
 
13
13
  public class PoiExcelClientAnchorVisitor extends AbstractPoiExcelCellAttributeVisitor<ClientAnchor> {
@@ -25,7 +25,7 @@ public class PoiExcelClientAnchorVisitor extends AbstractPoiExcelCellAttributeVi
25
25
  }
26
26
 
27
27
  @Override
28
- protected ClientAnchor getAttributeSource(Column column, ColumnOptionTask option, Cell cell) {
28
+ protected ClientAnchor getAttributeSource(PoiExcelColumnBean bean, Cell cell) {
29
29
  throw new UnsupportedOperationException();
30
30
  }
31
31
 
@@ -1,14 +1,12 @@
1
1
  package org.embulk.parser.poi_excel.visitor;
2
2
 
3
3
  import java.text.MessageFormat;
4
- import java.util.List;
5
4
 
6
5
  import org.apache.poi.ss.usermodel.Cell;
7
6
  import org.apache.poi.ss.usermodel.Row;
8
7
  import org.apache.poi.ss.util.CellReference;
9
8
  import org.embulk.parser.poi_excel.PoiExcelColumnValueType;
10
- import org.embulk.parser.poi_excel.PoiExcelParserPlugin.ColumnOptionTask;
11
- import org.embulk.parser.poi_excel.PoiExcelParserPlugin.PluginTask;
9
+ import org.embulk.parser.poi_excel.bean.PoiExcelColumnBean;
12
10
  import org.embulk.parser.poi_excel.visitor.embulk.CellVisitor;
13
11
  import org.embulk.spi.Column;
14
12
  import org.embulk.spi.ColumnVisitor;
@@ -26,14 +24,6 @@ public class PoiExcelColumnVisitor implements ColumnVisitor {
26
24
  this.visitorValue = visitorValue;
27
25
  this.pageBuilder = visitorValue.getPageBuilder();
28
26
  this.factory = visitorValue.getVisitorFactory();
29
-
30
- initializeColumnOptions();
31
- }
32
-
33
- protected void initializeColumnOptions() {
34
- PluginTask task = visitorValue.getPluginTask();
35
- List<ColumnOptionTask> options = visitorValue.getColumnOptions();
36
- new PoiExcelColumnIndex().initializeColumnIndex(task, options);
37
27
  }
38
28
 
39
29
  public void setRow(Row row) {
@@ -70,16 +60,16 @@ public class PoiExcelColumnVisitor implements ColumnVisitor {
70
60
  visitCell(column, visitor);
71
61
  } catch (Exception e) {
72
62
  String sheetName = visitorValue.getSheet().getSheetName();
73
- String ref = new CellReference(currentRow.getRowNum(), visitorValue.getColumnOption(column)
74
- .getColumnIndex()).formatAsString();
63
+ String ref = new CellReference(currentRow.getRowNum(), visitorValue.getColumnBean(column).getColumnIndex())
64
+ .formatAsString();
75
65
  throw new RuntimeException(MessageFormat.format("error at {0} cell={1}!{2}. {3}", column, sheetName, ref,
76
66
  e.getMessage()), e);
77
67
  }
78
68
  }
79
69
 
80
70
  protected void visitCell(Column column, CellVisitor visitor) {
81
- ColumnOptionTask option = visitorValue.getColumnOption(column);
82
- PoiExcelColumnValueType valueType = option.getValueTypeEnum();
71
+ PoiExcelColumnBean bean = visitorValue.getColumnBean(column);
72
+ PoiExcelColumnValueType valueType = bean.getValueType();
83
73
 
84
74
  switch (valueType) {
85
75
  case SHEET_NAME:
@@ -89,14 +79,17 @@ public class PoiExcelColumnVisitor implements ColumnVisitor {
89
79
  visitor.visitRowNumber(column, currentRow.getRowNum() + 1);
90
80
  return;
91
81
  case COLUMN_NUMBER:
92
- visitor.visitColumnNumber(column, option.getColumnIndex() + 1);
82
+ visitor.visitColumnNumber(column, bean.getColumnIndex() + 1);
83
+ return;
84
+ case CONSTANT:
85
+ visitCellConstant(column, bean.getValueTypeSuffix(), visitor);
93
86
  return;
94
87
  default:
95
88
  break;
96
89
  }
97
90
 
98
91
  assert valueType.useCell();
99
- Cell cell = currentRow.getCell(option.getColumnIndex());
92
+ Cell cell = currentRow.getCell(bean.getColumnIndex());
100
93
  if (cell == null) {
101
94
  visitCellNull(column);
102
95
  return;
@@ -104,43 +97,51 @@ public class PoiExcelColumnVisitor implements ColumnVisitor {
104
97
  switch (valueType) {
105
98
  case CELL_VALUE:
106
99
  case CELL_FORMULA:
107
- visitCellValue(column, option, cell, visitor);
100
+ visitCellValue(bean, cell, visitor);
108
101
  return;
109
102
  case CELL_STYLE:
110
- visitCellStyle(column, option, cell, visitor);
103
+ visitCellStyle(bean, cell, visitor);
111
104
  return;
112
105
  case CELL_FONT:
113
- visitCellFont(column, option, cell, visitor);
106
+ visitCellFont(bean, cell, visitor);
114
107
  return;
115
108
  case CELL_COMMENT:
116
- visitCellComment(column, option, cell, visitor);
109
+ visitCellComment(bean, cell, visitor);
117
110
  return;
118
111
  default:
119
112
  throw new UnsupportedOperationException(MessageFormat.format("unsupported value_type={0}", valueType));
120
113
  }
121
114
  }
122
115
 
116
+ protected void visitCellConstant(Column column, String value, CellVisitor visitor) {
117
+ if (value == null) {
118
+ pageBuilder.setNull(column);
119
+ return;
120
+ }
121
+ visitor.visitCellValueString(column, null, value);
122
+ }
123
+
123
124
  protected void visitCellNull(Column column) {
124
125
  pageBuilder.setNull(column);
125
126
  }
126
127
 
127
- private void visitCellValue(Column column, ColumnOptionTask option, Cell cell, CellVisitor visitor) {
128
+ private void visitCellValue(PoiExcelColumnBean bean, Cell cell, CellVisitor visitor) {
128
129
  PoiExcelCellValueVisitor delegator = factory.getPoiExcelCellValueVisitor();
129
- delegator.visitCellValue(column, option, cell, visitor);
130
+ delegator.visitCellValue(bean, cell, visitor);
130
131
  }
131
132
 
132
- private void visitCellStyle(Column column, ColumnOptionTask option, Cell cell, CellVisitor visitor) {
133
+ private void visitCellStyle(PoiExcelColumnBean bean, Cell cell, CellVisitor visitor) {
133
134
  PoiExcelCellStyleVisitor delegator = factory.getPoiExcelCellStyleVisitor();
134
- delegator.visit(column, option, cell, visitor);
135
+ delegator.visit(bean, cell, visitor);
135
136
  }
136
137
 
137
- private void visitCellFont(Column column, ColumnOptionTask option, Cell cell, CellVisitor visitor) {
138
+ private void visitCellFont(PoiExcelColumnBean bean, Cell cell, CellVisitor visitor) {
138
139
  PoiExcelCellFontVisitor delegator = factory.getPoiExcelCellFontVisitor();
139
- delegator.visit(column, option, cell, visitor);
140
+ delegator.visit(bean, cell, visitor);
140
141
  }
141
142
 
142
- private void visitCellComment(Column column, ColumnOptionTask option, Cell cell, CellVisitor visitor) {
143
+ private void visitCellComment(PoiExcelColumnBean bean, Cell cell, CellVisitor visitor) {
143
144
  PoiExcelCellCommentVisitor delegator = factory.getPoiExcelCellCommentVisitor();
144
- delegator.visit(column, option, cell, visitor);
145
+ delegator.visit(bean, cell, visitor);
145
146
  }
146
147
  }
@@ -15,6 +15,10 @@ public class PoiExcelVisitorFactory {
15
15
  visitorValue.setVisitorFactory(this);
16
16
  }
17
17
 
18
+ public final PoiExcelVisitorValue getVisitorValue() {
19
+ return visitorValue;
20
+ }
21
+
18
22
  // visitor root (Embulk ColumnVisitor)
19
23
  private PoiExcelColumnVisitor poiExcelColumnVisitor;
20
24
 
@@ -1,28 +1,25 @@
1
1
  package org.embulk.parser.poi_excel.visitor;
2
2
 
3
- import java.util.ArrayList;
4
- import java.util.List;
5
-
6
3
  import org.apache.poi.ss.usermodel.Sheet;
7
- import org.embulk.parser.poi_excel.PoiExcelParserPlugin.ColumnOptionTask;
8
4
  import org.embulk.parser.poi_excel.PoiExcelParserPlugin.PluginTask;
5
+ import org.embulk.parser.poi_excel.bean.PoiExcelColumnBean;
6
+ import org.embulk.parser.poi_excel.bean.PoiExcelSheetBean;
9
7
  import org.embulk.spi.Column;
10
- import org.embulk.spi.ColumnConfig;
11
8
  import org.embulk.spi.PageBuilder;
12
- import org.embulk.spi.SchemaConfig;
9
+ import org.embulk.spi.Schema;
13
10
 
14
11
  public class PoiExcelVisitorValue {
15
12
  private final PluginTask task;
16
13
  private final Sheet sheet;
17
14
  private final PageBuilder pageBuilder;
15
+ private final PoiExcelSheetBean sheetBean;
18
16
  private PoiExcelVisitorFactory factory;
19
17
 
20
- private List<ColumnOptionTask> columnOptions;
21
-
22
- public PoiExcelVisitorValue(PluginTask task, Sheet sheet, PageBuilder pageBuilder) {
18
+ public PoiExcelVisitorValue(PluginTask task, Schema schema, Sheet sheet, PageBuilder pageBuilder) {
23
19
  this.task = task;
24
20
  this.sheet = sheet;
25
21
  this.pageBuilder = pageBuilder;
22
+ this.sheetBean = new PoiExcelSheetBean(task, schema, sheet);
26
23
  }
27
24
 
28
25
  public PluginTask getPluginTask() {
@@ -45,19 +42,11 @@ public class PoiExcelVisitorValue {
45
42
  return factory;
46
43
  }
47
44
 
48
- public ColumnOptionTask getColumnOption(Column column) {
49
- return getColumnOptions().get(column.getIndex());
45
+ public PoiExcelSheetBean getSheetBean() {
46
+ return sheetBean;
50
47
  }
51
48
 
52
- public List<ColumnOptionTask> getColumnOptions() {
53
- if (columnOptions == null) {
54
- SchemaConfig schemaConfig = task.getColumns();
55
- columnOptions = new ArrayList<>(schemaConfig.getColumnCount());
56
- for (ColumnConfig c : schemaConfig.getColumns()) {
57
- ColumnOptionTask option = c.getOption().loadConfig(ColumnOptionTask.class);
58
- columnOptions.add(option);
59
- }
60
- }
61
- return columnOptions;
49
+ public PoiExcelColumnBean getColumnBean(Column column) {
50
+ return sheetBean.getColumnBean(column);
62
51
  }
63
52
  }
@@ -51,4 +51,9 @@ public class BooleanCellVisitor extends CellVisitor {
51
51
  public void visitColumnNumber(Column column, int index1) {
52
52
  pageBuilder.setBoolean(column, index1 != 0);
53
53
  }
54
+
55
+ @Override
56
+ protected void doConvertErrorConstant(Column column, String value) throws Exception {
57
+ pageBuilder.setBoolean(column, Boolean.parseBoolean(value));
58
+ }
54
59
  }
@@ -1,6 +1,11 @@
1
1
  package org.embulk.parser.poi_excel.visitor.embulk;
2
2
 
3
+ import java.text.MessageFormat;
4
+
3
5
  import org.apache.poi.ss.usermodel.Cell;
6
+ import org.embulk.config.ConfigException;
7
+ import org.embulk.parser.poi_excel.bean.PoiExcelColumnBean;
8
+ import org.embulk.parser.poi_excel.bean.PoiExcelColumnBean.ErrorStrategy;
4
9
  import org.embulk.parser.poi_excel.visitor.PoiExcelVisitorValue;
5
10
  import org.embulk.spi.Column;
6
11
  import org.embulk.spi.PageBuilder;
@@ -38,4 +43,29 @@ public abstract class CellVisitor {
38
43
  public abstract void visitRowNumber(Column column, int index1);
39
44
 
40
45
  public abstract void visitColumnNumber(Column column, int index1);
46
+
47
+ protected void doConvertError(Column column, Object srcValue, Throwable t) {
48
+ PoiExcelColumnBean bean = visitorValue.getColumnBean(column);
49
+ ErrorStrategy strategy = bean.getConvertErrorStrategy();
50
+ switch (strategy.getStrategy()) {
51
+ default:
52
+ break;
53
+ case CONSTANT:
54
+ String value = strategy.getValue();
55
+ if (value == null) {
56
+ pageBuilder.setNull(column);
57
+ } else {
58
+ try {
59
+ doConvertErrorConstant(column, value);
60
+ } catch (Exception e) {
61
+ throw new ConfigException(MessageFormat.format("constant value convert error. value={0}", value), e);
62
+ }
63
+ }
64
+ return;
65
+ }
66
+
67
+ throw new RuntimeException(MessageFormat.format("convert error. value={0}", srcValue), t);
68
+ }
69
+
70
+ protected abstract void doConvertErrorConstant(Column column, String value) throws Exception;
41
71
  }
@@ -17,7 +17,14 @@ public class DoubleCellVisitor extends CellVisitor {
17
17
 
18
18
  @Override
19
19
  public void visitCellValueString(Column column, Object source, String value) {
20
- pageBuilder.setDouble(column, Double.parseDouble(value));
20
+ double d;
21
+ try {
22
+ d = Double.parseDouble(value);
23
+ } catch (NumberFormatException e) {
24
+ doConvertError(column, value, e);
25
+ return;
26
+ }
27
+ pageBuilder.setDouble(column, d);
21
28
  }
22
29
 
23
30
  @Override
@@ -51,4 +58,9 @@ public class DoubleCellVisitor extends CellVisitor {
51
58
  public void visitColumnNumber(Column column, int index1) {
52
59
  pageBuilder.setDouble(column, index1);
53
60
  }
61
+
62
+ @Override
63
+ protected void doConvertErrorConstant(Column column, String value) throws Exception {
64
+ pageBuilder.setDouble(column, Double.parseDouble(value));
65
+ }
54
66
  }
@@ -17,7 +17,14 @@ public class LongCellVisitor extends CellVisitor {
17
17
 
18
18
  @Override
19
19
  public void visitCellValueString(Column column, Object source, String value) {
20
- pageBuilder.setLong(column, Long.parseLong(value));
20
+ long l;
21
+ try {
22
+ l = Long.parseLong(value);
23
+ } catch (NumberFormatException e) {
24
+ doConvertError(column, value, e);
25
+ return;
26
+ }
27
+ pageBuilder.setLong(column, l);
21
28
  }
22
29
 
23
30
  @Override
@@ -51,4 +58,9 @@ public class LongCellVisitor extends CellVisitor {
51
58
  public void visitColumnNumber(Column column, int index1) {
52
59
  pageBuilder.setLong(column, index1);
53
60
  }
61
+
62
+ @Override
63
+ protected void doConvertErrorConstant(Column column, String value) throws Exception {
64
+ pageBuilder.setLong(column, Long.parseLong(value));
65
+ }
54
66
  }
@@ -60,4 +60,9 @@ public class StringCellVisitor extends CellVisitor {
60
60
  String value = CellReference.convertNumToColString(index1 - 1);
61
61
  pageBuilder.setString(column, value);
62
62
  }
63
+
64
+ @Override
65
+ protected void doConvertErrorConstant(Column column, String value) throws Exception {
66
+ pageBuilder.setString(column, value);
67
+ }
63
68
  }
@@ -4,10 +4,12 @@ import java.util.Date;
4
4
  import java.util.TimeZone;
5
5
 
6
6
  import org.apache.poi.ss.usermodel.DateUtil;
7
+ import org.apache.poi.ss.usermodel.Sheet;
7
8
  import org.embulk.parser.poi_excel.PoiExcelParserPlugin.PluginTask;
8
9
  import org.embulk.parser.poi_excel.visitor.PoiExcelVisitorValue;
9
10
  import org.embulk.spi.Column;
10
11
  import org.embulk.spi.time.Timestamp;
12
+ import org.embulk.spi.time.TimestampParseException;
11
13
  import org.embulk.spi.time.TimestampParser;
12
14
  import org.embulk.spi.util.Timestamps;
13
15
 
@@ -22,23 +24,33 @@ public class TimestampCellVisitor extends CellVisitor {
22
24
  TimestampParser parser = getTimestampParser(column);
23
25
  TimeZone tz = parser.getDefaultTimeZone().toTimeZone();
24
26
  Date date = DateUtil.getJavaDate(value, tz);
25
- pageBuilder.setTimestamp(column, Timestamp.ofEpochMilli(date.getTime()));
27
+ Timestamp t = Timestamp.ofEpochMilli(date.getTime());
28
+ pageBuilder.setTimestamp(column, t);
26
29
  }
27
30
 
28
31
  @Override
29
32
  public void visitCellValueString(Column column, Object source, String value) {
30
- TimestampParser parser = getTimestampParser(column);
31
- pageBuilder.setTimestamp(column, parser.parse(value));
33
+ Timestamp t;
34
+ try {
35
+ TimestampParser parser = getTimestampParser(column);
36
+ t = parser.parse(value);
37
+ } catch (TimestampParseException e) {
38
+ doConvertError(column, value, e);
39
+ return;
40
+ }
41
+ pageBuilder.setTimestamp(column, t);
32
42
  }
33
43
 
34
44
  @Override
35
45
  public void visitCellValueBoolean(Column column, Object source, boolean value) {
36
- throw new UnsupportedOperationException("unsupported conversion Excel boolean to Embulk timestamp.");
46
+ doConvertError(column, value, new UnsupportedOperationException(
47
+ "unsupported conversion Excel boolean to Embulk timestamp"));
37
48
  }
38
49
 
39
50
  @Override
40
51
  public void visitCellValueError(Column column, Object source, int code) {
41
- pageBuilder.setNull(column);
52
+ doConvertError(column, code, new UnsupportedOperationException(
53
+ "unsupported conversion Excel Cell error code to Embulk timestamp"));
42
54
  }
43
55
 
44
56
  @Override
@@ -48,17 +60,27 @@ public class TimestampCellVisitor extends CellVisitor {
48
60
 
49
61
  @Override
50
62
  public void visitSheetName(Column column) {
51
- throw new UnsupportedOperationException("unsupported conversion sheet_name to Embulk timestamp.");
63
+ Sheet sheet = visitorValue.getSheet();
64
+ doConvertError(column, sheet.getSheetName(), new UnsupportedOperationException(
65
+ "unsupported conversion sheet_name to Embulk timestamp"));
52
66
  }
53
67
 
54
68
  @Override
55
69
  public void visitRowNumber(Column column, int index1) {
56
- throw new UnsupportedOperationException("unsupported conversion row_number to Embulk timestamp.");
70
+ doConvertError(column, index1, new UnsupportedOperationException(
71
+ "unsupported conversion row_number to Embulk timestamp"));
57
72
  }
58
73
 
59
74
  @Override
60
75
  public void visitColumnNumber(Column column, int index1) {
61
- throw new UnsupportedOperationException("unsupported conversion column_number to Embulk timestamp.");
76
+ doConvertError(column, index1, new UnsupportedOperationException(
77
+ "unsupported conversion column_number to Embulk timestamp"));
78
+ }
79
+
80
+ @Override
81
+ protected void doConvertErrorConstant(Column column, String value) throws Exception {
82
+ TimestampParser parser = getTimestampParser(column);
83
+ pageBuilder.setTimestamp(column, parser.parse(value));
62
84
  }
63
85
 
64
86
  private TimestampParser[] timestampParsers;
@@ -75,7 +75,6 @@ public class TestPoiExcelParserPlugin {
75
75
  EmbulkTestParserConfig parser = tester.newParserConfig(PoiExcelParserPlugin.TYPE);
76
76
  parser.set("sheets", Arrays.asList("test1"));
77
77
  parser.set("skip_header_lines", 1);
78
- parser.set("cell_error_null", false);
79
78
  parser.addColumn("sheet", "string").set("value", "sheet_name");
80
79
  parser.addColumn("sheet-n", "long").set("value", "sheet_name");
81
80
  parser.addColumn("row", "long").set("value", "row_number");
@@ -0,0 +1,184 @@
1
+ package org.embulk.parser.poi_excel;
2
+
3
+ import static org.hamcrest.CoreMatchers.is;
4
+ import static org.hamcrest.CoreMatchers.nullValue;
5
+ import static org.junit.Assert.assertThat;
6
+ import static org.junit.Assert.fail;
7
+
8
+ import java.net.URL;
9
+ import java.text.SimpleDateFormat;
10
+ import java.util.List;
11
+
12
+ import org.apache.poi.ss.usermodel.FormulaError;
13
+ import org.embulk.parser.EmbulkPluginTester;
14
+ import org.embulk.parser.EmbulkTestOutputPlugin.OutputRecord;
15
+ import org.embulk.parser.EmbulkTestParserConfig;
16
+ import org.embulk.spi.time.Timestamp;
17
+ import org.junit.Test;
18
+
19
+ public class TestPoiExcelParserPlugin_cellError {
20
+
21
+ @Test
22
+ public void testCellError_default() throws Exception {
23
+ try (EmbulkPluginTester tester = new EmbulkPluginTester()) {
24
+ tester.addParserPlugin(PoiExcelParserPlugin.TYPE, PoiExcelParserPlugin.class);
25
+
26
+ EmbulkTestParserConfig parser = tester.newParserConfig(PoiExcelParserPlugin.TYPE);
27
+ parser.set("sheet", "test1");
28
+ parser.set("skip_header_lines", 7);
29
+ parser.addColumn("b", "boolean").set("column_number", "A");
30
+ parser.addColumn("l", "long").set("column_number", "A");
31
+ parser.addColumn("d", "double").set("column_number", "A");
32
+ parser.addColumn("s", "string").set("column_number", "A");
33
+ parser.addColumn("t", "timestamp").set("column_number", "A");
34
+
35
+ URL inFile = getClass().getResource("test1.xls");
36
+ List<OutputRecord> result = tester.runParser(inFile, parser);
37
+
38
+ assertThat(result.size(), is(1));
39
+ OutputRecord r = result.get(0);
40
+ assertThat(r.getAsBoolean("b"), is(nullValue()));
41
+ assertThat(r.getAsLong("l"), is(nullValue()));
42
+ assertThat(r.getAsDouble("d"), is(nullValue()));
43
+ assertThat(r.getAsString("s"), is(nullValue()));
44
+ assertThat(r.getAsString("t"), is(nullValue()));
45
+ }
46
+ }
47
+
48
+ @Test
49
+ public void testCellError_code() throws Exception {
50
+ try (EmbulkPluginTester tester = new EmbulkPluginTester()) {
51
+ tester.addParserPlugin(PoiExcelParserPlugin.TYPE, PoiExcelParserPlugin.class);
52
+
53
+ EmbulkTestParserConfig parser = tester.newParserConfig(PoiExcelParserPlugin.TYPE);
54
+ parser.set("sheet", "test1");
55
+ parser.set("skip_header_lines", 7);
56
+ parser.set("on_cell_error", "error_code");
57
+ parser.addColumn("b", "boolean").set("column_number", "A");
58
+ parser.addColumn("l", "long").set("column_number", "A");
59
+ parser.addColumn("d", "double").set("column_number", "A");
60
+ parser.addColumn("s", "string").set("column_number", "A");
61
+
62
+ URL inFile = getClass().getResource("test1.xls");
63
+ List<OutputRecord> result = tester.runParser(inFile, parser);
64
+
65
+ OutputRecord r = result.get(0);
66
+ assertThat(r.getAsBoolean("b"), is(nullValue()));
67
+ assertThat(r.getAsLong("l"), is((long) FormulaError.DIV0.getCode()));
68
+ assertThat(r.getAsDouble("d"), is((double) FormulaError.DIV0.getCode()));
69
+ assertThat(r.getAsString("s"), is("#DIV/0!"));
70
+ }
71
+ }
72
+
73
+ @Test
74
+ public void testCellError_null() throws Exception {
75
+ try (EmbulkPluginTester tester = new EmbulkPluginTester()) {
76
+ tester.addParserPlugin(PoiExcelParserPlugin.TYPE, PoiExcelParserPlugin.class);
77
+
78
+ EmbulkTestParserConfig parser = tester.newParserConfig(PoiExcelParserPlugin.TYPE);
79
+ parser.set("sheet", "test1");
80
+ parser.set("skip_header_lines", 7);
81
+ parser.set("on_cell_error", "constant");
82
+ parser.addColumn("b", "boolean").set("column_number", "A");
83
+ parser.addColumn("l", "long").set("column_number", "A");
84
+ parser.addColumn("d", "double").set("column_number", "A");
85
+ parser.addColumn("s", "string").set("column_number", "A");
86
+ parser.addColumn("t", "timestamp").set("column_number", "A");
87
+
88
+ URL inFile = getClass().getResource("test1.xls");
89
+ List<OutputRecord> result = tester.runParser(inFile, parser);
90
+
91
+ assertThat(result.size(), is(1));
92
+ OutputRecord r = result.get(0);
93
+ assertThat(r.getAsBoolean("b"), is(nullValue()));
94
+ assertThat(r.getAsLong("l"), is(nullValue()));
95
+ assertThat(r.getAsDouble("d"), is(nullValue()));
96
+ assertThat(r.getAsString("s"), is(nullValue()));
97
+ assertThat(r.getAsString("t"), is(nullValue()));
98
+ }
99
+ }
100
+
101
+ @Test
102
+ public void testCellError_empty() throws Exception {
103
+ try (EmbulkPluginTester tester = new EmbulkPluginTester()) {
104
+ tester.addParserPlugin(PoiExcelParserPlugin.TYPE, PoiExcelParserPlugin.class);
105
+
106
+ EmbulkTestParserConfig parser = tester.newParserConfig(PoiExcelParserPlugin.TYPE);
107
+ parser.set("sheet", "test1");
108
+ parser.set("skip_header_lines", 7);
109
+ parser.set("on_cell_error", "constant.zzz");
110
+ parser.addColumn("s1", "string").set("column_number", "A");
111
+ parser.addColumn("s2", "string").set("column_number", "A").set("on_cell_error", "constant");
112
+ parser.addColumn("s3", "string").set("column_number", "A").set("on_cell_error", "constant.");
113
+ parser.addColumn("s4", "string").set("column_number", "A").set("on_cell_error", "constant. ");
114
+
115
+ URL inFile = getClass().getResource("test1.xls");
116
+ List<OutputRecord> result = tester.runParser(inFile, parser);
117
+
118
+ assertThat(result.size(), is(1));
119
+ OutputRecord r = result.get(0);
120
+ assertThat(r.getAsString("s1"), is("zzz"));
121
+ assertThat(r.getAsString("s2"), is(nullValue()));
122
+ assertThat(r.getAsString("s3"), is(""));
123
+ assertThat(r.getAsString("s4"), is(" "));
124
+ }
125
+ }
126
+
127
+ @Test
128
+ public void testCellError_constant() throws Exception {
129
+ try (EmbulkPluginTester tester = new EmbulkPluginTester()) {
130
+ tester.addParserPlugin(PoiExcelParserPlugin.TYPE, PoiExcelParserPlugin.class);
131
+
132
+ EmbulkTestParserConfig parser = tester.newParserConfig(PoiExcelParserPlugin.TYPE);
133
+ parser.set("sheet", "test1");
134
+ parser.set("skip_header_lines", 7);
135
+ parser.set("on_cell_error", "constant.0");
136
+ parser.addColumn("b", "boolean").set("column_number", "A");
137
+ parser.addColumn("l", "long").set("column_number", "A");
138
+ parser.addColumn("d", "double").set("column_number", "A");
139
+ parser.addColumn("s", "string").set("column_number", "A");
140
+ parser.addColumn("t", "timestamp").set("column_number", "A").set("format", "%Y/%m/%d")
141
+ .set("on_cell_error", "constant.2000/1/1");
142
+
143
+ URL inFile = getClass().getResource("test1.xls");
144
+ List<OutputRecord> result = tester.runParser(inFile, parser);
145
+
146
+ OutputRecord r = result.get(0);
147
+ assertThat(r.getAsBoolean("b"), is(false));
148
+ assertThat(r.getAsLong("l"), is(0L));
149
+ assertThat(r.getAsDouble("d"), is(0d));
150
+ assertThat(r.getAsString("s"), is("0"));
151
+ assertThat(r.getAsTimestamp("t"),
152
+ is(Timestamp.ofEpochMilli(new SimpleDateFormat("yyyy/MM/dd z").parse("2000/01/01 UTC").getTime())));
153
+ }
154
+ }
155
+
156
+ @Test
157
+ public void testCellError_exception() throws Exception {
158
+ try (EmbulkPluginTester tester = new EmbulkPluginTester()) {
159
+ tester.addParserPlugin(PoiExcelParserPlugin.TYPE, PoiExcelParserPlugin.class);
160
+
161
+ EmbulkTestParserConfig parser = tester.newParserConfig(PoiExcelParserPlugin.TYPE);
162
+ parser.set("sheet", "test1");
163
+ parser.set("skip_header_lines", 7);
164
+ parser.set("on_cell_error", "exception");
165
+ parser.addColumn("b", "boolean").set("column_number", "A");
166
+ parser.addColumn("l", "long").set("column_number", "A");
167
+ parser.addColumn("d", "double").set("column_number", "A");
168
+ parser.addColumn("s", "string").set("column_number", "A");
169
+ parser.addColumn("t", "timestamp").set("column_number", "A");
170
+
171
+ URL inFile = getClass().getResource("test1.xls");
172
+ try {
173
+ tester.runParser(inFile, parser);
174
+ } catch (Exception e) {
175
+ Throwable c1 = e.getCause();
176
+ assertThat(c1.getMessage().contains("error at Column"), is(true));
177
+ Throwable c2 = c1.getCause();
178
+ assertThat(c2.getMessage().contains("encount cell error"), is(true));
179
+ return; // success
180
+ }
181
+ fail("must throw Exception");
182
+ }
183
+ }
184
+ }