KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > j2ee > lib > Utils


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.test.j2ee.lib;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStreamReader JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.nio.channels.FileChannel JavaDoc;
31 import java.text.SimpleDateFormat JavaDoc;
32 import java.util.Date JavaDoc;
33 import java.util.Locale JavaDoc;
34 import junit.framework.AssertionFailedError;
35 import org.netbeans.jellytools.Bundle;
36 import org.netbeans.jellytools.EditorOperator;
37 import org.netbeans.jellytools.EditorWindowOperator;
38 import org.netbeans.jellytools.MainWindowOperator;
39 import org.netbeans.jellytools.NbDialogOperator;
40 import org.netbeans.jellytools.OutputTabOperator;
41 import org.netbeans.jellytools.ProjectsTabOperator;
42 import org.netbeans.jellytools.RuntimeTabOperator;
43 import org.netbeans.jellytools.actions.Action;
44 import org.netbeans.jellytools.actions.ActionNoBlock;
45 import org.netbeans.jellytools.nodes.Node;
46 import org.netbeans.jellytools.nodes.ProjectRootNode;
47 import org.netbeans.jemmy.EventTool;
48 import org.netbeans.jemmy.TimeoutExpiredException;
49 import org.netbeans.jemmy.operators.JButtonOperator;
50 import org.netbeans.jemmy.operators.JComboBoxOperator;
51 import org.netbeans.jemmy.operators.JLabelOperator;
52 import org.netbeans.jemmy.operators.JTabbedPaneOperator;
53 import org.netbeans.jemmy.operators.JTextFieldOperator;
54 import org.netbeans.jemmy.operators.JTreeOperator;
55 import org.netbeans.jemmy.operators.Operator;
56 import org.netbeans.junit.NbTestCase;
57
58 /**
59  *
60  * @author lm97939
61  */

