KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > localhistory > utils > 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 /*
20  * TestKit.java
21  *
22  * Created on 10 May 2006, 15:26
23  *
24  * To change this template, choose Tools | Template Manager
25  * and open the template in the editor.
26  */

27
28 package org.netbeans.test.localhistory.utils;
29
30
31 import java.io.BufferedInputStream JavaDoc;
32 import java.io.BufferedOutputStream JavaDoc;
33 import java.io.File JavaDoc;
34 import java.io.FileInputStream JavaDoc;
35 import java.io.FileOutputStream JavaDoc;
36 import java.io.PrintStream JavaDoc;
37 import java.util.Arrays JavaDoc;
38 import org.netbeans.jellytools.NbDialogOperator;
39 import org.netbeans.jellytools.NewFileNameLocationStepOperator;
40 import org.netbeans.jellytools.NewFileWizardOperator;
41 import org.netbeans.jellytools.NewProjectNameLocationStepOperator;
42 import org.netbeans.jellytools.NewProjectWizardOperator;
43 import org.netbeans.jellytools.ProjectsTabOperator;
44 import org.netbeans.jellytools.nodes.Node;
45 import org.netbeans.jemmy.operators.JCheckBoxOperator;
46 import org.netbeans.jemmy.operators.JTextFieldOperator;
47 import org.netbeans.junit.ide.ProjectSupport;
48
49 /**
50  *
51  * @author peter
52  */

53 public final class TestKit {
54         
55     public static File JavaDoc prepareProject(String JavaDoc category, String JavaDoc project, String JavaDoc project_name) throws Exception JavaDoc {
56         //create temporary folder for test
57
String JavaDoc folder = "work" + File.separator + "w" + System.currentTimeMillis();
58         File JavaDoc file = new File JavaDoc("/tmp", folder); // NOI18N
59
file.mkdirs();
60         file.mkdirs();
61         //PseudoVersioned project
62
NewProjectWizardOperator npwo = NewProjectWizardOperator.invoke();
63         npwo.selectCategory(category);
64         npwo.selectProject(project);
65         npwo.next();
66         NewProjectNameLocationStepOperator npnlso = new NewProjectNameLocationStepOperator();
67         new JTextFieldOperator(npnlso, 1).setText(file.getAbsolutePath()); // NOI18N
68
new JTextFieldOperator(npnlso, 0).setText(project_name); // NOI18N
69
//new JTextFieldOperator(npnlso, 2).setText(folder); // NOI18N
70
new NewProjectWizardOperator().finish();
71         Node rootNode = new ProjectsTabOperator().getProjectRootNode(project_name);
72         // wait classpath scanning finished
73
ProjectSupport.waitScanFinished();
74         return file;
75     }
76
77     public static void removeAllData(String JavaDoc projectName) {
78         Node rootNode = new ProjectsTabOperator().getProjectRootNode(projectName);
79         rootNode.performPopupActionNoBlock("Delete Project");
80         NbDialogOperator ndo = new NbDialogOperator("Delete");
81         JCheckBoxOperator cb = new JCheckBoxOperator(ndo, "Also");
82         cb.setSelected(true);
83         ndo.yes();
84         ndo.getTimeouts().setTimeout("ComponentOperator.WaitStateTimeout", 30000);
85         ndo.waitClosed();
86         //TestKit.deleteRecursively(file);
87
}
88     
89     public static void closeProject(String JavaDoc projectName) {
90         try {
91             Node rootNode = new ProjectsTabOperator().getProjectRootNode(projectName);
92             rootNode.performPopupActionNoBlock("Close Project");
93             try {
94                 Thread.sleep(2000);
95             } catch (InterruptedException JavaDoc ex) {
96                 ex.printStackTrace();
97             }
98         }
99         catch (Exception JavaDoc e) {
100             
101         }
102     }
103     
104     public static int compareThem(Object JavaDoc[] expected, Object JavaDoc[] actual, boolean sorted) {
105         int result = 0;
106         if (expected == null || actual == null)
107             return -1;
108         if (sorted) {
109             if (expected.length != actual.length) {
110                 return -1;
111             }
112             for (int i = 0; i < expected.length; i++) {
113                 if (((String JavaDoc) expected[i]).equals((String JavaDoc) actual[i])) {
114                     result++;
115                 } else {
116                     return -1;
117                 }
118             }
119         } else {
120             if (expected.length > actual.length) {
121                 return -1;
122             }
123             Arrays.sort(expected);
124             Arrays.sort(actual);
125             boolean found = false;
126             for (int i = 0; i < expected.length; i++) {
127                 if (((String JavaDoc) expected[i]).equals((String JavaDoc) actual[i])) {
128                     result++;
129                 } else {
130                     return -1;
131                 }
132             }
133             return result;
134         }
135         return result;
136     }
137     
138     public static void createNewPackage(String JavaDoc projectName, String JavaDoc packageName) {
139         NewFileWizardOperator nfwo = NewFileWizardOperator.invoke();
140         nfwo.selectProject(projectName);
141         nfwo.selectCategory("Java Classes");
142         nfwo.selectFileType("Java Package");
143         nfwo.next();
144         NewFileNameLocationStepOperator nfnlso = new NewFileNameLocationStepOperator();
145         nfnlso.txtObjectName().clearText();
146         nfnlso.txtObjectName().typeText(packageName);
147         nfnlso.finish();
148     }
149     
150     public static void createNewElement(String JavaDoc projectName, String JavaDoc packageName, String JavaDoc name) {
151         NewFileWizardOperator nfwo = NewFileWizardOperator.invoke();
152         nfwo.selectProject(projectName);
153         nfwo.selectCategory("Java Classes");
154         nfwo.selectFileType("Java Class");
155         nfwo.next();
156         NewFileNameLocationStepOperator nfnlso = new NewFileNameLocationStepOperator();
157         nfnlso.txtObjectName().clearText();
158         nfnlso.txtObjectName().typeText(name);
159         nfnlso.selectPackage(packageName);
160         nfnlso.finish();
161     }
162     
163     public static void copyTo(String JavaDoc source, String JavaDoc destination) {
164         try {
165             BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(source));
166             BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(destination));
167             boolean available = true;
168             byte[] buffer = new byte[1024];
169             int size;
170             try {
171                 while (available) {
172                     size = bis.read(buffer);
173                     if (size != -1) {
174                         bos.write(buffer, 0, size);
175                     } else {
176                         available = false;
177                     }
178                 }
179             } catch (Exception JavaDoc e) {
180                 e.printStackTrace();
181             } finally {
182                 bos.flush();
183                 bos.close();
184                 bis.close();
185             }
186         } catch (Exception JavaDoc e) {
187             e.printStackTrace();
188         }
189     }
190     
191     public static void printLogStream(PrintStream JavaDoc stream, String JavaDoc message) {
192         if (stream != null) {
193             stream.println(message);
194         }
195     }
196     
197 }
198
Popular Tags