camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cmoulli...@apache.org
Subject svn commit: r794245 - in /camel/trunk/components/camel-bindy/src: main/java/org/apache/camel/dataformat/bindy/ main/java/org/apache/camel/dataformat/bindy/annotation/ main/java/org/apache/camel/dataformat/bindy/csv/ test/data/ test/data3/ test/java/org...
Date Wed, 15 Jul 2009 12:09:01 GMT
Author: cmoulliard
Date: Wed Jul 15 12:09:00 2009
New Revision: 794245

URL: http://svn.apache.org/viewvc?rev=794245&view=rev
Log:
Add a new property in annotation DataField.java to define mandatory fields, generate exceptions
in BindyCsvDataFormat when record is empty in case of missing mandatory fields.

Added:
    camel/trunk/components/camel-bindy/src/test/data3/
    camel/trunk/components/camel-bindy/src/test/data3/csv.txt   (with props)
    camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMandatoryFieldsUnmarshallTest.java
  (with props)
Modified:
    camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractFactory.java
    camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java
    camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/DataField.java
    camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/csv/BindyCsvDataFormat.java
    camel/trunk/components/camel-bindy/src/test/data/csv.txt
    camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyComplexCsvUnmarshallTest.java
    camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvSkipFirstLineUnmarshallTest.java
    camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvUnmarshallDslTest.java
    camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvUnmarshallTest.java

Modified: camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractFactory.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractFactory.java?rev=794245&r1=794244&r2=794245&view=diff
==============================================================================
--- camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractFactory.java
(original)
+++ camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractFactory.java
Wed Jul 15 12:09:00 2009
@@ -17,14 +17,34 @@
 package org.apache.camel.dataformat.bindy;
 
 import java.lang.reflect.Field;
+import java.math.BigDecimal;
+import java.math.BigInteger;
 import java.text.NumberFormat;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.camel.dataformat.bindy.format.BigDecimalFormat;
+import org.apache.camel.dataformat.bindy.format.BigIntegerFormat;
+import org.apache.camel.dataformat.bindy.format.ByteFormat;
+import org.apache.camel.dataformat.bindy.format.BytePatternFormat;
+import org.apache.camel.dataformat.bindy.format.CharacterFormat;
+import org.apache.camel.dataformat.bindy.format.DatePatternFormat;
+import org.apache.camel.dataformat.bindy.format.DoubleFormat;
+import org.apache.camel.dataformat.bindy.format.DoublePatternFormat;
+import org.apache.camel.dataformat.bindy.format.FloatFormat;
+import org.apache.camel.dataformat.bindy.format.FloatPatternFormat;
+import org.apache.camel.dataformat.bindy.format.IntegerFormat;
+import org.apache.camel.dataformat.bindy.format.IntegerPatternFormat;
+import org.apache.camel.dataformat.bindy.format.LongFormat;
+import org.apache.camel.dataformat.bindy.format.LongPatternFormat;
+import org.apache.camel.dataformat.bindy.format.ShortFormat;
+import org.apache.camel.dataformat.bindy.format.ShortPatternFormat;
+import org.apache.camel.dataformat.bindy.format.StringFormat;
 import org.apache.camel.dataformat.bindy.util.AnnotationModelLoader;
 import org.apache.camel.spi.PackageScanClassResolver;
 import org.apache.camel.util.ObjectHelper;
@@ -162,6 +182,36 @@
         
         return nf;
     }
