camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cmoulli...@apache.org
Subject svn commit: r790511 - in /camel/trunk/components/camel-bindy/src: main/java/org/apache/camel/dataformat/bindy/ test/java/org/apache/camel/dataformat/bindy/csv/ test/java/org/apache/camel/dataformat/bindy/fix/
Date Thu, 02 Jul 2009 09:14:56 GMT
Author: cmoulliard
Date: Thu Jul  2 09:14:56 2009
New Revision: 790511

URL: http://svn.apache.org/viewvc?rev=790511&view=rev
Log:
CAMEL-1784. Add test to check if the field is null. If this is the case, the data is not exported
in the string generated (CSV, FIX, ...). Two new unit tests added

Added:
    camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvNullMarshallTest.java
  (with props)
    camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fix/BindySimpleKeyValuePairNullMarshallTest.java
  (with props)
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/BindyKeyValuePairFactory.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=790511&r1=790510&r2=790511&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  2 09:14:56 2009
@@ -130,7 +130,7 @@
             if (!data.get(pos).equals("")) {
 
                 DataField dataField = dataFields.get(pos);
-                ObjectHelper.notNull(dataField, "No position defined for the field positoned
: " + pos);
+                ObjectHelper.notNull(dataField, "No position defined for the field");
                 Field field = annotedFields.get(pos);
                 field.setAccessible(true);
 
@@ -139,10 +139,20 @@
                 }
 
                 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.set(model.get(field.getDeclaringClass().getName()), format.parse(data.get(pos)));
+                
+                // 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++;
         }
@@ -179,37 +189,60 @@
             // Change accessibility to allow to read protected/private fields
             field.setAccessible(true);
 
-            // Retrieve the format associated to the type
-            Format format = FormatFactory.getFormat(field.getType(), dataField.pattern(),
dataField.precision());
+            // Retrieve the format, pattern and precision associated to the type
+            Class type = field.getType();
+            String pattern = dataField.pattern();
+            int precision = dataField.precision();
             
-            // Get object to be formatted
-            Object obj = model.get(field.getDeclaringClass().getName());
+            // Create format
+            Format format = FormatFactory.getFormat(type, pattern, precision);
             
