1 22 package org.jboss.ejb3.test.dd.unit; 23 24 import java.net.URL ; 25 import java.util.Collection ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import org.jboss.ejb3.metamodel.AssemblyDescriptor; 29 import org.jboss.ejb3.metamodel.CmrField; 30 import org.jboss.ejb3.metamodel.ContainerTransaction; 31 import org.jboss.ejb3.metamodel.EjbJarDD; 32 import org.jboss.ejb3.metamodel.EjbJarDDObjectFactory; 33 import org.jboss.ejb3.metamodel.EjbRelation; 34 import org.jboss.ejb3.metamodel.EjbRelationshipRole; 35 import org.jboss.ejb3.metamodel.EnterpriseBean; 36 import org.jboss.ejb3.metamodel.EnterpriseBeans; 37 import org.jboss.ejb3.metamodel.EntityEnterpriseBean; 38 import org.jboss.ejb3.metamodel.JBossDDObjectFactory; 39 import org.jboss.ejb3.metamodel.MessageDrivenBean; 40 import org.jboss.ejb3.metamodel.MessageDrivenDestination; 41 import org.jboss.ejb3.metamodel.Method; 42 import org.jboss.ejb3.metamodel.MethodPermission; 43 import org.jboss.ejb3.metamodel.RelationshipRoleSource; 44 import org.jboss.ejb3.metamodel.Relationships; 45 import org.jboss.ejb3.metamodel.SecurityIdentity; 46 import org.jboss.ejb3.metamodel.Service; 47 import org.jboss.ejb3.metamodel.SessionEnterpriseBean; 48 import org.jboss.logging.Logger; 49 import org.jboss.xb.binding.ObjectModelFactory; 50 import org.jboss.xb.binding.Unmarshaller; 51 import org.jboss.xb.binding.UnmarshallerFactory; 52 import junit.framework.Test; 53 import junit.framework.TestCase; 54 import junit.framework.TestSuite; 55 56 import org.jboss.metamodel.descriptor.EjbLocalRef; 57 import org.jboss.metamodel.descriptor.NameValuePair; 58 import org.jboss.metamodel.descriptor.ResourceRef; 59 import org.jboss.metamodel.descriptor.SecurityRole; 60 61 68 69 public class EjbJarXmlTestCase extends TestCase 70 { 71 72 private static final Logger log = Logger.getLogger(EjbJarXmlTestCase.class); 73 74 public EjbJarXmlTestCase(String name) 75 { 76 77 super(name); 78 79 } 80 81 public void testUnmarshalDDXsd() throws Exception 83 { 84 ObjectModelFactory factory = new EjbJarDDObjectFactory(); 86 URL xmlUrl = getResourceUrl("dd/ejb-jar.xml"); 87 assertNotNull(xmlUrl); 88 Unmarshaller unmarshaller = UnmarshallerFactory.newInstance() 89 .newUnmarshaller(); 90 EjbJarDD dd = (EjbJarDD) unmarshaller.unmarshal(xmlUrl.openStream(), 91 factory, null); 92 assertNotNull(dd); 93 94 factory = new JBossDDObjectFactory(dd); 95 xmlUrl = getResourceUrl("dd/jboss.xml"); 96 assertNotNull(xmlUrl); 97 unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller(); 98 dd = (EjbJarDD) unmarshaller 99 .unmarshal(xmlUrl.openStream(), factory, null); 100 101 checkUnmarshalledDD(dd); 102 } 103 104 public void testUnmarshalMdb() throws Exception 105 { 106 ObjectModelFactory factory = new EjbJarDDObjectFactory(); 108 URL xmlUrl = getResourceUrl("dd/mdb/META-INF/ejb-jar.xml"); 109 assertNotNull(xmlUrl); 110 Unmarshaller unmarshaller = UnmarshallerFactory.newInstance() 111 .newUnmarshaller(); 112 EjbJarDD dd = (EjbJarDD) unmarshaller.unmarshal(xmlUrl.openStream(), 113 factory, null); 114 assertNotNull(dd); 115 116 factory = new JBossDDObjectFactory(dd); 117 xmlUrl = getResourceUrl("dd/mdb/META-INF/jboss.xml"); 118 assertNotNull(xmlUrl); 119 unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller(); 120 dd = (EjbJarDD) unmarshaller 121 .unmarshal(xmlUrl.openStream(), factory, null); 122 123 checkUnmarshalledMdb(dd); 124 } 125 126 private void checkUnmarshalledDD(EjbJarDD dd) 127 { 128 log.debug("unmarshalled DD: " + dd); 129 130 assertEquals("2.1", dd.getVersion()); 131 assertEquals("DukesBankEJBJAR", dd.getDisplayName()); 132 133 assertEquals("java:/jaas/dukesbank", dd.getSecurityDomain()); 134 135 Iterator ejbs = dd.getEnterpriseBeans().getEnterpriseBeans().iterator(); 136 assertNotNull(ejbs); 137 assertEquals(8, dd.getEnterpriseBeans().getEnterpriseBeans().size()); 138 139 EnterpriseBean ejb = null; 140 141 ejb = (EnterpriseBean) ejbs.next(); 142 assertEquals("ServiceSix", ejb.getEjbName()); 143 assertEquals("org.jboss.ejb3.test.service.ServiceSixLocal", ejb.getLocal()); 144 assertEquals("org.jboss.ejb3.test.service.ServiceSixRemote", ejb.getRemote()); 145 assertTrue(ejb instanceof Service); 146 Service service = (Service)ejb; 147 assertEquals("org.jboss.ejb3.test.service.ServiceSixManagement", service.getManagement()); 148 149 ejbs.next(); 150 ejb = (EnterpriseBean) ejbs.next(); 151 assertEquals("CustomerBean", ejb.getEjbName()); 152 assertTrue(ejb instanceof EntityEnterpriseBean); 153 assertEquals("Container", ((EntityEnterpriseBean) ejb) 154 .getPersistenceType()); 155 156 Relationships relationships = dd.getRelationships(); 157 assertNotNull(relationships); 158 List relations = relationships.getEjbRelations(); 159 assertEquals(2, relations.size()); 160 EjbRelation relation = (EjbRelation) relations.get(0); 161 assertEquals("account-customer", relation.getEjbRelationName()); 162 List roles = relation.getEjbRelationshipRoles(); 163 assertEquals(2, roles.size()); 164 EjbRelationshipRole role = (EjbRelationshipRole) roles.get(0); 165 assertEquals("customer-belongs-to-account", role 166 .getEjbRelationshipRoleName()); 167 assertEquals("Many", role.getMultiplicity()); 168 assertFalse(role.isCascadeDelete()); 169 RelationshipRoleSource source = role.getRelationshipRoleSource(); 170 assertNotNull(source); 171 assertEquals("CustomerBean", source.getEjbName()); 172 CmrField field = role.getCmrField(); 173 assertNotNull(field); 174 assertEquals("accounts", field.getCmrFieldName()); 175 assertEquals("java.util.Collection", field.getCmrFieldType()); 176 177 ejbs.next(); 178 ejbs.next(); 179 ejbs.next(); 180 ejb = (EnterpriseBean) ejbs.next(); 181 assertEquals("AccountControllerBean", ejb.getEjbName()); 182 assertEquals("java:/jaas/override", ejb.getSecurityDomain()); 183 184 assertEquals("com.sun.ebank.ejb.account.AccountControllerHome", ejb 185 .getHome()); 186 assertEquals("com.sun.ebank.ejb.account.AccountController", ejb 187 .getRemote()); 188 assertEquals("com.sun.ebank.ejb.account.AccountControllerBean", ejb 189 .getEjbClass()); 190 log.info("ejb.getLocal() " + ejb.getLocal()); 191 assertNull(ejb.getLocal()); 192 assertNull(ejb.getLocalHome()); 193 assertTrue(ejb instanceof SessionEnterpriseBean); 194 SessionEnterpriseBean session = (SessionEnterpriseBean) ejb; 195 assertEquals("Stateful", session.getSessionType()); 196 assertEquals(javax.ejb.TransactionManagementType.CONTAINER, session.getTransactionManagementType()); 197 assertEquals("ebankAccountController", session.getJndiName()); 198 SecurityIdentity si = session.getSecurityIdentity(); 199 assertNotNull(si); 200 assertTrue(si.isUseCallerIdentity()); 201 Collection ejblocalRefs = session.getEjbLocalRefs(); 202 assertEquals(3, ejblocalRefs.size()); 203 EjbLocalRef ejbLocalRef = (EjbLocalRef) ejblocalRefs.iterator().next(); 204 assertEquals("ejb/customer", ejbLocalRef.getEjbRefName()); 205 assertEquals("Entity", ejbLocalRef.getEjbRefType()); 206 assertEquals("com.sun.ebank.ejb.customer.LocalCustomerHome", ejbLocalRef 207 .getLocalHome()); 208 assertEquals("com.sun.ebank.ejb.customer.LocalCustomer", ejbLocalRef 209 .getLocal()); 210 assertEquals("CustomerBean", ejbLocalRef.getEjbLink()); 211 Collection resourceRefs = session.getResourceRefs(); 212 assertEquals(1, resourceRefs.size()); 213 ResourceRef resourceEnvRef = (ResourceRef) resourceRefs.iterator().next(); 214 assertEquals("jdbc/BankDB", resourceEnvRef.getResRefName()); 215 assertEquals("javax.sql.DataSource", resourceEnvRef.getResType()); 216 assertEquals("Container", resourceEnvRef.getResAuth()); 217 assertEquals("Shareable", resourceEnvRef.getResSharingScope()); 218 219 AssemblyDescriptor descriptor = dd.getAssemblyDescriptor(); 220 assertNotNull(descriptor); 221 List securityRoles = descriptor.getSecurityRoles(); 222 assertEquals(2, securityRoles.size()); 223 SecurityRole securityRole = (SecurityRole) securityRoles.get(0); 224 assertEquals("bankCustomer", securityRole.getRoleName()); 225 List methodPermissions = descriptor.getMethodPermissions(); 226 assertEquals(7, methodPermissions.size()); 227 MethodPermission methodPermission = (MethodPermission) methodPermissions 228 .get(0); 229 List roleNames = methodPermission.getRoleNames(); 230 assertNotNull(roleNames); 231 assertEquals(1, roleNames.size()); 232 assertEquals("bankCustomer", roleNames.get(0)); 233 List <Method> methods = methodPermission.getMethods(); 234 assertNotNull(methods); 235 assertEquals(methods.size(), 1); 236 Method method = methods.get(0); 237 assertEquals("CustomerBean", method.getEjbName()); 238 assertEquals("*", method.getMethodName()); 239 List containerTransactions = descriptor.getContainerTransactions(); 240 assertEquals(7, containerTransactions.size()); 241 ContainerTransaction containerTransaction = (ContainerTransaction) containerTransactions 242 .get(0); 243 assertEquals("Required", containerTransaction.getTransAttribute()); 244 method = containerTransaction.getMethod(); 245 assertNotNull(method); 246 assertEquals("AccountControllerBean", method.getEjbName()); 247 assertEquals("*", method.getMethodName()); 248 } 249 250 private void checkUnmarshalledMdb(EjbJarDD dd) 251 { 252 log.debug("unmarshalled DD: " + dd); 253 254 EnterpriseBeans ejbs = dd.getEnterpriseBeans(); 255 assertNotNull(ejbs); 256 assertEquals(9, ejbs.getEnterpriseBeans().size()); 257 Iterator ejbIterator = ejbs.getEnterpriseBeans().iterator(); 258 259 ejbIterator.next(); 260 ejbIterator.next(); 261 EnterpriseBean ejb = (EnterpriseBean)ejbIterator.next(); 262 assertEquals("ObjectMessageBean", ejb.getEjbName()); 263 assertEquals("org.jboss.ejb3.test.dd.mdb.ObjectMessageBean",ejb.getEjbClass()); 264 assertTrue(ejb instanceof MessageDrivenBean); 265 MessageDrivenBean mdb = (MessageDrivenBean) ejb; 266 NameValuePair property = (NameValuePair)mdb.getActivationConfig().getActivationConfigProperties().get(0); 267 assertEquals("AUTO_ACKNOWLEDGE", property.getValue()); 268 assertEquals("Bean", mdb.getTransactionType()); 269 MessageDrivenDestination destination = mdb.getMessageDrivenDestination(); 270 assertNotNull(destination); 271 assertEquals("javax.jms.Queue",destination.getDestinationType()); 272 } 273 274 private static URL getResourceUrl(String name) 275 { 276 URL url = Thread.currentThread().getContextClassLoader() 277 .getResource(name); 278 if (url == null) 279 { 280 throw new IllegalStateException ("Resource not found: " + name); 281 } 282 return url; 283 } 284 285 public static Test suite() throws Exception 286 { 287 return new TestSuite(EjbJarXmlTestCase.class); 288 } 289 290 } 291 | Popular Tags |