CollectionPropertyTests: minor adjustment to better test caching by type (fixes #508075)

While trying to have the standard non-plugged tests run, I found that the new caching
using adapters did not handle well the case in which we added instances with their own
contents - these would get notified as one ADD event, not as multiple ADDitions.

I've reverted the old change that inverted removals in EmfModel, and instead I have the
handling of adapter events go through the contents of the added/removed elements recursively.

I also avoid changing the cache through adapter events if the allContents/type/kind caches
have not been initialized previously by an explicit request to one of the allContents()
/ getAllOfType() / getAllOfKind() methods. Otherwise, the cache would be left in an invalid
state.

These bugs are easy to trigger if we just change the order in which the EUnitRunner prepares
the "Ecore" model, so I'm reusing the test for that as well by having the InMemoryEmfModel
created *before* it's populated.
diff --git a/plugins/org.eclipse.epsilon.emc.emf/src/org/eclipse/epsilon/emc/emf/EmfModel.java b/plugins/org.eclipse.epsilon.emc.emf/src/org/eclipse/epsilon/emc/emf/EmfModel.java
index c6e9913..4155f08 100644
--- a/plugins/org.eclipse.epsilon.emc.emf/src/org/eclipse/epsilon/emc/emf/EmfModel.java
+++ b/plugins/org.eclipse.epsilon.emc.emf/src/org/eclipse/epsilon/emc/emf/EmfModel.java
@@ -320,11 +320,17 @@
 	 */

 	protected void forceAddToCache(EObject instance) throws EolModelElementTypeNotFoundException {

 		super.addToCache(instance.eClass().getName(), instance);

+		for (EObject child : instance.eContents()) {

+			forceAddToCache(child);

+		}

 	}

 

 	/** @see #forceAddToCache(String, EObject) */

 	protected void forceRemoveFromCache(EObject instance) throws EolModelElementTypeNotFoundException {

 		super.removeFromCache(instance);

+		for (EObject child : instance.eContents()) {

+			forceRemoveFromCache(child);

+		}

 	}

 	

 	public void setupContainmentChangeListeners() {

diff --git a/plugins/org.eclipse.epsilon.eol.engine/src/org/eclipse/epsilon/eol/models/CachedModel.java b/plugins/org.eclipse.epsilon.eol.engine/src/org/eclipse/epsilon/eol/models/CachedModel.java
index 6d45f11..654ca23 100644
--- a/plugins/org.eclipse.epsilon.eol.engine/src/org/eclipse/epsilon/eol/models/CachedModel.java
+++ b/plugins/org.eclipse.epsilon.eol.engine/src/org/eclipse/epsilon/eol/models/CachedModel.java
@@ -74,21 +74,38 @@
 	protected boolean cachingEnabled = true;
 	
 	protected void addToCache(String type, ModelElementType instance) throws EolModelElementTypeNotFoundException {
-		allContentsCache.add(instance);
-		typeCache.put(getCacheKeyForType(type), instance);
+		if (allContentsAreCached) {
+			allContentsCache.add(instance);
+		}
+
+		Object typeCacheKey = getCacheKeyForType(type);
+		if (typeCache.containsKey(typeCacheKey)) {
+			typeCache.put(typeCacheKey, instance);
+		}
 
 		for (String kind : getAllTypeNamesOf(instance)) {
-			kindCache.put(getCacheKeyForType(kind), instance);
+			Object kindCacheKey = getCacheKeyForType(kind);
+			if (kindCache.containsKey(kindCacheKey)) {
+				kindCache.put(kindCacheKey, instance);
+			}
 		}
 	}
 
 	protected void removeFromCache(ModelElementType instance) throws EolModelElementTypeNotFoundException {
-		allContentsCache.remove(instance);
+		if (allContentsAreCached) {
+			allContentsCache.remove(instance);
+		}
 
-		typeCache.remove(getCacheKeyForType(getTypeNameOf(instance)), instance);
+		final Object typeCacheKey = getCacheKeyForType(getTypeNameOf(instance));
+		if (typeCache.containsKey(typeCacheKey)) {
+			typeCache.remove(typeCacheKey, instance);
+		}
 
 		for (String kind : getAllTypeNamesOf(instance)) {
-			kindCache.remove(getCacheKeyForType(kind), instance);
+			final Object kindCacheKey = getCacheKeyForType(kind);
+			if (kindCache.containsKey(kindCacheKey)) {
+				kindCache.remove(kindCacheKey, instance);
+			}
 		}
 	}
 
diff --git a/tests/org.eclipse.epsilon.eol.engine.test.acceptance/src/org/eclipse/epsilon/eol/engine/test/acceptance/CollectionPropertyTests.eol b/tests/org.eclipse.epsilon.eol.engine.test.acceptance/src/org/eclipse/epsilon/eol/engine/test/acceptance/CollectionPropertyTests.eol
index c8aa485..b340ded 100644
--- a/tests/org.eclipse.epsilon.eol.engine.test.acceptance/src/org/eclipse/epsilon/eol/engine/test/acceptance/CollectionPropertyTests.eol
+++ b/tests/org.eclipse.epsilon.eol.engine.test.acceptance/src/org/eclipse/epsilon/eol/engine/test/acceptance/CollectionPropertyTests.eol
@@ -13,5 +13,5 @@
 	var col : Sequence;
 	col.addAll(EClass.all);
 	col.addAll(EPackage.all);
-	assertEquals(col.name.size(), 21);
+	assertEquals(21, col.name.size());
 }
\ No newline at end of file
diff --git a/tests/org.eclipse.epsilon.eol.engine.test.acceptance/src/org/eclipse/epsilon/eol/engine/test/acceptance/eunit/EUnitRunner.java b/tests/org.eclipse.epsilon.eol.engine.test.acceptance/src/org/eclipse/epsilon/eol/engine/test/acceptance/eunit/EUnitRunner.java
index 89d707b..d82eee8 100644
--- a/tests/org.eclipse.epsilon.eol.engine.test.acceptance/src/org/eclipse/epsilon/eol/engine/test/acceptance/eunit/EUnitRunner.java
+++ b/tests/org.eclipse.epsilon.eol.engine.test.acceptance/src/org/eclipse/epsilon/eol/engine/test/acceptance/eunit/EUnitRunner.java
@@ -143,10 +143,10 @@
 		ResourceSet rs = new ResourceSetImpl();

 		rs.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", new XMIResourceFactoryImpl());

 		Resource r = rs.createResource(URI.createFileURI("foo.xmi"));

+		InMemoryEmfModel ecore = new InMemoryEmfModel("Ecore", r);

 		EObject copy = EcoreUtil.copy(EcorePackage.eINSTANCE.eResource().getContents().get(0));

 		r.getContents().add(copy);

 		rs.getResources().add(r);

-		InMemoryEmfModel ecore = new InMemoryEmfModel("Ecore", r);

 		return ecore;

 	}