KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > cvsmodule > TestKit


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 package org.netbeans.test.cvsmodule;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.FileReader JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import javax.swing.JCheckBoxMenuItem JavaDoc;
31 import org.netbeans.jellytools.EditorOperator;
32 import org.netbeans.jellytools.MainWindowOperator;
33 import org.netbeans.jellytools.NbDialogOperator;
34 import org.netbeans.jellytools.NewFileNameLocationStepOperator;
35 import org.netbeans.jellytools.NewFileWizardOperator;
36 import org.netbeans.jellytools.NewProjectNameLocationStepOperator;
37 import org.netbeans.jellytools.NewProjectWizardOperator;
38 import org.netbeans.jellytools.ProjectsTabOperator;
39 import org.netbeans.jellytools.nodes.Node;
40 import org.netbeans.jemmy.JemmyProperties;
41 import org.netbeans.jemmy.operators.JCheckBoxMenuItemOperator;
42 import org.netbeans.jemmy.operators.JCheckBoxOperator;
43 import org.netbeans.jemmy.operators.JMenuBarOperator;
44 import org.netbeans.jemmy.operators.JMenuItemOperator;
45 import org.netbeans.jemmy.operators.JTextFieldOperator;
46
47 /**
48  * Set of utility methods.
49  *
50  * @author Peter Pis
51  */

