KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.swing.event.DocumentListener JavaDoc;
24 import junit.framework.AssertionFailedError;
25 import junit.framework.Test;
26 import junit.textui.TestRunner;
27 import org.netbeans.api.project.Project;
28 import org.netbeans.jellytools.JellyTestCase;
29 import org.netbeans.jmi.javamodel.JavaClass;
30 import org.netbeans.junit.NbTestCase;
31 import org.netbeans.junit.NbTestSuite;
32 import org.netbeans.modules.j2ee.common.JMIUtils;
33 import org.netbeans.modules.j2ee.dd.api.ejb.CmpField;
34 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider;
35 import org.netbeans.modules.j2ee.dd.api.ejb.Ejb;
36 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar;
37 import org.netbeans.modules.j2ee.dd.api.ejb.EnterpriseBeans;
38 import org.netbeans.modules.j2ee.dd.api.ejb.Entity;
39 import org.netbeans.modules.j2ee.ddloaders.multiview.BeanDetailNode;
40 import org.netbeans.modules.j2ee.ddloaders.multiview.BeanDetailsPanel;
41 import org.netbeans.modules.j2ee.ddloaders.multiview.CmpFieldHelper;
42 import org.netbeans.modules.j2ee.ddloaders.multiview.CmpFieldsNode;
43 import org.netbeans.modules.j2ee.ddloaders.multiview.CmpFieldsTableModel;
44 import org.netbeans.modules.j2ee.ddloaders.multiview.EjbJarMultiViewDataObject;
45 import org.netbeans.modules.j2ee.ddloaders.multiview.EntityHelper;
46 import org.netbeans.modules.j2ee.ejbjarproject.EjbJarProject;
47 import org.netbeans.modules.j2ee.ejbcore.api.methodcontroller.EntityMethodController;
48 import org.netbeans.modules.j2ee.ejbcore.api.methodcontroller.EjbMethodController;
49 import org.netbeans.modules.xml.multiview.ui.SectionNodePanel;
50 import org.netbeans.test.j2ee.lib.J2eeProjectSupport;
51 import org.openide.cookies.EditCookie;
52 import org.openide.filesystems.FileObject;
53 import org.openide.loaders.DataObject;
54 import org.openide.nodes.Node;
55
56
57 /**
58  *
59  * @author blaha
60  */

