Added
Link Here
|
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-2007 Sun |
17 |
* Microsystems, Inc. All Rights Reserved. |
18 |
*/ |
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 junit.framework.Assert; |
29 |
import org.netbeans.api.java.classpath.ClassPath; |
30 |
import org.netbeans.api.java.queries.SourceForBinaryQuery.Result; |
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 |
import org.openide.util.Lookup; |
39 |
import org.openide.util.lookup.Lookups; |
40 |
import org.openide.util.lookup.ProxyLookup; |
41 |
|
42 |
/** |
43 |
* @author Tomas Zezula |
44 |
*/ |
45 |
public class BinaryForSourceQueryTest extends NbTestCase { |
46 |
|
47 |
private FileObject srcRoot1; |
48 |
private FileObject srcRoot2; |
49 |
private FileObject binaryRoot2; |
50 |
|
51 |
|
52 |
|
53 |
static { |
54 |
BinaryForSourceQueryTest.class.getClassLoader().setDefaultAssertionStatus(true); |
55 |
System.setProperty("org.openide.util.Lookup", BinaryForSourceQueryTest.Lkp.class.getName()); |
56 |
Assert.assertEquals(BinaryForSourceQueryTest.Lkp.class, Lookup.getDefault().getClass()); |
57 |
} |
58 |
|
59 |
public static class Lkp extends ProxyLookup { |
60 |
|
61 |
private static Lkp DEFAULT; |
62 |
|
63 |
public Lkp () { |
64 |
Assert.assertNull(DEFAULT); |
65 |
DEFAULT = this; |
66 |
ClassLoader l = Lkp.class.getClassLoader(); |
67 |
this.setLookups( |
68 |
new Lookup [] { |
69 |
Lookups.fixed (CPProvider.getDefault(), SFBQImpl.getDefault()), |
70 |
Lookups.metaInfServices(l), |
71 |
Lookups.singleton(l), |
72 |
}); |
73 |
} |
74 |
|
75 |
public void setLookupsWrapper(Lookup... l) { |
76 |
setLookups(l); |
77 |
} |
78 |
} |
79 |
|
80 |
|
81 |
public BinaryForSourceQueryTest (String n) { |
82 |
super(n); |
83 |
} |
84 |
|
85 |
@Override |
86 |
protected void setUp () throws IOException { |
87 |
this.clearWorkDir(); |
88 |
File wd = this.getWorkDir(); |
89 |
FileObject root = FileUtil.toFileObject(wd); |
90 |
assertNotNull(root); |
91 |
srcRoot1 = root.createFolder("src1"); |
92 |
assertNotNull(srcRoot1); |
93 |
srcRoot2 = root.createFolder("src2"); |
94 |
assertNotNull(srcRoot2); |
95 |
binaryRoot2 = root.createFolder("binary2"); |
96 |
assertNotNull(binaryRoot2); |
97 |
SFBQImpl.getDefault().register(srcRoot2.getURL(), binaryRoot2.getURL()); |
98 |
CPProvider.getDefault().register(srcRoot2, ClassPath.SOURCE, ClassPathSupport.createClassPath(new FileObject[] {srcRoot2})); |
99 |
CPProvider.getDefault().register(srcRoot2, ClassPath.EXECUTE, ClassPathSupport.createClassPath(new FileObject[] {binaryRoot2})); |
100 |
} |
101 |
|
102 |
public void testQuery() throws Exception { |
103 |
BinaryForSourceQuery.Result result = BinaryForSourceQuery.findBinaryRoots(srcRoot1.getURL()); |
104 |
assertEquals(0,result.getRoots().length); |
105 |
result = BinaryForSourceQuery.findBinaryRoots(srcRoot2.getURL()); |
106 |
assertEquals(1,result.getRoots().length); |
107 |
assertEquals(binaryRoot2.getURL(), result.getRoots()[0]); |
108 |
} |
109 |
|
110 |
private static class SFBQImpl implements SourceForBinaryQueryImplementation { |
111 |
|
112 |
private static SFBQImpl instance; |
113 |
|
114 |
private final Map<URL,URL> data = new HashMap<URL,URL>(); |
115 |
|
116 |
void register (URL source, URL binary) { |
117 |
data.put (binary,source); |
118 |
} |
119 |
|
120 |
public Result findSourceRoots(URL binaryRoot) { |
121 |
URL src = data.get (binaryRoot); |
122 |
if (src == null) { |
123 |
return null; |
124 |
} |
125 |
final FileObject fo = URLMapper.findFileObject(src); |
126 |
if (fo == null) { |
127 |
return null; |
128 |
} |
129 |
return new SourceForBinaryQuery.Result () { |
130 |
public FileObject[] getRoots() { |
131 |
return new FileObject[] {fo}; |
132 |
} |
133 |
public void addChangeListener (ChangeListener l) {} |
134 |
|
135 |
public void removeChangeListener (ChangeListener l) {} |
136 |
}; |
137 |
} |
138 |
|
139 |
static synchronized SFBQImpl getDefault () { |
140 |
if (instance == null) { |
141 |
instance = new SFBQImpl (); |
142 |
} |
143 |
return instance; |
144 |
} |
145 |
} |
146 |
|
147 |
private static class CPProvider implements ClassPathProvider { |
148 |
|
149 |
private static CPProvider instance; |
150 |
|
151 |
private final Map<FileObject,Map<String,ClassPath>> data = new HashMap<FileObject,Map<String,ClassPath>>(); |
152 |
|
153 |
void register (FileObject fo, String type, ClassPath cp) { |
154 |
Map<String,ClassPath> m = data.get (fo); |
155 |
if (m == null) { |
156 |
m = new HashMap<String,ClassPath>(); |
157 |
data.put (fo,m); |
158 |
} |
159 |
m.put (type,cp); |
160 |
} |
161 |
|
162 |
public ClassPath findClassPath(FileObject file, String type) { |
163 |
Map<String,ClassPath> m = data.get (file); |
164 |
if (m == null) { |
165 |
return null; |
166 |
} |
167 |
return m.get (type); |
168 |
} |
169 |
|
170 |
public static synchronized CPProvider getDefault () { |
171 |
if (instance == null) { |
172 |
instance = new CPProvider (); |
173 |
} |
174 |
return instance; |
175 |
} |
176 |
} |
177 |
|
178 |
|
179 |
} |