1 19 20 package org.netbeans.api.java.queries; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.net.URL ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 import javax.swing.event.ChangeListener ; 28 import org.netbeans.api.java.classpath.ClassPath; 29 import org.netbeans.api.java.queries.SourceForBinaryQuery.Result; 30 import org.netbeans.junit.MockServices; 31 import org.netbeans.junit.NbTestCase; 32 import org.netbeans.spi.java.classpath.ClassPathProvider; 33 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 34 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation; 35 import org.openide.filesystems.FileObject; 36 import org.openide.filesystems.FileUtil; 37 import org.openide.filesystems.URLMapper; 38 39 42 public class BinaryForSourceQueryTest extends NbTestCase { 43 44 private FileObject srcRoot1; 45 private FileObject srcRoot2; 46 private FileObject binaryRoot2; 47 48 49 public BinaryForSourceQueryTest (String n) { 50 super(n); 51 } 52 53 @Override 54 protected void setUp () throws IOException { 55 MockServices.setServices(new Class [] {CPProvider.class, SFBQImpl.class}); 56 this.clearWorkDir(); 57 File wd = this.getWorkDir(); 58 FileObject root = FileUtil.toFileObject(wd); 59 assertNotNull(root); 60 srcRoot1 = root.createFolder("src1"); 61 assertNotNull(srcRoot1); 62 srcRoot2 = root.createFolder("src2"); 63 assertNotNull(srcRoot2); 64 binaryRoot2 = root.createFolder("binary2"); 65 assertNotNull(binaryRoot2); 66 SFBQImpl.clear(); 67 CPProvider.clear(); 68 SFBQImpl.register(srcRoot2.getURL(), binaryRoot2.getURL()); 69 CPProvider.register(srcRoot2, ClassPath.SOURCE, ClassPathSupport.createClassPath(new FileObject[] {srcRoot2})); 70 CPProvider.register(srcRoot2, ClassPath.EXECUTE, ClassPathSupport.createClassPath(new FileObject[] {binaryRoot2})); 71 } 72 73 public void testQuery() throws Exception { 74 BinaryForSourceQuery.Result result = BinaryForSourceQuery.findBinaryRoots(srcRoot1.getURL()); 75 assertEquals(0,result.getRoots().length); 76 result = BinaryForSourceQuery.findBinaryRoots(srcRoot2.getURL()); 77 assertEquals(1,result.getRoots().length); 78 assertEquals(binaryRoot2.getURL(), result.getRoots()[0]); 79 } 80 81 public static class SFBQImpl implements SourceForBinaryQueryImplementation { 82 83 private static final Map <URL ,URL > data = new HashMap <URL ,URL >(); 84 85 static void clear () { 86 data.clear(); 87 } 88 89 static void register (URL source, URL binary) { 90 data.put (binary,source); 91 } 92 93 public Result findSourceRoots(URL binaryRoot) { 94 URL src = data.get (binaryRoot); 95 if (src == null) { 96 return null; 97 } 98 final FileObject fo = URLMapper.findFileObject(src); 99 if (fo == null) { 100 return null; 101 } 102 return new SourceForBinaryQuery.Result () { 103 public FileObject[] getRoots() { 104 return new FileObject[] {fo}; 105 } 106 public void addChangeListener (ChangeListener l) {} 107 108 public void removeChangeListener (ChangeListener l) {} 109 }; 110 } 111 } 112 113 public static class CPProvider implements ClassPathProvider { 114 115 116 private static final Map <FileObject,Map <String ,ClassPath>> data = new HashMap <FileObject,Map <String ,ClassPath>>(); 117 118 static void clear () { 119 data.clear(); 120 } 121 122 static void register (FileObject fo, String type, ClassPath cp) { 123 Map <String ,ClassPath> m = data.get (fo); 124 if (m == null) { 125 m = new HashMap <String ,ClassPath>(); 126 data.put (fo,m); 127 } 128 m.put (type,cp); 129 } 130 131 public ClassPath findClassPath(FileObject file, String type) { 132 Map <String ,ClassPath> m = data.get (file); 133 if (m == null) { 134 return null; 135 } 136 return m.get (type); 137 } 138 } 139 140 141 } 142 | Popular Tags |