[401682] Provide a routine to initialize the pathmap URI-mappings
https://bugs.eclipse.org/bugs/show_bug.cgi?id=401682

Extract (and fix) the stand-alone setup code for UML resources from the test framework to use it in the UMLResources API that currently relies on clients to provide the platform:/plugin URI mappings themselves (somehow).
diff --git a/plugins/org.eclipse.uml2.uml.resources/src/org/eclipse/uml2/uml/resources/ResourcesPlugin.java b/plugins/org.eclipse.uml2.uml.resources/src/org/eclipse/uml2/uml/resources/ResourcesPlugin.java
index 5f0dc3e..00e0672 100644
--- a/plugins/org.eclipse.uml2.uml.resources/src/org/eclipse/uml2/uml/resources/ResourcesPlugin.java
+++ b/plugins/org.eclipse.uml2.uml.resources/src/org/eclipse/uml2/uml/resources/ResourcesPlugin.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012 CEA and others.
+ * Copyright (c) 2012, 2013 CEA and others.
  * All rights reserved.   This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -7,6 +7,7 @@
  *
  * Contributors:
  *   CEA - initial API and implementation
+ *   Christian W. Damus (CEA) - 401682
  *
  */
 package org.eclipse.uml2.uml.resources;
@@ -21,6 +22,11 @@
 		extends EMFPlugin {
 
 	/**
+	 * @since 4.2
+	 */
+	public static final String PLUGIN_ID = "org.eclipse.uml2.uml.resources";
+
+	/**
 	 * The singleton instance of the plugin.
 	 */
 	public static final ResourcesPlugin INSTANCE = new ResourcesPlugin();
diff --git a/plugins/org.eclipse.uml2.uml.resources/src/org/eclipse/uml2/uml/resources/util/UMLResourcesUtil.java b/plugins/org.eclipse.uml2.uml.resources/src/org/eclipse/uml2/uml/resources/util/UMLResourcesUtil.java
index f644e83..3d28f82 100644
--- a/plugins/org.eclipse.uml2.uml.resources/src/org/eclipse/uml2/uml/resources/util/UMLResourcesUtil.java
+++ b/plugins/org.eclipse.uml2.uml.resources/src/org/eclipse/uml2/uml/resources/util/UMLResourcesUtil.java
@@ -9,10 +9,11 @@
  *   CEA - initial API and implementation
  *   Kenn Hussey (CEA) - 389542, 399544
  *   Mikael Barbero (Obeo) - 414572
- *   Christian W. Damus (CEA) - 414572
+ *   Christian W. Damus (CEA) - 414572, 401682
  */
 package org.eclipse.uml2.uml.resources.util;
 
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -37,6 +38,7 @@
 import org.eclipse.uml2.uml.resource.UML302UMLResource;
 import org.eclipse.uml2.uml.resource.UMLResource;
 import org.eclipse.uml2.uml.resource.XMI2UMLResource;
+import org.eclipse.uml2.uml.resources.ResourcesPlugin;
 import org.eclipse.uml2.uml.util.UMLUtil;
 
 /**
@@ -359,15 +361,13 @@
 	 * @since 4.2
 	 */
 	public static Map<URI, URI> initURIConverterURIMap(Map<URI, URI> uriMap) {
-		uriMap.put(URI.createURI(UMLResource.LIBRARIES_PATHMAP), URI
-			.createPlatformPluginURI(
-				"/org.eclipse.uml2.uml.resources/libraries/", true)); //$NON-NLS-1$
-		uriMap.put(URI.createURI(UMLResource.METAMODELS_PATHMAP), URI
-			.createPlatformPluginURI(
-				"/org.eclipse.uml2.uml.resources/metamodels/", true)); //$NON-NLS-1$
-		uriMap.put(URI.createURI(UMLResource.PROFILES_PATHMAP), URI
-			.createPlatformPluginURI(
-				"/org.eclipse.uml2.uml.resources/profiles/", true)); //$NON-NLS-1$
+		URI baseURI = getBaseUMLResourceURI();
+		mapUMLResourceURIs(uriMap, UMLResource.METAMODELS_PATHMAP,
+			baseURI.appendSegment("metamodels")); //$NON-NLS-1$
+		mapUMLResourceURIs(uriMap, UMLResource.PROFILES_PATHMAP,
+			baseURI.appendSegment("profiles")); //$NON-NLS-1$
+		mapUMLResourceURIs(uriMap, UMLResource.LIBRARIES_PATHMAP,
+			baseURI.appendSegment("libraries")); //$NON-NLS-1$
 
 		return uriMap;
 	}
@@ -397,4 +397,55 @@
 
 		return ePackageNsURIToProfileLocationMap;
 	}
