Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 6555B200C1F for ; Sat, 18 Feb 2017 10:33:05 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 64248160B63; Sat, 18 Feb 2017 09:33:05 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 56C2B160B71 for ; Sat, 18 Feb 2017 10:33:04 +0100 (CET) Received: (qmail 78334 invoked by uid 500); 18 Feb 2017 09:33:03 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 78185 invoked by uid 99); 18 Feb 2017 09:33:03 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 18 Feb 2017 09:33:03 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 4B5B4DFC63; Sat, 18 Feb 2017 09:33:03 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: davsclaus@apache.org To: commits@camel.apache.org Date: Sat, 18 Feb 2017 09:33:05 -0000 Message-Id: <500a54596322499cb756a94210ec682c@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [3/4] camel git commit: [CAMEL-10853] Add support for some more properties archived-at: Sat, 18 Feb 2017 09:33:05 -0000 [CAMEL-10853] Add support for some more properties Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/05a7d0c3 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/05a7d0c3 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/05a7d0c3 Branch: refs/heads/camel-2.18.x Commit: 05a7d0c395bb2a263d106211bfcb7be918de5162 Parents: d035f77 Author: Christian Ribeaud Authored: Fri Feb 17 18:55:18 2017 +0100 Committer: Claus Ibsen Committed: Sat Feb 18 10:28:54 2017 +0100 ---------------------------------------------------------------------- .../camel/model/dataformat/CsvDataFormat.java | 68 +++++++++++++++ .../model/dataformat/CsvDataFormatTest.java | 89 +++++++++++++++++++ .../camel/dataformat/csv/CsvDataFormat.java | 90 ++++++++++++++++++++ .../camel/dataformat/csv/CsvDataFormatTest.java | 83 ++++++++++++++++++ 4 files changed, 330 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/05a7d0c3/camel-core/src/main/java/org/apache/camel/model/dataformat/CsvDataFormat.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/dataformat/CsvDataFormat.java b/camel-core/src/main/java/org/apache/camel/model/dataformat/CsvDataFormat.java index 91fa1e7..5e35f62 100644 --- a/camel-core/src/main/java/org/apache/camel/model/dataformat/CsvDataFormat.java +++ b/camel-core/src/main/java/org/apache/camel/model/dataformat/CsvDataFormat.java @@ -17,6 +17,7 @@ package org.apache.camel.model.dataformat; import java.util.List; +import java.util.stream.Stream; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; @@ -78,6 +79,13 @@ public class CsvDataFormat extends DataFormatDefinition { private Boolean skipHeaderRecord; @XmlAttribute private String quoteMode; + @XmlAttribute + private Boolean ignoreHeaderCase; + @XmlAttribute + private Boolean trim; + @XmlAttribute + private Boolean trailingDelimiter; + // Unmarshall options @XmlAttribute private Boolean lazyLoad; @@ -163,6 +171,16 @@ public class CsvDataFormat extends DataFormatDefinition { if (quoteMode != null) { setProperty(camelContext, dataFormat, "quoteMode", quoteMode); } + Stream.of("trim", "ignoreHeaderCase", "trailingDelimiter") + .forEach(item -> { + try { + setProperty(camelContext, dataFormat, item, + CsvDataFormat.class.getDeclaredField(item).get(this)); + } catch (Exception e) { + // Not expected to happen + throw new AssertionError(e); + } + }); // Unmarshall options if (lazyLoad != null) { @@ -439,4 +457,54 @@ public class CsvDataFormat extends DataFormatDefinition { this.recordConverterRef = recordConverterRef; } + /** + * Sets whether or not to trim leading and trailing blanks. + *

+ * If {@code null} then the default value of the format used. + *

+ * + * @param trim whether or not to trim leading and trailing blanks. + * null value allowed. + */ + public void setTrim(Boolean trim) { + this.trim = trim; + } + + public Boolean getTrim() { + return trim; + } + + /** + * Sets whether or not to ignore case when accessing header names. + *

+ * If {@code null} then the default value of the format used. + *

+ * + * @param ignoreHeaderCase whether or not to ignore case when accessing header names. + * null value allowed. + */ + public void setIgnoreHeaderCase(Boolean ignoreHeaderCase) { + this.ignoreHeaderCase = ignoreHeaderCase; + } + + public Boolean getIgnoreHeaderCase() { + return ignoreHeaderCase; + } + + /** + * Sets whether or not to add a trailing delimiter. + *

+ * If {@code null} then the default value of the format used. + *

+ * + * @param trailingDelimiter whether or not to add a trailing delimiter. + */ + public void setTrailingDelimiter(Boolean trailingDelimiter) { + this.trailingDelimiter = trailingDelimiter; + } + + public Boolean getTrailingDelimiter() { + return trailingDelimiter; + } + } http://git-wip-us.apache.org/repos/asf/camel/blob/05a7d0c3/camel-core/src/test/java/org/apache/camel/model/dataformat/CsvDataFormatTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/model/dataformat/CsvDataFormatTest.java b/camel-core/src/test/java/org/apache/camel/model/dataformat/CsvDataFormatTest.java new file mode 100644 index 0000000..6831b67 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/model/dataformat/CsvDataFormatTest.java @@ -0,0 +1,89 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.model.dataformat; + + +import java.io.InputStream; +import java.io.OutputStream; +import java.util.BitSet; + +import javax.xml.bind.annotation.XmlAttribute; + +import org.apache.camel.Exchange; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.spi.DataFormat; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Test cases for corresponding class {@link CsvDataFormat}. + */ +public class CsvDataFormatTest { + + @Test + public void testConfigureDataFormatDataFormatCamelContext() { + CsvDataFormat csvDataFormat = new CsvDataFormat(); + csvDataFormat.setIgnoreHeaderCase(true); + csvDataFormat.setTrim(true); + csvDataFormat.setTrailingDelimiter(true); + MyDataFormat dataFormat = new MyDataFormat(); + DefaultCamelContext camelContext = new DefaultCamelContext(); + csvDataFormat.configureDataFormat(dataFormat, camelContext); + assertEquals(3, dataFormat.bitSet.cardinality()); + csvDataFormat.setIgnoreHeaderCase(false); + csvDataFormat.setTrim(false); + csvDataFormat.setTrailingDelimiter(false); + csvDataFormat.configureDataFormat(dataFormat, camelContext); + assertEquals(dataFormat.bitSet.cardinality(), 0); + } + + // + // Helper classes + // + + static final class MyDataFormat implements DataFormat { + + final BitSet bitSet = new BitSet(); + + MyDataFormat() { + } + + public void setIgnoreHeaderCase(Boolean ignoreHeaderCase) { + bitSet.set(0, ignoreHeaderCase); + } + + public void setTrim(Boolean trim) { + bitSet.set(2, trim); + } + + public void setTrailingDelimiter(Boolean trailingDelimiter) { + bitSet.set(3, trailingDelimiter); + } + + @Override + public Object unmarshal(Exchange exchange, InputStream stream) throws Exception { + throw new UnsupportedOperationException(); + } + + @Override + public void marshal(Exchange exchange, Object graph, OutputStream stream) throws Exception { + throw new UnsupportedOperationException(); + } + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/05a7d0c3/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java ---------------------------------------------------------------------- diff --git a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java index 53476d6..41c9529 100644 --- a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java +++ b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java @@ -57,6 +57,9 @@ public class CsvDataFormat extends ServiceSupport implements DataFormat, DataFor private boolean recordSeparatorDisabled; private String recordSeparator; private Boolean skipHeaderRecord; + private Boolean trim; + private Boolean ignoreHeaderCase; + private Boolean trailingDelimiter; // Unmarshal options private boolean lazyLoad; @@ -159,6 +162,18 @@ public class CsvDataFormat extends ServiceSupport implements DataFormat, DataFor if (skipHeaderRecord != null) { answer = answer.withSkipHeaderRecord(skipHeaderRecord); } + + if (trim != null) { + answer = answer.withTrim(trim); + } + + if (ignoreHeaderCase != null) { + answer = answer.withIgnoreHeaderCase(ignoreHeaderCase); + } + + if (trailingDelimiter != null) { + answer = answer.withTrailingDelimiter(trailingDelimiter); + } return answer; } @@ -681,5 +696,80 @@ public class CsvDataFormat extends ServiceSupport implements DataFormat, DataFor } //endregion + /** + * Sets whether or not to trim leading and trailing blanks. + *

