KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > jsf > JsfFunctionalTest


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 org.netbeans.test.jsf;
20
21 import java.awt.event.KeyEvent JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.swing.text.Document JavaDoc;
25 import org.netbeans.jellytools.JellyTestCase;
26 import org.netbeans.jellytools.NbDialogOperator;
27 import org.netbeans.jellytools.NewFileWizardOperator;
28 import org.netbeans.jellytools.NewProjectWizardOperator;
29 import org.netbeans.jellytools.Bundle;
30 import org.netbeans.jellytools.EditorOperator;
31 import org.netbeans.jellytools.NewWebProjectNameLocationStepOperator;
32 import org.netbeans.jellytools.ProjectsTabOperator;
33 import org.netbeans.jellytools.actions.ActionNoBlock;
34 import org.netbeans.jellytools.actions.Action;
35 import org.netbeans.jellytools.actions.EditAction;
36 import org.netbeans.jellytools.actions.OpenAction;
37 import org.netbeans.jellytools.modules.form.ComponentPaletteOperator;
38 import org.netbeans.jellytools.modules.web.nodes.WebPagesNode;
39 import org.netbeans.jellytools.nodes.SourcePackagesNode;
40 import org.netbeans.jemmy.JemmyProperties;
41 import org.netbeans.jemmy.TimeoutExpiredException;
42 import org.netbeans.jemmy.operators.JTableOperator;
43 import org.netbeans.jellytools.nodes.Node;
44 import org.netbeans.jellytools.nodes.SourcePackagesNode;
45 import org.netbeans.jemmy.operators.JButtonOperator;
46 import org.netbeans.jemmy.operators.JCheckBoxOperator;
47 import org.netbeans.jemmy.operators.JListOperator;
48 import org.netbeans.jemmy.operators.JTreeOperator;
49 import org.netbeans.jemmy.operators.Operator.DefaultStringComparator;
50 import org.netbeans.junit.NbTestSuite;
51 import org.netbeans.junit.ide.ProjectSupport;
52 import org.netbeans.modules.editor.NbEditorUtilities;
53 /* TODO - fix to use new implementation
54 import org.netbeans.modules.web.jsf.JSFConfigDataObject;
55 import org.netbeans.modules.web.jsf.JSFConfigUtilities;
56 import org.netbeans.modules.web.jsf.config.model.FacesConfig;
57 import org.netbeans.modules.web.jsf.config.model.ManagedBean;
58 import org.netbeans.modules.web.jsf.config.model.NavigationCase;
59 import org.netbeans.modules.web.jsf.config.model.NavigationRule;
60  */

61
62 /** Test JSF support.
63  *
64  * @author Lukasz Grela
65  * @author Jiri Skrivanek
66  */

