KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > j2ee > multiview > EBCMPFinderAndSelectMethodsTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.test.j2ee.multiview;
21
22 import java.io.File JavaDoc;
23 import java.lang.reflect.Modifier JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.LinkedList JavaDoc;
27 import java.util.List JavaDoc;
28 import junit.framework.AssertionFailedError;
29 import junit.textui.TestRunner;
30 import org.netbeans.api.project.Project;
31 import org.netbeans.jellytools.JellyTestCase;
32 import junit.framework.Test;
33 import org.netbeans.jmi.javamodel.JavaClass;
34 import org.netbeans.jmi.javamodel.Method;
35 import org.netbeans.junit.NbTestSuite;
36 import org.netbeans.modules.j2ee.common.JMIUtils;
37 import org.netbeans.modules.j2ee.dd.api.ejb.CmpField;
38 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider;
39 import org.netbeans.modules.j2ee.dd.api.ejb.Ejb;
40 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar;
41 import org.netbeans.modules.j2ee.dd.api.ejb.EnterpriseBeans;
42 import org.netbeans.modules.j2ee.dd.api.ejb.Entity;
43 import org.netbeans.modules.j2ee.dd.api.ejb.Query;
44 import org.netbeans.modules.j2ee.ddloaders.multiview.EjbJarMultiViewDataObject;
45 import org.netbeans.modules.j2ee.ddloaders.multiview.EntityHelper;
46 import org.netbeans.modules.j2ee.ddloaders.multiview.EntityNode;
47 import org.netbeans.modules.j2ee.ddloaders.multiview.FinderMethodsNode;
48 import org.netbeans.modules.j2ee.ddloaders.multiview.FinderMethodsTableModel;
49 import org.netbeans.modules.j2ee.ddloaders.multiview.QueryMethodHelper;
50 import org.netbeans.modules.j2ee.ddloaders.multiview.SelectMethodsNode;
51 import org.netbeans.modules.j2ee.ddloaders.multiview.SelectMethodsTableModel;
52 import org.netbeans.modules.j2ee.ejbcore.api.methodcontroller.EntityMethodController;
53 import org.netbeans.modules.j2ee.ejbjarproject.EjbJarProject;
54 import org.netbeans.test.j2ee.lib.J2eeProjectSupport;
55 import org.openide.cookies.EditCookie;
56 import org.openide.cookies.SaveCookie;
57 import org.openide.filesystems.FileObject;
58 import org.openide.loaders.DataObject;
59 import org.openide.nodes.Node;
60
61 /**
62  *
63  * @author jp159440
64  */

