KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > editor > rename > InstantRenameActionTest


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.modules.java.editor.rename;
21
22 import java.beans.PropertyVetoException JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Set JavaDoc;
29 import javax.swing.text.Document JavaDoc;
30 import org.netbeans.api.java.lexer.JavaTokenId;
31 import org.netbeans.api.java.source.JavaSource;
32 import org.netbeans.api.java.source.JavaSource.Phase;
33 import org.netbeans.api.java.source.SourceUtilsTestUtil;
34 import org.netbeans.api.lexer.Language;
35 import org.netbeans.junit.NbTestCase;
36 import org.netbeans.modules.editor.highlights.spi.Highlight;
37 import org.openide.cookies.EditorCookie;
38 import org.openide.filesystems.FileLock;
39 import org.openide.filesystems.FileObject;
40 import org.openide.filesystems.FileUtil;
41 import org.openide.filesystems.LocalFileSystem;
42 import org.openide.filesystems.Repository;
43 import org.openide.loaders.DataObject;
44
45 /**
46  *
47  * @author Jan Lahoda
48  */

49 public class InstantRenameActionTest extends NbTestCase {
50     
51     public InstantRenameActionTest(String JavaDoc name) {
52         super(name);
53     }
54     
55     @Override JavaDoc
56     protected void setUp() throws Exception JavaDoc {
57         SourceUtilsTestUtil.prepareTest(new String JavaDoc[] {"org/netbeans/modules/java/editor/resources/layer.xml"}, new Object JavaDoc[0]);
58     }
59     
60     public void testSimpleRename() throws Exception JavaDoc {
61         boolean[] wasResolved = new boolean[1];
62         Collection JavaDoc<Highlight> changePoints = performTest("package test; public class Test{private int xx; public void run() {xx = 1;}}", 68, wasResolved);
63         
64         validateChangePoints(changePoints, 103 - 59, 105 - 59, 126 - 59, 128 - 59);
65     }
66     
67     private void checkInnerclasses1(int caret) throws Exception JavaDoc {
68         boolean[] wasResolved = new boolean[1];
69         Collection JavaDoc<Highlight> changePoints = performTest("package test; public class Test{public void run() {Test1 t = new Test1();} private static class Test1{public Test1(){}}", caret, wasResolved);
70         
71         validateChangePoints(changePoints, 110 - 59, 115 - 59, 124 - 59, 129 - 59, 155 - 59, 160 - 59, 168 - 59, 173 - 59);
72     }
73     
74     public void testRenameInnerclass1() throws Exception JavaDoc {
75         checkInnerclasses1(113 - 59);
76     }
77     
78     public void testRenameInnerclass2() throws Exception JavaDoc {
79         checkInnerclasses1(127 - 59);
80     }
81     
82     public void testRenameInnerclass3() throws Exception JavaDoc {
83         checkInnerclasses1(158 - 59);
84     }
85     
86     public void testRenameInnerclass4() throws Exception JavaDoc {
87         checkInnerclasses1(171 - 59);
88     }
89     
90     public void testNonPrivateClassWithPrivateConstructor() throws Exception JavaDoc {
91         boolean[] wasResolved = new boolean[1];
92         Collection JavaDoc<Highlight> changePoints = performTest("package test; public class Test{public void run() {Test1 t = new Test1();} static class Test1{private Test1(){}}", 112 - 59, wasResolved);
93         
94         assertNull(changePoints);
95         assertTrue(wasResolved[0]);
96     }
97     
98     public void testBrokenSource89736() throws Exception JavaDoc {
99         boolean[] wasResolved = new boolean[1];
100         Collection JavaDoc<Highlight> changePoints = performTest("package test; public class Test{private void run(int hj test) {} }", 116 - 59, wasResolved);
101         
102         assertNull(changePoints);
103         assertFalse(wasResolved[0]);
104     }
105     
106     public void testLocalClassAreRenamable1() throws Exception JavaDoc {
107         boolean[] wasResolved = new boolean[1];
108         Collection JavaDoc<Highlight> changePoints = performTest("package test; public class Test{public void run() {class PPP {PPP() {} PPP p = new PPP();}}}", 117 - 59, wasResolved);
109         
110         assertNotNull(changePoints);
111         assertTrue(wasResolved[0]);
112         validateChangePoints(changePoints, 116 - 59, 119 - 59, 121 - 59, 124 - 59, 130 - 59, 133 - 59, 142 - 59, 145 - 59);
113     }
114     
115     public void testLocalClassAreRenamable2() throws Exception JavaDoc {
116         boolean[] wasResolved = new boolean[1];
117         Collection JavaDoc<Highlight> changePoints = performTest("package test; public class Test{public Test() {class PPP {PPP() {} PPP p = new PPP();}}}", 117 - 59, wasResolved);
118         
119         assertNotNull(changePoints);
120         assertTrue(wasResolved[0]);
121         validateChangePoints(changePoints, 116 - 59, 119 - 59, 121 - 59, 124 - 59, 130 - 59, 133 - 59, 142 - 59, 145 - 59);
122     }
123     
124     public void testLocalClassAreRenamable3() throws Exception JavaDoc {
125         boolean[] wasResolved = new boolean[1];
126         Collection JavaDoc<Highlight> changePoints = performTest("package test; public class Test{ {class PPP {PPP() {} PPP p = new PPP();}}}", 117 - 59, wasResolved);
127         
128         assertNotNull(changePoints);
129         assertTrue(wasResolved[0]);
130         validateChangePoints(changePoints, 116 - 59, 119 - 59, 121 - 59, 124 - 59, 130 - 59, 133 - 59, 142 - 59, 145 - 59);
131     }
132     
133     public void testLocalClassAreRenamable4() throws Exception JavaDoc {
134         boolean[] wasResolved = new boolean[1];
135         Collection JavaDoc<Highlight> changePoints = performTest("package test; public class Test{ static {class PPP {PPP() {} PPP p = new PPP();}}}", 117 - 59, wasResolved);
136         
137         assertNotNull(changePoints);
138         assertTrue(wasResolved[0]);
139         validateChangePoints(changePoints, 116 - 59, 119 - 59, 121 - 59, 124 - 59, 130 - 59, 133 - 59, 142 - 59, 145 - 59);
140     }
141     
142     private void validateChangePoints(Collection JavaDoc<Highlight> changePoints, int... origs) {
143         Set JavaDoc<Pair> awaited = new HashSet JavaDoc<Pair>();
144         
145         for (int cntr = 0; cntr < origs.length; cntr += 2) {
146             awaited.add(new Pair(origs[cntr], origs[cntr + 1]));
147         }
148         
149         Set JavaDoc<Pair> got = new HashSet JavaDoc<Pair>();
150         
151         for (Highlight h : changePoints) {
152             got.add(new Pair(h.getStart(), h.getEnd()));
153         }
154         
155         assertEquals(awaited, got);
156     }
157     
158     private static class Pair {
159         private int a;
160         private int b;
161         
162         public Pair(int a, int b) {
163             this.a = a;
164             this.b = b;
165         }
166         
167         @Override JavaDoc
168         public int hashCode() {
169             return a ^ b;
170         }
171         
172         @Override JavaDoc
173         public boolean equals(Object JavaDoc o) {
174             if (o instanceof Pair) {
175                 Pair p = (Pair) o;
176                 
177                 return a == p.a && b == p.b;
178             }
179             
180             return false;
181         }
182         
183         @Override JavaDoc
184         public String JavaDoc toString() {
185             return "(" + a + "," + b + ")";
186         }
187     }
188     
189     private FileObject source;
190     
191     private Collection JavaDoc<Highlight> performTest(String JavaDoc sourceCode, final int offset, boolean[] wasResolved) throws Exception JavaDoc {
192         FileObject root = makeScratchDir(this);
193         
194         FileObject sourceDir = root.createFolder("src");
195         FileObject buildDir = root.createFolder("build");
196         FileObject cacheDir = root.createFolder("cache");
197         
198         source = sourceDir.createFolder("test").createData("Test.java");
199         
200         writeIntoFile(source, sourceCode);
201         
202         SourceUtilsTestUtil.prepareTest(sourceDir, buildDir, cacheDir, new FileObject[0]);
203         
204         DataObject od = DataObject.find(source);
205         EditorCookie ec = od.getCookie(EditorCookie.class);
206         Document JavaDoc doc = ec.openDocument();
207         
208         doc.putProperty(Language.class, JavaTokenId.language());
209         
210         return InstantRenameAction.computeChangePoints(SourceUtilsTestUtil.getCompilationInfo(JavaSource.forFileObject(source), Phase.RESOLVED), offset, wasResolved);
211     }
212     
213     private void writeIntoFile(FileObject file, String JavaDoc what) throws Exception JavaDoc {
214         FileLock lock = file.lock();
215         OutputStream JavaDoc out = file.getOutputStream(lock);
216         
217         try {
218             out.write(what.getBytes());
219         } finally {
220             out.close();
221             lock.releaseLock();
222         }
223     }
224     
225     /**Copied from org.netbeans.api.project.
226      * Create a scratch directory for tests.
227      * Will be in /tmp or whatever, and will be empty.
228      * If you just need a java.io.File use clearWorkDir + getWorkDir.
229      */

230     public static FileObject makeScratchDir(NbTestCase test) throws IOException JavaDoc {
231         test.clearWorkDir();
232         File JavaDoc root = test.getWorkDir();
233         assert root.isDirectory() && root.list().length == 0;
234         FileObject fo = FileUtil.toFileObject(root);
235         if (fo != null) {
236             // Presumably using masterfs.
237
return fo;
238         } else {
239             // For the benefit of those not using masterfs.
240
LocalFileSystem lfs = new LocalFileSystem();
241             try {
242                 lfs.setRootDirectory(root);
243             } catch (PropertyVetoException JavaDoc e) {
244                 assert false : e;
245             }
246             Repository.getDefault().addFileSystem(lfs);
247             return lfs.getRoot();
248         }
249     }
250 }
251
Popular Tags