KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > web > WebProjectValidation


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.web;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.ConnectException JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.net.URLConnection JavaDoc;
28 import javax.swing.JComboBox JavaDoc;
29 import javax.swing.SwingUtilities JavaDoc;
30 import junit.framework.AssertionFailedError;
31 import org.netbeans.jellytools.Bundle;
32 import org.netbeans.jellytools.EditorOperator;
33 import org.netbeans.jellytools.EditorWindowOperator;
34 import org.netbeans.jellytools.JellyTestCase;
35 import org.netbeans.jellytools.MainWindowOperator;
36 import org.netbeans.jellytools.NbDialogOperator;
37 import org.netbeans.jellytools.NewFileWizardOperator;
38 import org.netbeans.jellytools.actions.BuildProjectAction;
39 import org.netbeans.jellytools.actions.EditAction;
40 import org.netbeans.jellytools.modules.web.NewJspFileNameStepOperator;
41 import org.netbeans.jellytools.nodes.SourcePackagesNode;
42 import org.netbeans.jemmy.EventTool;
43 import org.netbeans.jemmy.JemmyException;
44 import org.netbeans.jemmy.JemmyProperties;
45 import org.netbeans.jemmy.TimeoutExpiredException;
46 import org.netbeans.jellytools.WizardOperator;
47 import org.netbeans.jellytools.nodes.Node;
48 import org.netbeans.jellytools.actions.ActionNoBlock;
49 import org.netbeans.jellytools.actions.Action;
50 import org.netbeans.jellytools.NewProjectWizardOperator;
51 import org.netbeans.jellytools.NewWebProjectNameLocationStepOperator;
52 import org.netbeans.jellytools.OptionsOperator;
53 import org.netbeans.jellytools.OutputTabOperator;
54 import org.netbeans.jellytools.ProjectsTabOperator;
55 import org.netbeans.jellytools.RuntimeTabOperator;
56 import org.netbeans.jellytools.modules.j2ee.nodes.J2eeServerNode;
57 import org.netbeans.jellytools.modules.web.nodes.WebPagesNode;
58 import org.netbeans.jemmy.QueueTool;
59 import org.netbeans.jemmy.Timeouts;
60 import org.netbeans.jemmy.operators.JButtonOperator;
61 import org.netbeans.jemmy.operators.JCheckBoxOperator;
62 import org.netbeans.jemmy.operators.JComboBoxOperator;
63 import org.netbeans.jemmy.operators.JLabelOperator;
64 import org.netbeans.jemmy.operators.JListOperator;
65 import org.netbeans.jemmy.operators.JRadioButtonOperator;
66 import org.netbeans.jemmy.operators.JTextFieldOperator;
67 import org.netbeans.jemmy.operators.JTreeOperator;
68 import org.netbeans.jemmy.operators.Operator;
69 import org.netbeans.junit.NbTestSuite;
70 import org.netbeans.junit.ide.ProjectSupport;
71 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceCreationException;
72 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
73
74 /**
75  */

