KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > qa > form > OpenTempl_defaultPack


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.qa.form;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import org.netbeans.jellytools.NewFileWizardOperator;
25 import org.netbeans.jellytools.ProjectsTabOperator;
26 import org.netbeans.jellytools.modules.form.ComponentInspectorOperator;
27 import org.netbeans.jemmy.operators.JButtonOperator;
28 import org.netbeans.jemmy.operators.JComboBoxOperator;
29 import org.netbeans.junit.NbTestSuite;
30 import org.netbeans.jellytools.JellyTestCase;
31 import org.netbeans.jellytools.MainWindowOperator;
32 import org.netbeans.jellytools.NbDialogOperator;
33 import org.netbeans.jellytools.NewProjectNameLocationStepOperator;
34 import org.netbeans.jellytools.NewProjectWizardOperator;
35 import org.netbeans.jellytools.actions.DeleteAction;
36 import org.netbeans.jellytools.nodes.ProjectRootNode;
37 import org.netbeans.jemmy.operators.JCheckBoxOperator;
38 import org.netbeans.jemmy.operators.JTextFieldOperator;
39 import org.netbeans.qa.form.VisualDevelopmentUtil;
40 import org.netbeans.qa.form.DeleteDir;
41
42
43 /**
44  * A Test based on JellyTestCase. JellyTestCase redirects Jemmy output
45  * to a log file provided by NbTestCase. It can be inspected in results.
46  * It also sets timeouts necessary for NetBeans GUI testing.
47  *
48  * Any JemmyException (which is normally thrown as a result of an unsuccessful
49  * operation in Jemmy) going from a test is treated by JellyTestCase as a test
50  * failure; any other exception - as a test error.
51  *
52  * Additionally it:
53  * - closes all modal dialogs at the end of the test case (property jemmy.close.modal - default true)
54  * - generates component dump (XML file containing components information) in case of test failure (property jemmy.screen.xmldump - default false)
55  * - captures screen into a PNG file in case of test failure (property jemmy.screen.capture - default true)
56  * - waits at least 1000 ms between test cases (property jelly.wait.no.event - default true)
57  *
58  * @author Jana Maleckova
59  * Created on 29 January 2007, 15:59
60  */

61
62
63
64 public class OpenTempl_defaultPack extends JellyTestCase {
65     public String JavaDoc DATA_PROJECT_NAME = "Sample";
66     public String JavaDoc PACKAGE_NAME = "Source Package";
67     public String JavaDoc PROJECT_NAME = "General";
68     public String JavaDoc workdirpath;
69     
70     
71     MainWindowOperator mainWindow;
72     ProjectsTabOperator pto;
73     ComponentInspectorOperator cio;
74     
75     /** Constructor required by JUnit */
76     public OpenTempl_defaultPack(String JavaDoc name){
77         super(name);
78         
79     }
80     
81     /** Creates suite from particular test cases. You can define order of testcases here. */
82     public static NbTestSuite suite() {
83         NbTestSuite suite = new NbTestSuite();
84         suite.addTest(new OpenTempl_defaultPack("testApplet"));
85         suite.addTest(new OpenTempl_defaultPack("testDialog"));
86         suite.addTest(new OpenTempl_defaultPack("testFrame"));
87         suite.addTest(new OpenTempl_defaultPack("testInternalFrame"));
88         suite.addTest(new OpenTempl_defaultPack("testPanel"));
89         suite.addTest(new OpenTempl_defaultPack("testBean"));
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 DesktopAppTest("test1"));
99
}
100     
101     /** Called before every test case. */
102     public void setUp() throws IOException JavaDoc {
103         workdirpath = getWorkDir().getParentFile().getAbsolutePath();
104         System.out.println("######## "+getName()+" #######");
105     }
106     
107     /** Called after every test case. */
108     public void tearDown() {
109     }
110     
111     // Add test methods here, they have to start with 'test' name.
112

113     //method create new project in parent dir to workdir
114
public void begin() throws InterruptedException JavaDoc{
115         DeleteDir.delDir(workdirpath + System.getProperty("file.separator") + DATA_PROJECT_NAME);
116         Thread.sleep(5000);
117         mainWindow = MainWindowOperator.getDefault();
118         NewProjectWizardOperator npwo = NewProjectWizardOperator.invoke();
119         npwo.selectCategory(PROJECT_NAME);
120         npwo.selectProject("Java Application");
121         npwo.next();
122         
123         NewProjectNameLocationStepOperator tfo_name = new NewProjectNameLocationStepOperator();
124         tfo_name.txtProjectName().setText(DATA_PROJECT_NAME);
125         
126         NewProjectNameLocationStepOperator tfo1_location = new NewProjectNameLocationStepOperator();
127         tfo_name.txtLocation().setText(workdirpath);
128         JButtonOperator bo = new JButtonOperator(npwo, "Finish");
129         //bo.getSource().requestFocus();
130
bo.push();
131         
132         log("Project "+ DATA_PROJECT_NAME + " was created");
133         Thread.sleep(4000);
134         
135     }
136     public void deleteProject() throws InterruptedException JavaDoc {
137         //Project Deleting
138
pto = new ProjectsTabOperator();
139         ProjectRootNode prn = pto.getProjectRootNode(DATA_PROJECT_NAME);
140         prn.select();
141         
142         DeleteAction delProject = new DeleteAction();
143         delProject.perform();
144         
145         NbDialogOperator ndo = new NbDialogOperator("Delete Project");
146         JCheckBoxOperator cbo = new JCheckBoxOperator(ndo);
147         cbo.changeSelection(true);
148         ndo.yes();
149         
150         Thread.sleep(10000);
151         //check if project was really deleted from disc
152
File JavaDoc f = new File JavaDoc(workdirpath + System.getProperty("file.separator") + DATA_PROJECT_NAME);
153         System.out.println("adresar:"+f);
154         if (f.exists()) {
155             log("File " + DATA_PROJECT_NAME + " was not deleted correctly");
156             System.exit(1);
157         } else {
158             log ("File " + DATA_PROJECT_NAME + " was deleted correctly");
159         }
160     }
161     
162     public void openTemplate(String JavaDoc templateName) throws InterruptedException JavaDoc{
163         NewFileWizardOperator nfwo = NewFileWizardOperator.invoke();
164         nfwo.selectProject(DATA_PROJECT_NAME);
165         nfwo.selectCategory("Java GUI Forms");
166         nfwo.selectFileType(templateName);
167         nfwo.next();
168         JComboBoxOperator jcb_package = new JComboBoxOperator(nfwo, 1);
169         jcb_package.clearText();
170         Thread.sleep(2000);
171         
172         if ((templateName == "Bean Form")) {
173             nfwo.next();
174             JTextFieldOperator class_name = new JTextFieldOperator(nfwo);
175             class_name.setText("javax.swing.JButton");
176             nfwo.finish();
177             log (templateName + " is created correctly");
178         } else {
179             nfwo.finish();
180             log (templateName + " is created correctly");
181             Thread.sleep(3000);
182         }
183     }
184     
185     /** Test case 1.
186      *Create new JApplet template in default package
187      */

