KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > EndToEndTest


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package test;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.net.URI JavaDoc;
26 import java.net.URLConnection JavaDoc;
27 import java.util.Properties JavaDoc;
28 import javax.swing.JTextField JavaDoc;
29 import org.netbeans.jellytools.Bundle;
30 import org.netbeans.jellytools.EditorOperator;
31 import org.netbeans.jellytools.NewFileWizardOperator;
32 import org.netbeans.jellytools.OutputTabOperator;
33 import org.netbeans.jellytools.ProjectsTabOperator;
34 import org.netbeans.jellytools.actions.Action;
35 import org.netbeans.jellytools.actions.EditAction;
36 import org.netbeans.jellytools.modules.j2ee.nodes.J2eeServerNode;
37 import org.netbeans.jemmy.JemmyException;
38 import org.netbeans.jemmy.Waitable;
39 import org.netbeans.jemmy.Waiter;
40 import org.netbeans.jemmy.operators.JButtonOperator;
41 import org.netbeans.junit.NbTestSuite;
42 import org.netbeans.jellytools.JellyTestCase;
43 import org.netbeans.jellytools.NbDialogOperator;
44 import org.netbeans.jellytools.NewFileNameLocationStepOperator;
45 import org.netbeans.jellytools.NewProjectWizardOperator;
46 import org.netbeans.jellytools.NewWebProjectNameLocationStepOperator;
47 import org.netbeans.jellytools.actions.ActionNoBlock;
48 import org.netbeans.jellytools.actions.OpenAction;
49 import org.netbeans.jellytools.modules.web.NewJspFileNameStepOperator;
50 import org.netbeans.jellytools.modules.web.nodes.WebPagesNode;
51 import org.netbeans.jellytools.nodes.Node;
52 import org.netbeans.jemmy.JemmyProperties;
53 import org.netbeans.jemmy.TimeoutExpiredException;
54 import org.netbeans.jemmy.operators.JCheckBoxOperator;
55 import org.netbeans.jemmy.operators.JLabelOperator;
56 import org.netbeans.jemmy.operators.JRadioButtonOperator;
57 import org.netbeans.jemmy.operators.JTableOperator;
58 import org.netbeans.jemmy.operators.JTextFieldOperator;
59 import org.netbeans.jemmy.operators.JTreeOperator;
60 import org.netbeans.jemmy.operators.Operator.DefaultStringComparator;
61 import org.netbeans.junit.ide.ProjectSupport;
62
63 /** End-to-end scenario test based on
64  * http://qa.netbeans.org/modules/webapps/promo-f/frameworks/struts-user-scenario.html.
65  *
66  * @author Jiri Skrivanek
67  */