+
+	private static URI getBaseUMLResourceURI() {
+		URI umlMetamodel = URI.createURI(UMLResource.UML_METAMODEL_URI);
+		URL resultURL = UMLResourcesUtil.class.getClassLoader().getResource(
+			String.format("metamodels/%s", umlMetamodel.lastSegment())); //$NON-NLS-1$
+
+		URI result;
+
+		if (resultURL != null) {
+			// remove the /metamodel/UML.metamodel.uml segments of the resource
+			// we found
+			result = URI.createURI(resultURL.toExternalForm(), true)
+				.trimSegments(2);
+		} else {
+			// probably, we're not running with JARs, so assume the source
+			// project folder layout
+			resultURL = UMLResourcesUtil.class
+				.getResource("UMLResourcesUtil.class"); //$NON-NLS-1$
+
+			String baseURL = resultURL.toExternalForm();
+			baseURL = baseURL.substring(0, baseURL.lastIndexOf("/bin/")); //$NON-NLS-1$
+			result = URI.createURI(baseURL, true);
+		}
+
+		return result;
+	}
+
+	private static void mapUMLResourceURIs(Map<URI, URI> uriMap, String uri,
+			URI location) {
+
+		URI prefix = URI.createURI(uri);
+
+		// ensure trailing separator (make it a "URI prefix")
+		if (!prefix.hasTrailingPathSeparator()) {
+			prefix = prefix.appendSegment(""); //$NON-NLS-1$
+		}
+
+		// same with the location
+		if (!location.hasTrailingPathSeparator()) {
+			location = location.appendSegment(""); //$NON-NLS-1$
+		}
+
+		uriMap.put(prefix, location);
+
+		// and platform URIs, too
+		String folder = location.segment(location.segmentCount() - 2);
+		String platformURI = String.format("%s/%s/", //$NON-NLS-1$
+			ResourcesPlugin.PLUGIN_ID, folder);
+		uriMap.put(URI.createPlatformPluginURI(platformURI, true), location);
+		uriMap.put(URI.createPlatformResourceURI(platformURI, true), location);
+	}
 }
