1 19 20 package org.netbeans.test.java.gui.wizards; 21 22 23 import org.netbeans.jellytools.*; 24 import org.netbeans.jellytools.nodes.*; 25 import org.netbeans.jemmy.JemmyProperties; 26 import org.netbeans.jemmy.TestOut; 27 import org.netbeans.jemmy.operators.*; 28 import org.netbeans.test.java.Utilities; 29 import org.netbeans.test.java.gui.GuiUtilities; 30 import java.io.*; 31 import org.netbeans.jemmy.TimeoutExpiredException; 32 import org.netbeans.junit.AssertionFailedErrorException; 33 import org.netbeans.junit.NbTestSuite; 34 import org.netbeans.test.java.Common; 35 36 40 public class NewFileWizardTest extends JellyTestCase { 41 42 private static final String JAVA_BUNDLE_PATH = "org.netbeans.modules.java.project.Bundle"; 44 45 private static final int ACTION_TIMEOUT = 1000; 47 48 private static final String TEST_PROJECT_NAME = "TestProject"; 50 51 private static final String TEST_PACKAGE_NAME = "test"; 53 54 private static final String TEST_CLASS_NAME = "TestClass"; 56 57 private static final String TEST_PACKAGE_NAME_INVALID = "a/b"; 59 60 63 protected static PrintStream err; 64 67 protected static PrintStream log; 68 69 private String workDir = "/tmp"; 71 72 static String projectDir; 73 74 78 public static NbTestSuite suite() { 79 NbTestSuite suite = new NbTestSuite(); 80 suite.addTest(new NewFileWizardTest("testCreateProject")); 81 suite.addTest(new NewFileWizardTest("testCreatePackage")); 83 suite.addTest(new NewFileWizardTest("testDeletePackage")); 85 suite.addTest(new NewFileWizardTest("testDeleteProject")); 87 suite.addTest(new NewFileWizardTest("testNewFileWizardComplex")); 88 suite.addTest(new NewFileWizardTest("testCreatePackageFailure")); 89 suite.addTest(new NewFileWizardTest("testCreateInterface")); 90 suite.addTest(new NewFileWizardTest("testCreateAnnotation")); 91 suite.addTest(new NewFileWizardTest("testCreateEnum")); 92 suite.addTest(new NewFileWizardTest("testCreateException")); 93 suite.addTest(new NewFileWizardTest("testCreateJApplet")); 94 suite.addTest(new NewFileWizardTest("testCreateEmptyFile")); 95 suite.addTest(new NewFileWizardTest("testCreateMainClass")); 96 suite.addTest(new NewFileWizardTest("testCreatePackageInfo")); 97 suite.addTest(new NewFileWizardTest("testInvalidName")); 98 suite.addTest(new NewFileWizardTest("testExistingName")); 99 100 return suite; 101 } 102 103 107 public static void main(String [] args) { 108 junit.textui.TestRunner.run(suite()); 109 } 110 111 114 public void setUp() { 115 System.out.println("######## "+getName()+" #######"); 116 err = getLog(); 117 log = getRef(); 118 JemmyProperties.getProperties().setOutput(new TestOut(null, 119 new PrintWriter(err, true), new PrintWriter(err, false), null)); 120 try { 121 File wd = getWorkDir(); 122 workDir = wd.toString(); 123 } catch (IOException e) { } 124 } 125 126 130 public NewFileWizardTest(String testName) { 131 super(testName); 132 } 133 134 public void testCreateProject() { 135 projectDir = GuiUtilities.createProject(TEST_PROJECT_NAME, workDir); 136 } 137 138 141 public void testCreateProject(String projectName) { 142 projectDir = GuiUtilities.createProject(projectName, workDir); 143 } 144 145 public void testDeleteProject() { 146 GuiUtilities.deleteProject(TEST_PROJECT_NAME, null, projectDir, false); 147 } 148 149 152 public void testDeleteProject(String projectName) { 153 GuiUtilities.deleteProject(projectName, null, projectDir, false); 154 } 155 156 159 public void testCreatePackage() { 160 GuiUtilities.createPackage(TEST_PROJECT_NAME,TEST_PACKAGE_NAME); 161 } 162 163 public void testCreatePackage(String projName,String packName) { 164 GuiUtilities.createPackage(projName,packName); 165 } 166 167 170 public void testDeletePackage() { 171 Node pn = new ProjectsTabOperator().getProjectRootNode(TEST_PROJECT_NAME); 173 pn.select(); 174 175 Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString( 176 "org.netbeans.modules.java.j2seproject.Bundle", 177 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME); 178 n.select(); 179 n.performPopupAction(org.netbeans.jellytools.Bundle.getString( 180 "org.netbeans.core.projects.Bundle", "LBL_action_delete")); 181 182 new NbDialogOperator(Bundle.getString("org.openide.explorer.Bundle", 184 "MSG_ConfirmDeleteObjectTitle")).yes(); 185 186 } 187 188 196 public void testNewFileWizardComplex() { 197 testCreateProject(TEST_PROJECT_NAME); 199 200 testCreatePackage(TEST_PROJECT_NAME,TEST_PACKAGE_NAME); 202 203 Node pn = new ProjectsTabOperator().getProjectRootNode(TEST_PROJECT_NAME); 205 pn.select(); 206 207 NewFileWizardOperator op = NewFileWizardOperator.invoke(); 209 210 op.selectCategory(Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes")); 211 op.selectFileType(Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes/Class.java")); 212 op.next(); 213 214 JTextFieldOperator tf = new JTextFieldOperator(op); 215 tf.setText(TEST_CLASS_NAME); 216 217 op.finish(); 218 219 EditorOperator editor = new EditorOperator(TEST_CLASS_NAME); 221 String text = editor.getText(); 222 223 int oldIndex = 0; 225 for (int i=0; i<4; i++) { 226 oldIndex = text.indexOf(TEST_CLASS_NAME, oldIndex); 227 if (oldIndex>-1) oldIndex++; 228 } 229 230 assertTrue("Error in generated class "+TEST_CLASS_NAME+".java.", 231 oldIndex!=-1); 232 233 editor.close(); 234 235 testDeletePackage(); 237 238 testDeleteProject(TEST_PROJECT_NAME); 240 241 } 242 243 246 public void testCreatePackageFailure() { 247 NewFileWizardOperator op = NewFileWizardOperator.invoke(); 248 249 JDialogOperator jdo = new JDialogOperator( 251 org.netbeans.jellytools.Bundle.getString( 252 "org.netbeans.modules.project.ui.Bundle", 253 "LBL_NewFileWizard_Title")); 254 JTreeOperator jto = new JTreeOperator(jdo, 0); 255 boolean exitLoop = false; 256 for (int i=0; i<10; i++) { 257 for (int j=0; j<jto.getChildCount(jto.getRoot()); j++) { 258 if (jto.getChild(jto.getRoot(), j).toString()== 259 Bundle.getString(JAVA_BUNDLE_PATH, 260 "Templates/Classes")) { 261 exitLoop = true; 262 break; 263 } 264 } 265 if (exitLoop) break; 266 Utilities.takeANap(1000); 267 } 268 269 op.selectCategory(Bundle.getString(JAVA_BUNDLE_PATH, 271 "Templates/Classes")); 272 op.selectFileType(Bundle.getString(JAVA_BUNDLE_PATH, 273 "Templates/Classes/Package")); 274 op.next(); 275 276 JTextFieldOperator tfp = new JTextFieldOperator(op, 0); 278 tfp.setText(TEST_PACKAGE_NAME_INVALID); 279 Utilities.takeANap(1000); 286 287 291 JButtonOperator jbo = new JButtonOperator(op, "Finish"); 293 294 assertFalse("Finish button should be disabled for package with " 295 +"invalid name.", jbo.isEnabled()); 296 297 new NbDialogOperator(Bundle.getString( 298 "org.netbeans.modules.project.ui.Bundle", 299 "LBL_NewProjectWizard_Subtitle")+" " 300 +Bundle.getString(JAVA_BUNDLE_PATH, 301 "Templates/Classes/Package")).cancel(); 302 } 303 304 public void testCreateInterface() { 305 String expected = "" + 306 "/*\n"+ 307 " * MyIface.java\n"+ 308 " *\n"+ 309 " * Created on XXXXX xx, XXX, X:XX XX\n"+ 310 " *\n"+ 311 " * To change this template, choose Tools | Template Manager\n"+ 312 " * and open the template in the editor.\n"+ 313 " */\n"+ 314 "\n"+ 315 "package test;\n"+ 316 "\n"+ 317 "/**\n"+ 318 " *\n"+ 319 " * @author XXXXXXX\n"+ 320 " */\n"+ 321 "public interface MyIface {\n"+ 322 " \n"+ 323 "}\n"; 324 createAndVerify("MyIface" , 325 Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes/Interface.java"), 326 Common.unify(expected),true); 327 328 } 329 330 public void testCreateAnnotation() { 331 String expected = "" + 332 "/*\n"+ 333 " * MyAnnot.java\n"+ 334 " *\n"+ 335 " * Created on xxxxxxxxxxxxx\n"+ 336 " *\n"+ 337 " * To change this template, choose Tools | Template Manager\n"+ 338 " * and open the template in the editor.\n"+ 339 " */\n"+ 340 "\n"+ 341 "package test;\n"+ 342 "\n"+ 343 "/**\n"+ 344 " *\n"+ 345 " * @author xxxxxxx\n"+ 346 " */\n"+ 347 "public @interface MyAnnot {\n"+ 348 " \n"+ 349 "}\n"; 350 createAndVerify("MyAnnot" , 351 Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes/AnnotationType.java"), 352 Common.unify(expected),true); 353 } 354 355 public void testCreateEnum() { 356 String expected = "" + 357 "/*\n"+ 358 " * MyEnum.java\n"+ 359 " *\n"+ 360 " * Created on xxxxxxxxxxxxxxx\n"+ 361 " *\n"+ 362 " * To change this template, choose Tools | Template Manager\n"+ 363 " * and open the template in the editor.\n"+ 364 " */\n"+ 365 "\n"+ 366 "package test;\n"+ 367 "\n"+ 368 "/**\n"+ 369 " *\n"+ 370 " * @author xxxxxxxx\n"+ 371 " */\n"+ 372 "public enum MyEnum {\n"+ 373 " \n"+ 374 "}\n"; 375 createAndVerify("MyEnum" , 376 Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes/Enum.java"), 377 Common.unify(expected),true); 378 } 379 380 public void testCreateException() { 381 String expected = "" + 382 "/*\n"+ 383 " * MyExp.java\n"+ 384 " *\n"+ 385 " * Created on November 3, 2006, 2:40 PM\n"+ 386 " *\n"+ 387 " * To change this template, choose Tools | Template Manager\n"+ 388 " * and open the template in the editor.\n"+ 389 " */\n"+ 390 "\n"+ 391 "package test;\n"+ 392 "\n"+ 393 "/**\n"+ 394 " *\n"+ 395 " * @author jp159440\n"+ 396 " */\n"+ 397 "public class MyExp extends java.lang.Exception {\n"+ 398 " \n"+ 399 " /**\n"+ 400 " * Creates a new instance of <code>MyExp</code> without detail message.\n"+ 401 " */\n"+ 402 " public MyExp() {\n"+ 403 " }\n"+ 404 " \n"+ 405 " \n"+ 406 " /**\n"+ 407 " * Constructs an instance of <code>MyExp</code> with the specified detail message.\n"+ 408 " * @param msg the detail message.\n"+ 409 " */\n"+ 410 " public MyExp(String msg) {\n"+ 411 " super(msg);\n"+ 412 " }\n"+ 413 "}\n"; 414 415 createAndVerify("MyExp" , 416 Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes/Exception.java"), 417 Common.unify(expected),true); 418 } 419 420 421 public void testCreateJApplet() { 422 String expected = "" + 423 "/*\n"+ 424 " * MyJApplet.java\n"+ 425 " *\n"+ 426 " * Created on November 3, 2006, 2:45 PM\n"+ 427 " *\n"+ 428 " * To change this template, choose Tools | Template Manager\n"+ 429 " * and open the template in the editor.\n"+ 430 " */\n"+ 431 "\n"+ 432 "package test;\n"+ 433 "\n"+ 434 "/**\n"+ 435 " *\n"+ 436 " * @author jp159440\n"+ 437 " */\n"+ 438 "public class MyJApplet extends javax.swing.JApplet {\n"+ 439 " \n"+ 440 " /** Creates a new instance of MyJApplet */\n"+ 441 " public MyJApplet() {\n"+ 442 " }\n"+ 443 " \n"+ 444 "}\n"; 445 446 createAndVerify("MyJApplet" , 447 Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes/JApplet.java"), 448 Common.unify(expected),true); 449 } 450 451 public void testCreateEmptyFile() { 452 String expected = "" + 453 "package test;\n"+ 454 "\n"; 455 456 createAndVerify("Empty" , 457 Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes/Empty.java"), 458 Common.unify(expected),true); 459 } 460 461 public void testCreateMainClass() { 462 String expected = "" + 463 "/*\n"+ 464 " * MyMain.java\n"+ 465 " *\n"+ 466 " * Created on November 3, 2006, 2:50 PM\n"+ 467 " *\n"+ 468 " * To change this template, choose Tools | Template Manager\n"+ 469 " * and open the template in the editor.\n"+ 470 " */\n"+ 471 "\n"+ 472 "package test;\n"+ 473 "\n"+ 474 "/**\n"+ 475 " *\n"+ 476 " * @author jp159440\n"+ 477 " */\n"+ 478 "public class MyMain {\n"+ 479 " \n"+ 480 " /** Creates a new instance of MyMain */\n"+ 481 " public MyMain() {\n"+ 482 " }\n"+ 483 " \n"+ 484 " /**\n"+ 485 " * @param args the command line arguments\n"+ 486 " */\n"+ 487 " public static void main(String[] args) {\n"+ 488 " // TODO code application logic here\n"+ 489 " }\n"+ 490 " \n"+ 491 "}\n"; 492 493 createAndVerify("MyMain" , 494 Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes/Main.java"), 495 Common.unify(expected),true); 496 } 497 498 public void testCreatePackageInfo() { 499 String expected = "" + 500 "/*\n"+ 501 " * package-info.java\n"+ 502 " *\n"+ 503 " * Created on November 3, 2006, 2:51 PM\n"+ 504 " *\n"+ 505 " * To change this template, choose Tools | Template Manager\n"+ 506 " * and open the template in the editor.\n"+ 507 " */\n"+ 508 "\n"+ 509 "package test;\n"; 510 511 createAndVerify("package-info" , 512 Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes/package-info.java"), 513 Common.unify(expected),true); 514 } 515 516 public void testInvalidName() { 517 String expected = ""; 518 createAndVerify("Name.invalid" , 519 Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes/Class.java"), 520 Common.unify(expected),false); 521 } 522 523 public void testExistingName() { 524 String expected = ""; 525 createAndVerify("MyMain" , 526 Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes/Class.java"), 527 Common.unify(expected),false); 528 } 529 530 private void createIfNotOpened(String projName,String packName) { 531 Node pn = null; 532 try { 533 pn = new ProjectsTabOperator().getProjectRootNode(projName); 534 } catch(TimeoutExpiredException tee) { 535 System.out.println("Project is not opened, creating new one"); 536 testCreateProject(projName); 537 pn = new ProjectsTabOperator().getProjectRootNode(projName); 538 } 539 Node n = null; 540 try { 541 n = new Node(pn, org.netbeans.jellytools.Bundle.getString( 542 "org.netbeans.modules.java.j2seproject.Bundle", 543 "NAME_src.dir")+"|"+packName); 544 } catch (TimeoutExpiredException tee) { 545 System.out.println("Expected package not present, creating new one"); 546 testCreatePackage(projName,packName); 547 n = new Node(pn, org.netbeans.jellytools.Bundle.getString( 548 "org.netbeans.modules.java.j2seproject.Bundle", 549 "NAME_src.dir")+"|"+packName); 550 } 551 n.select(); 552 553 } 554 555 563 private void createAndVerify(String name, String type, String expectedContent, boolean shouldPass) { 564 createIfNotOpened(TEST_PROJECT_NAME, TEST_PACKAGE_NAME); 565 Node pn = new ProjectsTabOperator().getProjectRootNode(TEST_PROJECT_NAME); 567 pn.select(); 568 Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString( 569 "org.netbeans.modules.java.j2seproject.Bundle", 570 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME); 571 n.select(); 572 NewFileWizardOperator op = NewFileWizardOperator.invoke(); 574 575 op.selectCategory(Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes")); 576 op.selectFileType(type); 577 op.next(); 578 JTextFieldOperator tf = new JTextFieldOperator(op); 579 tf.setText(name); 580 if(!shouldPass) { 581 Utilities.takeANap(1000); 582 JButtonOperator jbo = new JButtonOperator(op,"Finish"); 583 assertFalse("Finish button should be disabled", jbo.isEnabled()); 584 new NbDialogOperator(Bundle.getString( 586 "org.netbeans.modules.project.ui.Bundle", 587 "LBL_NewProjectWizard_Subtitle")+" " 588 +type).cancel(); 589 return; 590 } 591 op.finish(); 592 EditorOperator editor = new EditorOperator(name); 594 String text = Common.unify(editor.getText()); 595 assertEquals("File doesnt have expected content",expectedContent,text); 596 editor.close(); 597 } 598 599 } 600 | Popular Tags |