Bug 550297 - Context menu 'Create View Model Project' action fails to
load ecore

* Count usages
* add test

Change-Id: Ia75d8a5b3738f38ed1b7de6eb655eb2650a37e77
Signed-off-by: Johannes Faltermeier <jfaltermeier@eclipsesource.com>
diff --git a/bundles/org.eclipse.emf.ecp.ide.util/src/org/eclipse/emf/ecp/ide/spi/util/EcoreHelper.java b/bundles/org.eclipse.emf.ecp.ide.util/src/org/eclipse/emf/ecp/ide/spi/util/EcoreHelper.java
index 3ad4390..b1aab16 100644
--- a/bundles/org.eclipse.emf.ecp.ide.util/src/org/eclipse/emf/ecp/ide/spi/util/EcoreHelper.java
+++ b/bundles/org.eclipse.emf.ecp.ide.util/src/org/eclipse/emf/ecp/ide/spi/util/EcoreHelper.java
@@ -147,6 +147,9 @@
 	 */
 	private static final Map<String, String> ECOREPATH_TO_WORKSPACEURI = new HashMap<String, String>();
 
+	/** Keeps track of how many times an ecore path was registered in order to not unregister it prematurely. */
+	private static final Map<String, Integer> ECOREPATH_TO_REGISTRATIONCOUNT = new HashMap<String, Integer>();
+
 	/**
 	 * Contains mapping between an platform resource URI and the URIs which reference it.
 	 */
@@ -184,6 +187,7 @@
 		initResourceSet(physicalResourceSet, true);
 		final URI uri = URI.createPlatformResourceURI(ecorePath, false);
 		ECOREPATH_TO_WORKSPACEURI.put(ecorePath, uri.toString());
+		ECOREPATH_TO_REGISTRATIONCOUNT.put(ecorePath, ECOREPATH_TO_REGISTRATIONCOUNT.getOrDefault(ecorePath, 0) + 1);
 		final Resource r = physicalResourceSet.createResource(uri);
 
 		loadResource(ecorePath, r);
@@ -360,6 +364,14 @@
 		if (ecorePath == null || !ECOREPATH_TO_WORKSPACEURI.containsKey(ecorePath)) {
 			return;
 		}
+
+		ECOREPATH_TO_REGISTRATIONCOUNT.put(ecorePath, ECOREPATH_TO_REGISTRATIONCOUNT.getOrDefault(ecorePath, 1) - 1);
+		if (ECOREPATH_TO_REGISTRATIONCOUNT.get(ecorePath) == 0) {
+			ECOREPATH_TO_REGISTRATIONCOUNT.remove(ecorePath);
+		} else {
+			return;
+		}
+
 		final String uriToUnregister = ECOREPATH_TO_WORKSPACEURI.remove(ecorePath);
 
 		final Set<String> referencedBy = WORKSPACEURI_REFERENCEDBY.get(uriToUnregister);
diff --git a/tests/org.eclipse.emf.ecp.ide.util.test/src/org/eclipse/emf/ecp/ide/util/test/EcoreHelperNoDependencies_PTest.java b/tests/org.eclipse.emf.ecp.ide.util.test/src/org/eclipse/emf/ecp/ide/util/test/EcoreHelperNoDependencies_PTest.java
index e8fcef7..1f6681a 100644
--- a/tests/org.eclipse.emf.ecp.ide.util.test/src/org/eclipse/emf/ecp/ide/util/test/EcoreHelperNoDependencies_PTest.java
+++ b/tests/org.eclipse.emf.ecp.ide.util.test/src/org/eclipse/emf/ecp/ide/util/test/EcoreHelperNoDependencies_PTest.java
@@ -119,4 +119,31 @@
 			packageRegistry.containsKey("a.nsuri"));
 	}
 
+	@Test
+	public void testMultipleRegistration() throws IOException {
+		// check initial setup
+		assertFalse("Package is already in the registry!",
+			packageRegistry.containsKey("a.nsuri"));
+
+		// register
+		EcoreHelper.registerEcore(aEcorePath);
+		assertTrue("Package not in the registry!",
+			packageRegistry.containsKey("a.nsuri"));
+
+		// register a second time
+		EcoreHelper.registerEcore(aEcorePath);
+		assertTrue("Package not in the registry!",
+			packageRegistry.containsKey("a.nsuri"));
+
+		// unregister
+		EcoreHelper.unregisterEcore(aEcorePath);
+		assertTrue("Package not in the registry!",
+			packageRegistry.containsKey("a.nsuri"));
+
+		// unregister a second time
+		EcoreHelper.unregisterEcore(aEcorePath);
+		assertFalse("Package is still in the registry!",
+			packageRegistry.containsKey("a.nsuri"));
+	}
+
 }