Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2011 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2011 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.modules.java.source.queries.spi; |
43 |
import java.io.File; |
44 |
import java.io.IOException; |
45 |
import java.io.OutputStreamWriter; |
46 |
import java.io.PrintWriter; |
47 |
import java.util.Arrays; |
48 |
import java.util.Collection; |
49 |
import java.util.Collections; |
50 |
import java.util.HashSet; |
51 |
import java.util.List; |
52 |
import java.util.Set; |
53 |
import org.netbeans.api.annotations.common.NonNull; |
54 |
import org.netbeans.api.annotations.common.NullAllowed; |
55 |
import org.netbeans.junit.NbTestCase; |
56 |
import org.netbeans.modules.java.source.queries.api.Function; |
57 |
import org.netbeans.modules.java.source.queries.api.Queries; |
58 |
import org.netbeans.modules.java.source.queries.api.QueryException; |
59 |
import org.netbeans.modules.java.source.queries.api.Updates; |
60 |
import org.openide.filesystems.FileLock; |
61 |
import org.openide.filesystems.FileObject; |
62 |
import org.openide.filesystems.FileUtil; |
63 |
import org.openide.util.Lookup; |
64 |
|
65 |
/** |
66 |
* |
67 |
* @author Tomas Zezula |
68 |
*/ |
69 |
public class ModelOperationsTest extends NbTestCase { |
70 |
|
71 |
protected static final String[] TEST_1 = { |
72 |
"org.me.test", |
73 |
"Test1", |
74 |
"package org.me.test;\n"+ |
75 |
"import java.util.Map;\n"+ |
76 |
"public class Test1{\n"+ |
77 |
" int a, b;\n"+ |
78 |
" float c\n;"+ |
79 |
" Test1 d\n;"+ |
80 |
" java.util.List e;\n"+ |
81 |
" Map<String,String> f;\n"+ |
82 |
"}" |
83 |
}; |
84 |
|
85 |
protected static final String[] TEST_2 = { |
86 |
"org.me.test", |
87 |
"Test2", |
88 |
"package org.me.test;\n"+ |
89 |
"public class Test2 extends Exception implements Runnable {\n"+ |
90 |
" public Test2(int a){}\n"+ |
91 |
" public void run(){}\n"+ |
92 |
"}\n"+ |
93 |
"class Test2Other implements Runnable,java.io.Serializable{\n" + |
94 |
" public void run(){}\n"+ |
95 |
"}" |
96 |
}; |
97 |
|
98 |
protected static final String[] TEST_3 = { |
99 |
"org.me.test", |
100 |
"Test3", |
101 |
"package org.me.test;\n"+ |
102 |
"public class Test3{\n"+ |
103 |
" Test3(){}\n"+ |
104 |
"}" |
105 |
}; |
106 |
|
107 |
protected static final String[] TEST_4 = { |
108 |
"org.me.test", |
109 |
"Test4", |
110 |
"package org.me.test;\n"+ |
111 |
"import java.util.Map;\n"+ |
112 |
"public class Test4{\n"+ |
113 |
" void m1() {}\n"+ |
114 |
" void m2(int a){}\n"+ |
115 |
" int m3(int a, int b){}\n"+ |
116 |
" java.util.List m4(Map<String,String>a, Map<String,String>b){}\n"+ |
117 |
" java.util.List<String> m5(Map<String,java.util.List<String>>a, String b){}\n"+ |
118 |
" void m6(int... a){}\n"+ |
119 |
" int m7(int... a){}\n" + |
120 |
"}" |
121 |
}; |
122 |
|
123 |
protected static final String[] TEST_5 = { |
124 |
"org.me.test", |
125 |
"Test5", |
126 |
"package org.me.test;\n"+ |
127 |
"import java.util.Map;\n"+ |
128 |
"public class Test5{\n"+ |
129 |
" void m1() {}\n"+ |
130 |
" @SuppressWarnings void m1(int a){}\n"+ |
131 |
" @SuppressWarnings int m2(int a, int b){}\n"+ |
132 |
"}" |
133 |
}; |
134 |
|
135 |
protected static final String[] TEST_6 = { |
136 |
"org.me.test", |
137 |
"Test6", |
138 |
"package org.me.test;\n"+ |
139 |
"public class Test6{\n"+ |
140 |
" int a;\n"+ |
141 |
"}" |
142 |
}; |
143 |
|
144 |
protected FileObject srcRoot; |
145 |
|
146 |
public ModelOperationsTest(String testName) { |
147 |
super(testName); |
148 |
} |
149 |
|
150 |
@Override |
151 |
protected void setUp() throws Exception { |
152 |
this.clearWorkDir(); |
153 |
srcRoot = FileUtil.createFolder(new File (getWorkDir(),"src")); |
154 |
} |
155 |
|
156 |
public void testGetTopLevelClasses() throws Exception { |
157 |
doTestGetTopLevelClasses(TEST_1, Arrays.asList("org.me.test.Test1")); |
158 |
doTestGetTopLevelClasses(TEST_2, Arrays.asList("org.me.test.Test2","org.me.test.Test2Other")); |
159 |
} |
160 |
|
161 |
private void doTestGetTopLevelClasses( |
162 |
@NonNull final String[] testCase, |
163 |
@NonNull final Collection<? extends String> expected) throws Exception { |
164 |
final FileObject fo1 = prepareTest( |
165 |
srcRoot, |
166 |
testCase[0], |
167 |
testCase[1], |
168 |
testCase[2]); |
169 |
final Collection<? extends String> tlc = Queries.query( |
170 |
fo1, |
171 |
new Function<Queries, Collection<? extends String>>(){ |
172 |
@Override |
173 |
public Collection<? extends String> apply(Queries param) throws QueryException { |
174 |
return param.getTopLevelClasses(); |
175 |
} |
176 |
}); |
177 |
assertContentEquals(expected,tlc); |
178 |
} |
179 |
|
180 |
public void testIsJavaBean() throws Exception { |
181 |
doTestIsJavaBean(TEST_1, "org.me.test.Test1", true); |
182 |
doTestIsJavaBean(TEST_2, "org.me.test.Test2", false); |
183 |
doTestIsJavaBean(TEST_2, "org.me.test.Test2Other", false); |
184 |
doTestIsJavaBean(TEST_3, "org.me.test.Test3", false); |
185 |
} |
186 |
|
187 |
private void doTestIsJavaBean( |
188 |
@NonNull final String[] testCase, |
189 |
@NonNull final String fqn, |
190 |
final boolean expected) throws Exception { |
191 |
final FileObject file = prepareTest( |
192 |
srcRoot, |
193 |
testCase[0], |
194 |
testCase[1], |
195 |
testCase[2]); |
196 |
final Boolean res = Queries.query( |
197 |
file, |
198 |
new Function<Queries, Boolean>(){ |
199 |
@Override |
200 |
public Boolean apply(Queries param) throws QueryException { |
201 |
return param.isJavaBean(fqn); |
202 |
} |
203 |
}); |
204 |
assertEquals(expected, res.booleanValue()); |
205 |
} |
206 |
|
207 |
public void testIsAvialable() throws Exception { |
208 |
doTestIsAvailable(TEST_1, "org.me.test.Test1", true); |
209 |
doTestIsAvailable(TEST_1, "java.lang.String", true); |
210 |
} |
211 |
|
212 |
private void doTestIsAvailable( |
213 |
@NonNull final String[] testCase, |
214 |
@NonNull final String fqn, |
215 |
final boolean expected) throws Exception { |
216 |
final FileObject file = prepareTest( |
217 |
srcRoot, |
218 |
testCase[0], |
219 |
testCase[1], |
220 |
testCase[2]); |
221 |
final Boolean res = Queries.query( |
222 |
file, |
223 |
new Function<Queries, Boolean>(){ |
224 |
@Override |
225 |
public Boolean apply(Queries param) throws QueryException { |
226 |
return param.isAvailable(fqn); |
227 |
} |
228 |
}); |
229 |
assertEquals(expected, res.booleanValue()); |
230 |
} |
231 |
|
232 |
public void testGetSuperClass() throws Exception { |
233 |
doTestGetSuperClass(TEST_1, "org.me.test.Test1", "java.lang.Object"); |
234 |
doTestGetSuperClass(TEST_2, "org.me.test.Test2", "java.lang.Exception"); |
235 |
doTestGetSuperClass(TEST_3, "java.lang.RuntimeException", "java.lang.Exception"); |
236 |
doTestGetSuperClass(TEST_3, "java.lang.Object", null); |
237 |
} |
238 |
|
239 |
private void doTestGetSuperClass( |
240 |
@NonNull final String[] testCase, |
241 |
@NonNull final String fqn, |
242 |
@NonNull final String expected) throws Exception { |
243 |
final FileObject file = prepareTest( |
244 |
srcRoot, |
245 |
testCase[0], |
246 |
testCase[1], |
247 |
testCase[2]); |
248 |
final String res = Queries.query( |
249 |
file, |
250 |
new Function<Queries, String>(){ |
251 |
@Override |
252 |
public String apply(Queries param) throws QueryException { |
253 |
return param.getSuperClass(fqn); |
254 |
} |
255 |
}); |
256 |
assertEquals(expected, res); |
257 |
} |
258 |
|
259 |
public void testGetInterfaces() throws Exception { |
260 |
doTestGetInterfaces(TEST_1, "org.me.test.Test1", Collections.<String>emptyList()); |
261 |
doTestGetInterfaces(TEST_2, "org.me.test.Test2", Collections.singleton("java.lang.Runnable")); |
262 |
doTestGetInterfaces(TEST_2, "org.me.test.Test2Other", |
263 |
Arrays.asList("java.lang.Runnable","java.io.Serializable")); |
264 |
} |
265 |
|
266 |
private void doTestGetInterfaces( |
267 |
@NonNull final String[] testCase, |
268 |
@NonNull final String fqn, |
269 |
@NonNull final Collection< ? extends String> expected) throws Exception { |
270 |
final FileObject file = prepareTest( |
271 |
srcRoot, |
272 |
testCase[0], |
273 |
testCase[1], |
274 |
testCase[2]); |
275 |
final Collection< ? extends String> res = Queries.query( |
276 |
file, |
277 |
new Function<Queries, Collection<? extends String>>(){ |
278 |
@Override |
279 |
public Collection<? extends String> apply(Queries param) throws QueryException { |
280 |
return param.getInterfaces(fqn); |
281 |
} |
282 |
}); |
283 |
assertContentEquals(expected, res); |
284 |
} |
285 |
|
286 |
public void testGetBinaryName() throws Exception { |
287 |
doTestGetBinaryName(TEST_1,"org.me.test.Test1","org.me.test.Test1"); |
288 |
doTestGetBinaryName(TEST_2,"org.me.test.Test2Other","org.me.test.Test2Other"); |
289 |
doTestGetBinaryName(TEST_2,"java.util.Map","java.util.Map"); |
290 |
doTestGetBinaryName(TEST_2,"java.util.Map.Entry","java.util.Map$Entry"); |
291 |
} |
292 |
|
293 |
private void doTestGetBinaryName( |
294 |
@NonNull final String[] testCase, |
295 |
@NonNull final String fqn, |
296 |
@NonNull final String expected) throws Exception { |
297 |
final FileObject file = prepareTest( |
298 |
srcRoot, |
299 |
testCase[0], |
300 |
testCase[1], |
301 |
testCase[2]); |
302 |
final String res = Queries.query( |
303 |
file, |
304 |
new Function<Queries, String>(){ |
305 |
@Override |
306 |
public String apply(Queries param) throws QueryException { |
307 |
return param.getClassBinaryName(fqn); |
308 |
} |
309 |
}); |
310 |
assertEquals(expected, res); |
311 |
} |
312 |
|
313 |
public void testGetFieldNames() throws Exception { |
314 |
doTestGetFieldNames(TEST_1,"org.me.test.Test1", false, "org.me.test.Test1", Collections.singletonList("d")); |
315 |
doTestGetFieldNames(TEST_1,"org.me.test.Test1", false, "float", Collections.singletonList("c")); |
316 |
doTestGetFieldNames(TEST_1,"org.me.test.Test1", false, "int", Arrays.asList("a","b")); |
317 |
doTestGetFieldNames(TEST_1,"org.me.test.Test1", false, "java.util.List", Collections.singletonList("e")); |
318 |
doTestGetFieldNames(TEST_1,"org.me.test.Test1", true, "java.util.List", Collections.singletonList("e")); |
319 |
doTestGetFieldNames(TEST_1,"org.me.test.Test1", false, "java.util.List<java.lang.String>", Collections.<String>emptyList()); |
320 |
doTestGetFieldNames(TEST_1,"org.me.test.Test1", true, "java.util.List<java.lang.String>", Collections.singletonList("e")); |
321 |
doTestGetFieldNames(TEST_1,"org.me.test.Test1", false, "java.util.Map", Collections.<String>emptyList()); |
322 |
doTestGetFieldNames(TEST_1,"org.me.test.Test1", true, "java.util.Map", Collections.singletonList("f")); |
323 |
doTestGetFieldNames(TEST_1,"org.me.test.Test1", false, "java.util.Map<java.lang.String,java.lang.String>", Collections.singletonList("f")); |
324 |
doTestGetFieldNames(TEST_1,"org.me.test.Test1", false, null, Arrays.asList("a","b","c","d","e","f")); |
325 |
} |
326 |
|
327 |
private void doTestGetFieldNames( |
328 |
@NonNull final String[] testCase, |
329 |
@NonNull final String fqn, |
330 |
final boolean useRT, |
331 |
@NonNull final String type, |
332 |
@NonNull final Collection<? extends String> expected) throws Exception { |
333 |
final FileObject file = prepareTest( |
334 |
srcRoot, |
335 |
testCase[0], |
336 |
testCase[1], |
337 |
testCase[2]); |
338 |
final Collection<? extends String> res = Queries.query( |
339 |
file, |
340 |
new Function<Queries, Collection<? extends String>>(){ |
341 |
@Override |
342 |
public Collection< ? extends String> apply(Queries param) throws QueryException { |
343 |
return param.getFieldNames(fqn, useRT, type); |
344 |
} |
345 |
}); |
346 |
assertContentEquals(expected, res); |
347 |
} |
348 |
|
349 |
public void testGetMethodNames() throws Exception { |
350 |
doTestGetMethodNames(TEST_4,"org.me.test.Test4", false, |
351 |
"void", |
352 |
Collections.<String>emptyList(), |
353 |
Collections.singletonList("m1")); |
354 |
doTestGetMethodNames(TEST_4,"org.me.test.Test4", false, |
355 |
"void", |
356 |
Collections.<String>singletonList("int"), |
357 |
Collections.singletonList("m2")); |
358 |
doTestGetMethodNames(TEST_4,"org.me.test.Test4", false, |
359 |
"int", |
360 |
Arrays.asList(new String[]{"int","int"}), |
361 |
Collections.singletonList("m3")); |
362 |
doTestGetMethodNames(TEST_4,"org.me.test.Test4", true, |
363 |
"java.util.List", |
364 |
Arrays.asList("java.util.Map","java.util.Map"), |
365 |
Collections.singletonList("m4")); |
366 |
doTestGetMethodNames(TEST_4,"org.me.test.Test4", false, |
367 |
"java.util.List", |
368 |
Arrays.asList("java.util.Map","java.util.Map"), |
369 |
Collections.<String>emptyList()); |
370 |
doTestGetMethodNames(TEST_4,"org.me.test.Test4", false, |
371 |
"java.util.List<java.lang.String>", |
372 |
Arrays.asList("java.util.Map<java.lang.String,java.lang.String>","java.util.Map<java.lang.String,java.lang.String>"), |
373 |
Collections.<String>emptyList()); |
374 |
doTestGetMethodNames(TEST_4,"org.me.test.Test4", false, |
375 |
"java.util.List", |
376 |
Arrays.asList("java.util.Map<java.lang.String,java.lang.String>","java.util.Map<java.lang.String,java.lang.String>"), |
377 |
Collections.singletonList("m4")); |
378 |
doTestGetMethodNames(TEST_4,"org.me.test.Test4", true, |
379 |
"java.util.List", |
380 |
Arrays.asList("java.util.Map","java.lang.String"), |
381 |
Collections.singletonList("m5")); |
382 |
doTestGetMethodNames(TEST_4,"org.me.test.Test4", false, |
383 |
"java.util.List", |
384 |
Arrays.asList("java.util.Map","java.lang.String"), |
385 |
Collections.<String>emptyList()); |
386 |
doTestGetMethodNames(TEST_4,"org.me.test.Test4", false, |
387 |
"java.util.List", |
388 |
Arrays.asList("java.util.Map<java.lang.String,java.util.List<java.lang.String>>","java.lang.String"), |
389 |
Collections.<String>emptyList()); |
390 |
doTestGetMethodNames(TEST_4,"org.me.test.Test4", false, |
391 |
"java.util.List<java.lang.String>", |
392 |
Arrays.asList("java.util.Map","java.lang.String"), |
393 |
Collections.<String>emptyList()); |
394 |
doTestGetMethodNames(TEST_4,"org.me.test.Test4", false, |
395 |
"java.util.List<java.lang.String>", |
396 |
Arrays.asList("java.util.Map<java.lang.String,java.util.List<java.lang.String>>","java.lang.String"), |
397 |
Collections.singletonList("m5")); |
398 |
doTestGetMethodNames(TEST_4,"org.me.test.Test4", false, |
399 |
"void", |
400 |
Collections.singletonList("int[]"), |
401 |
Collections.singletonList("m6")); |
402 |
doTestGetMethodNames(TEST_4,"org.me.test.Test4", false, |
403 |
null, |
404 |
Collections.singletonList("int[]"), |
405 |
Arrays.asList("m6","m7")); |
406 |
doTestGetMethodNames(TEST_4,"org.me.test.Test4", false, |
407 |
"void", |
408 |
null, |
409 |
Arrays.asList("m1","m2","m6")); |
410 |
doTestGetMethodNames(TEST_4,"org.me.test.Test4", false, |
411 |
null, |
412 |
null, |
413 |
Arrays.asList("m1","m2","m3","m4","m5","m6","m7")); |
414 |
} |
415 |
|
416 |
private void doTestGetMethodNames( |
417 |
@NonNull final String[] testCase, |
418 |
@NonNull final String fqn, |
419 |
final boolean useRT, |
420 |
@NonNull final String retType, |
421 |
@NonNull final List<? extends String> paramTypes, |
422 |
@NonNull final Collection<? extends String> expected) throws Exception { |
423 |
final FileObject file = prepareTest( |
424 |
srcRoot, |
425 |
testCase[0], |
426 |
testCase[1], |
427 |
testCase[2]); |
428 |
final Collection<? extends String> res = Queries.query( |
429 |
file, |
430 |
new Function<Queries, Collection<? extends String>>(){ |
431 |
@Override |
432 |
public Collection< ? extends String> apply(Queries param) throws QueryException { |
433 |
return param.getMethodNames( |
434 |
fqn, |
435 |
useRT, |
436 |
retType, |
437 |
paramTypes == null ? null : paramTypes.toArray(new String[paramTypes.size()])); |
438 |
} |
439 |
}); |
440 |
assertContentEquals(expected, res); |
441 |
} |
442 |
|
443 |
public void testGetMethodAnnotations() throws Exception { |
444 |
doTestGetMethodAnnotations(TEST_5,"org.me.test.Test5","m1",false, |
445 |
"void", |
446 |
Collections.<String>emptyList(), |
447 |
Collections.<String>emptyList()); |
448 |
doTestGetMethodAnnotations(TEST_5,"org.me.test.Test5", "m1",false, |
449 |
"void", |
450 |
Collections.singletonList("int"), |
451 |
Collections.singletonList("java.lang.SuppressWarnings")); |
452 |
doTestGetMethodAnnotations(TEST_5,"org.me.test.Test5", "m2",false, |
453 |
"int", |
454 |
Arrays.asList("int","int"), |
455 |
Collections.singletonList("java.lang.SuppressWarnings")); |
456 |
} |
457 |
|
458 |
private void doTestGetMethodAnnotations( |
459 |
@NonNull final String[] testCase, |
460 |
@NonNull final String fqn, |
461 |
@NonNull final String methodName, |
462 |
final boolean useRT, |
463 |
@NonNull final String retType, |
464 |
@NonNull final List<? extends String> paramTypes, |
465 |
@NonNull final Collection<? extends String> expected) throws Exception { |
466 |
final FileObject file = prepareTest( |
467 |
srcRoot, |
468 |
testCase[0], |
469 |
testCase[1], |
470 |
testCase[2]); |
471 |
final Collection<? extends String> res = Queries.query( |
472 |
file, |
473 |
new Function<Queries, Collection<? extends String>>(){ |
474 |
@Override |
475 |
public Collection< ? extends String> apply(Queries param) throws QueryException { |
476 |
return param.getMethodAnnotations( |
477 |
fqn, |
478 |
methodName, |
479 |
useRT, |
480 |
retType, |
481 |
paramTypes == null ? null : paramTypes.toArray(new String[paramTypes.size()])); |
482 |
} |
483 |
}); |
484 |
assertContentEquals(expected, res); |
485 |
} |
486 |
|
487 |
public void testModifyInterfaces() throws Exception { |
488 |
doTestModifyInterfaces(TEST_5,"org.me.test.Test5", |
489 |
Collections.singletonList("java.lang.Runnable"), |
490 |
Collections.<String>emptySet(), |
491 |
Collections.singletonList("java.lang.Runnable")); |
492 |
doTestModifyInterfaces(TEST_5,"org.me.test.Test5", |
493 |
Collections.singletonList("java.io.Serializable"), |
494 |
Collections.<String>emptySet(), |
495 |
Arrays.asList("java.lang.Runnable","java.io.Serializable")); |
496 |
doTestModifyInterfaces(TEST_5,"org.me.test.Test5", |
497 |
Collections.singletonList("java.io.Externalizable"), |
498 |
Collections.singletonList("java.io.Serializable"), |
499 |
Arrays.asList("java.lang.Runnable","java.io.Externalizable")); |
500 |
doTestModifyInterfaces(TEST_5,"org.me.test.Test5", |
501 |
Collections.<String>emptySet(), |
502 |
Arrays.asList("java.lang.Runnable","java.io.Externalizable"), |
503 |
Collections.<String>emptySet()); |
504 |
} |
505 |
|
506 |
private void doTestModifyInterfaces( |
507 |
@NonNull final String[] testCase, |
508 |
@NonNull final String clz, |
509 |
@NonNull final Collection<? extends String> toAdd, |
510 |
@NonNull final Collection<? extends String> toRemove, |
511 |
@NonNull final Collection<? extends String> expected |
512 |
) throws Exception { |
513 |
final FileObject file = prepareTest( |
514 |
srcRoot, |
515 |
testCase[0], |
516 |
testCase[1], |
517 |
testCase[2]); |
518 |
Updates.update( |
519 |
file, |
520 |
new Function<Updates, Boolean>(){ |
521 |
@Override |
522 |
public Boolean apply(final Updates param) throws QueryException { |
523 |
param.modifyInterfaces( |
524 |
clz, |
525 |
toAdd, |
526 |
toRemove); |
527 |
return true; |
528 |
} |
529 |
}); |
530 |
final Collection< ? extends String> res = Queries.query( |
531 |
file, |
532 |
new Function<Queries, Collection<? extends String>>(){ |
533 |
@Override |
534 |
public Collection<? extends String> apply(Queries param) throws QueryException { |
535 |
return param.getInterfaces(clz); |
536 |
} |
537 |
}); |
538 |
assertContentEquals(expected, res); |
539 |
} |
540 |
|
541 |
public void testSetSuperClass() throws Exception { |
542 |
doTestSetSuperClass(TEST_5,"org.me.test.Test5", |
543 |
"java.util.ArrayList", |
544 |
"java.util.ArrayList"); |
545 |
doTestSetSuperClass(TEST_5,"org.me.test.Test5", |
546 |
"java.lang.Object", |
547 |
"java.lang.Object"); |
548 |
} |
549 |
|
550 |
private void doTestSetSuperClass( |
551 |
@NonNull final String[] testCase, |
552 |
@NonNull final String clz, |
553 |
@NonNull final String superClz, |
554 |
@NonNull final String expected) throws Exception { |
555 |
final FileObject file = prepareTest( |
556 |
srcRoot, |
557 |
testCase[0], |
558 |
testCase[1], |
559 |
testCase[2]); |
560 |
Updates.update( |
561 |
file, |
562 |
new Function<Updates, Boolean>(){ |
563 |
@Override |
564 |
public Boolean apply(final Updates param) throws QueryException { |
565 |
param.setSuperClass( |
566 |
clz, |
567 |
superClz); |
568 |
return true; |
569 |
} |
570 |
}); |
571 |
final String res = Queries.query( |
572 |
file, |
573 |
new Function<Queries, String>(){ |
574 |
@Override |
575 |
public String apply(Queries param) throws QueryException { |
576 |
return param.getSuperClass(clz); |
577 |
} |
578 |
}); |
579 |
assertEquals(expected, res); |
580 |
} |
581 |
|
582 |
public void testModifyMethodAnnotations() throws Exception { |
583 |
doTestModifyMethodAnnotations(TEST_5,"org.me.test.Test5","m1",false, |
584 |
"void", |
585 |
Collections.<String>emptyList(), |
586 |
Collections.singleton("java.lang.Override"), |
587 |
Collections.<String>emptyList(), |
588 |
Collections.singleton("java.lang.Override")); |
589 |
doTestModifyMethodAnnotations(TEST_5,"org.me.test.Test5","m1",false, |
590 |
"void", |
591 |
Collections.<String>emptyList(), |
592 |
Collections.singleton("java.lang.SuppressWarnings"), |
593 |
Collections.<String>emptyList(), |
594 |
Arrays.asList("java.lang.Override","java.lang.SuppressWarnings")); |
595 |
doTestModifyMethodAnnotations(TEST_5,"org.me.test.Test5","m1",false, |
596 |
"void", |
597 |
Collections.<String>emptyList(), |
598 |
Collections.<String>emptyList(), |
599 |
Collections.singleton("java.lang.SuppressWarnings"), |
600 |
Collections.singleton("java.lang.Override")); |
601 |
doTestModifyMethodAnnotations(TEST_5,"org.me.test.Test5","m1",false, |
602 |
"void", |
603 |
Collections.<String>emptyList(), |
604 |
Collections.<String>emptyList(), |
605 |
Collections.singleton("java.lang.Override"), |
606 |
Collections.<String>emptySet()); |
607 |
doTestModifyMethodAnnotations(TEST_5,"org.me.test.Test5","m1",false, |
608 |
"void", |
609 |
Collections.<String>emptyList(), |
610 |
Arrays.asList("java.lang.Override","java.lang.SuppressWarnings"), |
611 |
Collections.<String>emptyList(), |
612 |
Arrays.asList("java.lang.Override","java.lang.SuppressWarnings")); |
613 |
doTestModifyMethodAnnotations(TEST_5,"org.me.test.Test5","m1",false, |
614 |
"void", |
615 |
Collections.<String>emptyList(), |
616 |
Collections.<String>emptyList(), |
617 |
Arrays.asList("java.lang.Override","java.lang.SuppressWarnings"), |
618 |
Collections.<String>emptySet()); |
619 |
} |
620 |
|
621 |
private void doTestModifyMethodAnnotations( |
622 |
@NonNull final String[] testCase, |
623 |
@NonNull final String fqn, |
624 |
@NonNull final String methodName, |
625 |
final boolean useRT, |
626 |
@NonNull final String retType, |
627 |
@NonNull final List<? extends String> paramTypes, |
628 |
@NonNull final Collection<? extends String> toAdd, |
629 |
@NonNull final Collection<? extends String> toRemove, |
630 |
@NonNull final Collection<? extends String> expected) throws Exception { |
631 |
final FileObject file = prepareTest( |
632 |
srcRoot, |
633 |
testCase[0], |
634 |
testCase[1], |
635 |
testCase[2]); |
636 |
Updates.update( |
637 |
file, |
638 |
new Function<Updates, Boolean>(){ |
639 |
@Override |
640 |
public Boolean apply(final Updates param) throws QueryException { |
641 |
param.modifyMethodAnnotations( |
642 |
fqn, |
643 |
methodName, |
644 |
useRT, |
645 |
retType, |
646 |
paramTypes.toArray(new String[paramTypes.size()]), |
647 |
toAdd, |
648 |
toRemove); |
649 |
return true; |
650 |
} |
651 |
}); |
652 |
final Collection<? extends String> res = Queries.query( |
653 |
file, |
654 |
new Function<Queries, Collection<? extends String>>(){ |
655 |
@Override |
656 |
public Collection<? extends String> apply(Queries param) throws QueryException { |
657 |
return param.getMethodAnnotations( |
658 |
fqn, |
659 |
methodName, |
660 |
useRT, |
661 |
retType, |
662 |
paramTypes.toArray(new String[paramTypes.size()])); |
663 |
} |
664 |
}); |
665 |
assertContentEquals(expected, res); |
666 |
} |
667 |
|
668 |
public void testRenameField() throws Exception { |
669 |
doTestRenameField(TEST_6,"org.me.test.Test6", |
670 |
"a", |
671 |
"b"); |
672 |
} |
673 |
|
674 |
private void doTestRenameField( |
675 |
@NonNull final String[] testCase, |
676 |
@NonNull final String fqn, |
677 |
@NonNull final String oldFieldName, |
678 |
@NonNull final String newFieldName) throws Exception { |
679 |
final FileObject file = prepareTest( |
680 |
srcRoot, |
681 |
testCase[0], |
682 |
testCase[1], |
683 |
testCase[2]); |
684 |
Updates.update( |
685 |
file, |
686 |
new Function<Updates, Boolean>(){ |
687 |
@Override |
688 |
public Boolean apply(final Updates param) throws QueryException { |
689 |
param.renameField( |
690 |
fqn, |
691 |
oldFieldName, |
692 |
newFieldName); |
693 |
return true; |
694 |
} |
695 |
}); |
696 |
final Collection<? extends String> res = Queries.query( |
697 |
file, |
698 |
new Function<Queries, Collection<? extends String>>(){ |
699 |
@Override |
700 |
public Collection<? extends String> apply(Queries param) throws QueryException { |
701 |
return param.getFieldNames(fqn, true, null); |
702 |
} |
703 |
}); |
704 |
assertContentEquals(Collections.singleton(newFieldName), res); |
705 |
} |
706 |
|
707 |
protected final <T> void assertContentEquals( |
708 |
@NullAllowed final Collection<? extends T> expected, |
709 |
@NullAllowed final Collection<? extends T> result) { |
710 |
if (expected == null) { |
711 |
assertNull("Expected null but got: " + result ,result); |
712 |
} else { |
713 |
final Set<T> e = new HashSet<T>(expected); |
714 |
for (T r : result) { |
715 |
if (!e.remove(r)) { |
716 |
throw new AssertionError("Expected: " + expected +" got:" + result); |
717 |
} |
718 |
} |
719 |
if (!e.isEmpty()) { |
720 |
throw new AssertionError("Expected: " + expected +" got:" + result); |
721 |
} |
722 |
} |
723 |
} |
724 |
|
725 |
protected final FileObject prepareTest( |
726 |
@NonNull final FileObject root, |
727 |
@NonNull final String pkg, |
728 |
@NonNull final String name, |
729 |
@NonNull final String content) throws IOException { |
730 |
if (Lookup.getDefault().lookup(QueriesController.class) == null) { |
731 |
throw new IllegalStateException("Run the ModelOperationsTest subclass in impl module."); |
732 |
} |
733 |
assert root != null; |
734 |
assert pkg != null; |
735 |
assert name != null; |
736 |
assert content != null; |
737 |
final String fileName = String.format("%s/%s.java", |
738 |
pkg.replace('.', '/'), |
739 |
name); |
740 |
FileObject fo = root.getFileObject(fileName); |
741 |
if (fo != null) { |
742 |
return fo; |
743 |
} |
744 |
fo = FileUtil.createData( |
745 |
srcRoot, |
746 |
fileName); |
747 |
final FileLock lock = fo.lock(); |
748 |
try { |
749 |
final PrintWriter out = new PrintWriter (new OutputStreamWriter(fo.getOutputStream(lock))); |
750 |
try { |
751 |
out.print(content); |
752 |
} finally { |
753 |
out.close(); |
754 |
} |
755 |
} finally { |
756 |
lock.releaseLock(); |
757 |
} |
758 |
return fo; |
759 |
} |
760 |
} |