1 17 18 package org.sape.carbon.services.ejb.local.test; 19 20 21 import java.rmi.RemoteException ; 22 23 import javax.ejb.CreateException ; 24 import javax.ejb.EJBHome ; 25 import javax.naming.Context ; 26 import javax.naming.NamingException ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 import junit.framework.Test; 32 import junit.framework.TestCase; 33 import junit.framework.TestSuite; 34 35 import org.sape.carbon.core.component.Lookup; 36 import org.sape.carbon.core.config.Config; 37 38 import org.sape.carbon.services.ejb.HomeFactoryException; 39 import org.sape.carbon.services.ejb.remote.RemoteHomeFactory; 40 import org.sape.carbon.services.jndi.InitialContextFactory; 41 import org.sape.carbon.services.jndi.InitialContextFactoryConfiguration; 42 43 public class LocalHomeFactoryTest extends TestCase { 44 45 48 private Log log = LogFactory.getLog(this.getClass()); 49 50 53 public static final String TEST_LOCAL_HOME_FACTORY = 54 "/ejb/test/LocalHomeFactoryTest"; 55 56 57 60 public static final String TEST_REMOTE_HOME_FACTORY = 61 "/ejb/test/RemoteHomeFactoryTest"; 62 63 67 public static final String TEST_MISCAST_HOME_FACTORY = 68 "/ejb/test/MiscastRemoteHomeFactoryTest"; 69 70 73 public static final String TEST_INITIAL_CONTEXT_FACTORY = 74 "/ejb/test/TestInitialContextFactory"; 75 76 79 public static final String TEST_REMOTE_EJB = 80 "org.sape.carbon.services.ejb.local.test.LocalGateway"; 81 82 83 86 public static final String TEST_LOCAL_EJB = 87 "org.sape.carbon.services.ejb.local.test.LocalTester"; 88 89 90 94 public static final String TEST_NONEXISTANT_EJB = 95 "org.sape.carbon.services.ejb.test.Nonexistant"; 96 97 98 public LocalHomeFactoryTest(String name) { 99 super(name); 100 } 101 102 103 public static void main(String args[]) throws Exception { 104 } 105 106 107 112 public void testLocalHomeFactoryClassCastException() { 113 114 EJBHome ejbHome = null; 115 116 try { 118 RemoteHomeFactory homeFactory = (RemoteHomeFactory) 119 Lookup.getInstance().fetchComponent(TEST_MISCAST_HOME_FACTORY); 120 121 ejbHome = homeFactory.lookup(TEST_REMOTE_EJB); 122 123 fail("Lookup for: " 124 + TEST_REMOTE_EJB 125 + " failed to intercept HomeFactoryClassCastException"); 126 } catch (HomeFactoryException hfe) { 127 } 129 } 130 131 132 136 public void testLocalHomeFactoryNamingException() { 137 138 EJBHome ejbHome = null; 139 140 RemoteHomeFactory homeFactory = (RemoteHomeFactory) 141 Lookup.getInstance().fetchComponent(TEST_REMOTE_HOME_FACTORY); 142 143 try { 145 146 ejbHome = homeFactory.lookup(TEST_NONEXISTANT_EJB); 147 148 fail("Lookup for: " 149 + TEST_NONEXISTANT_EJB 150 + " failed to intercept HomeFactoryNamingException"); 151 } catch (HomeFactoryException hfe) { 152 } 154 155 156 try { 158 159 ejbHome = homeFactory.lookup(TEST_REMOTE_EJB); 160 161 LocalGateway gateway = ((LocalGatewayHome) ejbHome).create(); 162 163 if (log.isTraceEnabled()) { 164 log.trace("Invoking test method on: " 165 + TEST_REMOTE_EJB 166 + " to validate LocalHomeFactory functionality"); 167 } 168 gateway.testLocalHomeFactoryNamingException(); 169 170 fail("Lookup for: " 171 + TEST_NONEXISTANT_EJB 172 + " failed to intercept HomeFactoryNamingException"); 173 } catch (HomeFactoryException hfe) { 174 } catch (RemoteException re) { 176 fail("Failed test; caught RemoteException: " + re); 177 } catch (CreateException ce) { 178 fail("Failed test; caught CreateException: " + ce); 179 } 180 } 181 182 183 186 public void testLocalHomeFactoryLookup() { 187 188 EJBHome ejbHome = null; 189 190 try { 191 RemoteHomeFactory homeFactory = (RemoteHomeFactory) 192 Lookup.getInstance().fetchComponent(TEST_REMOTE_HOME_FACTORY); 193 194 ejbHome = homeFactory.lookup(TEST_REMOTE_EJB); 195 } catch (HomeFactoryException hfe) { 196 fail("Failed to lookup home interface for EJB with logical name: " 197 + TEST_REMOTE_EJB 198 + " due to: " 199 + hfe); 200 } 201 202 try { 204 205 LocalGateway gateway = ((LocalGatewayHome) ejbHome).create(); 206 207 if (log.isTraceEnabled()) { 208 log.trace("Invoking test method on: " 209 + TEST_REMOTE_EJB 210 + " to validate LocalHomeFactory functionality"); 211 } 212 gateway.testLocalHomeFactoryLookup(); 213 214 } catch (Exception e) { 215 fail("Lookup for local EJB: " 216 + TEST_LOCAL_EJB 217 + " failed due to: " 218 + e); 219 } 220 } 221 222 223 227 public void testLocalHomeFactoryLookupWithContext() { 228 229 EJBHome ejbHome = null; 230 231 try { 232 InitialContextFactory factory = (InitialContextFactory) 233 Lookup.getInstance().fetchComponent(TEST_INITIAL_CONTEXT_FACTORY); 234 Context context = factory.getContext(); 235 236 RemoteHomeFactory homeFactory = (RemoteHomeFactory) 237 Lookup.getInstance().fetchComponent(TEST_REMOTE_HOME_FACTORY); 238 239 ejbHome = homeFactory.lookup(TEST_REMOTE_EJB, context); 240 } catch (HomeFactoryException hfe) { 241 fail("Failed to lookup home interface for EJB with logical name: " 242 + TEST_REMOTE_EJB 243 + " due to: " 244 + hfe); 245 } catch (NamingException ne) { 246 fail("Failed to lookup home interface for EJB with logical name: " 247 + TEST_REMOTE_EJB 248 + " due to: " 249 + ne); 250 } 251 252 try { 254 255 LocalGateway gateway = ((LocalGatewayHome) ejbHome).create(); 256 257 if (log.isTraceEnabled()) { 258 log.trace("Invoking test method on: " 259 + TEST_REMOTE_EJB 260 + " to validate LocalHomeFactory functionality"); 261 } 262 gateway.testLocalHomeFactoryLookupWithContext(); 263 264 } catch (Exception e) { 265 fail("Lookup for local EJB: " 266 + TEST_LOCAL_EJB 267 + " failed due to: " 268 + e); 269 } 270 } 271 272 273 277 public void testLocalHomeFactoryLookupWithCredentials() { 278 279 EJBHome ejbHome = null; 280 281 try { 282 RemoteHomeFactory homeFactory = (RemoteHomeFactory) 283 Lookup.getInstance().fetchComponent(TEST_REMOTE_HOME_FACTORY); 284 285 InitialContextFactoryConfiguration factoryConfig = 286 (InitialContextFactoryConfiguration) 287 Config.getInstance().fetchConfiguration(TEST_INITIAL_CONTEXT_FACTORY); 288 289 ejbHome = homeFactory.lookup(TEST_REMOTE_EJB, 290 factoryConfig.getEnvironment(Context.SECURITY_PRINCIPAL), 291 factoryConfig.getEnvironment(Context.SECURITY_CREDENTIALS)); 292 293 } catch (HomeFactoryException hfe) { 294 fail("Failed to lookup home interface for EJB with logical name: " 295 + TEST_REMOTE_EJB 296 + " due to: " 297 + hfe); 298 } 299 300 try { 302 303 LocalGateway gateway = ((LocalGatewayHome) ejbHome).create(); 304 305 if (log.isTraceEnabled()) { 306 log.trace("Invoking test method on: " 307 + TEST_REMOTE_EJB 308 + " to validate LocalHomeFactory functionality"); 309 } 310 gateway.testLocalHomeFactoryLookup(); 311 312 } catch (Exception e) { 313 fail("Lookup for local EJB: " 314 + TEST_LOCAL_EJB 315 + " failed due to: " 316 + e); 317 } 318 } 319 320 321 325 public static Test suite() { 326 327 TestSuite test = new TestSuite(); 328 329 test.addTest(new LocalHomeFactoryTest( 330 "testLocalHomeFactoryClassCastException")); 331 test.addTest(new LocalHomeFactoryTest( 332 "testLocalHomeFactoryNamingException")); 333 test.addTest(new LocalHomeFactoryTest( 334 "testLocalHomeFactoryLookup")); 335 test.addTest(new LocalHomeFactoryTest( 336 "testLocalHomeFactoryLookupWithContext")); 337 test.addTest(new LocalHomeFactoryTest( 338 "testLocalHomeFactoryLookupWithCredentials")); 339 340 return test; 341 } 342 } 343 | Popular Tags |