62 public class Utils {
63     
64     private NbTestCase nbtestcase;
65     private static final String JavaDoc LIBRARY_BUNDLE = "org.netbeans.modules.project.libraries.ui.Bundle";
66     
67     public Utils(NbTestCase nbtestcase) {
68         this.nbtestcase = nbtestcase;
69     }
70     
71     public static String JavaDoc getTimeIndex() {
72         return new SimpleDateFormat JavaDoc("HHmmssS",Locale.US).format(new Date JavaDoc());
73     }
74     
75     public static void addAppServer() {
76         String JavaDoc path = System.getProperty("j2ee.appserver.path");
77         if (path == null) {
78             throw new RuntimeException JavaDoc("Cannot setup appserver, property j2ee.appserver.path is not set.");
79         }
80         String JavaDoc username = System.getProperty("j2ee.appserver.username","admin");
81         String JavaDoc password = System.getProperty("j2ee.appserver.password","adminadmin");
82         
83         Node node = new Node(new RuntimeTabOperator().getRootNode(),Bundle.getStringTrimmed("org.netbeans.modules.j2ee.deployment.impl.ui.Bundle", "SERVER_REGISTRY_NODE"));
84         node.performPopupActionNoBlock(Bundle.getStringTrimmed("org.netbeans.modules.j2ee.deployment.impl.ui.actions.Bundle", "LBL_Add_Server_Instance"));
85         NbDialogOperator dialog = new NbDialogOperator(Bundle.getStringTrimmed("org.netbeans.modules.j2ee.deployment.impl.ui.wizard.Bundle", "LBL_ASIW_Title"));
86         new JComboBoxOperator(dialog).selectItem("Sun Java System Application Server");
87         new JButtonOperator(dialog,Bundle.getStringTrimmed("org.openide.Bundle", "CTL_NEXT")).push();
88         
89         //"Enter the Application Server location" or "Define Application Server Instance Properties"
90
if (new JLabelOperator(dialog,1).getText().equalsIgnoreCase(Bundle.getStringTrimmed("org.netbeans.modules.j2ee.sun.ide.j2ee.ui.Bundle", "TITLE_AddServerLocationPanel"))) {
91             new JTextFieldOperator(dialog).setText("");
92             new JTextFieldOperator(dialog).typeText(path);
93             new JButtonOperator(dialog,Bundle.getStringTrimmed("org.openide.Bundle", "CTL_NEXT")).push();
94         }
95         new JTextFieldOperator(dialog,0).setText("");
96         new JTextFieldOperator(dialog,1).setText("");
97         new JTextFieldOperator(dialog,0).typeText(username);
98         new JTextFieldOperator(dialog,1).typeText(password);
99         new JButtonOperator(dialog,Bundle.getStringTrimmed("org.openide.Bundle", "CTL_FINISH")).push();
100         new ProjectsTabOperator();
101     }
102     
103     /**
104      * Starts or Stops AppServer
105      * @param start if true, starts appserver, if false stops appserver.
106      */

107     public static void startStopServer(boolean start) {
108         RuntimeTabOperator runtimeTab = RuntimeTabOperator.invoke();
109         Node serverNode = new Node(runtimeTab.getRootNode(), Bundle.getStringTrimmed("org.netbeans.modules.j2ee.deployment.impl.ui.Bundle", "SERVER_REGISTRY_NODE")
110         +"|Application Server");
111         new org.netbeans.jemmy.EventTool().waitNoEvent(10000);
112         if (start)
113             serverNode.performPopupAction("Start");
114         else
115             serverNode.performPopupAction("Stop");
116         new org.netbeans.jemmy.EventTool().waitNoEvent(5000);
117         ProgressSupport.waitFinished((start?"Starting":"Stopping") + " Sun Java System Application Server", 500000);
118         new org.netbeans.jemmy.EventTool().waitNoEvent(2000);
119     }
120     
121     public static void prepareDatabase() {
122                 
123         new Action("Tools|"+Bundle.getStringTrimmed("org.netbeans.modules.derby.Bundle", "LBL_DerbyDatabase")+
124                 "|"+Bundle.getStringTrimmed("org.netbeans.modules.derby.Bundle", "LBL_StartAction"), null).performMenu();
125         OutputTabOperator outputOper = new OutputTabOperator(Bundle.getStringTrimmed("org.netbeans.modules.derby.Bundle", "LBL_outputtab"));
126         outputOper.getTimeouts().setTimeout("ComponentOperator.WaitStateTimeout", 120000);
127         outputOper.waitText("Server is ready to accept connections on port 1527.");
128         new Node(new RuntimeTabOperator().getRootNode(), "Databases|/sample").performPopupActionNoBlock("Connect");
129         try {
130             NbDialogOperator dialog = new NbDialogOperator("Connect");
131             new JTextFieldOperator(dialog,0).typeText("app");
132             dialog.ok();
133         } catch (TimeoutExpiredException e) {}
134     }
135     
136     public void assertFiles(File JavaDoc dir, String JavaDoc fileNames[], String JavaDoc goldenFilePrefix) throws IOException JavaDoc {
137         AssertionFailedError firstExc = null;
138         for (int i=0; i<fileNames.length; i++) {
139             File JavaDoc file = new File JavaDoc(dir, fileNames[i]);
140             try {
141                 File JavaDoc goldenFile = nbtestcase.getGoldenFile(goldenFilePrefix + fileNames[i]);
142                 nbtestcase.assertFile("File "+file.getAbsolutePath()+" is different than golden file "+goldenFile.getAbsolutePath()+".",
143                         file,
144                         goldenFile,
145                         new File JavaDoc(nbtestcase.getWorkDir(), fileNames[i]+".diff"),
146                         new TrimmingLineDiff());
147             } catch (AssertionFailedError e) {
148                 if (firstExc == null){
149                     firstExc = e;
150                 }
151                 File JavaDoc copy = new File JavaDoc(nbtestcase.getWorkDirPath(), goldenFilePrefix+fileNames[i]);
152                 copyFile(file,copy);
153             }
154         }
155         if (firstExc != null)
156             throw firstExc;
157     }
158     
159     /**
160      * Copy file in to out
161      * @param in File
162      * @param out File
163      * @throws Exception
164      */

165     public static void copyFile(File JavaDoc in, File JavaDoc out) {
166         try {
167             out.createNewFile();
168             FileChannel JavaDoc srcChannel = new FileInputStream JavaDoc(in).getChannel();
169             FileChannel JavaDoc dstChannel = new FileOutputStream JavaDoc(out).getChannel();
170             dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
171             srcChannel.close();
172             dstChannel.close();
173         } catch (IOException JavaDoc ioe) {
174             ioe.printStackTrace(System.err);
175         }
176     }
177     
178     /**
179      * Loads page specified by URL
180      * @param url_string URL
181      * @throws java.io.IOException
182      * @return downloaded page
183      */

184     public static String JavaDoc loadFromURL(String JavaDoc url_string) throws IOException JavaDoc {
185         URL JavaDoc url = new URL JavaDoc(url_string);
186         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(url.openStream()));
187         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
188         String JavaDoc line;
189         while ((line = reader.readLine()) != null) {
190             if (sb.length() > 0)
191                 sb.append("\n");
192             sb.append(line);
193         }
194         reader.close();
195         return sb.toString();
196     }
197     
198     /**
199      * Deploys Application
200      *
201      * @return downloaded page, null if url parameter was null
202      * @param projectName Name of Project to deploy
203      * @param url URL of page that should be downloaded, can be null.
204      * @throws java.io.IOException
205      */