67 public class JsfFunctionalTest extends JellyTestCase{
68     
69     public static final String JavaDoc PROJECT_NAME = "myjsfproject";
70     public static final String JavaDoc WELCOME_JSP = "welcomeJSF.jsp";
71     public static final String JavaDoc INDEX_JSP = "index.jsp";
72     public static final String JavaDoc FROM_ACTION = "FromAction";
73     public static final String JavaDoc FROM_ACTION1 = "FromAction1";
74     public static final String JavaDoc FROM_OUTCOME = "FromOutcome";
75     public static final String JavaDoc DESCRIPTION = "Description";
76     
77     public JsfFunctionalTest(String JavaDoc name) {
78         super(name);
79     }
80     
81     public static NbTestSuite suite() {
82         NbTestSuite suite = new NbTestSuite();
83         suite.addTest(new JsfFunctionalTest("testCreateWebProjectWithJSF"));
84         suite.addTest(new JsfFunctionalTest("testManagedBeanWizard"));
85         suite.addTest(new JsfFunctionalTest("testManagedBeanDelete"));
86         suite.addTest(new JsfFunctionalTest("testAddManagedBean"));
87         suite.addTest(new JsfFunctionalTest("testAddNavigationRule"));
88         suite.addTest(new JsfFunctionalTest("testAddNavigationCase"));
89         suite.addTest(new JsfFunctionalTest("testAddNavigationCaseWithNewRule"));
90         suite.addTest(new JsfFunctionalTest("testAddJSFToProject"));
91         suite.addTest(new JsfFunctionalTest("testJSFPalette"));
92         return suite;
93     }
94     
95     public void setUp() {
96         System.out.println("### "+getName()+" ###");
97     }
98     
99     /** Use for internal test execution inside IDE
100      * @param args command line arguments
101      */

102     public static void main(java.lang.String JavaDoc[] args) {
103         junit.textui.TestRunner.run(suite());
104     }
105     
106     public void testCreateWebProjectWithJSF() throws IOException JavaDoc {
107         // "Web"
108
String JavaDoc web = Bundle.getStringTrimmed(
109                 "org.netbeans.modules.web.core.Bundle",
110                 "OpenIDE-Module-Display-Category");
111         // "Web Application"
112
String JavaDoc webApplication = Bundle.getStringTrimmed(
113                 "org.netbeans.modules.web.project.ui.wizards.Bundle",
114                 "Templates/Project/Web/emptyWeb.xml");
115         NewProjectWizardOperator nop = NewProjectWizardOperator.invoke();
116         nop.selectCategory(web);
117         nop.selectProject(webApplication);
118         nop.next();
119         NewWebProjectNameLocationStepOperator lop = new NewWebProjectNameLocationStepOperator();
120         lop.setProjectName(PROJECT_NAME);
121         lop.setProjectLocation(getDataDir().getCanonicalPath());
122         lop.next();
123         NewProjectWizardOperator frameworkStep = new NewProjectWizardOperator();
124         // select JavaServer Faces
125
new JTableOperator(frameworkStep).selectCell(0, 0);;
126         frameworkStep.finish();
127         // Opening Projects
128
String JavaDoc openingProjectsTitle = Bundle.getString(
129                 "org.netbeans.modules.project.ui.Bundle",
130                 "LBL_Opening_Projects_Progress");
131         try {
132             // wait at most 60 second until progress dialog dismiss
133
JemmyProperties.setCurrentTimeout("ComponentOperator.WaitStateTimeout", 60000);
134             new NbDialogOperator(openingProjectsTitle).waitClosed();
135         } catch (TimeoutExpiredException e) {
136             // ignore when progress dialog was closed before we started to wait for it
137
}
138         ProjectSupport.waitScanFinished();
139         // Check project contains all needed files.
140
WebPagesNode webPages = new WebPagesNode(PROJECT_NAME);
141         Node welcomeJSF = new Node(webPages, "welcomeJSF.jsp");
142         new OpenAction().perform(welcomeJSF);
143         new EditorOperator("welcomeJSF.jsp").close();
144         Node facesconfig = new Node(webPages, "WEB-INF|faces-config.xml");
145         new OpenAction().perform(facesconfig);
146         // open faces-config.xml is used in next test cases
147
new EditorOperator("faces-config.xml");
148     }
149     
150     /** Test JSF Managed Bean Wizard. */
151     public void testManagedBeanWizard(){
152         NewFileWizardOperator projectWizard = NewFileWizardOperator.invoke();
153         String JavaDoc filetype = Bundle.getStringTrimmed(
154                 "org/netbeans/modules/web/jsf/resources/Bundle",
155                 "Templates/JSP_Servlet/JSFManagedBean.java");
156         projectWizard.selectCategory("Web");
157         projectWizard.selectFileType(filetype);
158         projectWizard.next();
159         NewJSFBeanStepOperator bean = new NewJSFBeanStepOperator();
160         bean.setClassName("MyManagedBean");
161         bean.selectScope("session");
162         bean.cboPackage().getTextField().setText("mypackage");
163         bean.finish();
164         // verify
165
new EditorOperator("MyManagedBean.java").close();
166         EditorOperator facesEditor = new EditorOperator("faces-config.xml");
167         String JavaDoc expected = "<managed-bean>";
168         assertTrue("faces-config.xml should contain "+expected, facesEditor.contains(expected));
169         expected = "<managed-bean-name>MyManagedBean</managed-bean-name>";
170         assertTrue("faces-config.xml should contain "+expected, facesEditor.contains(expected));
171         expected = "<managed-bean-class>mypackage.MyManagedBean</managed-bean-class>";
172         assertTrue("faces-config.xml should contain "+expected, facesEditor.contains(expected));
173         expected = "<managed-bean-scope>session</managed-bean-scope>";
174         assertTrue("faces-config.xml should contain "+expected, facesEditor.contains(expected));
175         
176         /* TODO - fix to use new implementation
177         Document document = facesEditor.txtEditorPane().getDocument();
178         JSFConfigDataObject data = (JSFConfigDataObject)NbEditorUtilities.getDataObject(document);
179         List mbeans = JSFConfigUtilities.getAllManagedBeans(data);
180         assertEquals("There should be one managed bean", 1, mbeans.size());
181         ManagedBean managedBean = (ManagedBean)mbeans.get(0);
182         assertEquals("Wrong managed-bean-name.", "MyManagedBean", managedBean.getManagedBeanName());
183         assertEquals("Wrong managed-bean-class.", "mypackage.MyManagedBean", managedBean.getManagedBeanClass());
184         assertEquals("Wrong managed-bean-scope.", "session", managedBean.getManagedBeanScope());
185          */

186     }
187     
188     /** Test that delete safely bean removes record from faces-config.xml. */
189     public void testManagedBeanDelete() {
190         Node node = new Node(new SourcePackagesNode(PROJECT_NAME), "mypackage|MyManagedBean.java");
191         new ActionNoBlock(null, "Refactor|Safely Delete...").perform(node);
192         NbDialogOperator safeDeleteDialog = new NbDialogOperator("Safe Delete");
193         new JButtonOperator(safeDeleteDialog, "Refactor").push();
194         node.waitNotPresent();
195         // verify
196
EditorOperator facesEditor = new EditorOperator("faces-config.xml");
197         /* TODO - fix to use new implementation
198         Document document = facesEditor.txtEditorPane().getDocument();
199         JSFConfigDataObject data = (JSFConfigDataObject)NbEditorUtilities.getDataObject(document);
200         List mbeans = JSFConfigUtilities.getAllManagedBeans(data);
201          */

202         //TODO - fails because of issue 77310
203
//assertEquals("There should be none managed bean.", 0, mbeans.size());
204
}
205     
206     /** Test adding JSF Managed Bean from faces-config.xml. */
207     public void testAddManagedBean(){
208         EditorOperator editor = new EditorOperator("faces-config.xml");
209         Action addBeanAction = new ActionNoBlock(null, "JavaServer Faces|Add Managed Bean...");
210         addBeanAction.perform(editor);
211         AddManagedBeanOperator addBeanOper = new AddManagedBeanOperator();
212         addBeanOper.setBeanName("SecondBean");
213         addBeanOper.setBeanClass("mypackage.MyManagedBean");
214         addBeanOper.selectScope("application");
215         addBeanOper.setBeanDescription(DESCRIPTION);
216         addBeanOper.add();
217         // verify
218
/* TODO - fix to use new implementation
219         Document document = editor.txtEditorPane().getDocument();
220         JSFConfigDataObject data = (JSFConfigDataObject)NbEditorUtilities.getDataObject(document);
221         List mbeans = JSFConfigUtilities.getAllManagedBeans(data);
222         for (int i = 0; i < mbeans.size(); i++) {
223             ManagedBean managedBean = (ManagedBean)mbeans.get(i);
224             if(managedBean.getManagedBeanName().equals("SecondBean")) {
225                 assertEquals("Wrong managed-bean-class.", "mypackage.MyManagedBean", managedBean.getManagedBeanClass());
226                 assertEquals("Wrong managed-bean-scope.", "application", managedBean.getManagedBeanScope());
227                 return;
228             }
229         }
230         fail("Managed bean record not added to faces-config.xml.");
231         */

232     }
233     
234     /** Test adding navigation rule from faces-config.xml. */
235     public void testAddNavigationRule() throws IOException JavaDoc {
236         EditorOperator editor = new EditorOperator("faces-config.xml");
237         Action addRule = new ActionNoBlock(null, "JavaServer Faces|Add Navigation Rule...");
238         addRule.perform(editor);
239         AddNavigationRuleDialogOperator rule = new AddNavigationRuleDialogOperator();
240         rule.setRuleFromView("/"+WELCOME_JSP);
241         rule.setRuleDescription(DESCRIPTION);
242         rule.add();
243         editor.save();
244         // verify
245
Document JavaDoc document = editor.txtEditorPane().getDocument();
246         /* TODO - fix to use new implementation
247         JSFConfigDataObject data = (JSFConfigDataObject)NbEditorUtilities.getDataObject(document);
248         FacesConfig facesConfig = data.getFacesConfig();
249         NavigationRule[] rules = facesConfig.getNavigationRule();
250         assertEquals("Wrong From View.", "/"+WELCOME_JSP, rules[0].getFromViewId());
251         assertTrue("Wrong rule description.", rules[0].getDescription()[0].indexOf(DESCRIPTION) > -1);
252         assertEquals("There should be one navigation rule", 1, rules.length);
253          */

254     }
255     
256     /** Test adding navigation case from faces-config.xml. */
257     public void testAddNavigationCase() throws IOException JavaDoc {
258         EditorOperator editor = new EditorOperator("faces-config.xml");
259         Action addCase = new ActionNoBlock(null, "JavaServer Faces|Add Navigation Case...");
260         addCase.perform(editor);
261         AddNavigationCaseDialogOperator caseOper = new AddNavigationCaseDialogOperator();
262         caseOper.selectFromView("/"+WELCOME_JSP);
263         caseOper.selectToView("/"+WELCOME_JSP);
264         caseOper.setFromAction(FROM_ACTION);
265         caseOper.setFromOutcome(FROM_OUTCOME);
266         caseOper.setRuleDescription(DESCRIPTION);
267         caseOper.add();
268         editor.save();
269         // verify
270
Document JavaDoc document = editor.txtEditorPane().getDocument();
271         /* TODO - fix to use new implementation
272         JSFConfigDataObject data = (JSFConfigDataObject)NbEditorUtilities.getDataObject(document);
273         FacesConfig facesConfig = data.getFacesConfig();
274         NavigationRule[] rules = facesConfig.getNavigationRule();
275         assertEquals("There should be one navigation rule.", 1, rules.length);
276         NavigationCase[] cases = rules[0].getNavigationCase();
277         assertTrue("There should be one navigation case.", cases.length==1);
278         assertEquals("Wrong From Action.", FROM_ACTION, cases[0].getFromAction());
279         assertEquals("Wrong From OutCome.", FROM_OUTCOME, cases[0].getFromOutcome());
280          */

281         // TODO - fails because of issue 91329
282
//assertFalse("Should not be redirected.", cases[0].isRedirected());
283
}
284     
285     /** Test adding navigation case with new rule from faces-config.xml. */
286     public void testAddNavigationCaseWithNewRule() throws IOException JavaDoc {
287         EditorOperator editor = new EditorOperator("faces-config.xml");
288         Action addCase = new ActionNoBlock(null, "JavaServer Faces|Add Navigation Case...");
289         addCase.perform(editor);
290         AddNavigationCaseDialogOperator caseOper = new AddNavigationCaseDialogOperator();
291         caseOper.cboFromView().getTextField().setText("/"+INDEX_JSP);
292         caseOper.setFromAction(FROM_ACTION);
293         caseOper.setFromOutcome(FROM_OUTCOME);
294         caseOper.setRuleDescription(DESCRIPTION);
295         caseOper.checkRedirect(true);
296         caseOper.cboToView().getTextField().setText("/"+INDEX_JSP);
297         caseOper.add();
298         editor.save();
299         // verify
300
Document JavaDoc document = editor.txtEditorPane().getDocument();
301         /* TODO - fix to use new implementation
302         JSFConfigDataObject data = (JSFConfigDataObject)NbEditorUtilities.getDataObject(document);
303         FacesConfig facesConfig = data.getFacesConfig();
304         NavigationRule[] rules = facesConfig.getNavigationRule();
305         assertEquals("There should be two navigation rules.", 2, rules.length);
306         assertEquals("Wrong From View.", "/"+INDEX_JSP, rules[1].getFromViewId());
307         NavigationCase[] cases = rules[1].getNavigationCase();
308         assertEquals("There should be one navigation case.", 1, cases.length);
309         assertEquals("Wrong From Action.", FROM_ACTION, cases[0].getFromAction());
310         assertEquals("Wrong From OutCome.", FROM_OUTCOME, cases[0].getFromOutcome());
311          */

312         // TODO - fails because of issue 91329
313
//assertTrue("Should be redirected.", cases[0].isRedirected());
314
}
315
316     /** Test adding JSF framework to existing web application. */
317     public void testAddJSFToProject() throws IOException JavaDoc {
318         // "Web"
319
String JavaDoc web = Bundle.getStringTrimmed(
320                 "org.netbeans.modules.web.core.Bundle",
321                 "OpenIDE-Module-Display-Category");
322         // "Web Application"
323
String JavaDoc webApplication = Bundle.getStringTrimmed(
324                 "org.netbeans.modules.web.project.ui.wizards.Bundle",
325                 "Templates/Project/Web/emptyWeb.xml");
326         NewProjectWizardOperator nop = NewProjectWizardOperator.invoke();
327         nop.selectCategory(web);
328         nop.selectProject(webApplication);
329         nop.next();
330         NewWebProjectNameLocationStepOperator lop = new NewWebProjectNameLocationStepOperator();
331         lop.setProjectName(PROJECT_NAME+"2");
332         lop.setProjectLocation(getDataDir().getCanonicalPath());
333         lop.finish();
334         // Opening Projects
335
String JavaDoc openingProjectsTitle = Bundle.getString(
336                 "org.netbeans.modules.project.ui.Bundle",
337                 "LBL_Opening_Projects_Progress");
338         try {
339             // wait at most 60 second until progress dialog dismiss
340
JemmyProperties.setCurrentTimeout("ComponentOperator.WaitStateTimeout", 60000);
341             new NbDialogOperator(openingProjectsTitle).waitClosed();
342         } catch (TimeoutExpiredException e) {
343             // ignore when progress dialog was closed before we started to wait for it
344
}
345    
346         // add JSF framework using project properties
347
// open project properties
348
ProjectsTabOperator.invoke().getProjectRootNode(PROJECT_NAME+"2").properties();
349         // "Project Properties"
350
String JavaDoc projectPropertiesTitle = Bundle.getStringTrimmed("org.netbeans.modules.web.project.ui.customizer.Bundle", "LBL_Customizer_Title");
351         NbDialogOperator propertiesDialogOper = new NbDialogOperator(projectPropertiesTitle);
352         // select "Frameworks" category
353
new Node(new JTreeOperator(propertiesDialogOper), "Frameworks").select();
354         new JButtonOperator(propertiesDialogOper, "Add").pushNoBlock();
355         NbDialogOperator addFrameworkOper = new NbDialogOperator("Add a Framework");
356         // select "JavaServer Faces" but item is instance of JSFFrameworkProvider which we need to select
357
new JListOperator(addFrameworkOper).selectItem("JSF");
358         addFrameworkOper.ok();
359         new JCheckBoxOperator(propertiesDialogOper, "Validate XML").setSelected(false);
360         new JCheckBoxOperator(propertiesDialogOper, "Verify Objects").setSelected(true);
361         // confirm properties dialog
362
propertiesDialogOper.ok();
363
364         // Check project contains all needed files.
365
WebPagesNode webPages = new WebPagesNode(PROJECT_NAME+"2");
366         Node welcomeJSF = new Node(webPages, "welcomeJSF.jsp");
367         Node facesconfig = new Node(webPages, "WEB-INF|faces-config.xml");
368         webPages.setComparator(new DefaultStringComparator(true, true));
369         Node webXML = new Node(webPages, "WEB-INF|web.xml");
370         new EditAction().performAPI(webXML);
371         EditorOperator webXMLEditor = new EditorOperator("web.xml");
372         webXMLEditor.select("validateXml");
373         assertTrue("Validate XML should be false.", webXMLEditor.getText(webXMLEditor.getLineNumber()+1).indexOf("false") > -1);
374         webXMLEditor.select("verifyObjects");
375         assertTrue("Verify Objects should be true.", webXMLEditor.getText(webXMLEditor.getLineNumber()+1).indexOf("true") > -1);
376         webXMLEditor.close();
377     }
378     
379     /** Test JSF Palette. */
380     public void testJSFPalette() {
381         EditorOperator editorOper = new EditorOperator(INDEX_JSP);
382         ComponentPaletteOperator paletteOper = new ComponentPaletteOperator();
383         // collapse HTML category
384
JCheckBoxOperator htmlCategoryOper = new JCheckBoxOperator(paletteOper, "HTML");
385         if(htmlCategoryOper.isSelected()) {
386             htmlCategoryOper.push();
387         }
388         // expand JSF category
389
JCheckBoxOperator jsfCategoryOper = new JCheckBoxOperator(paletteOper, "JSF");
390         if(!jsfCategoryOper.isSelected()) {
391             jsfCategoryOper.push();
392         }
393         paletteOper.selectComponent("JSF Form");
394         paletteOper.pushKey(KeyEvent.VK_ENTER);
395         new NbDialogOperator("Insert JSF Form").ok();
396         String JavaDoc expected = "<f:view>";
397         assertTrue("index.jsp should contain "+expected+".", editorOper.contains(expected));
398         expected = "<h:form>";
399         assertTrue("index.jsp should contain "+expected+".", editorOper.contains(expected));
400         expected = "</h:form>";
401         assertTrue("index.jsp should contain "+expected+".", editorOper.contains(expected));
402         expected = "</f:view>";
403         assertTrue("index.jsp should contain "+expected+".", editorOper.contains(expected));
404         
405         paletteOper.selectComponent("JSF Data Table");
406         paletteOper.pushKey(KeyEvent.VK_ENTER);
407         new NbDialogOperator("Insert JSF Data Table").ok();
408         expected = "<h:dataTable value=\"#{arrayOrCollectionOf}\" var=\"item\">";
409         assertTrue("index.jsp should contain "+expected+".", editorOper.contains(expected));
410         expected = "</h:dataTable>";
411         assertTrue("index.jsp should contain "+expected+".", editorOper.contains(expected));
412     }
413 }
414
Popular Tags