camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1326972 - in /camel/branches/camel-2.9.x: ./ components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/ components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/ components/camel-bindy/src/test/java/or...
Date Tue, 17 Apr 2012 07:41:39 GMT
Author: davsclaus
Date: Tue Apr 17 07:41:38 2012
New Revision: 1326972

URL: http://svn.apache.org/viewvc?rev=1326972&view=rev
Log:
CAMEL-5018 support the set the default value on the field of bindy CVS

Modified:
    camel/branches/camel-2.9.x/   (props changed)
    camel/branches/camel-2.9.x/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java
    camel/branches/camel-2.9.x/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/DataField.java
    camel/branches/camel-2.9.x/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvUnmarshallTest.java
    camel/branches/camel-2.9.x/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/simple/oneclass/Order.java

Propchange: camel/branches/camel-2.9.x/
------------------------------------------------------------------------------
  Merged /camel/trunk:r1292164

Propchange: camel/branches/camel-2.9.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: camel/branches/camel-2.9.x/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java?rev=1326972&r1=1326971&r2=1326972&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java
(original)
+++ camel/branches/camel-2.9.x/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java
Tue Apr 17 07:41:38 2012
@@ -200,7 +200,11 @@ public class BindyCsvFactory extends Bin
                     throw new IllegalArgumentException("Parsing error detected for field
defined at the position: " + pos + ", line: " + line, e);
                 }
             } else {
-                value = getDefaultValueForPrimitive(field.getType());
+                if (!dataField.defaultValue().isEmpty()) {
+                    value = format.parse(dataField.defaultValue());
+                } else {
+                    value = getDefaultValueForPrimitive(field.getType());
+                }
             }
 
             field.set(modelField, value);
@@ -211,14 +215,14 @@ public class BindyCsvFactory extends Bin
 
         LOG.debug("Counter mandatory fields: {}", counterMandatoryFields);
 
-        if (pos < totalFields) {
-            throw new IllegalArgumentException("Some fields are missing (optional or mandatory),
line: " + line);
-        }
-
         if (counterMandatoryFields < numberMandatoryFields) {
             throw new IllegalArgumentException("Some mandatory fields are missing, line:
" + line);
         }
 
+        if (pos < totalFields) {
+            setDefaultValuesForFields(model);
+        }
+
     }
 
     @SuppressWarnings("unchecked")
@@ -559,6 +563,29 @@ public class BindyCsvFactory extends Bin
             }
         }
     }
+    /**
+     * Set the default values for the non defined fields.
+     * @param The model which has its default fields set.
+     * @throws IllegalAccessException if the underlying fields are inaccessible
+     * @throws Exception In case the field cannot be parsed
+     */
+    private void setDefaultValuesForFields(final Map<String, Object> model) throws
IllegalAccessException,
+        Exception {
+        // Set the default values, if defined
+        for (int i = 1; i <= dataFields.size(); i++) {
+            Field field = annotatedFields.get(i);
+            field.setAccessible(true);
+            DataField dataField = dataFields.get(i);
+            Object modelField = model.get(field.getDeclaringClass().getName());
+            if (field.get(modelField) == null && !dataField.defaultValue().isEmpty())
{
+                String pattern = dataField.pattern();
+                Format<?> format = FormatFactory.getFormat(field.getType(), pattern,
getLocale(),
+                                                                 dataField.precision());
+                Object value = format.parse(dataField.defaultValue());
+                field.set(modelField, value);
+            }
+        }
+    }
 
     /**
      * Find the separator used to delimit the CSV fields

Modified: camel/branches/camel-2.9.x/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/DataField.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/DataField.java?rev=1326972&r1=1326971&r2=1326972&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/DataField.java
(original)
+++ camel/branches/camel-2.9.x/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/DataField.java
Tue Apr 17 07:41:38 2012
@@ -96,4 +96,9 @@ public @interface DataField {
      * Indicates to clip data in the field if it exceeds the allowed length when using fixed
length.
      */
     boolean clip() default false;
+    
+    /**
+     * Field's default value in case no value is set 
+     */
+    String defaultValue() default "";
 }

