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 2019/10/29 07:02:08 UTC

[camel] branch master updated: CAMEL-14085: Update camel-bindy to document that fixed length is based on number of characters and not number of bytes.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 93fc144  CAMEL-14085: Update camel-bindy to document that fixed length is based on number of characters and not number of bytes.
93fc144 is described below

commit 93fc144bf06f8d16e80f47ca4aeb9be41eab08f1
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Oct 29 08:01:43 2019 +0100

    CAMEL-14085: Update camel-bindy to document that fixed length is based on number of characters and not number of bytes.
---
 .../src/main/docs/bindy-dataformat.adoc            |   8 +-
 .../dataformat/bindy/annotation/DataField.java     |   2 +-
 .../bindy/annotation/FixedLengthRecord.java        |   2 +-
 .../fixed/multibytes/BindyMultiBytesTest.java      | 101 +++++++++++++++++++++
 4 files changed, 107 insertions(+), 6 deletions(-)

diff --git a/components/camel-bindy/src/main/docs/bindy-dataformat.adoc b/components/camel-bindy/src/main/docs/bindy-dataformat.adoc
index 30d01e7..dd29285 100644
--- a/components/camel-bindy/src/main/docs/bindy-dataformat.adoc
+++ b/components/camel-bindy/src/main/docs/bindy-dataformat.adoc
@@ -463,7 +463,7 @@ from 1 to ... - See the position parameter.
 
 |pattern |string |optional - default value = "" - will be used to format Decimal, Date,
 
-|length |int |optional - represents the length of the field for fixed length format
+|length |int |optional - represents the length of the field (number of characters) for fixed length format
 
 |precision |int |optional - represents the precision to be used when the Decimal number
 will be formatted/parsed
@@ -762,8 +762,8 @@ This attribute is only applicable to optional fields.
 
 The FixedLengthRecord annotation is used to identified the root class of
 the model. It represents a record = a line of a file/message containing
-data fixed length formatted and can be linked to several children model
-classes. This format is a bit particular beause data of a field can be
+data fixed length (number of characters) formatted and can be linked to several children model
+classes. This format is a bit particular because data of a field can be
 aligned to the right or to the left. +
  When the size of the data does not fill completely the length of the
 field, we can then add 'padd' characters.
@@ -793,7 +793,7 @@ provided line delimiter as "WINDOWS" unless any other value is provided
 
 |paddingChar |char |mandatory - default value = ' '
 
-|length |int |mandatory = size of the fixed length record
+|length |int |mandatory = size of the fixed length record (number of characters)
 
 |hasHeader |boolean |optional - Indicates that the record(s) of this type may
 be preceded by a single header record at the beginning of the file /
diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/DataField.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/DataField.java
index 44cd1a2..2efc563 100644
--- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/DataField.java
+++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/DataField.java
@@ -65,7 +65,7 @@ public @interface DataField {
     String timezone() default "";
 
     /**
-     * Length of the data block if the record is set to a fixed length
+     * Length of the data block (number of characters) if the record is set to a fixed length
      */
     int length() default 0;
     
diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/FixedLengthRecord.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/FixedLengthRecord.java
index 8539883..cff27fc 100644
--- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/FixedLengthRecord.java
+++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/FixedLengthRecord.java
@@ -64,7 +64,7 @@ public @interface FixedLengthRecord {
     char paddingChar() default ' ';
     
     /**
-     * The fixed length of the record. It means that the record will always be that long padded with {#paddingChar()}'s
+     * The fixed length of the record (number of characters). It means that the record will always be that long padded with {#paddingChar()}'s
      * @return the length of the record.
      */
     int length() default 0;
diff --git a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fixed/multibytes/BindyMultiBytesTest.java b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fixed/multibytes/BindyMultiBytesTest.java
new file mode 100644
index 0000000..f93d7f6
--- /dev/null
+++ b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fixed/multibytes/BindyMultiBytesTest.java
@@ -0,0 +1,101 @@
+/*
+ * 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.bindy.fixed.multibytes;
+
+import java.nio.charset.StandardCharsets;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Exchange;
+import org.apache.camel.Produce;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.dataformat.bindy.annotation.DataField;
+import org.apache.camel.dataformat.bindy.annotation.FixedLengthRecord;
+import org.apache.camel.dataformat.bindy.fixed.BindyFixedLengthDataFormat;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class BindyMultiBytesTest extends CamelTestSupport {
+
+    @Produce("direct:in")
+    private ProducerTemplate producer;
+
+    @EndpointInject("mock:result")
+    private MockEndpoint result;
+
+
+    // *************************************************************************
+    // TESTS
+    // *************************************************************************
+
+    /**
+     * Let's assume we want to read the content of a 10 bytes record from an UTF-8 encoded file.
+     * test string takes 10 bytes with 9 characters (2 char = 3 bytes content + padding).
+     * I assume to be able to read 9 char string from this 10 bytes fixed length record with bindy.
+     */
+    @Test
+    public void testMultiBytes() throws Exception {
+        String test = "a\u00DF        ";
+        assertEquals("Should be 10 length", 10, test.length());
+
+        byte[] testAsBytes = test.getBytes(StandardCharsets.UTF_8);
+        assertEquals("A\u00DF takes 11 bytes, because \u00DF takes 2", 11, testAsBytes.length);
+
+        result.expectedMessagesMatches(exchange -> test.equals(exchange.getIn().getBody(TestRecord.class).getField1()));
+
+        producer.sendBody(test);
+
+        result.assertIsSatisfied();
+    }
+
+    // *************************************************************************
+    // ROUTES
+    // *************************************************************************
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        RouteBuilder routeBuilder = new RouteBuilder() {
+
+            @Override
+            public void configure() throws Exception {
+                from("direct:in")
+                        .setHeader(Exchange.CHARSET_NAME, constant(StandardCharsets.UTF_8.name()))
+                        .unmarshal(new BindyFixedLengthDataFormat(TestRecord.class))
+                        .to("mock:result");
+            }
+        };
+
+        return routeBuilder;
+    }
+
+    @FixedLengthRecord(length = 10, paddingChar = ' ')
+    public static class TestRecord {
+
+        @DataField(pos = 1, length = 10)
+        private String field1;
+
+        public String getField1() {
+            return field1;
+        }
+
+        public void setField1(String field1) {
+            this.field1 = field1;
+        }
+
+    }
+}
\ No newline at end of file