KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > java > gui > copypaste > ClassNodeTest


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.java.gui.copypaste;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.PrintStream JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import junit.textui.TestRunner;
27 import org.netbeans.jellytools.EditorOperator;
28 import org.netbeans.jellytools.JellyTestCase;
29 import org.netbeans.jellytools.NbDialogOperator;
30 import org.netbeans.jellytools.ProjectsTabOperator;
31 import org.netbeans.jellytools.actions.CopyAction;
32 import org.netbeans.jellytools.actions.CutAction;
33 import org.netbeans.jellytools.actions.OpenAction;
34 import org.netbeans.jellytools.actions.PasteAction;
35 import org.netbeans.jellytools.modules.java.SimpleCopyAction;
36 import org.netbeans.jellytools.modules.java.SimpleMoveAction;
37 import org.netbeans.jellytools.nodes.Node;
38 import org.netbeans.jemmy.JemmyProperties;
39 import org.netbeans.jemmy.TestOut;
40 import org.netbeans.jemmy.operators.JEditorPaneOperator;
41 import org.netbeans.junit.NbTestSuite;
42 import org.netbeans.test.java.Utilities;
43 import org.netbeans.test.java.gui.GuiUtilities;
44
45
46
47 /**
48  * Tests copy, cut and paste operations on class node.
49  * @author Roman Strobl
50  */