Modified: camel/branches/camel-2.9.x/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvUnmarshallTest.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvUnmarshallTest.java?rev=1326972&r1=1326971&r2=1326972&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvUnmarshallTest.java
(original)
+++ camel/branches/camel-2.9.x/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvUnmarshallTest.java
Tue Apr 17 07:41:38 2012
@@ -16,6 +16,9 @@
  */
 package org.apache.camel.dataformat.bindy.csv;
 
+import java.util.List;
+import java.util.Map;
+
 import org.apache.camel.EndpointInject;
 import org.apache.camel.Exchange;
 import org.apache.camel.LoggingLevel;
@@ -24,6 +27,7 @@ import org.apache.camel.ProducerTemplate
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.dataformat.bindy.format.FormatException;
+import org.apache.camel.dataformat.bindy.model.simple.oneclass.Order;
 import org.apache.camel.processor.interceptor.Tracer;
 import org.apache.camel.test.junit4.TestSupport;
 import org.junit.Test;
@@ -31,7 +35,10 @@ import org.springframework.test.annotati
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
 
+
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
 
 @ContextConfiguration
 public class BindySimpleCsvUnmarshallTest extends AbstractJUnit4SpringContextTests {
@@ -100,6 +107,32 @@ public class BindySimpleCsvUnmarshallTes
 
     }
     
+    @SuppressWarnings("unchecked")
+    @Test
+    @DirtiesContext
+    public void testUnMarshallMessageWithMissingFields() throws Exception {
+       
+        // We suppress the firstName field of the first record
+        expected = "01,,,Cartier,ISIN,BE12345678,SELL,,1500,EUR\r\n" + "02,A1,,Preud'Homme,ISIN,XD12345678,BUY,,2500,USD,08-01-2009\r\n"
+            + "03,A2,Jacques,,,BE12345678,SELL,,1500,EUR,08-01-2009\r\n" + "04,A3,Michel,Dupond,,,BUY,,2500,USD,08-01-2009\r\n"
+            + "05,A4,Annie,Dutronc,ISIN,BE12345678,,,1500,EUR,08-01-2009\r\n" + "06,A5,Andr"
+ "\uc3a9" + ",Rieux,ISIN,XD12345678,SELL,Share,,USD,08-01-2009\r\n"
+            + "07,A6,Myl" + "\uc3a8" + "ne,Farmer,ISIN,BE12345678,BUY,1500,,,08-01-2009\r\n"
+ "08,A7,Eva,Longoria,ISIN,XD12345678,SELL,Share,2500,USD,\r\n"
+            + ",,,D,,BE12345678,SELL,,,,08-01-2009\r\n" + ",,,D,ISIN,BE12345678,,,,,08-01-2009\r\n"
+ ",,,D,ISIN,LU123456789,,,,,\r\n"
+            + "10,A8,Pauline,M,ISIN,XD12345678,SELL,Share,2500,USD,08-01-2009\r\n" + "10,A9,Pauline,M,ISIN,XD12345678,BUY,Share,2500.45";
+
+        template.sendBody(expected);
+
+        List<Map<String, Order>> orders = (List<Map<String, Order>>)
result.getExchanges().get(0).getIn().getBody();
+        
+        result.expectedMessageCount(1);
+        result.assertIsSatisfied();
+
+        assertNotNull(orders);
+        // As the @DataField defines a default value for the firstName, the
+        // value might not be empty
+        assertFalse(orders.get(0).get(Order.class.getName()).getFirstName().isEmpty());
+    }
+    
     public static class ContextConfig extends RouteBuilder {
         BindyCsvDataFormat camelDataFormat = new BindyCsvDataFormat("org.apache.camel.dataformat.bindy.model.simple.oneclass");
 

Modified: camel/branches/camel-2.9.x/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/simple/oneclass/Order.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/simple/oneclass/Order.java?rev=1326972&r1=1326971&r2=1326972&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/simple/oneclass/Order.java
(original)
+++ camel/branches/camel-2.9.x/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/simple/oneclass/Order.java
Tue Apr 17 07:41:38 2012
@@ -31,7 +31,7 @@ public class Order {
     @DataField(pos = 2)
     private String clientNr;
 
-    @DataField(pos = 3)
+    @DataField(pos = 3, defaultValue = "Joe")
     private String firstName;
 
     @DataField(pos = 4)



Mime
View raw message