KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > ra > MessageEndpointProxyTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.ra;
19
20 import org.apache.activemq.ra.ActiveMQEndpointWorker;
21 import org.apache.activemq.ra.InvalidMessageEndpointException;
22 import org.apache.activemq.ra.MessageEndpointProxy;
23 import org.jmock.MockObjectTestCase;
24 import org.jmock.Mock;
25
26 import javax.jms.MessageListener JavaDoc;
27 import javax.jms.Message JavaDoc;
28 import javax.resource.spi.endpoint.MessageEndpoint JavaDoc;
29 import javax.resource.ResourceException JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
31
32 /**
33  * @author <a HREF="mailto:michael.gaffney@panacya.com">Michael Gaffney </a>
34  */

35 public class MessageEndpointProxyTest extends MockObjectTestCase {
36
37     private Mock mockEndpoint;
38     private Mock stubMessage;
39     private MessageEndpointProxy endpointProxy;
40
41     public MessageEndpointProxyTest(String JavaDoc name) {
42         super(name);
43     }
44     
45     protected void setUp() {
46         mockEndpoint = new Mock(EndpointAndListener.class);
47         stubMessage = new Mock(Message JavaDoc.class);
48         endpointProxy = new MessageEndpointProxy((MessageEndpoint JavaDoc) mockEndpoint.proxy());
49     }
50
51     public void testInvalidConstruction() {
52         Mock mockEndpoint = new Mock(MessageEndpoint JavaDoc.class);
53         try {
54             MessageEndpointProxy proxy = new MessageEndpointProxy((MessageEndpoint JavaDoc) mockEndpoint.proxy());
55             fail("An exception should have been thrown");
56         } catch (IllegalArgumentException JavaDoc e) {
57             assertTrue(true);
58         }
59     }
60
61     public void testSuccessfulCallSequence() {
62         setupBeforeDeliverySuccessful();
63         setupOnMessageSuccessful();
64         setupAfterDeliverySuccessful();
65
66         doBeforeDeliveryExpectSuccess();
67         doOnMessageExpectSuccess();
68         doAfterDeliveryExpectSuccess();
69     }
70
71     public void testBeforeDeliveryFailure() {
72         mockEndpoint.expects(once()).method("beforeDelivery").with(isA(Method JavaDoc.class))
73                 .will(throwException(new ResourceException JavaDoc()));
74         mockEndpoint.expects(never()).method("onMessage");
75         mockEndpoint.expects(never()).method("afterDelivery");
76         setupExpectRelease();
77
78         try {
79             endpointProxy.beforeDelivery(ActiveMQEndpointWorker.ON_MESSAGE_METHOD);
80             fail("An exception should have been thrown");
81         } catch (Exception JavaDoc e) {
82             assertTrue(true);
83         }
84         doOnMessageExpectInvalidMessageEndpointException();
85         doAfterDeliveryExpectInvalidMessageEndpointException();
86         
87         doFullyDeadCheck();
88     }
89
90     public void testOnMessageFailure() {
91         setupBeforeDeliverySuccessful();
92         mockEndpoint.expects(once()).method("onMessage").with(same(stubMessage.proxy()))
93                 .will(throwException(new RuntimeException JavaDoc()));
94         setupAfterDeliverySuccessful();
95
96         doBeforeDeliveryExpectSuccess();
97         try {
98             endpointProxy.onMessage((Message JavaDoc) stubMessage.proxy());
99             fail("An exception should have been thrown");
100         } catch (Exception JavaDoc e) {
101             assertTrue(true);
102         }
103         doAfterDeliveryExpectSuccess();
104
105     }
106
107     public void testAfterDeliveryFailure() {
108         setupBeforeDeliverySuccessful();
109         setupOnMessageSuccessful();
110         mockEndpoint.expects(once()).method("afterDelivery")
111                 .will(throwException(new ResourceException JavaDoc()));
112         setupExpectRelease();
113
114         doBeforeDeliveryExpectSuccess();
115         doOnMessageExpectSuccess();
116         try {
117             endpointProxy.afterDelivery();
118             fail("An exception should have been thrown");
119         } catch (Exception JavaDoc e) {
120             assertTrue(true);
121         }
122
123         doFullyDeadCheck();
124     }
125
126     private void doFullyDeadCheck() {
127         doBeforeDeliveryExpectInvalidMessageEndpointException();
128         doOnMessageExpectInvalidMessageEndpointException();
129         doAfterDeliveryExpectInvalidMessageEndpointException();
130         doReleaseExpectInvalidMessageEndpointException();
131     }
132
133     private void setupAfterDeliverySuccessful() {
134         mockEndpoint.expects(once()).method("afterDelivery");
135     }
136
137     private void setupOnMessageSuccessful() {
138         mockEndpoint.expects(once()).method("onMessage").with(same(stubMessage.proxy()));
139     }
140
141     private void setupBeforeDeliverySuccessful() {
142         mockEndpoint.expects(once()).method("beforeDelivery").with(isA(Method JavaDoc.class));
143     }
144
145     private void setupExpectRelease() {
146         mockEndpoint.expects(once()).method("release");
147     }
148
149     private void doBeforeDeliveryExpectSuccess() {
150         try {
151             endpointProxy.beforeDelivery(ActiveMQEndpointWorker.ON_MESSAGE_METHOD);
152         } catch (Exception JavaDoc e) {
153             fail("No exception should have been thrown");
154         }
155     }
156
157     private void doOnMessageExpectSuccess() {
158         try {
159             endpointProxy.onMessage((Message JavaDoc) stubMessage.proxy());
160         } catch (Exception JavaDoc e) {
161             fail("No exception should have been thrown");
162         }
163     }
164
165     private void doAfterDeliveryExpectSuccess() {
166         try {
167             endpointProxy.afterDelivery();
168         } catch (Exception JavaDoc e) {
169             fail("No exception should have been thrown");
170         }
171     }
172
173     private void doBeforeDeliveryExpectInvalidMessageEndpointException() {
174         try {
175             endpointProxy.beforeDelivery(ActiveMQEndpointWorker.ON_MESSAGE_METHOD);
176             fail("An InvalidMessageEndpointException should have been thrown");
177         } catch (InvalidMessageEndpointException e) {
178             assertTrue(true);
179         } catch (Exception JavaDoc e) {
180             fail("An InvalidMessageEndpointException should have been thrown");
181         }
182     }
183
184     private void doOnMessageExpectInvalidMessageEndpointException() {
185         try {
186             endpointProxy.onMessage((Message JavaDoc) stubMessage.proxy());
187             fail("An InvalidMessageEndpointException should have been thrown");
188         } catch (InvalidMessageEndpointException e) {
189             assertTrue(true);
190         }
191     }
192
193     private void doAfterDeliveryExpectInvalidMessageEndpointException() {
194         try {
195             endpointProxy.afterDelivery();
196             fail("An InvalidMessageEndpointException should have been thrown");
197         } catch (InvalidMessageEndpointException e) {
198             assertTrue(true);
199         } catch (Exception JavaDoc e) {
200             fail("An InvalidMessageEndpointException should have been thrown");
201         }
202     }
203
204     private void doReleaseExpectInvalidMessageEndpointException() {
205         try {
206             endpointProxy.release();
207             fail("An InvalidMessageEndpointException should have been thrown");
208         } catch (InvalidMessageEndpointException e) {
209             assertTrue(true);
210         }
211     }
212
213     private interface EndpointAndListener extends MessageListener JavaDoc, MessageEndpoint JavaDoc {
214     }
215 }
216
Popular Tags