Added
Link Here
|
1 |
/****************************************************************************** |
2 |
* Copyright (c) 2006 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
10 |
****************************************************************************/ |
11 |
|
12 |
package org.eclipse.gmf.tests.runtime.emf.core; |
13 |
|
14 |
import java.util.ArrayList; |
15 |
import java.util.Collection; |
16 |
import java.util.Iterator; |
17 |
import java.util.List; |
18 |
|
19 |
import org.eclipse.emf.common.notify.Notifier; |
20 |
import org.eclipse.emf.common.util.TreeIterator; |
21 |
import org.eclipse.emf.ecore.EObject; |
22 |
import org.eclipse.emf.ecore.EReference; |
23 |
import org.eclipse.emf.ecore.resource.Resource; |
24 |
import org.eclipse.emf.ecore.resource.ResourceSet; |
25 |
import org.eclipse.emf.ecore.util.ECrossReferenceAdapter; |
26 |
import org.eclipse.emf.examples.extlibrary.Book; |
27 |
import org.eclipse.emf.examples.extlibrary.EXTLibraryFactory; |
28 |
import org.eclipse.emf.examples.extlibrary.EXTLibraryPackage; |
29 |
import org.eclipse.emf.examples.extlibrary.Employee; |
30 |
import org.eclipse.emf.examples.extlibrary.Library; |
31 |
import org.eclipse.emf.examples.extlibrary.Writer; |
32 |
import org.eclipse.gmf.runtime.emf.core.edit.MRunnable; |
33 |
import org.eclipse.gmf.runtime.emf.core.edit.MUndoInterval; |
34 |
import org.eclipse.gmf.runtime.emf.core.exceptions.MSLActionAbandonedException; |
35 |
import org.eclipse.gmf.runtime.emf.core.internal.domain.MSLEditingDomain; |
36 |
import org.eclipse.gmf.runtime.emf.core.internal.index.MSLCrossReferenceAdapter; |
37 |
import org.eclipse.gmf.runtime.emf.core.util.EObjectUtil; |
38 |
import org.eclipse.gmf.runtime.emf.core.util.ResourceUtil; |
39 |
|
40 |
/** |
41 |
* Test cases for the MSLCrossReferenceAdapter. |
42 |
* |
43 |
* @author Christian Vogt (cvogt) |
44 |
*/ |
45 |
public class CrossReferenceAdapterTests extends BaseCoreTests { |
46 |
|
47 |
private Resource otherRes; |
48 |
private Library otherRoot; |
49 |
private Book otherBook; |
50 |
private Writer otherWriter; |
51 |
private Employee otherEmp; |
52 |
|
53 |
/** |
54 |
* Tests to ensure that every object in the resource has |
55 |
* an attached CrossReferenceAdapter |
56 |
*/ |
57 |
public void test_hasCrossReferenceAdapter() { |
58 |
checkHasCrossReferenceAdapter(testResource); |
59 |
} |
60 |
|
61 |
/** |
62 |
* Helper method to check that the notifier and all its contents |
63 |
* have an attached CrossReferenceAdapter. |
64 |
* |
65 |
* @param notifier |
66 |
*/ |
67 |
private void checkHasCrossReferenceAdapter(Notifier notifier) { |
68 |
boolean result = hasCrossRefenenceAdapter(notifier); |
69 |
if (result) { |
70 |
TreeIterator iter = null; |
71 |
if (notifier instanceof ResourceSet) { |
72 |
iter = ((ResourceSet)notifier).getAllContents(); |
73 |
} else if (notifier instanceof Resource) { |
74 |
iter = ((Resource)notifier).getAllContents(); |
75 |
} else if (notifier instanceof EObject) { |
76 |
iter = ((EObject)notifier).eAllContents(); |
77 |
} else { |
78 |
fail("Invalid Object Type"); //$NON-NLS-1$ |
79 |
} |
80 |
|
81 |
while (iter.hasNext()) { |
82 |
if (!hasCrossRefenenceAdapter((Notifier)iter.next())) { |
83 |
result = false; |
84 |
break; |
85 |
} |
86 |
} |
87 |
} |
88 |
if (!result) { |
89 |
fail("CrossReferenceAdapter is missing."); //$NON-NLS-1$ |
90 |
} |
91 |
} |
92 |
|
93 |
/** |
94 |
* Returns true if the notifier has an attached CrossReferenceAdapter. |
95 |
* |
96 |
* @param notifier the notifier to check |
97 |
* @return |
98 |
*/ |
99 |
private boolean hasCrossRefenenceAdapter(Notifier notifier) { |
100 |
boolean result = false; |
101 |
for (Iterator i = notifier.eAdapters().iterator(); i.hasNext(); ) { |
102 |
if (i.next() instanceof MSLCrossReferenceAdapter) { |
103 |
result = true; |
104 |
} |
105 |
} |
106 |
return result; |
107 |
} |
108 |
|
109 |
/** |
110 |
* Tests that resource import and exports information is properly |
111 |
* maintained when many cross references are added and removed. |
112 |
*/ |
113 |
public void test_importsExportsAddRemoveMany() { |
114 |
Book rootBook = (Book)root.getBooks().get(0); |
115 |
Book level1Book = (Book)((Library)root.getBranches().get(0)).getBooks().get(0); |
116 |
final List books = new ArrayList(); |
117 |
books.add(rootBook); |
118 |
books.add(level1Book); |
119 |
|
120 |
MUndoInterval undo = domain.runInUndoInterval(new Runnable() { |
121 |
public void run() { |
122 |
try { |
123 |
domain.runAsWrite(new MRunnable() { |
124 |
|
125 |
public Object run() { |
126 |
otherWriter.getBooks().addAll(books); |
127 |
|
128 |
return null; |
129 |
}}); |
130 |
} catch (MSLActionAbandonedException e) { |
131 |
fail("Failed to update model: " + e.getLocalizedMessage()); //$NON-NLS-1$ |
132 |
} |
133 |
}}); |
134 |
|
135 |
Collection imports = domain.getImports(otherRes); |
136 |
assertTrue(imports.contains(testResource)); |
137 |
|
138 |
imports = domain.getAllImports(otherRes); |
139 |
assertTrue(imports.contains(testResource)); |
140 |
|
141 |
Collection exports = domain.getExports(testResource); |
142 |
assertTrue(exports.contains(otherRes)); |
143 |
|
144 |
exports = domain.getAllExports(testResource); |
145 |
assertTrue(exports.contains(otherRes)); |
146 |
|
147 |
// remove books by undo |
148 |
undo.undo(); |
149 |
|
150 |
imports = domain.getImports(otherRes); |
151 |
assertFalse(imports.contains(testResource)); |
152 |
|
153 |
imports = domain.getAllImports(otherRes); |
154 |
assertFalse(imports.contains(testResource)); |
155 |
|
156 |
exports = domain.getExports(testResource); |
157 |
assertFalse(exports.contains(otherRes)); |
158 |
|
159 |
exports = domain.getAllExports(testResource); |
160 |
assertFalse(exports.contains(otherRes)); |
161 |
} |
162 |
|
163 |
/** |
164 |
* Tests that resource import and exports information is properly |
165 |
* maintained when many cross references are added and removed |
166 |
* one at a time. |
167 |
*/ |
168 |
public void test_importsExportsAddManyRemoveOneAtATime() { |
169 |
final Book rootBook = (Book)root.getBooks().get(0); |
170 |
final Book level1Book = (Book)((Library)root.getBranches().get(0)).getBooks().get(0); |
171 |
final List books = new ArrayList(); |
172 |
books.add(rootBook); |
173 |
books.add(level1Book); |
174 |
|
175 |
domain.runInUndoInterval(new Runnable() { |
176 |
public void run() { |
177 |
try { |
178 |
domain.runAsWrite(new MRunnable() { |
179 |
|
180 |
public Object run() { |
181 |
otherWriter.getBooks().addAll(books); |
182 |
|
183 |
return null; |
184 |
}}); |
185 |
} catch (MSLActionAbandonedException e) { |
186 |
fail("Failed to update model: " + e.getLocalizedMessage()); //$NON-NLS-1$ |
187 |
} |
188 |
}}); |
189 |
|
190 |
Collection imports = domain.getImports(otherRes); |
191 |
assertTrue(imports.contains(testResource)); |
192 |
|
193 |
imports = domain.getAllImports(otherRes); |
194 |
assertTrue(imports.contains(testResource)); |
195 |
|
196 |
Collection exports = domain.getExports(testResource); |
197 |
assertTrue(exports.contains(otherRes)); |
198 |
|
199 |
exports = domain.getAllExports(testResource); |
200 |
assertTrue(exports.contains(otherRes)); |
201 |
|
202 |
domain.runInUndoInterval(new Runnable() { |
203 |
public void run() { |
204 |
try { |
205 |
domain.runAsWrite(new MRunnable() { |
206 |
|
207 |
public Object run() { |
208 |
otherWriter.getBooks().remove(rootBook); |
209 |
|
210 |
return null; |
211 |
}}); |
212 |
} catch (MSLActionAbandonedException e) { |
213 |
fail("Failed to update model: " + e.getLocalizedMessage()); //$NON-NLS-1$ |
214 |
} |
215 |
}}); |
216 |
|
217 |
imports = domain.getImports(otherRes); |
218 |
assertTrue(imports.contains(testResource)); |
219 |
|
220 |
imports = domain.getAllImports(otherRes); |
221 |
assertTrue(imports.contains(testResource)); |
222 |
|
223 |
exports = domain.getExports(testResource); |
224 |
assertTrue(exports.contains(otherRes)); |
225 |
|
226 |
exports = domain.getAllExports(testResource); |
227 |
assertTrue(exports.contains(otherRes)); |
228 |
|
229 |
domain.runUnvalidated(new MRunnable() { |
230 |
public Object run() { |
231 |
return domain.runInUndoInterval(new Runnable() { |
232 |
public void run() { |
233 |
try { |
234 |
domain.runAsWrite(new MRunnable() { |
235 |
|
236 |
public Object run() { |
237 |
otherWriter.getBooks().remove(level1Book); |
238 |
|
239 |
return null; |
240 |
}}); |
241 |
} catch (MSLActionAbandonedException e) { |
242 |
fail("Failed to update model: " + e.getLocalizedMessage()); //$NON-NLS-1$ |
243 |
} |
244 |
} |
245 |
}); |
246 |
} |
247 |
}); |
248 |
|
249 |
imports = domain.getImports(otherRes); |
250 |
assertFalse(imports.contains(testResource)); |
251 |
|
252 |
imports = domain.getAllImports(otherRes); |
253 |
assertFalse(imports.contains(testResource)); |
254 |
|
255 |
exports = domain.getExports(testResource); |
256 |
assertFalse(exports.contains(otherRes)); |
257 |
|
258 |
exports = domain.getAllExports(testResource); |
259 |
assertFalse(exports.contains(otherRes)); |
260 |
} |
261 |
|
262 |
/** |
263 |
* Tests that resource import and exports information is properly |
264 |
* maintained when a cross reference is set and unset. |
265 |
*/ |
266 |
public void test_importsExportsSetUnset() { |
267 |
final Writer rootWriter = (Writer)root.getWriters().get(0); |
268 |
|
269 |
|
270 |
MUndoInterval undo = domain.runInUndoInterval(new Runnable() { |
271 |
public void run() { |
272 |
try { |
273 |
domain.runAsWrite(new MRunnable() { |
274 |
|
275 |
public Object run() { |
276 |
otherBook.setAuthor(rootWriter); |
277 |
|
278 |
return null; |
279 |
}}); |
280 |
} catch (MSLActionAbandonedException e) { |
281 |
fail("Failed to update model: " + e.getLocalizedMessage()); //$NON-NLS-1$ |
282 |
} |
283 |
}}); |
284 |
|
285 |
Collection imports = domain.getImports(otherRes); |
286 |
assertTrue(imports.contains(testResource)); |
287 |
|
288 |
imports = domain.getAllImports(otherRes); |
289 |
assertTrue(imports.contains(testResource)); |
290 |
|
291 |
Collection exports = domain.getExports(testResource); |
292 |
assertTrue(exports.contains(otherRes)); |
293 |
|
294 |
exports = domain.getAllExports(testResource); |
295 |
assertTrue(exports.contains(otherRes)); |
296 |
|
297 |
// unset author by undo |
298 |
undo.undo(); |
299 |
|
300 |
imports = domain.getImports(otherRes); |
301 |
assertFalse(imports.contains(testResource)); |
302 |
|
303 |
imports = domain.getAllImports(otherRes); |
304 |
assertFalse(imports.contains(testResource)); |
305 |
|
306 |
exports = domain.getExports(testResource); |
307 |
assertFalse(exports.contains(otherRes)); |
308 |
|
309 |
exports = domain.getAllExports(testResource); |
310 |
assertFalse(exports.contains(otherRes)); |
311 |
} |
312 |
|
313 |
/** |
314 |
* Tests that resource import and exports information is properly |
315 |
* maintained when a cross reference is added and removed. |
316 |
*/ |
317 |
public void test_importsExportsAddRemove() { |
318 |
final Book rootBook = (Book)root.getBooks().get(0); |
319 |
|
320 |
MUndoInterval undo = domain.runInUndoInterval(new Runnable() { |
321 |
public void run() { |
322 |
try { |
323 |
domain.runAsWrite(new MRunnable() { |
324 |
|
325 |
public Object run() { |
326 |
otherWriter.getBooks().add(rootBook); |
327 |
|
328 |
return null; |
329 |
}}); |
330 |
} catch (MSLActionAbandonedException e) { |
331 |
fail("Failed to update model: " + e.getLocalizedMessage()); //$NON-NLS-1$ |
332 |
} |
333 |
}}); |
334 |
|
335 |
Collection imports = domain.getImports(otherRes); |
336 |
assertTrue(imports.contains(testResource)); |
337 |
|
338 |
imports = domain.getAllImports(otherRes); |
339 |
assertTrue(imports.contains(testResource)); |
340 |
|
341 |
Collection exports = domain.getExports(testResource); |
342 |
assertTrue(exports.contains(otherRes)); |
343 |
|
344 |
exports = domain.getAllExports(testResource); |
345 |
assertTrue(exports.contains(otherRes)); |
346 |
|
347 |
// remove book by undo |
348 |
undo.undo(); |
349 |
|
350 |
imports = domain.getImports(otherRes); |
351 |
assertFalse(imports.contains(testResource)); |
352 |
|
353 |
imports = domain.getAllImports(otherRes); |
354 |
assertFalse(imports.contains(testResource)); |
355 |
|
356 |
exports = domain.getExports(testResource); |
357 |
assertFalse(exports.contains(otherRes)); |
358 |
|
359 |
exports = domain.getAllExports(testResource); |
360 |
assertFalse(exports.contains(otherRes)); |
361 |
} |
362 |
|
363 |
/** |
364 |
* Tests that when a resource containing a cross reference is unload, |
365 |
* the cross reference information is maintained and not accessible. |
366 |
*/ |
367 |
public void test_unloadedCrossReference() { |
368 |
final Employee boss = EXTLibraryFactory.eINSTANCE.createEmployee(); |
369 |
|
370 |
domain.runInUndoInterval(new Runnable() { |
371 |
public void run() { |
372 |
try { |
373 |
domain.runAsWrite(new MRunnable() { |
374 |
|
375 |
public Object run() { |
376 |
root.getEmployees().add(boss); |
377 |
otherEmp.setManager(boss); |
378 |
|
379 |
return null; |
380 |
}}); |
381 |
} catch (MSLActionAbandonedException e) { |
382 |
fail("Failed to update model: " + e.getLocalizedMessage()); //$NON-NLS-1$ |
383 |
} |
384 |
}}); |
385 |
|
386 |
Collection xrefs = EObjectUtil.getReferencers( |
387 |
boss, |
388 |
new EReference[] {EXTLibraryPackage.eINSTANCE.getEmployee_Manager()}); |
389 |
|
390 |
assertTrue(xrefs.contains(otherEmp)); |
391 |
|
392 |
// unload the resource containing the employee |
393 |
otherRes.unload(); |
394 |
|
395 |
xrefs = EObjectUtil.getReferencers( |
396 |
boss, |
397 |
new EReference[] {EXTLibraryPackage.eINSTANCE.getEmployee_Manager()}); |
398 |
|
399 |
// cannot find references because resource was unloaded and are not returned |
400 |
assertTrue(xrefs.isEmpty()); |
401 |
} |
402 |
|
403 |
/** |
404 |
* Tests when a resource containing a cross reference and the referencer |
405 |
* is destroyed, that the cross reference information is updated. |
406 |
*/ |
407 |
public void test_destroyReferencer() { |
408 |
final Employee boss = EXTLibraryFactory.eINSTANCE.createEmployee(); |
409 |
|
410 |
domain.runInUndoInterval(new Runnable() { |
411 |
public void run() { |
412 |
try { |
413 |
domain.runAsWrite(new MRunnable() { |
414 |
|
415 |
public Object run() { |
416 |
root.getEmployees().add(boss); |
417 |
domain.getResourceSet().eAdapters().add(new ECrossReferenceAdapter()); |
418 |
otherEmp.setManager(boss); |
419 |
return null; |
420 |
}}); |
421 |
} catch (MSLActionAbandonedException e) { |
422 |
fail("Failed to update model: " + e.getLocalizedMessage()); //$NON-NLS-1$ |
423 |
} |
424 |
}}); |
425 |
|
426 |
Collection xrefs = EObjectUtil.getReferencers( |
427 |
boss, |
428 |
new EReference[] {EXTLibraryPackage.eINSTANCE.getEmployee_Manager()}); |
429 |
|
430 |
assertTrue(xrefs.contains(otherEmp)); |
431 |
|
432 |
domain.runInUndoInterval(new Runnable() { |
433 |
public void run() { |
434 |
try { |
435 |
domain.runAsWrite(new MRunnable() { |
436 |
|
437 |
public Object run() { |
438 |
EObjectUtil.destroy(otherEmp); |
439 |
return null; |
440 |
}}); |
441 |
} catch (MSLActionAbandonedException e) { |
442 |
fail("Failed to update model: " + e.getLocalizedMessage()); //$NON-NLS-1$ |
443 |
} |
444 |
}}); |
445 |
xrefs = EObjectUtil.getReferencers( |
446 |
boss, |
447 |
new EReference[] {EXTLibraryPackage.eINSTANCE.getEmployee_Manager()}); |
448 |
|
449 |
assertTrue(xrefs.isEmpty()); |
450 |
} |
451 |
|
452 |
/** |
453 |
* Tests when a resource containing a cross reference and the referencer |
454 |
* is detached, that the cross reference information is maintained. |
455 |
*/ |
456 |
public void test_detachReferencer() { |
457 |
final Employee boss = EXTLibraryFactory.eINSTANCE.createEmployee(); |
458 |
|
459 |
domain.runInUndoInterval(new Runnable() { |
460 |
public void run() { |
461 |
try { |
462 |
domain.runAsWrite(new MRunnable() { |
463 |
|
464 |
public Object run() { |
465 |
root.getEmployees().add(boss); |
466 |
otherEmp.setManager(boss); |
467 |
return null; |
468 |
}}); |
469 |
} catch (MSLActionAbandonedException e) { |
470 |
fail("Failed to update model: " + e.getLocalizedMessage()); //$NON-NLS-1$ |
471 |
} |
472 |
}}); |
473 |
|
474 |
Collection xrefs = EObjectUtil.getReferencers( |
475 |
boss, |
476 |
new EReference[] {EXTLibraryPackage.eINSTANCE.getEmployee_Manager()}); |
477 |
|
478 |
assertTrue(xrefs.contains(otherEmp)); |
479 |
|
480 |
domain.runInUndoInterval(new Runnable() { |
481 |
public void run() { |
482 |
try { |
483 |
domain.runAsWrite(new MRunnable() { |
484 |
|
485 |
public Object run() { |
486 |
otherRoot.getEmployees().remove(otherEmp); |
487 |
return null; |
488 |
}}); |
489 |
} catch (MSLActionAbandonedException e) { |
490 |
fail("Failed to update model: " + e.getLocalizedMessage()); //$NON-NLS-1$ |
491 |
} |
492 |
}}); |
493 |
xrefs = EObjectUtil.getReferencers( |
494 |
boss, |
495 |
new EReference[] {EXTLibraryPackage.eINSTANCE.getEmployee_Manager()}); |
496 |
|
497 |
assertTrue(xrefs.contains(otherEmp)); |
498 |
} |
499 |
|
500 |
/** |
501 |
* Tests when a resource containing a cross reference and the referenced object |
502 |
* is detached, that the cross reference information is maintained. |
503 |
*/ |
504 |
public void test_detachReferenced() { |
505 |
final Employee boss = EXTLibraryFactory.eINSTANCE.createEmployee(); |
506 |
|
507 |
domain.runInUndoInterval(new Runnable() { |
508 |
public void run() { |
509 |
try { |
510 |
domain.runAsWrite(new MRunnable() { |
511 |
|
512 |
public Object run() { |
513 |
root.getEmployees().add(boss); |
514 |
otherEmp.setManager(boss); |
515 |
return null; |
516 |
}}); |
517 |
} catch (MSLActionAbandonedException e) { |
518 |
fail("Failed to update model: " + e.getLocalizedMessage()); //$NON-NLS-1$ |
519 |
} |
520 |
}}); |
521 |
|
522 |
Collection xrefs = EObjectUtil.getReferencers( |
523 |
boss, |
524 |
new EReference[] {EXTLibraryPackage.eINSTANCE.getEmployee_Manager()}); |
525 |
|
526 |
assertTrue(xrefs.contains(otherEmp)); |
527 |
|
528 |
domain.runInUndoInterval(new Runnable() { |
529 |
public void run() { |
530 |
try { |
531 |
domain.runAsWrite(new MRunnable() { |
532 |
|
533 |
public Object run() { |
534 |
root.getEmployees().remove(boss); |
535 |
return null; |
536 |
}}); |
537 |
} catch (MSLActionAbandonedException e) { |
538 |
fail("Failed to update model: " + e.getLocalizedMessage()); //$NON-NLS-1$ |
539 |
} |
540 |
}}); |
541 |
xrefs = EObjectUtil.getReferencers( |
542 |
boss, |
543 |
new EReference[] {EXTLibraryPackage.eINSTANCE.getEmployee_Manager()}); |
544 |
|
545 |
assertTrue(xrefs.contains(otherEmp)); |
546 |
} |
547 |
|
548 |
/** |
549 |
* Tests when a resource containing a bi-directional cross reference |
550 |
* to an object and the reference is destroyed, that the cross |
551 |
* reference information is updated correctly |
552 |
*/ |
553 |
public void test_biDirectionalReference() { |
554 |
final Book rootBook = (Book)root.getBooks().get(0); |
555 |
|
556 |
domain.runInUndoInterval(new Runnable() { |
557 |
public void run() { |
558 |
try { |
559 |
domain.runAsWrite(new MRunnable() { |
560 |
|
561 |
public Object run() { |
562 |
otherWriter.getBooks().add(rootBook); |
563 |
return null; |
564 |
}}); |
565 |
} catch (MSLActionAbandonedException e) { |
566 |
fail("Failed to update model: " + e.getLocalizedMessage()); //$NON-NLS-1$ |
567 |
} |
568 |
}}); |
569 |
|
570 |
Collection xrefs = EObjectUtil.getReferencers( |
571 |
rootBook, |
572 |
new EReference[] {EXTLibraryPackage.eINSTANCE.getWriter_Books()}); |
573 |
|
574 |
assertTrue(xrefs.contains(otherWriter)); |
575 |
|
576 |
domain.runUnvalidated(new MRunnable() { |
577 |
public Object run() { |
578 |
return domain.runInUndoInterval(new Runnable() { |
579 |
public void run() { |
580 |
try { |
581 |
domain.runAsWrite(new MRunnable() { |
582 |
|
583 |
public Object run() { |
584 |
EObjectUtil.destroy(otherWriter); |
585 |
otherWriter = null; |
586 |
return null; |
587 |
}}); |
588 |
} catch (MSLActionAbandonedException e) { |
589 |
fail("Failed to update model: " + e.getLocalizedMessage()); //$NON-NLS-1$ |
590 |
} |
591 |
} |
592 |
}); |
593 |
} |
594 |
}); |
595 |
|
596 |
xrefs = EObjectUtil.getReferencers( |
597 |
rootBook, |
598 |
new EReference[] {EXTLibraryPackage.eINSTANCE.getWriter_Books()}); |
599 |
|
600 |
assertTrue(xrefs.isEmpty()); |
601 |
} |
602 |
|
603 |
/** |
604 |
* Tests when a resource containing a uni-directional cross reference |
605 |
* to an object in another resource and the reference is destroyed, |
606 |
* that the cross reference information is updated correctly |
607 |
*/ |
608 |
public void test_uniDirectionalReference() { |
609 |
final Employee boss = EXTLibraryFactory.eINSTANCE.createEmployee(); |
610 |
|
611 |
domain.runInUndoInterval(new Runnable() { |
612 |
public void run() { |
613 |
try { |
614 |
domain.runAsWrite(new MRunnable() { |
615 |
|
616 |
public Object run() { |
617 |
root.getEmployees().add(boss); |
618 |
otherEmp.setManager(boss); |
619 |
return null; |
620 |
}}); |
621 |
} catch (MSLActionAbandonedException e) { |
622 |
fail("Failed to update model: " + e.getLocalizedMessage()); //$NON-NLS-1$ |
623 |
} |
624 |
}}); |
625 |
|
626 |
Collection xrefs = EObjectUtil.getReferencers( |
627 |
boss, |
628 |
new EReference[] {EXTLibraryPackage.eINSTANCE.getEmployee_Manager()}); |
629 |
|
630 |
assertTrue(xrefs.contains(otherEmp)); |
631 |
|
632 |
domain.runInUndoInterval(new Runnable() { |
633 |
public void run() { |
634 |
try { |
635 |
domain.runAsWrite(new MRunnable() { |
636 |
|
637 |
public Object run() { |
638 |
EObjectUtil.destroy(otherEmp); |
639 |
otherEmp = null; |
640 |
return null; |
641 |
}}); |
642 |
} catch (MSLActionAbandonedException e) { |
643 |
fail("Failed to update model: " + e.getLocalizedMessage()); //$NON-NLS-1$ |
644 |
} |
645 |
}}); |
646 |
|
647 |
xrefs = EObjectUtil.getReferencers( |
648 |
boss, |
649 |
new EReference[] {EXTLibraryPackage.eINSTANCE.getEmployee_Manager()}); |
650 |
|
651 |
assertTrue(xrefs.isEmpty()); |
652 |
} |
653 |
|
654 |
/** |
655 |
* Tests retrieving referencers of a specific type. |
656 |
*/ |
657 |
public void test_getReferencersOfType() { |
658 |
final Employee boss = EXTLibraryFactory.eINSTANCE.createEmployee(); |
659 |
|
660 |
domain.runInUndoInterval(new Runnable() { |
661 |
public void run() { |
662 |
try { |
663 |
domain.runAsWrite(new MRunnable() { |
664 |
|
665 |
public Object run() { |
666 |
root.getEmployees().add(boss); |
667 |
otherEmp.setManager(boss); |
668 |
return null; |
669 |
}}); |
670 |
} catch (MSLActionAbandonedException e) { |
671 |
fail("Failed to update model: " + e.getLocalizedMessage()); //$NON-NLS-1$ |
672 |
} |
673 |
}}); |
674 |
|
675 |
MSLCrossReferenceAdapter crossReferenceAdapter = |
676 |
MSLCrossReferenceAdapter.getCrossReferenceAdapter(boss); |
677 |
|
678 |
// tests valid type |
679 |
Collection xrefs = crossReferenceAdapter.getInverseReferencers(boss, |
680 |
EXTLibraryPackage.eINSTANCE.getEmployee_Manager(), |
681 |
EXTLibraryPackage.eINSTANCE.getEmployee()); |
682 |
|
683 |
assertTrue(xrefs.contains(otherEmp)); |
684 |
|
685 |
// tests valid type |
686 |
xrefs = crossReferenceAdapter.getInverseReferencers(boss, |
687 |
null, |
688 |
EXTLibraryPackage.eINSTANCE.getEmployee()); |
689 |
|
690 |
assertTrue(xrefs.contains(otherEmp)); |
691 |
|
692 |
// tests invalid type |
693 |
xrefs = crossReferenceAdapter.getInverseReferencers(boss, |
694 |
EXTLibraryPackage.eINSTANCE.getEmployee_Manager(), |
695 |
EXTLibraryPackage.eINSTANCE.getLibrary()); |
696 |
|
697 |
assertTrue(xrefs.isEmpty()); |
698 |
|
699 |
// tests invalid type |
700 |
xrefs = crossReferenceAdapter.getInverseReferencers(boss, |
701 |
null, |
702 |
EXTLibraryPackage.eINSTANCE.getLibrary()); |
703 |
|
704 |
assertTrue(xrefs.isEmpty()); |
705 |
} |
706 |
|
707 |
/** |
708 |
* Tests retrieving referencers when the cross reference adapter |
709 |
* is not added to the ResourceSet when the domain is created, but rather |
710 |
* added at a later point in time. |
711 |
*/ |
712 |
public void test_addLateCrossReferenceAdapter() { |
713 |
MSLEditingDomain mslDomain = (MSLEditingDomain)domain; |
714 |
|
715 |
// remove the cross reference adapter from the ResourceSet |
716 |
mslDomain.getResourceSet().eAdapters().remove(mslDomain.getCrossReferenceAdapter()); |
717 |
|
718 |
final Employee boss = EXTLibraryFactory.eINSTANCE.createEmployee(); |
719 |
|
720 |
domain.runInUndoInterval(new Runnable() { |
721 |
public void run() { |
722 |
try { |
723 |
domain.runAsWrite(new MRunnable() { |
724 |
|
725 |
public Object run() { |
726 |
root.getEmployees().add(boss); |
727 |
otherEmp.setManager(boss); |
728 |
return null; |
729 |
}}); |
730 |
} catch (MSLActionAbandonedException e) { |
731 |
fail("Failed to update model: " + e.getLocalizedMessage()); //$NON-NLS-1$ |
732 |
} |
733 |
}}); |
734 |
|
735 |
Collection xrefs = EObjectUtil.getReferencers( |
736 |
boss, |
737 |
new EReference[] {EXTLibraryPackage.eINSTANCE.getEmployee_Manager()}); |
738 |
|
739 |
assertTrue(xrefs.isEmpty()); |
740 |
|
741 |
// add the cross reference adapter back to the ResourceSet |
742 |
mslDomain.getResourceSet().eAdapters().add(mslDomain.getCrossReferenceAdapter()); |
743 |
|
744 |
// check that all children of the ResourceSet have a cross reference adapter |
745 |
checkHasCrossReferenceAdapter(mslDomain.getResourceSet()); |
746 |
|
747 |
xrefs = EObjectUtil.getReferencers( |
748 |
boss, |
749 |
new EReference[] {EXTLibraryPackage.eINSTANCE.getEmployee_Manager()}); |
750 |
|
751 |
assertTrue(xrefs.contains(otherEmp)); |
752 |
} |
753 |
|
754 |
/* |
755 |
* Sets up a library test model: |
756 |
* |
757 |
* Library (otherRoot) |
758 |
* - Book (otherBook) |
759 |
* - Writer (otherWiter) |
760 |
* - Employee (otherEmp) |
761 |
*/ |
762 |
protected void setUp() throws Exception { |
763 |
|
764 |
super.setUp(); |
765 |
|
766 |
otherRes = ResourceUtil.create("/tmp/otherLibrary.extLibrary", //$NON-NLS-1$ |
767 |
EXTLibraryPackage.eINSTANCE.getLibrary()); |
768 |
|
769 |
domain.getResourceSet().getResources().add(otherRes); |
770 |
|
771 |
otherRoot = (Library)otherRes.getContents().get(0); |
772 |
otherBook = EXTLibraryFactory.eINSTANCE.createBook(); |
773 |
otherWriter = EXTLibraryFactory.eINSTANCE.createWriter(); |
774 |
otherEmp = EXTLibraryFactory.eINSTANCE.createEmployee(); |
775 |
|
776 |
domain.runInUndoInterval(new Runnable() { |
777 |
public void run() { |
778 |
try { |
779 |
domain.runAsWrite(new MRunnable() { |
780 |
|
781 |
public Object run() { |
782 |
|
783 |
otherRoot.getBooks().add(otherBook); |
784 |
otherRoot.getWriters().add(otherWriter); |
785 |
otherRoot.getEmployees().add(otherEmp); |
786 |
|
787 |
return null; |
788 |
}}); |
789 |
} catch (MSLActionAbandonedException e) { |
790 |
fail("Failed to setup test model: " + e.getLocalizedMessage()); //$NON-NLS-1$ |
791 |
} |
792 |
}}); |
793 |
} |
794 |
|
795 |
protected void tearDown() throws Exception { |
796 |
|
797 |
try { |
798 |
if (otherRes != null) { |
799 |
if (otherRes.isLoaded()) { |
800 |
otherRes.unload(); |
801 |
} |
802 |
|
803 |
otherRes.getResourceSet().getResources().remove(otherRes); |
804 |
} |
805 |
} finally { |
806 |
super.tearDown(); |
807 |
} |
808 |
} |
809 |
} |