76 public class WebProjectValidation extends JellyTestCase {
77     protected static ProjectHelper phelper = new ProjectHelper() {
78         public Node getSourceNode() {
79             return new SourcePackagesNode(PROJECT_NAME);
80         }
81     };
82     // location of sample project (parent of PROJECT_FOLDER)
83
protected static final String JavaDoc PROJECT_LOCATION =
84             System.getProperty("xtest.data");
85     // name of sample project
86
protected static String JavaDoc PROJECT_NAME = "SampleProject"; // NOI18N
87
// foloder of sample project
88
protected static String JavaDoc PROJECT_FOLDER =
89             PROJECT_LOCATION+File.separator+PROJECT_NAME;
90     protected TestURLDisplayer urlDisplayer;
91     private static final String JavaDoc BUILD_SUCCESSFUL = "BUILD SUCCESSFUL";
92     private ServerInstance server;
93     
94     /** Need to be defined because of JUnit */
95     public WebProjectValidation(String JavaDoc name) {
96         super(name);
97     }
98     
99     public static NbTestSuite suite() {
100         NbTestSuite suite = new NbTestSuite();
101         suite.addTest(new WebProjectValidation("testPreconditions"));
102         suite.addTest(new WebProjectValidation("testNewWebProject"));
103         suite.addTest(new WebProjectValidation("testNewJSP"));
104         suite.addTest(new WebProjectValidation("testNewJSP2"));
105         suite.addTest(new WebProjectValidation("testNewServlet"));
106         suite.addTest(new WebProjectValidation("testNewServlet2"));
107         suite.addTest(new WebProjectValidation("testBuildProject"));
108         suite.addTest(new WebProjectValidation("testCompileAllJSP"));
109         suite.addTest(new WebProjectValidation("testCompileJSP"));
110         suite.addTest(new WebProjectValidation("testCleanProject"));
111         suite.addTest(new WebProjectValidation("testRunProject"));
112         suite.addTest(new WebProjectValidation("testRunJSP"));
113         suite.addTest(new WebProjectValidation("testViewServlet"));
114         suite.addTest(new WebProjectValidation("testRunServlet"));
115         suite.addTest(new WebProjectValidation("testCreateTLD"));
116         suite.addTest(new WebProjectValidation("testCreateTagHandler"));
117         suite.addTest(new WebProjectValidation("testRunTag"));
118         suite.addTest(new WebProjectValidation("testNewHTML"));
119         suite.addTest(new WebProjectValidation("testRunHTML"));
120         suite.addTest(new WebProjectValidation("testNewSegment"));
121         suite.addTest(new WebProjectValidation("testNewDocument"));
122         suite.addTest(new WebProjectValidation("testStopServer"));
123         suite.addTest(new WebProjectValidation("testStartServer"));
124         suite.addTest(new WebProjectValidation("testBrowserSettings"));
125         suite.addTest(new WebProjectValidation("testFinish"));
126         return suite;
127     }
128     
129     /** Use for execution inside IDE */
130     public static void main(java.lang.String JavaDoc[] args) {
131         // run whole suite
132
junit.textui.TestRunner.run(suite());
133         // run only selected test case
134
//junit.textui.TestRunner.run(new MyModuleValidation("testT2"));
135
}
136     
137     public void setUp() {
138         System.out.println("######## "+getName()+" #######");
139         initDisplayer();
140         JemmyProperties.setCurrentTimeout(
141                 "ComponentOperator.WaitComponentTimeout", 180000);
142         JemmyProperties.setCurrentTimeout(
143                 "FrameWaiter.WaitFrameTimeout", 180000);
144         JemmyProperties.setCurrentTimeout(
145                 "DialogWaiter.WaitDialogTimeout",180000);
146         server = ServerInstance.getDefault();
147         
148         // extend Tomcat running check timeout
149
// TomcatManager tomcatManager = getTomcatManager();
150
// tomcatManager.getInstanceProperties().setProperty(
151
// TomcatProperties.PROP_RUNNING_CHECK_TIMEOUT, "8000");
152
}
153     
154     public void tearDown() throws IOException JavaDoc {
155         logAndCloseOutputs();
156     }
157     
158     public void testRegisterTomcat() {
159         // register Tomcat
160
String JavaDoc tomcatHome = System.getProperty("tomcat.home");
161         try {
162             InstanceProperties.createInstanceProperties("tomcat55:home="+tomcatHome+":base="+tomcatHome,
163                     "tomcat", "tomcat", "Testing Tomcat");
164         } catch(InstanceCreationException ice) {
165             fail(ice);
166         }
167         J2eeServerNode.invoke("Testing Tomcat");
168         RuntimeTabOperator.invoke();
169         sleep(30000);
170         
171     }
172     
173     /** checks if the Server ports are not used */
174     public void testPreconditions() throws Exception JavaDoc {
175         URLConnection JavaDoc connection = server.getServerURL().openConnection();
176         try {
177             connection.connect();
178             fail("Port: "+server.getServerURL()+" is used by different server.");
179         } catch (ConnectException JavaDoc e) { }
180         URL JavaDoc url = new URL JavaDoc("http://localhost:8025");
181         connection = url.openConnection();
182         try {
183             connection.connect();
184             fail("Connection to http://localhost:8025 established, but tomcat should not be running.");
185         } catch (ConnectException JavaDoc e) { }
186     }
187     
188     /** Test creation of web project.
189      * - open New Project wizard from main menu (File|New Project)
190      * - select Web|Web Application
191      * - in the next panel type project name and project location
192      * - finish the wizard
193      * - wait until scanning of java files is finished
194      * - check index.jsp is opened
195      */

196     public void testNewWebProject() throws IOException JavaDoc {
197         installJemmyQueue();
198         NewProjectWizardOperator projectWizard = NewProjectWizardOperator.invoke();
199         String JavaDoc category = Bundle.getStringTrimmed(
200                 "org.netbeans.modules.web.core.Bundle",
201                 "Templates/JSP_Servlet");
202         projectWizard.selectCategory(category);
203         projectWizard.next();
204         NewWebProjectNameLocationStepOperator nameStep =
205                 new NewWebProjectNameLocationStepOperator();
206         nameStep.txtProjectName().setText("");
207         nameStep.txtProjectName().typeText(PROJECT_NAME);
208         nameStep.txtProjectLocation().setText("");
209         nameStep.txtProjectLocation().typeText(PROJECT_LOCATION);
210         nameStep.finish();
211         Timeouts timeouts = nameStep.getTimeouts().cloneThis();
212         nameStep.getTimeouts().setTimeout("ComponentOperator.WaitStateTimeout", 60000);
213         nameStep.waitClosed();
214         nameStep.setTimeouts(timeouts);
215         // wait for project creation
216
sleep(5000);
217         ProjectSupport.waitScanFinished();
218         new EditorWindowOperator().getEditor("index.jsp");//NOI18N
219
ProjectSupport.waitScanFinished();
220         // XXX HACK
221
WebPagesNode webPages = new WebPagesNode(PROJECT_NAME);
222         new Node(webPages,"index.jsp");//NOI18N
223
new Node(webPages,"WEB-INF|web.xml");//NOI18N
224
new Node(webPages,"META-INF|context.xml");//NOI18N
225
ref(Util.dumpProjectView(PROJECT_NAME));
226         compareReferenceFiles();
227     }
228     
229     /** Test new JSP wizard.
230      * - open New File wizard from main menu (File|New File)
231      * - select sample project as target
232      * - select Web|JSP file type
233      * - in the next panel type name
234      * - finish the wizard
235      * - check file is open in editor and close all opened documents
236      */

237     public void testNewJSP() throws IOException JavaDoc {
238         // XXX workaround due to issue #46073
239
new ProjectsTabOperator().getProjectRootNode(PROJECT_NAME).select();
240         NewJspFileNameStepOperator nameStep = NewJspFileNameStepOperator.invoke();
241         nameStep.setJSPFileName("page1");
242         nameStep.finish();
243         // check class is opened in Editor and then close it
244
new EditorOperator("page1.jsp").close();
245         ref(Util.dumpProjectView(PROJECT_NAME));
246         //compareReferenceFiles();
247
}
248     
249     /** Test new JSP wizard.
250      * - open New File wizard from context menu on Web Pages (New|File)
251      * - select JSP file type
252      * - in the next panel type name in
253      * - finish the wizard
254      * - check file is open in editor and close all opened documents
255      */

256     public void testNewJSP2() throws IOException JavaDoc {
257         Node webPages = new WebPagesNode(PROJECT_NAME);
258         // create new .jsp
259
new ActionNoBlock(null, "New|JSP").perform(webPages);
260         NewJspFileNameStepOperator nameStep = new NewJspFileNameStepOperator();
261         nameStep.setJSPFileName("page2");
262         nameStep.finish();
263         // check class is opened in Editor and then close the document
264
new EditorOperator("page2.jsp").close();
265         ref(Util.dumpProjectView(PROJECT_NAME));
266         //compareReferenceFiles();
267
//compareDD();
268
}
269     
270     /** Test new servlet wizard.
271      * - open New File wizard from main menu (File|New File)
272      * - select sample project as target
273      * - select Web|Servlet file type
274      * - in the next panel type name
275      * - finish the wizard
276      * - check file is open in editor and close it
277      */

278     public void testNewServlet() throws IOException JavaDoc {
279         // create a new package
280
new ActionNoBlock("File|New File", null).perform();
281         // WORKAROUND
282
new EventTool().waitNoEvent(1000);
283         WizardOperator newFileWizard = new WizardOperator("New File");
284         new JComboBoxOperator(newFileWizard).selectItem(PROJECT_NAME);
285         // workaround due to issue #46007
286
new Node(new JTreeOperator(newFileWizard), "Java Classes").select();
287         new Node(new JTreeOperator(newFileWizard), "Web").select();
288         new JListOperator(newFileWizard, 1).selectItem("Servlet");
289         newFileWizard.next();
290         JTextFieldOperator txtPackageName = new JTextFieldOperator(newFileWizard);
291         // clear text field
292
txtPackageName.setText("");
293         txtPackageName.typeText("Servlet1");
294         JComboBoxOperator txtPackage = new JComboBoxOperator(newFileWizard,1);
295         // clear text field
296
txtPackage.clearText();
297         txtPackage.typeText("test1");
298         newFileWizard.next();
299         newFileWizard.finish();
300         // check class is opened in Editor and close it
301
new EditorOperator("Servlet1.java").close();
302         // check the servlet is specified in web.xml
303
WebPagesNode webPages = new WebPagesNode(PROJECT_NAME);
304         webPages.setComparator(new Operator.DefaultStringComparator(true, true));
305         Node webXml = new Node(webPages, "WEB-INF|web.xml");
306         new EditAction().performPopup(webXml);
307         String JavaDoc xmlText = new EditorOperator("web.xml").getText();
308         new EditorOperator("web.xml").closeAllDocuments();
309         String JavaDoc[] content = new String JavaDoc[] {
310             "<servlet-name>Servlet1</servlet-name>",
311             "<servlet-class>test1.Servlet1</servlet-class>",
312             "<url-pattern>/Servlet1</url-pattern>"
313         };
314         for (int i=0; i<content.length; i++) {
315             assertTrue("Servlet is not correctly specifeid in web.xml." +
316                     " Following line is missing in the web.xml:\n"+content[i],
317                     xmlText.indexOf(content[i]) != -1);
318         }
319         ref(Util.dumpProjectView(PROJECT_NAME));
320         //compareReferenceFiles();
321
//compareDD();
322
}
323     
324     /** Test new Servlet wizard.
325      * - open New File wizard from main menu (File|New File)
326      * - select sample project as target
327      * - select Java Classes|Package file type
328      * - in the next panel type package name in
329      * - finish the wizard
330      * - open New File wizard from context menu on created package node (New|File)
331      * - select Web|Servlet file type
332      * - in the next panel type class name in
333      * - finish the wizard
334      * - check class is open in editor and close it
335      */

336     public void testNewServlet2() throws IOException JavaDoc {
337         NewFileWizardOperator.create(PROJECT_NAME, "Java Classes", "Java Package",
338                 null, "test2");
339         Node sample1Node = new Node(phelper.getSourceNode(), "test2");
340         // create a new class
341
new ActionNoBlock(null, "New|Servlet").perform(sample1Node);
342         WizardOperator newFileWizard = new WizardOperator("New Servlet");
343         new JTextFieldOperator(newFileWizard).typeText("Servlet2");
344         newFileWizard.next();
345         new JTextFieldOperator(newFileWizard,1).setText("");
346         new JTextFieldOperator(newFileWizard,1).typeText("Servlet2Name");
347         new JTextFieldOperator(newFileWizard,2).setText("");
348         new JTextFieldOperator(newFileWizard,2).typeText("/Servlet2URL");
349         newFileWizard.finish();
350         // check class is opened in Editor and then close it
351
new EditorOperator("Servlet2.java").close();
352         ref(Util.dumpProjectView(PROJECT_NAME));
353         //compareReferenceFiles();
354
//compareDD();
355
}
356     
357     public void testBuildProject() {
358         Node rootNode = new ProjectsTabOperator().getProjectRootNode(PROJECT_NAME);
359         new BuildProjectAction().perform(rootNode);
360         MainWindowOperator.getDefault().waitStatusText("Finished building");
361         ref(Util.dumpFiles(new File JavaDoc(PROJECT_FOLDER)));
362         //compareReferenceFiles();
363
}
364     
365     public void testCompileAllJSP() {
366         Node rootNode = new ProjectsTabOperator().getProjectRootNode(PROJECT_NAME);
367         new Action(null,"Properties").perform(rootNode);
368         NbDialogOperator properties = new NbDialogOperator("Project Properties");
369         new Node(new JTreeOperator(properties), "Build|Compiling").select();
370         new JCheckBoxOperator(properties,2).changeSelection(true);
371         properties.ok();
372         
373         testBuildProject();
374         testCleanProject();
375         
376         new Action(null,"Properties").perform(rootNode);
377         properties = new NbDialogOperator("Project Properties");
378         new Node(new JTreeOperator(properties), "Build|Compiling").select();
379         new JCheckBoxOperator(properties,2).changeSelection(false);
380         properties.ok();
381     }
382     
383     public void testCompileJSP() {
384         Node rootNode = new ProjectsTabOperator().getProjectRootNode(PROJECT_NAME);
385         Node jspNode = new Node(rootNode,"Web Pages|page2.jsp");
386         new Action(null,"Compile File").perform(jspNode);
387         MainWindowOperator.getDefault().waitStatusText("Finished building");
388         ref(Util.dumpFiles(new File JavaDoc(PROJECT_FOLDER)));
389         //compareReferenceFiles();
390
}
391     
392     public void testCleanProject() {
393         Node rootNode = new ProjectsTabOperator().getProjectRootNode(PROJECT_NAME);
394         new Action(null,"Clean Project").perform(rootNode);
395         MainWindowOperator.getDefault().waitStatusText("Finished building");
396         ref(Util.dumpFiles(new File JavaDoc(PROJECT_FOLDER)));
397         //compareReferenceFiles();
398
}
399     
400     public void testRunProject() throws Exception JavaDoc {
401         Node rootNode = new ProjectsTabOperator().getProjectRootNode(PROJECT_NAME);
402         new Node(rootNode,"Web Pages|index.jsp").performPopupAction("Open");
403         EditorOperator editor = new EditorOperator("index.jsp");
404         editor.replace("<title>JSP Page</title>",
405                 "<title>SampleProject Index Page</title>");
406         editor.insert("Running Project\n",23,1);
407         new Action(null,"Run Project").perform(rootNode);
408         waitBuildSuccessful();
409         assertDisplayerContent("<title>SampleProject Index Page</title>");
410         editor.deleteLine(23);
411         editor.save();
412         editor.close();
413     }
414     
415     public void testRunJSP() {
416         Node rootNode = new ProjectsTabOperator().getProjectRootNode(PROJECT_NAME);
417         new Node(rootNode,"Web Pages|page2.jsp").performPopupAction("Open");
418         EditorOperator editor = new EditorOperator("page2.jsp");
419         editor.replace("<title>JSP Page</title>", "<title>Page 2</title>");
420         editor.insert("Running JSP\n",6,1);
421         new Action("Run|Run File|Run \"page2.jsp\"",null).perform();
422         waitBuildSuccessful();
423         assertDisplayerContent("<title>Page 2</title>");
424         editor.deleteLine(6);
425         editor.save();
426         editor.close();
427     }
428     
429     public void testViewServlet(){
430         String JavaDoc jspCode ="new String().toString();";
431         String JavaDoc runningViewServlet = "Running View Servlet";
432         String JavaDoc newTitle = "<h1>View Page";
433         Node rootNode = new ProjectsTabOperator().getProjectRootNode(PROJECT_NAME);
434         new Node(rootNode,"Web Pages|page2.jsp").performPopupAction("Open");
435         EditorOperator editor = new EditorOperator("page2.jsp");
436         editor.replace("<h1>", newTitle);
437         editor.insert(runningViewServlet+"\n", 21, 6);
438         editor.insert("<% " + jspCode + " %>\n" , 24, 6);
439         new Action("Run|Run File|Run \"page2.jsp\"",null).perform();
440         waitBuildSuccessful();
441         assertDisplayerContent("<title>Page 2</title>");
442         new Node(rootNode,"Web Pages|page2.jsp").performPopupAction("View Servlet");
443         EditorOperator servlet = new EditorOperator("page2_jsp.java");
444         assertNotNull("SERVLET CONTEXT SHOULD BE SHOWN", servlet);
445         assertTrue("SERVLET SHOULD CONTAIN ", servlet.contains("<h1>"));
446         assertTrue("SERVLET SHOULD CONTAIN ", servlet.contains(runningViewServlet));
447         assertTrue("SERVLET SHOULD CONTAIN ", servlet.contains("JspWriter out"));
448         assertTrue("SERVLET SHOULD CONTAIN ", servlet.contains(jspCode));
449         editor.replace(newTitle, "<h1>");
450         editor.deleteLine(24);
451         editor.deleteLine(21);
452         editor.save();
453         editor.close();
454         servlet.close();
455     }
456     
457     public void testRunServlet() {
458         Node sourceNode = phelper.getSourceNode();
459         new Node(sourceNode,"test1|Servlet1.java").performPopupAction("Open");
460         EditorOperator editor = new EditorOperator("Servlet1.java");
461         // TODO replace the absolute line numbers by a kind of find to avoid test
462
// fails due minor template changes
463
if (!editor.getText(29).equals(" /* TODO output your page here\n"))
464             throw new AssertionFailedError("Servlet1.java:30 Other content than expected.\n"+
465                     "was >"+editor.getText(29)+"<, expected > /* TODO output your page here\n<");
466         if (!editor.getText(38).equals(" */\n"))
467             throw new AssertionFailedError("Servlet1.java:39 Other content than expected.\n"+
468                     "was >"+editor.getText(3)+"<, expected > */\n<");
469         editor.deleteLine(38);
470         editor.deleteLine(29);
471         editor.replace("out.println(\"<title>Servlet Servlet1</title>\");",
472                 "out.println(\"<title>Servlet with name=\"+request.getParameter(\"name\")+\"</title>\");");
473         new ActionNoBlock("Run|Run File|Run \"Servlet1.java\"",null).perform();
474         NbDialogOperator dialog = new NbDialogOperator("Set Servlet Execution Uri");
475         JComboBoxOperator combo = new JComboBoxOperator(dialog);
476         combo.setSelectedItem(combo.getSelectedItem()+"?name=Servlet1");
477         dialog.ok();
478         //waitBuildSuccessful();
479
editor.close();
480         assertDisplayerContent("<title>Servlet with name=Servlet1</title>");
481     }
482     
483     public void testCreateTLD() {
484         //HACK
485
new Node(new ProjectsTabOperator().getProjectRootNode(PROJECT_NAME), "Web Pages|WEB-INF").expand();
486         
487         // workaround due to issue #46073
488
new ProjectsTabOperator().getProjectRootNode(PROJECT_NAME).select();
489         
490         new ActionNoBlock("File|New File", null).perform();
491         // WORKAROUND
492
new EventTool().waitNoEvent(1000);
493         WizardOperator newFileWizard = new WizardOperator("New File");
494         new JComboBoxOperator(newFileWizard).selectItem(PROJECT_NAME);
495         // workaround due to issue #46007
496
new Node(new JTreeOperator(newFileWizard), "Java Classes").select();
497         new Node(new JTreeOperator(newFileWizard), "Web").select();
498         new JListOperator(newFileWizard, 1).selectItem("Tag Library Descriptor");
499         newFileWizard.next();
500         JTextFieldOperator txtName = new JTextFieldOperator(newFileWizard);
501         // clear text field
502
txtName.setText("");
503         txtName.typeText("MyTags");
504         newFileWizard.finish();
505         //XXX try { Thread.currentThread().sleep(5000); } catch (InterruptedException e) {}
506
//XXX HACK #48865
507
new Node(new ProjectsTabOperator().getProjectRootNode(PROJECT_NAME), "Web Pages|WEB-INF|tlds|MyTags.tld");
508         // check class is opened in Editor and then close it
509
new EditorOperator("MyTags.tld").close();
510         ref(Util.dumpProjectView(PROJECT_NAME));
511         //compareReferenceFiles();
512
}
513     
514     public void testCreateTagHandler() {
515         // workaround due to issue #46073
516
new ProjectsTabOperator().getProjectRootNode(PROJECT_NAME).select();
517         
518         new ActionNoBlock("File|New File", null).perform();
519         // WORKAROUND
520
new EventTool().waitNoEvent(1000);
521         WizardOperator newFileWizard = new WizardOperator("New File");
522         new JComboBoxOperator(newFileWizard).selectItem(PROJECT_NAME);
523         // workaround due to issue #46007
524
new Node(new JTreeOperator(newFileWizard), "Java Classes").select();
525         new Node(new JTreeOperator(newFileWizard), "Web").select();
526         new JListOperator(newFileWizard, 1).selectItem("Tag Handler");
527         newFileWizard.next();
528         JTextFieldOperator txtName = new JTextFieldOperator(newFileWizard);
529         // clear text field
530
txtName.setText("");
531         txtName.typeText("MyTag");
532         JComboBoxOperator pkg = new JComboBoxOperator(newFileWizard,1);
533         pkg.clearText();
534         pkg.typeText("tags");
535         newFileWizard.next();
536         new JButtonOperator(newFileWizard).push();
537         NbDialogOperator dialog = new NbDialogOperator("Browse Files");
538         new Node(new JTreeOperator(dialog),"Web Pages|WEB-INF|tlds|MyTags.tld").select();
539         new JButtonOperator(dialog,"Select File").push();
540         newFileWizard.finish();
541         // HACK
542
new Node(phelper.getSourceNode(), "tags|MyTag.java");
543         // check class is opened in Editor and then close it
544
EditorOperator editor = new EditorOperator("MyTag.java");
545         editor.insert(getTagHandlerCode(),43,1);
546         editor.saveDocument();
547         editor.close();
548         ref(Util.dumpProjectView(PROJECT_NAME));
549         //compareReferenceFiles();
550
}
551     
552     protected String JavaDoc getTagHandlerCode() {
553         return "out.print(\"TagOutput\"); \n";
554     }
555     
556     public void testRunTag() throws Throwable JavaDoc {
557         Node rootNode = new ProjectsTabOperator().getProjectRootNode(PROJECT_NAME);
558         Node jspNode = new Node(rootNode,"Web Pages|page2.jsp");
559         jspNode.performPopupAction("Open");
560         EditorOperator editor = new EditorOperator("page2.jsp");
561         editor.replace("<title>Page 2</title>",
562                 "<title><my:MyTag/></title>");
563         editor.insert("<%@taglib prefix=\"my\" uri=\"/WEB-INF/tlds/MyTags\"%>\n", 3, 1);
564         
565         new Action(null,"Run File").perform(jspNode);
566         waitBuildSuccessful();
567         editor.close();
568         assertDisplayerContent("<title>TagOutput</title>");
569     }
570     
571     public void testNewHTML() throws IOException JavaDoc {
572         // workaround due to issue #46073
573
new ProjectsTabOperator().getProjectRootNode(PROJECT_NAME).select();
574         
575         new ActionNoBlock("File|New File", null).perform();
576         // WORKAROUND
577
new EventTool().waitNoEvent(1000);
578         WizardOperator newFileWizard = new WizardOperator("New File");
579         new JComboBoxOperator(newFileWizard).selectItem(PROJECT_NAME);
580         // workaround due to issue #46007
581
new Node(new JTreeOperator(newFileWizard), "Java Classes").select();
582         new Node(new JTreeOperator(newFileWizard), "Web").select();
583         new JListOperator(newFileWizard, 1).selectItem("HTML");
584         newFileWizard.next();
585         JTextFieldOperator txtPackageName = new JTextFieldOperator(newFileWizard);
586         // clear text field
587
txtPackageName.setText("");
588         txtPackageName.typeText("HTML");
589         newFileWizard.finish();
590         // check class is opened in Editor and then close it
591
new EditorOperator("HTML.html").close();
592         ref(Util.dumpProjectView(PROJECT_NAME));
593         new Node(new ProjectsTabOperator().getProjectRootNode(PROJECT_NAME),"Web Pages|HTML.html");
594         //compareReferenceFiles();
595
//compareDD();
596
}
597     
598     public void testRunHTML() {
599         Node rootNode = new ProjectsTabOperator().getProjectRootNode(PROJECT_NAME);
600         new Node(rootNode,"Web Pages|HTML.html").performPopupAction("Open");
601         new EditorOperator("HTML.html").replace("<title></title>",
602                 "<title>HTML Page</title>");
603         new Action("Run|Run File|Run \"HTML.html\"",null).perform();
604         //waitBuildSuccessful();
605
new EditorOperator("HTML.html").close();
606         assertDisplayerContent("<title>HTML Page</title>");
607     }
608     
609     public void testNewSegment() throws IOException JavaDoc {
610         Node sample1Node = new Node(new ProjectsTabOperator().getProjectRootNode(PROJECT_NAME),"Web Pages");
611         // create a new class
612
new ActionNoBlock(null, "New|JSP").perform(sample1Node);
613         // WORKAROUND
614
new EventTool().waitNoEvent(1000);
615         WizardOperator newFileWizard = new WizardOperator("New JSP File");
616         new JTextFieldOperator(newFileWizard).typeText("segment");
617         new JCheckBoxOperator(newFileWizard).changeSelection(true);
618         newFileWizard.finish();
619         // check class is opened in Editor and then close all documents
620
new EditorOperator("segment.jspf").close();
621         ref(Util.dumpProjectView(PROJECT_NAME));
622         new Node(new ProjectsTabOperator().getProjectRootNode(PROJECT_NAME),"Web Pages|segment.jspf");
623         //compareReferenceFiles();
624
//compareDD();
625
}
626     
627     public void testNewDocument() throws IOException JavaDoc {
628         Node sample1Node = new Node(new ProjectsTabOperator().getProjectRootNode(PROJECT_NAME),"Web Pages");
629         // create a new class
630
new ActionNoBlock(null, "New|JSP").perform(sample1Node);
631         // WORKAROUND
632
new EventTool().waitNoEvent(1000);
633         WizardOperator newFileWizard = new WizardOperator("New JSP File");
634         new JTextFieldOperator(newFileWizard).typeText("document");
635         new JRadioButtonOperator(newFileWizard,1).changeSelection(true);
636         newFileWizard.finish();
637         // check class is opened in Editor and then close all documents
638
new EditorOperator("document.jspx").close();
639         ref(Util.dumpProjectView(PROJECT_NAME));
640         new Node(new ProjectsTabOperator().getProjectRootNode(PROJECT_NAME),"Web Pages|document.jspx");
641         //compareReferenceFiles();
642
//compareDD();
643
}
644     
645     public void testStopServer() throws Exception JavaDoc {
646         server.stop();
647         //try { Thread.currentThread().sleep(5000); } catch (InterruptedException e) {}
648
URL JavaDoc url = server.getServerURL();
649         URLConnection JavaDoc connection = url.openConnection();
650         try {
651             connection.connect();
652             fail("Connection to: "+url+" established, but the server" +
653                     " should not be running.");
654         } catch (ConnectException JavaDoc e) { }
655     }
656     
657     public void testStartServer() throws Exception JavaDoc {
658         server.start();
659         URL JavaDoc url = server.getServerURL();
660         URLConnection JavaDoc connection = url.openConnection();
661         connection.connect();
662     }
663     
664     public void testBrowserSettings() {
665         OptionsOperator optionsOper = OptionsOperator.invoke();
666         optionsOper.selectGeneral();
667         // "Web Browser:"
668
String JavaDoc webBrowserLabel = Bundle.getStringTrimmed(
669                 "org.netbeans.modules.options.general.Bundle",
670                 "CTL_Web_Browser");
671         JLabelOperator jloWebBrowser = new JLabelOperator(optionsOper, webBrowserLabel);
672         JComboBoxOperator combo = new JComboBoxOperator((JComboBox JavaDoc)jloWebBrowser.getLabelFor());
673         for (int i=0; i<combo.getItemCount(); i++) {
674             String JavaDoc item = combo.getItemAt(i).toString();
675             log("Browser: "+item);
676             if (!item.equals("<Default System Browser>") &&
677                     !item.equals("Mozilla") &&
678                     !item.equals("Netscape") &&
679                     !item.equals("Swing HTML Browser") &&
680                     !item.equals("Internet Explorer") &&
681                     !item.equals("Firefox") &&
682                     !item.equals("External Browser (command-line)"))
683                 fail("Unrecognized settings in Web Browser Options");
684         }
685         optionsOper.close();
686     }
687     
688     public void testFinish() {
689         server.stop();
690         ProjectSupport.closeProject(PROJECT_NAME);
691     }
692     
693     //********************************************************
694

695     protected void sleep(int milis) {
696         try {
697             Thread.sleep(milis);
698         } catch (InterruptedException JavaDoc ex) {
699             throw new JemmyException("Interrupted", ex);
700         }
701     }
702     
703     private void logAndCloseOutputs() throws IOException JavaDoc {
704         long timeout = JemmyProperties.getCurrentTimeout(
705                 "ComponentOperator.WaitComponentTimeout");
706         JemmyProperties.setCurrentTimeout(
707                 "ComponentOperator.WaitComponentTimeout", 3000);
708         try {
709             for (int i=0; i<10; i++) {
710                 OutputTabOperator output = new OutputTabOperator("");
711                 String JavaDoc log_name = "Output"+i+".log";
712                 int index = 0;
713                 while (new File JavaDoc(getWorkDir(),log_name).exists()) {
714                     log_name = "Output"+i+"_"+(++index)+".log";
715                 }
716                 log(log_name,output.getName()+"\n-------------\n\n"+
717                         output.getText());
718                 output.close();
719             }
720         } catch (TimeoutExpiredException e) { } catch (IOException JavaDoc e) { throw e; } finally {
721             JemmyProperties.setCurrentTimeout(
722                     "ComponentOperator.WaitComponentTimeout", timeout);
723         }
724     }
725     
726     private void waitBuildSuccessful() {
727         OutputTabOperator console = new OutputTabOperator(PROJECT_NAME);
728         console.getTimeouts().setTimeout("ComponentOperator.WaitStateTimeout", 180000);
729         console.waitText(BUILD_SUCCESSFUL);
730     }
731     
732     // private TomcatManager getTomcatManager() throws DeploymentManagerCreationException {
733
// String tomcatUri = "tomcat55:home=$bundled_home:base=$bundled_base";
734
// return (TomcatManager) TomcatFactory.create55().
735
// getDisconnectedDeploymentManager(tomcatUri);
736
// }
737

738     private void initDisplayer() {
739         if (urlDisplayer == null) {
740             urlDisplayer = TestURLDisplayer.getInstance();
741         }
742         urlDisplayer.invalidateURL();
743     }
744     
745     private void assertDisplayerContent(String JavaDoc substr) {
746         try {
747             urlDisplayer.waitURL();
748         } catch (InterruptedException JavaDoc ex) {
749             throw new JemmyException("Waiting interrupted.", ex);
750         }
751         String JavaDoc page = urlDisplayer.readURL();
752         boolean contains = page.indexOf(substr) > -1;
753         if (!contains) {
754             log("DUMP OF: "+urlDisplayer.getURL()+"\n");
755             log(page);
756         }
757         assertTrue("The '"+urlDisplayer.getURL()+"' page does not contain '"+substr+"'", contains);
758     }
759     
760     protected void installJemmyQueue() {
761         try {
762             SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
763                 public void run() {
764                     QueueTool.installQueue();
765                 }
766             });
767         } catch (Exception JavaDoc ex) {
768             throw new JemmyException("Cannot install Jemmy Queue.", ex);
769         }
770     }
771     
772     private static class ServerInstance {
773         private String JavaDoc host;
774         private int serverPort;
775         private String JavaDoc nodeName;
776         private String JavaDoc userName;
777         private String JavaDoc password;
778         private URL JavaDoc serverURL;
779         private static ServerInstance instance;
780         
781         private ServerInstance() {
782         }
783         
784         public static ServerInstance getDefault() {
785             if (instance == null) {
786                 instance = new ServerInstance();
787                 instance.host = "localhost";
788                 instance.serverPort = 8080;
789                 instance.nodeName = "Bundled Tomcat";
790                 instance.userName = "tomcat";
791                 instance.password = "tomcat";
792             }
793             return instance;
794         }
795         
796         public URL JavaDoc getServerURL() {
797             if (serverURL == null) {
798                 try {
799                     serverURL = new URL JavaDoc("http",host,serverPort,"");
800                 } catch (MalformedURLException JavaDoc mue) {
801                     throw new JemmyException("Cannot create server URL.", mue);
802                 }
803             }
804             return serverURL;
805         }
806         
807         public J2eeServerNode getServerNode() {
808             return J2eeServerNode.invoke(nodeName);
809         }
810         
811         public void stop() {
812             getServerNode().stop();
813         }
814         
815         public void start() {
816             getServerNode().start();
817         }
818     }
819 }
820
821
Popular Tags