1 19 20 package org.netbeans.api.java.source; 21 22 import java.io.OutputStream ; 23 import java.net.URL ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import javax.lang.model.element.TypeElement; 27 import javax.lang.model.type.TypeMirror; 28 import org.netbeans.api.java.classpath.ClassPath; 29 import org.netbeans.api.java.source.CancellableTask; 30 import org.netbeans.api.java.source.ClasspathInfo; 31 import org.netbeans.api.java.source.CompilationController; 32 import org.netbeans.api.java.source.CompilationInfo; 33 import org.netbeans.api.java.source.JavaSource; 34 import org.netbeans.api.java.source.JavaSource.Phase; 35 import org.netbeans.api.java.source.SourceUtilsTestUtil; 36 import org.netbeans.junit.NbTestCase; 37 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 38 import org.openide.filesystems.FileLock; 39 import org.openide.filesystems.FileObject; 40 import org.openide.filesystems.FileSystem; 41 import org.openide.filesystems.FileUtil; 42 43 47 public class TypeMirrorHandleTest extends NbTestCase { 48 49 private FileObject testSource; 50 51 public TypeMirrorHandleTest(String testName) { 52 super(testName); 53 } 54 55 protected void setUp() throws Exception { 56 SourceUtilsTestUtil.prepareTest(new String [0], new Object [0]); 57 } 58 59 private TypeMirror parse(CompilationInfo info, String name) { 60 TypeElement string = info.getElements().getTypeElement("test.Test"); 61 62 assertNotNull(string); 63 64 return info.getTreeUtilities().parseType(name, string); 65 } 66 67 private void testCase(CompilationInfo info, String name) { 69 TypeMirror tm = parse(info, name); 70 TypeMirrorHandle th = TypeMirrorHandle.create(tm); 71 72 assertTrue(info.getTypes().isSameType(th.resolve(info), tm)); 73 assertTrue(info.getTypes().isSameType(tm, th.resolve(info))); 74 } 75 76 private void writeIntoFile(FileObject file, String what) throws Exception { 77 FileLock lock = file.lock(); 78 OutputStream out = file.getOutputStream(lock); 79 80 try { 81 out.write(what.getBytes()); 82 } finally { 83 out.close(); 84 lock.releaseLock(); 85 } 86 } 87 88 private void prepareTest() throws Exception { 89 FileSystem fs = FileUtil.createMemoryFileSystem(); 90 91 testSource = fs.getRoot().createData("Test.java"); 92 assertNotNull(testSource); 93 } 94 95 public void testTypeMirrorHandle() throws Exception { 96 prepareTest(); 97 writeIntoFile(testSource, "package test; public class Test<T> {}"); 98 ClassPath empty = ClassPathSupport.createClassPath(new URL [0]); 99 JavaSource js = JavaSource.create(ClasspathInfo.create(ClassPathSupport.createClassPath(SourceUtilsTestUtil.getBootClassPath().toArray(new URL [0])), empty, empty), testSource); 100 101 js.runUserActionTask(new CancellableTask<CompilationController>() { 102 public void cancel() { 103 } 104 public void run(CompilationController info) throws Exception { 105 info.toPhase(Phase.RESOLVED); 106 testCase(info, "java.util.Map"); 107 testCase(info, "java.util.Map<java.lang.Object, java.util.List>"); 108 testCase(info, "java.util.Map<java.lang.Object, java.util.List<java.lang.String>>"); 109 testCase(info, "int[]"); 110 } 111 }, true); 112 } 113 114 public void testTypeMirrorHandleCannotResolve() throws Exception { 115 prepareTest(); 116 writeIntoFile(testSource, "package test; public class Test {} class Test1{}"); 117 ClassPath empty = ClassPathSupport.createClassPath(new URL [0]); 118 JavaSource js = JavaSource.create(ClasspathInfo.create(ClassPathSupport.createClassPath(SourceUtilsTestUtil.getBootClassPath().toArray(new URL [0])), empty, empty), testSource); 119 final List <TypeMirrorHandle> handles = new ArrayList <TypeMirrorHandle>(); 120 121 js.runUserActionTask(new CancellableTask<CompilationController>() { 122 public void cancel() { 123 } 124 public void run(CompilationController info) throws Exception { 125 info.toPhase(Phase.RESOLVED); 126 handles.add(TypeMirrorHandle.create(parse(info, "test.Test1"))); 127 handles.add(TypeMirrorHandle.create(parse(info, "java.util.List<test.Test1>"))); 128 handles.add(TypeMirrorHandle.create(parse(info, "test.Test1[]"))); 129 } 130 }, true); 131 writeIntoFile(testSource, "package test; public class Test {}"); 132 js.runUserActionTask(new CancellableTask<CompilationController>() { 133 public void cancel() { 134 } 135 public void run(CompilationController info) throws Exception { 136 info.toPhase(Phase.RESOLVED); 137 138 int count = 0; 139 140 for (TypeMirrorHandle h : handles) { 141 assertNull(String.valueOf(count++), h.resolve(info)); 142 } 143 } 144 }, true); 145 } 146 147 } 148 | Popular Tags |