Author: davsclaus
Date: Mon Oct 19 11:21:06 2009
New Revision: 826642

URL: http://svn.apache.org/viewvc?rev=826642&view=rev
Log:
CAMEL-2078: jpa producer requires a message body.

Added:
    
camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaProducerNoBodyTest.java
   (with props)
Modified:
    
camel/trunk/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java

Modified: 
camel/trunk/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java?rev=826642&r1=826641&r2=826642&view=diff
==============================================================================
--- 
camel/trunk/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java
 (original)
+++ 
camel/trunk/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java
 Mon Oct 19 11:21:06 2009
@@ -24,11 +24,10 @@
 import org.apache.camel.Consumer;
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
-import org.apache.camel.NoTypeConversionAvailableException;
+import org.apache.camel.InvalidPayloadException;
+import org.apache.camel.InvalidPayloadRuntimeException;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
-import org.apache.camel.RuntimeCamelException;
-import org.apache.camel.builder.ExpressionBuilder;
 import org.apache.camel.impl.ExpressionAdapter;
 import org.apache.camel.impl.ScheduledPollEndpoint;
 import org.apache.camel.util.IntrospectionSupport;
@@ -253,25 +252,27 @@
     }
 
     protected Expression createProducerExpression() {
-        final Class<?> type = getEntityType();
-        if (type == null) {
-            return ExpressionBuilder.bodyExpression();
-        } else {
-            return new ExpressionAdapter() {
-                public Object evaluate(Exchange exchange) {
-                    Object answer = exchange.getIn().getBody(type);
-                    if (answer == null) {
-                        Object defaultValue = exchange.getIn().getBody();
-                        if (defaultValue != null) {
-                            throw new RuntimeCamelException(new 
NoTypeConversionAvailableException(defaultValue, type));
-                        }
-
-                        // if we don't have a body then lets instantiate and 
inject a new instance
-                        answer = 
exchange.getContext().getInjector().newInstance(type);
+        return new ExpressionAdapter() {
+            public Object evaluate(Exchange exchange) {
+                Object answer;
+
+                // must have a body
+                try {
+                    if (getEntityType() == null) {
+                        answer = exchange.getIn().getMandatoryBody();
+                    } else {
+                        answer = 
exchange.getIn().getMandatoryBody(getEntityType());
                     }
+                } catch (InvalidPayloadException e) {
+                    throw new InvalidPayloadRuntimeException(exchange, 
getEntityType());
+                }
+
+                if (answer == null) {
+                    throw new InvalidPayloadRuntimeException(exchange, 
getEntityType());
+                } else {
                     return answer;
                 }
-            };
-        }
+            }
+        };
     }
 }

Added: 
camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaProducerNoBodyTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaProducerNoBodyTest.java?rev=826642&view=auto
==============================================================================
--- 
camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaProducerNoBodyTest.java
 (added)
+++ 
camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaProducerNoBodyTest.java
 Mon Oct 19 11:21:06 2009
@@ -0,0 +1,94 @@
+/**
+ * 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.processor.jpa;
+
+import java.util.List;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.InvalidPayloadRuntimeException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.examples.SendEmail;
+import org.apache.camel.spring.SpringCamelContext;
+import org.apache.camel.spring.SpringRouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+import org.springframework.orm.jpa.JpaTemplate;
+import org.springframework.orm.jpa.JpaTransactionManager;
+import org.springframework.transaction.TransactionDefinition;
+import org.springframework.transaction.TransactionStatus;
+import org.springframework.transaction.support.TransactionCallback;
+import org.springframework.transaction.support.TransactionTemplate;
+
+/**
+ * @version $Revision$
+ */
+public class JpaProducerNoBodyTest extends CamelTestSupport {
+    protected static final String SELECT_ALL_STRING = "select x from " + 
SendEmail.class.getName() + " x";
+
+    protected ApplicationContext applicationContext;
+    protected JpaTemplate jpaTemplate;
+
+    @Test
+    public void testRouteJpa() throws Exception {
+        try {
+            template.sendBody("direct:start", null);
+            fail("Should have thrown an exception");
+        } catch (CamelExecutionException e) {
+            InvalidPayloadRuntimeException cause = 
assertIsInstanceOf(InvalidPayloadRuntimeException.class, e.getCause());
+            assertTrue(cause.getMessage().contains("Body is null"));
+        }
+    }
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        applicationContext = new 
ClassPathXmlApplicationContext("org/apache/camel/processor/jpa/springJpaRouteTest.xml");
+        cleanupRepository();
+        return SpringCamelContext.springCamelContext(applicationContext);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new SpringRouteBuilder() {
+            public void configure() {
+                from("direct:start").to("jpa://" + 
SendEmail.class.getName()).to("mock:result");
+            }
+        };
+    }
+
+    protected void cleanupRepository() {
+        jpaTemplate = (JpaTemplate)applicationContext.getBean("jpaTemplate", 
JpaTemplate.class);
+
+        TransactionTemplate transactionTemplate = new TransactionTemplate();
+        transactionTemplate.setTransactionManager(new 
JpaTransactionManager(jpaTemplate.getEntityManagerFactory()));
+        
transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
+
+        transactionTemplate.execute(new TransactionCallback() {
+            public Object doInTransaction(TransactionStatus arg0) {
+                List list = jpaTemplate.find(SELECT_ALL_STRING);
+                for (Object item : list) {
+                    jpaTemplate.remove(item);
+                }
+                jpaTemplate.flush();
+                return Boolean.TRUE;
+            }
+        });
+    }
+
+}

Propchange: 
camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaProducerNoBodyTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaProducerNoBodyTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date


Reply via email to