|
26 | 26 | import org.embulk.config.ConfigException; |
27 | 27 | import org.embulk.config.ConfigSource; |
28 | 28 | import org.embulk.util.config.ConfigMapperFactory; |
| 29 | +import org.embulk.util.csv.CsvTokenizer; |
29 | 30 | import org.embulk.util.text.Newline; |
30 | 31 | import org.junit.Rule; |
31 | 32 | import org.junit.Test; |
@@ -86,4 +87,138 @@ public void checkLoadConfig() { |
86 | 87 | assertEquals(Optional.of(new CsvParserPlugin.QuoteCharacter('\\')), task.getQuoteChar()); |
87 | 88 | assertEquals(true, task.getAllowOptionalColumns()); |
88 | 89 | } |
| 90 | + |
| 91 | + @SuppressWarnings("deprecation") |
| 92 | + @Test |
| 93 | + public void testCsvTokenizerQuoteBackslash() { |
| 94 | + final ConfigSource config = CONFIG_MAPPER_FACTORY.newConfigSource() |
| 95 | + .set("charset", "utf-16") |
| 96 | + .set("newline", "LF") |
| 97 | + .set("header_line", true) |
| 98 | + .set("delimiter", "\t") |
| 99 | + .set("quote", "\\") |
| 100 | + .set("columns", ImmutableList.of(ImmutableMap.of("name", "id", "type", "string"))); |
| 101 | + final CsvParserPlugin.PluginTask task = |
| 102 | + CONFIG_MAPPER_FACTORY.createConfigMapper().map(config, CsvParserPlugin.PluginTask.class); |
| 103 | + |
| 104 | + final CsvTokenizer.Builder builder = CsvParserPlugin.buildCsvTokenizerBuilderForTesting(task); |
| 105 | + assertEquals('\\', builder.peekQuote()); |
| 106 | + } |
| 107 | + |
| 108 | + @SuppressWarnings("deprecation") |
| 109 | + @Test |
| 110 | + public void testCsvTokenizerQuoteQuotation() { |
| 111 | + final ConfigSource config = CONFIG_MAPPER_FACTORY.newConfigSource() |
| 112 | + .set("charset", "utf-16") |
| 113 | + .set("newline", "LF") |
| 114 | + .set("header_line", true) |
| 115 | + .set("delimiter", "\t") |
| 116 | + .set("quote", "\"") |
| 117 | + .set("columns", ImmutableList.of(ImmutableMap.of("name", "id", "type", "string"))); |
| 118 | + final CsvParserPlugin.PluginTask task = |
| 119 | + CONFIG_MAPPER_FACTORY.createConfigMapper().map(config, CsvParserPlugin.PluginTask.class); |
| 120 | + |
| 121 | + final CsvTokenizer.Builder builder = CsvParserPlugin.buildCsvTokenizerBuilderForTesting(task); |
| 122 | + assertEquals('\"', builder.peekQuote()); |
| 123 | + } |
| 124 | + |
| 125 | + @SuppressWarnings("deprecation") |
| 126 | + @Test |
| 127 | + public void testCsvTokenizerQuoteUnspecified() { |
| 128 | + final ConfigSource config = CONFIG_MAPPER_FACTORY.newConfigSource() |
| 129 | + .set("charset", "utf-16") |
| 130 | + .set("newline", "LF") |
| 131 | + .set("header_line", true) |
| 132 | + .set("delimiter", "\t") |
| 133 | + .set("columns", ImmutableList.of(ImmutableMap.of("name", "id", "type", "string"))); |
| 134 | + final CsvParserPlugin.PluginTask task = |
| 135 | + CONFIG_MAPPER_FACTORY.createConfigMapper().map(config, CsvParserPlugin.PluginTask.class); |
| 136 | + |
| 137 | + final CsvTokenizer.Builder builder = CsvParserPlugin.buildCsvTokenizerBuilderForTesting(task); |
| 138 | + assertEquals('\"', builder.peekQuote()); |
| 139 | + } |
| 140 | + |
| 141 | + @SuppressWarnings("deprecation") |
| 142 | + @Test |
| 143 | + public void testCsvTokenizerQuoteNull() { |
| 144 | + final ConfigSource config = CONFIG_MAPPER_FACTORY.newConfigSource() |
| 145 | + .set("charset", "utf-16") |
| 146 | + .set("newline", "LF") |
| 147 | + .set("header_line", true) |
| 148 | + .set("delimiter", "\t") |
| 149 | + .setNested("quote", null) // #setNested is needed to set null |
| 150 | + .set("columns", ImmutableList.of(ImmutableMap.of("name", "id", "type", "string"))); |
| 151 | + final CsvParserPlugin.PluginTask task = |
| 152 | + CONFIG_MAPPER_FACTORY.createConfigMapper().map(config, CsvParserPlugin.PluginTask.class); |
| 153 | + |
| 154 | + final CsvTokenizer.Builder builder = CsvParserPlugin.buildCsvTokenizerBuilderForTesting(task); |
| 155 | + assertEquals(CsvTokenizer.NO_QUOTE, builder.peekQuote()); |
| 156 | + } |
| 157 | + |
| 158 | + @SuppressWarnings("deprecation") |
| 159 | + @Test |
| 160 | + public void testCsvTokenizerEscapeBackslash() { |
| 161 | + final ConfigSource config = CONFIG_MAPPER_FACTORY.newConfigSource() |
| 162 | + .set("charset", "utf-16") |
| 163 | + .set("newline", "LF") |
| 164 | + .set("header_line", true) |
| 165 | + .set("delimiter", "\t") |
| 166 | + .set("escape", "\\") |
| 167 | + .set("columns", ImmutableList.of(ImmutableMap.of("name", "id", "type", "string"))); |
| 168 | + final CsvParserPlugin.PluginTask task = |
| 169 | + CONFIG_MAPPER_FACTORY.createConfigMapper().map(config, CsvParserPlugin.PluginTask.class); |
| 170 | + |
| 171 | + final CsvTokenizer.Builder builder = CsvParserPlugin.buildCsvTokenizerBuilderForTesting(task); |
| 172 | + assertEquals('\\', builder.peekEscape()); |
| 173 | + } |
| 174 | + |
| 175 | + @SuppressWarnings("deprecation") |
| 176 | + @Test |
| 177 | + public void testCsvTokenizerEscapeSlash() { |
| 178 | + final ConfigSource config = CONFIG_MAPPER_FACTORY.newConfigSource() |
| 179 | + .set("charset", "utf-16") |
| 180 | + .set("newline", "LF") |
| 181 | + .set("header_line", true) |
| 182 | + .set("delimiter", "\t") |
| 183 | + .set("escape", "/") |
| 184 | + .set("columns", ImmutableList.of(ImmutableMap.of("name", "id", "type", "string"))); |
| 185 | + final CsvParserPlugin.PluginTask task = |
| 186 | + CONFIG_MAPPER_FACTORY.createConfigMapper().map(config, CsvParserPlugin.PluginTask.class); |
| 187 | + |
| 188 | + final CsvTokenizer.Builder builder = CsvParserPlugin.buildCsvTokenizerBuilderForTesting(task); |
| 189 | + assertEquals('/', builder.peekEscape()); |
| 190 | + } |
| 191 | + |
| 192 | + @SuppressWarnings("deprecation") |
| 193 | + @Test |
| 194 | + public void testCsvTokenizerEscapeUnspecified() { |
| 195 | + final ConfigSource config = CONFIG_MAPPER_FACTORY.newConfigSource() |
| 196 | + .set("charset", "utf-16") |
| 197 | + .set("newline", "LF") |
| 198 | + .set("header_line", true) |
| 199 | + .set("delimiter", "\t") |
| 200 | + .set("columns", ImmutableList.of(ImmutableMap.of("name", "id", "type", "string"))); |
| 201 | + final CsvParserPlugin.PluginTask task = |
| 202 | + CONFIG_MAPPER_FACTORY.createConfigMapper().map(config, CsvParserPlugin.PluginTask.class); |
| 203 | + |
| 204 | + final CsvTokenizer.Builder builder = CsvParserPlugin.buildCsvTokenizerBuilderForTesting(task); |
| 205 | + assertEquals('\\', builder.peekEscape()); |
| 206 | + } |
| 207 | + |
| 208 | + @SuppressWarnings("deprecation") |
| 209 | + @Test |
| 210 | + public void testCsvTokenizerEscapeNull() { |
| 211 | + final ConfigSource config = CONFIG_MAPPER_FACTORY.newConfigSource() |
| 212 | + .set("charset", "utf-16") |
| 213 | + .set("newline", "LF") |
| 214 | + .set("header_line", true) |
| 215 | + .set("delimiter", "\t") |
| 216 | + .setNested("escape", null) // #setNested is needed to set null |
| 217 | + .set("columns", ImmutableList.of(ImmutableMap.of("name", "id", "type", "string"))); |
| 218 | + final CsvParserPlugin.PluginTask task = |
| 219 | + CONFIG_MAPPER_FACTORY.createConfigMapper().map(config, CsvParserPlugin.PluginTask.class); |
| 220 | + |
| 221 | + final CsvTokenizer.Builder builder = CsvParserPlugin.buildCsvTokenizerBuilderForTesting(task); |
| 222 | + assertEquals(CsvTokenizer.NO_ESCAPE, builder.peekEscape()); |
| 223 | + } |
89 | 224 | } |
0 commit comments