1 10 11 package org.mule.tck.providers; 12 13 import com.mockobjects.dynamic.C; 14 import com.mockobjects.dynamic.Mock; 15 16 import java.beans.ExceptionListener ; 17 import java.util.HashMap ; 18 19 import org.mule.MuleException; 20 import org.mule.MuleManager; 21 import org.mule.config.i18n.Message; 22 import org.mule.impl.ImmutableMuleEndpoint; 23 import org.mule.impl.MuleDescriptor; 24 import org.mule.impl.endpoint.MuleEndpoint; 25 import org.mule.impl.endpoint.MuleEndpointURI; 26 import org.mule.providers.AbstractConnector; 27 import org.mule.tck.AbstractMuleTestCase; 28 import org.mule.tck.testmodels.fruit.Apple; 29 import org.mule.umo.UMOComponent; 30 import org.mule.umo.endpoint.UMOEndpoint; 31 import org.mule.umo.provider.UMOConnector; 32 import org.mule.umo.provider.UMOMessageAdapter; 33 import org.mule.umo.provider.UMOMessageDispatcher; 34 35 39 public abstract class AbstractConnectorTestCase extends AbstractMuleTestCase 40 { 41 protected MuleDescriptor descriptor; 42 43 protected UMOConnector connector; 44 45 50 protected void doSetUp() throws Exception 51 { 52 getManager(true); 53 descriptor = getTestDescriptor("apple", Apple.class.getName()); 54 MuleManager.getInstance().start(); 55 connector = getConnector(); 56 } 57 58 protected void doTearDown() throws Exception 59 { 60 if (!connector.isDisposed()) 61 { 62 connector.dispose(); 63 } 64 } 65 66 public void testConnectorExceptionHandling() throws Exception 67 { 68 assertNotNull(connector); 69 70 Mock ehandlerMock = new Mock(ExceptionListener .class, "exceptionHandler"); 72 73 ehandlerMock.expect("exceptionThrown", C.isA(Exception .class)); 74 75 assertNotNull(connector.getExceptionListener()); 76 connector.setExceptionListener((ExceptionListener )ehandlerMock.proxy()); 77 connector.handleException(new MuleException(Message.createStaticMessage("Dummy"))); 78 79 if (connector instanceof AbstractConnector) 80 { 81 ehandlerMock.expect("exceptionThrown", C.isA(Exception .class)); 82 ((AbstractConnector)connector).exceptionThrown(new MuleException( 83 Message.createStaticMessage("Dummy"))); 84 } 85 86 ehandlerMock.verify(); 87 88 connector.setExceptionListener(null); 89 try 90 { 91 connector.handleException(new MuleException(Message.createStaticMessage("Dummy"))); 92 fail("Should have thrown exception as no strategy is set"); 93 } 94 catch (RuntimeException e) 95 { 96 } 98 } 99 100 public void testConnectorLifecycle() throws Exception 101 { 102 assertNotNull(connector); 103 104 assertTrue(!connector.isStarted()); 105 assertTrue(!connector.isDisposed()); 106 connector.startConnector(); 107 assertTrue(connector.isStarted()); 108 assertTrue(!connector.isDisposed()); 109 connector.stopConnector(); 110 assertTrue(!connector.isStarted()); 111 assertTrue(!connector.isDisposed()); 112 connector.dispose(); 113 assertTrue(!connector.isStarted()); 114 assertTrue(connector.isDisposed()); 115 116 try 117 { 118 connector.startConnector(); 119 fail("Connector cannot be restarted after being disposing"); 120 } 121 catch (Exception e) 122 { 123 } 125 } 126 127 public void testConnectorListenerSupport() throws Exception 128 { 129 assertNotNull(connector); 130 131 MuleDescriptor d = getTestDescriptor("anApple", Apple.class.getName()); 132 133 UMOComponent component = MuleManager.getInstance().getModel().registerComponent(d); 134 UMOEndpoint endpoint = new MuleEndpoint("test", new MuleEndpointURI(getTestEndpointURI()), connector, 135 null, UMOEndpoint.ENDPOINT_TYPE_SENDER, 0, null, new HashMap ()); 136 137 try 138 { 139 connector.registerListener(null, null); 140 fail("cannot register null"); 141 } 142 catch (Exception e) 143 { 144 } 145 146 try 147 { 148 connector.registerListener(null, endpoint); 149 fail("cannot register null"); 150 } 151 catch (Exception e) 152 { 153 } 154 155 try 156 { 157 connector.registerListener(component, null); 158 fail("cannot register null"); 159 } 160 catch (Exception e) 161 { 162 } 163 164 connector.registerListener(component, endpoint); 165 166 connector.unregisterListener(component, endpoint); 168 try 170 { 171 connector.unregisterListener(null, null); 172 fail("cannot unregister null"); 173 } 174 catch (Exception e) 175 { 176 } 178 try 179 { 180 connector.unregisterListener(component, null); 181 fail("cannot unregister null"); 182 } 183 catch (Exception e) 184 { 185 } 187 188 try 189 { 190 connector.unregisterListener(null, endpoint); 191 fail("cannot unregister null"); 192 } 193 catch (Exception e) 194 { 195 } 197 connector.unregisterListener(component, endpoint); 198 MuleManager.getInstance().getModel().unregisterComponent(d); 199 } 200 201 public void testConnectorBeanProps() throws Exception 202 { 203 assertNotNull(connector); 204 205 try 206 { 207 connector.setName(null); 208 fail("Should throw IllegalArgumentException if name set to null"); 209 } 210 catch (IllegalArgumentException e) 211 { 212 } 213 214 connector.setName("Test"); 215 assertEquals("Test", connector.getName()); 216 217 assertNotNull("Protocol must be set as a constant", connector.getProtocol()); 218 219 } 220 221 public void testConnectorMessageAdapter() throws Exception 222 { 223 UMOConnector connector = getConnector(); 224 assertNotNull(connector); 225 UMOMessageAdapter adapter = connector.getMessageAdapter(getValidMessage()); 226 assertNotNull(adapter); 227 } 228 229 public void testConnectorMessageDispatcher() throws Exception 230 { 231 UMOConnector connector = getConnector(); 232 assertNotNull(connector); 233 assertNotNull(connector.getDispatcherFactory()); 234 UMOMessageDispatcher dispatcher = connector.getDispatcher(new ImmutableMuleEndpoint("test", 235 new MuleEndpointURI(getTestEndpointURI()), connector, null, UMOEndpoint.ENDPOINT_TYPE_SENDER, 0, 236 null, null)); 237 assertNotNull(dispatcher); 238 assertEquals(connector, dispatcher.getConnector()); 239 } 240 241 public void testConnectorInitialise() throws Exception 242 { 243 UMOConnector connector = getConnector(); 244 245 try 246 { 247 connector.initialise(); 248 fail("A connector cannot be initialised more than once"); 249 } 250 catch (Exception e) 251 { 252 } 254 } 255 256 public abstract UMOConnector getConnector() throws Exception ; 257 258 public abstract Object getValidMessage() throws Exception ; 259 260 public abstract String getTestEndpointURI(); 261 } 262 | Popular Tags |