Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2008 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* If you wish your version of this file to be governed by only the CDDL |
25 |
* or only the GPL Version 2, indicate your decision by adding |
26 |
* "[Contributor] elects to include this software in this distribution |
27 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
28 |
* single choice of license, a recipient has the option to distribute |
29 |
* your version of this file under either the CDDL, the GPL Version 2 or |
30 |
* to extend the choice of license to its licensees as provided above. |
31 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
32 |
* Version 2 license, then the option applies only if the new code is |
33 |
* made subject to such option by the copyright holder. |
34 |
* |
35 |
* Contributor(s): |
36 |
* |
37 |
* Portions Copyrighted 2008 Sun Microsystems, Inc. |
38 |
*/ |
39 |
|
40 |
package org.netbeans.api.java.queries; |
41 |
|
42 |
import java.io.File; |
43 |
import java.io.FileOutputStream; |
44 |
import java.net.URL; |
45 |
import java.util.Arrays; |
46 |
import java.util.Collections; |
47 |
import java.util.HashMap; |
48 |
import java.util.List; |
49 |
import java.util.Map; |
50 |
import java.util.concurrent.atomic.AtomicBoolean; |
51 |
import java.util.zip.ZipEntry; |
52 |
import java.util.zip.ZipOutputStream; |
53 |
import javax.swing.event.ChangeEvent; |
54 |
import javax.swing.event.ChangeListener; |
55 |
import org.netbeans.api.java.queries.SourceForBinaryQuery.Result; |
56 |
import org.netbeans.junit.MockServices; |
57 |
import org.netbeans.junit.NbTestCase; |
58 |
import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation; |
59 |
import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation2; |
60 |
import org.netbeans.spi.java.queries.support.SourceForBinaryQueryimplementation2Base; |
61 |
import org.openide.filesystems.FileObject; |
62 |
import org.openide.filesystems.FileUtil; |
63 |
import org.openide.util.ChangeSupport; |
64 |
|
65 |
/** |
66 |
* |
67 |
* @author Tomas Zezula |
68 |
*/ |
69 |
public class SourceForBinaryQueryTest extends NbTestCase { |
70 |
|
71 |
public SourceForBinaryQueryTest (final String name) { |
72 |
super (name); |
73 |
} |
74 |
|
75 |
private FileObject br1; |
76 |
private FileObject br2; |
77 |
private FileObject sr1; |
78 |
private FileObject sr2; |
79 |
|
80 |
private static final Map<URL,List<FileObject>> map = new HashMap<URL, List<FileObject>>(); |
81 |
|
82 |
@Override |
83 |
protected void setUp() throws Exception { |
84 |
this.clearWorkDir(); |
85 |
super.setUp(); |
86 |
File wd = this.getWorkDir(); |
87 |
FileObject wdfo = FileUtil.toFileObject(wd); |
88 |
br1 = wdfo.createFolder("bin1"); |
89 |
br2 = wdfo.createFolder("bin2"); |
90 |
sr1 = wdfo.createFolder("src1"); |
91 |
File zf = new File (wd,"src2.zip"); |
92 |
ZipOutputStream zos = new ZipOutputStream (new FileOutputStream (zf)); |
93 |
zos.putNextEntry(new ZipEntry("foo.java")); |
94 |
zos.closeEntry(); |
95 |
zos.close(); |
96 |
sr2 = FileUtil.getArchiveRoot(FileUtil.toFileObject(zf)); |
97 |
map.put(br1.getURL(), Collections.singletonList(sr1)); |
98 |
map.put(br2.getURL(), Collections.singletonList(sr2)); |
99 |
} |
100 |
|
101 |
public void testSFBQImpl () throws Exception { |
102 |
MockServices.setServices(LegacySFBQImpl.class); |
103 |
SourceForBinaryQuery.Result2 res = SourceForBinaryQuery.findSourceRoots2(br1.getURL()); |
104 |
assertNotNull(res); |
105 |
assertEquals(1, res.getRoots().length); |
106 |
assertEquals(Collections.singletonList(sr1), Arrays.asList(res.getRoots())); |
107 |
assertTrue(res.preferSources()); |
108 |
|
109 |
res = SourceForBinaryQuery.findSourceRoots2(br2.getURL()); |
110 |
assertNotNull(res); |
111 |
assertEquals(1, res.getRoots().length); |
112 |
assertEquals(Collections.singletonList(sr2), Arrays.asList(res.getRoots())); |
113 |
assertFalse(res.preferSources()); |
114 |
|
115 |
res = SourceForBinaryQuery.findSourceRoots2(getWorkDir().toURI().toURL()); |
116 |
assertNotNull(res); |
117 |
assertEquals(0, res.getRoots().length); |
118 |
assertTrue(res.preferSources()); |
119 |
} |
120 |
|
121 |
public void testSFBQImpl2 () throws Exception { |
122 |
MockServices.setServices(LeafSFBQImpl.class); |
123 |
SourceForBinaryQuery.Result2 res = SourceForBinaryQuery.findSourceRoots2(br1.getURL()); |
124 |
assertNotNull(res); |
125 |
assertEquals(1, res.getRoots().length); |
126 |
assertEquals(Collections.singletonList(sr1), Arrays.asList(res.getRoots())); |
127 |
assertFalse(res.preferSources()); |
128 |
|
129 |
res = SourceForBinaryQuery.findSourceRoots2(br2.getURL()); |
130 |
assertNotNull(res); |
131 |
assertEquals(1, res.getRoots().length); |
132 |
assertEquals(Collections.singletonList(sr2), Arrays.asList(res.getRoots())); |
133 |
assertTrue(res.preferSources()); |
134 |
|
135 |
res = SourceForBinaryQuery.findSourceRoots2(getWorkDir().toURI().toURL()); |
136 |
assertNotNull(res); |
137 |
assertEquals(0, res.getRoots().length); |
138 |
assertTrue(res.preferSources()); |
139 |
|
140 |
} |
141 |
|
142 |
|
143 |
public void testSFBQDelegatingImpl () throws Exception { |
144 |
DelegatingSFBImpl.impl = new LegacySFBQImpl(); |
145 |
MockServices.setServices(DelegatingSFBImpl.class); |
146 |
SourceForBinaryQuery.Result2 res = SourceForBinaryQuery.findSourceRoots2(br1.getURL()); |
147 |
assertNotNull(res); |
148 |
assertEquals(1, res.getRoots().length); |
149 |
assertEquals(Collections.singletonList(sr1), Arrays.asList(res.getRoots())); |
150 |
assertTrue(res.preferSources()); |
151 |
|
152 |
res = SourceForBinaryQuery.findSourceRoots2(br2.getURL()); |
153 |
assertNotNull(res); |
154 |
assertEquals(1, res.getRoots().length); |
155 |
assertEquals(Collections.singletonList(sr2), Arrays.asList(res.getRoots())); |
156 |
assertFalse(res.preferSources()); |
157 |
|
158 |
res = SourceForBinaryQuery.findSourceRoots2(getWorkDir().toURI().toURL()); |
159 |
assertNotNull(res); |
160 |
assertEquals(0, res.getRoots().length); |
161 |
assertTrue(res.preferSources()); |
162 |
|
163 |
} |
164 |
|
165 |
|
166 |
public void testSFBQDelegatingImpl2 () throws Exception { |
167 |
DelegatingSFBImpl.impl = new LeafSFBQImpl(); |
168 |
MockServices.setServices(DelegatingSFBImpl.class); |
169 |
SourceForBinaryQuery.Result2 res = SourceForBinaryQuery.findSourceRoots2(br1.getURL()); |
170 |
assertNotNull(res); |
171 |
assertEquals(1, res.getRoots().length); |
172 |
assertEquals(Collections.singletonList(sr1), Arrays.asList(res.getRoots())); |
173 |
assertFalse(res.preferSources()); |
174 |
|
175 |
res = SourceForBinaryQuery.findSourceRoots2(br2.getURL()); |
176 |
assertNotNull(res); |
177 |
assertEquals(1, res.getRoots().length); |
178 |
assertEquals(Collections.singletonList(sr2), Arrays.asList(res.getRoots())); |
179 |
assertTrue(res.preferSources()); |
180 |
|
181 |
res = SourceForBinaryQuery.findSourceRoots2(getWorkDir().toURI().toURL()); |
182 |
assertNotNull(res); |
183 |
assertEquals(0, res.getRoots().length); |
184 |
assertTrue(res.preferSources()); |
185 |
} |
186 |
|
187 |
|
188 |
public void testListening () throws Exception { |
189 |
DelegatingSFBImpl.impl = new LeafSFBQImpl(); |
190 |
MockServices.setServices(DelegatingSFBImpl.class); |
191 |
SourceForBinaryQuery.Result2 res = SourceForBinaryQuery.findSourceRoots2(br1.getURL()); |
192 |
final AtomicBoolean fired = new AtomicBoolean (); |
193 |
ChangeListener l; |
194 |
res.addChangeListener( l = new ChangeListener() { |
195 |
public void stateChanged(ChangeEvent e) { |
196 |
fired.set(true); |
197 |
} |
198 |
}); |
199 |
((LeafSFBQImpl)DelegatingSFBImpl.impl).lastResult.fire(); |
200 |
res.removeChangeListener(l); |
201 |
assertTrue(fired.get()); |
202 |
} |
203 |
|
204 |
//Prefers sources for archives => behaves opposite to default |
205 |
public static class LeafSFBQImpl implements SourceForBinaryQueryImplementation2 { |
206 |
|
207 |
static R2 lastResult; |
208 |
|
209 |
public Result findSourceRoots2(URL binaryRoot) { |
210 |
lastResult = null; |
211 |
List<FileObject> data = map.get(binaryRoot); |
212 |
if (data != null) { |
213 |
return lastResult = new R2(data.toArray(new FileObject[data.size()]), prefSources(data)); |
214 |
} |
215 |
return null; |
216 |
} |
217 |
|
218 |
private static boolean prefSources(List<FileObject> l) { |
219 |
for (FileObject f : l ) { |
220 |
if (FileUtil.getArchiveFile(f) != null) { |
221 |
return true; |
222 |
} |
223 |
} |
224 |
return false; |
225 |
} |
226 |
|
227 |
public Result findSourceRoots(URL binaryRoot) { |
228 |
return this.findSourceRoots2(binaryRoot); |
229 |
} |
230 |
|
231 |
} |
232 |
|
233 |
public static class DelegatingSFBImpl extends SourceForBinaryQueryimplementation2Base { |
234 |
|
235 |
private static SourceForBinaryQueryImplementation impl; |
236 |
|
237 |
|
238 |
public Result findSourceRoots2(URL binaryRoot) { |
239 |
if (this.impl == null) { |
240 |
throw new IllegalStateException (); |
241 |
} |
242 |
else if (this.impl instanceof SourceForBinaryQueryImplementation2) { |
243 |
return ((SourceForBinaryQueryImplementation2)this.impl).findSourceRoots2(binaryRoot); |
244 |
} |
245 |
else { |
246 |
return asResult(this.impl.findSourceRoots(binaryRoot)); |
247 |
} |
248 |
} |
249 |
|
250 |
public Result findSourceRoots(URL binaryRoot) { |
251 |
return this.findSourceRoots2(binaryRoot); |
252 |
} |
253 |
|
254 |
} |
255 |
|
256 |
public static class LegacySFBQImpl implements SourceForBinaryQueryImplementation { |
257 |
|
258 |
public Result findSourceRoots(URL binaryRoot) { |
259 |
List<FileObject> data = map.get(binaryRoot); |
260 |
if (data != null) { |
261 |
return new R(data.toArray(new FileObject[data.size()])); |
262 |
} |
263 |
return null; |
264 |
} |
265 |
|
266 |
} |
267 |
|
268 |
private static class R implements SourceForBinaryQuery.Result { |
269 |
|
270 |
final FileObject[] roots; |
271 |
private final ChangeSupport chs = new ChangeSupport(this); |
272 |
|
273 |
public R (final FileObject[] roots) { |
274 |
this.roots = roots; |
275 |
} |
276 |
|
277 |
public FileObject[] getRoots() { |
278 |
return this.roots; |
279 |
} |
280 |
|
281 |
public void addChangeListener(ChangeListener l) { |
282 |
chs.addChangeListener(l); |
283 |
} |
284 |
|
285 |
public void removeChangeListener(ChangeListener l) { |
286 |
chs.removeChangeListener(l); |
287 |
} |
288 |
|
289 |
void fire () { |
290 |
chs.fireChange(); |
291 |
} |
292 |
|
293 |
} |
294 |
|
295 |
private static class R2 extends R implements SourceForBinaryQueryImplementation2.Result { |
296 |
|
297 |
final boolean ps; |
298 |
|
299 |
public R2 (final FileObject[] roots, final boolean ps) { |
300 |
super (roots); |
301 |
this.ps = ps; |
302 |
} |
303 |
|
304 |
public boolean preferSources() { |
305 |
return ps; |
306 |
} |
307 |
} |
308 |
|
309 |
} |