KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > subversion > utils > TestKit


1 /*
2  * TestKit.java
3  *
4  * Created on 10 May 2006, 15:26
5  *
6  * To change this template, choose Tools | Template Manager
7  * and open the template in the editor.
8  */

9
10 package org.netbeans.test.subversion.utils;
11
12 import java.io.BufferedInputStream JavaDoc;
13 import java.io.BufferedOutputStream JavaDoc;
14 import java.io.File JavaDoc;
15 import java.io.FileInputStream JavaDoc;
16 import java.io.FileOutputStream JavaDoc;
17 import java.io.PrintStream JavaDoc;
18 import java.util.Arrays JavaDoc;
19 import javax.swing.JCheckBoxMenuItem JavaDoc;
20 import org.netbeans.jellytools.MainWindowOperator;
21 import org.netbeans.jellytools.NbDialogOperator;
22 import org.netbeans.jellytools.NewFileNameLocationStepOperator;
23 import org.netbeans.jellytools.NewFileWizardOperator;
24 import org.netbeans.jellytools.NewProjectNameLocationStepOperator;
25 import org.netbeans.jellytools.NewProjectWizardOperator;
26 import org.netbeans.jellytools.ProjectsTabOperator;
27 import org.netbeans.jellytools.nodes.Node;
28 import org.netbeans.jemmy.operators.JCheckBoxMenuItemOperator;
29 import org.netbeans.jemmy.operators.JCheckBoxOperator;
30 import org.netbeans.jemmy.operators.JMenuBarOperator;
31 import org.netbeans.jemmy.operators.JMenuItemOperator;
32 import org.netbeans.jemmy.operators.JTextFieldOperator;
33 import org.netbeans.junit.ide.ProjectSupport;
34
35 /**
36  *
37  * @author peter
38  */