-            if (obj != null) {
+            // Get field from model
+            Object modelField = model.get(field.getDeclaringClass().getName());
+            
+            if (modelField != null) {
 
                 if (this.isMessageOrdered()) {
 
                     // Generate a key using the number of the section
                     // and the position of the field
-                    Integer key1 = sections.get(obj.getClass().getName());
+                    Integer key1 = sections.get(modelField.getClass().getName());
                     Integer key2 = dataField.position();
                     Integer keyGenerated = generateKey(key1, key2);
                     
                     if (LOG.isDebugEnabled()) {
                         LOG.debug("Key generated : " + String.valueOf(keyGenerated) + ",
for section : " + key1);
                     }                    
-
-                    // Add the content to the TreeMap according to the position defined
-                    String value = format.format(field.get(obj));
-                    positions.put(keyGenerated, value);
-            
-                    if (LOG.isDebugEnabled()) {
-                        LOG.debug("Positions size : " + positions.size());
-                    }
+                    
+                    // Get field value
+                    Object value = field.get(modelField);
+                    
+                    // Add value to the list if not null
+					if (value != null) {
+						
+						// Format field value
+						String valueFormated = format.format(value);
+						
+						// Add the content to the TreeMap according to the position defined
+						positions.put(keyGenerated, valueFormated);
+
+						if (LOG.isDebugEnabled()) {
+							LOG.debug("Positions size : " + positions.size());
+						}
+					}
                 } else {
-                    // Convert the content to a String and append it to the builder
-                    builder.append(format.format(field.get(obj)));
-                    if (it.hasNext()) {
+                    // Get field value
+                    Object value = field.get(modelField);
+                    
+                    // Add value to the list if not null
+					if (value != null) {
+						
+						// Format field value
+						String valueFormated = format.format(value);
+						builder.append( valueFormated );
+					}
+                    
+                	if (it.hasNext()) {
                         builder.append(separator);
                     }
                 }

Modified: camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyKeyValuePairFactory.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyKeyValuePairFactory.java?rev=790511&r1=790510&r2=790511&view=diff
==============================================================================
--- camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyKeyValuePairFactory.java
(original)
+++ camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyKeyValuePairFactory.java
Thu Jul  2 09:14:56 2009
@@ -133,10 +133,10 @@
                 String[] keyValuePair = data.get(pos).split(this.getKeyValuePairSeparator());
 
                 int tag = Integer.parseInt(keyValuePair[0]);
-                String value = keyValuePair[1];
+                String keyValue = keyValuePair[1];
 
                 if (LOG.isDebugEnabled()) {
-                    LOG.debug("Key : " + tag + ", value : " + value);
+                    LOG.debug("Key : " + tag + ", value : " + keyValue);
                 }
 
                 KeyValuePairField keyValuePairField = keyValuePairFields.get(tag);
@@ -146,14 +146,25 @@
                 field.setAccessible(true);
 
                 if (LOG.isDebugEnabled()) {
-                    LOG.debug("Tag : " + tag + ", Data : " + value + ", Field type : " +
field.getType());
+                    LOG.debug("Tag : " + tag + ", Data : " + keyValue + ", Field type : "
+ field.getType());
                 }
 
                 Format<?> format;
+                
+                // Get pattern defined for the field
                 String pattern = keyValuePairField.pattern();
-
+                
+                // Create format object to format the field 
                 format = FormatFactory.getFormat(field.getType(), pattern, keyValuePairField.precision());
-                field.set(model.get(field.getDeclaringClass().getName()), format.parse(value));
+                
+                // field object to be set
+                Object modelField = model.get(field.getDeclaringClass().getName());
+                
+                // format the value of the key received
+                Object value = format.parse( keyValue );
+                
+                
+                field.set( modelField , value );
 
             }
 
@@ -187,59 +198,84 @@
 
         while (it.hasNext()) {
 
-            KeyValuePairField keyValuePairField = keyValuePairFieldsSorted.get(it.next());
-            ObjectHelper.notNull(keyValuePairField, "KeyValuePair is null !");
-
-            // Retrieve the field
-            Field field = annotedFields.get(keyValuePairField.tag());
-            // Change accessibility to allow to read protected/private fields
-            field.setAccessible(true);
-
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Tag : " + keyValuePairField.tag() + ", Field type : " + field.getType()
+ ", class : " + field.getDeclaringClass().getName());
-            }
-
-            // Retrieve the format associated to the type
-            Format format;
-            String pattern = keyValuePairField.pattern();
-            format = FormatFactory.getFormat(field.getType(), pattern, keyValuePairField.precision());
+			KeyValuePairField keyValuePairField = keyValuePairFieldsSorted.get(it.next());
+			ObjectHelper.notNull(keyValuePairField, "KeyValuePair is null !");
 
-            // Get object to be formatted
-            Object obj = model.get(field.getDeclaringClass().getName());
-            
-            if (obj != null) {
-                if (this.isMessageOrdered()) {
-                    // Generate a key using the number of the section
-                    // and the position of the field
-                    Integer key1 = sections.get(obj.getClass().getName());
-                    Integer key2 = keyValuePairField.position();
-                    Integer keyGenerated = generateKey(key1, key2);
-                    
-                    if (LOG.isDebugEnabled()) {
-                        LOG.debug("Key generated : " + String.valueOf(keyGenerated) + ",
for section : " + key1);
-                    }                    
-
-                    // Add the content to the TreeMap according to the position defined
-                    String value = keyValuePairField.tag() + this.getKeyValuePairSeparator()
+ format.format(field.get(obj));
-                    positions.put(keyGenerated, value);
-
-                    if (LOG.isDebugEnabled()) {
-                        LOG.debug("Positions size : " + positions.size());
-                    }
-                } else {
-                    // Convert the content to a String and append it to the
-                    // builder
-                    // Add the tag followed by its key value pair separator
-                    // the data and finish by the pair separator
-                    String value = keyValuePairField.tag() + this.getKeyValuePairSeparator()
+ format.format(field.get(obj)) + separator;
-                    builder.append(value);
-                    
-                    if (LOG.isDebugEnabled()) {
-                        LOG.debug("Value added : " + keyValuePairField.tag() + this.getKeyValuePairSeparator()
-                            + format.format(field.get(obj)) + separator);
-                    }
-                }
-            }
+			// Retrieve the field
+			Field field = annotedFields.get(keyValuePairField.tag());
+			// Change accessibility to allow to read protected/private fields
+			field.setAccessible(true);
+
+			if (LOG.isDebugEnabled()) {
+				LOG.debug("Tag : " + keyValuePairField.tag() + ", Field type : " + field.getType() +
", class : "
+						+ field.getDeclaringClass().getName());
+			}
+
+			// Retrieve the format, pattern and precision associated to the type
+			Class type = field.getType();
+			String pattern = keyValuePairField.pattern();
+			int precision = keyValuePairField.precision();
+
+			// Create format
+			Format format = FormatFactory.getFormat(type, pattern, precision);
+
+			// Get object to be formatted
+			Object obj = model.get(field.getDeclaringClass().getName());
+
+			if (obj != null) {
+
+				// Get field value
+				Object keyValue = field.get(obj);
+
+				if (this.isMessageOrdered()) {
+					// Generate a key using the number of the section
+					// and the position of the field
+					Integer key1 = sections.get(obj.getClass().getName());
+					Integer key2 = keyValuePairField.position();
+					Integer keyGenerated = generateKey(key1, key2);
+
+					if (LOG.isDebugEnabled()) {
+						LOG.debug("Key generated : " + String.valueOf(keyGenerated) + ", for section : " +
key1);
+					}
+
+					// Add value to the list if not null
+					if (keyValue != null) {
+
+						// Format field value
+						String valueFormated = format.format(keyValue);
+
+						// Create the key value string
+						String value = keyValuePairField.tag() + this.getKeyValuePairSeparator() + valueFormated;
+
+						// Add the content to the TreeMap according to the
+						// position defined
+						positions.put(keyGenerated, value);
+
+						if (LOG.isDebugEnabled()) {
+							LOG.debug("Positions size : " + positions.size());
+						}
+					}
+				} else {
+
+					// Add value to the list if not null
+					if (keyValue != null) {
+
+						// Format field value
+						String valueFormated = format.format(keyValue);
+
+						// Create the key value string
+						String value = keyValuePairField.tag() + this.getKeyValuePairSeparator() + valueFormated
+ separator;
+
+						// Add content to the stringBuilder
+						builder.append(value);
+
+						if (LOG.isDebugEnabled()) {
+							LOG.debug("Value added : " + keyValuePairField.tag() + this.getKeyValuePairSeparator()
+									+ valueFormated + separator);
+						}
+					}
+				}
+			}
         }
 
         // Iterate through the list to generate

Added: camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvNullMarshallTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvNullMarshallTest.java?rev=790511&view=auto
==============================================================================
--- camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvNullMarshallTest.java
(added)
+++ camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvNullMarshallTest.java
Thu Jul  2 09:14:56 2009
@@ -0,0 +1,104 @@
+/**
+ * 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 java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+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.dataformat.bindy.model.simple.oneclass.Order;
+import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration;
+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.context.ContextConfiguration;
+import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
+
+@ContextConfiguration(locations = "org.apache.camel.dataformat.bindy.csv.BindySimpleCsvMarshallTest$ContextConfig",
loader = JavaConfigContextLoader.class)
+public class BindySimpleCsvNullMarshallTest extends AbstractJUnit4SpringContextTests {
+
+    private List<Map<String, Object>> models = new ArrayList<Map<String,
Object>>();
+    private String result = "1,B2,Keira,Knightley,ISIN,XX23456789,BUY,,450.45,EUR,14-01-2009\r\n";
+
+    @Produce(uri = "direct:start")
+    private ProducerTemplate template;
+
+    @EndpointInject(uri = "mock:result")
+    private MockEndpoint resultEndpoint;
+
+    @Test
+    public void testMarshallMessage() throws Exception {
+        resultEndpoint.expectedBodiesReceived(result);
+
+        template.sendBody(generateModel());
+
+        resultEndpoint.assertIsSatisfied();
+    }
+
+    public List<Map<String, Object>> generateModel() {
+        Map<String, Object> modelObjects = new HashMap<String, Object>();
+
+        Order order = new Order();
+        order.setOrderNr(1);
+        order.setOrderType("BUY");
+        order.setClientNr("B2");
+        order.setFirstName("Keira");
+        order.setLastName("Knightley");
+        order.setAmount(new BigDecimal("450.45").setScale(2));
+        order.setInstrumentCode("ISIN");
+        order.setInstrumentNumber("XX23456789");
+        order.setInstrumentType(null); // Null field
+        order.setCurrency("EUR");
+
+        Calendar calendar = new GregorianCalendar();
+        calendar.set(2009, 0, 14);
+        order.setOrderDate(calendar.getTime());
+
+        modelObjects.put(order.getClass().getName(), order);
+
+        models.add(modelObjects);
+
+        return models;
+    }
+
+    @Configuration
+    public static class ContextConfig extends SingleRouteCamelConfiguration {
+        BindyCsvDataFormat camelDataFormat = new BindyCsvDataFormat("org.apache.camel.dataformat.bindy.model.simple.oneclass");
+
+        @Override
+        @Bean
+        public RouteBuilder route() {
+            return new RouteBuilder() {
+                @Override
+                public void configure() {
+                    from("direct:start").marshal(camelDataFormat).to("mock:result");
+                }
+            };
+        }
+    }
+
+}

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

Added: camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fix/BindySimpleKeyValuePairNullMarshallTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fix/BindySimpleKeyValuePairNullMarshallTest.java?rev=790511&view=auto
==============================================================================
--- camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fix/BindySimpleKeyValuePairNullMarshallTest.java
(added)
+++ camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fix/BindySimpleKeyValuePairNullMarshallTest.java
Thu Jul  2 09:14:56 2009
@@ -0,0 +1,109 @@
+/**
+ * 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.fix;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+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.dataformat.bindy.kvp.BindyKeyValuePairDataFormat;
+import org.apache.camel.dataformat.bindy.model.fix.simple.Header;
+import org.apache.camel.dataformat.bindy.model.fix.simple.Order;
+import org.apache.camel.dataformat.bindy.model.fix.simple.Trailer;
+import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration;
+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.context.ContextConfiguration;
+import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
+
+@ContextConfiguration(locations = "org.apache.camel.dataformat.bindy.fix.BindySimpleKeyValuePairNullMarshallTest$ContextConfig",
loader = JavaConfigContextLoader.class)
+public class BindySimpleKeyValuePairNullMarshallTest extends AbstractJUnit4SpringContextTests
{
+
+    private List<Map<String, Object>> models = new ArrayList<Map<String,
Object>>();
+    private String result = "1=BE.CHM.0018=FIX 4.19=2010=22011=CHM0001-0122=434=148=BE000124567849=INVMGR54=156=BRKR58=this
is a camel - bindy test\r\n";
+
+    @Produce(uri = "direct:start")
+    private ProducerTemplate template;
+
+    @EndpointInject(uri = "mock:result")
+    private MockEndpoint resultEndpoint;
+
+    @Test
+    public void testMarshallMessage() throws Exception {
+        resultEndpoint.expectedBodiesReceived(result);
+        template.sendBody(generateModel());
+
+        resultEndpoint.assertIsSatisfied();
+    }
+
+    public List<Map<String, Object>> generateModel() {
+        Map<String, Object> modelObjects = new HashMap<String, Object>();
+
+        Header header = new Header();
+        header.setBeginString("FIX 4.1");
+        header.setBodyLength(20);
+        header.setMsgSeqNum(1);
+        header.setMsgType(null); // NULL value
+        header.setSendCompId("INVMGR");
+        header.setTargetCompId("BRKR");
+        
+        Trailer trailer = new Trailer();
+        trailer.setCheckSum(220); 
+        
+        Order order = new Order();
+        order.setAccount("BE.CHM.001");
+        order.setClOrdId("CHM0001-01");
+        order.setIDSource("4");
+        order.setSecurityId("BE0001245678");
+        order.setSide("1");
+        order.setText("this is a camel - bindy test");
+        
+        order.setHeader(header);
+        order.setTrailer(trailer);
+        
+        modelObjects.put(order.getClass().getName(), order);
+        modelObjects.put(header.getClass().getName(), header);
+        modelObjects.put(trailer.getClass().getName(), trailer);
+ 
+        models.add(modelObjects);
+        return models;
+    }
+
+    @Configuration
+    public static class ContextConfig extends SingleRouteCamelConfiguration {
+        BindyKeyValuePairDataFormat camelDataFormat = new BindyKeyValuePairDataFormat("org.apache.camel.dataformat.bindy.model.fix.simple");
+
+        @Override
+        @Bean
+        public RouteBuilder route() {
+            return new RouteBuilder() {
+                @Override
+                public void configure() {
+                    from("direct:start").marshal(camelDataFormat).to("mock:result");
+                }
+            };
+        }
+    }
+}

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



Mime
View raw message