+    
+    public static Object getDefaultValueforPrimitive(Class<?> clazz) throws Exception
{
+        if (clazz == byte.class || clazz == Byte.class) {
+            return Byte.MIN_VALUE;
+        } else if (clazz == short.class || clazz == Short.class) {
+            return Short.MIN_VALUE;
+        } else if (clazz == int.class || clazz == Integer.class) {
+            return Integer.MIN_VALUE;
+        } else if (clazz == long.class || clazz == Long.class) {
+            return Long.MIN_VALUE;
+        } else if (clazz == float.class || clazz == Float.class) {
+            return Float.MIN_VALUE;
+        } else if (clazz == double.class || clazz == Double.class) {
+            return Double.MIN_VALUE;
+        } else if (clazz == BigDecimal.class) {
+            return BigDecimal.ZERO;
+        } else if (clazz == BigInteger.class) {
+            return BigInteger.ZERO;
+        } else if (clazz == String.class) {
+            return null;
+        } else if (clazz == Date.class) {
+            return null;
+        } else if (clazz == char.class || clazz == Character.class) {
+            return null;
+        } else if (clazz == boolean.class || clazz == Boolean.class) {
+        	return false;
+        } else {
+            throw new IllegalArgumentException("Can not find type corresponding : " + clazz.getCanonicalName());
+        }
+    }
 
     /**
      * Find the carriage return set

Modified: camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java?rev=794245&r1=794244&r2=794245&view=diff
==============================================================================
--- camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java
(original)
+++ camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java
Wed Jul 15 12:09:00 2009
@@ -24,6 +24,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
+import java.util.jar.JarException;
 
 import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
 import org.apache.camel.dataformat.bindy.annotation.DataField;
@@ -48,6 +49,9 @@
     private Map<Integer, DataField> dataFields = new LinkedHashMap<Integer, DataField>();
     private Map<Integer, Field> annotedFields = new LinkedHashMap<Integer, Field>();
     private Map<String, Integer> sections = new HashMap<String, Integer>();
+    private int numberOptionalFields = 0;
+    private int numberMandatoryFields = 0;
+    private int totalFields = 0;
 
     private String separator;
     private boolean skipFirstLine;
@@ -95,6 +99,13 @@
                         LOG.debug("Position defined in the class : " + cl.getName() + ",
position : "
                                   + dataField.pos() + ", Field : " + dataField.toString());
                     }
+                    
+                    if ( dataField.required() ) {
+                    	++numberMandatoryFields;
+                    } else {
+                    	++numberOptionalFields;
+                    }
+                    
                     dataFields.put(dataField.pos(), dataField);
                     annotedFields.put(dataField.pos(), field);
                 }
@@ -113,13 +124,75 @@
             if (!linkFields.isEmpty()) {
                 annotedLinkFields.put(cl.getName(), linkFields);
             }
+            
+            totalFields = numberMandatoryFields + numberOptionalFields;
+            
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Number of optional fields : " + numberOptionalFields);
+                LOG.debug("Number of mandatory fields : " + numberMandatoryFields);
+                LOG.debug("Total : " + totalFields);
+            }  
+            
         }
     }
 
-    public void bind(List<String> data, Map<String, Object> model) throws Exception
{
+    public void bind(List<String> tokens, Map<String, Object> model) throws Exception
{
 
         int pos = 0;
-        while (pos < data.size()) {
+        int counterMandatoryFields = 0;
+ 
+        for (String data : tokens) {
+        	
+        	// Get DataField from model
+            DataField dataField = dataFields.get(pos);
+            ObjectHelper.notNull(dataField, "No position " + pos + " defined for the field
: " + data);
+            
+            if ( dataField.required()) {
+            	// Increment counter of mandatory fields
+            	++counterMandatoryFields;
+            	
+            	// Check if content of the field is empty
+            	// This is not possible for mandatory fields
+            	if ( data.equals("")) {
+            		throw new IllegalArgumentException("The mandatory field defined at the position
" + pos + " is empty !");
+            	}
+            }
+            
+            // Get Field to be setted
+            Field field = annotedFields.get(pos);
+            field.setAccessible(true);
+            
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Pos : " + pos + ", Data : " + data + ", Field type : " + field.getType());
+            }
+            
+            Format<?> format;
+            
+            // Get pattern defined for the field
+            String pattern = dataField.pattern();
+            
+            // Create format object to format the field 
+            format = FormatFactory.getFormat(field.getType(), pattern, dataField.precision());
+            
+            // field object to be set
+            Object modelField = model.get(field.getDeclaringClass().getName());
+            
+            // format the data received
+            Object value = null;
+            
+            if ( ! data.equals("") ) {
+            	value = format.parse( data );
+            } else   {
+            	value = getDefaultValueforPrimitive(field.getType());
+            }
+            
+            field.set(modelField, value);
+            
+            ++pos;            
+            
+        }
+        
+/*        while (pos < data.size()) {
 
             // Set the field with the data received
             // Only when no empty line is provided
@@ -130,9 +203,14 @@
 
                 DataField dataField = dataFields.get(pos);
                 ObjectHelper.notNull(dataField, "No position defined for the field");
+                
+                if ( dataField.required()) {
+                	++counterMandatoryFields;
+                }
+                
                 Field field = annotedFields.get(pos);
                 field.setAccessible(true);
-
+                
                 if (LOG.isDebugEnabled()) {
                     LOG.debug("Pos : " + pos + ", Data : " + data.get(pos) + ", Field type
: " + field.getType());
                 }
@@ -153,8 +231,23 @@
                 
                 field.set(modelField, value);
             }
-            pos++;
+
+            ++pos;
+        }*/
+        
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Counter mandatory fields : " + counterMandatoryFields);
         }
