You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2011/06/29 10:17:16 UTC

svn commit: r1140996 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/component/mock/ camel-core/src/test/java/org/apache/camel/component/bean/ camel-core/src/test/java/org/apache/camel/component/mock/ components/camel-hl7/src/test/java/org...

Author: davsclaus
Date: Wed Jun 29 08:17:15 2011
New Revision: 1140996

URL: http://svn.apache.org/viewvc?rev=1140996&view=rev
Log:
CAMEL-4159: MockEndpoint expectedHeader / expectedProperty now supports setting multiple expectations. Fixed test.

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithAnnotationAndExchangeTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java
    camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7DataFormatTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java?rev=1140996&r1=1140995&r2=1140996&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java Wed Jun 29 08:17:15 2011
@@ -26,6 +26,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
@@ -49,6 +50,7 @@ import org.apache.camel.impl.DefaultEndp
 import org.apache.camel.impl.InterceptSendToEndpoint;
 import org.apache.camel.spi.BrowsableEndpoint;
 import org.apache.camel.util.CamelContextHelper;
+import org.apache.camel.util.CaseInsensitiveMap;
 import org.apache.camel.util.ExchangeHelper;
 import org.apache.camel.util.ExpressionComparator;
 import org.apache.camel.util.FileUtil;
@@ -92,12 +94,10 @@ public class MockEndpoint extends Defaul
     private volatile int expectedMinimumCount;
     private volatile List<Object> expectedBodyValues;
     private volatile List<Object> actualBodyValues;