65 public class EBCMPFinderAndSelectMethodsTest extends JellyTestCase{
66     
67     public static final String JavaDoc EJB_PROJECT_NAME = "TestCMP";
68     public static final String JavaDoc EJB_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + EJB_PROJECT_NAME;
69     
70     private static Project project;
71     private static FileObject ddFo;
72     private static EjbJar ejbJar;
73     private static EjbJarMultiViewDataObject ddObj;
74     private static Entity bean;
75     private static CmpField[] cmpFields;
76     private static Query[] query;
77     
78     /** Creates a new instance of EBCMPFinderAndSelectMethods */
79     public EBCMPFinderAndSelectMethodsTest(String JavaDoc testName) {
80         super(testName);
81     }
82     
83     protected void setUp() throws Exception JavaDoc {
84         super.setUp();
85     }
86     
87     protected void tearDown() throws Exception JavaDoc {
88     }
89     
90     public static Test suite() {
91         NbTestSuite suite = new NbTestSuite();
92         suite.addTest(new EBCMPFinderAndSelectMethodsTest("testOpenProject"));
93         
94         //test select methods
95
suite.addTest(new EBCMPFinderAndSelectMethodsTest("testAddCMPSelectMethod"));
96         suite.addTest(new EBCMPFinderAndSelectMethodsTest("testExistingCMPSelectMethods"));
97         //suite.addTest(new EBCMPFinderAndSelectMethodsTest("testModifyNameCMPSelectMethod"));
98
suite.addTest(new EBCMPFinderAndSelectMethodsTest("testModifyReturnTypeCMPSelectMethod"));
99         suite.addTest(new EBCMPFinderAndSelectMethodsTest("testModifyQueryCMPSelectMethod"));
100         suite.addTest(new EBCMPFinderAndSelectMethodsTest("testModifyDescriptionCMPSelectMethod"));
101         suite.addTest(new EBCMPFinderAndSelectMethodsTest("testRemoveCMPSelectMethod"));
102         
103         //test finder methods
104
suite.addTest(new EBCMPFinderAndSelectMethodsTest("testExistingCMPFinderMethod"));
105         suite.addTest(new EBCMPFinderAndSelectMethodsTest("testAddCMPFinderMethod"));
106         suite.addTest(new EBCMPFinderAndSelectMethodsTest("testEditReturnCMPFinderMethod"));
107         suite.addTest(new EBCMPFinderAndSelectMethodsTest("testEditQueryCMPFinderMethod"));
108         suite.addTest(new EBCMPFinderAndSelectMethodsTest("testEditDescriptionCMPFinderMethod"));
109         suite.addTest(new EBCMPFinderAndSelectMethodsTest("testRemoveCMPFinderMethod"));
110         
111         suite.addTest(new EBCMPFinderAndSelectMethodsTest("testAddRemoteFinderMethod"));
112         suite.addTest(new EBCMPFinderAndSelectMethodsTest("testTransferRemote2LocalFinderMethod"));
113         suite.addTest(new EBCMPFinderAndSelectMethodsTest("testTransferLocal2RemoteFinderMethod"));
114         suite.addTest(new EBCMPFinderAndSelectMethodsTest("testBothInterfacesFinderMetohd"));
115         return suite;
116     }
117     
118     public void testOpenProject() throws Exception JavaDoc{
119         File JavaDoc projectDir = new File JavaDoc(EJB_PROJECT_PATH);
120         project = (Project)J2eeProjectSupport.openProject(projectDir);
121         assertNotNull("Project is null.", project);
122         Thread.sleep(1000);
123         
124         EjbJarProject ejbJarProject = (EjbJarProject)project;
125         ddFo = ejbJarProject.getAPIEjbJar().getDeploymentDescriptor(); // deployment descriptor
126
assertNotNull("ejb-jar.xml FileObject is null.", ddFo);
127         
128         ejbJar = DDProvider.getDefault().getDDRoot(ddFo);
129         
130         ddObj = (EjbJarMultiViewDataObject)DataObject.find(ddFo); //MultiView Editor
131
assertNotNull("MultiViewDO is null.",ddObj);
132         
133         EditCookie edit = (EditCookie)ddObj.getCookie(EditCookie.class);
134         edit.edit();
135         Thread.sleep(1000);
136         
137         // select CustomerBean
138
EnterpriseBeans beans = DDProvider.getDefault().getDDRoot(ddFo).getEnterpriseBeans();
139         bean = (Entity)beans.findBeanByName(EnterpriseBeans.ENTITY,
140                 Ejb.EJB_NAME ,"CustomerBean");
141         
142         cmpFields = bean.getCmpField();
143         
144         ddObj.showElement(bean); //open visual editor
145
Utils.waitForAWTDispatchThread();
146     }
147     
148     public static void main(java.lang.String JavaDoc[] args) {
149         // run only selected test case
150
TestRunner.run(suite());
151     }
152     
153     public void testExistingCMPFinderMethod() throws Exception JavaDoc {
154         // inspects the contents of FinderMethods table via table model
155
String JavaDoc[] methodsNames = {"findById","findByLastName","findByFirstName"};
156         String JavaDoc[] methodsQuerys = {"SELECT OBJECT(c) FROM Customer AS c WHERE c.id = ?1",
157         "SELECT OBJECT(c) FROM Customer AS c WHERE c.lastName = ?1",
158         "SELECT OBJECT(c) FROM Customer AS c WHERE c.firstName = ?1"};
159         String JavaDoc description = "auto generated method";
160         FinderMethodsNode fmn = getFinderMethodNode(ddObj);
161         fmn.getSectionNodePanel().open(); //open section in edior
162
EntityHelper entHelper = ddObj.getEntityHelper(bean);
163         FinderMethodsTableModel tableModel = entHelper.queries.getFinderMethodsTableModel();
164         assertTrue("FinderMethod table has unexpected number of rows",tableModel.getRowCount()==3);
165         for(int i=0;i<tableModel.getRowCount();i++) { //controling each row in table
166
assertEquals("Methods names do not match in "+i+". row",methodsNames[i],(String JavaDoc)tableModel.getValueAt(i,0));
167             assertTrue("Return Collection is not set corectlly in "+i+". row",((Boolean JavaDoc)tableModel.getValueAt(i,1)).booleanValue());
168             assertEquals("Result interfaces do not match in "+i+". row","local",(String JavaDoc)tableModel.getValueAt(i,2));
169             assertEquals("Querys do not match in "+i+". row",methodsQuerys[i],(String JavaDoc)tableModel.getValueAt(i,3));
170             assertEquals("Descriptions do not match in "+i+". row",description,(String JavaDoc)tableModel.getValueAt(i,4));
171         }
172     }
173     
174     public void testAddCMPFinderMethod() throws Exception JavaDoc {
175         String JavaDoc methodName = "findByField" ;
176         Method method = JMIUtils.createMethod(JMIUtils.findClass("cmp.CustomerBean"));
177         method.setName(methodName);
178         method.setType(JMIUtils.resolveType("java.util.Collection"));
179         JMIUtils.addException(method, "javax.ejb.FinderException");
180         EntityHelper entityHelper = new EntityHelper(ddObj, bean);
181         EntityMethodController emc = entityHelper.getEntityMethodController();
182         emc.addMethod(method,true,false);
183         emc.addEjbQl(method,"SELECT OBJECT(o) FROM Customer o",ddFo);
184         Thread.currentThread().sleep(500);
185         getRowNumberByName(methodName);
186         Utils utils = new Utils(this);
187         utils.checkInXML(ddObj,"<method-name>"+methodName+"</method-name>");
188         utils.checkInXML(ddObj,"<ejb-ql>SELECT OBJECT(o) FROM Customer o</ejb-ql>");
189         utils.checkFiles("testAddCMPFinderMethod", null, new String JavaDoc[]{"CustomerLocalHome.java"});
190     }
191     
192     public void testEditReturnCMPFinderMethod() throws Exception JavaDoc {
193         EntityHelper entHelper = ddObj.getEntityHelper(bean);
194         FinderMethodsTableModel tableModel = entHelper.queries.getFinderMethodsTableModel();
195         Utils utils = new Utils(this);
196         int row = getRowNumberByName("findByField");
197         tableModel.setValueAt(new Boolean JavaDoc(false),row,1);
198         Thread.currentThread().sleep(500);
199         QueryMethodHelper qmh = getQueryMethodHelperByName("findByField");
200         assertEquals("Model not updated","cmp.CustomerLocal",qmh.getReturnType());
201         utils.checkFiles("testEditReturnCMPFinderMethod",null,new String JavaDoc[]{"CustomerLocalHome.java"});
202     }
203     
204     public void testEditQueryCMPFinderMethod() throws Exception JavaDoc {
205         EntityHelper entHelper = ddObj.getEntityHelper(bean);
206         QueryMethodHelper helper = getQueryMethodHelperByName("findByField");
207         Utils utils = new Utils(this);
208         int rowIndex = getRowNumberByName("findByField");
209         boolean publishToLocal = helper.localMethod != null;
210         boolean publishToRemote = helper.remoteMethod != null;
211         boolean returnsCollection = helper.returnsCollection();
212         Method method = helper.getPrototypeMethod();
213         Query query = (Query) entHelper.queries.getFinderMethod(rowIndex).clone();
214         query.setEjbQl("XXXX");
215         helper.updateFinderMethod(method, query, !returnsCollection, publishToLocal, publishToRemote);
216         Thread.currentThread().sleep(500);
217         SaveCookie saveCookie = (SaveCookie)ddObj.getCookie(SaveCookie.class);
218         assertNotNull("Save cookie is null, Data object isn't changed!", saveCookie);
219         saveCookie.save();
220         utils.checkInXML(ddObj,"<ejb-ql>XXXX</ejb-ql>");
221         assertEquals("Table model not updated","XXXX",entHelper.queries.getFinderMethodsTableModel().getValueAt(rowIndex,3));
222         assertEquals("Model not updated","XXXX",helper.getEjbQl());
223     }
224     
225     public void testEditDescriptionCMPFinderMethod() throws Exception JavaDoc {
226         EntityHelper entHelper = ddObj.getEntityHelper(bean);
227         FinderMethodsTableModel tableModel = entHelper.queries.getFinderMethodsTableModel();
228         Utils utils = new Utils(this);
229         int rowIndex = getRowNumberByName("findByField");
230         tableModel.setValueAt("Modified description",rowIndex,4);
231         Thread.currentThread().sleep(500);
232         QueryMethodHelper helper = getQueryMethodHelperByName("findByField");
233         SaveCookie saveCookie = (SaveCookie)ddObj.getCookie(SaveCookie.class);
234         assertNotNull("Save cookie is null, Data object isn't changed!", saveCookie);
235         saveCookie.save();
236         utils.checkInXML(ddObj,"<description>Modified description</description>");
237         assertEquals("Table model not updated","Modified description",entHelper.queries.getFinderMethodsTableModel().getValueAt(rowIndex,4));
238         assertEquals("Model not updated","Modified description",helper.getDefaultDescription());
239     }
240     
241     public void testEditNameCMPFinderMethod() throws Exception JavaDoc {
242         //TODO
243
}
244     
245     public void testRemoveCMPFinderMethod() throws Exception JavaDoc {
246         EntityHelper entHelper = ddObj.getEntityHelper(bean);
247         EntityMethodController emc = entHelper.getEntityMethodController();
248         int row = getRowNumberByName("findByField");
249         entHelper.queries.getFinderMethodsTableModel().removeRow(row);
250         delayAndSave(ddObj);
251         Utils utils = new Utils(this);
252         utils.checkFiles("testRemoveCMPFinderMethod",null,new String JavaDoc[]{"CustomerLocalHome.java"});
253         try {
254             utils.checkInXML(ddObj,"<method-name>findByName</method-name>");
255         } catch (AssertionFailedError aef) {
256             //ok
257
return;
258         }
259         fail("Method not removed from dd");
260         return;
261     }
262     
263     public void testAddCMPSelectMethod() throws Exception JavaDoc {
264         SelectMethodsNode smn = getSelectMethodNode(ddObj);
265         smn.getSectionNodePanel().open();
266         Method method = JMIUtils.createMethod(JMIUtils.findClass("cmp.CustomerBean"));
267         method.setName("ejbSelectBy");
268         method.setType(JMIUtils.resolveType("int"));
269         method.setModifiers(Modifier.PUBLIC|Modifier.ABSTRACT);
270         JMIUtils.addException(method, "javax.ejb.FinderException");
271         EntityHelper entityHelper = new EntityHelper(ddObj, bean);
272         EntityMethodController emc = entityHelper.getEntityMethodController();
273         emc.addSelectMethod(method,"SELECT COUNT(o) FROM Customer o",ddFo);
274         Utils utils = new Utils(this);
275         utils.checkFiles("testAddCMPSelectMethod",null,new String JavaDoc[]{"CustomerBean.java"});
276         utils.checkInXML(ddObj,"<method-name>ejbSelectBy</method-name>");
277         
278     }
279     
280     public void testExistingCMPSelectMethods() throws Exception JavaDoc {
281         EntityHelper entHelper = ddObj.getEntityHelper(bean);
282         SelectMethodsTableModel tableModel = entHelper.queries.getSelectMethodsTableModel();
283         assertTrue("Wrong number of select methods",tableModel.getRowCount()==1);
284         assertEquals("Unexpected Select method name","ejbSelectBy",tableModel.getValueAt(0,0));
285         assertEquals("Unexpected Select method return type","int",tableModel.getValueAt(0,1));
286         assertEquals("Unexpected Select method query","SELECT COUNT(o) FROM Customer o",tableModel.getValueAt(0,2));
287         assertEquals("Unexpected Select method description",null,tableModel.getValueAt(0,3));
288     }
289     
290     
291     public void testModifyNameCMPSelectMethod() throws Exception JavaDoc {
292         EntityHelper entHelper = ddObj.getEntityHelper(bean);
293         SelectMethodsTableModel tableModel = entHelper.queries.getSelectMethodsTableModel();
294         Query query = (Query) entHelper.queries.getSelecMethod(0).clone();
295         QueryMethodHelper helper = tableModel.getQueryMethodHelper(0);
296         Method method = helper.getPrototypeMethod();
297         method.setType(JMIUtils.resolveType(method.getTypeName().getName()));
298         method.setName("ejbSelectByMod");
299         helper.updateSelectMethod(method,query);
300     }
301     
302     public void testModifyReturnTypeCMPSelectMethod() throws Exception JavaDoc {
303         EntityHelper entHelper = ddObj.getEntityHelper(bean);
304         SelectMethodsTableModel tableModel = entHelper.queries.getSelectMethodsTableModel();
305         Query query = (Query) entHelper.queries.getSelecMethod(0).clone();
306         QueryMethodHelper helper = tableModel.getQueryMethodHelper(0);
307         Method method = helper.getPrototypeMethod();
308         method.setType(JMIUtils.resolveType("char"));
309         helper.updateSelectMethod(method,query);
310         Utils utils = new Utils(this);
311         utils.checkFiles("testModifyReturnTypeCMPSelectMethod",null,new String JavaDoc[]{"CustomerBean.java"});
312     }
313     
314     public void testModifyQueryCMPSelectMethod() throws Exception JavaDoc {
315         EntityHelper entHelper = ddObj.getEntityHelper(bean);
316         SelectMethodsTableModel tableModel = entHelper.queries.getSelectMethodsTableModel();
317         Query query = (Query) entHelper.queries.getSelecMethod(0).clone();
318         QueryMethodHelper helper = tableModel.getQueryMethodHelper(0);
319         Method method = helper.getPrototypeMethod();
320         query.setEjbQl("XXXX");
321         helper.updateSelectMethod(method,query);
322         delayAndSave(ddObj);
323         Utils utils = new Utils(this);
324         utils.checkInXML(ddObj,"<ejb-ql>XXXX</ejb-ql>");
325     }
326     
327     public void testModifyDescriptionCMPSelectMethod() throws Exception JavaDoc {
328         EntityHelper entHelper = ddObj.getEntityHelper(bean);
329         SelectMethodsTableModel tableModel = entHelper.queries.getSelectMethodsTableModel();
330         tableModel.setValueAt("New description",0,3);
331         delayAndSave(ddObj);
332         Utils utils = new Utils(this);
333         utils.checkInXML(ddObj,"<description>New description</description>");
334     }
335     public void testRemoveCMPSelectMethod() throws Exception JavaDoc {
336         EntityHelper entHelper = ddObj.getEntityHelper(bean);
337         EntityMethodController emc = entHelper.getEntityMethodController();
338         entHelper.queries.getSelectMethodsTableModel().removeRow(0);
339         delayAndSave(ddObj);
340         Utils utils = new Utils(this);
341         utils.checkFiles("testRemoveCMPSelectMethod",null,new String JavaDoc[]{"CustomerBean.java"});
342         try {
343             utils.checkInXML(ddObj,"<method-name>ejbSelectBy</method-name>");
344         } catch (AssertionFailedError aef) {
345             //ok
346
return;
347         }
348         fail("Method not removed from dd");
349         return;
350     }
351     
352     public void testAddRemoteFinderMethod() throws Exception JavaDoc {
353         ddObj.getEntityHelper(bean).addInterfaces(false); //added remote interface (tested elsewhere)
354
String JavaDoc methodName = "findByAux" ;
355         String JavaDoc queryString ="SELECT OBJECT(o) FROM Customer AUX";
356         Method method = JMIUtils.createMethod(JMIUtils.findClass("cmp.CustomerBean"));
357         method.setName(methodName);
358         method.setType(JMIUtils.resolveType("java.util.Collection"));
359         JMIUtils.addException(method, "javax.ejb.FinderException");
360         //JMIUtils.addException(method, "java.rmi.RemoteException");
361
EntityHelper entityHelper = new EntityHelper(ddObj, bean);
362         EntityMethodController emc = entityHelper.getEntityMethodController();
363         emc.addMethod(method,false,false); //adding to remote iface
364
emc.addEjbQl(method,queryString,ddFo);
365         delayAndSave(ddObj);
366         //testing table model
367
int row = getRowNumberByName(methodName);
368         FinderMethodsTableModel tableModel = entityHelper.queries.getFinderMethodsTableModel();
369         assertEquals("Wrong Result Interface description",tableModel.getValueAt(row,2),"remote");
370         Utils utils = new Utils(this);
371         //testig XML
372
utils.checkInXML(ddObj,"<method-name>"+methodName+"</method-name>");
373         utils.checkInXML(ddObj,"<ejb-ql>"+queryString+"</ejb-ql>");
374         //testig classes
375
JavaClass remoteHome = entityHelper.getHomeInterfaceClass();
376         Method findMethod = getMethodByName(remoteHome,methodName);
377         assertNotNull("Method not found",findMethod);
378         assertEquals("Wrong return type","java.util.Collection",findMethod.getType().getName());
379         assertEquals("Wrong modifiers",(Modifier.ABSTRACT | Modifier.PUBLIC),findMethod.getModifiers());
380         List JavaDoc expectedEx = new ArrayList JavaDoc();
381         expectedEx.add("javax.ejb.FinderException");
382         expectedEx.add("java.rmi.RemoteException");
383         throwsExceptions(expectedEx,findMethod.getExceptions());
384     }
385     
386     public void testTransferRemote2LocalFinderMethod() throws Exception JavaDoc {
387         String JavaDoc methodName = "findByAux" ;
388         EntityHelper entHelper = ddObj.getEntityHelper(bean);
389         QueryMethodHelper helper = getQueryMethodHelperByName(methodName);
390         Utils utils = new Utils(this);
391         int rowIndex = getRowNumberByName(methodName);
392         FinderMethodsTableModel tableModel = entHelper.queries.getFinderMethodsTableModel();
393         boolean publishToLocal = true;
394         boolean publishToRemote = false;
395         boolean returnsCollection = helper.returnsCollection();
396         Method method = helper.getPrototypeMethod();
397         Query query = (Query) entHelper.queries.getFinderMethod(rowIndex).clone();
398         JMIUtils.addException(method, "javax.ejb.FinderException");
399         helper.updateFinderMethod(method, query, !returnsCollection, publishToLocal, publishToRemote);
400         //testing table model
401
assertEquals("Wrong Result Interface description",tableModel.getValueAt(rowIndex,2),"local");
402         //testing classes
403
JavaClass remoteHome = entHelper.getHomeInterfaceClass();
404         JavaClass localHome = entHelper.getLocalHomeInterfaceClass();
405         Method finder = getMethodByName(remoteHome,methodName);
406         assertNull("Method not deleted from remote interface",finder);
407         finder = getMethodByName(localHome,methodName);
408         assertNotNull("Method not found in local interface",finder);
409         assertEquals("Wrong return type","java.util.Collection",finder.getType().getName());
410         List JavaDoc expectedEx = new ArrayList JavaDoc();
411         expectedEx.add("javax.ejb.FinderException");
412         throwsExceptions(expectedEx,finder.getExceptions());
413     }
414     
415     public void testTransferLocal2RemoteFinderMethod() throws Exception JavaDoc {
416         String JavaDoc methodName = "findByFirstName" ;
417         EntityHelper entHelper = ddObj.getEntityHelper(bean);
418         QueryMethodHelper helper = getQueryMethodHelperByName(methodName);
419         FinderMethodsTableModel tableModel = entHelper.queries.getFinderMethodsTableModel();
420         Utils utils = new Utils(this);
421         int rowIndex = getRowNumberByName(methodName);
422         boolean publishToLocal = false;
423         boolean publishToRemote = true;
424         boolean returnsCollection = helper.returnsCollection();
425         Method method = helper.getPrototypeMethod();
426         Query query = (Query) entHelper.queries.getFinderMethod(rowIndex).clone();
427         JMIUtils.addException(method, "javax.ejb.FinderException");
428         helper.updateFinderMethod(method, query, !returnsCollection, publishToLocal, publishToRemote);
429         // testing table model
430
assertEquals("Wrong Result Interface description",tableModel.getValueAt(rowIndex,2),"remote");
431         //testing classes
432
JavaClass remoteHome = entHelper.getHomeInterfaceClass();
433         JavaClass localHome = entHelper.getLocalHomeInterfaceClass();
434         Method finder = getMethodByName(localHome,methodName);
435         assertNull("Method not deleted from local interface",finder);
436         finder = getMethodByName(remoteHome,methodName);
437         assertNotNull("Method not found in remote interface",finder);
438         assertEquals("Wrong return type","java.util.Collection",finder.getType().getName());
439         List JavaDoc expectedEx = new ArrayList JavaDoc();
440         expectedEx.add("javax.ejb.FinderException");
441         expectedEx.add("java.rmi.RemoteException");
442         throwsExceptions(expectedEx,finder.getExceptions());
443     }
444     
445     public void testBothInterfacesFinderMetohd() throws Exception JavaDoc {
446         String JavaDoc methodName = "findByFirstName" ;
447         EntityHelper entHelper = ddObj.getEntityHelper(bean);
448         QueryMethodHelper helper = getQueryMethodHelperByName(methodName);
449         FinderMethodsTableModel tableModel = entHelper.queries.getFinderMethodsTableModel();
450         Utils utils = new Utils(this);
451         int rowIndex = getRowNumberByName(methodName);
452         boolean publishToLocal = true;
453         boolean publishToRemote = true;
454         boolean returnsCollection = helper.returnsCollection();
455         Method method = helper.getPrototypeMethod();
456         Query query = (Query) entHelper.queries.getFinderMethod(rowIndex).clone();
457         helper.updateFinderMethod(method, query, !returnsCollection, publishToLocal, publishToRemote);
458         delayAndSave(ddObj);
459         // test table model (generation of methods tested in previous methods
460
assertEquals("Wrong Result Interface description",tableModel.getValueAt(rowIndex,2),"local+remote");
461     }
462     
463     private QueryMethodHelper getQueryMethodHelperByName(String JavaDoc name) {
464         EntityHelper entHelper = ddObj.getEntityHelper(bean);
465         Query[] queries = entHelper.queries.getQuery();
466         for (int i = 0; i < queries.length; i++) {
467             if(queries[i].getQueryMethod().getMethodName().equals(name)) return entHelper.queries.getQueryMethodHelper(queries[i]);
468         }
469         assertTrue("Querymethod "+name+" not found",false);
470         return null;
471     }
472     
473     private int getRowNumberByName(String JavaDoc name) {
474         EntityHelper entityHelper = new EntityHelper(ddObj, bean);
475         FinderMethodsTableModel tableModel = entityHelper.queries.getFinderMethodsTableModel();
476         int i;
477         for(i=0;i<tableModel.getRowCount();i++) {
478             if(tableModel.getValueAt(i,0).equals(name)) break;
479         }
480         assertTrue("Method "+name+" is not added to table model",i!=tableModel.getRowCount());
481         return i;
482     }
483     
484     private FinderMethodsNode getFinderMethodNode(EjbJarMultiViewDataObject ddObj) {
485         EntityNode entityNode = Utils.getEntityNode(ddObj);
486         assertNotNull("No Entitynode found",entityNode);
487         Node[] nodes = Utils.getChildrenNodes(entityNode);
488         for (int i = 0; i < nodes.length; i++) {
489             if(nodes[i] instanceof FinderMethodsNode) return (FinderMethodsNode)nodes[i];
490         }
491         fail("No FinderMethodsNode found");
492         return null;
493     }
494     
495     private SelectMethodsNode getSelectMethodNode(EjbJarMultiViewDataObject ddObj) {
496         EntityNode entityNode = Utils.getEntityNode(ddObj);
497         assertNotNull("No Entitynode found",entityNode);
498         Node[] nodes = Utils.getChildrenNodes(entityNode);
499         for (int i = 0; i < nodes.length; i++) {
500             if(nodes[i] instanceof SelectMethodsNode) return (SelectMethodsNode)nodes[i];
501         }
502         fail("No SelectMethodsNode found");
503         return null;
504     }
505     
506     private void delayAndSave(EjbJarMultiViewDataObject obj) throws Exception JavaDoc {
507         new StepIterator() {
508             SaveCookie saveCookie;
509             public boolean step() throws Exception JavaDoc {
510                 saveCookie = (SaveCookie) ddObj.getCookie(SaveCookie.class);
511                 return saveCookie!=null;
512             }
513         };
514         SaveCookie saveCookie = (SaveCookie) ddObj.getCookie(SaveCookie.class);
515         assertNotNull("Document was not modified",saveCookie);
516         saveCookie.save();
517     }
518     
519     private Method getMethodByName(JavaClass jc,String JavaDoc name) {
520         Method[] methods = JMIUtils.getMethods(jc);
521         for(int i=0;i<methods.length;i++) if(methods[i].getName().equals(name)) return methods[i];
522         return null;
523     }
524     
525     private boolean throwsExceptions(List JavaDoc expected,List JavaDoc currentlyThrown) throws Exception JavaDoc {
526         String JavaDoc temp;
527         // assertEquals("Count of expected and really declared exception differ",expected.size(),currentlyThrown.size());
528
List JavaDoc exNames = new LinkedList JavaDoc();
529         Iterator JavaDoc it = currentlyThrown.iterator();
530         while(it.hasNext()) {
531             temp = ((JavaClass)it.next()).getName();
532             assertTrue("Unexpected exception "+temp+" is declared to be thrown",expected.contains(temp));
533             exNames.add(temp);
534         }
535         it = expected.iterator();
536         while(it.hasNext()) {
537             temp = (String JavaDoc)it.next();
538             assertTrue("Expected exception "+temp+" is not declared to be thrown",exNames.contains(temp));
539         }
540         return true;
541     }
542     
543 }
544
Popular Tags