+ * If {@code null} then the default value of the format used. + *

+ * + * @param trim whether or not to trim leading and trailing blanks. + * null value allowed. + * @return Current {@code CsvDataFormat}, fluent API. + */ + public CsvDataFormat setTrim(Boolean trim) { + this.trim = trim; + return this; + } + /** + * Indicates whether or not to trim leading and trailing blanks. + * + * @return {@link Boolean#TRUE} if leading and trailing blanks should be + * trimmed. {@link Boolean#FALSE} otherwise. Could return + * null if value has NOT been set. + */ + public Boolean getTrim() { + return trim; + } + + /** + * Sets whether or not to ignore case when accessing header names. + *

+ * If {@code null} then the default value of the format used. + *

+ * + * @param ignoreHeaderCase whether or not to ignore case when accessing header names. + * null value allowed. + * @return Current {@code CsvDataFormat}, fluent API. + */ + public CsvDataFormat setIgnoreHeaderCase(Boolean ignoreHeaderCase) { + this.ignoreHeaderCase = ignoreHeaderCase; + return this; + } + + /** + * Indicates whether or not to ignore case when accessing header names. + * + * @return {@link Boolean#TRUE} if case should be ignored when accessing + * header name. {@link Boolean#FALSE} otherwise. Could return + * null if value has NOT been set. + */ + public Boolean getIgnoreHeaderCase() { + return ignoreHeaderCase; + } + + /** + * Sets whether or not to add a trailing delimiter. + *

+ * If {@code null} then the default value of the format used. + *

+ * + * @param trailingDelimiter whether or not to add a trailing delimiter. + * @return Current {@code CsvDataFormat}, fluent API. + */ + public CsvDataFormat setTrailingDelimiter(Boolean trailingDelimiter) { + this.trailingDelimiter = trailingDelimiter; + return this; + } + + /** + * Indicates whether or not to add a trailing delimiter. + * + * @return {@link Boolean#TRUE} if a trailing delimiter should be added. + * {@link Boolean#FALSE} otherwise. Could return null + * if value has NOT been set. + */ + public Boolean getTrailingDelimiter() { + return trailingDelimiter; + } } http://git-wip-us.apache.org/repos/asf/camel/blob/05a7d0c3/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvDataFormatTest.java ---------------------------------------------------------------------- diff --git a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvDataFormatTest.java b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvDataFormatTest.java index 1d6e7b3..dc94686 100644 --- a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvDataFormatTest.java +++ b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvDataFormatTest.java @@ -435,4 +435,87 @@ public class CsvDataFormatTest { // Properly used (it doesn't modify the format) assertEquals(CSVFormat.DEFAULT, dataFormat.getActiveFormat()); } + + @Test + public void testTrim() { + // Set to TRUE + CsvDataFormat dataFormat = new CsvDataFormat().setTrim(true); + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertEquals(Boolean.TRUE, dataFormat.getTrim()); + // Properly used + assertTrue(dataFormat.getActiveFormat().getTrim()); + + // NOT set + dataFormat = new CsvDataFormat(); + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertEquals(null, dataFormat.getTrim()); + // Properly used + assertFalse(dataFormat.getActiveFormat().getTrim()); + + // Set to false + dataFormat = new CsvDataFormat().setTrim(false); + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertEquals(Boolean.FALSE, dataFormat.getTrim()); + // Properly used + assertFalse(dataFormat.getActiveFormat().getTrim()); + + } + + @Test + public void testIgnoreHeaderCase() { + // Set to TRUE + CsvDataFormat dataFormat = new CsvDataFormat().setIgnoreHeaderCase(true); + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertEquals(Boolean.TRUE, dataFormat.getIgnoreHeaderCase()); + // Properly used + assertTrue(dataFormat.getActiveFormat().getIgnoreHeaderCase()); + + // NOT set + dataFormat = new CsvDataFormat(); + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertEquals(null, dataFormat.getIgnoreHeaderCase()); + // Properly used + assertFalse(dataFormat.getActiveFormat().getIgnoreHeaderCase()); + + // Set to false + dataFormat = new CsvDataFormat().setIgnoreHeaderCase(false); + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertEquals(Boolean.FALSE, dataFormat.getIgnoreHeaderCase()); + // Properly used + assertFalse(dataFormat.getActiveFormat().getIgnoreHeaderCase()); + } + + @Test + public void testTrailingDelimiter() { + // Set to TRUE + CsvDataFormat dataFormat = new CsvDataFormat().setTrailingDelimiter(true); + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertEquals(Boolean.TRUE, dataFormat.getTrailingDelimiter()); + // Properly used + assertTrue(dataFormat.getActiveFormat().getTrailingDelimiter()); + + // NOT set + dataFormat = new CsvDataFormat(); + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertEquals(null, dataFormat.getTrailingDelimiter()); + // Properly used + assertFalse(dataFormat.getActiveFormat().getTrailingDelimiter()); + + // Set to false + dataFormat = new CsvDataFormat().setTrailingDelimiter(false); + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertEquals(Boolean.FALSE, dataFormat.getTrailingDelimiter()); + // Properly used + assertFalse(dataFormat.getActiveFormat().getTrailingDelimiter()); + } + }