51 public class ClassNodeTest extends JellyTestCase {
52     
53     // name of sample project
54
private static final String JavaDoc TEST_PROJECT_NAME = "default";
55
56     // path to sample files
57
private static final String JavaDoc TEST_PACKAGE_PATH =
58             "org.netbeans.test.java.gui.copypaste";
59             
60     // name of sample package
61
private static final String JavaDoc TEST_PACKAGE_NAME = TEST_PACKAGE_PATH+".test";
62
63     // name of sample package 2
64
private static final String JavaDoc TEST_PACKAGE_NAME_2 =
65             TEST_PACKAGE_PATH+".test2";
66
67     // name of sample package 3
68
private static final String JavaDoc TEST_PACKAGE_NAME_3 =
69             TEST_PACKAGE_PATH+".test3";
70
71     // name of sample class
72
private static final String JavaDoc TEST_CLASS_NAME = "TestClass";
73
74     // name of sample interface
75
private static final String JavaDoc TEST_IFACE_NAME = "TestInterface";
76
77     /**
78      * error log
79      */

80     protected static PrintStream JavaDoc err;
81     
82     /**
83      * standard log
84      */

85     protected static PrintStream JavaDoc log;
86    
87     // workdir, default /tmp, changed to NBJUnit workdir during test
88
private String JavaDoc workDir = "/tmp";
89     
90     // actual directory with project
91
private static String JavaDoc projectDir;
92     
93     
94     /**
95      * Needs to be defined because of JUnit
96      * @param name Name of test
97      */

98     public ClassNodeTest(String JavaDoc name) {
99         super(name);
100     }
101
102     /**
103      * Adds tests into the test suite.
104      * @return suite
105      */

106     public static NbTestSuite suite() {
107         NbTestSuite suite = new NbTestSuite();
108         // prepare testing project and package - not a core test but needed
109
suite.addTest(new ClassNodeTest("testCopyPaste"));
110         suite.addTest(new ClassNodeTest("testCutPaste"));
111         suite.addTest(new ClassNodeTest("testCopyPasteInterface"));
112         suite.addTest(new ClassNodeTest("testCutPasteInterface"));
113         return suite;
114     }
115     
116     /**
117      * Main method for standalone execution.
118      * @param args the command line arguments
119      */

120     public static void main(java.lang.String JavaDoc[] args) {
121         TestRunner.run(suite());
122     }
123     
124     /**
125      * Sets up logging facilities.
126      */

127     public void setUp() {
128         System.out.println("######## "+getName()+" #######");
129         err = getLog();
130         log = getRef();
131         JemmyProperties.getProperties().setOutput(new TestOut(null,
132                 new PrintWriter JavaDoc(err, true), new PrintWriter JavaDoc(err, false), null));
133         try {
134             File JavaDoc wd = getWorkDir();
135             workDir = wd.toString();
136         } catch (IOException JavaDoc e) { }
137     }
138     
139     /**
140      * Tests copy and paste on a class node.
141      */

142     public void testCopyPaste() {
143         Node pn = new ProjectsTabOperator().getProjectRootNode(
144                 TEST_PROJECT_NAME);
145         pn.select();
146         
147         GuiUtilities.waitForChildNode(TEST_PROJECT_NAME,
148                 org.netbeans.jellytools.Bundle.getString(
149                 "org.netbeans.modules.java.j2seproject.Bundle",
150                 "NAME_src.dir"), TEST_PACKAGE_NAME_2);
151
152         GuiUtilities.waitForChildNode(TEST_PROJECT_NAME,
153                 org.netbeans.jellytools.Bundle.getString(
154                 "org.netbeans.modules.java.j2seproject.Bundle",
155                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME,
156                 TEST_CLASS_NAME);
157         
158         // perform copy
159
Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
160                 "org.netbeans.modules.java.j2seproject.Bundle",
161                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME+"|"
162                 +TEST_CLASS_NAME);
163         //n.select();
164
//Utilities.takeANap(10000);
165

166         new CopyAction().perform(n);
167         
168         // perform paste
169
Node n2 = new Node(pn, org.netbeans.jellytools.Bundle.getString(
170                 "org.netbeans.modules.java.j2seproject.Bundle",
171                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME_2);
172   
173         n2.select();
174         new SimpleCopyAction().perform();
175                 
176         Utilities.takeANap(1000);
177         
178         // check if pasted file exists
179
GuiUtilities.waitForChildNode(TEST_PROJECT_NAME,
180                 org.netbeans.jellytools.Bundle.getString(
181                 "org.netbeans.modules.java.j2seproject.Bundle",
182                 "NAME_src.dir")+"|"
183                 +TEST_PACKAGE_NAME_2, TEST_CLASS_NAME);
184
185         Node node = new Node(pn, org.netbeans.jellytools.Bundle.getString(
186                 "org.netbeans.modules.java.j2seproject.Bundle",
187                 "NAME_src.dir")+"|"
188                 +TEST_PACKAGE_NAME_2+"|"+TEST_CLASS_NAME);
189         new OpenAction().perform();
190         
191         // open new testclass
192
Node n3 = new Node(pn, org.netbeans.jellytools.Bundle.getString(
193                 "org.netbeans.modules.java.j2seproject.Bundle",
194                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME_2+"|"
195                 +TEST_CLASS_NAME);
196         n3.select();
197         new OpenAction().perform();
198                 
199         // update testclass window
200
JEditorPaneOperator txtOper = new EditorOperator(TEST_CLASS_NAME).txtEditorPane();
201         
202         // compare files
203
ref(txtOper.getText());
204         compareReferenceFiles();
205     }
206
207
208     /**
209      * Tests cut and paste operations on a class node.
210      */

211     public void testCutPaste() {
212         Node pn = new ProjectsTabOperator().getProjectRootNode(TEST_PROJECT_NAME);
213         pn.select();
214         
215         GuiUtilities.waitForChildNode(TEST_PROJECT_NAME,
216                 org.netbeans.jellytools.Bundle.getString(
217                 "org.netbeans.modules.java.j2seproject.Bundle",
218                 "NAME_src.dir"), TEST_PACKAGE_NAME_2);
219         
220         Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
221                 "org.netbeans.modules.java.j2seproject.Bundle",
222                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME+"|"
223                 +TEST_CLASS_NAME);
224         
225         // perform cut
226
n.select();
227         new CutAction().perform();
228         
229         // perform paste
230
Node n2 = new Node(pn, org.netbeans.jellytools.Bundle.getString(
231                 "org.netbeans.modules.java.j2seproject.Bundle",
232                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME_3);
233         n2.select();
234         new SimpleMoveAction().perform();
235         
236         Utilities.takeANap(1000);
237         
238         // cancel refactoring dialog
239

240 // refactoring is not invoked when moving files in project view
241
// NbDialogOperator refact = new NbDialogOperator(
242
// org.netbeans.jellytools.Bundle.getString(
243
// "org.netbeans.modules.refactoring.ui.Bundle",
244
// "LBL_MoveClass"));
245
// refact.cancel();
246
// refact.waitClosed();
247

248         // check if pasted file exists !!! - Cannot exist if refact was canceled ???
249
GuiUtilities.waitForChildNode(TEST_PROJECT_NAME,
250                 org.netbeans.jellytools.Bundle.getString(
251                 "org.netbeans.modules.java.j2seproject.Bundle",
252                 "NAME_src.dir")+"|"
253                 +TEST_PACKAGE_NAME_3, TEST_CLASS_NAME);
254         
255         Node n3 = new Node(pn, org.netbeans.jellytools.Bundle.getString(
256                 "org.netbeans.modules.java.j2seproject.Bundle",
257                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME_3+"|"
258                 +TEST_CLASS_NAME);
259                 
260         // close old testclass window
261
new EditorOperator(TEST_CLASS_NAME).close(true);
262         
263         // open new testclass
264
Node n4 = new Node(pn, org.netbeans.jellytools.Bundle.getString(
265                 "org.netbeans.modules.java.j2seproject.Bundle",
266                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME_3+"|"
267                 +TEST_CLASS_NAME);
268         n4.select();
269         new OpenAction().perform();
270         
271         // update testclass window
272
JEditorPaneOperator txtOper = new EditorOperator(TEST_CLASS_NAME).txtEditorPane();
273
274         // compare files
275
ref(txtOper.getText());
276         compareReferenceFiles();
277         
278         // check if original file was deleted
279
Node n5 = new Node(pn, org.netbeans.jellytools.Bundle.getString(
280                 "org.netbeans.modules.java.j2seproject.Bundle",
281                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME);
282         String JavaDoc[] children = n5.getChildren();
283         assertEquals(children.length, 1);
284     }
285     
286     /**
287      * Tests copy and paste on an interface.
288      */

289     public void testCopyPasteInterface() {
290         Node pn = new ProjectsTabOperator().getProjectRootNode(
291                 TEST_PROJECT_NAME);
292         pn.select();
293                         
294         // perform copy
295
Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
296                 "org.netbeans.modules.java.j2seproject.Bundle",
297                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME+"|"
298                 +TEST_IFACE_NAME);
299         //n.select();
300

301         //Utilities.takeANap(3000);
302
new CopyAction().perform(n);
303         
304         // perform paste
305
Node n2 = new Node(pn, org.netbeans.jellytools.Bundle.getString(
306                 "org.netbeans.modules.java.j2seproject.Bundle",
307                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME_2);
308   
309         n2.select();
310         new SimpleCopyAction().perform();
311         
312         Utilities.takeANap(1000);
313
314         // check if pasted file exists
315
GuiUtilities.waitForChildNode(TEST_PROJECT_NAME,
316                 org.netbeans.jellytools.Bundle.getString(
317                 "org.netbeans.modules.java.j2seproject.Bundle",
318                 "NAME_src.dir")+"|"
319                 +TEST_PACKAGE_NAME_2, TEST_IFACE_NAME);
320         
321         // open new testiface
322
Node n3 = new Node(pn, org.netbeans.jellytools.Bundle.getString(
323                 "org.netbeans.modules.java.j2seproject.Bundle",
324                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME_2+"|"
325                 +TEST_IFACE_NAME);
326         n3.select();
327         new OpenAction().perform();
328                 
329         // update testiface window
330
JEditorPaneOperator txtOper = new EditorOperator(TEST_IFACE_NAME).txtEditorPane();
331         
332         // compare files
333
ref(txtOper.getText());
334         compareReferenceFiles();
335     }
336     
337     /**
338      * Tests cut and paste on an interface.
339      */

340     public void testCutPasteInterface() {
341         Node pn = new ProjectsTabOperator().getProjectRootNode(TEST_PROJECT_NAME);
342         pn.select();
343                     
344         // perform cut
345
Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
346                 "org.netbeans.modules.java.j2seproject.Bundle",
347                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME+"|"
348                 +TEST_IFACE_NAME);
349         n.select();
350         
351         new CutAction().perform();
352         
353         // perform paste
354
Node n2 = new Node(pn, org.netbeans.jellytools.Bundle.getString(
355                 "org.netbeans.modules.java.j2seproject.Bundle",
356                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME_3);
357   
358         n2.select();
359         new SimpleMoveAction().perform();
360         
361         Utilities.takeANap(1000);
362
363 // refactoring is not invoked when moving files in project window
364
// NbDialogOperator refact = new NbDialogOperator(
365
// org.netbeans.jellytools.Bundle.getString(
366
// "org.netbeans.modules.refactoring.ui.Bundle",
367
// "LBL_MoveClass"));
368
// refact.cancel();
369
// refact.waitClosed();
370

371         // check if pasted file exists !!! - Cannot exist if refact was canceled ???
372
GuiUtilities.waitForChildNode(TEST_PROJECT_NAME,
373                 org.netbeans.jellytools.Bundle.getString(
374                 "org.netbeans.modules.java.j2seproject.Bundle",
375                 "NAME_src.dir")+"|"
376                 +TEST_PACKAGE_NAME_3, TEST_IFACE_NAME);
377         
378         // open new testiface
379
Node n3 = new Node(pn, org.netbeans.jellytools.Bundle.getString(
380                 "org.netbeans.modules.java.j2seproject.Bundle",
381                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME_3+"|"
382                 +TEST_IFACE_NAME);
383         n3.select();
384         new OpenAction().perform();
385                 
386         // compare files
387
ref(new EditorOperator(TEST_IFACE_NAME).getText());
388         compareReferenceFiles();
389
390         // check if original file was deleted
391
Node n5 = new Node(pn, org.netbeans.jellytools.Bundle.getString(
392                 "org.netbeans.modules.java.j2seproject.Bundle",
393                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME);
394         String JavaDoc[] children = n5.getChildren();
395         assertEquals(children.length, 0);
396     }
397         
398 }
399
Popular Tags