KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > java > gui > fiximports > FixImportsTest


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.fiximports;
21
22 import java.awt.event.KeyEvent JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.PrintStream JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27 import javax.swing.ComboBoxModel JavaDoc;
28 import javax.swing.JComboBox JavaDoc;
29 import javax.swing.table.TableModel JavaDoc;
30 import org.netbeans.jellytools.EditorOperator;
31 import org.netbeans.jellytools.JellyTestCase;
32 import org.netbeans.jellytools.MainWindowOperator;
33 import org.netbeans.jellytools.ProjectsTabOperator;
34 import org.netbeans.jellytools.actions.OpenAction;
35 import org.netbeans.jellytools.modules.java.FixImportsOperator;
36 import org.netbeans.jellytools.nodes.Node;
37 import org.netbeans.jemmy.JemmyProperties;
38 import org.netbeans.jemmy.TestOut;
39 import org.netbeans.junit.NbTestSuite;
40 import org.netbeans.test.java.Utilities;
41 import org.netbeans.test.java.gui.GuiUtilities;
42
43
44 /**
45  * Tests Fix Imports.
46  * @author Roman Strobl
47  */

48 public class FixImportsTest extends JellyTestCase {
49        
50     // name of sample project
51
private static final String JavaDoc TEST_PROJECT_NAME = "default";
52     
53     // path to sample files
54
private static final String JavaDoc TEST_PACKAGE_PATH =
55             "org.netbeans.test.java.gui.fiximports";
56     
57     // name of sample package
58
private static final String JavaDoc TEST_PACKAGE_NAME = TEST_PACKAGE_PATH+".test";
59     
60     // name of sample class
61
private static final String JavaDoc TEST_CLASS_NAME = "TestClass";
62
63     /**
64      * error log
65      */

66     protected static PrintStream JavaDoc err;
67     
68     /**
69      * standard log
70      */

71     protected static PrintStream JavaDoc log;
72    
73     // workdir, default /tmp, changed to NBJUnit workdir during test
74
private String JavaDoc workDir = "/tmp";
75     
76     // actual directory with project
77
private static String JavaDoc projectDir;
78        
79     /**
80      * Adds tests into the test suite.
81      * @return suite
82      */

83     public static NbTestSuite suite() {
84         NbTestSuite suite = new NbTestSuite();
85         // prepare testing project and package - not a core test but needed
86
suite.addTest(new FixImportsTest("testFixImports"));
87         suite.addTest(new FixImportsTest("testFixImportsComplex"));
88         return suite;
89     }
90
91      /**
92      * Main method for standalone execution.
93      * @param args the command line arguments
94      */

95     public static void main(String JavaDoc[] args) {
96         junit.textui.TestRunner.run(suite());
97     }
98     
99     /**
100      * Sets up logging facilities.
101      */

102     public void setUp() {
103         System.out.println("######## "+getName()+" #######");
104         err = getLog();
105         log = getRef();
106         JemmyProperties.getProperties().setOutput(new TestOut(null,
107                 new PrintWriter JavaDoc(err, true), new PrintWriter JavaDoc(err, false), null));
108         try {
109             File JavaDoc wd = getWorkDir();
110             workDir = wd.toString();
111         } catch (IOException JavaDoc e) { }
112     }
113     
114     /**
115      * Creates a new instance of JavaElementsTest
116      * @param testName name of test
117      */

118     public FixImportsTest(String JavaDoc testName) {
119         super(testName);
120     }
121
122     /**
123      * Prepares testing project, package and class.
124      */

125     public void testCreateProjectAndPackageAndClass() {
126         projectDir = GuiUtilities.createProjectAndPackageAndClass(
127                 TEST_PROJECT_NAME, TEST_PACKAGE_NAME, TEST_CLASS_NAME, workDir);
128     }
129     
130     /**
131      * Fix imports test.
132      */

133     public void testFixImports() {
134         Node pn = new ProjectsTabOperator().getProjectRootNode(
135                 TEST_PROJECT_NAME);
136         pn.select();
137
138         Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
139                 "org.netbeans.modules.java.j2seproject.Bundle",
140                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME+"|"
141                 +TEST_CLASS_NAME);
142         
143         n.select();
144         new OpenAction().perform();
145                
146         // test fix imports on Vector
147
EditorOperator editor = new EditorOperator(TEST_CLASS_NAME);
148         editor.insert("Vector v = new Vector();\n", 15, 1);
149         Utilities.takeANap(1000);
150         
151         MainWindowOperator.getDefault().pushKey(KeyEvent.VK_F,
152                 KeyEvent.ALT_MASK | KeyEvent.SHIFT_MASK);
153                         FixImportsOperator fio = new FixImportsOperator();
154         fio.ok();
155         // wait for fix imports
156
for (int i=0; i<10; i++) {
157             Utilities.takeANap(1000);
158             if(editor.getText().contains("import java.util.Vector;")) break;
159             System.out.println(MainWindowOperator.getDefault().getStatusText());
160         }
161                               
162         ref(editor.getText());
163         
164         compareReferenceFiles();
165     }
166
167     /**
168      * Complex fix imports test.
169      */

170     public void testFixImportsComplex() {
171         // test fix imports on List
172
EditorOperator editor = new EditorOperator(TEST_CLASS_NAME);
173         editor.insert("List l = new List();\n", 17, 1);
174         
175         Utilities.takeANap(1000);
176         
177         // invoke fix imports
178
MainWindowOperator.getDefault().pushKey(KeyEvent.VK_F,
179                 KeyEvent.ALT_MASK | KeyEvent.SHIFT_MASK);
180
181         FixImportsOperator fio = new FixImportsOperator();
182
183         TableModel JavaDoc tm = fio.tabHtmlTheFollowingClassNamesWereFoundInMoreThanOnePackageSelectTheFullyQualifiedNameToUseInTheImportStatementHtml().getModel();
184         JComboBox JavaDoc jcb = (JComboBox JavaDoc) tm.getValueAt(0, 1);
185
186         ComboBoxModel JavaDoc cbm = jcb.getModel();
187         int in;
188         for(in=0;in<cbm.getSize();in++) {
189             if(cbm.getElementAt(in).equals("java.util.List")) {
190                 jcb.setSelectedIndex(in);
191                 break;
192             }
193         }
194         assertTrue("java.util.List not listed",in<cbm.getSize());
195         fio.ok();
196                 
197         // wait for fix imports
198
for (int i=0; i<10; i++) {
199             Utilities.takeANap(1000);
200             if (editor.getText().contains("import java.util.List;")) break;
201             System.out.println(MainWindowOperator.getDefault().getStatusText());
202         }
203                 
204         ref(editor.getText());
205         
206         compareReferenceFiles();
207         
208         EditorOperator.closeDiscardAll();
209     }
210         
211 }
212
Popular Tags