KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > dd > unit > EjbJarXmlTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.test.dd.unit;
23
24 import java.net.URL JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
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 /**
62  * JUnit TestCase for JbossXB usage for ejb-jar.xml deployment descriptor for
63  * version 1.4 schema
64  *
65  * @version <tt>$Revision: 45409 $</tt>
66  * @author <a HREF="mailto:bdecoste@jboss.com">William DeCoste</a>
67  */

68
69 public class EjbJarXmlTestCase extends TestCase
70 {
71
72    private static final Logger log = Logger.getLogger(EjbJarXmlTestCase.class);
73
74    public EjbJarXmlTestCase(String JavaDoc name)
75    {
76
77       super(name);
78
79    }
80
81    // Tests
82
public void testUnmarshalDDXsd() throws Exception JavaDoc
83    {
84       // create an object model factory
85
ObjectModelFactory factory = new EjbJarDDObjectFactory();
86       URL JavaDoc 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 JavaDoc
105    {
106       // create an object model factory
107
ObjectModelFactory factory = new EjbJarDDObjectFactory();
108       URL JavaDoc 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 JavaDoc 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 JavaDoc relations = relationships.getEjbRelations();
159       assertEquals(2, relations.size());
160       EjbRelation relation = (EjbRelation) relations.get(0);
161       assertEquals("account-customer", relation.getEjbRelationName());
162       List JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc securityRoles = descriptor.getSecurityRoles();
222       assertEquals(2, securityRoles.size());
223       SecurityRole securityRole = (SecurityRole) securityRoles.get(0);
224       assertEquals("bankCustomer", securityRole.getRoleName());
225       List JavaDoc methodPermissions = descriptor.getMethodPermissions();
226       assertEquals(7, methodPermissions.size());
227       MethodPermission methodPermission = (MethodPermission) methodPermissions
228             .get(0);
229       List JavaDoc roleNames = methodPermission.getRoleNames();
230       assertNotNull(roleNames);
231       assertEquals(1, roleNames.size());
232       assertEquals("bankCustomer", roleNames.get(0));
233       List JavaDoc<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 JavaDoc 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 JavaDoc 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 JavaDoc getResourceUrl(String JavaDoc name)
275    {
276       URL JavaDoc url = Thread.currentThread().getContextClassLoader()
277             .getResource(name);
278       if (url == null)
279       {
280          throw new IllegalStateException JavaDoc("Resource not found: " + name);
281       }
282       return url;
283    }
284
285    public static Test suite() throws Exception JavaDoc
286    {
287       return new TestSuite(EjbJarXmlTestCase.class);
288    }
289
290 }
291
Popular Tags