1 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 ; 27 import javax.jms.Message ; 28 import javax.resource.spi.endpoint.MessageEndpoint ; 29 import javax.resource.ResourceException ; 30 import java.lang.reflect.Method ; 31 32 35 public class MessageEndpointProxyTest extends MockObjectTestCase { 36 37 private Mock mockEndpoint; 38 private Mock stubMessage; 39 private MessageEndpointProxy endpointProxy; 40 41 public MessageEndpointProxyTest(String name) { 42 super(name); 43 } 44 45 protected void setUp() { 46 mockEndpoint = new Mock(EndpointAndListener.class); 47 stubMessage = new Mock(Message .class); 48 endpointProxy = new MessageEndpointProxy((MessageEndpoint ) mockEndpoint.proxy()); 49 } 50 51 public void testInvalidConstruction() { 52 Mock mockEndpoint = new Mock(MessageEndpoint .class); 53 try { 54 MessageEndpointProxy proxy = new MessageEndpointProxy((MessageEndpoint ) mockEndpoint.proxy()); 55 fail("An exception should have been thrown"); 56 } catch (IllegalArgumentException 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 .class)) 73 .will(throwException(new ResourceException ())); 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 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 ())); 94 setupAfterDeliverySuccessful(); 95 96 doBeforeDeliveryExpectSuccess(); 97 try { 98 endpointProxy.onMessage((Message ) stubMessage.proxy()); 99 fail("An exception should have been thrown"); 100 } catch (Exception 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 ())); 112 setupExpectRelease(); 113 114 doBeforeDeliveryExpectSuccess(); 115 doOnMessageExpectSuccess(); 116 try { 117 endpointProxy.afterDelivery(); 118 fail("An exception should have been thrown"); 119 } catch (Exception 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 .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 e) { 153 fail("No exception should have been thrown"); 154 } 155 } 156 157 private void doOnMessageExpectSuccess() { 158 try { 159 endpointProxy.onMessage((Message ) stubMessage.proxy()); 160 } catch (Exception e) { 161 fail("No exception should have been thrown"); 162 } 163 } 164 165 private void doAfterDeliveryExpectSuccess() { 166 try { 167 endpointProxy.afterDelivery(); 168 } catch (Exception 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 e) { 180 fail("An InvalidMessageEndpointException should have been thrown"); 181 } 182 } 183 184 private void doOnMessageExpectInvalidMessageEndpointException() { 185 try { 186 endpointProxy.onMessage((Message ) 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 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 , MessageEndpoint { 214 } 215 } 216 | Popular Tags |