KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
24 import javax.swing.text.StyledDocument JavaDoc;
25 import junit.textui.TestRunner;
26 import org.netbeans.api.project.FileOwnerQuery;
27 import org.netbeans.api.project.Project;
28 import junit.framework.Test;
29 import org.netbeans.jmi.javamodel.JavaClass;
30 import org.netbeans.junit.NbTestSuite;
31 import org.netbeans.modules.j2ee.common.JMIUtils;
32 import org.netbeans.modules.j2ee.dd.api.common.ResourceRef;
33 import org.netbeans.modules.j2ee.dd.api.ejb.CmpField;
34 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar;
35 import org.netbeans.modules.j2ee.dd.api.ejb.EnterpriseBeans;
36 import org.netbeans.modules.j2ee.dd.api.ejb.Entity;
37 import org.netbeans.modules.j2ee.dd.api.ejb.Relationships;
38 import org.netbeans.modules.j2ee.ddloaders.multiview.BeanEnvironmentNode;
39 import org.netbeans.modules.j2ee.ddloaders.multiview.CmpRelationshipsTableModel;
40 import org.netbeans.modules.j2ee.ddloaders.multiview.EjbJarMultiViewDataObject;
41 import org.netbeans.modules.j2ee.ddloaders.multiview.EjbReferencesNode;
42 import org.netbeans.modules.j2ee.ddloaders.multiview.EjbReferencesTableModel;
43 import org.netbeans.modules.j2ee.ddloaders.multiview.EnvironmentEntriesNode;
44 import org.netbeans.modules.j2ee.ddloaders.multiview.EnvironmentEntriesTableModel;
45 import org.netbeans.modules.j2ee.ddloaders.multiview.InnerTablePanel;
46 import org.netbeans.modules.j2ee.ddloaders.multiview.RelationshipHelper;
47 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider;
48 import org.netbeans.modules.j2ee.ddloaders.multiview.ResourceEnvironmentReferencesNode;
49 import org.netbeans.modules.j2ee.ddloaders.multiview.ResourceEnvironmentReferencesTableModel;
50 import org.netbeans.modules.j2ee.ddloaders.multiview.ResourceReferencesNode;
51 import org.netbeans.modules.j2ee.ddloaders.multiview.ResourceReferencesTableModel;
52 import org.netbeans.modules.j2ee.ddloaders.multiview.SecurityRoleReferencesNode;
53 import org.netbeans.modules.j2ee.ddloaders.multiview.SecurityRoleReferencesTableModel;
54 import org.netbeans.modules.j2ee.api.ejbjar.EjbReference;
55 import org.netbeans.modules.j2ee.ejbjarproject.EjbJarProject;
56 import org.netbeans.modules.j2ee.dd.api.ejb.Ejb;
57 import org.netbeans.modules.xml.multiview.ToolBarMultiViewElement;
58 import org.netbeans.modules.xml.multiview.XmlMultiViewEditorSupport;
59 import org.netbeans.spi.project.ui.LogicalViewProvider;
60 import org.netbeans.modules.javacore.api.JavaModel;
61 import org.netbeans.modules.xml.multiview.ui.SectionNodePanel;
62 import org.netbeans.test.j2ee.lib.J2eeProjectSupport;
63 import org.openide.cookies.EditCookie;
64 import org.openide.cookies.SaveCookie;
65 import org.openide.filesystems.FileUtil;
66 import org.openide.loaders.DataObject;
67 import org.openide.nodes.Node;
68
69 /**
70  *
71  * @author jhorvath
72  */