-    private volatile String headerName;
-    private volatile Object headerValue;
-    private volatile Object actualHeader;
-    private volatile String propertyName;
-    private volatile Object propertyValue;
-    private volatile Object actualProperty;
+    private volatile Map<String, Object> expectedHeaderValues;
+    private volatile Map<String, Object> actualHeaderValues;
+    private volatile Map<String, Object> expectedPropertyValues;
+    private volatile Map<String, Object> actualPropertyValues;
     private volatile Processor reporter;
 
     public MockEndpoint(String endpointUri, Component component) {
@@ -462,47 +462,93 @@ public class MockEndpoint extends Defaul
 
     /**
      * Sets an expectation that the given header name & value are received by this endpoint
+     * <p/>
+     * You can set multiple expectations for different header names.
+     * If you set a value of <tt>null</tt> that means we accept either the header is absent, or its value is <tt>null</tt>
      */
     public void expectedHeaderReceived(final String name, final Object value) {
-        this.headerName = name;
-        this.headerValue = value;
+        if (expectedHeaderValues == null) {
+            expectedHeaderValues = new CaseInsensitiveMap();
+        }
+        expectedHeaderValues.put(name, value);
 
         expects(new Runnable() {
             public void run() {
-                assertTrue("No header with name " + headerName + " found.", actualHeader != null);
+                for (int i = 0; i < getReceivedExchanges().size(); i++) {
+                    Exchange exchange = getReceivedExchanges().get(i);
+                    for (Map.Entry<String, Object> entry : expectedHeaderValues.entrySet()) {
+                        String key = entry.getKey();
+                        Object expectedValue = entry.getValue();
+
+                        // we accept that an expectedValue of null also means that the header may be absent
+                        if (expectedValue != null) {
+                            assertTrue("Exchange " + i + " has no headers", exchange.getIn().hasHeaders());
+                            boolean hasKey = exchange.getIn().getHeaders().containsKey(key);
+                            assertTrue("No header with name " + key + " found for message: " + i, hasKey);
+                        }
+
+                        Object actualValue = exchange.getIn().getHeader(key);
+                        if (actualValue instanceof Expression) {
+                            actualValue = ((Expression)actualValue).evaluate(exchange, expectedValue.getClass());
+                        } else if (actualValue instanceof Predicate) {
+                            actualValue = ((Predicate)actualValue).matches(exchange);
+                        } else if (expectedValue != null) {
+                            actualValue = getCamelContext().getTypeConverter().convertTo(expectedValue.getClass(), actualValue);
+                            assertTrue("There is no type conversion possible from " + actualValue.getClass().getName()
+                                    + " to " + expectedValue.getClass().getName(), actualValue != null);
+                        }
 
-                Object actualValue;
-                if (actualHeader instanceof Expression) {
-                    actualValue = ((Expression)actualHeader).evaluate(mostRecentExchange(), headerValue.getClass());
-                } else if (actualHeader instanceof Predicate) {
-                    actualValue = ((Predicate)actualHeader).matches(mostRecentExchange());
-                } else {                    
-                    actualValue = getCamelContext().getTypeConverter().convertTo(headerValue.getClass(), actualHeader);
-                    assertTrue("There is no type conversion possible from " + actualHeader.getClass().getName() 
-                            + " to " + headerValue.getClass().getName(), actualValue != null);
+                        assertEquals("Header with name " + key + " for message: " + i, expectedValue, actualValue);
+                    }
                 }
-                assertEquals("Header with name " + headerName, headerValue, actualValue);
             }
         });
     }
 
-    private Exchange mostRecentExchange() {
-        return receivedExchanges.get(receivedExchanges.size() - 1);
-    }
-    
     /**
      * Sets an expectation that the given property name & value are received by this endpoint
+     * <p/>
+     * You can set multiple expectations for different property names.
+     * If you set a value of <tt>null</tt> that means we accept either the property is absent, or its value is <tt>null</tt>
      */
     public void expectedPropertyReceived(final String name, final Object value) {
-        this.propertyName = name;
-        this.propertyValue = value;
+        if (expectedPropertyValues == null) {
+            expectedPropertyValues = new ConcurrentHashMap<String, Object>();
+        }
+        if (value != null) {
+            // ConcurrentHashMap cannot store null values
+            expectedPropertyValues.put(name, value);
+        }
 
         expects(new Runnable() {
             public void run() {
-                assertTrue("No property with name " + propertyName + " found.", actualProperty != null);
+                for (int i = 0; i < getReceivedExchanges().size(); i++) {
+                    Exchange exchange = getReceivedExchanges().get(i);
+                    for (Map.Entry<String, Object> entry : expectedPropertyValues.entrySet()) {
+                        String key = entry.getKey();
+                        Object expectedValue = entry.getValue();
+
+                        // we accept that an expectedValue of null also means that the header may be absent
+                        if (expectedValue != null) {
+                            assertTrue("Exchange " + i + " has no properties", !exchange.getProperties().isEmpty());
+                            boolean hasKey = exchange.getProperties().containsKey(key);
+                            assertTrue("No property with name " + key + " found for message: " + i, hasKey);
+                        }
+
+                        Object actualValue = exchange.getProperty(key);
+                        if (actualValue instanceof Expression) {
+                            actualValue = ((Expression)actualValue).evaluate(exchange, expectedValue.getClass());
+                        } else if (actualValue instanceof Predicate) {
+                            actualValue = ((Predicate)actualValue).matches(exchange);
+                        } else if (expectedValue != null) {
+                            actualValue = getCamelContext().getTypeConverter().convertTo(expectedValue.getClass(), actualValue);
+                            assertTrue("There is no type conversion possible from " + actualValue.getClass().getName()
+                                    + " to " + expectedValue.getClass().getName(), actualValue != null);
+                        }
 
-                Object actualValue = getCamelContext().getTypeConverter().convertTo(actualProperty.getClass(), propertyValue);
-                assertEquals("Property with name " + propertyName, actualValue, actualProperty);
+                        assertEquals("Property with name " + key + " for message: " + i, expectedValue, actualValue);
+                    }
+                }
             }
         });
     }
@@ -977,6 +1023,10 @@ public class MockEndpoint extends Defaul
         expectedMinimumCount = -1;
         expectedBodyValues = null;
         actualBodyValues = new ArrayList<Object>();
+        expectedHeaderValues = null;
+        actualHeaderValues = null;
+        expectedPropertyValues = null;
+        actualPropertyValues = null;
     }
 
     protected synchronized void onExchange(Exchange exchange) {
@@ -1009,12 +1059,20 @@ public class MockEndpoint extends Defaul
         Message in = copy.getIn();
         Object actualBody = in.getBody();
 
-        if (headerName != null) {
-            actualHeader = in.getHeader(headerName);
+        if (expectedHeaderValues != null) {
+            if (actualHeaderValues == null) {
+                actualHeaderValues = new CaseInsensitiveMap();
+            }
+            if (in.hasHeaders()) {
+                actualHeaderValues.putAll(in.getHeaders());
+            }
         }
 
-        if (propertyName != null) {
-            actualProperty = copy.getProperty(propertyName);
+        if (expectedPropertyValues != null) {
+            if (actualPropertyValues == null) {
+                actualPropertyValues = new ConcurrentHashMap<String, Object>();
+            }
+            actualPropertyValues.putAll(copy.getProperties());
         }
 
         if (expectedBodyValues != null) {

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithAnnotationAndExchangeTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithAnnotationAndExchangeTest.java?rev=1140996&r1=1140995&r2=1140996&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithAnnotationAndExchangeTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithAnnotationAndExchangeTest.java Wed Jun 29 08:17:15 2011
@@ -34,7 +34,6 @@ public class BeanWithAnnotationAndExchan
     public void testBeanWithAnnotationAndExchangeTest() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedBodiesReceived("StartMyBean");
-        mock.expectedHeaderReceived("foo", "bar");
         mock.expectedHeaderReceived("user", "admin");
 
         template.requestBody("direct:in", "Start");

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java?rev=1140996&r1=1140995&r2=1140996&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java Wed Jun 29 08:17:15 2011
@@ -17,7 +17,9 @@
 package org.apache.camel.component.mock;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.camel.ContextTestSupport;
@@ -434,7 +436,7 @@ public class MockEndpointTest extends Co
             assertMockEndpointsSatisfied();
             fail("Should have thrown exception");
         } catch (AssertionError e) {
-            assertEquals("mock://result No header with name bar found.", e.getMessage());
+            assertEquals("mock://result No header with name bar found for message: 0", e.getMessage());
         }
     }
 
@@ -449,7 +451,7 @@ public class MockEndpointTest extends Co
             assertMockEndpointsSatisfied();
             fail("Should have thrown exception");
         } catch (AssertionError e) {
-            assertEquals("mock://result Header with name bar. Expected: <cheese> but was: <beer>", e.getMessage());
+            assertEquals("mock://result Header with name bar for message: 0. Expected: <cheese> but was: <beer>", e.getMessage());
         }
     }
     
@@ -465,7 +467,7 @@ public class MockEndpointTest extends Co
             assertMockEndpointsSatisfied();
             fail("Should have thrown exception");
         } catch (AssertionError e) {
-            assertEquals("mock://result No property with name bar found.", e.getMessage());
+            assertEquals("mock://result No property with name bar found for message: 0", e.getMessage());
         }
     }
 
