embulk-parser-poi_excel 0.1.0

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 (48) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +120 -0
  5. data/build.gradle +77 -0
  6. data/classpath/commons-codec-1.9.jar +0 -0
  7. data/classpath/embulk-parser-poi_excel-0.1.0.jar +0 -0
  8. data/classpath/embulk-standards-0.7.5.jar +0 -0
  9. data/classpath/poi-3.13.jar +0 -0
  10. data/classpath/poi-ooxml-3.13.jar +0 -0
  11. data/classpath/poi-ooxml-schemas-3.13.jar +0 -0
  12. data/classpath/stax-api-1.0.1.jar +0 -0
  13. data/classpath/xmlbeans-2.6.0.jar +0 -0
  14. data/gradle/wrapper/gradle-wrapper.jar +0 -0
  15. data/gradle/wrapper/gradle-wrapper.properties +6 -0
  16. data/gradlew +164 -0
  17. data/gradlew.bat +90 -0
  18. data/lib/embulk/guess/poi_excel.rb +61 -0
  19. data/lib/embulk/parser/poi_excel.rb +3 -0
  20. data/src/main/java/org/embulk/parser/poi_excel/PoiExcelColumnValueType.java +39 -0
  21. data/src/main/java/org/embulk/parser/poi_excel/PoiExcelParserPlugin.java +199 -0
  22. data/src/main/java/org/embulk/parser/poi_excel/visitor/AbstractPoiExcelCellAttributeVisitor.java +133 -0
  23. data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelCellCommentVisitor.java +68 -0
  24. data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelCellFontVisitor.java +117 -0
  25. data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelCellStyleVisitor.java +205 -0
  26. data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelCellVisitor.java +194 -0
  27. data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelColorVisitor.java +81 -0
  28. data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelColumnIndex.java +174 -0
  29. data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelColumnVisitor.java +146 -0
  30. data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelVisitorFactory.java +171 -0
  31. data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelVisitorValue.java +63 -0
  32. data/src/main/java/org/embulk/parser/poi_excel/visitor/embulk/BooleanCellVisitor.java +54 -0
  33. data/src/main/java/org/embulk/parser/poi_excel/visitor/embulk/CellVisitor.java +41 -0
  34. data/src/main/java/org/embulk/parser/poi_excel/visitor/embulk/DoubleCellVisitor.java +54 -0
  35. data/src/main/java/org/embulk/parser/poi_excel/visitor/embulk/LongCellVisitor.java +54 -0
  36. data/src/main/java/org/embulk/parser/poi_excel/visitor/embulk/StringCellVisitor.java +63 -0
  37. data/src/main/java/org/embulk/parser/poi_excel/visitor/embulk/TimestampCellVisitor.java +73 -0
  38. data/src/test/java/org/embulk/parser/EmbulkPluginTester.java +176 -0
  39. data/src/test/java/org/embulk/parser/EmbulkTestFileInputPlugin.java +83 -0
  40. data/src/test/java/org/embulk/parser/EmbulkTestOutputPlugin.java +193 -0
  41. data/src/test/java/org/embulk/parser/EmbulkTestParserConfig.java +51 -0
  42. data/src/test/java/org/embulk/parser/poi_excel/TestPoiExcelParserPlugin.java +187 -0
  43. data/src/test/java/org/embulk/parser/poi_excel/TestPoiExcelParserPlugin_cellComment.java +42 -0
  44. data/src/test/java/org/embulk/parser/poi_excel/TestPoiExcelParserPlugin_cellFont.java +125 -0
  45. data/src/test/java/org/embulk/parser/poi_excel/TestPoiExcelParserPlugin_cellStyle.java +132 -0
  46. data/src/test/java/org/embulk/parser/poi_excel/TestPoiExcelParserPlugin_columnNumber.java +188 -0
  47. data/src/test/resources/org/embulk/parser/poi_excel/test1.xls +0 -0
  48. metadata +118 -0