73 public class EnvironmentPanelTest extends DDTestCase {
74     public static final String JavaDoc EJB_PROJECT_NAME = "TestCMPRelationships";
75     public static final String JavaDoc EJB_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + EJB_PROJECT_NAME;
76
77     private static final String JavaDoc TEST_BEAN_NAME="EntityABean";
78     
79     public EnvironmentPanelTest(String JavaDoc testName) {
80         super(testName);
81     }
82     
83     protected void setUp() throws Exception JavaDoc {
84         super.setUp();
85         System.out.println("######## "+getName()+" #######");
86     }
87     
88     protected void tearDown() throws Exception JavaDoc {
89     }
90     
91     public static Test suite() {
92         NbTestSuite suite = new NbTestSuite();
93         
94         suite.addTest(new EnvironmentPanelTest("testOpenProject"));
95           
96         suite.addTest(new EnvironmentPanelTest("testAddBeanReference"));
97         
98         suite.addTest(new EnvironmentPanelTest("testEnvironmentEntries"));
99        
100         suite.addTest(new EnvironmentPanelTest("testXMTToVisualSync"));
101        
102         suite.addTest(new EnvironmentPanelTest("testResourceReferences"));
103         suite.addTest(new EnvironmentPanelTest("testResourceEnvironmentReferences"));
104         suite.addTest(new EnvironmentPanelTest("testSecurityRoleReferences"));
105         
106         suite.addTest(new EnvironmentPanelTest("testModifyEnvironmentEntries"));
107         suite.addTest(new EnvironmentPanelTest("testModifyResourceReferences"));
108         suite.addTest(new EnvironmentPanelTest("testModifyResourceEnvironmentReferences"));
109         suite.addTest(new EnvironmentPanelTest("testModifySecurityRoleReferences"));
110         
111         //suite.addTest(new EnvironmentPanelTest("testRemoveEnvironmentEntries"));
112
suite.addTest(new EnvironmentPanelTest("testRemoveResourceReferences"));
113         suite.addTest(new EnvironmentPanelTest("testRemoveResourceEnvironmentReferences"));
114         suite.addTest(new EnvironmentPanelTest("testRemoveSecurityRoleReferences"));
115         
116         return suite;
117     }
118     
119     /** Use for execution inside IDE */
120     public static void main(java.lang.String JavaDoc[] args) {
121         // run only selected test case
122
TestRunner.run(suite());
123     }
124     
125     /*
126      * Method open project
127      *
128      */

129     public void testOpenProject() throws Exception JavaDoc{
130         File JavaDoc projectDir = new File JavaDoc(EJB_PROJECT_PATH);
131         project = (Project)J2eeProjectSupport.openProject(projectDir);
132         assertNotNull("Project is null.", project);
133         Thread.sleep(1000);
134         
135         EjbJarProject ejbJarProject = (EjbJarProject)project;
136         ddFo = ejbJarProject.getAPIEjbJar().getDeploymentDescriptor(); // deployment descriptor
137
assertNotNull("ejb-jar.xml FileObject is null.", ddFo);
138         
139         ejbJar = DDProvider.getDefault().getDDRoot(ddFo);
140         
141         ddObj = (EjbJarMultiViewDataObject)DataObject.find(ddFo); //MultiView Editor
142
assertNotNull("MultiViewDO is null.",ddObj);
143         
144         EditCookie edit = (EditCookie)ddObj.getCookie(EditCookie.class);
145         edit.edit();
146         
147         Thread.sleep(500);
148         showBean();
149         Utils.waitForAWTDispatchThread();
150     }
151     
152     public void testXMTToVisualSync() throws Exception JavaDoc {
153         ddObj.openView(2);
154         ddObj.createInputStream();
155         XmlMultiViewEditorSupport editor = (XmlMultiViewEditorSupport)ddObj.createCookie(EditCookie.class);
156         StyledDocument JavaDoc document=editor.getDocument();
157         String JavaDoc str=document.getText(0,document.getLength());
158         int testPos=str.indexOf("testEntry");
159         document.insertString(testPos, "XXXX", null);
160         showBean();
161         Thread.sleep(1500);
162         EnvironmentEntriesTableModel model=(EnvironmentEntriesTableModel) getEnvironmentEntriesPanel().getTable().getModel();
163         boolean fail=false;
164         if (!"XXXXtestEntry".equals(model.getValueAt(0, 0))) fail=true;
165         document.remove(testPos, 4);
166         if (fail) fail("change is not propagated");
167     }
168     
169     public void testEnvironmentEntries() throws Exception JavaDoc{
170         Thread.sleep(500);
171         EnvironmentEntriesTableModel model=(EnvironmentEntriesTableModel) getEnvironmentEntriesPanel().getTable().getModel();
172         model.addRow();
173         model.setValueAt("testEntry", 0, 0);
174         model.setValueAt("true", 0, 2);
175         model.setValueAt("test", 0, 3);
176         Utils utils = new Utils(this);
177         utils.checkInXML(ddObj, "<env-entry-name>testEntry</env-entry-name>");
178         utils.save(ddObj);
179         assertFile("ejb-jar.xml");
180     }
181     
182     public void testModifyEnvironmentEntries() throws Exception JavaDoc{
183         Thread.sleep(500);
184         EnvironmentEntriesTableModel model=(EnvironmentEntriesTableModel) getEnvironmentEntriesPanel().getTable().getModel();
185         model.setValueAt("testEntryModified", 0, 0);
186         model.setValueAt("false", 0, 2);
187         model.setValueAt("change", 0, 3);
188         Utils utils = new Utils(this);
189         utils.checkInXML(ddObj, "<env-entry-name>testEntryModified</env-entry-name>");
190         utils.save(ddObj);
191         assertFile("ejb-jar.xml");
192     }
193     
194   /* public void testRemoveEnvironmentEntries() throws Exception{
195         Thread.sleep(500);
196         EnvironmentEntriesTableModel model=(EnvironmentEntriesTableModel) getEnvironmentEntriesPanel().getTable().getModel();
197         model.removeRow(0);
198         Utils utils = new Utils(this);
199         utils.save(ddObj);
200         assertFile("ejb-jar.xml");
201     }*/

202     
203     public void testResourceReferences() throws Exception JavaDoc{
204         
205         Thread.sleep(500);
206         ResourceReferencesTableModel model=(ResourceReferencesTableModel) getResourceReferencesPanel().getTable().getModel();
207         model.addRow("test");
208         model.setValueAt("test", 0, 1);
209         model.setValueAt(ResourceRef.RES_SHARING_SCOPE_SHAREABLE, 0, 3);
210         model.setValueAt("test", 0, 4);
211         Thread.sleep(500);
212         Utils utils = new Utils(this);
213         utils.save(ddObj);
214         assertFile("ejb-jar.xml");
215     }
216     
217     public void testModifyResourceReferences() throws Exception JavaDoc{
218         
219         Thread.sleep(500);
220         ResourceReferencesTableModel model=(ResourceReferencesTableModel) getResourceReferencesPanel().getTable().getModel();
221         model.setValueAt("test1", 0, 1);
222         model.setValueAt(ResourceRef.RES_SHARING_SCOPE_SHAREABLE, 0, 3);
223         model.setValueAt("test1", 0, 4);
224         Thread.sleep(500);
225         Utils utils = new Utils(this);
226         utils.save(ddObj);
227         assertFile("ejb-jar.xml");
228     }
229     
230     public void testRemoveResourceReferences() throws Exception JavaDoc{
231         
232         Thread.sleep(5000);
233         ResourceReferencesTableModel model=(ResourceReferencesTableModel) getResourceReferencesPanel().getTable().getModel();
234         model.removeRow(0);
235         Thread.sleep(5000);
236         Utils utils = new Utils(this);
237         utils.save(ddObj);
238         assertFile("ejb-jar.xml");
239     }
240     
241     public void testResourceEnvironmentReferences() throws Exception JavaDoc{
242         Thread.sleep(500);
243         ResourceEnvironmentReferencesTableModel model=(ResourceEnvironmentReferencesTableModel) getResourceEnvironmentReferencesPanel().getTable().getModel();
244         model.addRow();
245         model.setValueAt("test", 0, 0);
246         model.setValueAt("test", 0, 1);
247         model.setValueAt("test", 0, 2);
248         Thread.sleep(500);
249         Utils utils = new Utils(this);
250         utils.save(ddObj);
251         assertFile("ejb-jar.xml");
252     }
253     
254     public void testModifyResourceEnvironmentReferences() throws Exception JavaDoc{
255         Thread.sleep(500);
256         ResourceEnvironmentReferencesTableModel model=(ResourceEnvironmentReferencesTableModel) getResourceEnvironmentReferencesPanel().getTable().getModel();
257         model.setValueAt("test1", 0, 0);
258         model.setValueAt("test1", 0, 1);
259         model.setValueAt("test1", 0, 2);
260         Thread.sleep(500);
261         Utils utils = new Utils(this);
262         utils.save(ddObj);
263         assertFile("ejb-jar.xml");
264     }
265     
266     public void testRemoveResourceEnvironmentReferences() throws Exception JavaDoc{
267         Thread.sleep(500);
268         ResourceEnvironmentReferencesTableModel model=(ResourceEnvironmentReferencesTableModel) getResourceEnvironmentReferencesPanel().getTable().getModel();
269         model.removeRow(0);
270         Thread.sleep(500);
271         Utils utils = new Utils(this);
272         utils.save(ddObj);
273         assertFile("ejb-jar.xml");
274     }
275     
276     public void testSecurityRoleReferences() throws Exception JavaDoc{
277         Thread.sleep(500);
278         SecurityRoleReferencesTableModel model=(SecurityRoleReferencesTableModel) getSecurityRoleReferencesPanel().getTable().getModel();
279         model.addRow();
280         model.setValueAt("test", 0, 0);
281         model.setValueAt("test", 0, 1);
282         model.setValueAt("test", 0, 2);
283         Thread.sleep(500);
284         Utils utils = new Utils(this);
285         utils.save(ddObj);
286         assertFile("ejb-jar.xml");
287     }
288     
289     public void testModifySecurityRoleReferences() throws Exception JavaDoc{
290         Thread.sleep(500);
291         SecurityRoleReferencesTableModel model=(SecurityRoleReferencesTableModel) getSecurityRoleReferencesPanel().getTable().getModel();
292         model.setValueAt("test1", 0, 0);
293         model.setValueAt("test1", 0, 1);
294         model.setValueAt("test1", 0, 2);
295         Thread.sleep(500);
296         Utils utils = new Utils(this);
297         utils.save(ddObj);
298         assertFile("ejb-jar.xml");
299     }
300     
301     public void testRemoveSecurityRoleReferences() throws Exception JavaDoc{
302         Thread.sleep(500);
303         SecurityRoleReferencesTableModel model=(SecurityRoleReferencesTableModel) getSecurityRoleReferencesPanel().getTable().getModel();
304         model.removeRow(0);
305         Thread.sleep(500);
306         Utils utils = new Utils(this);
307         utils.save(ddObj);
308         assertFile("ejb-jar.xml");
309     }
310     
311     public void testAddBeanReference() throws Exception JavaDoc {
312         EnterpriseBeans eb=ddObj.getEjbJar().getEnterpriseBeans();
313         Entity entityA=eb.getEntity(0);
314         EjbReferencesTableModel model=(EjbReferencesTableModel) getEjbReferencesPanel().getTable().getModel();
315         Thread.sleep(500);
316         JavaClass beanClass = (JavaClass) JMIUtils.resolveType(entityA.getEjbClass());
317         Node ejbNode = getEjbNode(beanClass, "EntityBBean");
318         EjbReference ref = (EjbReference) ejbNode.getCookie(org.netbeans.modules.j2ee.api.ejbjar.EjbReference.class);
319         Thread.sleep(500);
320         org.netbeans.modules.j2ee.ejbcore.Utils.addReference(beanClass, ref, null, true, true, "ejb/Test", project);
321         Thread.sleep(500);
322         model.refreshView();
323         assertEquals("bad reference name", "ejb/Test", model.getValueAt(0,0));
324         assertEquals("bad bean name", "EntityBBean", model.getValueAt(0,1));
325         assertEquals("local/remote", "remote", model.getValueAt(0,2));
326         Utils utils = new Utils(this);
327         assertFile("ejb-jar.xml");
328     }
329     
330     protected Node getEjbNode(JavaClass beanClass, String JavaDoc ejbName) {
331         Project[] allProjects = org.netbeans.modules.j2ee.ejbcore.Utils.getCallableEjbProjects(project);
332         for (int i = 0; i < allProjects.length; i++) {
333             LogicalViewProvider lvp =
334                     (LogicalViewProvider) allProjects[i].getLookup().lookup(LogicalViewProvider.class);
335             Node projectView = lvp.createLogicalView();
336             Node [] nodes = projectView.getChildren().getNodes();
337             for (int j = 0; j < nodes.length; j++) {
338                 if ("EJBS".equals(nodes[j].getName())) {
339                     Node [] ejbs = nodes[j].getChildren().getNodes();
340                     for (int k = 0; k < ejbs.length; k++) {
341                         System.out.println("*** "+ejbs[k].getName());
342                         if (ejbName.equals(ejbs[k].getName())) return ejbs[k];
343                     }
344                 }
345             }
346         }
347         return null;
348     }
349     
350     //open bean's panel in visual editor
351
protected void showBean() throws IOException JavaDoc {
352         EnterpriseBeans beans = DDProvider.getDefault().getDDRoot(ddFo).getEnterpriseBeans();
353         bean = (Entity)beans.findBeanByName(EnterpriseBeans.ENTITY,
354                 Ejb.EJB_NAME , TEST_BEAN_NAME);
355         ddObj.showElement(bean);
356     }
357     
358     protected BeanEnvironmentNode getBeanEnvironmentNode(){
359         try {
360             showBean();
361             try {
362                 Thread.sleep(500);
363             } catch (InterruptedException JavaDoc ex) {
364                 ex.printStackTrace();
365             }
366             Node[] nnChild = Utils.getChildrenNodes(Utils.getEntityNode(ddObj));
367             for(int k = 0; k < nnChild.length; k++){
368                 if(nnChild[k] instanceof BeanEnvironmentNode) {
369                     SectionNodePanel envPanel = ((BeanEnvironmentNode)nnChild[k]).getSectionNodePanel();
370                     envPanel.open();
371                     return (BeanEnvironmentNode) nnChild[k];
372                 }
373             }
374         } catch (IOException JavaDoc ex) {
375             ex.printStackTrace();
376         }
377         return null;
378     }
379     
380     protected InnerTablePanel getEnvironmentEntriesPanel(){
381         Node[] nnChild = Utils.getChildrenNodes(getBeanEnvironmentNode());
382         for(int k = 0; k < nnChild.length; k++){
383             if(nnChild[k] instanceof EnvironmentEntriesNode) {
384                 SectionNodePanel environmentPanel = ((EnvironmentEntriesNode)nnChild[k]).getSectionNodePanel();
385                 environmentPanel.open();
386                 return (InnerTablePanel) environmentPanel.getInnerPanel();
387             }
388         }
389         return null;
390     }
391     
392     protected InnerTablePanel getResourceReferencesPanel(){
393         Node[] nnChild = Utils.getChildrenNodes(getBeanEnvironmentNode());
394         for(int k = 0; k < nnChild.length; k++){
395             if(nnChild[k] instanceof ResourceReferencesNode) {
396                 SectionNodePanel resourceReferencesPanel = ((ResourceReferencesNode)nnChild[k]).getSectionNodePanel();
397                 resourceReferencesPanel.open();
398                 return (InnerTablePanel) resourceReferencesPanel.getInnerPanel();
399             }
400         }
401         return null;
402     }
403     
404     protected InnerTablePanel getEjbReferencesPanel(){
405         Node[] nnChild = Utils.getChildrenNodes(getBeanEnvironmentNode());
406         for(int k = 0; k < nnChild.length; k++){
407             if(nnChild[k] instanceof EjbReferencesNode) {
408                 SectionNodePanel ejbReferencesPanel = ((EjbReferencesNode)nnChild[k]).getSectionNodePanel();
409                 ejbReferencesPanel.open();
410                 return (InnerTablePanel) ejbReferencesPanel.getInnerPanel();
411             }
412         }
413         return null;
414     }
415     
416     protected InnerTablePanel getResourceEnvironmentReferencesPanel(){
417         Node[] nnChild = Utils.getChildrenNodes(getBeanEnvironmentNode());
418         for(int k = 0; k < nnChild.length; k++){
419             if(nnChild[k] instanceof ResourceEnvironmentReferencesNode) {
420                 SectionNodePanel reReferencesPanel = ((ResourceEnvironmentReferencesNode)nnChild[k]).getSectionNodePanel();
421                 reReferencesPanel.open();
422                 return (InnerTablePanel) reReferencesPanel.getInnerPanel();
423             }
424         }
425         return null;
426     }
427     
428     protected InnerTablePanel getSecurityRoleReferencesPanel(){
429         Node[] nnChild = Utils.getChildrenNodes(getBeanEnvironmentNode());
430         for(int k = 0; k < nnChild.length; k++){
431             if(nnChild[k] instanceof SecurityRoleReferencesNode) {
432                 SectionNodePanel srReferencesPanel = ((SecurityRoleReferencesNode)nnChild[k]).getSectionNodePanel();
433                 srReferencesPanel.open();
434                 return (InnerTablePanel) srReferencesPanel.getInnerPanel();
435             }
436         }
437         return null;
438     }
439
440 }
441
Popular Tags