You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2025/02/14 19:40:54 UTC
(camel) branch camel-4.10.x updated: CAMEL-21734: camel-csv - Allow to configurer header via CsvFormat reference (#17091)
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-4.10.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.10.x by this push:
new 935015f1ab9 CAMEL-21734: camel-csv - Allow to configurer header via CsvFormat reference (#17091)
935015f1ab9 is described below
commit 935015f1ab97e7d933d1f0b61816b0346cfb0c3a
Author: Benjamin Graf <gr...@users.noreply.github.com>
AuthorDate: Sun Feb 9 16:52:57 2025 +0100
CAMEL-21734: camel-csv - Allow to configurer header via CsvFormat reference (#17091)
---
.../apache/camel/dataformat/csv/CsvMarshaller.java | 8 +-
.../csv/CsvMarshalHeaderFromCsvFormatTest.java | 119 +++++++++++++++++++++
2 files changed, 123 insertions(+), 4 deletions(-)
diff --git a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvMarshaller.java b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvMarshaller.java
index 32e095eba13..1563af2e701 100644
--- a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvMarshaller.java
+++ b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvMarshaller.java
@@ -53,15 +53,15 @@ public abstract class CsvMarshaller {
public static CsvMarshaller create(CSVFormat format, CsvDataFormat dataFormat) {
org.apache.camel.util.ObjectHelper.notNull(format, "CSV format");
org.apache.camel.util.ObjectHelper.notNull(dataFormat, "CSV data format");
+ // Get header columns before potential clearing
+ String[] headers = format.getHeader();
// If we don't want the header record, clear it
if (format.getSkipHeaderRecord()) {
format = format.withHeader((String[]) null);
}
- String header = dataFormat.getHeader();
- if (header != null && !header.isEmpty()) {
- String[] columns = header.split(",");
- return new FixedColumnsMarshaller(format, columns);
+ if (headers != null && headers.length != 0) {
+ return new FixedColumnsMarshaller(format, headers);
}
return new DynamicColumnsMarshaller(format);
}
diff --git a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalHeaderFromCsvFormatTest.java b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalHeaderFromCsvFormatTest.java
new file mode 100644
index 00000000000..12ee7a3c400
--- /dev/null
+++ b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalHeaderFromCsvFormatTest.java
@@ -0,0 +1,119 @@
+/*
+ * 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.dataformat.csv;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Stream;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Produce;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.apache.commons.csv.CSVFormat;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * <b>Camel</b> based test cases for {@link org.apache.camel.dataformat.csv.CsvDataFormat}.
+ */
+public class CsvMarshalHeaderFromCsvFormatTest extends CamelTestSupport {
+
+ @TempDir
+ File folder;
+
+ @Produce("direct:start")
+ ProducerTemplate producerTemplate;
+
+ File outputFile;
+
+ @Override
+ protected void setupResources() {
+ outputFile = new File(folder, "output.csv");
+ }
+
+ @Test
+ void testSendBody() throws IOException {
+ Map<String, String> body = new LinkedHashMap<>();
+ body.put("first_name", "John");
+ body.put("last_name", "Doe");
+ String fileName = outputFile.getName();
+ assertEquals("output.csv", fileName);
+ producerTemplate.sendBodyAndHeader(body, Exchange.FILE_NAME, fileName);
+ body = new LinkedHashMap<>();
+ body.put("first_name", "Max");
+ body.put("last_name", "Mustermann");
+ producerTemplate.sendBodyAndHeader(body, Exchange.FILE_NAME, fileName);
+ try (Stream<String> stream = Files.lines(Paths.get(outputFile.toURI()))
+ .filter(l -> !l.isBlank())) {
+ List<String> lines = stream.toList();
+ // We got twice the headers... :(
+ assertEquals(4, lines.size());
+ }
+ }
+
+ @Test
+ void testSendBodyWithList() throws IOException {
+ List<List<String>> body = Collections.singletonList(Arrays.asList("John", "Doe"));
+ String fileName = outputFile.getName();
+ assertEquals("output.csv", fileName);
+ producerTemplate.sendBodyAndHeader(body, Exchange.FILE_NAME, fileName);
+ body = Collections.singletonList(Arrays.asList("Max", "Mustermann"));
+ producerTemplate.sendBodyAndHeader(body, Exchange.FILE_NAME, fileName);
+ try (Stream<String> stream = Files.lines(Paths.get(outputFile.toURI()))
+ .filter(l -> !l.isBlank())) {
+ List<String> lines = stream.toList();
+ // We got twice the headers... :(
+ assertEquals(4, lines.size());
+ }
+ }
+
+ @Override
+ protected RoutesBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ String uri
+ = String.format("file:%s?charset=utf-8&fileExist=Append", outputFile.getParentFile().getAbsolutePath());
+ from("direct:start").marshal(createCsvDataFormat()).to(uri);
+ }
+ };
+ }
+
+ private static CsvDataFormat createCsvDataFormat() {
+ CSVFormat format = CSVFormat.Builder.create()
+ .setDelimiter('\t')
+ .setTrim(true)
+ .setIgnoreSurroundingSpaces(true)
+ .setHeader("first_name", "last_name")
+ .get();
+ CsvDataFormat dataFormat = new CsvDataFormat();
+ dataFormat.setFormat(format);
+ return dataFormat;
+ }
+}