@@ -0,0 +1,187 @@
1
+ package org.embulk.parser.poi_excel;
2
+
3
+ import static org.hamcrest.CoreMatchers.is;
4
+ import static org.junit.Assert.assertThat;
5
+
6
+ import java.net.URL;
7
+ import java.text.ParseException;
8
+ import java.text.SimpleDateFormat;
9
+ import java.util.Arrays;
10
+ import java.util.List;
11
+ import java.util.TimeZone;
12
+
13
+ import org.embulk.config.ConfigSource;
14
+ import org.embulk.parser.EmbulkPluginTester;
15
+ import org.embulk.parser.EmbulkTestOutputPlugin.OutputRecord;
16
+ import org.embulk.parser.EmbulkTestParserConfig;
17
+ import org.embulk.spi.time.Timestamp;
18
+ import org.junit.Test;
19
+
20
+ public class TestPoiExcelParserPlugin {
21
+
22
+ @Test
23
+ public void test1() throws ParseException {
24
+ try (EmbulkPluginTester tester = new EmbulkPluginTester()) {
25
+ tester.addParserPlugin(PoiExcelParserPlugin.TYPE, PoiExcelParserPlugin.class);
26
+
27
+ EmbulkTestParserConfig parser = tester.newParserConfig(PoiExcelParserPlugin.TYPE);
28
+ parser.set("sheet", "test1");
29
+ parser.set("skip_header_lines", 1);
30
+ parser.set("default_timezone", "Asia/Tokyo");
31
+ parser.addColumn("boolean", "boolean");
32
+ parser.addColumn("long", "long");
33
+ parser.addColumn("double", "double");
34
+ parser.addColumn("string", "string");
35
+ parser.addColumn("timestamp", "timestamp").set("format", "%Y/%m/%d");
36
+
37
+ URL inFile = getClass().getResource("test1.xls");
38
+ List<OutputRecord> result = tester.runParser(inFile, parser);
39
+
40
+ assertThat(result.size(), is(7));
41
+ check1(result, 0, true, 123L, 123.4d, "abc", "2015/10/4");
42
+ check1(result, 1, false, 456L, 456.7d, "def", "2015/10/5");
43
+ check1(result, 2, false, 123L, 123d, "456", "2015/10/6");
44
+ check1(result, 3, true, 123L, 123.4d, "abc", "2015/10/7");
45
+ check1(result, 4, true, 123L, 123.4d, "abc", "2015/10/4");
46
+ check1(result, 5, true, 1L, 1d, "true", null);
47
+ check1(result, 6, null, null, null, null, null);
48
+ }
49
+ }
50
+
51
+ private SimpleDateFormat sdf;
52
+ {
53
+ sdf = new SimpleDateFormat("yyyy/MM/dd");
54
+ sdf.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));
55
+ }
56
+
57
+ private void check1(List<OutputRecord> result, int index, Boolean b, Long l, Double d, String s, String t)
58
+ throws ParseException {
59
+ Timestamp timestamp = (t != null) ? Timestamp.ofEpochMilli(sdf.parse(t).getTime()) : null;
60
+
61
+ OutputRecord r = result.get(index);
62
+ // System.out.println(r);
63
+ assertThat(r.getAsBoolean("boolean"), is(b));
64
+ assertThat(r.getAsLong("long"), is(l));
65
+ assertThat(r.getAsDouble("double"), is(d));
66
+ assertThat(r.getAsString("string"), is(s));
67
+ assertThat(r.getAsTimestamp("timestamp"), is(timestamp));
68
+ }
69
+
70
+ @Test
71
+ public void testRowNumber() throws ParseException {
72
+ try (EmbulkPluginTester tester = new EmbulkPluginTester()) {
73
+ tester.addParserPlugin(PoiExcelParserPlugin.TYPE, PoiExcelParserPlugin.class);
74
+
75
+ EmbulkTestParserConfig parser = tester.newParserConfig(PoiExcelParserPlugin.TYPE);
76
+ parser.set("sheet", "test1");
77
+ parser.set("skip_header_lines", 1);
78
+ parser.set("cell_error_null", false);
79
+ parser.addColumn("sheet", "string").set("value", "sheet_name");
80
+ parser.addColumn("sheet-n", "long").set("value", "sheet_name");
81
+ parser.addColumn("row", "long").set("value", "row_number");
82
+ parser.addColumn("flag", "boolean");
83
+ parser.addColumn("col-n", "long").set("value", "column_number");
84
+ parser.addColumn("col-s", "string").set("value", "column_number");
85
+
86
+ URL inFile = getClass().getResource("test1.xls");
87
+ List<OutputRecord> result = tester.runParser(inFile, parser);
88
+
89
+ assertThat(result.size(), is(7));
90
+ check4(result, 0, "test1", true);
91
+ check4(result, 1, "test1", false);
92
+ check4(result, 2, "test1", false);
93
+ check4(result, 3, "test1", true);
94
+ check4(result, 4, "test1", true);
95
+ check4(result, 5, "test1", true);
96
+ check4(result, 6, "test1", null);
97
+ }
98
+ }
99
+
100
+ private void check4(List<OutputRecord> result, int index, String sheetName, Boolean b) {
101
+ OutputRecord r = result.get(index);
102
+ // System.out.println(r);
103
+ assertThat(r.getAsString("sheet"), is(sheetName));
104
+ assertThat(r.getAsLong("sheet-n"), is(0L));
105
+ assertThat(r.getAsLong("row"), is((long) (index + 2)));
106
+ assertThat(r.getAsBoolean("flag"), is(b));
107
+ assertThat(r.getAsLong("col-n"), is(1L));
108
+ assertThat(r.getAsString("col-s"), is("A"));
109
+ }
110
+
111
+ @Test
112
+ public void testForumlaReplace() throws ParseException {
113
+ try (EmbulkPluginTester tester = new EmbulkPluginTester()) {
114
+ tester.addParserPlugin(PoiExcelParserPlugin.TYPE, PoiExcelParserPlugin.class);
115
+
116
+ EmbulkTestParserConfig parser = tester.newParserConfig(PoiExcelParserPlugin.TYPE);
117
+ parser.set("sheet", "formula_replace");
118
+
119
+ ConfigSource replace0 = tester.newConfigSource();
120
+ replace0.set("regex", "test1");
121
+ replace0.set("to", "merged_cell");
122
+ ConfigSource replace1 = tester.newConfigSource();
123
+ replace1.set("regex", "B1");
124
+ replace1.set("to", "B${row}");
125
+ parser.set("formula_replace", Arrays.asList(replace0, replace1));
126
+
127
+ parser.addColumn("text", "string");
128
+
129
+ URL inFile = getClass().getResource("test1.xls");
130
+ List<OutputRecord> result = tester.runParser(inFile, parser);
131
+
132
+ assertThat(result.size(), is(2));
133
+ assertThat(result.get(0).getAsString("text"), is("test3-a1"));
134
+ assertThat(result.get(1).getAsString("text"), is("test2-b2"));
135
+ }
136
+ }
137
+
138
+ @Test
139
+ public void testSearchMergedCell_true() throws ParseException {
140
+ try (EmbulkPluginTester tester = new EmbulkPluginTester()) {
141
+ tester.addParserPlugin(PoiExcelParserPlugin.TYPE, PoiExcelParserPlugin.class);
142
+
143
+ EmbulkTestParserConfig parser = tester.newParserConfig(PoiExcelParserPlugin.TYPE);
144
+ parser.set("sheet", "merged_cell");
145
+ parser.addColumn("a", "string");
146
+ parser.addColumn("b", "string");
147
+
148
+ URL inFile = getClass().getResource("test1.xls");
149
+ List<OutputRecord> result = tester.runParser(inFile, parser);
150
+
151
+ assertThat(result.size(), is(4));
152
+ check6(result, 0, "test3-a1", "test3-a1");
153
+ check6(result, 1, "data", "0");
154
+ check6(result, 2, null, null);
155
+ check6(result, 3, null, null);
156
+ }
157
+ }
158
+
159
+ @Test
160
+ public void testSearchMergedCell_false() throws ParseException {
161
+ try (EmbulkPluginTester tester = new EmbulkPluginTester()) {
162
+ tester.addParserPlugin(PoiExcelParserPlugin.TYPE, PoiExcelParserPlugin.class);
163
+
164
+ EmbulkTestParserConfig parser = tester.newParserConfig(PoiExcelParserPlugin.TYPE);
165
+ parser.set("sheet", "merged_cell");
166
+ parser.set("search_merged_cell", false);
167
+ parser.addColumn("a", "string");
168
+ parser.addColumn("b", "string");
169
+
170
+ URL inFile = getClass().getResource("test1.xls");
171
+ List<OutputRecord> result = tester.runParser(inFile, parser);
172
+
173
+ assertThat(result.size(), is(4));
174
+ check6(result, 0, "test3-a1", null);
175
+ check6(result, 1, "data", "0");
176
+ check6(result, 2, null, null);
177
+ check6(result, 3, null, null);
178
+ }
179
+ }
180
+
181
+ private void check6(List<OutputRecord> result, int index, String a, String b) {
182
+ OutputRecord r = result.get(index);
183
+ // System.out.println(r);
184
+ assertThat(r.getAsString("a"), is(a));
185
+ assertThat(r.getAsString("b"), is(b));
186
+ }
187
+ }
@@ -0,0 +1,42 @@
1
+ package org.embulk.parser.poi_excel;
2
+
3
+ import static org.hamcrest.CoreMatchers.is;
4
+ import static org.junit.Assert.assertThat;
5
+
6
+ import java.net.URL;
7
+ import java.text.ParseException;
8
+ import java.util.List;
9
+
10
+ import org.embulk.parser.EmbulkPluginTester;
11
+ import org.embulk.parser.EmbulkTestOutputPlugin.OutputRecord;
12
+ import org.embulk.parser.EmbulkTestParserConfig;
13
+ import org.junit.Test;
14
+
15
+ public class TestPoiExcelParserPlugin_cellComment {
16
+
17
+ @Test
18
+ public void testComment_key() throws ParseException {
19
+ try (EmbulkPluginTester tester = new EmbulkPluginTester()) {
20
+ tester.addParserPlugin(PoiExcelParserPlugin.TYPE, PoiExcelParserPlugin.class);
21
+
22
+ EmbulkTestParserConfig parser = tester.newParserConfig(PoiExcelParserPlugin.TYPE);
23
+ parser.set("sheet", "comment");
24
+ parser.addColumn("author", "string").set("value", "cell_comment.author");
25
+ parser.addColumn("comment", "string").set("value", "cell_comment.string");
26
+
27
+ URL inFile = getClass().getResource("test1.xls");
28
+ List<OutputRecord> result = tester.runParser(inFile, parser);
29
+
30
+ assertThat(result.size(), is(2));
31
+ check1(result, 0, "hishidama", "hishidama:\nmy comment");
32
+ check1(result, 1, null, null);
33
+ }
34
+ }
35
+
36
+ private void check1(List<OutputRecord> result, int index, String author, String comment) {
37
+ OutputRecord record = result.get(index);
38
+ // System.out.println(record);
39
+ assertThat(record.getAsString("comment"), is(comment));
40
+ assertThat(record.getAsString("author"), is(author));
41
+ }
42
+ }
@@ -0,0 +1,125 @@
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.ParseException;
10
+ import java.util.Arrays;
11
+ import java.util.List;
12
+
13
+ import org.embulk.parser.EmbulkPluginTester;
14
+ import org.embulk.parser.EmbulkTestOutputPlugin.OutputRecord;
15
+ import org.embulk.parser.EmbulkTestParserConfig;
16
+ import org.junit.Test;
17
+
18
+ public class TestPoiExcelParserPlugin_cellFont {
19
+
20
+ @Test
21
+ public void testFont_key() throws ParseException {
22
+ try (EmbulkPluginTester tester = new EmbulkPluginTester()) {
23
+ tester.addParserPlugin(PoiExcelParserPlugin.TYPE, PoiExcelParserPlugin.class);
24
+
25
+ EmbulkTestParserConfig parser = tester.newParserConfig(PoiExcelParserPlugin.TYPE);
26
+ parser.set("sheet", "style");
27
+ parser.addColumn("color-text", "string");
28
+ parser.addColumn("font-color", "long").set("column_number", "C").set("value", "cell_font.color");
29
+ parser.addColumn("font-bold", "boolean").set("value", "cell_font.bold");
30
+
31
+ URL inFile = getClass().getResource("test1.xls");
32
+ List<OutputRecord> result = tester.runParser(inFile, parser);
33
+
34
+ assertThat(result.size(), is(5));
35
+ check1(result, 0, "red", null, false);
36
+ check1(result, 1, "green", 0xff0000L, true);
37
+ check1(result, 2, "blue", null, null);
38
+ check1(result, 3, "white", null, null);
39
+ check1(result, 4, "black", null, null);
40
+ }
41
+ }
42
+
43
+ private void check1(List<OutputRecord> result, int index, String colorText, Long fontColor, Boolean fontBold) {
44
+ OutputRecord record = result.get(index);
45
+ // System.out.println(record);
46
+ assertThat(record.getAsString("color-text"), is(colorText));
47
+ assertThat(record.getAsLong("font-color"), is(fontColor));
48
+ assertThat(record.getAsBoolean("font-bold"), is(fontBold));
49
+ }
50
+
51
+ @Test
52
+ public void testFont_all() throws ParseException {
53
+ try (EmbulkPluginTester tester = new EmbulkPluginTester()) {
54
+ tester.addParserPlugin(PoiExcelParserPlugin.TYPE, PoiExcelParserPlugin.class);
55
+
56
+ EmbulkTestParserConfig parser = tester.newParserConfig(PoiExcelParserPlugin.TYPE);
57
+ parser.set("sheet", "style");
58
+ parser.addColumn("color-text", "string");
59
+ parser.addColumn("color-font", "string").set("column_number", "C").set("value", "cell_font");
60
+
61
+ URL inFile = getClass().getResource("test1.xls");
62
+ List<OutputRecord> result = tester.runParser(inFile, parser);
63
+
64
+ assertThat(result.size(), is(5));
65
+ check2(result, 0, "red", null, false);
66
+ check2(result, 1, "green", 0xff0000L, true);
67
+ check2(result, 2, "blue", null, null);
68
+ check2(result, 3, "white", null, null);
69
+ check2(result, 4, "black", null, null);
70
+ }
71
+ }
72
+
73
+ private void check2(List<OutputRecord> result, int index, String colorText, Long fontColor, Boolean fontBold) {
74
+ OutputRecord record = result.get(index);
75
+ // System.out.println(record);
76
+ assertThat(record.getAsString("color-text"), is(colorText));
77
+ String font = record.getAsString("color-font");
78
+ if (fontColor == null && fontBold == null) {
79
+ assertThat(font, is(nullValue()));
80
+ return;
81
+ }
82
+
83
+ if (fontColor == null) {
84
+ if (!font.contains("\"color\":null")) {
85
+ fail(font);
86
+ }
87
+ } else {
88
+ if (!font.contains(String.format("\"color\":\"%06x\"", fontColor))) {
89
+ fail(font);
90
+ }
91
+ }
92
+ if (fontBold == null) {
93
+ if (!font.contains("\"bold\":null")) {
94
+ fail(font);
95
+ }
96
+ } else {
97
+ if (!font.contains(String.format("\"bold\":%b", fontBold))) {
98
+ fail(font);
99
+ }
100
+ }
101
+ }
102
+
103
+ @Test
104
+ public void testFont_keys() throws ParseException {
105
+ try (EmbulkPluginTester tester = new EmbulkPluginTester()) {
106
+ tester.addParserPlugin(PoiExcelParserPlugin.TYPE, PoiExcelParserPlugin.class);
107
+
108
+ EmbulkTestParserConfig parser = tester.newParserConfig(PoiExcelParserPlugin.TYPE);
109
+ parser.set("sheet", "style");
110
+ parser.addColumn("color-text", "string");
111
+ parser.addColumn("color-font", "string").set("column_number", "C").set("value", "cell_font")
112
+ .set("attribute_name", Arrays.asList("color", "bold"));
113
+
114
+ URL inFile = getClass().getResource("test1.xls");
115
+ List<OutputRecord> result = tester.runParser(inFile, parser);
116
+
117
+ assertThat(result.size(), is(5));
118
+ check2(result, 0, "red", null, false);
119
+ check2(result, 1, "green", 0xff0000L, true);
120
+ check2(result, 2, "blue", null, null);
121
+ check2(result, 3, "white", null, null);
122
+ check2(result, 4, "black", null, null);
123
+ }
124
+ }
125
+ }
@@ -0,0 +1,132 @@
1
+ package org.embulk.parser.poi_excel;
2
+
3
+ import static org.hamcrest.CoreMatchers.*;
4
+ import static org.junit.Assert.*;
5
+
6
+ import java.net.URL;
7
+ import java.text.ParseException;
8
+ import java.util.Arrays;
9
+ import java.util.List;
10
+
11
+ import org.apache.poi.ss.usermodel.CellStyle;
12
+ import org.embulk.parser.EmbulkPluginTester;
13
+ import org.embulk.parser.EmbulkTestOutputPlugin.OutputRecord;
14
+ import org.embulk.parser.EmbulkTestParserConfig;
15
+ import org.junit.Test;
16
+
17
+ public class TestPoiExcelParserPlugin_cellStyle {
18
+
19
+ @Test
20
+ public void testStyle_key() throws ParseException {
21
+ try (EmbulkPluginTester tester = new EmbulkPluginTester()) {
22
+ tester.addParserPlugin(PoiExcelParserPlugin.TYPE, PoiExcelParserPlugin.class);
23
+
24
+ EmbulkTestParserConfig parser = tester.newParserConfig(PoiExcelParserPlugin.TYPE);
25
+ parser.set("sheet", "style");
26
+ parser.addColumn("color-text", "string");
27
+ parser.addColumn("color", "string").set("value", "cell_style.fill_foreground_color");
28
+ parser.addColumn("border-text", "string");
29
+ parser.addColumn("border-top", "long").set("value", "cell_style.border_top");
30
+ parser.addColumn("border-bottom", "long").set("value", "cell_style.border_bottom");
31
+ parser.addColumn("border-left", "long").set("value", "cell_style.border_left");
32
+ parser.addColumn("border-right", "long").set("value", "cell_style.border_right");
33
+ parser.addColumn("border-all", "long").set("value", "cell_style.border");
34
+
35
+ URL inFile = getClass().getResource("test1.xls");
36
+ List<OutputRecord> result = tester.runParser(inFile, parser);
37
+
38
+ assertThat(result.size(), is(5));
39
+ check1(result, 0, "red", 255, 0, 0, "top", CellStyle.BORDER_THIN, 0, 0, 0);
40
+ check1(result, 1, "green", 0, 128, 0, null, 0, 0, 0, 0);
41
+ check1(result, 2, "blue", 0, 0, 255, "left", 0, 0, CellStyle.BORDER_THIN, 0);
42
+ check1(result, 3, "white", 255, 255, 255, "right", 0, 0, 0, CellStyle.BORDER_THIN);
43
+ check1(result, 4, "black", 0, 0, 0, "bottom", 0, CellStyle.BORDER_MEDIUM, 0, 0);
44
+ }
45
+ }
46
+
47
+ private void check1(List<OutputRecord> result, int index, String colorText, int r, int g, int b, String borderText,
48
+ long top, long bottom, long left, long right) {
49
+ OutputRecord record = result.get(index);
50
+ // System.out.println(record);
51
+ assertThat(record.getAsString("color-text"), is(colorText));
52
+ assertThat(record.getAsString("color"), is(String.format("%02x%02x%02x", r, g, b)));
53
+ assertThat(record.getAsString("border-text"), is(borderText));
54
+ assertThat(record.getAsLong("border-top"), is(top));
55
+ assertThat(record.getAsLong("border-bottom"), is(bottom));
56
+ assertThat(record.getAsLong("border-left"), is(left));
57
+ assertThat(record.getAsLong("border-right"), is(right));
58
+ assertThat(record.getAsLong("border-all"), is(top << 24 | bottom << 16 | left << 8 | right));
59
+ }
60
+
61
+ @Test
62
+ public void testStyle_all() throws ParseException {
63
+ try (EmbulkPluginTester tester = new EmbulkPluginTester()) {
64
+ tester.addParserPlugin(PoiExcelParserPlugin.TYPE, PoiExcelParserPlugin.class);
65
+
66
+ EmbulkTestParserConfig parser = tester.newParserConfig(PoiExcelParserPlugin.TYPE);
67
+ parser.set("sheet", "style");
68
+ parser.addColumn("color-text", "string");
69
+ parser.addColumn("color-style", "string").set("column_number", "A").set("value", "cell_style");
70
+ parser.addColumn("border-style", "string").set("column_number", "B").set("value", "cell_style");
71
+
72
+ URL inFile = getClass().getResource("test1.xls");
73
+ List<OutputRecord> result = tester.runParser(inFile, parser);
74
+
75
+ assertThat(result.size(), is(5));
76
+ check2(result, 0, "red", 255, 0, 0, "top", CellStyle.BORDER_THIN, 0, 0, 0);
77
+ check2(result, 1, "green", 0, 128, 0, null, 0, 0, 0, 0);
78
+ check2(result, 2, "blue", 0, 0, 255, "left", 0, 0, CellStyle.BORDER_THIN, 0);
79
+ check2(result, 3, "white", 255, 255, 255, "right", 0, 0, 0, CellStyle.BORDER_THIN);
80
+ check2(result, 4, "black", 0, 0, 0, "bottom", 0, CellStyle.BORDER_MEDIUM, 0, 0);
81
+ }
82
+ }
83
+
84
+ private void check2(List<OutputRecord> result, int index, String colorText, int r, int g, int b, String borderText,
85
+ long top, long bottom, long left, long right) {
86
+ OutputRecord record = result.get(index);
87
+ // System.out.println(record);
88
+ assertThat(record.getAsString("color-text"), is(colorText));
89
+ String color = record.getAsString("color-style");
90
+ if (!color.contains(String.format("\"fill_foreground_color\":\"%02x%02x%02x\"", r, g, b))) {
91
+ fail(color);
92
+ }
93
+ String border = record.getAsString("border-style");
94
+ if (!border.contains(String.format("\"border_top\":%d", top))) {
95
+ fail(border);
96
+ }
97
+ if (!border.contains(String.format("\"border_bottom\":%d", bottom))) {
98
+ fail(border);
99
+ }
100
+ if (!border.contains(String.format("\"border_left\":%d", left))) {
101
+ fail(border);
102
+ }
103
+ if (!border.contains(String.format("\"border_right\":%d", right))) {
104
+ fail(border);
105
+ }
106
+ }
107
+
108
+ @Test
109
+ public void testStyle_keys() throws ParseException {
110
+ try (EmbulkPluginTester tester = new EmbulkPluginTester()) {
111
+ tester.addParserPlugin(PoiExcelParserPlugin.TYPE, PoiExcelParserPlugin.class);
112
+
113
+ EmbulkTestParserConfig parser = tester.newParserConfig(PoiExcelParserPlugin.TYPE);
114
+ parser.set("sheet", "style");
115
+ parser.addColumn("color-text", "string");
116
+ parser.addColumn("color-style", "string").set("column_number", "A").set("value", "cell_style")
117
+ .set("cell_style_name", Arrays.asList("fill_foreground_color"));
118
+ parser.addColumn("border-style", "string").set("column_number", "B").set("value", "cell_style")
119
+ .set("attribute_name", Arrays.asList("border_top", "border_bottom", "border_left", "border_right"));
120
+
121
+ URL inFile = getClass().getResource("test1.xls");
122
+ List<OutputRecord> result = tester.runParser(inFile, parser);
123
+
124
+ assertThat(result.size(), is(5));
125
+ check2(result, 0, "red", 255, 0, 0, "top", CellStyle.BORDER_THIN, 0, 0, 0);
126
+ check2(result, 1, "green", 0, 128, 0, null, 0, 0, 0, 0);
127
+ check2(result, 2, "blue", 0, 0, 255, "left", 0, 0, CellStyle.BORDER_THIN, 0);
128
+ check2(result, 3, "white", 255, 255, 255, "right", 0, 0, 0, CellStyle.BORDER_THIN);
129
+ check2(result, 4, "black", 0, 0, 0, "bottom", 0, CellStyle.BORDER_MEDIUM, 0, 0);
130
+ }
131
+ }
132
+ }
@@ -0,0 +1,188 @@
1
+ package org.embulk.parser.poi_excel;
2
+
3
+ import static org.hamcrest.CoreMatchers.is;
4
+ import static org.junit.Assert.assertThat;
5
+
6
+ import java.net.URL;
7
+ import java.text.ParseException;
8
+ import java.util.List;
9
+
10
+ import org.apache.poi.ss.usermodel.FormulaError;
11
+ import org.embulk.parser.EmbulkPluginTester;
12
+ import org.embulk.parser.EmbulkTestOutputPlugin.OutputRecord;
13
+ import org.embulk.parser.EmbulkTestParserConfig;
14
+ import org.junit.Test;
15
+
16
+ public class TestPoiExcelParserPlugin_columnNumber {
17
+
18
+ @Test
19
+ public void testColumnNumber_string() throws Exception {
20
+ try (EmbulkPluginTester tester = new EmbulkPluginTester()) {
21
+ tester.addParserPlugin(PoiExcelParserPlugin.TYPE, PoiExcelParserPlugin.class);
22
+
23
+ EmbulkTestParserConfig parser = tester.newParserConfig(PoiExcelParserPlugin.TYPE);
24
+ parser.set("sheet", "test1");
25
+ parser.set("skip_header_lines", 1);
26
+ parser.set("cell_error_null", false);
27
+ parser.addColumn("text", "string").set("column_number", "D");
28
+
29
+ URL inFile = getClass().getResource("test1.xls");
30
+ List<OutputRecord> result = tester.runParser(inFile, parser);
31
+
32
+ assertThat(result.size(), is(7));
33
+ assertThat(result.get(0).getAsString("text"), is("abc"));
34
+ assertThat(result.get(1).getAsString("text"), is("def"));
35
+ assertThat(result.get(2).getAsString("text"), is("456"));
36
+ assertThat(result.get(3).getAsString("text"), is("abc"));
37
+ assertThat(result.get(4).getAsString("text"), is("abc"));
38
+ assertThat(result.get(5).getAsString("text"), is("true"));
39
+ assertThat(result.get(6).getAsString("text"), is("#DIV/0!"));
40
+ }
41
+ }
42
+
43
+ @Test
44
+ public void testColumnNumber_int() throws Exception {
45
+ try (EmbulkPluginTester tester = new EmbulkPluginTester()) {
46
+ tester.addParserPlugin(PoiExcelParserPlugin.TYPE, PoiExcelParserPlugin.class);
47
+
48
+ EmbulkTestParserConfig parser = tester.newParserConfig(PoiExcelParserPlugin.TYPE);
49
+ parser.set("sheet", "test1");
50
+ parser.set("skip_header_lines", 1);
51
+ parser.set("cell_error_null", false);
52
+ parser.addColumn("long", "long").set("column_number", 2);
53
+ parser.addColumn("double", "double");
54
+
55
+ URL inFile = getClass().getResource("test1.xls");
56
+ List<OutputRecord> result = tester.runParser(inFile, parser);
57
+
58
+ assertThat(result.size(), is(7));
59
+ check_int(result, 0, 123L, 123.4d);
60
+ check_int(result, 1, 456L, 456.7d);
61
+ check_int(result, 2, 123L, 123d);
62
+ check_int(result, 3, 123L, 123.4d);
63
+ check_int(result, 4, 123L, 123.4d);
64
+ check_int(result, 5, 1L, 1d);
65
+ check_int(result, 6, (long) FormulaError.DIV0.getCode(), (double) FormulaError.DIV0.getCode());
66
+ }
67
+ }
68
+
69
+ private void check_int(List<OutputRecord> result, int index, Long l, Double d) throws ParseException {
70
+ OutputRecord r = result.get(index);
71
+ // System.out.println(r);
72
+ assertThat(r.getAsLong("long"), is(l));
73
+ assertThat(r.getAsDouble("double"), is(d));
74
+ }
75
+
76
+ @Test
77
+ public void testColumnNumber_move() throws Exception {
78
+ try (EmbulkPluginTester tester = new EmbulkPluginTester()) {
79
+ tester.addParserPlugin(PoiExcelParserPlugin.TYPE, PoiExcelParserPlugin.class);
80
+
81
+ EmbulkTestParserConfig parser = tester.newParserConfig(PoiExcelParserPlugin.TYPE);
82
+ parser.set("sheet", "test1");
83
+ parser.set("skip_header_lines", 1);
84
+ parser.addColumn("long1", "long").set("column_number", 2);
85
+ parser.addColumn("long2", "long").set("column_number", "=");
86
+ parser.addColumn("double1", "double").set("column_number", "+");
87
+ parser.addColumn("double2", "double").set("column_number", "=");
88
+ parser.addColumn("long3", "long").set("column_number", "-");
89
+
90
+ URL inFile = getClass().getResource("test1.xls");
91
+ List<OutputRecord> result = tester.runParser(inFile, parser);
92
+
93
+ assertThat(result.size(), is(7));
94
+ check_move(result, 0, 123L, 123.4d);
95
+ check_move(result, 1, 456L, 456.7d);
96
+ check_move(result, 2, 123L, 123d);
97
+ check_move(result, 3, 123L, 123.4d);
98
+ check_move(result, 4, 123L, 123.4d);
99
+ check_move(result, 5, 1L, 1d);
100
+ check_move(result, 6, null, null);
101
+ }
102
+ }
103
+
104
+ private void check_move(List<OutputRecord> result, int index, Long l, Double d) throws ParseException {
105
+ OutputRecord r = result.get(index);
106
+ // System.out.println(r);
107
+ assertThat(r.getAsLong("long1"), is(l));
108
+ assertThat(r.getAsLong("long2"), is(l));
109
+ assertThat(r.getAsLong("long3"), is(l));
110
+ assertThat(r.getAsDouble("double1"), is(d));
111
+ assertThat(r.getAsDouble("double2"), is(d));
112
+ }
113
+
114
+ @Test
115
+ public void testColumnNumber_move2() throws Exception {
116
+ try (EmbulkPluginTester tester = new EmbulkPluginTester()) {
117
+ tester.addParserPlugin(PoiExcelParserPlugin.TYPE, PoiExcelParserPlugin.class);
118
+
119
+ EmbulkTestParserConfig parser = tester.newParserConfig(PoiExcelParserPlugin.TYPE);
120
+ parser.set("sheet", "test1");
121
+ parser.set("skip_header_lines", 1);
122
+ parser.addColumn("long1", "long").set("column_number", 2);
123
+ parser.addColumn("string1", "string").set("column_number", "+2");
124
+ parser.addColumn("long2", "long").set("column_number", "=long1");
125
+ parser.addColumn("string2", "string").set("column_number", "=string1");
126
+ parser.addColumn("long3", "long").set("column_number", "-2");
127
+
128
+ URL inFile = getClass().getResource("test1.xls");
129
+ List<OutputRecord> result = tester.runParser(inFile, parser);
130
+
131
+ assertThat(result.size(), is(7));
132
+ check_move2(result, 0, 123L, "abc");
133
+ check_move2(result, 1, 456L, "def");
134
+ check_move2(result, 2, 123L, "456");
135
+ check_move2(result, 3, 123L, "abc");
136
+ check_move2(result, 4, 123L, "abc");
137
+ check_move2(result, 5, 1L, "true");
138
+ check_move2(result, 6, null, null);
139
+ }
140
+ }
141
+
142
+ private void check_move2(List<OutputRecord> result, int index, Long l, String s) throws ParseException {
143
+ OutputRecord r = result.get(index);
144
+ // System.out.println(r);
145
+ assertThat(r.getAsLong("long1"), is(l));
146
+ assertThat(r.getAsLong("long2"), is(l));
147
+ assertThat(r.getAsLong("long3"), is(l));
148
+ assertThat(r.getAsString("string1"), is(s));
149
+ assertThat(r.getAsString("string2"), is(s));
150
+ }
151
+
152
+ @Test
153
+ public void testColumnNumber_moveName() throws Exception {
154
+ try (EmbulkPluginTester tester = new EmbulkPluginTester()) {
155
+ tester.addParserPlugin(PoiExcelParserPlugin.TYPE, PoiExcelParserPlugin.class);
156
+
157
+ EmbulkTestParserConfig parser = tester.newParserConfig(PoiExcelParserPlugin.TYPE);
158
+ parser.set("sheet", "test1");
159
+ parser.set("skip_header_lines", 1);
160
+ parser.addColumn("long1", "long").set("column_number", 2);
161
+ parser.addColumn("double1", "double").set("column_number", "+long1");
162
+ parser.addColumn("long2", "long").set("column_number", "=long1");
163
+ parser.addColumn("boolean1", "boolean").set("column_number", "-long1");
164
+
165
+ URL inFile = getClass().getResource("test1.xls");
166
+ List<OutputRecord> result = tester.runParser(inFile, parser);
167
+
168
+ assertThat(result.size(), is(7));
169
+ check_moveName(result, 0, true, 123L, 123.4d);
170
+ check_moveName(result, 1, false, 456L, 456.7d);
171
+ check_moveName(result, 2, false, 123L, 123d);
172
+ check_moveName(result, 3, true, 123L, 123.4d);
173
+ check_moveName(result, 4, true, 123L, 123.4d);
174
+ check_moveName(result, 5, true, 1L, 1d);
175
+ check_moveName(result, 6, null, null, null);
176
+ }
177
+ }
178
+
179
+ private void check_moveName(List<OutputRecord> result, int index, Boolean b, Long l, Double d)
180
+ throws ParseException {
181
+ OutputRecord r = result.get(index);
182
+ // System.out.println(r);
183
+ assertThat(r.getAsLong("long1"), is(l));
184
+ assertThat(r.getAsLong("long2"), is(l));
185
+ assertThat(r.getAsDouble("double1"), is(d));
186
+ assertThat(r.getAsBoolean("boolean1"), is(b));
187
+ }
188
+ }