1 19 20 package org.netbeans.modules.java.editor.codegen; 21 22 import com.sun.source.util.TreePath; 23 import java.beans.PropertyVetoException ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.io.OutputStream ; 27 import javax.lang.model.element.ExecutableElement; 28 import javax.lang.model.element.TypeElement; 29 import javax.lang.model.type.TypeMirror; 30 import javax.lang.model.util.ElementFilter; 31 import org.netbeans.api.java.source.CancellableTask; 32 import org.netbeans.api.java.source.CompilationController; 33 import org.netbeans.api.java.source.CompilationInfo; 34 import org.netbeans.api.java.source.JavaSource; 35 import org.netbeans.api.java.source.JavaSource.Phase; 36 import org.netbeans.api.java.source.ModificationResult; 37 import org.netbeans.api.java.source.SourceUtilsTestUtil; 38 import org.netbeans.api.java.source.WorkingCopy; 39 import org.netbeans.junit.NbTestCase; 40 import org.openide.filesystems.FileLock; 41 import org.openide.filesystems.FileObject; 42 import org.openide.filesystems.FileUtil; 43 import org.openide.filesystems.LocalFileSystem; 44 import org.openide.filesystems.Repository; 45 46 50 public class GeneratorUtilsTest extends NbTestCase { 51 52 public GeneratorUtilsTest(String testName) { 53 super(testName); 54 } 55 56 protected void setUp() throws Exception { 57 SourceUtilsTestUtil.prepareTest(new String [0], new Object [0]); 58 } 59 60 private void writeIntoFile(FileObject file, String what) throws Exception { 61 FileLock lock = file.lock(); 62 OutputStream out = file.getOutputStream(lock); 63 64 try { 65 out.write(what.getBytes()); 66 } finally { 67 out.close(); 68 lock.releaseLock(); 69 } 70 } 71 72 public void testImplementAllAbstractMethods1() throws Exception { 73 performTest("package test;\npublic class Test implements Runnable {\npublic Test(){\n}\n }\n", 54, new RunnableValidator()); 74 } 75 76 84 public void testImplementAllAbstractMethods4() throws Exception { 85 performTest("package test;\npublic class Test implements Runnable {\npublic Test(){\n}\npublic void testMethod() {\n} }\n", 54, new RunnableValidator()); 86 } 87 88 public void testImplementAllAbstractMethods5() throws Exception { 89 performTest("package test;import java.util.concurrent.*;\npublic class Test implements Future<String>{\npublic Test(){\n} }\n", 89, new SimpleFutureValidator("java.lang.String")); 90 } 91 92 100 public void testImplementAllAbstractMethods7() throws Exception { 101 performTest("package test;\npublic class Test extends java.util.AbstractList{\npublic Test(){\n} }\n", 64, new Validator() { 102 public void validate(CompilationInfo info) { 103 } 104 }); 105 } 106 107 109 120 public void testImplementAllAbstractMethods9() throws Exception { 121 performTest("package test;\npublic class Test implements java.util.concurrent.ExecutorService {\npublic Test(){\n} }\n", 30, new Validator() { 122 public void validate(CompilationInfo info) { 123 } 124 }); 125 } 126 127 public void testImplementAllAbstractMethodsa() throws Exception { 128 performTest("package test;\npublic class Test implements XX {\npublic Test(){\n} }\ninterface XX {public <T extends java.util.List> void test(T t);}", 30, new Validator() { 129 public void validate(CompilationInfo info) { 130 } 131 }); 132 } 133 134 public static interface Validator { 135 136 public void validate(CompilationInfo info); 137 138 } 139 140 private final class RunnableValidator implements Validator { 141 142 public void validate(CompilationInfo info) { 143 TypeElement test = info.getElements().getTypeElement("test.Test"); 144 145 boolean foundRunMethod = false; 146 147 for (ExecutableElement ee : ElementFilter.methodsIn(test.getEnclosedElements())) { 148 if ("run".equals(ee.getSimpleName().toString())) { 149 if (ee.getParameters().isEmpty()) { 150 assertFalse(foundRunMethod); 151 foundRunMethod = true; 152 } 153 } 154 } 155 156 assertTrue(foundRunMethod); 157 } 158 159 } 160 161 private final class SimpleFutureValidator extends FutureValidator { 162 163 private String returnTypeName; 164 165 public SimpleFutureValidator(String returnTypeName) { 166 this.returnTypeName = returnTypeName; 167 } 168 169 protected TypeMirror returnType(CompilationInfo info) { 170 TypeElement returnTypeElement = info.getElements().getTypeElement(returnTypeName); 171 172 return returnTypeElement.asType(); 173 } 174 } 175 176 private abstract class FutureValidator implements Validator { 177 178 protected abstract TypeMirror returnType(CompilationInfo info); 179 180 public void validate(CompilationInfo info) { 181 TypeElement test = info.getElements().getTypeElement("test.Test"); 182 TypeMirror returnType = returnType(info); 183 184 boolean hasShortGet = false; 185 boolean hasLongGet = false; 186 187 for (ExecutableElement ee : ElementFilter.methodsIn(test.getEnclosedElements())) { 188 if ("get".equals(ee.getSimpleName().toString())) { 189 if (ee.getParameters().isEmpty()) { 190 assertFalse(hasShortGet); 191 assertTrue(info.getTypes().isSameType(returnType, ee.getReturnType())); 192 hasShortGet = true; 193 } 194 if (ee.getParameters().size() == 2) { 195 assertFalse(hasLongGet); 196 assertTrue(info.getTypes().isSameType(returnType, ee.getReturnType())); 197 hasLongGet = true; 198 } 199 } 200 } 201 202 assertTrue(hasShortGet); 203 assertTrue(hasLongGet); 204 } 205 206 } 207 208 private void performTest(String sourceCode, final int offset, final Validator validator) throws Exception { 209 FileObject root = makeScratchDir(this); 210 211 FileObject sourceDir = root.createFolder("src"); 212 FileObject buildDir = root.createFolder("build"); 213 FileObject cacheDir = root.createFolder("cache"); 214 215 FileObject source = sourceDir.createFolder("test").createData("Test.java"); 216 217 writeIntoFile(source, sourceCode); 218 219 SourceUtilsTestUtil.prepareTest(sourceDir, buildDir, cacheDir, new FileObject[0]); 220 221 JavaSource js = JavaSource.forFileObject(source); 222 223 ModificationResult result = js.runModificationTask(new CancellableTask<WorkingCopy>() { 224 public void cancel() { 225 } 226 public void run(WorkingCopy copy) throws Exception { 227 copy.toPhase(Phase.RESOLVED); 228 TreePath tp = copy.getTreeUtilities().pathFor(offset); 229 GeneratorUtils.generateAllAbstractMethodImplementations(copy, tp); 230 } 231 }); 232 233 result.commit(); 234 235 js.runUserActionTask(new CancellableTask<CompilationController>() { 236 public void cancel() { 237 } 238 public void run(CompilationController controller) throws Exception { 239 System.err.println("text:"); 240 System.err.println(controller.getText()); 241 controller.toPhase(Phase.RESOLVED); 242 243 assertEquals(controller.getDiagnostics().toString(), 0, controller.getDiagnostics().size()); 244 245 validator.validate(controller); 246 } 247 }, true); 248 } 249 250 255 public static FileObject makeScratchDir(NbTestCase test) throws IOException { 256 test.clearWorkDir(); 257 File root = test.getWorkDir(); 258 assert root.isDirectory() && root.list().length == 0; 259 FileObject fo = FileUtil.toFileObject(root); 260 if (fo != null) { 261 return fo; 263 } else { 264 LocalFileSystem lfs = new LocalFileSystem(); 266 try { 267 lfs.setRootDirectory(root); 268 } catch (PropertyVetoException e) { 269 assert false : e; 270 } 271 Repository.getDefault().addFileSystem(lfs); 272 return lfs.getRoot(); 273 } 274 } 275 276 } 277 | Popular Tags |