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: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.modules.java.editor.rename;
041:
042: import java.awt.event.KeyEvent;
043: import java.util.Collections;
044: import java.util.Enumeration;
045: import java.util.regex.Pattern;
046: import javax.swing.JEditorPane;
047: import javax.swing.JFrame;
048: import javax.swing.event.ChangeEvent;
049: import javax.swing.text.Document;
050: import javax.swing.undo.UndoManager;
051: import org.netbeans.api.editor.mimelookup.MimePath;
052: import org.netbeans.api.editor.mimelookup.test.MockMimeLookup;
053: import org.netbeans.api.java.lexer.JavaTokenId;
054: import org.netbeans.api.java.source.SourceUtilsTestUtil;
055: import org.netbeans.api.java.source.TestUtilities;
056: import org.netbeans.api.lexer.Language;
057: import org.netbeans.editor.BaseDocument;
058: import org.netbeans.junit.NbTestCase;
059: import org.netbeans.modules.editor.java.JavaKit;
060: import org.netbeans.modules.java.JavaDataLoader;
061: import org.netbeans.modules.java.JavaDataObject;
062: import org.openide.LifecycleManager;
063: import org.openide.cookies.EditorCookie;
064: import org.openide.filesystems.FileObject;
065: import org.openide.filesystems.FileUtil;
066: import org.openide.loaders.DataLoader;
067: import org.openide.loaders.DataLoaderPool;
068: import org.openide.loaders.DataObject;
069: import org.openide.util.Lookup;
070: import org.openide.util.LookupEvent;
071: import org.openide.util.LookupListener;
072:
073: /**
074: *
075: * @author lahvac
076: */
077: public class InstantRenamePerformerTest extends NbTestCase {
078:
079: public InstantRenamePerformerTest(String testName) {
080: super (testName);
081: }
082:
083: protected void setUp() throws Exception {
084: MockMimeLookup.setInstances(MimePath.parse("text/x-java"),
085: new JavaKit());
086: MockMimeLookup mml = new MockMimeLookup();
087: Class.forName(SourceUtilsTestUtil.class.getName(), true,
088: SourceUtilsTestUtil.class.getClassLoader());
089: SourceUtilsTestUtil
090: .prepareTest(
091: new String[] { "org/netbeans/modules/java/editor/resources/layer.xml" },
092: new Object[] {
093: new DefaultPool(),
094: mml,
095: JavaDataLoader.findObject(
096: JavaDataLoader.class, true) });
097: }
098:
099: @Override
100: protected void tearDown() throws Exception {
101: super .tearDown();
102: LifecycleManager.getDefault().saveAll();
103: }
104:
105: public void testSimple1() throws Exception {
106: KeyEvent ke = new KeyEvent(new JFrame(), KeyEvent.KEY_TYPED, 0,
107: 0, KeyEvent.VK_UNDEFINED, 'a');
108: performTest(
109: "package test; public class Test { public void test() {int x|xx = 0; int y = xxx; } }",
110: 80 - 22,
111: ke,
112: "package test; public class Test { public void test() {int axxx = 0; int y = axxx; } }",
113: true);
114: }
115:
116: public void testSimple2() throws Exception {
117: KeyEvent ke = new KeyEvent(new JFrame(), KeyEvent.KEY_TYPED, 0,
118: 0, KeyEvent.VK_UNDEFINED, 'a');
119: performTest(
120: "package test; public class Test { public void test() {int x|xx = 0; int y = xxx; } }",
121: 84 - 22 - 1,
122: ke,
123: "package test; public class Test { public void test() {int xxxa = 0; int y = xxxa; } }",
124: true);
125: }
126:
127: public void testSimple3() throws Exception {
128: KeyEvent ke = new KeyEvent(new JFrame(), KeyEvent.KEY_PRESSED,
129: 0, 0, KeyEvent.VK_BACK_SPACE, '\0');
130: performTest(
131: "package test; public class Test { public void test() {int a|bc = 0; int y = abc; } }",
132: 83 - 22 - 1,
133: ke,
134: "package test; public class Test { public void test() {int ac = 0; int y = ac; } }",
135: true);
136: }
137:
138: public void testSimple4() throws Exception {
139: KeyEvent ke = new KeyEvent(new JFrame(), KeyEvent.KEY_PRESSED,
140: 0, 0, KeyEvent.VK_BACK_SPACE, '\0');
141: performTest(
142: "package test; public class Test { public void test() {int a|bc = 0; int y = abc; } }",
143: 84 - 22 - 1,
144: ke,
145: "package test; public class Test { public void test() {int ab = 0; int y = ab; } }",
146: true);
147: }
148:
149: public void testSimple5() throws Exception {
150: KeyEvent ke = new KeyEvent(new JFrame(), KeyEvent.KEY_PRESSED,
151: 0, 0, KeyEvent.VK_DELETE, '\0');
152: performTest(
153: "package test; public class Test { public void test() {int a|bc = 0; int y = abc; } }",
154: 80 - 22,
155: ke,
156: "package test; public class Test { public void test() {int bc = 0; int y = bc; } }",
157: true);
158: }
159:
160: public void testCancel1() throws Exception {
161: KeyEvent ke = new KeyEvent(new JFrame(), KeyEvent.KEY_TYPED, 0,
162: 0, KeyEvent.VK_UNDEFINED, 'a');
163: performTest(
164: "package test; public class Test { public void test() {int x|xx = 0; int y = xxx; } }",
165: 79 - 22,
166: ke,
167: "package test; public class Test { public void test() {inta xxx = 0; int y = xxx; } }",
168: false);
169: }
170:
171: public void testCancel2() throws Exception {
172: KeyEvent ke = new KeyEvent(new JFrame(), KeyEvent.KEY_TYPED, 0,
173: 0, KeyEvent.VK_UNDEFINED, 'a');
174: performTest(
175: "package test; public class Test { public void test() {int x|xx = 0; int y = xxx; } }",
176: 85 - 22 - 1,
177: ke,
178: "package test; public class Test { public void test() {int xxx a= 0; int y = xxx; } }",
179: false);
180: }
181:
182: public void testCancel3() throws Exception {
183: KeyEvent ke = new KeyEvent(new JFrame(), KeyEvent.KEY_PRESSED,
184: 0, 0, KeyEvent.VK_BACK_SPACE, '\0');
185: performTest(
186: "package test; public class Test { public void test() {int x|xx = 0; int y = xxx; } }",
187: 79 - 22,
188: ke,
189: "package test; public class Test { public void test() {in xxx = 0; int y = xxx; } }",
190: false);
191: }
192:
193: public void testCancel4() throws Exception {
194: KeyEvent ke = new KeyEvent(new JFrame(), KeyEvent.KEY_PRESSED,
195: 0, 0, KeyEvent.VK_BACK_SPACE, '\0');
196: performTest(
197: "package test; public class Test { public void test() {int x|xx = 0; int y = xxx; } }",
198: 85 - 22 - 1,
199: ke,
200: "package test; public class Test { public void test() {int xxx= 0; int y = xxx; } }",
201: false);
202: }
203:
204: public void testCancel5() throws Exception {
205: KeyEvent ke = new KeyEvent(new JFrame(), KeyEvent.KEY_PRESSED,
206: 0, 0, KeyEvent.VK_DELETE, '\0');
207: performTest(
208: "package test; public class Test { public void test() {int x|xx = 0; int y = xxx; } }",
209: 79 - 22,
210: ke,
211: "package test; public class Test { public void test() {intxxx = 0; int y = xxx; } }",
212: false);
213: }
214:
215: public void testCancel6() throws Exception {
216: KeyEvent ke = new KeyEvent(new JFrame(), KeyEvent.KEY_PRESSED,
217: 0, 0, KeyEvent.VK_DELETE, '\0');
218: performTest(
219: "package test; public class Test { public void test() {int x|xx = 0; int y = xxx; } }",
220: 84 - 22 - 1,
221: ke,
222: "package test; public class Test { public void test() {int xxx= 0; int y = xxx; } }",
223: false);
224: }
225:
226: //TODO:
227: // public void testUndoAndContinue1() throws Exception {
228: // KeyEvent ke = new KeyEvent(new JFrame(), KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_BACK_SPACE, '\0');
229: // performTest("package test; public class Test { public void test() {int x|xx = 0; int y = xxx; } }", 80 - 22, ke, "package test; public class Test { public void test() {int xxx = 0; int y = xxx; } }", true);
230: // }
231: //
232: // public void testUndoAndContinue2() throws Exception {
233: // KeyEvent ke = new KeyEvent(new JFrame(), KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_DELETE, '\0');
234: // performTest("package test; public class Test { public void test() {int x|xx = 0; int y = xxx; } }", 84 - 22 - 1, ke, "package test; public class Test { public void test() {int xxx = 0; int y = xxx; } }", true);
235: // }
236:
237: public void testSelection1() throws Exception {
238: KeyEvent ke = new KeyEvent(new JFrame(), KeyEvent.KEY_TYPED, 0,
239: 0, KeyEvent.VK_UNDEFINED, 'a');
240: performTest(
241: "package test; public class Test { public void test() {int x|xx = 0; int y = xxx; } }",
242: 80 - 22,
243: ke,
244: 84 - 22 - 1,
245: "package test; public class Test { public void test() {int a = 0; int y = a; } }",
246: true);
247: }
248:
249: public void testSelection2() throws Exception {
250: KeyEvent ke = new KeyEvent(new JFrame(), KeyEvent.KEY_TYPED, 0,
251: 0, KeyEvent.VK_UNDEFINED, 'a');
252: performTest(
253: "package test; public class Test { public void test() {int x|xx = 0; int y = xxx; } }",
254: 79 - 22,
255: ke,
256: 85 - 22 - 1,
257: "package test; public class Test { public void test() {inta= 0; int y = xxx; } }",
258: false);
259: }
260:
261: public void testSelection3() throws Exception {
262: KeyEvent ke = new KeyEvent(new JFrame(), KeyEvent.KEY_TYPED, 0,
263: 0, KeyEvent.VK_UNDEFINED, 'a');
264: performTest(
265: "package test; public class Test { public void test() {int x|xx = 0; int y = xxx; } }",
266: 81 - 22,
267: ke,
268: 85 - 22 - 1,
269: "package test; public class Test { public void test() {int xa= 0; int y = xxx; } }",
270: false);
271: }
272:
273: public void testSelection4() throws Exception {
274: KeyEvent ke = new KeyEvent(new JFrame(), KeyEvent.KEY_TYPED, 0,
275: 0, KeyEvent.VK_UNDEFINED, 'a');
276: performTest(
277: "package test; public class Test { public void test() {int x|xx = 0; int y = xxx; } }",
278: 79 - 22,
279: ke,
280: 83 - 22 - 1,
281: "package test; public class Test { public void test() {intax = 0; int y = xxx; } }",
282: false);
283: }
284:
285: public void testSelection126704a() throws Exception {
286: KeyEvent ke = new KeyEvent(new JFrame(), KeyEvent.KEY_TYPED, 0,
287: 0, KeyEvent.VK_UNDEFINED, 'a');
288: performTest(
289: "package test; public class Test { public void test() {int x| = 0; int y = x; } }",
290: 81 - 22,
291: ke,
292: 80 - 22,
293: "package test; public class Test { public void test() {int a = 0; int y = a; } }",
294: true);
295: }
296:
297: public void testSelection126704b() throws Exception {
298: KeyEvent ke = new KeyEvent(new JFrame(), KeyEvent.KEY_TYPED, 0,
299: 0, KeyEvent.VK_UNDEFINED, 'a');
300: performTest(
301: "package test; public class Test { public void test() {int x| = 0; int y = x; } }",
302: 80 - 22,
303: ke,
304: 82 - 22 - 1,
305: "package test; public class Test { public void test() {int a = 0; int y = a; } }",
306: true);
307: }
308:
309: private void performTest(String sourceCode, int offset,
310: KeyEvent ke, String golden, boolean stillInRename)
311: throws Exception {
312: performTest(sourceCode, offset, ke, -1, golden, stillInRename);
313: }
314:
315: private void performTest(String sourceCode, int offset,
316: KeyEvent ke, int selectionEnd, String golden,
317: boolean stillInRename) throws Exception {
318: clearWorkDir();
319:
320: FileObject root = FileUtil.toFileObject(getWorkDir());
321:
322: FileObject sourceDir = root.createFolder("src");
323: FileObject buildDir = root.createFolder("build");
324: FileObject cacheDir = root.createFolder("cache");
325: FileObject testDir = sourceDir.createFolder("test");
326:
327: FileObject source = testDir.createData("Test.java");
328:
329: TestUtilities.copyStringToFile(source, sourceCode.replaceFirst(
330: Pattern.quote("|"), ""));
331:
332: SourceUtilsTestUtil.prepareTest(sourceDir, buildDir, cacheDir,
333: new FileObject[0]);
334: SourceUtilsTestUtil.compileRecursively(sourceDir);
335:
336: DataObject od = DataObject.find(source);
337:
338: assertTrue(od instanceof JavaDataObject);
339:
340: EditorCookie ec = od.getCookie(EditorCookie.class);
341: Document doc = ec.openDocument();
342:
343: assertTrue(doc instanceof BaseDocument);
344:
345: UndoManager um = new UndoManager();
346:
347: doc.addUndoableEditListener(um);
348: doc.putProperty(BaseDocument.UNDO_MANAGER_PROP, um);
349: doc.putProperty(Language.class, JavaTokenId.language());
350:
351: C p = new C();
352:
353: // p.setEditorKit(new JavaKit());
354:
355: p.setDocument(doc);
356:
357: p.setCaretPosition(sourceCode.indexOf('|'));
358:
359: InstantRenamePerformer.invokeInstantRename(p);
360:
361: p.setCaretPosition(offset);
362:
363: if (selectionEnd != (-1)) {
364: p.moveCaretPosition(selectionEnd);
365: }
366:
367: p.processKeyEvent(ke);
368:
369: assertEquals(
370: stillInRename,
371: p.getClientProperty(InstantRenamePerformer.class) != null);
372: assertEquals(golden, doc.getText(0, doc.getLength()));
373: }
374:
375: @Override
376: protected boolean runInEQ() {
377: return true;
378: }
379:
380: private static final class DefaultPool extends DataLoaderPool
381: implements LookupListener {
382: private final Lookup.Result<DataLoader> result;
383:
384: public DefaultPool() {
385: result = Lookup.getDefault().lookupResult(DataLoader.class);
386: result.addLookupListener(this );
387: }
388:
389: protected Enumeration<? extends DataLoader> loaders() {
390: return Collections.enumeration(result.allInstances());
391: }
392:
393: public void resultChanged(LookupEvent e) {
394: fireChangeEvent(new ChangeEvent(this ));
395: }
396: }
397:
398: private static final class C extends JEditorPane {
399:
400: @Override
401: public void processKeyEvent(KeyEvent e) {
402: super.processKeyEvent(e);
403: }
404:
405: }
406: }
|