CsvExtractorFactory implements IExtractorFactory<StreamConnector> { @Override public Extractor createExtractor(StreamConnector connector, ExtractorModel model) { String charsetName = model.getPropertyValue(CHARSET_PROPERTY); Charset charset = charsetName != null ? Charset.forName(charsetName) : defaultCharset; CSVFormat csvFormat = CSVFormat.RFC4180; String delimiter = model.getPropertyValue(DELIMITER_PROPERTY); if (delimiter != null) { if (delimiter.length() != 1) { throw new LmRuntimeException("Invalid delimiter (should be exactly one character): " + delimiter); } csvFormat = csvFormat.withDelimiter(delimiter.charAt(0)); } String readFromString = model.getPropertyValue(READ_FROM_PROPERTY); Integer readFrom = readFromString != null ? Integer.valueOf(readFromString) : null; return new CsvExtractor(connector, model.getAttributes(), charset, csvFormat, readFrom); } CsvExtractorFactory(); @Override String getExtractorType(); @Override Class<StreamConnector> getConnectorType(); @Override Extractor createExtractor(StreamConnector connector, ExtractorModel model); static final String DELIMITER_PROPERTY; static final String READ_FROM_PROPERTY; static final String CHARSET_PROPERTY; }
@Test public void testCreateExtractor() { extractorFactory.createExtractor(connectorMock, model); }
@Test public void testBasicIntegration() throws IOException { when(connectorMock.getInputStream(anyMap())) .thenReturn(new ByteArrayInputStream("r1c1,r1c2,r1c3\nr2c1,r2c2,r2c3".getBytes())); RowReader reader = extractorFactory.createExtractor(connectorMock, model).getReader(Collections.emptyMap()); doCheck(reader); }
@Test public void testSetDelimiter() throws IOException { when(connectorMock.getInputStream(anyMap())) .thenReturn(new ByteArrayInputStream("r1c1;r1c2;r1c3\nr2c1;r2c2;r2c3".getBytes())); model.addProperty(CsvExtractorFactory.DELIMITER_PROPERTY, ";"); RowReader reader = extractorFactory.createExtractor(connectorMock, model).getReader(Collections.emptyMap()); doCheck(reader); }
@Test public void testSetReadFrom() throws IOException { when(connectorMock.getInputStream(anyMap())) .thenReturn(new ByteArrayInputStream("k1,k2,k3\nr1c1,r1c2,r1c3\nr2c1,r2c2,r2c3".getBytes())); model.addProperty(CsvExtractorFactory.READ_FROM_PROPERTY, "2"); RowReader reader = extractorFactory.createExtractor(connectorMock, model).getReader(Collections.emptyMap()); doCheck(reader); }