+        
+     
+        if ( pos < totalFields ) {
+        	throw new IllegalArgumentException("Some fields are missing (optional or mandatory)
!!");
+        }
+        
+        if ( counterMandatoryFields < numberMandatoryFields) {
+        	throw new IllegalArgumentException("Some mandatory fields are missing !!");
+        }
+        
     }
 
     public String unbind(Map<String, Object> model) throws Exception {

Modified: camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/DataField.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/DataField.java?rev=794245&r1=794244&r2=794245&view=diff
==============================================================================
--- camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/DataField.java
(original)
+++ camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/DataField.java
Wed Jul 15 12:09:00 2009
@@ -29,6 +29,7 @@
  * The length (optional) allows to define for fixed length message the size of the data's
block
  * The precision(optional) reflects the precision to be used with BigDecimal number
  * The position (optional) identify the position of the field in the CSV generated
+ * The required (optional) property identifies a field which is mandatory.
  */
 @Documented
 @Retention(RetentionPolicy.RUNTIME)
@@ -77,5 +78,13 @@
      * @return int 
      */
     int position() default 0;
+    
+    /**
+     * 
+     * Indicates if the field is mandatory
+     * 
+     * @return boolean
+     */
+    boolean required() default false;
 
 }

Modified: camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/csv/BindyCsvDataFormat.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/csv/BindyCsvDataFormat.java?rev=794245&r1=794244&r2=794245&view=diff
==============================================================================
--- camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/csv/BindyCsvDataFormat.java
(original)
+++ camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/csv/BindyCsvDataFormat.java
Wed Jul 15 12:09:00 2009
@@ -119,24 +119,44 @@
 
                 // Split the CSV record according to the separator defined in
                 // annotated class @CSVRecord
-                List<String> result = Arrays.asList(line.split(separator));
+                String[] tokens = line.split(separator, -1);
+                List<String> result = Arrays.asList(tokens);
                 
-                // Bind data from CSV record with model classes
-                factory.bind(result, model);
-
-                // Link objects together
-                factory.link(model);
-
-                // Add objects graph to the list
-                models.add(model);
+                if ( result.size() == 0 || result.isEmpty() ) {
+                	throw new java.lang.IllegalArgumentException("No records have been defined
in the CSV !");
+                }
 
-                if (LOG.isDebugEnabled()) {
-                    LOG.debug("Graph of objects created : " + model);
+                if ( result.size() > 0 ) {
+                	
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Size of the record splitted : " + result.size());
+                    }
+                	
+                    // Bind data from CSV record with model classes
+                    factory.bind(result, model);
+
+                    // Link objects together
+                    factory.link(model);
+
+                    // Add objects graph to the list
+                    models.add(model);
+
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Graph of objects created : " + model);
+                    }
+                	
                 }
+ 
 
             }