206     public static String JavaDoc deploy(String JavaDoc projectName, String JavaDoc url, boolean projectNameInStatus) throws IOException JavaDoc {
207         JTreeOperator tree = ProjectsTabOperator.invoke().tree();
208         tree.setComparator(new Operator.DefaultStringComparator(true, true));
209         Node node = new ProjectRootNode(tree, projectName);
210         node.performPopupAction(Bundle.getStringTrimmed("org.netbeans.modules.j2ee.earproject.ui.Bundle", "LBL_DeployAction_Name"));
211         MainWindowOperator.getDefault().getTimeouts().setTimeout("Waiter.WaitingTime", 600000);
212         MainWindowOperator.getDefault().waitStatusText(Bundle.getString("org.apache.tools.ant.module.run.Bundle", "FMT_finished_target_status", new String JavaDoc[] {(projectNameInStatus?projectName:"build.xml")+" (run-deploy)"}));
213         if (url != null)
214             return Utils.loadFromURL(url);
215         return null;
216     }
217     
218     public static String JavaDoc deploy(String JavaDoc projectnName, String JavaDoc url) throws IOException JavaDoc {
219         return deploy(projectnName, url, false);
220     }
221     
222     /** Undeploys Application. Verifies that application node in runtime disappears.
223      * @param app Name of application to undeploy
224      */

225     
226     public static void undeploy(String JavaDoc category, String JavaDoc app) {
227         RuntimeTabOperator runtimeTab = RuntimeTabOperator.invoke();
228         Node rootNode = new Node(runtimeTab.getRootNode(), Bundle.getStringTrimmed("org.netbeans.modules.j2ee.deployment.impl.ui.Bundle", "SERVER_REGISTRY_NODE")
229         +"|Application Server|"
230                 + Bundle.getStringTrimmed("org.netbeans.modules.j2ee.sun.ide.j2ee.runtime.nodes.Bundle", "LBL_Applications") + "|"
231                 + category);
232         rootNode.performPopupAction("Refresh");
233         Node node = new Node(rootNode, app);
234         node.performPopupAction(Bundle.getStringTrimmed("org.netbeans.modules.j2ee.sun.ide.j2ee.runtime.nodes.Bundle", "LBL_Undeploy"));
235         node.waitNotPresent();
236     }
237     
238     public static void undeploy(String JavaDoc app) {
239         undeploy(Bundle.getStringTrimmed("org.netbeans.modules.j2ee.sun.ide.j2ee.runtime.nodes.Bundle", "LBL_AppModules"), app);
240     }
241     
242     /** Checks whether firstText in on firstLine and secondText in on secondLine. If deleteLine is true,
243      * deletes insertLine and inserts there insertText.
244      */

