Removed
Link Here
|
1 |
/****************************************************************************** |
2 |
* Copyright (c) 2005 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 |
|
13 |
package org.eclipse.gmf.tests.runtime.emf.core.internal.resources; |
14 |
|
15 |
import java.io.IOException; |
16 |
import java.util.List; |
17 |
import java.util.Map; |
18 |
|
19 |
import junit.framework.Test; |
20 |
import junit.framework.TestSuite; |
21 |
|
22 |
import org.eclipse.core.resources.IFile; |
23 |
import org.eclipse.core.resources.IFileState; |
24 |
import org.eclipse.core.runtime.CoreException; |
25 |
import org.eclipse.core.runtime.Path; |
26 |
import org.eclipse.emf.common.notify.Notification; |
27 |
import org.eclipse.emf.common.notify.impl.AdapterImpl; |
28 |
import org.eclipse.emf.common.util.URI; |
29 |
import org.eclipse.emf.ecore.EObject; |
30 |
import org.eclipse.emf.ecore.resource.Resource; |
31 |
import org.eclipse.emf.ecore.resource.ResourceSet; |
32 |
import org.eclipse.emf.ecore.util.EcoreUtil; |
33 |
|
34 |
import org.eclipse.emf.examples.extlibrary.Book; |
35 |
import org.eclipse.emf.examples.extlibrary.EXTLibraryFactory; |
36 |
import org.eclipse.emf.examples.extlibrary.EXTLibraryPackage; |
37 |
import org.eclipse.emf.examples.extlibrary.Library; |
38 |
import org.eclipse.emf.examples.extlibrary.Writer; |
39 |
import org.eclipse.gmf.runtime.emf.core.EventTypes; |
40 |
import org.eclipse.gmf.runtime.emf.core.edit.DemuxedMListener; |
41 |
import org.eclipse.gmf.runtime.emf.core.edit.MEditingDomain; |
42 |
import org.eclipse.gmf.runtime.emf.core.edit.MFilter; |
43 |
import org.eclipse.gmf.runtime.emf.core.edit.MResourceOption; |
44 |
import org.eclipse.gmf.runtime.emf.core.edit.MRunnable; |
45 |
import org.eclipse.gmf.runtime.emf.core.edit.MUndoInterval; |
46 |
import org.eclipse.gmf.runtime.emf.core.exceptions.MSLActionAbandonedException; |
47 |
import org.eclipse.gmf.runtime.emf.core.internal.resourcemap.ResourceMap; |
48 |
import org.eclipse.gmf.runtime.emf.core.resources.CannotAbsorbException; |
49 |
import org.eclipse.gmf.runtime.emf.core.resources.CannotSeparateException; |
50 |
import org.eclipse.gmf.runtime.emf.core.resources.ILogicalResource; |
51 |
|
52 |
|
53 |
/** |
54 |
* Tests the {@link org.eclipse.gmf.runtime.emf.core.internal.resources.LogicalResource} |
55 |
* class and its load/save/handler/helper/util cohorts. |
56 |
* |
57 |
* @author Christian W. Damus (cdamus) |
58 |
*/ |
59 |
public class LogicalResourceTest |
60 |
extends BaseLogicalResourceTest { |
61 |
|
62 |
public LogicalResourceTest(String name) { |
63 |
super(name); |
64 |
} |
65 |
|
66 |
public static Test suite() { |
67 |
return new TestSuite(LogicalResourceTest.class, "Basic Tests"); //$NON-NLS-1$ |
68 |
} |
69 |
|
70 |
DemuxedMListener aListener; |
71 |
|
72 |
/** |
73 |
* Tests the getMappedResources() method. |
74 |
*/ |
75 |
public void test_getMappedResources() { |
76 |
Map resources = logres.getMappedResources(); |
77 |
assertEquals(1, resources.size()); |
78 |
|
79 |
Resource nested = (Resource) resources.get(root); |
80 |
assertNotNull(nested); |
81 |
assertEquals(logres.getURI(), nested.getURI()); |
82 |
|
83 |
nested = (ILogicalResource) resources.get(subunit1); |
84 |
assertNull(nested); |
85 |
|
86 |
try { |
87 |
logres.separate(subunit1, URI.createPlatformResourceURI(SUBUNIT_NAME1)); |
88 |
} catch (CannotSeparateException e) { |
89 |
fail("Should not fail to separate: " + e); //$NON-NLS-1$ |
90 |
} |
91 |
nested = (Resource) resources.get(subunit1); |
92 |
assertNotNull(nested); |
93 |
|
94 |
try { |
95 |
logres.absorb(subunit1); |
96 |
} catch (CannotAbsorbException e) { |
97 |
fail("Should not fail to absorb: " + e); //$NON-NLS-1$ |
98 |
} |
99 |
nested = (Resource) resources.get(subunit1); |
100 |
assertNull(nested); |
101 |
|
102 |
assertEquals(1, resources.size()); |
103 |
} |
104 |
|
105 |
/** |
106 |
* Specifically tests the representation of the logical resource's roots |
107 |
* in the getMappedResources() map. |
108 |
*/ |
109 |
public void test_getMappedResources_logicalRoots() { |
110 |
Map resources = logres.getMappedResources(); |
111 |
assertEquals(1, resources.size()); |
112 |
|
113 |
Resource unit = (Resource) resources.get(root); |
114 |
assertNotNull(unit); |
115 |
assertEquals(logres.getURI(), unit.getURI()); |
116 |
|
117 |
// test adding another root |
118 |
Library otherRoot = EXTLibraryFactory.eINSTANCE.createLibrary(); |
119 |
logres.getContents().add(otherRoot); |
120 |
|
121 |
assertEquals(2, resources.size()); |
122 |
unit = (Resource) resources.get(otherRoot); |
123 |
assertNotNull(unit); |
124 |
assertEquals(logres.getURI(), unit.getURI()); |
125 |
|
126 |
// test replacing a root using the set() method |
127 |
Library yetAnotherRoot = EXTLibraryFactory.eINSTANCE.createLibrary(); |
128 |
logres.getContents().set(1, yetAnotherRoot); |
129 |
|
130 |
assertEquals(2, resources.size()); |
131 |
unit = (Resource) resources.get(otherRoot); |
132 |
assertNull(unit); // the old root is no longer mapped to a unit |
133 |
unit = (Resource) resources.get(yetAnotherRoot); |
134 |
assertNotNull(unit); // the new one is |
135 |
assertEquals(logres.getURI(), unit.getURI()); |
136 |
|
137 |
// test removing a root |
138 |
logres.getContents().remove(yetAnotherRoot); |
139 |
|
140 |
assertEquals(1, resources.size()); |
141 |
unit = (Resource) resources.get(yetAnotherRoot); |
142 |
assertNull(unit); // the old root is no longer mapped to a unit |
143 |
} |
144 |
|
145 |
/** |
146 |
* Tests the URIs of sub-units and objects in the logical resource. |
147 |
*/ |
148 |
public void test_uris() { |
149 |
String subunitFrag = logres.getURIFragment(subunit1); |
150 |
URI subunitUri = EcoreUtil.getURI(subunit1); |
151 |
|
152 |
URI nestedUri = URI.createPlatformResourceURI(SUBUNIT_NAME1); |
153 |
|
154 |
try { |
155 |
logres.separate(subunit1, nestedUri); |
156 |
} catch (CannotSeparateException e) { |
157 |
fail("Should not fail to separate: " + e); //$NON-NLS-1$ |
158 |
} |
159 |
|
160 |
Resource nested = (Resource) logres.getMappedResources().get( |
161 |
subunit1); |
162 |
assertNotNull(nested); |
163 |
assertEquals(nestedUri, nested.getURI()); |
164 |
|
165 |
assertEquals(subunitFrag, logres.getURIFragment(subunit1)); |
166 |
assertEquals(subunitUri, EcoreUtil.getURI(subunit1)); |
167 |
} |
168 |
|
169 |
/** |
170 |
* Tests a monolithic resource (no sub-units). |
171 |
*/ |
172 |
public void test_saveLoad_monolith() { |
173 |
String rootFrag = logres.getURIFragment(root); |
174 |
String subunitFrag = logres.getURIFragment(subunit1); |
175 |
|
176 |
saveLogicalResource(); |
177 |
|
178 |
assertFalse(logres.isModified()); |
179 |
|
180 |
// discard and re-load the resource |
181 |
createLogicalResource(RESOURCE_NAME); |
182 |
|
183 |
loadLogicalResource(); |
184 |
|
185 |
assertFalse(logres.isModified()); |
186 |
|
187 |
Map resources = logres.getMappedResources(); |
188 |
assertEquals(1, resources.size()); |
189 |
|
190 |
assertEquals(1, logres.getContents().size()); |
191 |
assertFalse(logres.getContents().get(0) instanceof ResourceMap); |
192 |
|
193 |
root = lookupLibrary(rootFrag); |
194 |
assertNotNull(root); |
195 |
|
196 |
// in monolithic case, there should be no resource map. Should look |
197 |
// just like any old XMI resource |
198 |
assertFalse( |
199 |
((Resource) resources.get(root)).getContents().get(0) instanceof ResourceMap); |
200 |
|
201 |
subunit1 = lookupLibrary(subunitFrag); |
202 |
assertNotNull(subunit1); |
203 |
assertTrue(EcoreUtil.isAncestor(root, subunit1)); |
204 |
} |
205 |
|
206 |
/** |
207 |
* Tests single-level subunit nesting. |
208 |
*/ |
209 |
public void test_saveLoad_singleNesting() { |
210 |
String rootFrag = logres.getURIFragment(root); |
211 |
String subunitFrag = logres.getURIFragment(subunit1); |
212 |
|
213 |
try { |
214 |
logres.separate(subunit1, URI.createPlatformResourceURI(SUBUNIT_NAME1)); |
215 |
} catch (CannotSeparateException e) { |
216 |
fail("Should not fail to separate: " + e); //$NON-NLS-1$ |
217 |
} |
218 |
|
219 |
assertTrue(logres.isModified()); |
220 |
|
221 |
saveLogicalResource(); |
222 |
|
223 |
assertFalse(logres.isModified()); |
224 |
|
225 |
IFile subunitFile = project.getParent().getFile(new Path(SUBUNIT_NAME1)); |
226 |
assertNotNull(subunitFile); |
227 |
assertTrue(subunitFile.exists()); |
228 |
|
229 |
// discard and re-load the resource |
230 |
createLogicalResource(RESOURCE_NAME); |
231 |
|
232 |
loadLogicalResource(); |
233 |
|
234 |
assertFalse(logres.isModified()); |
235 |
|
236 |
Map resources = logres.getMappedResources(); |
237 |
assertEquals(2, resources.size()); |
238 |
|
239 |
assertEquals(1, logres.getContents().size()); |
240 |
assertFalse(logres.getContents().get(0) instanceof ResourceMap); |
241 |
|
242 |
root = lookupLibrary(rootFrag); |
243 |
assertNotNull(root); |
244 |
|
245 |
subunit1 = lookupLibrary(subunitFrag); |
246 |
assertNotNull(subunit1); |
247 |
assertTrue(EcoreUtil.isAncestor(root, subunit1)); |
248 |
} |
249 |
|
250 |
/** |
251 |
* Tests multi-level subunit nesting. |
252 |
*/ |
253 |
public void test_saveLoad_multiNesting() { |
254 |
String rootFrag = logres.getURIFragment(root); |
255 |
String subunit1Frag = logres.getURIFragment(subunit1); |
256 |
String subunit2Frag = logres.getURIFragment(subunit2); |
257 |
|
258 |
try { |
259 |
logres.separate(subunit1, URI.createPlatformResourceURI(SUBUNIT_NAME1)); |
260 |
logres.separate(subunit2, URI.createPlatformResourceURI(SUBUNIT_NAME2)); |
261 |
} catch (CannotSeparateException e) { |
262 |
fail("Should not fail to separate: " + e); //$NON-NLS-1$ |
263 |
} |
264 |
|
265 |
assertTrue(logres.isModified()); |
266 |
|
267 |
saveLogicalResource(); |
268 |
|
269 |
assertFalse(logres.isModified()); |
270 |
|
271 |
IFile subunitFile = project.getParent().getFile(new Path(SUBUNIT_NAME1)); |
272 |
assertNotNull(subunitFile); |
273 |
assertTrue(subunitFile.exists()); |
274 |
|
275 |
subunitFile = project.getParent().getFile(new Path(SUBUNIT_NAME2)); |
276 |
assertNotNull(subunitFile); |
277 |
assertTrue(subunitFile.exists()); |
278 |
|
279 |
// discard and re-load the resource |
280 |
createLogicalResource(RESOURCE_NAME); |
281 |
|
282 |
loadLogicalResource(); |
283 |
|
284 |
assertFalse(logres.isModified()); |
285 |
|
286 |
Map resources = logres.getMappedResources(); |
287 |
assertEquals(3, resources.size()); |
288 |
|
289 |
assertEquals(1, logres.getContents().size()); |
290 |
assertFalse(logres.getContents().get(0) instanceof ResourceMap); |
291 |
|
292 |
root = lookupLibrary(rootFrag); |
293 |
assertNotNull(root); |
294 |
|
295 |
subunit1 = lookupLibrary(subunit1Frag); |
296 |
assertNotNull(subunit1); |
297 |
subunit2 = lookupLibrary(subunit2Frag); |
298 |
assertNotNull(subunit2); |
299 |
assertTrue(EcoreUtil.isAncestor(root, subunit2)); |
300 |
assertTrue(EcoreUtil.isAncestor(subunit1, subunit2)); |
301 |
} |
302 |
|
303 |
/** |
304 |
* Tests multi-level subunit nesting in which two different sub-units |
305 |
* are stored in the same physical resource. |
306 |
*/ |
307 |
public void test_saveLoad_multiNestingSameUnit() { |
308 |
String rootFrag = logres.getURIFragment(root); |
309 |
String subunit1Frag = logres.getURIFragment(subunit1); |
310 |
String subunit2Frag = logres.getURIFragment(subunit2); |
311 |
String subunit3Frag = logres.getURIFragment(subunit3); |
312 |
|
313 |
try { |
314 |
logres.separate(subunit1, URI.createPlatformResourceURI(SUBUNIT_NAME1)); |
315 |
logres.separate(subunit2, URI.createPlatformResourceURI(SUBUNIT_NAME2)); |
316 |
logres.separate(subunit3, URI.createPlatformResourceURI(SUBUNIT_NAME2)); // same file |
317 |
} catch (CannotSeparateException e) { |
318 |
fail("Should not fail to separate: " + e); //$NON-NLS-1$ |
319 |
} |
320 |
|
321 |
assertTrue(logres.isModified()); |
322 |
|
323 |
saveLogicalResource(); |
324 |
|
325 |
assertFalse(logres.isModified()); |
326 |
|
327 |
IFile subunitFile = project.getParent().getFile(new Path(SUBUNIT_NAME1)); |
328 |
assertNotNull(subunitFile); |
329 |
assertTrue(subunitFile.exists()); |
330 |
|
331 |
subunitFile = project.getParent().getFile(new Path(SUBUNIT_NAME2)); |
332 |
assertNotNull(subunitFile); |
333 |
assertTrue(subunitFile.exists()); |
334 |
|
335 |
try { |
336 |
// the sub-unit file should only have been written to once, to write |
337 |
// both roots |
338 |
IFileState[] history = subunitFile.getHistory(null); |
339 |
assertNotNull(history); |
340 |
assertEquals(0, history.length); // file was only written once |
341 |
} catch (CoreException e) { |
342 |
fail("Failed to get file history: " + e.getLocalizedMessage()); //$NON-NLS-1$ |
343 |
} |
344 |
|
345 |
// discard and re-load the resource |
346 |
createLogicalResource(RESOURCE_NAME); |
347 |
|
348 |
loadLogicalResource(); |
349 |
|
350 |
Map resources = logres.getMappedResources(); |
351 |
assertEquals(4, resources.size()); |
352 |
|
353 |
assertEquals(1, logres.getContents().size()); |
354 |
assertFalse(logres.getContents().get(0) instanceof ResourceMap); |
355 |
|
356 |
root = lookupLibrary(rootFrag); |
357 |
assertNotNull(root); |
358 |
|
359 |
subunit1 = lookupLibrary(subunit1Frag); |
360 |
assertNotNull(subunit1); |
361 |
subunit2 = lookupLibrary(subunit2Frag); |
362 |
assertNotNull(subunit2); |
363 |
subunit3 = lookupLibrary(subunit3Frag); |
364 |
assertNotNull(subunit3); |
365 |
assertTrue(EcoreUtil.isAncestor(subunit1, subunit2)); |
366 |
assertTrue(EcoreUtil.isAncestor(root, subunit3)); |
367 |
} |
368 |
|
369 |
/** |
370 |
* Tests logical roots as sub-units. |
371 |
*/ |
372 |
public void test_saveLoad_separateLogicalRoot() { |
373 |
String rootFrag = logres.getURIFragment(root); |
374 |
|
375 |
try { |
376 |
logres.separate(root, URI.createPlatformResourceURI(SUBUNIT_NAME1)); |
377 |
} catch (CannotSeparateException e) { |
378 |
fail("Should not fail to separate: " + e); //$NON-NLS-1$ |
379 |
} |
380 |
|
381 |
assertTrue(logres.isModified()); |
382 |
|
383 |
saveLogicalResource(); |
384 |
|
385 |
assertFalse(logres.isModified()); |
386 |
|
387 |
IFile subunitFile = project.getParent().getFile(new Path(SUBUNIT_NAME1)); |
388 |
assertNotNull(subunitFile); |
389 |
assertTrue(subunitFile.exists()); |
390 |
|
391 |
// discard and re-load the resource |
392 |
createLogicalResource(RESOURCE_NAME); |
393 |
|
394 |
loadLogicalResource(); |
395 |
|
396 |
assertFalse(logres.isModified()); |
397 |
|
398 |
Map resources = logres.getMappedResources(); |
399 |
assertEquals(1, resources.size()); // nothing maps to the physical root resource |
400 |
|
401 |
assertEquals(1, logres.getContents().size()); |
402 |
assertFalse(logres.getContents().get(0) instanceof ResourceMap); |
403 |
|
404 |
root = lookupLibrary(rootFrag); |
405 |
assertNotNull(root); |
406 |
assertTrue(logres.isSeparate(root)); |
407 |
assertNull(root.eContainer()); |
408 |
assertTrue(logres.getContents().contains(root)); |
409 |
|
410 |
// absorb the root again |
411 |
try { |
412 |
logres.absorb(root); |
413 |
} catch (CannotAbsorbException e) { |
414 |
fail("Should not fail to absorb: " + e); //$NON-NLS-1$ |
415 |
} |
416 |
|
417 |
saveLogicalResource(); |
418 |
|
419 |
// discard and re-load the resource |
420 |
createLogicalResource(RESOURCE_NAME); |
421 |
loadLogicalResource(); |
422 |
|
423 |
root = lookupLibrary(rootFrag); |
424 |
assertNotNull(root); |
425 |
assertFalse(logres.isSeparate(root)); |
426 |
assertNull(root.eContainer()); |
427 |
assertTrue(logres.getContents().contains(root)); |
428 |
} |
429 |
|
430 |
/** |
431 |
* Tests multiple sub-units under the same parent, to verify that they are |
432 |
* loaded again in the correct order. This method uses the |
433 |
* <tt>subunit<i>n</i></tt> fields to store different elements than the |
434 |
* other tests. |
435 |
*/ |
436 |
public void test_multipleSubunitsOfSameParent() { |
437 |
// create a third child of the root element |
438 |
subunit3 = EXTLibraryFactory.eINSTANCE.createLibrary(); |
439 |
root.getBranches().add(subunit3); |
440 |
subunit3.setName("level1-2"); //$NON-NLS-1$ |
441 |
|
442 |
subunit1 = (Library) root.getBranches().get(0); |
443 |
subunit2 = (Library) root.getBranches().get(1); |
444 |
subunit3 = (Library) root.getBranches().get(2); |
445 |
|
446 |
String rootFrag = logres.getURIFragment(root); |
447 |
String subunit1Frag = logres.getURIFragment(subunit1); |
448 |
String subunit2Frag = logres.getURIFragment(subunit2); |
449 |
String subunit3Frag = logres.getURIFragment(subunit3); |
450 |
|
451 |
try { |
452 |
logres.separate(subunit1, URI.createPlatformResourceURI(SUBUNIT_NAME1)); |
453 |
logres.separate(subunit2, URI.createPlatformResourceURI(SUBUNIT_NAME2)); |
454 |
logres.separate(subunit3, URI.createPlatformResourceURI(SUBUNIT_NAME2)); |
455 |
} catch (CannotSeparateException e) { |
456 |
fail("Should not fail to separate: " + e); //$NON-NLS-1$ |
457 |
} |
458 |
|
459 |
assertTrue(logres.isModified()); |
460 |
|
461 |
saveLogicalResource(); |
462 |
|
463 |
assertFalse(logres.isModified()); |
464 |
|
465 |
// discard and re-load the resource |
466 |
createLogicalResource(RESOURCE_NAME); |
467 |
|
468 |
loadLogicalResource(); |
469 |
|
470 |
root = lookupLibrary(rootFrag); |
471 |
assertNotNull(root); |
472 |
|
473 |
subunit1 = lookupLibrary(subunit1Frag); |
474 |
assertNotNull(subunit1); |
475 |
subunit2 = lookupLibrary(subunit2Frag); |
476 |
assertNotNull(subunit2); |
477 |
subunit3 = lookupLibrary(subunit3Frag); |
478 |
assertNotNull(subunit3); |
479 |
|
480 |
// verify the ordering of the sub-units |
481 |
assertEquals(0, root.getBranches().indexOf(subunit1)); |
482 |
assertEquals(1, root.getBranches().indexOf(subunit2)); |
483 |
assertEquals(2, root.getBranches().indexOf(subunit3)); |
484 |
} |
485 |
|
486 |
/** |
487 |
* Tests absorption of an internal (middle) unit in multi-level subunit |
488 |
* nesting. |
489 |
*/ |
490 |
public void test_absorb_internal_multiNesting() { |
491 |
String rootFrag = logres.getURIFragment(root); |
492 |
String subunit1Frag = logres.getURIFragment(subunit1); |
493 |
String subunit2Frag = logres.getURIFragment(subunit2); |
494 |
|
495 |
try { |
496 |
logres.separate(subunit1, URI.createPlatformResourceURI(SUBUNIT_NAME1)); |
497 |
logres.separate(subunit2, URI.createPlatformResourceURI(SUBUNIT_NAME2)); |
498 |
} catch (CannotSeparateException e) { |
499 |
fail("Should not fail to separate: " + e); //$NON-NLS-1$ |
500 |
} |
501 |
|
502 |
saveLogicalResource(); |
503 |
|
504 |
// get the file modification stamps for later comparison |
505 |
IFile file = project.getParent().getFile(new Path(RESOURCE_NAME)); |
506 |
assertNotNull(file); |
507 |
long rootStamp = file.getModificationStamp(); |
508 |
file = project.getParent().getFile(new Path(SUBUNIT_NAME1)); |
509 |
assertNotNull(file); |
510 |
long subunit1Stamp = file.getModificationStamp(); |
511 |
file = project.getParent().getFile(new Path(SUBUNIT_NAME2)); |
512 |
assertNotNull(file); |
513 |
long subunit2Stamp = file.getModificationStamp(); |
514 |
|
515 |
// discard and re-load the resource |
516 |
createLogicalResource(RESOURCE_NAME); |
517 |
|
518 |
loadLogicalResource(); |
519 |
|
520 |
Map resources = logres.getMappedResources(); |
521 |
assertEquals(3, resources.size()); |
522 |
|
523 |
root = lookupLibrary(rootFrag); |
524 |
assertNotNull(root); |
525 |
|
526 |
subunit1 = lookupLibrary(subunit1Frag); |
527 |
assertNotNull(subunit1); |
528 |
subunit2 = lookupLibrary(subunit2Frag); |
529 |
assertNotNull(subunit2); |
530 |
assertTrue(EcoreUtil.isAncestor(root, subunit2)); |
531 |
assertTrue(EcoreUtil.isAncestor(subunit1, subunit2)); |
532 |
|
533 |
assertTrue(logres.isSeparate(subunit1)); |
534 |
|
535 |
try { |
536 |
logres.absorb(subunit1); |
537 |
} catch (CannotAbsorbException e) { |
538 |
fail("Should not fail to absorb: " + e); //$NON-NLS-1$ |
539 |
} |
540 |
|
541 |
assertFalse(logres.isSeparate(subunit1)); |
542 |
assertEquals(2, resources.size()); |
543 |
|
544 |
saveLogicalResource(); |
545 |
|
546 |
// all files needed to be rewritten by the save, because the subunit2 |
547 |
// was updated to reference the root unit as its parent when subunit1 |
548 |
// was absorbed |
549 |
file = project.getParent().getFile(new Path(RESOURCE_NAME)); |
550 |
assertNotNull(file); |
551 |
assertFalse(rootStamp == file.getModificationStamp()); |
552 |
file = project.getParent().getFile(new Path(SUBUNIT_NAME1)); |
553 |
assertNotNull(file); |
554 |
assertFalse(subunit1Stamp == file.getModificationStamp()); |
555 |
file = project.getParent().getFile(new Path(SUBUNIT_NAME2)); |
556 |
assertNotNull(file); |
557 |
assertFalse(subunit2Stamp == file.getModificationStamp()); |
558 |
} |
559 |
|
560 |
/** |
561 |
* Tests absorption of a leaf unit in multi-level subunit nesting. |
562 |
*/ |
563 |
public void test_absorb_leaf_multiNesting() { |
564 |
String rootFrag = logres.getURIFragment(root); |
565 |
String subunit1Frag = logres.getURIFragment(subunit1); |
566 |
String subunit2Frag = logres.getURIFragment(subunit2); |
567 |
|
568 |
try { |
569 |
logres.separate(subunit1, URI.createPlatformResourceURI(SUBUNIT_NAME1)); |
570 |
logres.separate(subunit2, URI.createPlatformResourceURI(SUBUNIT_NAME2)); |
571 |
} catch (CannotSeparateException e) { |
572 |
fail("Should not fail to separate: " + e); //$NON-NLS-1$ |
573 |
} |
574 |
|
575 |
saveLogicalResource(); |
576 |
|
577 |
// get the file modification stamps for later comparison |
578 |
IFile file = project.getParent().getFile(new Path(RESOURCE_NAME)); |
579 |
assertNotNull(file); |
580 |
long rootStamp = file.getModificationStamp(); |
581 |
file = project.getParent().getFile(new Path(SUBUNIT_NAME1)); |
582 |
assertNotNull(file); |
583 |
long subunit1Stamp = file.getModificationStamp(); |
584 |
file = project.getParent().getFile(new Path(SUBUNIT_NAME2)); |
585 |
assertNotNull(file); |
586 |
long subunit2Stamp = file.getModificationStamp(); |
587 |
|
588 |
// discard and re-load the resource |
589 |
createLogicalResource(RESOURCE_NAME); |
590 |
|
591 |
loadLogicalResource(); |
592 |
|
593 |
Map resources = logres.getMappedResources(); |
594 |
assertEquals(3, resources.size()); |
595 |
|
596 |
root = lookupLibrary(rootFrag); |
597 |
assertNotNull(root); |
598 |
|
599 |
subunit1 = lookupLibrary(subunit1Frag); |
600 |
assertNotNull(subunit1); |
601 |
subunit2 = lookupLibrary(subunit2Frag); |
602 |
assertNotNull(subunit2); |
603 |
assertTrue(EcoreUtil.isAncestor(root, subunit2)); |
604 |
assertTrue(EcoreUtil.isAncestor(subunit1, subunit2)); |
605 |
|
606 |
assertTrue(logres.isSeparate(subunit2)); |
607 |
|
608 |
try { |
609 |
logres.absorb(subunit2); |
610 |
} catch (CannotAbsorbException e) { |
611 |
fail("Should not fail to absorb: " + e); //$NON-NLS-1$ |
612 |
} |
613 |
|
614 |
assertFalse(logres.isSeparate(subunit2)); |
615 |
assertEquals(2, resources.size()); |
616 |
|
617 |
saveLogicalResource(); |
618 |
|
619 |
// the subunit1 and subunit2 files should have been written by the |
620 |
// second save, but not the root file |
621 |
file = project.getParent().getFile(new Path(RESOURCE_NAME)); |
622 |
assertNotNull(file); |
623 |
assertEquals(rootStamp, file.getModificationStamp()); |
624 |
file = project.getParent().getFile(new Path(SUBUNIT_NAME1)); |
625 |
assertNotNull(file); |
626 |
assertFalse(subunit1Stamp == file.getModificationStamp()); |
627 |
file = project.getParent().getFile(new Path(SUBUNIT_NAME2)); |
628 |
assertNotNull(file); |
629 |
assertFalse(subunit2Stamp == file.getModificationStamp()); |
630 |
} |
631 |
|
632 |
/** |
633 |
* Test saving a logical resource with sub-units under a new URI. |
634 |
*/ |
635 |
public void test_saveAs() { |
636 |
String rootFrag = logres.getURIFragment(root); |
637 |
String subunit1Frag = logres.getURIFragment(subunit1); |
638 |
String subunit2Frag = logres.getURIFragment(subunit2); |
639 |
|
640 |
try { |
641 |
logres.separate(subunit1, URI.createPlatformResourceURI(SUBUNIT_NAME1)); |
642 |
logres.separate(subunit2, URI.createPlatformResourceURI(SUBUNIT_NAME2)); |
643 |
} catch (CannotSeparateException e) { |
644 |
fail("Should not fail to separate: " + e); //$NON-NLS-1$ |
645 |
} |
646 |
|
647 |
assertTrue(logres.isModified()); |
648 |
|
649 |
saveLogicalResource(); |
650 |
|
651 |
assertFalse(logres.isModified()); |
652 |
|
653 |
// record the modification stamps of the various units for later |
654 |
// comparison |
655 |
IFile file = project.getParent().getFile(new Path(RESOURCE_NAME)); |
656 |
assertNotNull(file); |
657 |
long rootStamp = file.getModificationStamp(); |
658 |
|
659 |
file = project.getParent().getFile(new Path(SUBUNIT_NAME1)); |
660 |
assertNotNull(file); |
661 |
long subunit1Stamp = file.getModificationStamp(); |
662 |
|
663 |
file = project.getParent().getFile(new Path(SUBUNIT_NAME2)); |
664 |
assertNotNull(file); |
665 |
long subunit2Stamp = file.getModificationStamp(); |
666 |
|
667 |
Map resources = logres.getMappedResources(); |
668 |
assertEquals(3, resources.size()); |
669 |
|
670 |
String newName = "/" + PROJECT_NAME + "/newuri.extlibrary"; //$NON-NLS-1$ //$NON-NLS-2$ |
671 |
logres.setURI(URI.createPlatformResourceURI(newName)); |
672 |
|
673 |
// reduced to a single physical resource |
674 |
assertEquals(1, resources.size()); |
675 |
|
676 |
saveLogicalResource(); |
677 |
|
678 |
// discard and re-load the resource |
679 |
createLogicalResource(newName); |
680 |
|
681 |
loadLogicalResource(); |
682 |
resources = logres.getMappedResources(); |
683 |
assertEquals(1, resources.size()); |
684 |
|
685 |
// verify the structure of the file (that nothing is missing) |
686 |
root = lookupLibrary(rootFrag); |
687 |
assertNotNull(root); |
688 |
|
689 |
subunit1 = lookupLibrary(subunit1Frag); |
690 |
assertNotNull(subunit1); |
691 |
subunit2 = lookupLibrary(subunit2Frag); |
692 |
assertNotNull(subunit2); |
693 |
assertTrue(EcoreUtil.isAncestor(root, subunit2)); |
694 |
assertTrue(EcoreUtil.isAncestor(subunit1, subunit2)); |
695 |
|
696 |
// load the former URI again |
697 |
createLogicalResource(RESOURCE_NAME); |
698 |
|
699 |
loadLogicalResource(); |
700 |
|
701 |
// the old one still has three units |
702 |
resources = logres.getMappedResources(); |
703 |
assertEquals(3, resources.size()); |
704 |
|
705 |
// verify the structure of the file (that nothing is missing) |
706 |
root = lookupLibrary(rootFrag); |
707 |
assertNotNull(root); |
708 |
|
709 |
subunit1 = lookupLibrary(subunit1Frag); |
710 |
assertNotNull(subunit1); |
711 |
subunit2 = lookupLibrary(subunit2Frag); |
712 |
assertNotNull(subunit2); |
713 |
assertTrue(EcoreUtil.isAncestor(root, subunit2)); |
714 |
assertTrue(EcoreUtil.isAncestor(subunit1, subunit2)); |
715 |
|
716 |
// check the modification stamps to verify that the old units are |
717 |
// not changed |
718 |
file = project.getParent().getFile(new Path(RESOURCE_NAME)); |
719 |
assertNotNull(file); |
720 |
assertEquals(rootStamp, file.getModificationStamp()); |
721 |
|
722 |
file = project.getParent().getFile(new Path(SUBUNIT_NAME1)); |
723 |
assertNotNull(file); |
724 |
assertEquals(subunit1Stamp, file.getModificationStamp()); |
725 |
|
726 |
file = project.getParent().getFile(new Path(SUBUNIT_NAME2)); |
727 |
assertNotNull(file); |
728 |
assertEquals(subunit2Stamp, file.getModificationStamp()); |
729 |
} |
730 |
|
731 |
/** Tests undo/redo of unit separation and absorption. */ |
732 |
public void test_undoRedo() { |
733 |
Map resources = logres.getMappedResources(); |
734 |
assertEquals(1, resources.size()); |
735 |
|
736 |
Resource nested = (Resource) resources.get(root); |
737 |
assertNotNull(nested); |
738 |
assertEquals(logres.getURI(), nested.getURI()); |
739 |
|
740 |
nested = (Resource) resources.get(subunit1); |
741 |
assertNull(nested); |
742 |
|
743 |
stopWriting(); |
744 |
startWriting(); |
745 |
|
746 |
try { |
747 |
logres.separate(subunit1, URI.createPlatformResourceURI(SUBUNIT_NAME1)); |
748 |
} catch (CannotSeparateException e) { |
749 |
fail("Should not fail to separate: " + e); //$NON-NLS-1$ |
750 |
} |
751 |
|
752 |
nested = (Resource) resources.get(subunit1); |
753 |
assertNotNull(nested); |
754 |
|
755 |
MUndoInterval undo = stopWriting(); |
756 |
|
757 |
// test undo of separation |
758 |
undo.undo(); |
759 |
|
760 |
startReading(); |
761 |
|
762 |
nested = (Resource) resources.get(subunit1); |
763 |
assertNull(nested); |
764 |
assertFalse(logres.isSeparate(subunit1)); |
765 |
|
766 |
stopReading(); |
767 |
|
768 |
// test redo of separation |
769 |
undo.redo(); |
770 |
|
771 |
startReading(); |
772 |
|
773 |
nested = (Resource) resources.get(subunit1); |
774 |
assertNotNull(nested); |
775 |
assertTrue(logres.isSeparate(subunit1)); |
776 |
|
777 |
stopReading(); |
778 |
|
779 |
startWriting(); |
780 |
|
781 |
try { |
782 |
logres.absorb(subunit1); |
783 |
} catch (CannotAbsorbException e) { |
784 |
fail("Should not fail to absorb: " + e); //$NON-NLS-1$ |
785 |
} |
786 |
nested = (Resource) resources.get(subunit1); |
787 |
assertNull(nested); |
788 |
|
789 |
undo = stopWriting(); |
790 |
|
791 |
// test undo of absorption |
792 |
undo.undo(); |
793 |
|
794 |
startReading(); |
795 |
|
796 |
nested = (Resource) resources.get(subunit1); |
797 |
assertNotNull(nested); |
798 |
assertTrue(logres.isSeparate(subunit1)); |
799 |
|
800 |
stopReading(); |
801 |
|
802 |
// test undo of absorption |
803 |
undo.redo(); |
804 |
|
805 |
startReading(); |
806 |
|
807 |
nested = (Resource) resources.get(subunit1); |
808 |
assertNull(nested); |
809 |
assertFalse(logres.isSeparate(subunit1)); |
810 |
|
811 |
stopReading(); |
812 |
} |
813 |
|
814 |
/** |
815 |
* Tests that the dirty states of units are correctly updated when |
816 |
* separating an element. |
817 |
*/ |
818 |
public void test_dirtyState_separate() { |
819 |
saveLogicalResource(); |
820 |
|
821 |
Resource rootRes = (Resource) logres.getMappedResources().get(root); |
822 |
|
823 |
assertFalse(logres.isModified()); |
824 |
assertFalse(rootRes.isModified()); |
825 |
|
826 |
try { |
827 |
logres.separate(subunit1, URI.createPlatformResourceURI(SUBUNIT_NAME1)); |
828 |
} catch (CannotSeparateException e) { |
829 |
fail("Should not fail to separate: " + e); //$NON-NLS-1$ |
830 |
} |
831 |
|
832 |
Resource sub1Res = (Resource) logres.getMappedResources().get(subunit1); |
833 |
|
834 |
assertTrue(logres.isModified()); |
835 |
assertTrue(rootRes.isModified()); |
836 |
assertTrue(sub1Res.isModified()); |
837 |
|
838 |
saveLogicalResource(); |
839 |
|
840 |
assertFalse(logres.isModified()); |
841 |
assertFalse(rootRes.isModified()); |
842 |
assertFalse(sub1Res.isModified()); |
843 |
} |
844 |
|
845 |
/** |
846 |
* Tests that the dirty states of units are correctly updated when |
847 |
* absorbing an element. |
848 |
*/ |
849 |
public void test_dirtyState_absorb() { |
850 |
try { |
851 |
logres.separate(subunit1, URI.createPlatformResourceURI(SUBUNIT_NAME1)); |
852 |
} catch (CannotSeparateException e) { |
853 |
fail("Should not fail to separate: " + e); //$NON-NLS-1$ |
854 |
} |
855 |
|
856 |
saveLogicalResource(); |
857 |
|
858 |
Resource rootRes = (Resource) logres.getMappedResources().get(root); |
859 |
Resource sub1Res = (Resource) logres.getMappedResources().get(subunit1); |
860 |
|
861 |
assertFalse(logres.isModified()); |
862 |
assertFalse(rootRes.isModified()); |
863 |
assertFalse(sub1Res.isModified()); |
864 |
|
865 |
try { |
866 |
logres.absorb(subunit1); |
867 |
} catch (CannotAbsorbException e) { |
868 |
fail("Should not fail to absorb: " + e); //$NON-NLS-1$ |
869 |
} |
870 |
|
871 |
assertTrue(logres.isModified()); |
872 |
assertTrue(rootRes.isModified()); |
873 |
assertTrue(sub1Res.isModified()); |
874 |
|
875 |
saveLogicalResource(); |
876 |
|
877 |
assertFalse(logres.isModified()); |
878 |
assertFalse(rootRes.isModified()); |
879 |
assertFalse(sub1Res.isModified()); |
880 |
} |
881 |
|
882 |
/** |
883 |
* Tests that the dirty states of units are correctly updated when |
884 |
* modifying an element. |
885 |
*/ |
886 |
public void test_dirtyState_modify() { |
887 |
try { |
888 |
logres.separate(subunit1, URI.createPlatformResourceURI(SUBUNIT_NAME1)); |
889 |
} catch (CannotSeparateException e) { |
890 |
fail("Should not fail to separate: " + e); //$NON-NLS-1$ |
891 |
} |
892 |
|
893 |
saveLogicalResource(); |
894 |
|
895 |
Resource rootRes = (Resource) logres.getMappedResources().get(root); |
896 |
Resource sub1Res = (Resource) logres.getMappedResources().get(subunit1); |
897 |
|
898 |
assertFalse(logres.isModified()); |
899 |
assertFalse(rootRes.isModified()); |
900 |
assertFalse(sub1Res.isModified()); |
901 |
|
902 |
subunit1.setAddress("500 Pennsylvania Ave."); //$NON-NLS-1$ |
903 |
|
904 |
assertTrue(logres.isModified()); |
905 |
assertFalse(rootRes.isModified()); // didn't update the root unit |
906 |
assertTrue(sub1Res.isModified()); |
907 |
|
908 |
saveLogicalResource(); |
909 |
|
910 |
assertFalse(logres.isModified()); |
911 |
assertFalse(rootRes.isModified()); |
912 |
assertFalse(sub1Res.isModified()); |
913 |
|
914 |
root.setAddress("10 Downing St."); //$NON-NLS-1$ |
915 |
|
916 |
assertTrue(logres.isModified()); |
917 |
assertTrue(rootRes.isModified()); |
918 |
assertFalse(sub1Res.isModified()); // didn't update the sub-unit |
919 |
} |
920 |
|
921 |
/** Tests the EMF-style listening for notifications from adapters. */ |
922 |
public void test_basicEventing() { |
923 |
class TestAdapter extends AdapterImpl { |
924 |
List notifications = new java.util.ArrayList(); |
925 |
|
926 |
public void notifyChanged(Notification msg) { |
927 |
if (!(msg.getNotifier() instanceof Resource) || |
928 |
(msg.getFeatureID(Resource.class) == Resource.RESOURCE__CONTENTS)) { |
929 |
// don't want the touch events for resource isModified |
930 |
notifications.add(msg); |
931 |
} |
932 |
} |
933 |
} |
934 |
|
935 |
TestAdapter adapter = new TestAdapter(); |
936 |
|
937 |
URI logicalUri = logres.getURI(); |
938 |
URI subunitUri = URI.createPlatformResourceURI(SUBUNIT_NAME1); |
939 |
|
940 |
// listen for notifications of separation/absorption on the parent |
941 |
EObject parent = subunit1.eContainer(); |
942 |
parent.eAdapters().add(adapter); |
943 |
|
944 |
try { |
945 |
logres.separate(subunit1, subunitUri); |
946 |
} catch (CannotSeparateException e) { |
947 |
fail("Should not fail to separate: " + e); //$NON-NLS-1$ |
948 |
} |
949 |
|
950 |
assertEquals(1, adapter.notifications.size()); |
951 |
Notification event = (Notification) adapter.notifications.get(0); |
952 |
adapter.notifications.clear(); |
953 |
|
954 |
assertSame(parent, event.getNotifier()); |
955 |
assertEquals(EventTypes.SEPARATE, event.getEventType()); |
956 |
assertSame(EXTLibraryPackage.eINSTANCE.getLibrary_Branches(), event.getFeature()); |
957 |
assertEquals(logicalUri, ((Resource) event.getOldValue()).getURI()); |
958 |
assertEquals(subunitUri, ((Resource) event.getNewValue()).getURI()); |
959 |
|
960 |
try { |
961 |
logres.absorb(subunit1); |
962 |
} catch (CannotAbsorbException e) { |
963 |
fail("Should not fail to absorb: " + e); //$NON-NLS-1$ |
964 |
} |
965 |
|
966 |
assertEquals(1, adapter.notifications.size()); |
967 |
event = (Notification) adapter.notifications.get(0); |
968 |
adapter.notifications.clear(); |
969 |
|
970 |
assertSame(parent, event.getNotifier()); |
971 |
assertEquals(EventTypes.ABSORB, event.getEventType()); |
972 |
assertSame(EXTLibraryPackage.eINSTANCE.getLibrary_Branches(), event.getFeature()); |
973 |
assertEquals(subunitUri, ((Resource) event.getOldValue()).getURI()); |
974 |
assertEquals(logicalUri, ((Resource) event.getNewValue()).getURI()); |
975 |
|
976 |
logres.eAdapters().add(adapter); |
977 |
|
978 |
try { |
979 |
logres.separate(root, subunitUri); // logical root |
980 |
} catch (CannotSeparateException e) { |
981 |
fail("Should not fail to separate: " + e); //$NON-NLS-1$ |
982 |
} |
983 |
|
984 |
assertEquals(1, adapter.notifications.size()); |
985 |
event = (Notification) adapter.notifications.get(0); |
986 |
adapter.notifications.clear(); |
987 |
|
988 |
assertSame(logres, event.getNotifier()); |
989 |
assertEquals(EventTypes.SEPARATE, event.getEventType()); |
990 |
assertSame(null, event.getFeature()); |
991 |
assertEquals(Resource.RESOURCE__CONTENTS, event.getFeatureID(Resource.class)); |
992 |
assertEquals(logicalUri, ((Resource) event.getOldValue()).getURI()); |
993 |
assertEquals(subunitUri, ((Resource) event.getNewValue()).getURI()); |
994 |
|
995 |
try { |
996 |
logres.absorb(root); // logical root |
997 |
} catch (CannotAbsorbException e) { |
998 |
fail("Should not fail to absorb: " + e); //$NON-NLS-1$ |
999 |
} |
1000 |
|
1001 |
assertEquals(1, adapter.notifications.size()); |
1002 |
event = (Notification) adapter.notifications.get(0); |
1003 |
adapter.notifications.clear(); |
1004 |
|
1005 |
assertSame(logres, event.getNotifier()); |
1006 |
assertEquals(EventTypes.ABSORB, event.getEventType()); |
1007 |
assertSame(null, event.getFeature()); |
1008 |
assertEquals(Resource.RESOURCE__CONTENTS, event.getFeatureID(Resource.class)); |
1009 |
assertEquals(subunitUri, ((Resource) event.getOldValue()).getURI()); |
1010 |
assertEquals(logicalUri, ((Resource) event.getNewValue()).getURI()); |
1011 |
} |
1012 |
|
1013 |
/** Tests the MSL-style listening for notifications from MListeners. */ |
1014 |
public void test_mslEventing() { |
1015 |
class TestListener extends DemuxedMListener { |
1016 |
Notification event; |
1017 |
ILogicalResource res; |
1018 |
EObject element; |
1019 |
Resource subunit; |
1020 |
|
1021 |
TestListener() { |
1022 |
super(domain); |
1023 |
} |
1024 |
|
1025 |
public MFilter getFilter() { |
1026 |
return MFilter.SEPARATED_ABSORBED_FILTER; |
1027 |
} |
1028 |
|
1029 |
public void handleElementSeparatedEvent( |
1030 |
Notification notification, ILogicalResource resource, |
1031 |
EObject eObject, Resource newResource) { |
1032 |
event = notification; |
1033 |
res = resource; |
1034 |
element = eObject; |
1035 |
subunit = newResource; |
1036 |
} |
1037 |
|
1038 |
public void handleElementAbsorbedEvent( |
1039 |
Notification notification, ILogicalResource resource, |
1040 |
EObject eObject, Resource oldResource) { |
1041 |
event = notification; |
1042 |
res = resource; |
1043 |
element = eObject; |
1044 |
subunit = oldResource; |
1045 |
} |
1046 |
} |
1047 |
|
1048 |
TestListener listener = new TestListener(); |
1049 |
listener.startListening(); |
1050 |
|
1051 |
URI subunitUri = URI.createPlatformResourceURI(SUBUNIT_NAME1); |
1052 |
|
1053 |
try { |
1054 |
logres.separate(subunit1, subunitUri); |
1055 |
} catch (CannotSeparateException e) { |
1056 |
fail("Should not fail to separate: " + e); //$NON-NLS-1$ |
1057 |
} |
1058 |
|
1059 |
// must complete the write action in order for events to fire |
1060 |
stopWriting(); |
1061 |
|
1062 |
assertSame(subunit1, listener.element); |
1063 |
assertEquals(EventTypes.SEPARATE, listener.event.getEventType()); |
1064 |
assertSame(logres, listener.res); |
1065 |
assertEquals(subunitUri, listener.subunit.getURI()); |
1066 |
|
1067 |
startWriting(); |
1068 |
|
1069 |
try { |
1070 |
logres.absorb(subunit1); |
1071 |
} catch (CannotAbsorbException e) { |
1072 |
fail("Should not fail to absorb: " + e); //$NON-NLS-1$ |
1073 |
} |
1074 |
|
1075 |
// must complete the write action in order for events to fire |
1076 |
stopWriting(); |
1077 |
|
1078 |
assertSame(subunit1, listener.element); |
1079 |
assertEquals(EventTypes.ABSORB, listener.event.getEventType()); |
1080 |
assertSame(logres, listener.res); |
1081 |
assertEquals(subunitUri, listener.subunit.getURI()); |
1082 |
|
1083 |
try { |
1084 |
logres.separate(root, subunitUri); // logical root |
1085 |
} catch (CannotSeparateException e) { |
1086 |
fail("Should not fail to separate: " + e); //$NON-NLS-1$ |
1087 |
} |
1088 |
|
1089 |
// must complete the write action in order for events to fire |
1090 |
stopWriting(); |
1091 |
|
1092 |
assertSame(root, listener.element); |
1093 |
assertEquals(EventTypes.SEPARATE, listener.event.getEventType()); |
1094 |
assertSame(logres, listener.res); |
1095 |
assertEquals(subunitUri, listener.subunit.getURI()); |
1096 |
|
1097 |
startWriting(); |
1098 |
|
1099 |
try { |
1100 |
logres.absorb(root); // logical root |
1101 |
} catch (CannotAbsorbException e) { |
1102 |
fail("Should not fail to absorb: " + e); //$NON-NLS-1$ |
1103 |
} |
1104 |
|
1105 |
// must complete the write action in order for events to fire |
1106 |
stopWriting(); |
1107 |
|
1108 |
assertSame(root, listener.element); |
1109 |
assertEquals(EventTypes.ABSORB, listener.event.getEventType()); |
1110 |
assertSame(logres, listener.res); |
1111 |
assertEquals(subunitUri, listener.subunit.getURI()); |
1112 |
} |
1113 |
|
1114 |
/** |
1115 |
* Tests that we can correctly load models from the 6.x physical structure. |
1116 |
*/ |
1117 |
public void test_compatibility_6_0() { |
1118 |
URI uri = URI.createURI(MslCoreTestsBundle.getEntry( |
1119 |
"/test_models/test_6_0_format.extlibrary").toString()); //$NON-NLS-1$ |
1120 |
logres = createNewLogicalResource(uri); |
1121 |
loadLogicalResource(); |
1122 |
|
1123 |
assertTrue(logres.isLoaded()); |
1124 |
assertEquals(1, ((ResourceSet) logres.getAdapter(ResourceSet.class)).getResources().size()); |
1125 |
|
1126 |
assertEquals(1, logres.getContents().size()); |
1127 |
Library library = (Library) logres.getContents().get(0); |
1128 |
|
1129 |
assertEquals("Library of Congress", library.getName()); //$NON-NLS-1$ |
1130 |
|
1131 |
assertEquals(1, library.getWriters().size()); |
1132 |
Writer writer = (Writer) library.getWriters().get(0); |
1133 |
assertEquals("Master Librarian", writer.getName()); //$NON-NLS-1$ |
1134 |
|
1135 |
assertEquals(1, library.getBooks().size()); |
1136 |
Book book = (Book) library.getBooks().get(0); |
1137 |
assertEquals("All Books In Existence", book.getTitle()); //$NON-NLS-1$ |
1138 |
|
1139 |
assertSame(writer, book.getAuthor()); |
1140 |
assertEquals(1, writer.getBooks().size()); |
1141 |
assertSame(book, writer.getBooks().get(0)); |
1142 |
} |
1143 |
|
1144 |
/** |
1145 |
* Tests that we properly propagate errors from the unit to the logical |
1146 |
* resource. |
1147 |
*/ |
1148 |
public void test_load_errors_RATLC00537775() { |
1149 |
URI uri = URI.createURI(MslCoreTestsBundle.getEntry( |
1150 |
"/test_models/test_corrupted_model.extlibrary").toString()); //$NON-NLS-1$ |
1151 |
logres = createNewLogicalResource(uri); |
1152 |
|
1153 |
Map options = new java.util.HashMap(); |
1154 |
|
1155 |
options.put(ILogicalResource.OPTION_LOAD_ALL_UNITS, |
1156 |
Boolean.TRUE); |
1157 |
options.put(ILogicalResource.OPTION_AUTO_LOAD_UNITS, |
1158 |
Boolean.TRUE); |
1159 |
|
1160 |
try { |
1161 |
logres.load(options); |
1162 |
} catch (IOException e) { |
1163 |
// Check to ensure that the resource is loaded but it has |
1164 |
// errors. |
1165 |
assertTrue(logres.isLoaded()); |
1166 |
assertTrue(!logres.getErrors().isEmpty()); |
1167 |
return; |
1168 |
} |
1169 |
|
1170 |
fail("Logical resource load should have failed because of corrupted XML."); //$NON-NLS-1$ |
1171 |
} |
1172 |
|
1173 |
/** |
1174 |
* Tests that the DemuxingMListener will not report a model unloaded when |
1175 |
* the errors list of a resource is emptied. |
1176 |
*/ |
1177 |
public void test_load_errors_RATLC00537693() { |
1178 |
final MEditingDomain editingDomain = MEditingDomain.createNewDomain(); |
1179 |
URI uri = URI.createURI(MslCoreTestsBundle.getEntry( |
1180 |
"/test_models/test_corrupted_model.extlibrary").toString()); //$NON-NLS-1$ |
1181 |
|
1182 |
final Resource[] r = new Resource[1]; |
1183 |
|
1184 |
try { |
1185 |
r[0] = editingDomain.createResource(uri.toString(),MResourceOption.URI); |
1186 |
r[0].unload(); |
1187 |
} catch (Exception e) { |
1188 |
// Do nothing, just proceed. |
1189 |
} |
1190 |
|
1191 |
Map options = new java.util.HashMap(); |
1192 |
|
1193 |
options.put(ILogicalResource.OPTION_LOAD_ALL_UNITS, |
1194 |
Boolean.TRUE); |
1195 |
options.put(ILogicalResource.OPTION_AUTO_LOAD_UNITS, |
1196 |
Boolean.TRUE); |
1197 |
|
1198 |
try { |
1199 |
r[0].load(options); |
1200 |
// We shouldn't get this far. |
1201 |
fail(); |
1202 |
} catch (IOException e) { |
1203 |
// Do nothing. |
1204 |
} |
1205 |
|
1206 |
// Make sure that there are errors to be cleared from the resource. |
1207 |
assertTrue(r[0].getErrors().size() > 0); |
1208 |
|
1209 |
final int[] listenerCalled = new int[1]; |
1210 |
listenerCalled[0] = 0; |
1211 |
|
1212 |
aListener = new DemuxedMListener(editingDomain) { |
1213 |
public void handleResourceUnloadedEvent(Notification notification, Resource resource, EObject modelRoot) { |
1214 |
listenerCalled[0]++; |
1215 |
} |
1216 |
}; |
1217 |
aListener.startListening(); |
1218 |
|
1219 |
// Clear all of the errors from the resource in a proper write action. |
1220 |
editingDomain.runInUndoInterval(new Runnable() { |
1221 |
public void run() { |
1222 |
try { |
1223 |
editingDomain.runAsWrite(new MRunnable() { |
1224 |
public Object run() { |
1225 |
r[0].getErrors().clear(); |
1226 |
return null; |
1227 |
} |
1228 |
}); |
1229 |
} catch (MSLActionAbandonedException e) { |
1230 |
fail(); |
1231 |
} |
1232 |
} |
1233 |
}); |
1234 |
|
1235 |
assertEquals(0,listenerCalled[0]); |
1236 |
} |
1237 |
} |