-
-            return models;
+            
+            // Test if models list is empty or not
+            // If this is the case (correspond to an empty stream, ...)
+            if ( models.size() == 0 ) {
+            	throw new java.lang.Exception("No records have been defined in the CSV !");
+        	} else {
+        		return models;
+        	}
 
         } finally {
             scanner.close();

Modified: camel/trunk/components/camel-bindy/src/test/data/csv.txt
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/test/data/csv.txt?rev=794245&r1=794244&r2=794245&view=diff
==============================================================================
--- camel/trunk/components/camel-bindy/src/test/data/csv.txt (original)
+++ camel/trunk/components/camel-bindy/src/test/data/csv.txt Wed Jul 15 12:09:00 2009
@@ -1,4 +1,3 @@
-
 01,,Albert,Cartier,ISIN,BE12345678,SELL,,1500,EUR,08-01-2009
 02,A1,,Preud'Homme,ISIN,XD12345678,BUY,,2500,USD,08-01-2009
 03,A2,Jacques,,,BE12345678,SELL,,1500,EUR,08-01-2009

Added: camel/trunk/components/camel-bindy/src/test/data3/csv.txt
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/test/data3/csv.txt?rev=794245&view=auto
==============================================================================
--- camel/trunk/components/camel-bindy/src/test/data3/csv.txt (added)
+++ camel/trunk/components/camel-bindy/src/test/data3/csv.txt Wed Jul 15 12:09:00 2009
@@ -0,0 +1,7 @@
+order nr,client ref,first name, last name,instrument code,instrument name,order type, instrument
type, quantity,currency,date
+01,A0,Albert,Cartier,ISIN,,,,,EUR,08-01-2009
+02,A1,Jacques,Preud'Homme,,,BUY,,2500,USD,08-01-2009
+03,A2,Jacques,Delporte,,BE12345678,SELL,,1500,EUR,08-01-2009
+,blable,,,,,,,,,
+,,,,,,,,
+,,,,,
\ No newline at end of file

Propchange: camel/trunk/components/camel-bindy/src/test/data3/csv.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyComplexCsvUnmarshallTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyComplexCsvUnmarshallTest.java?rev=794245&r1=794244&r2=794245&view=diff
==============================================================================
--- camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyComplexCsvUnmarshallTest.java
(original)
+++ camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyComplexCsvUnmarshallTest.java
Wed Jul 15 12:09:00 2009
@@ -17,6 +17,8 @@
 package org.apache.camel.dataformat.bindy.csv;
 
 import org.apache.camel.EndpointInject;
+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.spring.javaconfig.SingleRouteCamelConfiguration;
@@ -30,11 +32,29 @@
 @ContextConfiguration(locations = "org.apache.camel.dataformat.bindy.csv.BindyComplexCsvUnmarshallTest$ContextConfig",
loader = JavaConfigContextLoader.class)
 public class BindyComplexCsvUnmarshallTest extends AbstractJUnit4SpringContextTests {
 
-    @EndpointInject(uri = "mock:result")
-    private MockEndpoint resultEndpoint;
+	private String record = "01,,Albert,Cartier,ISIN,BE12345678,SELL,,1500,EUR,08-01-2009\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é,Rieux,ISIN,XD12345678,SELL,Share,,USD,08-01-2009\r\n" +
+    "07,A6,Mylè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,USD,08-01-2009";
+	
+	@EndpointInject(uri = "mock:result")
+	private MockEndpoint resultEndpoint;
+
+	@Produce(uri = "direct:start")
+	protected ProducerTemplate template;
 
     @Test
     public void testUnMarshallMessage() throws Exception {
+    	template.sendBody(record);
         resultEndpoint.expectedMessageCount(1);
         resultEndpoint.assertIsSatisfied();
     }
@@ -49,7 +69,10 @@
             return new RouteBuilder() {
                 @Override
                 public void configure() {
-                    from("file://src/test/data?noop=true").unmarshal(camelDataFormat).to("mock:result");
+                    //from("file://src/test/data?noop=true")
+                	from("direct:start")
+                    .unmarshal(camelDataFormat)
+                    .to("mock:result");
                 }
             };
         }

Added: camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMandatoryFieldsUnmarshallTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMandatoryFieldsUnmarshallTest.java?rev=794245&view=auto
==============================================================================
--- camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMandatoryFieldsUnmarshallTest.java
(added)
+++ camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMandatoryFieldsUnmarshallTest.java
Wed Jul 15 12:09:00 2009
@@ -0,0 +1,152 @@
+/**
+ * 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.csv;
+
+import static org.junit.Assert.fail;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.EndpointInject;
+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.spring.javaconfig.SingleRouteCamelConfiguration;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.junit.Test;
+import org.springframework.config.java.annotation.Bean;
+import org.springframework.config.java.annotation.Configuration;
+import org.springframework.config.java.test.JavaConfigContextLoader;
+import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
+import org.springframework.util.Assert;
+
+@ContextConfiguration(locations = "org.apache.camel.dataformat.bindy.csv.BindySimpleCsvMandatoryFieldsUnmarshallTest$ContextConfig",
loader = JavaConfigContextLoader.class)
+public class BindySimpleCsvMandatoryFieldsUnmarshallTest extends AbstractJUnit4SpringContextTests
{
+	
+	private static final transient Log LOG = LogFactory.getLog(BindySimpleCsvMandatoryFieldsUnmarshallTest.class);
+	
+	String header = "order nr,client ref,first name, last name,instrument code,instrument name,order
type, instrument type, quantity,currency,date\r\n";
+	String record1 = "";
+	String record2 = ",,blabla,,,,,,,,";
+	String record3 = "1,A1,Charles,Moulliard,ISIN,LU123456789,,,,,";
+	String record4 = "1,A1,Charles,,ISIN,LU123456789,,,,,";
+	String record5 = ",,,,,,,,,,";
+
+    @EndpointInject(uri = "mock:result1")
+    protected MockEndpoint resultEndpoint1;
+    
+    @EndpointInject(uri = "mock:result2")
+    protected MockEndpoint resultEndpoint2;
+    
+    @Produce(uri = "direct:start1")
+    protected ProducerTemplate template1;
+    
+    @Produce(uri = "direct:start2")
+    protected ProducerTemplate template2;
+
+    @DirtiesContext
+    @Test
+    public void testEmptyRecord() throws Exception {
+    	resultEndpoint1.expectedMessageCount(0);
+    	
+    	try {
+    		template1.sendBody(record1);
+            fail("Should have thrown an exception");
+    	} catch (CamelExecutionException e) {
+    	   // Assert.isInstanceOf(java.lang.IllegalArgumentException.class, e.getCause());
+    		 Assert.isInstanceOf(Exception.class, e.getCause());
+    	  // LOG.info(">> Error : " + e);
+    	}
+    	
+        resultEndpoint1.assertIsSatisfied();
+    }
+    
+    @DirtiesContext
+    @Test
+    public void testEmptyFields() throws Exception {
+    	resultEndpoint1.expectedMessageCount(1);
+   		template1.sendBody(record2);
+    	
+        resultEndpoint1.assertIsSatisfied();
+    }
+    
+    @DirtiesContext
+    @Test
+    public void testOneOptionalField() throws Exception {
+    	resultEndpoint1.expectedMessageCount(1);
+    	
+    	template1.sendBody(record2);
+        resultEndpoint1.assertIsSatisfied();
+    }
+    
+    @DirtiesContext
+    @Test
+    public void testSeveralOptionalField() throws Exception {
+    	resultEndpoint1.expectedMessageCount(1);
+    	
+    	template1.sendBody(record3);
+        resultEndpoint1.assertIsSatisfied();
+    }
+    
+    @DirtiesContext
+    @Test
+    public void testMandatoryFields() throws Exception {
+    	resultEndpoint2.expectedMessageCount(1);
+    	
+    	template2.sendBody(header + record3);
+        resultEndpoint2.assertIsSatisfied();
+    }
+    
+    @DirtiesContext
+    @Test
+    public void testMissingMandatoryFields() throws Exception {
+    	resultEndpoint2.expectedMessageCount(1);
+    	
+    	try {
+    		template2.sendBody(header + record4);
+    		resultEndpoint2.assertIsSatisfied();
+    	} catch (CamelExecutionException e) {
+      	  //LOG.info(">> Error : " + e);
+      	}
+    }
+    
+    @Configuration
+    public static class ContextConfig extends SingleRouteCamelConfiguration {
+        BindyCsvDataFormat formatOptional = new BindyCsvDataFormat("org.apache.camel.dataformat.bindy.model.simple.oneclass");
+        BindyCsvDataFormat formatMandatory = new BindyCsvDataFormat("org.apache.camel.dataformat.bindy.model.simple.oneclassmandatory");
+
+        @Override
+        @Bean
+        public RouteBuilder route() {
+            return new RouteBuilder() {
+                @Override
+                public void configure() {
+                	try {
+                		from("direct:start1").unmarshal(formatOptional).to("mock:result1");
+                		from("direct:start2").unmarshal(formatMandatory).to("mock:result2");
+                	} catch (Exception e) {
+                		//
+                	}
+                }
+            };
+        }
+    }
+}
+
+    
\ No newline at end of file

Propchange: camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMandatoryFieldsUnmarshallTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvSkipFirstLineUnmarshallTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvSkipFirstLineUnmarshallTest.java?rev=794245&r1=794244&r2=794245&view=diff
==============================================================================
--- camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvSkipFirstLineUnmarshallTest.java
(original)
+++ camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvSkipFirstLineUnmarshallTest.java
Wed Jul 15 12:09:00 2009
@@ -17,6 +17,8 @@
 package org.apache.camel.dataformat.bindy.csv;
 
 import org.apache.camel.EndpointInject;
+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.spring.javaconfig.SingleRouteCamelConfiguration;
@@ -29,11 +31,31 @@
 
 @ContextConfiguration(locations = "org.apache.camel.dataformat.bindy.csv.BindySimpleCsvSkipFirstLineUnmarshallTest$ContextConfig",
loader = JavaConfigContextLoader.class)
 public class BindySimpleCsvSkipFirstLineUnmarshallTest extends AbstractJUnit4SpringContextTests
{
-    @EndpointInject(uri = "mock:result")
-    private MockEndpoint resultEndpoint;
+	
+	private String record = "order nr,client ref,first name, last name,instrument code,instrument
name,order type, instrument type, quantity,currency,date\r\n" +
+	"01,,Albert,Cartier,ISIN,BE12345678,SELL,,1500,EUR,08-01-2009\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é,Rieux,ISIN,XD12345678,SELL,Share,,USD,08-01-2009\r\n" +
+    "07,A6,Mylè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,USD,08-01-2009";
+
+	@EndpointInject(uri = "mock:result")
+	private MockEndpoint resultEndpoint;
+
+	@Produce(uri = "direct:start")
+	protected ProducerTemplate template;
 
     @Test
     public void testUnMarshallMessage() throws Exception {
+    	template.sendBody(record);
         resultEndpoint.expectedMessageCount(1);
         resultEndpoint.assertIsSatisfied();
     }
@@ -48,7 +70,10 @@
             return new RouteBuilder() {
                 @Override
                 public void configure() {
-                    from("file://src/test/data2?noop=true").unmarshal(camelDataFormat).to("mock:result");
+                    //from("file://src/test/data2")
+                	from("direct:start")
+                    .unmarshal(camelDataFormat)
+                    .to("mock:result");
                 }
             };
         }

Modified: camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvUnmarshallDslTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvUnmarshallDslTest.java?rev=794245&r1=794244&r2=794245&view=diff
==============================================================================
--- camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvUnmarshallDslTest.java
(original)
+++ camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvUnmarshallDslTest.java
Wed Jul 15 12:09:00 2009
@@ -17,6 +17,8 @@
 package org.apache.camel.dataformat.bindy.csv;
 
 import org.apache.camel.EndpointInject;
+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.model.dataformat.BindyType;
@@ -31,11 +33,29 @@
 @ContextConfiguration(locations = "org.apache.camel.dataformat.bindy.csv.BindySimpleCsvUnmarshallTest$ContextConfig",
loader = JavaConfigContextLoader.class)
 public class BindySimpleCsvUnmarshallDslTest extends AbstractJUnit4SpringContextTests {
 
-    @EndpointInject(uri = "mock:result")
-    private MockEndpoint resultEndpoint;
+	private String record ="01,,Albert,Cartier,ISIN,BE12345678,SELL,,1500,EUR,08-01-2009\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é,Rieux,ISIN,XD12345678,SELL,Share,,USD,08-01-2009\r\n" +
+    "07,A6,Mylè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,USD,08-01-2009";
+
+	@EndpointInject(uri = "mock:result")
+	private MockEndpoint resultEndpoint;
+
+	@Produce(uri = "direct:start")
+	protected ProducerTemplate template;
 
     @Test
     public void testUnMarshallMessage() throws Exception {
+    	template.sendBody(record);
         resultEndpoint.expectedMessageCount(1);
         resultEndpoint.assertIsSatisfied();
     }
@@ -48,7 +68,8 @@
             return new RouteBuilder() {
                 @Override
                 public void configure() {
-                    from("file://src/test/data?noop=true")
+                    //from("file://src/test/data?noop=true")
+                	from("direct:start")
                         .unmarshal().bindy(BindyType.Csv, "org.apache.camel.dataformat.bindy.model.simple.oneclass")
                         .to("mock:result");
                 }

Modified: camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvUnmarshallTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvUnmarshallTest.java?rev=794245&r1=794244&r2=794245&view=diff
==============================================================================
--- camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvUnmarshallTest.java
(original)
+++ camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvUnmarshallTest.java
Wed Jul 15 12:09:00 2009
@@ -16,10 +16,16 @@
  */
 package org.apache.camel.dataformat.bindy.csv;
 
+import java.util.List;
+
 import org.apache.camel.EndpointInject;
+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.spring.javaconfig.SingleRouteCamelConfiguration;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.junit.Test;
 import org.springframework.config.java.annotation.Bean;
 import org.springframework.config.java.annotation.Configuration;
@@ -29,14 +35,44 @@
 
 @ContextConfiguration(locations = "org.apache.camel.dataformat.bindy.csv.BindySimpleCsvUnmarshallTest$ContextConfig",
loader = JavaConfigContextLoader.class)
 public class BindySimpleCsvUnmarshallTest extends AbstractJUnit4SpringContextTests {
+	
+	private static final transient Log LOG = LogFactory.getLog(BindySimpleCsvUnmarshallTest.class);
+	
+	private String record ="01,,Albert,Cartier,ISIN,BE12345678,SELL,,1500,EUR,08-01-2009\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é,Rieux,ISIN,XD12345678,SELL,Share,,USD,08-01-2009\r\n"
+
+                            "07,A6,Mylè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,USD,08-01-2009";
 
     @EndpointInject(uri = "mock:result")
     private MockEndpoint resultEndpoint;
+    
+    @Produce(uri = "direct:start")
+    protected ProducerTemplate template;
 
     @Test
     public void testUnMarshallMessage() throws Exception {
-        resultEndpoint.expectedMessageCount(1);
+    	
+    	template.sendBody(record);
+ 
+        resultEndpoint.expectedMessageCount(1);        
         resultEndpoint.assertIsSatisfied();
+    	
+/*    	List<Exchange> exchanges = resultEndpoint.getExchanges(); 
+    	
+    	for(Exchange exchange : exchanges) {
+    		Object body = exchange.getOut().getBody();
+    		LOG.debug("Body received : " + body.toString());
+    	}*/
+
     }
 
     @Configuration
@@ -49,9 +85,12 @@
             return new RouteBuilder() {
                 @Override
                 public void configure() {
-                    from("file://src/test/data?noop=true").unmarshal(camelDataFormat).to("mock:result");
+                    //from("file://src/test/data?move=./target/done").unmarshal(camelDataFormat).to("mock:result");
+                	from("direct:start").unmarshal(camelDataFormat).to("mock:result");
                 }
             };
         }
     }
 }
+
+    
\ No newline at end of file



Mime
View raw message