diff --git a/tests/org.eclipse.uml2.uml.tests/src/org/eclipse/uml2/uml/bug/tests/Bug401682Test.java b/tests/org.eclipse.uml2.uml.tests/src/org/eclipse/uml2/uml/bug/tests/Bug401682Test.java
new file mode 100644
index 0000000..4df956b
--- /dev/null
+++ b/tests/org.eclipse.uml2.uml.tests/src/org/eclipse/uml2/uml/bug/tests/Bug401682Test.java
@@ -0,0 +1,121 @@
+/*
+ * Copyright (c) 2013 CEA and others.
+ * All rights reserved.   This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Christian W. Damus (CEA) - initial API and implementation
+ */
+package org.eclipse.uml2.uml.bug.tests;
+
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
+import org.eclipse.uml2.common.util.UML2Util;
+import org.eclipse.uml2.uml.Model;
+import org.eclipse.uml2.uml.Package;
+import org.eclipse.uml2.uml.Profile;
+import org.eclipse.uml2.uml.UMLPackage;
+import org.eclipse.uml2.uml.resource.UMLResource;
+import org.eclipse.uml2.uml.resources.ResourcesPlugin;
+import org.eclipse.uml2.uml.tests.util.StandaloneSupport;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class Bug401682Test
+		extends TestCase {
+
+	private ResourceSet rset;
+
+	/**
+	 * Initializes me with my name.
+	 * 
+	 * @param name
+	 *            my name
+	 */
+	public Bug401682Test(String name) {
+		super(name);
+	}
+
+	public static Test suite() {
+		return new TestSuite(Bug401682Test.class, "Bug 401682 tests"); //$NON-NLS-1$
+	}
+
+	public void testAccessResourcesByPathmapURI() {
+		assertModel(UMLResource.ECORE_METAMODEL_URI);
+		assertProfile(UMLResource.ECORE_PROFILE_URI);
+		assertModel(UMLResource.ECORE_PRIMITIVE_TYPES_LIBRARY_URI);
+	}
+
+	public void testAccessResourcesByPlatformPluginURI() {
+		assertModel(String.format(
+			"platform:/plugin/%s/metamodels/Ecore.metamodel.uml",
+			ResourcesPlugin.PLUGIN_ID));
+		assertProfile(String.format(
+			"platform:/plugin/%s/profiles/Ecore.profile.uml",
+			ResourcesPlugin.PLUGIN_ID));
+		assertModel(String.format(
+			"platform:/plugin/%s/libraries/EcorePrimitiveTypes.library.uml",
+			ResourcesPlugin.PLUGIN_ID));
+	}
+
+	public void testAccessResourcesByPlatformResourceURI() {
+		assertModel(String.format(
+			"platform:/resource/%s/metamodels/Ecore.metamodel.uml",
+			ResourcesPlugin.PLUGIN_ID));
+		assertProfile(String.format(
+			"platform:/resource/%s/profiles/Ecore.profile.uml",
+			ResourcesPlugin.PLUGIN_ID));
+		assertModel(String.format(
+			"platform:/resource/%s/libraries/EcorePrimitiveTypes.library.uml",
+			ResourcesPlugin.PLUGIN_ID));
+	}
+
+	//
+	// Test framework
+	//
+
+	@Override
+	protected void setUp()
+			throws Exception {
+
+		rset = new ResourceSetImpl();
+
+		if (StandaloneSupport.isStandalone()) {
+			StandaloneSupport.init(rset);
+		}
+	}
+
+	@Override
+	protected void tearDown()
+			throws Exception {
+
+		for (Resource next : rset.getResources()) {
+			next.unload();
+		}
+
+		rset.getResources().clear();
+		rset.eAdapters().clear();
+		rset = null;
+	}
+
+	void assertModel(String uri) {
+		assertPackage(uri, Model.class);
+	}
+
+	void assertProfile(String uri) {
+		assertPackage(uri, Profile.class);
+	}
+
+	void assertPackage(String uri, Class<? extends Package> packageType) {
+		Package pkg = UML2Util.load(rset, URI.createURI(uri, true),
+			UMLPackage.Literals.PACKAGE);
+		assertNotNull("Resource not load", pkg);
+		assertTrue(packageType.isInstance(pkg));
+	}
+}
diff --git a/tests/org.eclipse.uml2.uml.tests/src/org/eclipse/uml2/uml/bug/tests/UMLBugTests.java b/tests/org.eclipse.uml2.uml.tests/src/org/eclipse/uml2/uml/bug/tests/UMLBugTests.java
index a43d733..70a021a 100644
--- a/tests/org.eclipse.uml2.uml.tests/src/org/eclipse/uml2/uml/bug/tests/UMLBugTests.java
+++ b/tests/org.eclipse.uml2.uml.tests/src/org/eclipse/uml2/uml/bug/tests/UMLBugTests.java
@@ -7,6 +7,7 @@
  *
  * Contributors:
  *   Christian W. Damus (CEA) - initial API and implementation
+ *   Christian W. Damus (CEA) - 401682
  */
 package org.eclipse.uml2.uml.bug.tests;
 
@@ -37,6 +38,7 @@
 		result.addTest(Bug392833Test.suite());
 		result.addTest(Bug403365Test.suite());
 		result.addTest(Bug300957Test.suite());
