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.modules.java.editor.rename;
043:
044: import java.beans.PropertyVetoException;
045: import java.io.File;
046: import java.io.IOException;
047: import java.io.OutputStream;
048: import java.util.Collection;
049: import java.util.HashSet;
050: import java.util.Set;
051: import javax.swing.text.Document;
052: import org.netbeans.api.java.lexer.JavaTokenId;
053: import org.netbeans.api.java.source.JavaSource;
054: import org.netbeans.api.java.source.JavaSource.Phase;
055: import org.netbeans.api.java.source.SourceUtilsTestUtil;
056: import org.netbeans.api.lexer.Language;
057: import org.netbeans.api.lexer.Token;
058: import org.netbeans.junit.NbTestCase;
059: import org.openide.cookies.EditorCookie;
060: import org.openide.filesystems.FileLock;
061: import org.openide.filesystems.FileObject;
062: import org.openide.filesystems.FileUtil;
063: import org.openide.filesystems.LocalFileSystem;
064: import org.openide.filesystems.Repository;
065: import org.openide.loaders.DataObject;
066:
067: /**
068: *
069: * @author Jan Lahoda
070: */
071: public class InstantRenameActionTest extends NbTestCase {
072:
073: public InstantRenameActionTest(String name) {
074: super (name);
075: }
076:
077: @Override
078: protected void setUp() throws Exception {
079: SourceUtilsTestUtil
080: .prepareTest(
081: new String[] { "org/netbeans/modules/java/editor/resources/layer.xml" },
082: new Object[0]);
083: }
084:
085: public void testSimpleRename() throws Exception {
086: boolean[] wasResolved = new boolean[1];
087: Collection<Token<JavaTokenId>> changePoints = performTest(
088: "package test; public class Test{private int xx; public void run() {xx = 1;}}",
089: 68, wasResolved);
090:
091: validateChangePoints(changePoints, 103 - 59, 105 - 59,
092: 126 - 59, 128 - 59);
093: }
094:
095: private void checkInnerclasses1(int caret) throws Exception {
096: boolean[] wasResolved = new boolean[1];
097: Collection<Token<JavaTokenId>> changePoints = performTest(
098: "package test; public class Test{public void run() {Test1 t = new Test1();} private static class Test1{public Test1(){}}",
099: caret, wasResolved);
100:
101: validateChangePoints(changePoints, 110 - 59, 115 - 59,
102: 124 - 59, 129 - 59, 155 - 59, 160 - 59, 168 - 59,
103: 173 - 59);
104: }
105:
106: public void testRenameInnerclass1() throws Exception {
107: checkInnerclasses1(113 - 59);
108: }
109:
110: public void testRenameInnerclass2() throws Exception {
111: checkInnerclasses1(127 - 59);
112: }
113:
114: public void testRenameInnerclass3() throws Exception {
115: checkInnerclasses1(158 - 59);
116: }
117:
118: public void testRenameInnerclass4() throws Exception {
119: checkInnerclasses1(171 - 59);
120: }
121:
122: public void testNonPrivateClassWithPrivateConstructor()
123: throws Exception {
124: boolean[] wasResolved = new boolean[1];
125: Collection<Token<JavaTokenId>> changePoints = performTest(
126: "package test; public class Test{public void run() {Test1 t = new Test1();} static class Test1{private Test1(){}}",
127: 112 - 59, wasResolved);
128:
129: assertNull(changePoints);
130: assertTrue(wasResolved[0]);
131: }
132:
133: public void testBrokenSource89736() throws Exception {
134: boolean[] wasResolved = new boolean[1];
135: Collection<Token<JavaTokenId>> changePoints = performTest(
136: "package test; public class Test{private void run(int hj test) {} }",
137: 116 - 59, wasResolved);
138:
139: assertNull(changePoints);
140: assertFalse(wasResolved[0]);
141: }
142:
143: public void testLocalClassAreRenamable1() throws Exception {
144: boolean[] wasResolved = new boolean[1];
145: Collection<Token<JavaTokenId>> changePoints = performTest(
146: "package test; public class Test{public void run() {class PPP {PPP() {} PPP p = new PPP();}}}",
147: 117 - 59, wasResolved);
148:
149: assertNotNull(changePoints);
150: assertTrue(wasResolved[0]);
151: validateChangePoints(changePoints, 116 - 59, 119 - 59,
152: 121 - 59, 124 - 59, 130 - 59, 133 - 59, 142 - 59,
153: 145 - 59);
154: }
155:
156: public void testLocalClassAreRenamable2() throws Exception {
157: boolean[] wasResolved = new boolean[1];
158: Collection<Token<JavaTokenId>> changePoints = performTest(
159: "package test; public class Test{public Test() {class PPP {PPP() {} PPP p = new PPP();}}}",
160: 117 - 59, wasResolved);
161:
162: assertNotNull(changePoints);
163: assertTrue(wasResolved[0]);
164: validateChangePoints(changePoints, 116 - 59, 119 - 59,
165: 121 - 59, 124 - 59, 130 - 59, 133 - 59, 142 - 59,
166: 145 - 59);
167: }
168:
169: public void testLocalClassAreRenamable3() throws Exception {
170: boolean[] wasResolved = new boolean[1];
171: Collection<Token<JavaTokenId>> changePoints = performTest(
172: "package test; public class Test{ {class PPP {PPP() {} PPP p = new PPP();}}}",
173: 117 - 59, wasResolved);
174:
175: assertNotNull(changePoints);
176: assertTrue(wasResolved[0]);
177: validateChangePoints(changePoints, 116 - 59, 119 - 59,
178: 121 - 59, 124 - 59, 130 - 59, 133 - 59, 142 - 59,
179: 145 - 59);
180: }
181:
182: public void testLocalClassAreRenamable4() throws Exception {
183: boolean[] wasResolved = new boolean[1];
184: Collection<Token<JavaTokenId>> changePoints = performTest(
185: "package test; public class Test{ static {class PPP {PPP() {} PPP p = new PPP();}}}",
186: 117 - 59, wasResolved);
187:
188: assertNotNull(changePoints);
189: assertTrue(wasResolved[0]);
190: validateChangePoints(changePoints, 116 - 59, 119 - 59,
191: 121 - 59, 124 - 59, 130 - 59, 133 - 59, 142 - 59,
192: 145 - 59);
193: }
194:
195: private void validateChangePoints(
196: Collection<Token<JavaTokenId>> changePoints, int... origs) {
197: Set<Pair> awaited = new HashSet<Pair>();
198:
199: for (int cntr = 0; cntr < origs.length; cntr += 2) {
200: awaited.add(new Pair(origs[cntr], origs[cntr + 1]));
201: }
202:
203: Set<Pair> got = new HashSet<Pair>();
204:
205: for (Token<JavaTokenId> h : changePoints) {
206: got.add(new Pair(h.offset(null), h.offset(null)
207: + h.length()));
208: }
209:
210: assertEquals(awaited, got);
211: }
212:
213: private static class Pair {
214: private int a;
215: private int b;
216:
217: public Pair(int a, int b) {
218: this .a = a;
219: this .b = b;
220: }
221:
222: @Override
223: public int hashCode() {
224: return a ^ b;
225: }
226:
227: @Override
228: public boolean equals(Object o) {
229: if (o instanceof Pair) {
230: Pair p = (Pair) o;
231:
232: return a == p.a && b == p.b;
233: }
234:
235: return false;
236: }
237:
238: @Override
239: public String toString() {
240: return "(" + a + "," + b + ")";
241: }
242: }
243:
244: private FileObject source;
245:
246: private Collection<Token<JavaTokenId>> performTest(
247: String sourceCode, final int offset, boolean[] wasResolved)
248: throws Exception {
249: FileObject root = makeScratchDir(this );
250:
251: FileObject sourceDir = root.createFolder("src");
252: FileObject buildDir = root.createFolder("build");
253: FileObject cacheDir = root.createFolder("cache");
254:
255: source = sourceDir.createFolder("test").createData("Test.java");
256:
257: writeIntoFile(source, sourceCode);
258:
259: SourceUtilsTestUtil.prepareTest(sourceDir, buildDir, cacheDir,
260: new FileObject[0]);
261:
262: DataObject od = DataObject.find(source);
263: EditorCookie ec = od.getCookie(EditorCookie.class);
264: Document doc = ec.openDocument();
265:
266: doc.putProperty(Language.class, JavaTokenId.language());
267:
268: return InstantRenamePerformer.computeChangePoints(
269: SourceUtilsTestUtil.getCompilationInfo(JavaSource
270: .forFileObject(source), Phase.RESOLVED),
271: offset, wasResolved);
272: }
273:
274: private void writeIntoFile(FileObject file, String what)
275: throws Exception {
276: FileLock lock = file.lock();
277: OutputStream out = file.getOutputStream(lock);
278:
279: try {
280: out.write(what.getBytes());
281: } finally {
282: out.close();
283: lock.releaseLock();
284: }
285: }
286:
287: /**Copied from org.netbeans.api.project.
288: * Create a scratch directory for tests.
289: * Will be in /tmp or whatever, and will be empty.
290: * If you just need a java.io.File use clearWorkDir + getWorkDir.
291: */
292: public static FileObject makeScratchDir(NbTestCase test)
293: throws IOException {
294: test.clearWorkDir();
295: File root = test.getWorkDir();
296: assert root.isDirectory() && root.list().length == 0;
297: FileObject fo = FileUtil.toFileObject(root);
298: if (fo != null) {
299: // Presumably using masterfs.
300: return fo;
301: } else {
302: // For the benefit of those not using masterfs.
303: LocalFileSystem lfs = new LocalFileSystem();
304: try {
305: lfs.setRootDirectory(root);
306: } catch (PropertyVetoException e) {
307: assert false : e;
308: }
309: Repository.getDefault().addFileSystem(lfs);
310: return lfs.getRoot();
311: }
312: }
313: }
|