188     public void testApplet() throws InterruptedException JavaDoc, IOException JavaDoc {
189         
190         begin();
191        
192         openTemplate("JApplet Form");
193          Thread.sleep(10000);
194         testFormFile("NewJApplet");
195         testJavaFile("NewJApplet");
196         
197     }
198     
199     /** Test case 2.
200      * Create new JDialog template in default package
201      */

202     public void testDialog() throws InterruptedException JavaDoc, IOException JavaDoc {
203         
204         openTemplate("JDialog Form");
205         
206         //check if template is generated correctly
207
testFormFile("NewJDialog");
208         testJavaFile("NewJDialog");
209         
210     }
211     
212     /** Test case 3.
213      * Create new JFrame template in default package
214      */

215     public void testFrame() throws InterruptedException JavaDoc, IOException JavaDoc {
216         
217         openTemplate("JFrame Form");
218         //check if template is generated correctly
219
testFormFile("NewJFrame");
220         testJavaFile("NewJFrame");
221     }
222     
223     
224     /** Test case 4.
225      * Create new JInternalFrame template in default package
226      */

227     public void testInternalFrame() throws InterruptedException JavaDoc, IOException JavaDoc {
228         
229         openTemplate("JInternalFrame Form");
230         
231         //check if template is generated correctly
232
testFormFile("NewJInternalFrame");
233         testJavaFile("NewJInternalFrame");
234     }
235     
236     
237     /** Test case 5.
238      * Create new JPanel template in default package
239      */

240     public void testPanel() throws InterruptedException JavaDoc, IOException JavaDoc {
241         
242         openTemplate("JPanel Form");
243         
244         //check if template is generated correctly
245
testFormFile("NewJPanel");
246         testJavaFile("NewJPanel");
247         
248     }
249     
250     /** Test case 6. oa
251      * Create new Bean template in default package
252      */

253     public void testBean() throws InterruptedException JavaDoc, IOException JavaDoc {
254         
255         openTemplate("Bean Form");
256         
257         
258         testFormFile("NewBeanForm");
259         //Bug in generating of new Bean Form template 95403
260
//testJavaFile("NewBeanForm");
261

262         deleteProject();
263     }
264     
265     public void testFormFile(String JavaDoc formfile) throws IOException JavaDoc {
266         try {
267             
268             getRef().print(VisualDevelopmentUtil.readFromFile(
269                     getWorkDir().getParentFile().getAbsolutePath() + File.separatorChar + DATA_PROJECT_NAME + File.separatorChar + "src" + File.separatorChar + formfile + ".form")
270                     );
271             // System.out.println("reffile: " + this.getName()+".ref");
272

273         } catch (Exception JavaDoc e) {
274             fail("Fail during create reffile: " + e.getMessage());
275         }
276         
277         assertFile(new File JavaDoc(getWorkDir() + File.separator + this.getName()+".ref"), getGoldenFile(formfile + "FormFile.pass"), new File JavaDoc(getWorkDir(), formfile+".diff"));
278         
279         
280         
281         //compareReferenceFiles("TestScenario.ref", "testFormFile.pass", null);
282
}
283     
284     public void testJavaFile(String JavaDoc javafile) throws IOException JavaDoc{
285         try {
286             String JavaDoc pokus = VisualDevelopmentUtil.readFromFile(
287                     getWorkDir().getParentFile().getAbsolutePath() + File.separatorChar + DATA_PROJECT_NAME + File.separatorChar + "src" + File.separatorChar + javafile + ".java");
288             int start = pokus.indexOf("/*");
289             int end = pokus.indexOf("*/");
290             pokus = pokus.substring(0, start )+ pokus.substring(end +2);
291             
292             start = pokus.indexOf("/**");
293             end = pokus.indexOf("*/");
294             pokus = pokus.substring(0, start )+ pokus.substring(end +2);
295             getRef().print(pokus);
296             // System.out.println("reffile: " + this.getName()+".ref");
297

298         } catch (Exception JavaDoc e) {
299             fail("Fail during create reffile: " + e.getMessage());
300         }
301         
302         assertFile(new File JavaDoc(getWorkDir() + File.separator + this.getName()+".ref"), getGoldenFile(javafile + "JavaFile.pass"), new File JavaDoc(getWorkDir(), javafile+".diff"));
303         
304         
305     }
306 }
307
Popular Tags