@@ -480,7 +482,7 @@ public class MockEndpointTest extends Co
             assertMockEndpointsSatisfied();
             fail("Should have thrown exception");
         } catch (AssertionError e) {
-            assertEquals("mock://result Property with name bar. Expected: <cheese> but was: <beer>", e.getMessage());
+            assertEquals("mock://result Property with name bar for message: 0. Expected: <cheese> but was: <beer>", e.getMessage());
         }
     }
 
@@ -569,6 +571,204 @@ public class MockEndpointTest extends Co
         assertMockEndpointsSatisfied();
     }
 
+    public void testSetMultipleExpectedHeaders() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.expectedHeaderReceived("foo", 123);
+        mock.expectedHeaderReceived("bar", "beer");
+
+        Map<String, Object> map = new HashMap<String, Object>();
+        map.put("foo", 123);
+        map.put("bar", "beer");
+        template.sendBodyAndHeaders("direct:a", "Hello World", map);
+
+        mock.assertIsSatisfied();
+    }
+
+    public void testSetMultipleExpectedHeaders2() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(2);
+        mock.expectedHeaderReceived("foo", 123);
+        mock.expectedHeaderReceived("bar", "beer");
+
+        Map<String, Object> map = new HashMap<String, Object>();
+        map.put("foo", 123);
+        map.put("bar", "beer");
+        template.sendBodyAndHeaders("direct:a", "Hello World", map);
+        template.sendBodyAndHeaders("direct:a", "Hello World", map);
+
+        mock.assertIsSatisfied();
+    }
+
+    public void testSetMultipleExpectedHeaders3() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.expectedHeaderReceived("foo", 123);
+        mock.expectedHeaderReceived("bar", null);
+
+        Map<String, Object> map = new HashMap<String, Object>();
+        map.put("foo", 123);
+        template.sendBodyAndHeaders("direct:a", "Hello World", map);
+
+        mock.assertIsSatisfied();
+    }
+
+    public void testSetMultipleExpectedHeadersShouldFail() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.expectedHeaderReceived("foo", 123);
+        mock.expectedHeaderReceived("bar", "beer");
+
+        Map<String, Object> map = new HashMap<String, Object>();
+        map.put("foo", 456);
+        map.put("bar", "beer");
+        template.sendBodyAndHeaders("direct:a", "Hello World", map);
+
+        mock.assertIsNotSatisfied();
+    }
+
+    public void testSetMultipleExpectedHeadersShouldFail2() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.expectedHeaderReceived("foo", 123);
+        mock.expectedHeaderReceived("bar", "beer");
+
+        Map<String, Object> map = new HashMap<String, Object>();
+        map.put("foo", 123);
+        map.put("bar", "wine");
+        template.sendBodyAndHeaders("direct:a", "Hello World", map);
+
+        mock.assertIsNotSatisfied();
+    }
+
+    public void testSetMultipleExpectedHeadersShouldFail3() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(2);
+        mock.expectedHeaderReceived("foo", 123);
+        mock.expectedHeaderReceived("bar", "beer");
+
+        Map<String, Object> map = new HashMap<String, Object>();
+        map.put("foo", 123);
+        map.put("bar", "beer");
+        template.sendBodyAndHeaders("direct:a", "Hello World", map);
+
+        map = new HashMap<String, Object>();
+        map.put("foo", 123);
+        map.put("bar", "wine");
+        template.sendBodyAndHeaders("direct:a", "Hello World", map);
+
+        mock.assertIsNotSatisfied();
+    }
+
+    public void testSetMultipleExpectedProperties() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.expectedPropertyReceived("foo", 123);
+        mock.expectedPropertyReceived("bar", "beer");
+
+        template.send("direct:a", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.setProperty("foo", 123);
+                exchange.setProperty("bar", "beer");
+            }
+        });
+
+        mock.assertIsSatisfied();
+    }
+
+    public void testSetMultipleExpectedProperties2() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(2);
+        mock.expectedPropertyReceived("foo", 123);
+        mock.expectedPropertyReceived("bar", "beer");
+
+        template.send("direct:a", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.setProperty("foo", 123);
+                exchange.setProperty("bar", "beer");
+            }
+        });
+
+        template.send("direct:a", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.setProperty("foo", 123);
+                exchange.setProperty("bar", "beer");
+            }
+        });
+
+        mock.assertIsSatisfied();
+    }
+
+    public void testSetMultipleExpectedProperties3() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.expectedPropertyReceived("foo", 123);
+        mock.expectedPropertyReceived("bar", null);
+
+        template.send("direct:a", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.setProperty("foo", 123);
+            }
+        });
+
+        mock.assertIsSatisfied();
+    }
+
+    public void testSetMultipleExpectedPropertiesShouldFail() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.expectedPropertyReceived("foo", 123);
+        mock.expectedPropertyReceived("bar", "beer");
+
+        template.send("direct:a", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.setProperty("foo", 456);
+                exchange.setProperty("bar", "beer");
+            }
+        });
+
+        mock.assertIsNotSatisfied();
+    }
+
+    public void testSetMultipleExpectedPropertiesShouldFail2() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.expectedPropertyReceived("foo", 123);
+        mock.expectedPropertyReceived("bar", "beer");
+
+        template.send("direct:a", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.setProperty("foo", 123);
+                exchange.setProperty("bar", "wine");
+            }
+        });
+
+        mock.assertIsNotSatisfied();
+    }
+
+    public void testSetMultipleExpectedPropertiesShouldFail3() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(2);
+        mock.expectedPropertyReceived("foo", 123);
+        mock.expectedPropertyReceived("bar", "beer");
+
+        template.send("direct:a", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.setProperty("foo", 123);
+                exchange.setProperty("bar", "beer");
+            }
+        });
+
+        template.send("direct:a", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.setProperty("foo", 123);
+                exchange.setProperty("bar", "wine");
+            }
+        });
+
+        mock.assertIsNotSatisfied();
+    }
+
     protected void sendMessages(int... counters) {
         for (int counter : counters) {
             template.sendBodyAndHeader("direct:a", createTestMessage(counter), "counter", counter);

Modified: camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7DataFormatTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7DataFormatTest.java?rev=1140996&r1=1140995&r2=1140996&view=diff
==============================================================================
--- camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7DataFormatTest.java (original)
+++ camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7DataFormatTest.java Wed Jun 29 08:17:15 2011
@@ -52,7 +52,7 @@ public class HL7DataFormatTest extends C
         mock.expectedMessageCount(1);
         mock.message(0).body().isInstanceOf(Message.class);
 
-        mock.expectedHeaderReceived(HL7Constants.HL7_SENDING_APPLICATION, "MYSERVER");
+        mock.expectedHeaderReceived(HL7Constants.HL7_SENDING_APPLICATION, "MYSENDER");
         mock.expectedHeaderReceived(HL7Constants.HL7_SENDING_FACILITY, "MYSENDERAPP");
         mock.expectedHeaderReceived(HL7Constants.HL7_RECEIVING_APPLICATION, "MYCLIENT");
         mock.expectedHeaderReceived(HL7Constants.HL7_RECEIVING_FACILITY, "MYCLIENTAPP");