39 public final class TestKit {
40     public final static String JavaDoc MODIFIED_COLOR = "#0000FF";
41     public final static String JavaDoc NEW_COLOR = "#008000";
42     public final static String JavaDoc CONFLICT_COLOR = "#FF0000";
43     public final static String JavaDoc IGNORED_COLOR = "#999999";
44     
45     public final static String JavaDoc MODIFIED_STATUS = "[Modified ]";
46     public final static String JavaDoc NEW_STATUS = "[New ]";
47     public final static String JavaDoc CONFLICT_STATUS = "[Conflict ]";
48     public final static String JavaDoc IGNORED_STATUS = "[Ignored ]";
49     public final static String JavaDoc UPTODATE_STATUS = "";
50     
51     public static File JavaDoc prepareProject(String JavaDoc category, String JavaDoc project, String JavaDoc project_name) throws Exception JavaDoc {
52         //create temporary folder for test
53
String JavaDoc folder = "work" + File.separator + "w" + System.currentTimeMillis();
54         File JavaDoc file = new File JavaDoc("/tmp", folder); // NOI18N
55
file.mkdirs();
56         RepositoryMaintenance.deleteFolder(file);
57         file.mkdirs();
58         //PseudoVersioned project
59
NewProjectWizardOperator npwo = NewProjectWizardOperator.invoke();
60         npwo.selectCategory(category);
61         npwo.selectProject(project);
62         npwo.next();
63         NewProjectNameLocationStepOperator npnlso = new NewProjectNameLocationStepOperator();
64         new JTextFieldOperator(npnlso, 1).setText(file.getAbsolutePath()); // NOI18N
65
new JTextFieldOperator(npnlso, 0).setText(project_name); // NOI18N
66
//new JTextFieldOperator(npnlso, 2).setText(folder); // NOI18N
67
new NewProjectWizardOperator().finish();
68         Node rootNode = new ProjectsTabOperator().getProjectRootNode(project_name);
69         // wait classpath scanning finished
70
ProjectSupport.waitScanFinished();
71         return file;
72     }
73     
74     public static String JavaDoc getColor(String JavaDoc nodeHtmlDisplayName) {
75         
76         if (nodeHtmlDisplayName == null || nodeHtmlDisplayName.length() < 1)
77             return "";
78         int hashPos = nodeHtmlDisplayName.indexOf('#');
79         nodeHtmlDisplayName = nodeHtmlDisplayName.substring(hashPos);
80         hashPos = nodeHtmlDisplayName.indexOf('"');
81         nodeHtmlDisplayName = nodeHtmlDisplayName.substring(0, hashPos);
82         return nodeHtmlDisplayName;
83     }
84     
85     public static String JavaDoc getStatus(String JavaDoc nodeHtmlDisplayName) {
86         if (nodeHtmlDisplayName == null || nodeHtmlDisplayName.length() < 1)
87             return "";
88         String JavaDoc status;
89         int pos1 = nodeHtmlDisplayName.indexOf('[');
90         int pos2 = nodeHtmlDisplayName.indexOf(']');
91         if ((pos1 != -1) && (pos2 != -1))
92             status = nodeHtmlDisplayName.substring(pos1, pos2 + 1);
93         else
94             status = "";
95         return status;
96     }
97
98     public static void removeAllData(String JavaDoc projectName) {
99         Node rootNode = new ProjectsTabOperator().getProjectRootNode(projectName);
100         rootNode.performPopupActionNoBlock("Delete Project");
101         NbDialogOperator ndo = new NbDialogOperator("Delete");
102         JCheckBoxOperator cb = new JCheckBoxOperator(ndo, "Also");
103         cb.setSelected(true);
104         ndo.yes();
105         ndo.getTimeouts().setTimeout("ComponentOperator.WaitStateTimeout", 30000);
106         ndo.waitClosed();
107         //TestKit.deleteRecursively(file);
108
}
109     
110     public static void closeProject(String JavaDoc projectName) {
111         try {
112             Node rootNode = new ProjectsTabOperator().getProjectRootNode(projectName);
113             rootNode.performPopupActionNoBlock("Close Project");
114             try {
115                 Thread.sleep(2000);
116             } catch (InterruptedException JavaDoc ex) {
117                 ex.printStackTrace();
118             }
119         }
120         catch (Exception JavaDoc e) {
121             
122         }
123     }
124     
125     public static void finalRemove() throws Exception JavaDoc {
126         closeProject("JavaApp");
127         closeProject("SVNApplication");
128         RepositoryMaintenance.deleteFolder(new File JavaDoc("/tmp/work"));
129         RepositoryMaintenance.deleteFolder(new File JavaDoc("/tmp/repo"));
130     }
131     
132     public static int compareThem(Object JavaDoc[] expected, Object JavaDoc[] actual, boolean sorted) {
133         int result = 0;
134         if (expected == null || actual == null)
135             return -1;
136         if (sorted) {
137             if (expected.length != actual.length) {
138                 return -1;
139             }
140             for (int i = 0; i < expected.length; i++) {
141                 if (((String JavaDoc) expected[i]).equals((String JavaDoc) actual[i])) {
142                     result++;
143                 } else {
144                     return -1;
145                 }
146             }
147         } else {
148             if (expected.length > actual.length) {
149                 return -1;
150             }
151             Arrays.sort(expected);
152             Arrays.sort(actual);
153             boolean found = false;
154             for (int i = 0; i < expected.length; i++) {
155                 if (((String JavaDoc) expected[i]).equals((String JavaDoc) actual[i])) {
156                     result++;
157                 } else {
158                     return -1;
159                 }
160             }
161             return result;
162         }
163         return result;
164     }
165     
166     public static void createNewElements(String JavaDoc projectName, String JavaDoc packageName, String JavaDoc name) {
167         NewFileWizardOperator nfwo = NewFileWizardOperator.invoke();
168         nfwo.selectProject(projectName);
169         nfwo.selectCategory("Java Classes");
170         nfwo.selectFileType("Java Package");
171         nfwo.next();
172         NewFileNameLocationStepOperator nfnlso = new NewFileNameLocationStepOperator();
173         nfnlso.txtObjectName().clearText();
174         nfnlso.txtObjectName().typeText(packageName);
175         nfnlso.finish();
176         
177         nfwo = NewFileWizardOperator.invoke();
178         nfwo.selectProject(projectName);
179         nfwo.selectCategory("Java Classes");
180         nfwo.selectFileType("Java Class");
181         nfwo.next();
182         nfnlso = new NewFileNameLocationStepOperator();
183         nfnlso.txtObjectName().clearText();
184         nfnlso.txtObjectName().typeText(name);
185         nfnlso.selectPackage(packageName);
186         nfnlso.finish();
187     }
188     
189     public static void createNewPackage(String JavaDoc projectName, String JavaDoc packageName) {
190         NewFileWizardOperator nfwo = NewFileWizardOperator.invoke();
191         nfwo.selectProject(projectName);
192         nfwo.selectCategory("Java Classes");
193         nfwo.selectFileType("Java Package");
194         nfwo.next();
195         NewFileNameLocationStepOperator nfnlso = new NewFileNameLocationStepOperator();
196         nfnlso.txtObjectName().clearText();
197         nfnlso.txtObjectName().typeText(packageName);
198         nfnlso.finish();
199     }
200     
201     public static void createNewElement(String JavaDoc projectName, String JavaDoc packageName, String JavaDoc name) {
202         NewFileWizardOperator nfwo = NewFileWizardOperator.invoke();
203         nfwo.selectProject(projectName);
204         nfwo.selectCategory("Java Classes");
205         nfwo.selectFileType("Java Class");
206         nfwo.next();
207         NewFileNameLocationStepOperator nfnlso = new NewFileNameLocationStepOperator();
208         nfnlso.txtObjectName().clearText();
209         nfnlso.txtObjectName().typeText(name);
210         nfnlso.selectPackage(packageName);
211         nfnlso.finish();
212     }
213     
214     public static void copyTo(String JavaDoc source, String JavaDoc destination) {
215         try {
216             BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(source));
217             BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(destination));
218             boolean available = true;
219             byte[] buffer = new byte[1024];
220             int size;
221             try {
222                 while (available) {
223                     size = bis.read(buffer);
224                     if (size != -1) {
225                         bos.write(buffer, 0, size);
226                     } else {
227                         available = false;
228                     }
229                 }
230             } catch (Exception JavaDoc e) {
231                 e.printStackTrace();
232             } finally {
233                 bos.flush();
234                 bos.close();
235                 bis.close();
236             }
237         } catch (Exception JavaDoc e) {
238             e.printStackTrace();
239         }
240     }
241     
242     public static void printLogStream(PrintStream JavaDoc stream, String JavaDoc message) {
243         if (stream != null) {
244             stream.println(message);
245         }
246     }
247     
248     public static void showStatusLabels() {
249         JMenuBarOperator mbo = new JMenuBarOperator(MainWindowOperator.getDefault().getJMenuBar());
250         JMenuItemOperator mo = mbo.showMenuItem("View|Show Subversion Status Labels");
251         JCheckBoxMenuItemOperator cbmio = new JCheckBoxMenuItemOperator((JCheckBoxMenuItem JavaDoc) mo.getSource());
252         if (!cbmio.getState())
253             cbmio.push();
254     }
255 }
256
Popular Tags