1 19 20 package org.netbeans.modules.java.editor.rename; 21 22 import java.beans.PropertyVetoException ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.io.OutputStream ; 26 import java.util.Collection ; 27 import java.util.HashSet ; 28 import java.util.Set ; 29 import javax.swing.text.Document ; 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 49 public class InstantRenameActionTest extends NbTestCase { 50 51 public InstantRenameActionTest(String name) { 52 super(name); 53 } 54 55 @Override 56 protected void setUp() throws Exception { 57 SourceUtilsTestUtil.prepareTest(new String [] {"org/netbeans/modules/java/editor/resources/layer.xml"}, new Object [0]); 58 } 59 60 public void testSimpleRename() throws Exception { 61 boolean[] wasResolved = new boolean[1]; 62 Collection <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 { 68 boolean[] wasResolved = new boolean[1]; 69 Collection <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 { 75 checkInnerclasses1(113 - 59); 76 } 77 78 public void testRenameInnerclass2() throws Exception { 79 checkInnerclasses1(127 - 59); 80 } 81 82 public void testRenameInnerclass3() throws Exception { 83 checkInnerclasses1(158 - 59); 84 } 85 86 public void testRenameInnerclass4() throws Exception { 87 checkInnerclasses1(171 - 59); 88 } 89 90 public void testNonPrivateClassWithPrivateConstructor() throws Exception { 91 boolean[] wasResolved = new boolean[1]; 92 Collection <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 { 99 boolean[] wasResolved = new boolean[1]; 100 Collection <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 { 107 boolean[] wasResolved = new boolean[1]; 108 Collection <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 { 116 boolean[] wasResolved = new boolean[1]; 117 Collection <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 { 125 boolean[] wasResolved = new boolean[1]; 126 Collection <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 { 134 boolean[] wasResolved = new boolean[1]; 135 Collection <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 <Highlight> changePoints, int... origs) { 143 Set <Pair> awaited = new HashSet <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 <Pair> got = new HashSet <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 168 public int hashCode() { 169 return a ^ b; 170 } 171 172 @Override 173 public boolean equals(Object 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 184 public String toString() { 185 return "(" + a + "," + b + ")"; 186 } 187 } 188 189 private FileObject source; 190 191 private Collection <Highlight> performTest(String sourceCode, final int offset, boolean[] wasResolved) throws Exception { 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 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 what) throws Exception { 214 FileLock lock = file.lock(); 215 OutputStream out = file.getOutputStream(lock); 216 217 try { 218 out.write(what.getBytes()); 219 } finally { 220 out.close(); 221 lock.releaseLock(); 222 } 223 } 224 225 230 public static FileObject makeScratchDir(NbTestCase test) throws IOException { 231 test.clearWorkDir(); 232 File root = test.getWorkDir(); 233 assert root.isDirectory() && root.list().length == 0; 234 FileObject fo = FileUtil.toFileObject(root); 235 if (fo != null) { 236 return fo; 238 } else { 239 LocalFileSystem lfs = new LocalFileSystem(); 241 try { 242 lfs.setRootDirectory(root); 243 } catch (PropertyVetoException e) { 244 assert false : e; 245 } 246 Repository.getDefault().addFileSystem(lfs); 247 return lfs.getRoot(); 248 } 249 } 250 } 251 | Popular Tags |