+		result.addTest(Bug401682Test.suite());
 
 		return result;
 	}
diff --git a/tests/org.eclipse.uml2.uml.tests/src/org/eclipse/uml2/uml/tests/util/StandaloneSupport.java b/tests/org.eclipse.uml2.uml.tests/src/org/eclipse/uml2/uml/tests/util/StandaloneSupport.java
index 2504243..2a53137 100644
--- a/tests/org.eclipse.uml2.uml.tests/src/org/eclipse/uml2/uml/tests/util/StandaloneSupport.java
+++ b/tests/org.eclipse.uml2.uml.tests/src/org/eclipse/uml2/uml/tests/util/StandaloneSupport.java
@@ -7,20 +7,13 @@
  *
  * Contributors:
  *   Christian W. Damus (CEA) - initial API and implementation
- *   Christian W. Damus (CEA) - 414572
+ *   Christian W. Damus (CEA) - 414572, 401682
  *
  */
 package org.eclipse.uml2.uml.tests.util;
 
-import java.io.File;
-import java.net.URL;
-import java.util.Map;
-
-import org.eclipse.emf.common.util.URI;
 import org.eclipse.emf.ecore.plugin.EcorePlugin;
 import org.eclipse.emf.ecore.resource.ResourceSet;
-import org.eclipse.emf.ecore.resource.URIConverter;
-import org.eclipse.uml2.uml.resource.UMLResource;
 import org.eclipse.uml2.uml.resources.util.UMLResourcesUtil;
 
 /**
@@ -41,65 +34,11 @@
 
 	public static void initGlobals() {
 		UMLResourcesUtil.initGlobalRegistries();
-		initUMLResourceMappings(URIConverter.INSTANCE.getURIMap());
 	}
 
 	public static ResourceSet init(ResourceSet rset) {
 		UMLResourcesUtil.initLocalRegistries(rset);
-
-		initUMLResourceMappings(rset.getURIConverter().getURIMap());
-
 		return rset;
 	}
 
-	private static void initUMLResourceMappings(Map<URI, URI> uriMap) {
-		mapUMLResourceURIs(uriMap, UMLResource.UML_METAMODEL_URI, "metamodels");
-		mapUMLResourceURIs(uriMap, UMLResource.ECORE_PROFILE_URI, "profiles");
-		mapUMLResourceURIs(uriMap, UMLResource.UML_PRIMITIVE_TYPES_LIBRARY_URI,
-			"libraries");
-	}
-
-	public static void mapUMLResourceURIs(Map<URI, URI> uriMap, String uri,
-			String folder) {
-
-		URI uriToMap = URI.createURI(uri);
-		URI prefix = uriToMap.trimSegments(1).appendSegment(""); // ensure
-																	// trailing
-																	// separator
-
-		URL resourceURL = StandaloneSupport.class.getClassLoader().getResource(
-			"/" + folder + "/" + uriToMap.lastSegment());
-
-		if (resourceURL == null) {
-			// probably, we're not running with JARs, so assuming a git
-			// workspace
-			try {
-				resourceURL = new File("")
-					.getAbsoluteFile()
-					.getParentFile()
-					.getParentFile()
-					.toURI()
-					.resolve(
-						"plugins/org.eclipse.uml2.uml.resources/" + folder
-							+ "/" + uriToMap.lastSegment()).toURL();
-			} catch (Exception e) {
-				e.printStackTrace();
-			}
-		}
-
-		if (resourceURL != null) {
-			URI resolved = URI.createURI(resourceURL.toExternalForm())
-				.trimSegments(1).appendSegment("");
-			uriMap.put(prefix, resolved);
-
-			// and platform URIs, too
-			uriMap.put(URI
-				.createURI("platform:/plugin/org.eclipse.uml2.uml.resources/"
-					+ folder + "/"), resolved);
-			uriMap.put(URI
-				.createURI("platform:/resource/org.eclipse.uml2.uml.resources/"
-					+ folder + "/"), resolved);
-		}
-	}
-
 }
\ No newline at end of file