245     public void checkAndModify(String JavaDoc file, int firstLine, String JavaDoc firstText,
246             int secondLine, String JavaDoc secondText, int insertLine, boolean deleteLine, String JavaDoc insertText) {
247         EditorOperator editor = new EditorWindowOperator().getEditor(file);
248         if (firstText != null) {
249             if (!(editor.getText(firstLine).indexOf(firstText)>=0))
250                 nbtestcase.fail("I expect text '"+firstText+"' on line "+firstLine+" in "+file+"."+
251                         "There is text: '"+editor.getText(firstLine)+"'.");
252         }
253         if (secondText != null) {
254             if (!(editor.getText(secondLine).indexOf(secondText)>=0))
255                 nbtestcase.fail("I expect text '"+secondText+"' on line "+secondLine+" in "+file+"."+
256                         "There is text: '"+editor.getText(secondLine)+"'.");
257         }
258         if (deleteLine)
259             editor.deleteLine(insertLine);
260         if (insertText != null)
261             editor.insert(insertText, insertLine, 1);
262         editor.save();
263     }
264     
265     public static void buildProject(String JavaDoc projectName) {
266         Node node = new ProjectsTabOperator().getProjectRootNode(projectName);
267         node.performPopupAction(Bundle.getStringTrimmed(
268                 "org.netbeans.modules.j2ee.earproject.ui.Bundle", "LBL_BuildAction_Name"));
269         MainWindowOperator.getDefault().getTimeouts().setTimeout("Waiter.WaitingTime", 300000);
270         MainWindowOperator.getDefault().waitStatusText(Bundle.getString(
271                 "org.apache.tools.ant.module.run.Bundle", "FMT_finished_target_status",
272                 new String JavaDoc[] {projectName.replace(' ', '_') + " (dist)"}));
273         new EventTool().waitNoEvent(2500);
274     }
275     
276     public static void cleanProject(String JavaDoc projectName) {
277         Node node = new ProjectsTabOperator().getProjectRootNode(projectName);
278         node.performPopupAction(Bundle.getStringTrimmed(
279                 "org.netbeans.modules.j2ee.earproject.ui.Bundle", "LBL_CleanAction_Name"));
280         MainWindowOperator.getDefault().getTimeouts().setTimeout("Waiter.WaitingTime", 300000);
281         MainWindowOperator.getDefault().waitStatusText(Bundle.getString(
282                 "org.apache.tools.ant.module.run.Bundle", "FMT_finished_target_status",
283                 new String JavaDoc[] {projectName.replace(' ', '_') + " (clean)"}));
284         new EventTool().waitNoEvent(2500);
285     }
286     
287     public static void createLibrary(String JavaDoc name, String JavaDoc[] jars, String JavaDoc[] srcs, String JavaDoc[] javadocs) {
288         if ((name == null) || (name.indexOf(" ") > -1)) {
289             throw new IllegalArgumentException JavaDoc("Name cannot be null nor contain spaces");
290         }
291         if (jars == null) {
292             jars = new String JavaDoc[0];
293         }
294         if (srcs == null) {
295             srcs = new String JavaDoc[0];
296         }
297         if (javadocs == null) {
298             javadocs = new String JavaDoc[0];
299         }
300         new ActionNoBlock("Tools|Library Manager", null).performMenu();
301         NbDialogOperator ndo = new NbDialogOperator(Bundle.getStringTrimmed(
302                 LIBRARY_BUNDLE, "CTL_LibrariesManager"));
303         new JButtonOperator(ndo, Bundle.getStringTrimmed(
304                 LIBRARY_BUNDLE, "CTL_NewLibrary")).push();
305         NbDialogOperator ndo2 = new NbDialogOperator(Bundle.getStringTrimmed(
306                 LIBRARY_BUNDLE, "CTL_CreateLibrary"));
307         JTextFieldOperator jtfo = new JTextFieldOperator(ndo2, 0);
308         jtfo.clearText();
309         jtfo.typeText(name);
310         ndo2.ok();
311         JTabbedPaneOperator jtpo = new JTabbedPaneOperator(ndo, "Classpath");
312         for (int i = 0; i < jars.length; i++) {
313             new JButtonOperator(jtpo, "Add JAR/Folder...").push();
314             ndo2 = new NbDialogOperator("Browse JAR/Folder");
315             jtfo = new JTextFieldOperator(ndo2, 0);
316             jtfo.clearText();
317             jtfo.typeText(jars[i]);
318             new JButtonOperator(ndo2, "Add JAR/Folder").push();
319         }
320         jtpo.selectPage("Sources");
321         for (int i = 0; i < srcs.length; i++) {
322             new JButtonOperator(jtpo, "Add JAR/Folder...").push();
323             ndo2 = new NbDialogOperator("Browse JAR/Folder");
324             jtfo = new JTextFieldOperator(ndo2, 0);
325             jtfo.clearText();
326             jtfo.typeText(srcs[i]);
327             new JButtonOperator(ndo2, "Add JAR/Folder").push();
328         }
329         jtpo.selectPage("Javadoc");
330         for (int i = 0; i < javadocs.length; i++) {
331             new JButtonOperator(jtpo, "Add ZIP/Folder...").push();
332             ndo2 = new NbDialogOperator("Browse ZIP/Folder");
333             jtfo = new JTextFieldOperator(ndo2, 0);
334             jtfo.clearText();
335             jtfo.typeText(javadocs[i]);
336             new JButtonOperator(ndo2, "Add ZIP/Folder").push();
337         }
338         ndo.ok();
339     }
340     
341     public static void openOutputTab() {
342         new ActionNoBlock("Window|Output", null).performMenu();
343     }
344 }
345
Popular Tags