52 public class TestKit {
53     final static String JavaDoc MODIFIED_COLOR = "#0000FF";
54     final static String JavaDoc NEW_COLOR = "#008000";
55     final static String JavaDoc CONFLICT_COLOR = "#FF0000";
56     final static String JavaDoc IGNORED_COLOR = "#999999";
57     
58     public static File JavaDoc createTmpFolder(String JavaDoc prefix) throws IOException JavaDoc {
59         String JavaDoc tmpDir = System.getProperty("java.io.tmpdir"); // NOI18N
60
File JavaDoc tmpFolder = new File JavaDoc(tmpDir);
61         // generate unique name for tmp folder
62
File JavaDoc tmp = File.createTempFile(prefix, "", tmpFolder); // NOI18N
63
if (tmp.delete() == false) {
64             throw new IOException JavaDoc("Can not delete " + tmp);
65         };
66         if (tmp.mkdirs() == false) {
67             throw new IOException JavaDoc("Can not create " + tmp);
68         };
69         return tmp;
70     }
71     
72     public static void deleteRecursively(File JavaDoc dir) {
73         if (dir.isDirectory()) {
74             String JavaDoc[] files = dir.list();
75             for (int i = 0; i < files.length; i++) {
76                 deleteRecursively(new File JavaDoc(dir, files[i])); // RECURSION
77
}
78         }
79         dir.delete();
80     }
81     
82     public static void pseudoVersion(File JavaDoc file) throws Exception JavaDoc {
83         File JavaDoc CVSdir = new File JavaDoc(file, "CVS");
84         File JavaDoc Entries = new File JavaDoc(CVSdir, "Entries");
85         OutputStream JavaDoc out = new FileOutputStream JavaDoc(Entries);
86         String JavaDoc data = "D\n" +
87                 "/Main.java/1.1/Wed Sep 14 08:51:41 2005//";
88         out.write(data.getBytes("utf8"));
89         out.flush();
90         out.close();
91         //Root
92
File JavaDoc Root = new File JavaDoc(CVSdir, "Root");
93         out = new FileOutputStream JavaDoc(Root);
94         data = ":local:/Projects/CVSrepo";
95         out.write(data.getBytes("utf8"));
96         out.flush();
97         out.close();
98         //Repository
99
File JavaDoc Repository = new File JavaDoc(CVSdir, "Repository");
100         out = new FileOutputStream JavaDoc(Repository);
101         data = "ForImport/src/forimport";
102         out.write(data.getBytes("utf8"));
103         out.flush();
104         out.close();
105     }
106     
107     public static File JavaDoc prepareProject(String JavaDoc category, String JavaDoc project, String JavaDoc project_name, String JavaDoc edit_file) throws Exception JavaDoc {
108         //create temporary folder for test
109
String JavaDoc folder = "" + System.currentTimeMillis();
110         File JavaDoc file = new File JavaDoc("/tmp", folder); // NOI18N
111
file.mkdirs();
112         //PseudoVersioned project
113
NewProjectWizardOperator npwo = NewProjectWizardOperator.invoke();
114         npwo.selectCategory(category);
115         npwo.selectProject(project);
116         npwo.next();
117         NewProjectNameLocationStepOperator npnlso = new NewProjectNameLocationStepOperator();
118         new JTextFieldOperator(npnlso, 1).setText(file.getAbsolutePath()); // NOI18N
119
new JTextFieldOperator(npnlso, 0).setText(project_name); // NOI18N
120
//new JTextFieldOperator(npnlso, 2).setText(folder); // NOI18N
121
new NewProjectWizardOperator().finish();
122         Node rootNode = new ProjectsTabOperator().getProjectRootNode(project_name);
123         // wait classpath scanning finished
124
return file;
125     }
126     
127     public static void removeAllData(String JavaDoc project_name) {
128         Node rootNode = new ProjectsTabOperator().getProjectRootNode(project_name);
129         rootNode.performPopupActionNoBlock("Delete Project");
130         NbDialogOperator ndo = new NbDialogOperator("Delete");
131         JCheckBoxOperator cb = new JCheckBoxOperator(ndo, "Also");
132         cb.setSelected(true);
133         ndo.yes();
134         ndo.getTimeouts().setTimeout("ComponentOperator.WaitStateTimeout", 30000);
135         ndo.waitClosed();
136         //TestKit.deleteRecursively(file);
137
}
138     
139     public static void unversionProject(File JavaDoc file, String JavaDoc project_name) throws Exception JavaDoc {
140         File JavaDoc folder_CVS = new File JavaDoc(file, project_name + File.separator + "src" + File.separator + project_name.toLowerCase() + File.separator + "CVS");
141         //System.out.println("File: " + file);
142
TestKit.deleteRecursively(folder_CVS);
143         //System.out.println("File: " + file.getAbsolutePath());
144
EditorOperator eo = new EditorOperator("Main.java");
145         eo.insert("//Comment\n");
146         eo.save();
147     }
148     
149     public static void createNewElements(String JavaDoc projectName) {
150         String JavaDoc pack = "xx";
151         
152         NewFileWizardOperator nfwo = NewFileWizardOperator.invoke();
153         nfwo.selectProject(projectName);
154         nfwo.selectCategory("Java Classes");
155         nfwo.selectFileType("Java Package");
156         nfwo.next();
157         NewFileNameLocationStepOperator nfnlso = new NewFileNameLocationStepOperator();
158         nfnlso.txtObjectName().clearText();
159         nfnlso.txtObjectName().typeText(pack);
160         nfnlso.finish();
161         //
162
nfwo = NewFileWizardOperator.invoke();
163         nfwo.selectProject(projectName);
164         nfwo.selectCategory("Java Classes");
165         nfwo.selectFileType("Java Class");
166         nfwo.next();
167         nfnlso = new NewFileNameLocationStepOperator();
168         nfnlso.txtObjectName().clearText();
169         nfnlso.txtObjectName().typeText("NewClass");
170         nfnlso.selectPackage(pack);
171         nfnlso.finish();
172         //
173
nfwo = NewFileWizardOperator.invoke();
174         nfwo.selectProject(projectName);
175         nfwo.selectCategory("Java GUI Forms");
176         nfwo.selectFileType("JFrame Form");
177         nfwo.next();
178         nfnlso = new NewFileNameLocationStepOperator();
179         nfnlso.txtObjectName().clearText();
180         nfnlso.txtObjectName().typeText("NewJFrame");
181         nfnlso.selectPackage(pack);
182         nfnlso.finish();
183     }
184     
185     public static void createNewElementsCommitCvs11(String JavaDoc projectName) {
186         String JavaDoc pack = "xx";
187         
188         NewFileWizardOperator nfwo = NewFileWizardOperator.invoke();
189         nfwo.selectProject(projectName);
190         nfwo.selectCategory("Java Classes");
191         nfwo.selectFileType("Java Package");
192         nfwo.next();
193         NewFileNameLocationStepOperator nfnlso = new NewFileNameLocationStepOperator();
194         nfnlso.txtObjectName().clearText();
195         nfnlso.txtObjectName().typeText(pack);
196         nfnlso.finish();
197         //
198
nfwo = NewFileWizardOperator.invoke();
199         nfwo.selectProject(projectName);
200         nfwo.selectCategory("Java Classes");
201         nfwo.selectFileType("Java Class");
202         nfwo.next();
203         nfnlso = new NewFileNameLocationStepOperator();
204         nfnlso.txtObjectName().clearText();
205         nfnlso.txtObjectName().typeText("NewClass");
206         nfnlso.selectPackage(pack);
207         nfnlso.finish();
208         //
209
nfwo = NewFileWizardOperator.invoke();
210         nfwo.selectProject(projectName);
211         nfwo.selectCategory("Java Classes");
212         nfwo.selectFileType("Java Class");
213         nfwo.next();
214         nfnlso = new NewFileNameLocationStepOperator();
215         nfnlso.txtObjectName().clearText();
216         nfnlso.txtObjectName().typeText("NewClass2");
217         nfnlso.selectPackage(pack);
218         nfnlso.finish();
219         //
220
nfwo = NewFileWizardOperator.invoke();
221         nfwo.selectProject(projectName);
222         nfwo.selectCategory("Java Classes");
223         nfwo.selectFileType("Java Class");
224         nfwo.next();
225         nfnlso = new NewFileNameLocationStepOperator();
226         nfnlso.txtObjectName().clearText();
227         nfnlso.txtObjectName().typeText("NewClass3");
228         nfnlso.selectPackage(pack);
229         nfnlso.finish();
230         //
231
nfwo = NewFileWizardOperator.invoke();
232         nfwo.selectProject(projectName);
233         nfwo.selectCategory("Java Classes");
234         nfwo.selectFileType("Java Class");
235         nfwo.next();
236         nfnlso = new NewFileNameLocationStepOperator();
237         nfnlso.txtObjectName().clearText();
238         nfnlso.txtObjectName().typeText("NewClass4");
239         nfnlso.selectPackage(pack);
240         nfnlso.finish();
241     }
242     
243     public static void createNewElementsCommitCvs12(String JavaDoc projectName) {
244         String JavaDoc pack = "aa";
245         
246         NewFileWizardOperator nfwo = NewFileWizardOperator.invoke();
247         nfwo.selectProject(projectName);
248         nfwo.selectCategory("Java Classes");
249         nfwo.selectFileType("Java Package");
250         nfwo.next();
251         NewFileNameLocationStepOperator nfnlso = new NewFileNameLocationStepOperator();
252         nfnlso.txtObjectName().clearText();
253         nfnlso.txtObjectName().typeText("aa");
254         nfnlso.finish();
255         
256         //
257
nfwo = NewFileWizardOperator.invoke();
258         nfwo.selectProject(projectName);
259         nfwo.selectCategory("Java Classes");
260         nfwo.selectFileType("Java Package");
261         nfwo.next();
262         nfnlso = new NewFileNameLocationStepOperator();
263         nfnlso.txtObjectName().clearText();
264         nfnlso.txtObjectName().typeText("bb");
265         nfnlso.finish();
266         //
267
nfwo = NewFileWizardOperator.invoke();
268         nfwo.selectProject(projectName);
269         nfwo.selectCategory("Java Classes");
270         nfwo.selectFileType("Java Class");
271         nfwo.next();
272         nfnlso = new NewFileNameLocationStepOperator();
273         nfnlso.txtObjectName().clearText();
274         nfnlso.txtObjectName().typeText("NewClass");
275         nfnlso.selectPackage("aa");
276         nfnlso.finish();
277         
278         //
279
nfwo = NewFileWizardOperator.invoke();
280         nfwo.selectProject(projectName);
281         nfwo.selectCategory("Java Classes");
282         nfwo.selectFileType("Java Class");
283         nfwo.next();
284         nfnlso = new NewFileNameLocationStepOperator();
285         nfnlso.txtObjectName().clearText();
286         nfnlso.txtObjectName().typeText("NewClass2");
287         nfnlso.selectPackage("aa");
288         nfnlso.finish();
289         //
290
nfwo = NewFileWizardOperator.invoke();
291         nfwo.selectProject(projectName);
292         nfwo.selectCategory("Java Classes");
293         nfwo.selectFileType("Java Class");
294         nfwo.next();
295         nfnlso = new NewFileNameLocationStepOperator();
296         nfnlso.txtObjectName().clearText();
297         nfnlso.txtObjectName().typeText("NewClass");
298         nfnlso.selectPackage("bb");
299         nfnlso.finish();
300         //
301
nfwo = NewFileWizardOperator.invoke();
302         nfwo.selectProject(projectName);
303         nfwo.selectCategory("Java Classes");
304         nfwo.selectFileType("Java Class");
305         nfwo.next();
306         nfnlso = new NewFileNameLocationStepOperator();
307         nfnlso.txtObjectName().clearText();
308         nfnlso.txtObjectName().typeText("NewClass2");
309         nfnlso.selectPackage("bb");
310         nfnlso.finish();
311     }
312     
313     public static int compareThem(Object JavaDoc[] expected, Object JavaDoc[] actual, boolean sorted) {
314         int result = 0;
315         if (expected == null || actual == null)
316             return -1;
317         if (sorted) {
318             if (expected.length != actual.length) {
319                 return -1;
320             }
321             for (int i = 0; i < expected.length; i++) {
322                 if (((String JavaDoc) expected[i]).equals((String JavaDoc) actual[i])) {
323                     result++;
324                 } else {
325                     return -1;
326                 }
327             }
328         } else {
329             if (expected.length > actual.length) {
330                 return -1;
331             }
332             Arrays.sort(expected);
333             Arrays.sort(actual);
334             for (int i = 0; i < expected.length; i++) {
335                 if (((String JavaDoc) expected[i]).equals((String JavaDoc) actual[i])) {
336                     result++;
337                 } else {
338                     return -1;
339                 }
340             }
341         }
342         return result;
343     }
344     
345     public static String JavaDoc getCVSroot(File JavaDoc cvsFolder) {
346         String JavaDoc root = new String JavaDoc();
347         if (cvsFolder.isDirectory()) {
348             try {
349                 BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(cvsFolder + File.separator + "Root"));
350                 root = br.readLine();
351                 if (root == null || root.length() == 0)
352                     return "";
353                 br.close();
354             } catch (IOException JavaDoc e) {
355                 return "";
356             }
357         }
358         return root;
359     }
360     
361     public static String JavaDoc getColor(String JavaDoc nodeHtmlDisplayName) {
362         
363         if (nodeHtmlDisplayName == null || nodeHtmlDisplayName.length() < 1)
364             return "";
365         int hashPos = nodeHtmlDisplayName.indexOf('#');
366         nodeHtmlDisplayName = nodeHtmlDisplayName.substring(hashPos);
367         hashPos = nodeHtmlDisplayName.indexOf('"');
368         nodeHtmlDisplayName = nodeHtmlDisplayName.substring(0, hashPos);
369         return nodeHtmlDisplayName;
370     }
371     
372     public static InputStream JavaDoc getStream(String JavaDoc dir, String JavaDoc protocolName) throws Exception JavaDoc {
373         File JavaDoc file = new File JavaDoc(dir, protocolName);
374         InputStream JavaDoc in = new FileInputStream JavaDoc(file);
375         return in;
376     }
377     
378     public static void closeProject(String JavaDoc projectName) {
379         try {
380             Node rootNode = new ProjectsTabOperator().getProjectRootNode(projectName);
381             rootNode.performPopupActionNoBlock("Close Project");
382             try {
383                 Thread.sleep(2000);
384             } catch (InterruptedException JavaDoc ex) {
385                 ex.printStackTrace();
386             }
387         } catch (Exception JavaDoc e) {
388             
389         }
390     }
391     public static String JavaDoc getStatus(String JavaDoc nodeHtmlDisplayName) {
392         if (nodeHtmlDisplayName == null || nodeHtmlDisplayName.length() < 1)
393             return "";
394         String JavaDoc status;
395         int pos1 = nodeHtmlDisplayName.indexOf('[');
396         int pos2 = nodeHtmlDisplayName.indexOf(']');
397         if ((pos1 != -1) && (pos2 != -1))
398             status = nodeHtmlDisplayName.substring(pos1, pos2 + 1);
399         else
400             status = "";
401         return status;
402     }
403     
404     public static void showStatusLabels() {
405         JMenuBarOperator mbo = new JMenuBarOperator(MainWindowOperator.getDefault().getJMenuBar());
406         JMenuItemOperator mo = mbo.showMenuItem("View|Show CVS Status Labels");
407         JCheckBoxMenuItemOperator cbmio = new JCheckBoxMenuItemOperator((JCheckBoxMenuItem JavaDoc) mo.getSource());
408         if (!cbmio.getState())
409             cbmio.push();
410     }
411 }
412
Popular Tags