You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by cm...@apache.org on 2009/07/16 11:56:17 UTC

svn commit: r794612 - in /camel/trunk/components/camel-bindy/src: main/java/org/apache/camel/dataformat/bindy/ main/java/org/apache/camel/dataformat/bindy/csv/ test/java/org/apache/camel/dataformat/bindy/csv/

Author: cmoulliard
Date: Thu Jul 16 09:56:17 2009
New Revision: 794612

URL: http://svn.apache.org/viewvc?rev=794612&view=rev
Log:
Cleanup code, add a new test for record having too much data, change java.lang.exception into java.lang.IllegalArgumentException

Modified:
    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/csv/BindyCsvDataFormat.java
    camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMandatoryFieldsUnmarshallTest.java

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=794612&r1=794611&r2=794612&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 Thu Jul 16 09:56:17 2009
@@ -193,49 +193,6 @@
             
         }
         
-/*        while (pos < data.size()) {
-
-            // Set the field with the data received
-            // Only when no empty line is provided
-            // Data is transformed according to the pattern defined or by
-            // default the type of the field (int, double, String, ...)
-
-            if (!data.get(pos).equals("")) {
-
-                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());
-                }
-
-                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 = format.parse(data.get(pos));
-                
-                field.set(modelField, value);
-            }
-
-            ++pos;
-        }*/
-        
         if (LOG.isDebugEnabled()) {
             LOG.debug("Counter mandatory fields : " + counterMandatoryFields);
         }

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=794612&r1=794611&r2=794612&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 Thu Jul 16 09:56:17 2009
@@ -132,12 +132,12 @@
                         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);
+                   	// 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);
 
@@ -152,7 +152,7 @@
             // 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 !");
+                throw new java.lang.IllegalArgumentException("No records have been defined in the CSV !");
             } else {
                 return models;
             }

Modified: 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=794612&r1=794611&r2=794612&view=diff
==============================================================================
--- camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMandatoryFieldsUnmarshallTest.java (original)
+++ camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMandatoryFieldsUnmarshallTest.java Thu Jul 16 09:56:17 2009
@@ -55,12 +55,13 @@
     protected ProducerTemplate template2;
   
     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 = ",,,,,,,,,,";
-
+    String record1 = ""; // empty records
+    String record2 = ",,blabla,,,,,,,,"; // optional fields
+    String record3 = "1,A1,Charles,Moulliard,ISIN,LU123456789,,,,,"; // mandatory fields present (A1, Charles, Moulliard)
+    String record4 = "1,A1,Charles,,ISIN,LU123456789,,,,,"; // mandatory field missing
+    String record5 = ",,,,,,,,,,"; // record with no data
+    String record6 = ",,,,,,,,,,,,,,"; // too much data in the record (only 11 are accepted by the model
+    
     @DirtiesContext
     @Test
     public void testEmptyRecord() throws Exception {
@@ -70,8 +71,6 @@
             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);
         }
@@ -99,13 +98,29 @@
 
     @DirtiesContext
     @Test
-    public void testSeveralOptionalField() throws Exception {
+    public void testSeveralOptionalFields() throws Exception {
         resultEndpoint1.expectedMessageCount(1);
 
         template1.sendBody(record3);
         resultEndpoint1.assertIsSatisfied();
     }
+    
+    @DirtiesContext
+    @Test
+    public void testTooMuchFields() throws Exception {
+        resultEndpoint1.expectedMessageCount(0);
+        
+        try {
+            template1.sendBody(record6);
+            fail("Should have thrown an exception");
+        } catch (CamelExecutionException e) {
+            // expected
+            Assert.isInstanceOf(IllegalArgumentException.class, e.getCause());
+        }
 
+        resultEndpoint1.assertIsSatisfied();
+    }
+    
     @DirtiesContext
     @Test
     public void testMandatoryFields() throws Exception {
@@ -141,12 +156,8 @@
             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) {
-                        //
-                    }
                 }
             };
         }