View Javadoc
1   /*
2    * Copyright 2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *     
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.platform.camel.core.camel.exception;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.fail;
20  
21  import org.apache.camel.EndpointInject;
22  import org.apache.camel.RuntimeCamelException;
23  import org.apache.camel.component.mock.MockEndpoint;
24  import org.junit.After;
25  import org.junit.Test;
26  import org.openehealth.ipf.platform.camel.core.camel.TestSupport;
27  import org.springframework.test.context.ContextConfiguration;
28  
29  
30  /**
31   * @author Martin Krasser
32   */
33  @ContextConfiguration(locations = {"/context-camel-exception.xml"})
34  public class ExceptionHandlingTest extends TestSupport {
35  
36      @EndpointInject(uri="mock:success")
37      private MockEndpoint success;
38      
39      @EndpointInject(uri="mock:error1")
40      private MockEndpoint error1;
41      
42      @EndpointInject(uri="mock:error2")
43      private MockEndpoint error2;
44      
45      @EndpointInject(uri="mock:error3")
46      private MockEndpoint error3;
47  
48      @After
49      public void tearDown() throws Exception {
50          success.reset();
51          error1.reset();
52          error2.reset();
53          error3.reset();
54      }
55  
56      @Test
57      public void testError1() throws Exception {
58          error1.expectedMessageCount(1);
59          error2.expectedMessageCount(0);
60          error3.expectedMessageCount(0);
61          success.expectedMessageCount(0);
62          try {
63              producerTemplate.sendBody("direct:input", "blah");
64              fail("failure not reported");
65          } catch (RuntimeCamelException e) {
66              assertEquals(Exception1.class, e.getCause().getClass());
67          }
68          error1.assertIsSatisfied();
69          error2.assertIsSatisfied();
70          error3.assertIsSatisfied();
71          success.assertIsSatisfied();
72      }
73   
74      @Test
75      public void testError2() throws Exception {
76          error1.expectedMessageCount(0);
77          error2.expectedMessageCount(1);
78          error3.expectedMessageCount(0);
79          success.expectedMessageCount(0);
80          try {
81              producerTemplate.sendBody("direct:input", "blub");
82          } catch (RuntimeCamelException e) {
83              assertEquals(Exception2.class, e.getCause().getClass());
84          }
85          error1.assertIsSatisfied();
86          error2.assertIsSatisfied();
87          error3.assertIsSatisfied();
88          success.assertIsSatisfied();
89      }
90   
91      @Test
92      public void testError3() throws Exception {
93          error1.expectedMessageCount(0);
94          error2.expectedMessageCount(0);
95          error3.expectedMessageCount(1);
96          success.expectedMessageCount(0);
97          try {
98              producerTemplate.sendBody("direct:input", "oink");
99          } catch (RuntimeCamelException e) {
100             assertEquals(Exception3.class, e.getCause().getClass());
101         }
102         error1.assertIsSatisfied();
103         error2.assertIsSatisfied();
104         error3.assertIsSatisfied();
105         success.assertIsSatisfied();
106     }
107  
108     @Test
109     public void testSuccess() throws Exception {
110         error1.expectedMessageCount(0);
111         error2.expectedMessageCount(0);
112         error3.expectedMessageCount(0);
113         success.expectedMessageCount(1);
114         producerTemplate.sendBody("direct:input", "clean");
115         error1.assertIsSatisfied();
116         error2.assertIsSatisfied();
117         error3.assertIsSatisfied();
118         success.assertIsSatisfied();
119     }
120  
121 }