68 public class EndToEndTest extends JellyTestCase {
69     
70     public static final String JavaDoc PROJECT_NAME = "StrutsWebApplication";
71     
72     /** Constructor required by JUnit */
73     public EndToEndTest(String JavaDoc name) {
74         super(name);
75     }
76     
77     /** Creates suite from particular test cases. You can define order of testcases here. */
78     public static NbTestSuite suite() {
79         NbTestSuite suite = new NbTestSuite();
80         suite.addTest(new EndToEndTest("testSetupStrutsProject"));
81         suite.addTest(new EndToEndTest("testCreateLoginPage"));
82         suite.addTest(new EndToEndTest("testCreateLoginBean"));
83         suite.addTest(new EndToEndTest("testCreateLoginAction"));
84         suite.addTest(new EndToEndTest("testCreateSecurityManager"));
85         suite.addTest(new EndToEndTest("testCreateForward"));
86         suite.addTest(new EndToEndTest("testCreateShopPage"));
87         suite.addTest(new EndToEndTest("testCreateLogoutPage"));
88         suite.addTest(new EndToEndTest("testCreateForwardInclude"));
89         suite.addTest(new EndToEndTest("testRunApplication"));
90         return suite;
91     }
92     
93     /* Method allowing test execution directly from the IDE. */
94     public static void main(java.lang.String JavaDoc[] args) {
95         // run whole suite
96
junit.textui.TestRunner.run(suite());
97         // run only selected test case
98
//junit.textui.TestRunner.run(new EndToEndTest("test1"));
99
}
100     
101     /** Called before every test case. */
102     public void setUp() {
103         System.out.println("######## "+getName()+" #######");
104     }
105     
106     /** Called after every test case. */
107     public void tearDown() {
108     }
109     
110     /** Create web application with struts support and check correctness. */
111     public void testSetupStrutsProject() throws IOException JavaDoc {
112         // "Web"
113
String JavaDoc web = Bundle.getStringTrimmed(
114                 "org.netbeans.modules.web.core.Bundle",
115                 "OpenIDE-Module-Display-Category");
116         // "Web Application"
117
String JavaDoc webApplication = Bundle.getStringTrimmed(
118                 "org.netbeans.modules.web.project.ui.wizards.Bundle",
119                 "Templates/Project/Web/emptyWeb.xml");
120         NewProjectWizardOperator nop = NewProjectWizardOperator.invoke();
121         nop.selectCategory(web);
122         nop.selectProject(webApplication);
123         nop.next();
124         NewWebProjectNameLocationStepOperator lop = new NewWebProjectNameLocationStepOperator();
125         lop.setProjectName(PROJECT_NAME);
126         lop.setProjectLocation(getDataDir().getCanonicalPath());
127         lop.next();
128         NewProjectWizardOperator frameworkStep = new NewProjectWizardOperator();
129         // select Struts
130
new JTableOperator(frameworkStep).selectCell(1, 0);
131         // set ApplicationResource location
132
new JTextFieldOperator(
133                 (JTextField JavaDoc)new JLabelOperator(frameworkStep, "Application Resource:").getLabelFor()
134                 ).setText("com.mycompany.eshop.struts.ApplicationResource");
135         frameworkStep.finish();
136         // Opening Projects
137
String JavaDoc openingProjectsTitle = Bundle.getString(
138                 "org.netbeans.modules.project.ui.Bundle",
139                 "LBL_Opening_Projects_Progress");
140         try {
141             // wait at most 60 second until progress dialog dismiss
142
JemmyProperties.setCurrentTimeout("ComponentOperator.WaitStateTimeout", 60000);
143             new NbDialogOperator(openingProjectsTitle).waitClosed();
144         } catch (TimeoutExpiredException e) {
145             // ignore when progress dialog was closed before we started to wait for it
146
}
147         ProjectSupport.waitScanFinished();
148         // Check project contains all needed files.
149
WebPagesNode webPages = new WebPagesNode(PROJECT_NAME);
150         new Node(webPages, "welcomeStruts.jsp");
151         Node strutsConfig = new Node(webPages, "WEB-INF|struts-config.xml");
152         new OpenAction().performAPI(strutsConfig);
153         webPages.setComparator(new DefaultStringComparator(true, true));
154         Node webXML = new Node(webPages, "WEB-INF|web.xml");
155         new EditAction().performAPI(webXML);
156         EditorOperator webXMLEditor = new EditorOperator("web.xml");
157         String JavaDoc expected = "<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>";
158         assertTrue("ActionServlet should be created in web.xml.", webXMLEditor.getText().indexOf(expected) > -1);
159         webXMLEditor.replace("index.jsp", "login.jsp");
160         webXMLEditor.save();
161     }
162     
163     /** Create login.jsp and insert prepared source code to it. */
164     public void testCreateLoginPage() throws IOException JavaDoc {
165         NewFileWizardOperator newWizardOper = NewFileWizardOperator.invoke();
166         newWizardOper.selectProject(PROJECT_NAME);
167         newWizardOper.selectCategory("Web");
168         newWizardOper.selectFileType("JSP");
169         newWizardOper.next();
170         NewJspFileNameStepOperator jspStepOper = new NewJspFileNameStepOperator();
171         jspStepOper.setJSPFileName("login");
172         jspStepOper.setFolder("");
173         jspStepOper.finish();
174         // verify
175
EditorOperator loginEditorOper = new EditorOperator("login.jsp");
176         Properties JavaDoc properties = new Properties JavaDoc();
177         properties.load(this.getClass().getResourceAsStream("Bundle.properties"));
178         String JavaDoc sourceCode = properties.getProperty("login");
179         // wait for text to be displayed
180
loginEditorOper.txtEditorPane().waitText("JSP Page", -1);
181         loginEditorOper.replace(loginEditorOper.getText(), sourceCode);
182         loginEditorOper.save();
183     }
184     
185     /** Create bean which handles login form. */
186     public void testCreateLoginBean() throws IOException JavaDoc {
187         NewFileWizardOperator newWizardOper = NewFileWizardOperator.invoke();
188         newWizardOper.selectProject(PROJECT_NAME);
189         newWizardOper.selectCategory("Web");
190         newWizardOper.selectFileType("Struts ActionForm Bean");
191         newWizardOper.next();
192         NewFileNameLocationStepOperator nameStepOper = new NewFileNameLocationStepOperator();
193         nameStepOper.setObjectName("LoginForm");
194         nameStepOper.setPackage("com.mycompany.eshop.struts.forms");
195         nameStepOper.finish();
196         EditorOperator loginEditorOper = new EditorOperator("LoginForm.java");
197         Properties JavaDoc properties = new Properties JavaDoc();
198         properties.load(this.getClass().getResourceAsStream("Bundle.properties"));
199         String JavaDoc sourceCode = properties.getProperty("LoginForm");
200         loginEditorOper.replace(loginEditorOper.getText(), sourceCode);
201         loginEditorOper.save();
202         EditorOperator strutsConfigEditor = new EditorOperator("struts-config.xml");
203         String JavaDoc expected = "<form-bean name=\"LoginForm\" type=\"com.mycompany.eshop.struts.forms.LoginForm\"/>";
204         assertTrue("form-bean record should be added to struts-config.xml.", strutsConfigEditor.getText().indexOf(expected) > -1);
205     }
206     
207     /** Create struts action which verify input fields in login form. */
208     public void testCreateLoginAction() throws IOException JavaDoc {
209         NewFileWizardOperator newWizardOper = NewFileWizardOperator.invoke();
210         newWizardOper.selectProject(PROJECT_NAME);
211         newWizardOper.selectCategory("Web");
212         newWizardOper.selectFileType("Struts Action");
213         newWizardOper.next();
214         NewFileNameLocationStepOperator nameStepOper = new NewFileNameLocationStepOperator();
215         nameStepOper.setObjectName("LoginVerifyAction");
216         nameStepOper.setPackage("com.mycompany.eshop.struts.actions");
217         JTextFieldOperator txtActionPath = new JTextFieldOperator(
218                 (JTextField JavaDoc)new JLabelOperator(nameStepOper, "Action Path:").getLabelFor());
219         txtActionPath.setText("/Login/Verify");
220         nameStepOper.next();
221         // "ActionForm Bean, Parameter" page
222
NewFileWizardOperator actionBeanStepOper = new NewFileWizardOperator();
223         // set Input Resource
224
new JTextFieldOperator(actionBeanStepOper, "/").setText("/login.jsp");
225         new JRadioButtonOperator(actionBeanStepOper, "Request").push();
226         actionBeanStepOper.finish();
227         EditorOperator loginEditorOper = new EditorOperator("LoginVerifyAction.java");
228         Properties JavaDoc properties = new Properties JavaDoc();
229         properties.load(this.getClass().getResourceAsStream("Bundle.properties"));
230         String JavaDoc sourceCode = properties.getProperty("LoginVerifyAction");
231         loginEditorOper.replace("return mapping.findForward(SUCCESS);", sourceCode);
232         loginEditorOper.save();
233         EditorOperator strutsConfigEditor = new EditorOperator("struts-config.xml");
234         String JavaDoc expected = "<action input=\"/login.jsp\" name=\"LoginForm\" path=\"/Login/Verify\" scope=\"request\" type=\"com.mycompany.eshop.struts.actions.LoginVerifyAction\"/>";
235         assertTrue("action record should be added to struts-config.xml.", strutsConfigEditor.getText().indexOf(expected) > -1);
236     }
237     
238     /** Create SecurityManager class. */
239     public void testCreateSecurityManager() throws IOException JavaDoc {
240         NewFileWizardOperator.create(PROJECT_NAME, "Java Classes", "Empty Java File", "com.mycompany.eshop.security", "SecurityManager");
241         EditorOperator editorOper = new EditorOperator("SecurityManager.java");
242         Properties JavaDoc properties = new Properties JavaDoc();
243         properties.load(this.getClass().getResourceAsStream("Bundle.properties"));
244         String JavaDoc sourceCode = properties.getProperty("SecurityManager");
245         editorOper.replace(editorOper.getText(), sourceCode);
246         editorOper.save();
247     }
248     
249     /** Call "Add Forward" action in struts-config.xml and fill in the dialog values. */
250     public void testCreateForward() {
251         EditorOperator strutsConfigEditor = new EditorOperator("struts-config.xml");
252         ActionNoBlock addForwardAction = new ActionNoBlock(null, "Struts|Add Forward");
253         addForwardAction.setComparator(new DefaultStringComparator(true, true));
254         addForwardAction.perform(strutsConfigEditor);
255         NbDialogOperator addForwardOper = new NbDialogOperator("Add Forward");
256         JTextFieldOperator txtForwardName = new JTextFieldOperator(
257                 (JTextField JavaDoc)new JLabelOperator(addForwardOper, "Forward Name:").getLabelFor());
258         txtForwardName.setText("success");
259         new JTextFieldOperator(addForwardOper, "/").setText("/shop.jsp");
260         // set Redirect check box
261
new JCheckBoxOperator(addForwardOper).push();
262         // select Action as Location
263
new JRadioButtonOperator(addForwardOper, "Action:", 1).push();
264         new JButtonOperator(addForwardOper, "Add").push();
265         String JavaDoc expected = "<forward name=\"success\" path=\"/shop.jsp\" redirect=\"true\"/>";
266         assertTrue("forward record should be added to struts-config.xml.", strutsConfigEditor.getText().indexOf(expected) > -1);
267         strutsConfigEditor.save();
268     }
269     
270     /** Create shop.jsp and insert prepared source code to it. */
271     public void testCreateShopPage() throws IOException JavaDoc {
272         NewFileWizardOperator newWizardOper = NewFileWizardOperator.invoke();
273         newWizardOper.selectProject(PROJECT_NAME);
274         newWizardOper.selectCategory("Web");
275         newWizardOper.selectFileType("JSP");
276         newWizardOper.next();
277         NewJspFileNameStepOperator jspStepOper = new NewJspFileNameStepOperator();
278         jspStepOper.setJSPFileName("shop");
279         jspStepOper.setFolder("");
280         jspStepOper.finish();
281         // verify
282
EditorOperator editorOper = new EditorOperator("shop.jsp");
283         Properties JavaDoc properties = new Properties JavaDoc();
284         properties.load(this.getClass().getResourceAsStream("Bundle.properties"));
285         String JavaDoc sourceCode = properties.getProperty("shop");
286         // wait for text to be displayed
287
editorOper.txtEditorPane().waitText("JSP Page", -1);
288         editorOper.replace(editorOper.getText(), sourceCode);
289         editorOper.save();
290     }
291     
292     /** Create logout.jsp and insert prepared source code to it. */
293     public void testCreateLogoutPage() throws IOException JavaDoc {
294         NewFileWizardOperator newWizardOper = NewFileWizardOperator.invoke();
295         newWizardOper.selectProject(PROJECT_NAME);
296         newWizardOper.selectCategory("Web");
297         newWizardOper.selectFileType("JSP");
298         newWizardOper.next();
299         NewJspFileNameStepOperator jspStepOper = new NewJspFileNameStepOperator();
300         jspStepOper.setJSPFileName("logout");
301         jspStepOper.setFolder("");
302         jspStepOper.finish();
303         // verify
304
EditorOperator editorOper = new EditorOperator("logout.jsp");
305         Properties JavaDoc properties = new Properties JavaDoc();
306         properties.load(this.getClass().getResourceAsStream("Bundle.properties"));
307         String JavaDoc sourceCode = properties.getProperty("logout");
308         // wait for text to be displayed
309
editorOper.txtEditorPane().waitText("JSP Page", -1);
310         editorOper.replace(editorOper.getText(), sourceCode);
311         editorOper.save();
312     }
313     
314     /** Call "Add Forward/Include" action in struts-config.xml and fill in the dialog values. */
315     public void testCreateForwardInclude() {
316         EditorOperator strutsConfigEditor = new EditorOperator("struts-config.xml");
317         ActionNoBlock addForwardAction = new ActionNoBlock(null, "Struts|Add Forward/Include");
318         addForwardAction.perform(strutsConfigEditor);
319         NbDialogOperator addForwardOper = new NbDialogOperator("Add Forward/Include Action");
320         // set Action Path
321
new JTextFieldOperator(addForwardOper, "/").setText("/Logout");
322         new JButtonOperator(addForwardOper, "Browse").pushNoBlock();
323         NbDialogOperator browseOper = new NbDialogOperator("Browse Files");
324         new Node(new JTreeOperator(browseOper), "Web Pages|logout.jsp").select();
325         new JButtonOperator(browseOper, "Select File").push();
326         new JButtonOperator(addForwardOper, "Add").push();
327         String JavaDoc expected = "<action forward=\"/logout.jsp\" path=\"/Logout\"/>";
328         assertTrue("forward record should be added to struts-config.xml.", strutsConfigEditor.getText().indexOf(expected) > -1);
329         strutsConfigEditor.save();
330     }
331
332     /** Run created application. */
333     public void testRunApplication() {
334         // not display browser on run
335
// open project properties
336
ProjectsTabOperator.invoke().getProjectRootNode(PROJECT_NAME).properties();
337         // "Project Properties"
338
String JavaDoc projectPropertiesTitle = Bundle.getStringTrimmed("org.netbeans.modules.web.project.ui.customizer.Bundle", "LBL_Customizer_Title");
339         NbDialogOperator propertiesDialogOper = new NbDialogOperator(projectPropertiesTitle);
340         // select "Run" category
341
new Node(new JTreeOperator(propertiesDialogOper), "Run").select();
342         String JavaDoc displayBrowserLabel = Bundle.getStringTrimmed("org.netbeans.modules.web.project.ui.customizer.Bundle", "LBL_CustomizeRun_DisplayBrowser_JCheckBox");
343         new JCheckBoxOperator(propertiesDialogOper, displayBrowserLabel).setSelected(false);
344         // confirm properties dialog
345
propertiesDialogOper.ok();
346
347         try {
348             // "Run Project"
349
String JavaDoc runProjectItem = Bundle.getString("org.netbeans.modules.web.project.ui.Bundle", "LBL_RunAction_Name");
350             new Action(null, runProjectItem).perform(new ProjectsTabOperator().getProjectRootNode(PROJECT_NAME));
351             waitText(PROJECT_NAME, 240000, "Login page");
352         } finally {
353             // log messages from output
354
getLog("RunOutput").print(new OutputTabOperator(PROJECT_NAME).getText());
355             getLog("ServerLog").print(new OutputTabOperator("Sun").getText());
356             // stop Tomcat server
357
try {
358                 J2eeServerNode serverNode = new J2eeServerNode("Sun");
359                 serverNode.stop();
360             } catch (JemmyException e) {
361                 // ignore it
362
}
363         }
364     }
365     
366     /** Opens URL connection and waits for given text. It thows TimeoutExpiredException
367      * if timeout expires.
368      * @param urlSuffix suffix added to server URL
369      * @param timeout time to wait
370      * @param text text to be found
371      */

372     public static void waitText(final String JavaDoc urlSuffix, final long timeout, final String JavaDoc text) {
373         Waitable waitable = new Waitable() {
374             public Object JavaDoc actionProduced(Object JavaDoc obj) {
375                 InputStream JavaDoc is = null;
376                 try {
377                     URLConnection JavaDoc connection = new URI JavaDoc("http://localhost:8080/"+urlSuffix).toURL().openConnection();
378                     connection.setReadTimeout(Long.valueOf(timeout).intValue());
379                     is = connection.getInputStream();
380                     BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
381                     String JavaDoc line = br.readLine();
382                     while(line != null) {
383                         if(line.indexOf(text) > -1) {
384                             return Boolean.TRUE;
385                         }
386                         line = br.readLine();
387                     }
388                     is.close();
389                 } catch (Exception JavaDoc e) {
390                     //e.printStackTrace();
391
return null;
392                 } finally {
393                     if(is != null) {
394                         try {
395                             is.close();
396                         } catch (IOException JavaDoc e) {
397                             // ignore
398
}
399                     }
400                 }
401                 return null;
402             }
403             public String JavaDoc getDescription() {
404                 return("Text \""+text+"\" at http://localhost:8080/"+urlSuffix);
405             }
406         };
407         Waiter waiter = new Waiter(waitable);
408         waiter.getTimeouts().setTimeout("Waiter.WaitingTime", timeout);
409         try {
410             waiter.waitAction(null);
411         } catch (InterruptedException JavaDoc e) {
412             throw new JemmyException("Exception while waiting for connection.", e);
413         }
414     }
415 }
416
Popular Tags