001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.test.java.gui.fiximports;
043:
044: import java.awt.event.KeyEvent;
045: import java.io.File;
046: import java.io.IOException;
047: import java.io.PrintStream;
048: import java.io.PrintWriter;
049: import javax.swing.ComboBoxModel;
050: import org.netbeans.jellytools.EditorOperator;
051: import org.netbeans.jellytools.JellyTestCase;
052: import org.netbeans.jellytools.MainWindowOperator;
053: import org.netbeans.jellytools.ProjectsTabOperator;
054: import org.netbeans.jellytools.actions.OpenAction;
055: import org.netbeans.jellytools.modules.java.FixAllImports;
056: import org.netbeans.jellytools.nodes.Node;
057: import org.netbeans.jemmy.JemmyProperties;
058: import org.netbeans.jemmy.TestOut;
059: import org.netbeans.jemmy.operators.JComboBoxOperator;
060: import org.netbeans.junit.NbTestSuite;
061: import org.netbeans.test.java.Utilities;
062:
063: /**
064: * Tests Fix Imports.
065: * @author Roman Strobl
066: */
067: public class FixImportsTest extends JellyTestCase {
068:
069: // name of sample project
070: private static final String TEST_PROJECT_NAME = "default";
071:
072: // path to sample files
073: private static final String TEST_PACKAGE_PATH = "org.netbeans.test.java.gui.fiximports";
074:
075: // name of sample package
076: private static final String TEST_PACKAGE_NAME = TEST_PACKAGE_PATH
077: + ".test";
078:
079: // name of sample class
080: private static final String TEST_CLASS_NAME = "TestClass";
081:
082: /**
083: * error log
084: */
085: protected static PrintStream err;
086:
087: /**
088: * standard log
089: */
090: protected static PrintStream log;
091:
092: // workdir, default /tmp, changed to NBJUnit workdir during test
093: private String workDir = "/tmp";
094:
095: // actual directory with project
096: private static String projectDir;
097:
098: /**
099: * Adds tests into the test suite.
100: * @return suite
101: */
102: public static NbTestSuite suite() {
103: NbTestSuite suite = new NbTestSuite();
104: suite.addTest(new FixImportsTest("testFixImports"));
105: suite.addTest(new FixImportsTest("testFixImportsComplex"));
106: suite.addTest(new FixImportsTest("testCancel"));
107: suite.addTest(new FixImportsTest("testNothingToFix"));
108: suite.addTest(new FixImportsTest("testRemoveAndAdd"));
109: suite.addTest(new FixImportsTest("testRemoveUnused"));
110: suite.addTest(new FixImportsTest("testStatusBar"));
111: suite.addTest(new FixImportsTest("testCheckboxPersistence"));
112: return suite;
113: }
114:
115: /**
116: * Main method for standalone execution.
117: * @param args the command line arguments
118: */
119: public static void main(String[] args) {
120: junit.textui.TestRunner.run(suite());
121: }
122:
123: /**
124: * Sets up logging facilities.
125: */
126: @Override
127: public void setUp() {
128: System.out.println("######## " + getName() + " #######");
129: err = getLog();
130: log = getRef();
131: JemmyProperties.getProperties().setOutput(
132: new TestOut(null, new PrintWriter(err, true),
133: new PrintWriter(err, false), null));
134: try {
135: File wd = getWorkDir();
136: workDir = wd.toString();
137: } catch (IOException e) {
138: }
139: }
140:
141: /**
142: * Creates a new instance of JavaElementsTest
143: * @param testName name of test
144: */
145: public FixImportsTest(String testName) {
146: super (testName);
147: }
148:
149: /**
150: * Fix imports test.
151: */
152: public void testFixImports() {
153: Node pn = new ProjectsTabOperator()
154: .getProjectRootNode(TEST_PROJECT_NAME);
155: pn.select();
156:
157: Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
158: "org.netbeans.modules.java.j2seproject.Bundle",
159: "NAME_src.dir")
160: + "|" + TEST_PACKAGE_NAME + "|" + TEST_CLASS_NAME);
161:
162: n.select();
163: new OpenAction().perform();
164:
165: // test fix imports on Vector
166: EditorOperator editor = new EditorOperator(TEST_CLASS_NAME);
167: editor.insert("Vector v = new Vector();\n", 15, 1);
168: Utilities.takeANap(100);
169: MainWindowOperator.getDefault().pushKey(KeyEvent.VK_I,
170: KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK);
171: //FixAllImports fio = new FixAllImports();
172: //fio.ok();
173: // wait for fix imports
174: for (int i = 0; i < 10; i++) {
175: Utilities.takeANap(1000);
176: if (editor.getText().contains("import java.util.Vector;"))
177: break;
178: System.out.println(MainWindowOperator.getDefault()
179: .getStatusText());
180: }
181:
182: ref(editor.getText());
183:
184: compareReferenceFiles();
185: }
186:
187: /**
188: * Complex fix imports test.
189: */
190: public void testFixImportsComplex() {
191: // test fix imports on List
192: EditorOperator editor = new EditorOperator(TEST_CLASS_NAME);
193: editor.insert("List l = new List();\n", 17, 1);
194:
195: Utilities.takeANap(100);
196:
197: // invoke fix imports
198: MainWindowOperator.getDefault().pushKey(KeyEvent.VK_I,
199: KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK);
200: FixAllImports fio = new FixAllImports();
201:
202: JComboBoxOperator cbo = fio.cbo(0);
203: ComboBoxModel cbm = cbo.getModel();
204: int in;
205: for (in = 0; in < cbm.getSize(); in++) {
206: if (cbm.getElementAt(in).equals("java.util.List")) {
207: cbo.setSelectedIndex(in);
208: break;
209: }
210: }
211: assertTrue("java.util.List not listed", in < cbm.getSize());
212: fio.ok();
213: // wait for fix imports
214: for (int i = 0; i < 10; i++) {
215: Utilities.takeANap(1000);
216: if (editor.getText().contains("import java.util.List;"))
217: break;
218: //System.out.println(MainWindowOperator.getDefault().getStatusText());
219: }
220: ref(editor.getText());
221: compareReferenceFiles();
222: editor.close(false);
223: }
224:
225: public void testRemoveUnused() {
226: Node pn = new ProjectsTabOperator()
227: .getProjectRootNode(TEST_PROJECT_NAME);
228: pn.select();
229:
230: Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
231: "org.netbeans.modules.java.j2seproject.Bundle",
232: "NAME_src.dir")
233: + "|" + TEST_PACKAGE_NAME + "|" + TEST_CLASS_NAME);
234: n.select();
235: new OpenAction().perform();
236: EditorOperator editor = new EditorOperator(TEST_CLASS_NAME);
237: try {
238: editor.insert("import java.util.Date;\n", 7, 1);
239: Utilities.takeANap(100);
240: MainWindowOperator.getDefault().pushKey(KeyEvent.VK_I,
241: KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK);
242: assertFalse("Import is not removed", editor.getText()
243: .contains("import java.util.Date;"));
244: editor.insert("List l;\n", 12, 1);
245: editor.insert("import java.util.Date;\n", 7, 1);
246: Utilities.takeANap(100);
247: MainWindowOperator.getDefault().pushKey(KeyEvent.VK_I,
248: KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK);
249: FixAllImports fio = new FixAllImports();
250: fio.cbRemoveUnusedImports().setSelected(false);
251: fio.ok();
252: assertTrue("Import is removed", editor.getText().contains(
253: "import java.util.Date;"));
254: } finally {
255: editor.close(false);
256: }
257: }
258:
259: public void testRemoveAndAdd() {
260: Node pn = new ProjectsTabOperator()
261: .getProjectRootNode(TEST_PROJECT_NAME);
262: pn.select();
263:
264: Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
265: "org.netbeans.modules.java.j2seproject.Bundle",
266: "NAME_src.dir")
267: + "|" + TEST_PACKAGE_NAME + "|" + TEST_CLASS_NAME);
268: n.select();
269: new OpenAction().perform();
270: EditorOperator editor = new EditorOperator(TEST_CLASS_NAME);
271: try {
272: editor.insert("import java.util.Date;\n", 7, 1);
273: editor.insert("List m;\n", 16, 1);
274: Utilities.takeANap(100);
275: MainWindowOperator.getDefault().pushKey(KeyEvent.VK_I,
276: KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK);
277:
278: FixAllImports fio = new FixAllImports();
279: fio.cbRemoveUnusedImports().setSelected(true);
280: fio.ok();
281: assertFalse("Import is not removed", editor.getText()
282: .contains("import java.util.Date;"));
283: assertTrue("Import is not added", editor.getText()
284: .contains("import java.util.List;"));
285: } finally {
286: editor.close(false);
287: }
288:
289: }
290:
291: public void testNothingToFix() {
292: Node pn = new ProjectsTabOperator()
293: .getProjectRootNode(TEST_PROJECT_NAME);
294: pn.select();
295:
296: Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
297: "org.netbeans.modules.java.j2seproject.Bundle",
298: "NAME_src.dir")
299: + "|" + TEST_PACKAGE_NAME + "|" + TEST_CLASS_NAME);
300: n.select();
301: new OpenAction().perform();
302: EditorOperator editor = new EditorOperator(TEST_CLASS_NAME);
303: try {
304: Utilities.takeANap(100);
305: MainWindowOperator.getDefault().pushKey(KeyEvent.VK_I,
306: KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK);
307: Utilities.takeANap(200);
308: assertEquals(MainWindowOperator.getDefault()
309: .getStatusText(),
310: "Nothing to fix in import statements.");
311: } finally {
312: editor.close(false);
313: }
314:
315: }
316:
317: public void testCancel() {
318: Node pn = new ProjectsTabOperator()
319: .getProjectRootNode(TEST_PROJECT_NAME);
320: pn.select();
321:
322: Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
323: "org.netbeans.modules.java.j2seproject.Bundle",
324: "NAME_src.dir")
325: + "|" + TEST_PACKAGE_NAME + "|" + TEST_CLASS_NAME);
326: n.select();
327: new OpenAction().perform();
328: EditorOperator editor = new EditorOperator(TEST_CLASS_NAME);
329: try {
330: editor.insert("List m;\n", 15, 1);
331: Utilities.takeANap(100);
332: MainWindowOperator.getDefault().pushKey(KeyEvent.VK_I,
333: KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK);
334: FixAllImports fio = new FixAllImports();
335: fio.cancel();
336: assertFalse("Import is added", editor.getText().contains(
337: "import java.util.List;"));
338: } finally {
339: editor.close(false);
340: }
341: }
342:
343: public void testCheckboxPersistence() {
344: Node pn = new ProjectsTabOperator()
345: .getProjectRootNode(TEST_PROJECT_NAME);
346: pn.select();
347:
348: Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
349: "org.netbeans.modules.java.j2seproject.Bundle",
350: "NAME_src.dir")
351: + "|" + TEST_PACKAGE_NAME + "|" + TEST_CLASS_NAME);
352: n.select();
353: new OpenAction().perform();
354: EditorOperator editor = new EditorOperator(TEST_CLASS_NAME);
355: try {
356: editor.insert("List m;\n", 15, 1);
357: Utilities.takeANap(100);
358: MainWindowOperator.getDefault().pushKey(KeyEvent.VK_I,
359: KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK);
360: FixAllImports fio = new FixAllImports();
361: fio.cbRemoveUnusedImports().setSelected(false);
362: fio.cancel();
363: Utilities.takeANap(100);
364: MainWindowOperator.getDefault().pushKey(KeyEvent.VK_I,
365: KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK);
366: fio = new FixAllImports();
367: assertEquals("Checkbox state is not persistent", fio
368: .cbRemoveUnusedImports().isSelected(), false);
369: fio.cancel();
370: } finally {
371: editor.close(false);
372: }
373: }
374:
375: public void testStatusBar() {
376: Node pn = new ProjectsTabOperator()
377: .getProjectRootNode(TEST_PROJECT_NAME);
378: pn.select();
379:
380: Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
381: "org.netbeans.modules.java.j2seproject.Bundle",
382: "NAME_src.dir")
383: + "|" + TEST_PACKAGE_NAME + "|" + TEST_CLASS_NAME);
384: n.select();
385: new OpenAction().perform();
386: EditorOperator editor = new EditorOperator(TEST_CLASS_NAME);
387: try {
388: Utilities.takeANap(100);
389: editor.insert("import java.util.Date;\n", 7, 1);
390: MainWindowOperator.getDefault().pushKey(KeyEvent.VK_I,
391: KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK);
392: Utilities.takeANap(200);
393: assertEquals("Nothing to fix in import statements.",
394: MainWindowOperator.getDefault().getStatusText());
395: } finally {
396: editor.close(false);
397: }
398: }
399:
400: }
|