1 19 20 package org.netbeans.test.j2ee.multiview; 21 22 import java.io.File ; 23 import javax.swing.event.DocumentListener ; 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 61 public class EBDetailsAndCMPFieldPanelTest extends JellyTestCase { 62 public static final String EJB_PROJECT_NAME = "TestCMP"; 63 public static final String 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 testName) { 76 super(testName); 77 } 78 79 protected void setUp() throws Exception { 80 super.setUp(); 81 } 82 83 protected void tearDown() throws Exception { 84 } 85 86 public static Test suite() { 87 NbTestSuite suite = new NbTestSuite(); 88 suite.addTest(new EBDetailsAndCMPFieldPanelTest("testOpenProject")); 90 96 97 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 109 public static void main(java.lang.String [] args) { 110 TestRunner.run(suite()); 112 } 113 114 public void testOpenProject() throws Exception { 115 File projectDir = new File (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(); assertNotNull("ejb-jar.xml FileObject is null.", ddFo); 123 124 ejbJar = DDProvider.getDefault().getDDRoot(ddFo); 125 126 ddObj = (EjbJarMultiViewDataObject)DataObject.find(ddFo); assertNotNull("MultiViewDO is null.",ddObj); 128 129 EditCookie edit = (EditCookie)ddObj.getCookie(EditCookie.class); 130 edit.edit(); 131 Thread.sleep(1000); 132 133 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); Utils.waitForAWTDispatchThread(); 142 } 143 144 public void testEBName() throws Exception { 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 []{"ejb-jar.xml"},null); 152 } 153 154 public void testDescription() throws Exception { 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 []{"ejb-jar.xml"},null); 162 } 163 164 public void testSmallIcon() throws Exception { 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 []{"ejb-jar.xml"},null); 172 } 173 174 public void testLargeIcon() throws Exception { 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 []{"ejb-jar.xml"},null); 182 } 183 184 public void testRevertChanges() throws Exception { 185 bean.setDisplayName("CustomerEB"); bean.setDescription("jdbc:mysql://localhost:3306/users [blaha on Default schema]"); bean.setSmallIcon(null); bean.setLargeIcon(null); ejbJar.write(ddFo); 190 } 191 192 public void testExistingCMPFields(){ 194 String [] fieldsName = new String []{"firstName","id","lastName"}; 195 String [] fieldsType = new String []{"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)); assertEquals(fieldsType[i], cmpTableModel.getValueAt(i,1)); assertTrue(((Boolean )cmpTableModel.getValueAt(i,2)).booleanValue()); if(cmpTableModel.getValueAt(i,0).equals("id")){ 203 assertFalse(((Boolean )cmpTableModel.getValueAt(i,3)).booleanValue()); }else{ 205 assertTrue(((Boolean )cmpTableModel.getValueAt(i,3)).booleanValue()); } 207 assertFalse(((Boolean )cmpTableModel.getValueAt(i,4)).booleanValue()); assertFalse(((Boolean )cmpTableModel.getValueAt(i,5)).booleanValue()); assertNull(cmpTableModel.getValueAt(i,6)); } 211 } 212 213 public void testAddNewCMPField() throws Exception { 214 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 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 )cmpTableModel.getValueAt(row,2)).booleanValue()); 234 assertTrue("Local setter is disabled.", ((Boolean )cmpTableModel.getValueAt(row,3)).booleanValue()); 235 assertFalse(((Boolean )cmpTableModel.getValueAt(row,4)).booleanValue()); 236 assertFalse(((Boolean )cmpTableModel.getValueAt(row,5)).booleanValue()); 237 assertEquals("test CMP field", cmpTableModel.getValueAt(row,6)); 238 } 239 Utils utils = new Utils(this); 240 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 { 246 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 []{"ejb-jar.xml"}, 254 new String []{"CustomerBean.java", "CustomerLocalBusiness.java"}); 255 } 256 257 public void testChangeCMPMethods() throws Exception { 258 Utils utils = new Utils(this); 259 getCmpTableModel().setValueAt(new Boolean ("false"),row,2); 260 utils.checkFiles("testChangeCMPMethods", new String []{"ejb-jar.xml"}, 261 new String []{"CustomerBean.java", "CustomerLocalBusiness.java"}); 262 } 263 264 public void testChangeCMPType() throws Exception { 265 getCmpTableModel().setValueAt("boolean",row, 1); 266 Utils utils = new Utils(this); 267 utils.checkFiles("testChangeCMPType", new String []{"ejb-jar.xml"}, 268 new String []{"CustomerBean.java", "CustomerLocalBusiness.java"}); 269 } 270 271 public void testChangeCMPDescription() throws Exception { 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 []{"ejb-jar.xml"}, null); 277 } 278 279 public void testDeleteCMPField() throws Exception { 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 } 289 for(int i = 0; i < getCmpTableModel().getRowCount(); i++){ 291 assertFalse(getCmpTableModel().getValueAt(i,0).equals("testCMPField")); 292 } 293 utils.checkFiles("testDeleteCMPField", new String []{"ejb-jar.xml"}, 294 new String []{"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 |