61 public class EBDetailsAndCMPFieldPanelTest extends JellyTestCase {
62     public static final String JavaDoc EJB_PROJECT_NAME = "TestCMP";
63     public static final String JavaDoc EJB_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + EJB_PROJECT_NAME;
64     
65     private static Project project;
66     private static EjbJarMultiViewDataObject ddObj;
67     private static FileObject ddFo;
68     private static Entity bean;
69     private static EjbJar ejbJar;
70     private static CmpField[] cmpFields;
71     private static EntityMethodController mC;
72     private static CmpField newCmpField;
73     private static int row;
74     
75     public EBDetailsAndCMPFieldPanelTest(String JavaDoc testName) {
76         super(testName);
77     }
78     
79     protected void setUp() throws Exception JavaDoc {
80         super.setUp();
81     }
82     
83     protected void tearDown() throws Exception JavaDoc {
84     }
85     
86     public static Test suite() {
87         NbTestSuite suite = new NbTestSuite();
88         // general panel
89
suite.addTest(new EBDetailsAndCMPFieldPanelTest("testOpenProject"));
90          /*
91         suite.addTest(new EBDetailsAndCMPFieldPanelTest("testEBName"));
92         suite.addTest(new EBDetailsAndCMPFieldPanelTest("testDescription"));
93         suite.addTest(new EBDetailsAndCMPFieldPanelTest("testSmallIcon"));
94         suite.addTest(new EBDetailsAndCMPFieldPanelTest("testLargeIcon"));
95         suite.addTest(new EBDetailsAndCMPFieldPanelTest("testRevertChanges"));*/

96         
97         // CMP fields panel
98
suite.addTest(new EBDetailsAndCMPFieldPanelTest("testExistingCMPFields"));
99         suite.addTest(new EBDetailsAndCMPFieldPanelTest("testAddNewCMPField"));
100         suite.addTest(new EBDetailsAndCMPFieldPanelTest("testChangeCMPFieldName"));
101         suite.addTest(new EBDetailsAndCMPFieldPanelTest("testChangeCMPMethods"));
102         suite.addTest(new EBDetailsAndCMPFieldPanelTest("testChangeCMPType"));
103         suite.addTest(new EBDetailsAndCMPFieldPanelTest("testChangeCMPDescription"));
104         suite.addTest(new EBDetailsAndCMPFieldPanelTest("testDeleteCMPField"));
105         return suite;
106     }
107     
108     /** Use for execution inside IDE */
109     public static void main(java.lang.String JavaDoc[] args) {
110         // run only selected test case
111
TestRunner.run(suite());
112     }
113     
114     public void testOpenProject() throws Exception JavaDoc{
115         File JavaDoc projectDir = new File JavaDoc(EJB_PROJECT_PATH);
116         project = (Project)J2eeProjectSupport.openProject(projectDir);
117         assertNotNull("Project is null.", project);
118         Thread.sleep(1000);
119         
120         EjbJarProject ejbJarProject = (EjbJarProject)project;
121         ddFo = ejbJarProject.getAPIEjbJar().getDeploymentDescriptor(); // deployment descriptor
122
assertNotNull("ejb-jar.xml FileObject is null.", ddFo);
123         
124         ejbJar = DDProvider.getDefault().getDDRoot(ddFo);
125         
126         ddObj = (EjbJarMultiViewDataObject)DataObject.find(ddFo); //MultiView Editor
127
assertNotNull("MultiViewDO is null.",ddObj);
128         
129         EditCookie edit = (EditCookie)ddObj.getCookie(EditCookie.class);
130         edit.edit();
131         Thread.sleep(1000);
132         
133         // select CustomerBean
134
EnterpriseBeans beans = DDProvider.getDefault().getDDRoot(ddFo).getEnterpriseBeans();
135         bean = (Entity)beans.findBeanByName(EnterpriseBeans.ENTITY,
136                 Ejb.EJB_NAME ,"CustomerBean");
137         
138         cmpFields = bean.getCmpField();
139         
140         ddObj.showElement(bean); //open visual editor
141
Utils.waitForAWTDispatchThread();
142     }
143     
144     public void testEBName() throws Exception JavaDoc{
145         assertEquals("CustomerEB",getBeanDetailPanel().getDisplayNameTextField().getText());
146         getBeanDetailPanel().getDisplayNameTextField().setText("testBeanName");
147         assertEquals("testBeanName", bean.getDisplayName(null));
148         Utils utils = new Utils(this);
149         utils.checkInXML(ddObj, "<display-name>testBeanName</display-name>");
150         utils.save(ddObj);
151         utils.checkFiles("testEBName",new String JavaDoc[]{"ejb-jar.xml"},null);
152     }
153     
154     public void testDescription() throws Exception JavaDoc{
155         assertEquals("jdbc:mysql://localhost:3306/users [blaha on Default schema]",getBeanDetailPanel().getDescriptionTextArea().getText());
156         getBeanDetailPanel().getDescriptionTextArea().setText("testDescription");
157         assertEquals("testDescription", bean.getDescription(null));
158         Utils utils = new Utils(this);
159         utils.checkInXML(ddObj, "<description>testDescription</description>");
160         utils.save(ddObj);
161         utils.checkFiles("testDescription",new String JavaDoc[]{"ejb-jar.xml"},null);
162     }
163     
164     public void testSmallIcon() throws Exception JavaDoc{
165         assertEquals("",getBeanDetailPanel().getSmallIconTextField().getText().trim());
166         getBeanDetailPanel().getSmallIconTextField().setText("testEntitySmallIcon");
167         assertEquals("testEntitySmallIcon", bean.getSmallIcon(null));
168         Utils utils = new Utils(this);
169         utils.checkInXML(ddObj, "<small-icon>testEntitySmallIcon</small-icon>");
170         utils.save(ddObj);
171         utils.checkFiles("testSmallIcon",new String JavaDoc[]{"ejb-jar.xml"},null);
172     }
173     
174     public void testLargeIcon() throws Exception JavaDoc{
175         assertEquals("",getBeanDetailPanel().getLargeIconTextField().getText().trim());
176         getBeanDetailPanel().getLargeIconTextField().setText("testEntityLargeIcon");
177         assertEquals("testEntityLargeIcon", bean.getLargeIcon(null));
178         Utils utils = new Utils(this);
179         utils.checkInXML(ddObj, "<large-icon>testEntityLargeIcon</large-icon>");
180         utils.save(ddObj);
181         utils.checkFiles("testLargeIcon",new String JavaDoc[]{"ejb-jar.xml"},null);
182     }
183     
184     public void testRevertChanges() throws Exception JavaDoc{
185         bean.setDisplayName("CustomerEB"); // back changes
186
bean.setDescription("jdbc:mysql://localhost:3306/users [blaha on Default schema]"); // back changes
187
bean.setSmallIcon(null); // back changes
188
bean.setLargeIcon(null); // back changes
189
ejbJar.write(ddFo);
190     }
191     
192     //CMP Field panel
193
public void testExistingCMPFields(){
194         String JavaDoc[] fieldsName = new String JavaDoc[]{"firstName","id","lastName"};
195         String JavaDoc[] fieldsType = new String JavaDoc[]{"java.lang.String","java.lang.Long","java.lang.String"};
196         for(int i = 0; i < cmpFields.length; i++){
197             CmpFieldsTableModel cmpTableModel = getCmpTableModel();
198             int row = ddObj.getEntityHelper(bean).cmpFields.getFieldRow(cmpFields[i]);
199             assertEquals(fieldsName[i], cmpTableModel.getValueAt(i,0)); //name
200
assertEquals(fieldsType[i], cmpTableModel.getValueAt(i,1)); //type
201
assertTrue(((Boolean JavaDoc)cmpTableModel.getValueAt(i,2)).booleanValue()); // local getter
202
if(cmpTableModel.getValueAt(i,0).equals("id")){
203                 assertFalse(((Boolean JavaDoc)cmpTableModel.getValueAt(i,3)).booleanValue()); // local setter
204
}else{
205                 assertTrue(((Boolean JavaDoc)cmpTableModel.getValueAt(i,3)).booleanValue()); // local setter
206
}
207             assertFalse(((Boolean JavaDoc)cmpTableModel.getValueAt(i,4)).booleanValue()); // no remote getter
208
assertFalse(((Boolean JavaDoc)cmpTableModel.getValueAt(i,5)).booleanValue()); // no remote setter
209
assertNull(cmpTableModel.getValueAt(i,6)); // no description
210
}
211     }
212     
213     public void testAddNewCMPField() throws Exception JavaDoc{
214         // create new CMP field
215
JavaClass javaClass = JMIUtils.findClass("cmp.CustomerBean");
216         assertNotNull("Can't find class", javaClass);
217         mC = (EntityMethodController)EjbMethodController.createFromClass(javaClass);
218         mC.addField(JMIUtils.createField(javaClass, "testCMPField", "java.lang.Float"),
219                 ddFo, true, true, false, false, "test CMP field");
220         // check values in visual editor
221
CmpFieldsTableModel cmpTableModel = getCmpTableModel();
222         cmpFields = bean.getCmpField();
223         for(int i = 0; i < cmpFields.length; i++){
224             if(!cmpFields[i].getFieldName().equals("testCMPField")){
225                 if(i == (cmpFields.length - 1)){
226                     fail("Can't find new CMP field");
227                 }
228                 continue;
229             }
230             int row = ddObj.getEntityHelper(bean).cmpFields.getFieldRow(cmpFields[i]);
231             assertEquals("testCMPField", cmpTableModel.getValueAt(row,0));
232             assertEquals("java.lang.Float", cmpTableModel.getValueAt(row,1));
233             assertTrue(((Boolean JavaDoc)cmpTableModel.getValueAt(row,2)).booleanValue());
234             assertTrue("Local setter is disabled.", ((Boolean JavaDoc)cmpTableModel.getValueAt(row,3)).booleanValue());
235             assertFalse(((Boolean JavaDoc)cmpTableModel.getValueAt(row,4)).booleanValue());
236             assertFalse(((Boolean JavaDoc)cmpTableModel.getValueAt(row,5)).booleanValue());
237             assertEquals("test CMP field", cmpTableModel.getValueAt(row,6));
238         }
239         Utils utils = new Utils(this);
240         // check xml view
241
utils.checkInXML(ddObj, "<field-name>testCMPField</field-name>");
242         utils.checkInXML(ddObj, "<description>test CMP field</description>");
243     }
244     
245     public void testChangeCMPFieldName() throws Exception JavaDoc{
246         // find CMP field, it should have index 3
247
newCmpField = bean.getCmpField(3);
248         assertEquals("testCMPField", newCmpField.getFieldName());
249         row = ddObj.getEntityHelper(bean).cmpFields.getFieldRow(newCmpField);
250         getCmpTableModel().setValueAt("testChangeCMPFieldName",row, 0);
251         Utils utils = new Utils(this);
252         utils.checkInXML(ddObj, "<field-name>testChangeCMPFieldName</field-name>");
253         utils.checkFiles("testChangeCMPFieldName", new String JavaDoc[]{"ejb-jar.xml"},
254                 new String JavaDoc[]{"CustomerBean.java", "CustomerLocalBusiness.java"});
255     }
256     
257     public void testChangeCMPMethods() throws Exception JavaDoc{
258         Utils utils = new Utils(this);
259         getCmpTableModel().setValueAt(new Boolean JavaDoc("false"),row,2);
260         utils.checkFiles("testChangeCMPMethods", new String JavaDoc[]{"ejb-jar.xml"},
261                 new String JavaDoc[]{"CustomerBean.java", "CustomerLocalBusiness.java"});
262     }
263     
264     public void testChangeCMPType() throws Exception JavaDoc{
265         getCmpTableModel().setValueAt("boolean",row, 1);
266         Utils utils = new Utils(this);
267         utils.checkFiles("testChangeCMPType", new String JavaDoc[]{"ejb-jar.xml"},
268                 new String JavaDoc[]{"CustomerBean.java", "CustomerLocalBusiness.java"});
269     }
270     
271     public void testChangeCMPDescription() throws Exception JavaDoc{
272         getCmpTableModel().setValueAt("testChangeCMPDescription",row, 6);
273         Utils utils = new Utils(this);
274         utils.checkInXML(ddObj, "<description>testChangeCMPDescription</description>");
275         utils.save(ddObj);
276         utils.checkFiles("testChangeCMPDescription", new String JavaDoc[]{"ejb-jar.xml"}, null);
277     }
278     
279     public void testDeleteCMPField() throws Exception JavaDoc{
280         mC.deleteField(newCmpField, ddFo);
281         Utils utils = new Utils(this);
282         try {
283             utils.checkInXML(ddObj, "<description>test CMP field</description>");
284             utils.checkInXML(ddObj, "<field-name>testCMPField</field-name>");
285             fail("Elements testCMPField is still visible in xml view.");
286         } catch (AssertionFailedError e) {
287             // it's ok
288
}
289         // try to find CMP field in dataModel
290
for(int i = 0; i < getCmpTableModel().getRowCount(); i++){
291             assertFalse(getCmpTableModel().getValueAt(i,0).equals("testCMPField"));
292         }
293         utils.checkFiles("testDeleteCMPField", new String JavaDoc[]{"ejb-jar.xml"},
294                 new String JavaDoc[]{"CustomerBean.java", "CustomerLocalBusiness.java"});
295     }
296     
297     private BeanDetailsPanel getBeanDetailPanel(){
298         Node[] nnChild = Utils.getChildrenNodes(Utils.getEntityNode(ddObj));
299         for(int k = 0; k < nnChild.length; k++){
300             if(nnChild[k] instanceof BeanDetailNode){
301                 SectionNodePanel overviewPanel = ((BeanDetailNode)nnChild[k]).getSectionNodePanel();
302                 overviewPanel.open();
303                 return (BeanDetailsPanel)overviewPanel.getInnerPanel();
304             }
305         }
306         return null;
307     }
308     
309     private CmpFieldsTableModel getCmpTableModel(){
310         Node[] nnChild = Utils.getChildrenNodes(Utils.getEntityNode(ddObj));
311         for(int k = 0; k < nnChild.length; k++){
312             if(nnChild[k] instanceof CmpFieldsNode){
313                 SectionNodePanel cmpPanel = ((CmpFieldsNode)nnChild[k]).getSectionNodePanel();
314                 cmpPanel.open();
315                 EntityHelper entHelper = ddObj.getEntityHelper(bean);
316                 return entHelper.cmpFields.getCmpFieldsTableModel();
317             }
318         }
319         return null;
320     }
321     
322     private CmpFieldHelper getCmpFieldHelper(int row){
323         return ddObj.getEntityHelper(bean).cmpFields.getCmpFieldHelper(row);
324     }
325     
326 }
Popular Tags