This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch df2 in repository https://gitbox.apache.org/repos/asf/camel.git
commit fd1e67334c0be4dbae1a5488070578193f2cf22d Author: Claus Ibsen <[email protected]> AuthorDate: Mon Aug 18 21:25:15 2025 +0200 CAMEL-22354: dataformats - Align all data formats getter setters to model --- .../univocity/AbstractUniVocityDataFormat.java | 476 +++++++-------------- .../univocity/UniVocityCsvDataFormat.java | 96 +---- .../univocity/UniVocityFixedDataFormat.java | 117 ++--- .../univocity/UniVocityTsvDataFormat.java | 34 +- .../UniVocityCsvDataFormatMarshalTest.java | 20 +- .../univocity/UniVocityCsvDataFormatTest.java | 73 ++-- .../UniVocityCsvDataFormatUnmarshalTest.java | 34 +- .../UniVocityFixedDataFormatMarshalTest.java | 23 +- .../univocity/UniVocityFixedDataFormatTest.java | 140 +++--- .../UniVocityFixedDataFormatUnmarshalTest.java | 43 +- .../UniVocityTsvDataFormatMarshalTest.java | 12 +- .../univocity/UniVocityTsvDataFormatTest.java | 61 ++- .../UniVocityTsvDataFormatUnmarshalTest.java | 35 +- .../UniVocityAbstractDataFormatReifier.java | 20 +- .../UniVocityFixedWidthDataFormatReifier.java | 12 +- 15 files changed, 441 insertions(+), 755 deletions(-) diff --git a/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/AbstractUniVocityDataFormat.java b/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/AbstractUniVocityDataFormat.java index fbc9328d35e..0e05b2f8ef9 100644 --- a/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/AbstractUniVocityDataFormat.java +++ b/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/AbstractUniVocityDataFormat.java @@ -58,7 +58,7 @@ public abstract class AbstractUniVocityDataFormat< protected Boolean ignoreTrailingWhitespaces; protected Boolean ignoreLeadingWhitespaces; protected boolean headersDisabled; - protected String[] headers; + protected String headers; protected Boolean headerExtractionEnabled; protected Integer numberOfRecordsToRead; protected String emptyValue; @@ -75,16 +75,13 @@ public abstract class AbstractUniVocityDataFormat< private volatile Unmarshaller<P> unmarshaller; private final HeaderRowProcessor headerRowProcessor = new HeaderRowProcessor(); - /** - * {@inheritDoc} - */ @Override public void marshal(Exchange exchange, Object body, OutputStream stream) throws Exception { if (writerSettings == null) { writerSettings = createAndConfigureWriterSettings(); } if (marshaller == null) { - marshaller = new Marshaller<>(headers, headers == null); + marshaller = new Marshaller<>(headersAsArray(), headers == null); } try (Writer writer = new OutputStreamWriter(stream, getCharsetName(exchange))) { @@ -92,9 +89,6 @@ public abstract class AbstractUniVocityDataFormat< } } - /** - * {@inheritDoc} - */ @Override public Object unmarshal(Exchange exchange, InputStream stream) throws Exception { if (parserSettings == null) { @@ -110,310 +104,12 @@ public abstract class AbstractUniVocityDataFormat< return unmarshaller.unmarshal(reader, parser, headerRowProcessor); } - /** - * Gets the String representation of a null value. If {@code null} then the default settings value is used. - * - * @return the String representation of a null value - * @see com.univocity.parsers.common.CommonSettings#getNullValue() - */ - public String getNullValue() { - return nullValue; - } - - /** - * Sets the String representation of a null value. If {@code null} then the default settings value is used. - * - * @param nullValue the String representation of a null value - * @return current data format instance, fluent API - * @see com.univocity.parsers.common.CommonSettings#setNullValue(String) - */ - public DF setNullValue(String nullValue) { - this.nullValue = nullValue; - return self(); - } - - /** - * Gets whether empty lines should be ignored. If {@code null} then the default settings value is used. - * - * @return whether empty lines should be ignored - * @see com.univocity.parsers.common.CommonSettings#getSkipEmptyLines() - */ - public Boolean getSkipEmptyLines() { - return skipEmptyLines; - } - - /** - * Sets whether empty lines should be ignored. If {@code null} then the default settings value is used. - * - * @param skipEmptyLines whether empty lines should be ignored - * @return current data format instance, fluent API - * @see com.univocity.parsers.common.CommonSettings#setSkipEmptyLines(boolean) - */ - public DF setSkipEmptyLines(Boolean skipEmptyLines) { - this.skipEmptyLines = skipEmptyLines; - return self(); - } - - /** - * Gets whether trailing whitespaces should be ignored. If {@code null} then the default settings value is used. - * - * @return whethertrailing whitespaces should be ignored - * @see com.univocity.parsers.common.CommonSettings#getIgnoreTrailingWhitespaces() - */ - public Boolean getIgnoreTrailingWhitespaces() { - return ignoreTrailingWhitespaces; - } - - /** - * Sets whether trailing whitespaces should be ignored. If {@code null} then the default settings value is used. - * - * @param ignoreTrailingWhitespaces whether trailing whitespaces should be ignored - * @return current data format instance, fluent API - * @see com.univocity.parsers.common.CommonSettings#setIgnoreTrailingWhitespaces(boolean) - */ - public DF setIgnoreTrailingWhitespaces(Boolean ignoreTrailingWhitespaces) { - this.ignoreTrailingWhitespaces = ignoreTrailingWhitespaces; - return self(); - } - - /** - * Gets whetherleading whitespaces should be ignored. If {@code null} then the default settings value is used. - * - * @return whetherleading whitespaces should be ignored - * @see com.univocity.parsers.common.CommonSettings#getIgnoreLeadingWhitespaces() - */ - public Boolean getIgnoreLeadingWhitespaces() { - return ignoreLeadingWhitespaces; - } - - /** - * Sets whetherleading whitespaces should be ignored. If {@code null} then the default settings value is used. - * - * @param ignoreLeadingWhitespaces whetherleading whitespaces should be ignored - * @return current data format instance, fluent API - * @see com.univocity.parsers.common.CommonSettings#setIgnoreLeadingWhitespaces(boolean) - */ - public DF setIgnoreLeadingWhitespaces(Boolean ignoreLeadingWhitespaces) { - this.ignoreLeadingWhitespaces = ignoreLeadingWhitespaces; - return self(); - } - - /** - * Gets whetherheaders are disabled. If {@code true} then it passes {@code null} to - * {@link com.univocity.parsers.common.CommonSettings#setHeaders(String...)} in order to disabled them. - * - * @return whetherheaders are disabled - * @see com.univocity.parsers.common.CommonSettings#getHeaders() - */ - public boolean isHeadersDisabled() { - return headersDisabled; - } - - /** - * Sets whetherheaders are disabled. If {@code true} then it passes {@code null} to - * {@link com.univocity.parsers.common.CommonSettings#setHeaders(String...)} in order to disabled them. - * - * @param headersDisabled whetherheaders are disabled - * @return current data format instance, fluent API - * @see com.univocity.parsers.common.CommonSettings#setHeaders(String...) - */ - public DF setHeadersDisabled(boolean headersDisabled) { - this.headersDisabled = headersDisabled; - return self(); - } - - /** - * Gets the headers. If {@code null} then the default settings value is used. - * - * @return the headers - * @see com.univocity.parsers.common.CommonSettings#getHeaders() - */ - public String[] getHeaders() { - return headers; - } - - /** - * Sets the headers. If {@code null} then the default settings value is used. - * - * @param headers the headers - * @return current data format instance, fluent API - * @see com.univocity.parsers.common.CommonSettings#setHeaders(String...) - */ - public DF setHeaders(String[] headers) { - this.headers = headers; - return self(); - } - - /** - * Gets whetherthe header extraction is enabled. If {@code null} then the default settings value is used. - * - * @return whetherthe header extraction is enabled - * @see com.univocity.parsers.common.CommonParserSettings#isHeaderExtractionEnabled() - */ - public Boolean getHeaderExtractionEnabled() { - return headerExtractionEnabled; - } - - /** - * Sets whetherthe header extraction is enabled. If {@code null} then the default settings value is used. - * - * @param headerExtractionEnabled whetherthe header extraction is enabled - * @return current data format instance, fluent API - * @see com.univocity.parsers.common.CommonParserSettings#setHeaderExtractionEnabled(boolean) - */ - public DF setHeaderExtractionEnabled(Boolean headerExtractionEnabled) { - this.headerExtractionEnabled = headerExtractionEnabled; - return self(); - } - - /** - * Gets the number of records to read. If {@code null} then the default settings value is used. - * - * @return the number of records to read - * @see com.univocity.parsers.common.CommonParserSettings#getNumberOfRecordsToRead() - */ - public Integer getNumberOfRecordsToRead() { - return numberOfRecordsToRead; - } - - /** - * Sets the number of records to read. If {@code null} then the default settings value is used. - * - * @param numberOfRecordsToRead the number of records to read - * @return current data format instance, fluent API - * @see com.univocity.parsers.common.CommonParserSettings#setNumberOfRecordsToRead(long) - */ - public DF setNumberOfRecordsToRead(Integer numberOfRecordsToRead) { - this.numberOfRecordsToRead = numberOfRecordsToRead; - return self(); - } - - /** - * Gets the String representation of an empty value. If {@code null} then the default settings value is used. - * - * @return the String representation of an empty value - * @see com.univocity.parsers.common.CommonWriterSettings#getEmptyValue() - */ - public String getEmptyValue() { - return emptyValue; - } - - /** - * Sets the String representation of an empty value. If {@code null} then the default settings value is used. - * - * @param emptyValue the String representation of an empty value - * @return current data format instance, fluent API - * @see com.univocity.parsers.common.CommonWriterSettings#setEmptyValue(String) - */ - public DF setEmptyValue(String emptyValue) { - this.emptyValue = emptyValue; - return self(); - } - - /** - * Gets the line separator. If {@code null} then the default format value is used. - * - * @return the line separator - * @see com.univocity.parsers.common.Format#getLineSeparatorString() - */ - public String getLineSeparator() { - return lineSeparator; - } - - /** - * Sets the line separator. If {@code null} then the default format value is used. - * - * @param lineSeparator the line separator - * @return current data format instance, fluent API - * @see Format#setLineSeparator(String) - */ - public DF setLineSeparator(String lineSeparator) { - this.lineSeparator = lineSeparator; - return self(); - } - - /** - * Gets the normalized line separator. If {@code null} then the default format value is used. - * - * @return the normalized line separator - * @see com.univocity.parsers.common.Format#getNormalizedNewline() - */ - public Character getNormalizedLineSeparator() { - return normalizedLineSeparator; - } - - /** - * Sets the normalized line separator. If {@code null} then the default format value is used. - * - * @param normalizedLineSeparator the normalized line separator - * @return current data format instance, fluent API - * @see Format#setNormalizedNewline(char) - */ - public DF setNormalizedLineSeparator(Character normalizedLineSeparator) { - this.normalizedLineSeparator = normalizedLineSeparator; - return self(); - } - - /** - * Gets the comment symbol. If {@code null} then the default format value is used. - * - * @return the comment symbol - * @see com.univocity.parsers.common.Format#getComment() - */ - public Character getComment() { - return comment; - } - - /** - * Gets the comment symbol. If {@code null} then the default format value is used. - * - * @param comment the comment symbol - * @return current data format instance, fluent API - * @see com.univocity.parsers.common.Format#setComment(char) - */ - public DF setComment(Character comment) { - this.comment = comment; - return self(); - } - - /** - * Gets whetherthe unmarshalling should read lines lazily. - * - * @return whetherthe unmarshalling should read lines lazily - */ - public boolean isLazyLoad() { - return lazyLoad; - } - - /** - * Sets whetherthe unmarshalling should read lines lazily. - * - * @param lazyLoad whetherthe unmarshalling should read lines lazily - * @return current data format instance, fluent API - */ - public DF setLazyLoad(boolean lazyLoad) { - this.lazyLoad = lazyLoad; - return self(); - } - - /** - * Gets whetherthe unmarshalling should produces maps instead of lists. - * - * @return whetherthe unmarshalling should produces maps instead of lists - */ - public boolean isAsMap() { - return asMap; - } - - /** - * Sets whetherthe unmarshalling should produces maps instead of lists. - * - * @param asMap whetherthe unmarshalling should produces maps instead of lists - * @return current data format instance, fluent API - */ - public DF setAsMap(boolean asMap) { - this.asMap = asMap; - return self(); + String[] headersAsArray() { + if (headers != null) { + return headers.split(","); + } else { + return null; + } } /** @@ -539,18 +235,156 @@ public abstract class AbstractUniVocityDataFormat< if (headersDisabled) { settings.setHeaders((String[]) null); } else if (headers != null) { - settings.setHeaders(headers); + settings.setHeaders(headersAsArray()); } } - /** - * Returns {@code this} as the proper data format type. It helps the fluent API with inheritance. - * - * @return {@code this} as the proper data format type - */ - @SuppressWarnings("unchecked") - private DF self() { - return (DF) this; + public String getNullValue() { + return nullValue; + } + + public void setNullValue(String nullValue) { + this.nullValue = nullValue; + } + + public Boolean getSkipEmptyLines() { + return skipEmptyLines; + } + + public void setSkipEmptyLines(Boolean skipEmptyLines) { + this.skipEmptyLines = skipEmptyLines; + } + + public Boolean getIgnoreTrailingWhitespaces() { + return ignoreTrailingWhitespaces; + } + + public void setIgnoreTrailingWhitespaces(Boolean ignoreTrailingWhitespaces) { + this.ignoreTrailingWhitespaces = ignoreTrailingWhitespaces; + } + + public Boolean getIgnoreLeadingWhitespaces() { + return ignoreLeadingWhitespaces; + } + + public void setIgnoreLeadingWhitespaces(Boolean ignoreLeadingWhitespaces) { + this.ignoreLeadingWhitespaces = ignoreLeadingWhitespaces; + } + + public boolean isHeadersDisabled() { + return headersDisabled; + } + + public void setHeadersDisabled(boolean headersDisabled) { + this.headersDisabled = headersDisabled; + } + + public String getHeaders() { + return headers; + } + + public void setHeaders(String headers) { + this.headers = headers; + } + + public Boolean getHeaderExtractionEnabled() { + return headerExtractionEnabled; + } + + public void setHeaderExtractionEnabled(Boolean headerExtractionEnabled) { + this.headerExtractionEnabled = headerExtractionEnabled; + } + + public Integer getNumberOfRecordsToRead() { + return numberOfRecordsToRead; + } + + public void setNumberOfRecordsToRead(Integer numberOfRecordsToRead) { + this.numberOfRecordsToRead = numberOfRecordsToRead; + } + + public String getEmptyValue() { + return emptyValue; + } + + public void setEmptyValue(String emptyValue) { + this.emptyValue = emptyValue; + } + + public String getLineSeparator() { + return lineSeparator; + } + + public void setLineSeparator(String lineSeparator) { + this.lineSeparator = lineSeparator; + } + + public Character getNormalizedLineSeparator() { + return normalizedLineSeparator; + } + + public void setNormalizedLineSeparator(Character normalizedLineSeparator) { + this.normalizedLineSeparator = normalizedLineSeparator; + } + + public Character getComment() { + return comment; + } + + public void setComment(Character comment) { + this.comment = comment; + } + + public boolean isLazyLoad() { + return lazyLoad; + } + + public void setLazyLoad(boolean lazyLoad) { + this.lazyLoad = lazyLoad; + } + + public boolean isAsMap() { + return asMap; + } + + public void setAsMap(boolean asMap) { + this.asMap = asMap; + } + + public CWS getWriterSettings() { + return writerSettings; + } + + public void setWriterSettings(CWS writerSettings) { + this.writerSettings = writerSettings; + } + + public Marshaller<W> getMarshaller() { + return marshaller; + } + + public void setMarshaller(Marshaller<W> marshaller) { + this.marshaller = marshaller; + } + + public CPS getParserSettings() { + return parserSettings; + } + + public void setParserSettings(CPS parserSettings) { + this.parserSettings = parserSettings; + } + + public Unmarshaller<P> getUnmarshaller() { + return unmarshaller; + } + + public void setUnmarshaller(Unmarshaller<P> unmarshaller) { + this.unmarshaller = unmarshaller; + } + + public HeaderRowProcessor getHeaderRowProcessor() { + return headerRowProcessor; } @Override diff --git a/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/UniVocityCsvDataFormat.java b/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/UniVocityCsvDataFormat.java index ceaf2615b7e..70611f9d59b 100644 --- a/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/UniVocityCsvDataFormat.java +++ b/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/UniVocityCsvDataFormat.java @@ -32,110 +32,49 @@ import org.apache.camel.spi.annotations.Dataformat; public class UniVocityCsvDataFormat extends AbstractUniVocityDataFormat<CsvFormat, CsvWriterSettings, CsvWriter, CsvParserSettings, CsvParser, UniVocityCsvDataFormat> { - protected Boolean quoteAllFields; - protected Character quote; - protected Character quoteEscape; - protected Character delimiter; - - /** - * Gets whether or not all fields must be quoted. If {@code null} then the default settings value is used. - * - * @return whether or not all fields must be quoted - * @see com.univocity.parsers.csv.CsvWriterSettings#getQuoteAllFields() - */ + + private Boolean quoteAllFields; + private Character quote; + private Character quoteEscape; + private Character delimiter; + public Boolean getQuoteAllFields() { return quoteAllFields; } - /** - * Gets whether or not all fields must be quoted. If {@code null} then the default settings value is used. - * - * @param quoteAllFields whether or not all fields must be quoted - * @return current data format instance, fluent API - * @see com.univocity.parsers.csv.CsvWriterSettings#setQuoteAllFields(boolean) - */ - public UniVocityCsvDataFormat setQuoteAllFields(Boolean quoteAllFields) { + public void setQuoteAllFields(Boolean quoteAllFields) { this.quoteAllFields = quoteAllFields; - return this; } - /** - * Gets the quote symbol. If {@code null} then the default format value is used. - * - * @return the quote symbol - * @see com.univocity.parsers.csv.CsvFormat#getQuote() - */ public Character getQuote() { return quote; } - /** - * Sets the quote symbol. If {@code null} then the default format value is used. - * - * @param quote the quote symbol - * @return current data format instance, fluent API - * @see com.univocity.parsers.csv.CsvFormat#setQuote(char) - */ - public UniVocityCsvDataFormat setQuote(Character quote) { + public void setQuote(Character quote) { this.quote = quote; - return this; } - /** - * Gets the quote escape symbol. If {@code null} then the default format value is used. - * - * @return the quote escape symbol - * @see com.univocity.parsers.csv.CsvFormat#getQuoteEscape() - */ public Character getQuoteEscape() { return quoteEscape; } - /** - * Sets the quote escape symbol. If {@code null} then the default format value is used. - * - * @param quoteEscape the quote escape symbol - * @return current data format instance, fluent API - * @see com.univocity.parsers.csv.CsvFormat#setQuoteEscape(char) - */ - public UniVocityCsvDataFormat setQuoteEscape(Character quoteEscape) { + public void setQuoteEscape(Character quoteEscape) { this.quoteEscape = quoteEscape; - return this; } - /** - * Gets the delimiter symbol. If {@code null} then the default format value is used. - * - * @return the delimiter symbol - * @see com.univocity.parsers.csv.CsvFormat#getDelimiter() - */ public Character getDelimiter() { return delimiter; } - /** - * Sets the delimiter symbol. If {@code null} then the default format value is used. - * - * @param delimiter the delimiter symbol - * @return current data format instance, fluent API - * @see com.univocity.parsers.csv.CsvFormat#setDelimiter(char) - */ - public UniVocityCsvDataFormat setDelimiter(Character delimiter) { + public void setDelimiter(Character delimiter) { this.delimiter = delimiter; - return this; } - /** - * {@inheritDoc} - */ @Override protected CsvWriterSettings createWriterSettings() { return new CsvWriterSettings(); } - /** - * {@inheritDoc} - */ @Override protected void configureWriterSettings(CsvWriterSettings settings) { super.configureWriterSettings(settings); @@ -145,25 +84,16 @@ public class UniVocityCsvDataFormat } } - /** - * {@inheritDoc} - */ @Override protected CsvWriter createWriter(Writer writer, CsvWriterSettings settings) { return new CsvWriter(writer, settings); } - /** - * {@inheritDoc} - */ @Override protected CsvParserSettings createParserSettings() { return new CsvParserSettings(); } - /** - * {@inheritDoc} - */ @Override protected void configureParserSettings(CsvParserSettings settings) { super.configureParserSettings(settings); @@ -173,17 +103,11 @@ public class UniVocityCsvDataFormat } } - /** - * {@inheritDoc} - */ @Override protected CsvParser createParser(CsvParserSettings settings) { return new CsvParser(settings); } - /** - * {@inheritDoc} - */ @Override protected void configureFormat(CsvFormat format) { super.configureFormat(format); diff --git a/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/UniVocityFixedDataFormat.java b/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/UniVocityFixedDataFormat.java index 09dd52c3390..55b7f3e0e93 100644 --- a/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/UniVocityFixedDataFormat.java +++ b/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/UniVocityFixedDataFormat.java @@ -34,118 +34,66 @@ import org.apache.camel.spi.annotations.Dataformat; public class UniVocityFixedDataFormat extends AbstractUniVocityDataFormat<FixedWidthFormat, FixedWidthWriterSettings, FixedWidthWriter, FixedWidthParserSettings, FixedWidthParser, UniVocityFixedDataFormat> { - protected int[] fieldLengths; - protected Boolean skipTrailingCharsUntilNewline; - protected Boolean recordEndsOnNewline; - protected Character padding; - /** - * Gets the field lengths. It's used to construct uniVocity {@link com.univocity.parsers.fixed.FixedWidthFields} - * instance. - * - * @return the field lengths - */ - public int[] getFieldLengths() { + private String fieldLengths; + private Boolean skipTrailingCharsUntilNewline; + private Boolean recordEndsOnNewline; + private Character padding; + + public int[] fieldLengthsAsArray() { + if (fieldLengths == null) { + return null; + } + String[] arr = fieldLengths.split(","); + int[] answer = new int[arr.length]; + for (int i = 0; i < arr.length; i++) { + answer[i] = Integer.parseInt(arr[i]); + } + return answer; + } + + public String getFieldLengths() { return fieldLengths; } - /** - * Sets the field lengths It's used to construct uniVocity {@link com.univocity.parsers.fixed.FixedWidthFields} - * instance. - * - * @param fieldLengths the field length - * @return current data format instance, fluent API - */ - public UniVocityFixedDataFormat setFieldLengths(int[] fieldLengths) { + public void setFieldLengths(String fieldLengths) { this.fieldLengths = fieldLengths; - return this; } - /** - * Gets whether or not trailing characters until new line must be ignored. - * - * @return whether or not trailing characters until new line must be ignored - * @see com.univocity.parsers.fixed.FixedWidthParserSettings#getSkipTrailingCharsUntilNewline() - */ public Boolean getSkipTrailingCharsUntilNewline() { return skipTrailingCharsUntilNewline; } - /** - * Sets whether or not trailing characters until new line must be ignored. - * - * @param skipTrailingCharsUntilNewline whether or not trailing characters until new line must be ignored - * @return current data format instance, fluent API - * @see com.univocity.parsers.fixed.FixedWidthParserSettings#setSkipTrailingCharsUntilNewline(boolean) - */ - public UniVocityFixedDataFormat setSkipTrailingCharsUntilNewline(Boolean skipTrailingCharsUntilNewline) { + public void setSkipTrailingCharsUntilNewline(Boolean skipTrailingCharsUntilNewline) { this.skipTrailingCharsUntilNewline = skipTrailingCharsUntilNewline; - return this; } - /** - * Gets whether or not the record ends on new line. - * - * @return whether or not the record ends on new line - * @see com.univocity.parsers.fixed.FixedWidthParserSettings#getRecordEndsOnNewline() - */ public Boolean getRecordEndsOnNewline() { return recordEndsOnNewline; } - /** - * Sets whether or not the record ends on new line - * - * @param recordEndsOnNewline whether or not the record ends on new line - * @return current data format instance, fluent API - * @see com.univocity.parsers.fixed.FixedWidthParserSettings#setRecordEndsOnNewline(boolean) - */ - public UniVocityFixedDataFormat setRecordEndsOnNewline(Boolean recordEndsOnNewline) { + public void setRecordEndsOnNewline(Boolean recordEndsOnNewline) { this.recordEndsOnNewline = recordEndsOnNewline; - return this; } - /** - * Gets the padding symbol. If {@code null} then the default format value is used. - * - * @return the padding symbol - * @see com.univocity.parsers.fixed.FixedWidthFormat#getPadding() - */ public Character getPadding() { return padding; } - /** - * Sets the padding symbol. If {@code null} then the default format value is used. - * - * @param padding the padding symbol - * @return current data format instance, fluent API - * @see com.univocity.parsers.fixed.FixedWidthFormat#setPadding(char) - */ - public UniVocityFixedDataFormat setPadding(Character padding) { + public void setPadding(Character padding) { this.padding = padding; - return this; } - /** - * {@inheritDoc} - */ @Override protected FixedWidthWriterSettings createWriterSettings() { return new FixedWidthWriterSettings(createFixedWidthFields()); } - /** - * {@inheritDoc} - */ @Override protected FixedWidthWriter createWriter(Writer writer, FixedWidthWriterSettings settings) { return new FixedWidthWriter(writer, settings); } - /** - * {@inheritDoc} - */ @Override protected FixedWidthParserSettings createParserSettings() { return new FixedWidthParserSettings(createFixedWidthFields()); @@ -163,17 +111,11 @@ public class UniVocityFixedDataFormat } } - /** - * {@inheritDoc} - */ @Override protected FixedWidthParser createParser(FixedWidthParserSettings settings) { return new FixedWidthParser(settings); } - /** - * {@inheritDoc} - */ @Override protected void configureFormat(FixedWidthFormat format) { super.configureFormat(format); @@ -196,20 +138,23 @@ public class UniVocityFixedDataFormat } // If there's no header then we only use their length - if (headers == null) { - return new FixedWidthFields(fieldLengths); + if (headers == null || headers.isBlank()) { + return new FixedWidthFields(fieldLengthsAsArray()); } + String[] arr1 = headersAsArray(); + int[] arr2 = fieldLengthsAsArray(); + // Use both headers and field lengths (same size and no duplicate headers) - if (fieldLengths.length != headers.length) { + if (arr1.length != arr2.length) { throw new IllegalArgumentException( "The headers and fieldLengths must have the same number of element in order to use the fixed-width format."); } LinkedHashMap<String, Integer> fields = new LinkedHashMap<>(); - for (int i = 0; i < headers.length; i++) { - fields.put(headers[i], fieldLengths[i]); + for (int i = 0; i < arr1.length; i++) { + fields.put(arr1[i], arr2[i]); } - if (fields.size() != headers.length) { + if (fields.size() != arr1.length) { throw new IllegalArgumentException("The headers cannot have duplicates in order to use the fixed-width format."); } return new FixedWidthFields(fields); diff --git a/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/UniVocityTsvDataFormat.java b/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/UniVocityTsvDataFormat.java index 204b1c3e4eb..33d4bcaea7b 100644 --- a/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/UniVocityTsvDataFormat.java +++ b/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/UniVocityTsvDataFormat.java @@ -32,65 +32,37 @@ import org.apache.camel.spi.annotations.Dataformat; public class UniVocityTsvDataFormat extends AbstractUniVocityDataFormat<TsvFormat, TsvWriterSettings, TsvWriter, TsvParserSettings, TsvParser, UniVocityTsvDataFormat> { - protected Character escapeChar; - /** - * Gets the escape character symbol. If {@code null} then the default format value is used. - * - * @return the escape character symbol - * @see com.univocity.parsers.tsv.TsvFormat#getEscapeChar() - */ + private Character escapeChar; + public Character getEscapeChar() { return escapeChar; } - /** - * Sets the escape character symbol. If {@code null} then the default settings value is used. - * - * @param escapeChar the escape character symbol - * @return current data format instance, fluent API - * @see com.univocity.parsers.tsv.TsvFormat#setEscapeChar(char) - */ - public UniVocityTsvDataFormat setEscapeChar(Character escapeChar) { + public void setEscapeChar(Character escapeChar) { this.escapeChar = escapeChar; - return this; } - /** - * {@inheritDoc} - */ @Override protected TsvWriterSettings createWriterSettings() { return new TsvWriterSettings(); } - /** - * {@inheritDoc} - */ @Override protected TsvWriter createWriter(Writer writer, TsvWriterSettings settings) { return new TsvWriter(writer, settings); } - /** - * {@inheritDoc} - */ @Override protected TsvParserSettings createParserSettings() { return new TsvParserSettings(); } - /** - * {@inheritDoc} - */ @Override protected TsvParser createParser(TsvParserSettings settings) { return new TsvParser(settings); } - /** - * {@inheritDoc} - */ @Override protected void configureFormat(TsvFormat format) { super.configureFormat(format); diff --git a/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityCsvDataFormatMarshalTest.java b/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityCsvDataFormatMarshalTest.java index e9cc0ffb21a..fb6e4b160c2 100644 --- a/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityCsvDataFormatMarshalTest.java +++ b/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityCsvDataFormatMarshalTest.java @@ -125,17 +125,19 @@ public final class UniVocityCsvDataFormatMarshalTest extends CamelTestSupport { tests.put("default", new UniVocityCsvDataFormat()); // Write a CSV with specific headers - tests.put("header", new UniVocityCsvDataFormat() - .setHeaders(new String[] { "A", "C" })); + var df = new UniVocityCsvDataFormat(); + df.setHeaders("A,C"); + tests.put("header", df); // Write a CSV with an advanced configuration - tests.put("advanced", new UniVocityCsvDataFormat() - .setNullValue("N/A") - .setEmptyValue("empty") - .setQuote('_') - .setQuoteAllFields(true) - .setQuoteEscape('-') - .setDelimiter(';')); + df = new UniVocityCsvDataFormat(); + df.setNullValue("N/A"); + df.setEmptyValue("empty"); + df.setQuote('_'); + df.setQuoteAllFields(true); + df.setQuoteEscape('-'); + df.setDelimiter(';'); + tests.put("advanced", df); return new RouteBuilder() { @Override diff --git a/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityCsvDataFormatTest.java b/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityCsvDataFormatTest.java index 77cf1e5d9ce..ffcf661dec2 100644 --- a/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityCsvDataFormatTest.java +++ b/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityCsvDataFormatTest.java @@ -29,8 +29,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue; public final class UniVocityCsvDataFormatTest { @Test public void shouldConfigureNullValue() { - UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat() - .setNullValue("N/A"); + UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat(); + dataFormat.setNullValue("N/A"); assertEquals("N/A", dataFormat.getNullValue()); assertEquals("N/A", dataFormat.createAndConfigureWriterSettings().getNullValue()); @@ -39,8 +39,8 @@ public final class UniVocityCsvDataFormatTest { @Test public void shouldConfigureSkipEmptyLines() { - UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat() - .setSkipEmptyLines(true); + UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat(); + dataFormat.setSkipEmptyLines(true); assertTrue(dataFormat.getSkipEmptyLines()); assertTrue(dataFormat.createAndConfigureWriterSettings().getSkipEmptyLines()); @@ -49,8 +49,8 @@ public final class UniVocityCsvDataFormatTest { @Test public void shouldConfigureIgnoreTrailingWhitespaces() { - UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat() - .setIgnoreTrailingWhitespaces(true); + UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat(); + dataFormat.setIgnoreTrailingWhitespaces(true); assertTrue(dataFormat.getIgnoreTrailingWhitespaces()); assertTrue(dataFormat.createAndConfigureWriterSettings().getIgnoreTrailingWhitespaces()); @@ -59,8 +59,8 @@ public final class UniVocityCsvDataFormatTest { @Test public void shouldConfigureIgnoreLeadingWhitespaces() { - UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat() - .setIgnoreLeadingWhitespaces(true); + UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat(); + dataFormat.setIgnoreLeadingWhitespaces(true); assertTrue(dataFormat.getIgnoreLeadingWhitespaces()); assertTrue(dataFormat.createAndConfigureWriterSettings().getIgnoreLeadingWhitespaces()); @@ -69,8 +69,8 @@ public final class UniVocityCsvDataFormatTest { @Test public void shouldConfigureHeadersDisabled() { - UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat() - .setHeadersDisabled(true); + UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat(); + dataFormat.setHeadersDisabled(true); assertTrue(dataFormat.isHeadersDisabled()); assertNull(dataFormat.createAndConfigureWriterSettings().getHeaders()); @@ -79,18 +79,17 @@ public final class UniVocityCsvDataFormatTest { @Test public void shouldConfigureHeaders() { - UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat() - .setHeaders(new String[] { "A", "B", "C" }); + UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat(); + dataFormat.setHeaders("A,B,C"); - assertArrayEquals(new String[] { "A", "B", "C" }, dataFormat.getHeaders()); assertArrayEquals(new String[] { "A", "B", "C" }, dataFormat.createAndConfigureWriterSettings().getHeaders()); assertArrayEquals(new String[] { "A", "B", "C" }, dataFormat.createAndConfigureParserSettings().getHeaders()); } @Test public void shouldConfigureHeaderExtractionEnabled() { - UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat() - .setHeaderExtractionEnabled(true); + UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat(); + dataFormat.setHeaderExtractionEnabled(true); assertTrue(dataFormat.getHeaderExtractionEnabled()); assertTrue(dataFormat.createAndConfigureParserSettings().isHeaderExtractionEnabled()); @@ -98,8 +97,8 @@ public final class UniVocityCsvDataFormatTest { @Test public void shouldConfigureNumberOfRecordsToRead() { - UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat() - .setNumberOfRecordsToRead(42); + UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat(); + dataFormat.setNumberOfRecordsToRead(42); assertEquals(Integer.valueOf(42), dataFormat.getNumberOfRecordsToRead()); assertEquals(42, dataFormat.createAndConfigureParserSettings().getNumberOfRecordsToRead()); @@ -107,8 +106,8 @@ public final class UniVocityCsvDataFormatTest { @Test public void shouldConfigureEmptyValue() { - UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat() - .setEmptyValue("empty"); + UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat(); + dataFormat.setEmptyValue("empty"); assertEquals("empty", dataFormat.getEmptyValue()); assertEquals("empty", dataFormat.createAndConfigureWriterSettings().getEmptyValue()); @@ -117,8 +116,8 @@ public final class UniVocityCsvDataFormatTest { @Test public void shouldConfigureLineSeparator() { - UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat() - .setLineSeparator("ls"); + UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat(); + dataFormat.setLineSeparator("ls"); assertEquals("ls", dataFormat.getLineSeparator()); assertEquals("ls", dataFormat.createAndConfigureWriterSettings().getFormat().getLineSeparatorString()); @@ -127,8 +126,8 @@ public final class UniVocityCsvDataFormatTest { @Test public void shouldConfigureNormalizedLineSeparator() { - UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat() - .setNormalizedLineSeparator('n'); + UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat(); + dataFormat.setNormalizedLineSeparator('n'); assertEquals(Character.valueOf('n'), dataFormat.getNormalizedLineSeparator()); assertEquals('n', dataFormat.createAndConfigureWriterSettings().getFormat().getNormalizedNewline()); @@ -137,8 +136,8 @@ public final class UniVocityCsvDataFormatTest { @Test public void shouldConfigureComment() { - UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat() - .setComment('c'); + UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat(); + dataFormat.setComment('c'); assertEquals(Character.valueOf('c'), dataFormat.getComment()); assertEquals('c', dataFormat.createAndConfigureWriterSettings().getFormat().getComment()); @@ -147,24 +146,24 @@ public final class UniVocityCsvDataFormatTest { @Test public void shouldConfigureLazyLoad() { - UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat() - .setLazyLoad(true); + UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat(); + dataFormat.setLazyLoad(true); assertTrue(dataFormat.isLazyLoad()); } @Test public void shouldConfigureAsMap() { - UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat() - .setAsMap(true); + UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat(); + dataFormat.setAsMap(true); assertTrue(dataFormat.isAsMap()); } @Test public void shouldConfigureQuoteAllFields() { - UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat() - .setQuoteAllFields(true); + UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat(); + dataFormat.setQuoteAllFields(true); assertTrue(dataFormat.getQuoteAllFields()); assertTrue(dataFormat.createAndConfigureWriterSettings().getQuoteAllFields()); @@ -172,8 +171,8 @@ public final class UniVocityCsvDataFormatTest { @Test public void shouldConfigureQuote() { - UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat() - .setQuote('q'); + UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat(); + dataFormat.setQuote('q'); assertEquals(Character.valueOf('q'), dataFormat.getQuote()); assertEquals('q', dataFormat.createAndConfigureWriterSettings().getFormat().getQuote()); @@ -182,8 +181,8 @@ public final class UniVocityCsvDataFormatTest { @Test public void shouldConfigureQuoteEscape() { - UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat() - .setQuoteEscape('e'); + UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat(); + dataFormat.setQuoteEscape('e'); assertEquals(Character.valueOf('e'), dataFormat.getQuoteEscape()); assertEquals('e', dataFormat.createAndConfigureWriterSettings().getFormat().getQuoteEscape()); @@ -192,8 +191,8 @@ public final class UniVocityCsvDataFormatTest { @Test public void shouldConfigureDelimiter() { - UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat() - .setDelimiter('d'); + UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat(); + dataFormat.setDelimiter('d'); assertEquals(Character.valueOf('d'), dataFormat.getDelimiter()); assertEquals('d', dataFormat.createAndConfigureWriterSettings().getFormat().getDelimiter()); diff --git a/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityCsvDataFormatUnmarshalTest.java b/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityCsvDataFormatUnmarshalTest.java index d595aad63f9..0759cc6b9d5 100644 --- a/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityCsvDataFormatUnmarshalTest.java +++ b/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityCsvDataFormatUnmarshalTest.java @@ -159,27 +159,31 @@ public final class UniVocityCsvDataFormatUnmarshalTest extends CamelTestSupport tests.put("default", new UniVocityCsvDataFormat()); // Reading CSV as Map - tests.put("map", new UniVocityCsvDataFormat() - .setAsMap(true) - .setHeaderExtractionEnabled(true)); + var df = new UniVocityCsvDataFormat(); + df.setAsMap(true); + df.setHeaderExtractionEnabled(true); + tests.put("map", df); // Reading CSV as Map with specific headers - tests.put("mapWithHeaders", new UniVocityCsvDataFormat() - .setAsMap(true) - .setHeaders(new String[] { "A", "B", "C" })); + df = new UniVocityCsvDataFormat(); + df.setAsMap(true); + df.setHeaders("A,B,C"); + tests.put("mapWithHeaders", df); // Reading CSV using an iterator - tests.put("lazy", new UniVocityCsvDataFormat() - .setLazyLoad(true)); + df = new UniVocityCsvDataFormat(); + df.setLazyLoad(true); + tests.put("lazy", df); // Reading CSV using advanced configuration - tests.put("advanced", new UniVocityCsvDataFormat() - .setNullValue("N/A") - .setDelimiter(';') - .setIgnoreLeadingWhitespaces(true) - .setIgnoreTrailingWhitespaces(false) - .setComment('!') - .setSkipEmptyLines(true)); + df = new UniVocityCsvDataFormat(); + df.setNullValue("N/A"); + df.setDelimiter(';'); + df.setIgnoreLeadingWhitespaces(true); + df.setIgnoreTrailingWhitespaces(false); + df.setComment('!'); + df.setSkipEmptyLines(true); + tests.put("advanced", df); return new RouteBuilder() { @Override diff --git a/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityFixedDataFormatMarshalTest.java b/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityFixedDataFormatMarshalTest.java index 4d097ba3f2f..47b8189e182 100644 --- a/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityFixedDataFormatMarshalTest.java +++ b/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityFixedDataFormatMarshalTest.java @@ -122,20 +122,23 @@ public final class UniVocityFixedDataFormatMarshalTest extends CamelTestSupport final Map<String, DataFormat> tests = new HashMap<>(); // Default writing of fixed-width - tests.put("default", new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 3, 3, 5 })); + var df = new UniVocityFixedDataFormat(); + df.setFieldLengths("3,3,5"); + tests.put("default", df); // Write a fixed-width with specific headers - tests.put("header", new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 3, 5 }) - .setHeaders(new String[] { "A", "C" })); + df = new UniVocityFixedDataFormat(); + df.setFieldLengths("3,5"); + df.setHeaders("A,C"); + tests.put("header", df); // Write a fixed-width with an advanced configuration - tests.put("advanced", new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 5, 5 }) - .setNullValue("N/A") - .setEmptyValue("empty") - .setPadding('_')); + df = new UniVocityFixedDataFormat(); + df.setFieldLengths("5,5"); + df.setNullValue("N/A"); + df.setEmptyValue("empty"); + df.setPadding('_'); + tests.put("advanced", df); return new RouteBuilder() { @Override diff --git a/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityFixedDataFormatTest.java b/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityFixedDataFormatTest.java index 7285411ea2b..a4d7fb644a9 100644 --- a/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityFixedDataFormatTest.java +++ b/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityFixedDataFormatTest.java @@ -30,9 +30,9 @@ import static org.junit.jupiter.api.Assertions.assertTrue; public final class UniVocityFixedDataFormatTest { @Test public void shouldConfigureNullValue() { - UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 1, 2, 3 }) - .setNullValue("N/A"); + UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat(); + dataFormat.setFieldLengths("1,2,3"); + dataFormat.setNullValue("N/A"); assertEquals("N/A", dataFormat.getNullValue()); assertEquals("N/A", dataFormat.createAndConfigureWriterSettings().getNullValue()); @@ -41,9 +41,9 @@ public final class UniVocityFixedDataFormatTest { @Test public void shouldConfigureSkipEmptyLines() { - UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 1, 2, 3 }) - .setSkipEmptyLines(true); + UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat(); + dataFormat.setFieldLengths("1,2,3"); + dataFormat.setSkipEmptyLines(true); assertTrue(dataFormat.getSkipEmptyLines()); assertTrue(dataFormat.createAndConfigureWriterSettings().getSkipEmptyLines()); @@ -52,9 +52,9 @@ public final class UniVocityFixedDataFormatTest { @Test public void shouldConfigureIgnoreTrailingWhitespaces() { - UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 1, 2, 3 }) - .setIgnoreTrailingWhitespaces(true); + UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat(); + dataFormat.setFieldLengths("1,2,3"); + dataFormat.setIgnoreTrailingWhitespaces(true); assertTrue(dataFormat.getIgnoreTrailingWhitespaces()); assertTrue(dataFormat.createAndConfigureWriterSettings().getIgnoreTrailingWhitespaces()); @@ -63,9 +63,9 @@ public final class UniVocityFixedDataFormatTest { @Test public void shouldConfigureIgnoreLeadingWhitespaces() { - UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 1, 2, 3 }) - .setIgnoreLeadingWhitespaces(true); + UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat(); + dataFormat.setFieldLengths("1,2,3"); + dataFormat.setIgnoreLeadingWhitespaces(true); assertTrue(dataFormat.getIgnoreLeadingWhitespaces()); assertTrue(dataFormat.createAndConfigureWriterSettings().getIgnoreLeadingWhitespaces()); @@ -74,9 +74,9 @@ public final class UniVocityFixedDataFormatTest { @Test public void shouldConfigureHeadersDisabled() { - UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 1, 2, 3 }) - .setHeadersDisabled(true); + UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat(); + dataFormat.setFieldLengths("1,2,3"); + dataFormat.setHeadersDisabled(true); assertTrue(dataFormat.isHeadersDisabled()); assertNull(dataFormat.createAndConfigureWriterSettings().getHeaders()); @@ -85,20 +85,20 @@ public final class UniVocityFixedDataFormatTest { @Test public void shouldConfigureHeaders() { - UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 1, 2, 3 }) - .setHeaders(new String[] { "A", "B", "C" }); + UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat(); + dataFormat.setFieldLengths("1,2,3"); + dataFormat.setHeaders("A,B,C"); - assertArrayEquals(new String[] { "A", "B", "C" }, dataFormat.getHeaders()); + assertEquals("A,B,C", dataFormat.getHeaders()); assertArrayEquals(new String[] { "A", "B", "C" }, dataFormat.createAndConfigureWriterSettings().getHeaders()); assertArrayEquals(new String[] { "A", "B", "C" }, dataFormat.createAndConfigureParserSettings().getHeaders()); } @Test public void shouldConfigureHeaderExtractionEnabled() { - UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 1, 2, 3 }) - .setHeaderExtractionEnabled(true); + UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat(); + dataFormat.setFieldLengths("1,2,3"); + dataFormat.setHeaderExtractionEnabled(true); assertTrue(dataFormat.getHeaderExtractionEnabled()); assertTrue(dataFormat.createAndConfigureParserSettings().isHeaderExtractionEnabled()); @@ -106,9 +106,9 @@ public final class UniVocityFixedDataFormatTest { @Test public void shouldConfigureNumberOfRecordsToRead() { - UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 1, 2, 3 }) - .setNumberOfRecordsToRead(42); + UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat(); + dataFormat.setFieldLengths("1,2,3"); + dataFormat.setNumberOfRecordsToRead(42); assertEquals(Integer.valueOf(42), dataFormat.getNumberOfRecordsToRead()); assertEquals(42, dataFormat.createAndConfigureParserSettings().getNumberOfRecordsToRead()); @@ -116,9 +116,9 @@ public final class UniVocityFixedDataFormatTest { @Test public void shouldConfigureEmptyValue() { - UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 1, 2, 3 }) - .setEmptyValue("empty"); + UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat(); + dataFormat.setFieldLengths("1,2,3"); + dataFormat.setEmptyValue("empty"); assertEquals("empty", dataFormat.getEmptyValue()); assertEquals("empty", dataFormat.createAndConfigureWriterSettings().getEmptyValue()); @@ -126,9 +126,9 @@ public final class UniVocityFixedDataFormatTest { @Test public void shouldConfigureLineSeparator() { - UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 1, 2, 3 }) - .setLineSeparator("ls"); + UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat(); + dataFormat.setFieldLengths("1,2,3"); + dataFormat.setLineSeparator("ls"); assertEquals("ls", dataFormat.getLineSeparator()); assertEquals("ls", dataFormat.createAndConfigureWriterSettings().getFormat().getLineSeparatorString()); @@ -137,9 +137,9 @@ public final class UniVocityFixedDataFormatTest { @Test public void shouldConfigureNormalizedLineSeparator() { - UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 1, 2, 3 }) - .setNormalizedLineSeparator('n'); + UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat(); + dataFormat.setFieldLengths("1,2,3"); + dataFormat.setNormalizedLineSeparator('n'); assertEquals(Character.valueOf('n'), dataFormat.getNormalizedLineSeparator()); assertEquals('n', dataFormat.createAndConfigureWriterSettings().getFormat().getNormalizedNewline()); @@ -148,9 +148,9 @@ public final class UniVocityFixedDataFormatTest { @Test public void shouldConfigureComment() { - UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 1, 2, 3 }) - .setComment('c'); + UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat(); + dataFormat.setFieldLengths("1,2,3"); + dataFormat.setComment('c'); assertEquals(Character.valueOf('c'), dataFormat.getComment()); assertEquals('c', dataFormat.createAndConfigureWriterSettings().getFormat().getComment()); @@ -159,27 +159,27 @@ public final class UniVocityFixedDataFormatTest { @Test public void shouldConfigureLazyLoad() { - UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 1, 2, 3 }) - .setLazyLoad(true); + UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat(); + dataFormat.setFieldLengths("1,2,3"); + dataFormat.setLazyLoad(true); assertTrue(dataFormat.isLazyLoad()); } @Test public void shouldConfigureAsMap() { - UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 1, 2, 3 }) - .setAsMap(true); + UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat(); + dataFormat.setFieldLengths("1,2,3"); + dataFormat.setAsMap(true); assertTrue(dataFormat.isAsMap()); } @Test public void shouldConfigurePadding() { - UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 1, 2, 3 }) - .setPadding('p'); + UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat(); + dataFormat.setFieldLengths("1,2,3"); + dataFormat.setPadding('p'); assertEquals(Character.valueOf('p'), dataFormat.getPadding()); assertEquals('p', dataFormat.createAndConfigureWriterSettings().getFormat().getPadding()); @@ -188,9 +188,9 @@ public final class UniVocityFixedDataFormatTest { @Test public void shouldConfigureSkipTrailingCharsUntilNewline() { - UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 1, 2, 3 }) - .setSkipTrailingCharsUntilNewline(true); + UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat(); + dataFormat.setFieldLengths("1,2,3"); + dataFormat.setSkipTrailingCharsUntilNewline(true); assertTrue(dataFormat.getSkipTrailingCharsUntilNewline()); assertTrue(dataFormat.createAndConfigureParserSettings().getSkipTrailingCharsUntilNewline()); @@ -198,9 +198,9 @@ public final class UniVocityFixedDataFormatTest { @Test public void shouldConfigureRecordEndsOnNewline() { - UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 1, 2, 3 }) - .setRecordEndsOnNewline(true); + UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat(); + dataFormat.setFieldLengths("1,2,3"); + dataFormat.setRecordEndsOnNewline(true); assertTrue(dataFormat.getRecordEndsOnNewline()); assertTrue(dataFormat.createAndConfigureParserSettings().getRecordEndsOnNewline()); @@ -208,22 +208,22 @@ public final class UniVocityFixedDataFormatTest { @Test public void shouldConfigureFieldLengthWithLengthsOnly() { - UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 1, 2, 3 }); + UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat(); + dataFormat.setFieldLengths("1,2,3"); - assertArrayEquals(new int[] { 1, 2, 3 }, dataFormat.getFieldLengths()); + assertEquals("1,2,3", dataFormat.getFieldLengths()); dataFormat.createAndConfigureWriterSettings(); } @Test public void shouldConfigureFieldLengthWithHeadersAndLengths() { - UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 1, 2, 3 }) - .setHeaders(new String[] { "A", "B", "C" }); + UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat(); + dataFormat.setFieldLengths("1,2,3"); + dataFormat.setHeaders("A,B,C"); - assertArrayEquals(new int[] { 1, 2, 3 }, dataFormat.getFieldLengths()); - assertArrayEquals(new String[] { "A", "B", "C" }, dataFormat.getHeaders()); + assertEquals("1,2,3", dataFormat.getFieldLengths()); + assertEquals("A,B,C", dataFormat.getHeaders()); dataFormat.createAndConfigureWriterSettings(); } @@ -236,24 +236,24 @@ public final class UniVocityFixedDataFormatTest { @Test public void shouldNotAllowHeadersAndLengthsOfDifferentSize() { - UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 1, 2, 3, 4 }) - .setHeaders(new String[] { "A", "B", "C" }); + UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat(); + dataFormat.setFieldLengths("1,2,3,4"); + dataFormat.setHeaders("A,B,C"); - assertArrayEquals(new int[] { 1, 2, 3, 4 }, dataFormat.getFieldLengths()); - assertArrayEquals(new String[] { "A", "B", "C" }, dataFormat.getHeaders()); + assertEquals("1,2,3,4", dataFormat.getFieldLengths()); + assertEquals("A,B,C", dataFormat.getHeaders()); assertThrows(IllegalArgumentException.class, () -> dataFormat.createAndConfigureWriterSettings()); } @Test public void shouldNotAllowHeadersWithSameName() { - UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 1, 2, 3 }) - .setHeaders(new String[] { "A", "B", "A" }); + UniVocityFixedDataFormat dataFormat = new UniVocityFixedDataFormat(); + dataFormat.setFieldLengths("1,2,3"); + dataFormat.setHeaders("A,B,A"); - assertArrayEquals(new int[] { 1, 2, 3 }, dataFormat.getFieldLengths()); - assertArrayEquals(new String[] { "A", "B", "A" }, dataFormat.getHeaders()); + assertEquals("1,2,3", dataFormat.getFieldLengths()); + assertEquals("A,B,A", dataFormat.getHeaders()); assertThrows(IllegalArgumentException.class, () -> dataFormat.createAndConfigureWriterSettings()); } diff --git a/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityFixedDataFormatUnmarshalTest.java b/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityFixedDataFormatUnmarshalTest.java index ed70d7a0a87..cc73e718d95 100644 --- a/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityFixedDataFormatUnmarshalTest.java +++ b/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityFixedDataFormatUnmarshalTest.java @@ -155,33 +155,38 @@ public final class UniVocityFixedDataFormatUnmarshalTest extends CamelTestSuppor final Map<String, DataFormat> tests = new HashMap<>(); // Default reading of fixed-width - tests.put("default", new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 3, 3, 5 })); + var df = new UniVocityFixedDataFormat(); + df.setFieldLengths("3,3,5"); + tests.put("default", df); // Reading fixed-width as Map - tests.put("map", new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 3, 3, 5 }) - .setAsMap(true) - .setHeaderExtractionEnabled(true)); + df = new UniVocityFixedDataFormat(); + df.setFieldLengths("3,3,5"); + df.setAsMap(true); + df.setHeaderExtractionEnabled(true); + tests.put("map", df); // Reading fixed-width as Map with specific headers - tests.put("mapWithHeaders", new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 3, 3, 5 }) - .setAsMap(true) - .setHeaders(new String[] { "A", "B", "C" })); + df = new UniVocityFixedDataFormat(); + df.setFieldLengths("3,3,5"); + df.setAsMap(true); + df.setHeaders("A,B,C"); + tests.put("mapWithHeaders", df); // Reading fixed-width using an iterator - tests.put("lazy", new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 3, 3, 5 }) - .setLazyLoad(true)); + df = new UniVocityFixedDataFormat(); + df.setFieldLengths("3,3,5"); + df.setLazyLoad(true); + tests.put("lazy", df); // Reading fixed-width using advanced configuration - tests.put("advanced", new UniVocityFixedDataFormat() - .setFieldLengths(new int[] { 3, 3 }) - .setNullValue("N/A") - .setPadding('_') - .setComment('!') - .setSkipEmptyLines(true)); + df = new UniVocityFixedDataFormat(); + df.setFieldLengths("3,3"); + df.setNullValue("N/A"); + df.setPadding('_'); + df.setComment('!'); + df.setSkipEmptyLines(true); + tests.put("advanced", df); return new RouteBuilder() { @Override diff --git a/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityTsvDataFormatMarshalTest.java b/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityTsvDataFormatMarshalTest.java index 090ce3134b7..ada56390a2e 100644 --- a/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityTsvDataFormatMarshalTest.java +++ b/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityTsvDataFormatMarshalTest.java @@ -125,13 +125,15 @@ public final class UniVocityTsvDataFormatMarshalTest extends CamelTestSupport { tests.put("default", new UniVocityTsvDataFormat()); // Write a TSV with specific headers - tests.put("header", new UniVocityTsvDataFormat() - .setHeaders(new String[] { "A", "C" })); + var df = new UniVocityTsvDataFormat(); + df.setHeaders("A,C"); + tests.put("header", df); // Write a TSV with an advanced configuration - tests.put("advanced", new UniVocityTsvDataFormat() - .setNullValue("N/A") - .setEmptyValue("empty")); + df = new UniVocityTsvDataFormat(); + df.setNullValue("N/A"); + df.setEmptyValue("empty"); + tests.put("advanced", df); return new RouteBuilder() { @Override diff --git a/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityTsvDataFormatTest.java b/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityTsvDataFormatTest.java index 970aa4668af..70965b529d0 100644 --- a/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityTsvDataFormatTest.java +++ b/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityTsvDataFormatTest.java @@ -29,8 +29,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue; public final class UniVocityTsvDataFormatTest { @Test public void shouldConfigureNullValue() { - UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat() - .setNullValue("N/A"); + UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat(); + dataFormat.setNullValue("N/A"); assertEquals("N/A", dataFormat.getNullValue()); assertEquals("N/A", dataFormat.createAndConfigureWriterSettings().getNullValue()); @@ -39,8 +39,8 @@ public final class UniVocityTsvDataFormatTest { @Test public void shouldConfigureSkipEmptyLines() { - UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat() - .setSkipEmptyLines(true); + UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat(); + dataFormat.setSkipEmptyLines(true); assertTrue(dataFormat.getSkipEmptyLines()); assertTrue(dataFormat.createAndConfigureWriterSettings().getSkipEmptyLines()); @@ -49,8 +49,8 @@ public final class UniVocityTsvDataFormatTest { @Test public void shouldConfigureIgnoreTrailingWhitespaces() { - UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat() - .setIgnoreTrailingWhitespaces(true); + UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat(); + dataFormat.setIgnoreTrailingWhitespaces(true); assertTrue(dataFormat.getIgnoreTrailingWhitespaces()); assertTrue(dataFormat.createAndConfigureWriterSettings().getIgnoreTrailingWhitespaces()); @@ -59,8 +59,8 @@ public final class UniVocityTsvDataFormatTest { @Test public void shouldConfigureIgnoreLeadingWhitespaces() { - UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat() - .setIgnoreLeadingWhitespaces(true); + UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat(); + dataFormat.setIgnoreLeadingWhitespaces(true); assertTrue(dataFormat.getIgnoreLeadingWhitespaces()); assertTrue(dataFormat.createAndConfigureWriterSettings().getIgnoreLeadingWhitespaces()); @@ -69,8 +69,8 @@ public final class UniVocityTsvDataFormatTest { @Test public void shouldConfigureHeadersDisabled() { - UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat() - .setHeadersDisabled(true); + UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat(); + dataFormat.setHeadersDisabled(true); assertTrue(dataFormat.isHeadersDisabled()); assertNull(dataFormat.createAndConfigureWriterSettings().getHeaders()); @@ -79,18 +79,17 @@ public final class UniVocityTsvDataFormatTest { @Test public void shouldConfigureHeaders() { - UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat() - .setHeaders(new String[] { "A", "B", "C" }); + UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat(); + dataFormat.setHeaders("A,B,C"); - assertArrayEquals(new String[] { "A", "B", "C" }, dataFormat.getHeaders()); assertArrayEquals(new String[] { "A", "B", "C" }, dataFormat.createAndConfigureWriterSettings().getHeaders()); assertArrayEquals(new String[] { "A", "B", "C" }, dataFormat.createAndConfigureParserSettings().getHeaders()); } @Test public void shouldConfigureHeaderExtractionEnabled() { - UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat() - .setHeaderExtractionEnabled(true); + UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat(); + dataFormat.setHeaderExtractionEnabled(true); assertTrue(dataFormat.getHeaderExtractionEnabled()); assertTrue(dataFormat.createAndConfigureParserSettings().isHeaderExtractionEnabled()); @@ -98,8 +97,8 @@ public final class UniVocityTsvDataFormatTest { @Test public void shouldConfigureNumberOfRecordsToRead() { - UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat() - .setNumberOfRecordsToRead(42); + UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat(); + dataFormat.setNumberOfRecordsToRead(42); assertEquals(Integer.valueOf(42), dataFormat.getNumberOfRecordsToRead()); assertEquals(42, dataFormat.createAndConfigureParserSettings().getNumberOfRecordsToRead()); @@ -107,8 +106,8 @@ public final class UniVocityTsvDataFormatTest { @Test public void shouldConfigureEmptyValue() { - UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat() - .setEmptyValue("empty"); + UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat(); + dataFormat.setEmptyValue("empty"); assertEquals("empty", dataFormat.getEmptyValue()); assertEquals("empty", dataFormat.createAndConfigureWriterSettings().getEmptyValue()); @@ -116,8 +115,8 @@ public final class UniVocityTsvDataFormatTest { @Test public void shouldConfigureLineSeparator() { - UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat() - .setLineSeparator("ls"); + UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat(); + dataFormat.setLineSeparator("ls"); assertEquals("ls", dataFormat.getLineSeparator()); assertEquals("ls", dataFormat.createAndConfigureWriterSettings().getFormat().getLineSeparatorString()); @@ -126,8 +125,8 @@ public final class UniVocityTsvDataFormatTest { @Test public void shouldConfigureNormalizedLineSeparator() { - UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat() - .setNormalizedLineSeparator('n'); + UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat(); + dataFormat.setNormalizedLineSeparator('n'); assertEquals(Character.valueOf('n'), dataFormat.getNormalizedLineSeparator()); assertEquals('n', dataFormat.createAndConfigureWriterSettings().getFormat().getNormalizedNewline()); @@ -136,8 +135,8 @@ public final class UniVocityTsvDataFormatTest { @Test public void shouldConfigureComment() { - UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat() - .setComment('c'); + UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat(); + dataFormat.setComment('c'); assertEquals(Character.valueOf('c'), dataFormat.getComment()); assertEquals('c', dataFormat.createAndConfigureWriterSettings().getFormat().getComment()); @@ -146,24 +145,24 @@ public final class UniVocityTsvDataFormatTest { @Test public void shouldConfigureLazyLoad() { - UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat() - .setLazyLoad(true); + UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat(); + dataFormat.setLazyLoad(true); assertTrue(dataFormat.isLazyLoad()); } @Test public void shouldConfigureAsMap() { - UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat() - .setAsMap(true); + UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat(); + dataFormat.setAsMap(true); assertTrue(dataFormat.isAsMap()); } @Test public void shouldConfigureEscapeChar() { - UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat() - .setEscapeChar('e'); + UniVocityTsvDataFormat dataFormat = new UniVocityTsvDataFormat(); + dataFormat.setEscapeChar('e'); assertEquals(Character.valueOf('e'), dataFormat.getEscapeChar()); assertEquals('e', dataFormat.createAndConfigureWriterSettings().getFormat().getEscapeChar()); diff --git a/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityTsvDataFormatUnmarshalTest.java b/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityTsvDataFormatUnmarshalTest.java index 870b022ca62..b1c886e5f4d 100644 --- a/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityTsvDataFormatUnmarshalTest.java +++ b/components/camel-univocity-parsers/src/test/java/org/apache/camel/dataformat/univocity/UniVocityTsvDataFormatUnmarshalTest.java @@ -157,27 +157,28 @@ public final class UniVocityTsvDataFormatUnmarshalTest extends CamelTestSupport // Default reading of TSV tests.put("default", new UniVocityTsvDataFormat()); - // Reading TSV as Map - tests.put("map", new UniVocityTsvDataFormat() - .setAsMap(true) - .setHeaderExtractionEnabled(true)); + var df = new UniVocityTsvDataFormat(); + df.setAsMap(true); + df.setHeaderExtractionEnabled(true); + tests.put("map", df); - // Reading TSV as Map with specific headers - tests.put("mapWithHeaders", new UniVocityTsvDataFormat() - .setAsMap(true) - .setHeaders(new String[] { "A", "B", "C" })); + df = new UniVocityTsvDataFormat(); + df.setAsMap(true); + df.setHeaders("A,B,C"); + tests.put("mapWithHeaders", df); - // Reading TSV using an iterator - tests.put("lazy", new UniVocityTsvDataFormat() - .setLazyLoad(true)); + df = new UniVocityTsvDataFormat(); + df.setLazyLoad(true); + tests.put("lazy", df); // Reading TSV using advanced configuration - tests.put("advanced", new UniVocityTsvDataFormat() - .setNullValue("N/A") - .setIgnoreLeadingWhitespaces(true) - .setIgnoreTrailingWhitespaces(false) - .setComment('!') - .setSkipEmptyLines(true)); + df = new UniVocityTsvDataFormat(); + df.setNullValue("N/A"); + df.setIgnoreLeadingWhitespaces(true); + df.setIgnoreTrailingWhitespaces(false); + df.setComment('!'); + df.setSkipEmptyLines(true); + tests.put("advanced", df); return new RouteBuilder() { @Override diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/dataformat/UniVocityAbstractDataFormatReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/dataformat/UniVocityAbstractDataFormatReifier.java index f5b7c5a53fc..4a49c981305 100644 --- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/dataformat/UniVocityAbstractDataFormatReifier.java +++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/dataformat/UniVocityAbstractDataFormatReifier.java @@ -16,9 +16,8 @@ */ package org.apache.camel.reifier.dataformat; -import java.util.ArrayList; -import java.util.List; import java.util.Map; +import java.util.StringJoiner; import org.apache.camel.CamelContext; import org.apache.camel.model.DataFormatDefinition; @@ -49,21 +48,20 @@ public class UniVocityAbstractDataFormatReifier<T extends UniVocityAbstractDataF properties.put("asMap", definition.getAsMap()); } - /** - * Gets only the headers with non-null and non-empty names. It returns {@code null} if there's no such headers. - * - * @return The headers with non-null and non-empty names - */ - private String[] getValidHeaderNames() { + private String getValidHeaderNames() { if (definition.getHeaders() == null) { return null; } - List<String> names = new ArrayList<>(definition.getHeaders().size()); + StringJoiner sj = new StringJoiner(","); for (UniVocityHeader header : definition.getHeaders()) { if (header.getName() != null && !header.getName().isEmpty()) { - names.add(header.getName()); + sj.add(header.getName()); } } - return names.isEmpty() ? null : names.toArray(new String[0]); + if (sj.length() > 0) { + return sj.toString(); + } else { + return null; + } } } diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/dataformat/UniVocityFixedWidthDataFormatReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/dataformat/UniVocityFixedWidthDataFormatReifier.java index 86cbcfeb86f..f90a0f963f9 100644 --- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/dataformat/UniVocityFixedWidthDataFormatReifier.java +++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/dataformat/UniVocityFixedWidthDataFormatReifier.java @@ -17,6 +17,7 @@ package org.apache.camel.reifier.dataformat; import java.util.Map; +import java.util.StringJoiner; import org.apache.camel.CamelContext; import org.apache.camel.model.DataFormatDefinition; @@ -38,16 +39,13 @@ public class UniVocityFixedWidthDataFormatReifier extends UniVocityAbstractDataF properties.put("padding", definition.getPadding()); } - private int[] getFieldLengths() { + private String getFieldLengths() { if (definition.getHeaders() != null) { - int i = 0; - int[] arr = new int[definition.getHeaders().size()]; + StringJoiner sj = new StringJoiner(","); for (UniVocityHeader header : definition.getHeaders()) { - String len = header.getLength(); - int num = Integer.parseInt(len); - arr[i++] = num; + sj.add(header.getLength()); } - return arr; + return sj.toString(); } else { return null; }
