Adds tests to load UML profile in NotLoadingResourceSet.
[EMFCompare2.git] / plugins / org.eclipse.emf.compare.uml2.ide.tests / src / org / eclipse / emf / compare / uml2 / ide / tests / profile / ProfileLoadingTest.java
bloba33f1a3f8535f1075f374c8be17c116159a9429d
1 /*******************************************************************************
2 * Copyright (c) 2014 Obeo.
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 * Obeo - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.emf.compare.uml2.ide.tests.profile;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertTrue;
17 import com.google.common.base.Function;
18 import com.google.common.collect.Collections2;
19 import com.google.common.collect.Lists;
20 import com.google.common.collect.Sets;
22 import java.util.Collection;
23 import java.util.Set;
25 import org.eclipse.core.resources.IStorage;
26 import org.eclipse.core.runtime.NullProgressMonitor;
27 import org.eclipse.emf.common.EMFPlugin;
28 import org.eclipse.emf.common.util.URI;
29 import org.eclipse.emf.compare.ComparePackage;
30 import org.eclipse.emf.compare.ide.internal.utils.NotLoadingResourceSet;
31 import org.eclipse.emf.compare.ide.internal.utils.URIStorage;
32 import org.eclipse.emf.compare.ide.utils.StorageTraversal;
33 import org.eclipse.emf.compare.uml2.profile.test.uml2comparetestprofile.ACliche;
34 import org.eclipse.emf.compare.uml2.profile.test.uml2comparetestprofile.UML2CompareTestProfilePackage;
35 import org.eclipse.emf.ecore.EObject;
36 import org.eclipse.emf.ecore.EPackage;
37 import org.eclipse.emf.ecore.EcorePackage;
38 import org.eclipse.emf.ecore.impl.DynamicEObjectImpl;
39 import org.eclipse.emf.ecore.resource.Resource;
40 import org.eclipse.emf.ecore.resource.URIConverter;
41 import org.eclipse.emf.ecore.resource.impl.ExtensibleURIConverterImpl;
42 import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;
43 import org.eclipse.uml2.uml.NamedElement;
44 import org.eclipse.uml2.uml.Stereotype;
45 import org.eclipse.uml2.uml.UMLPackage;
46 import org.eclipse.uml2.uml.internal.resource.UMLResourceFactoryImpl;
47 import org.junit.After;
48 import org.junit.AfterClass;
49 import org.junit.BeforeClass;
50 import org.junit.Test;
52 /**
53 * Tests the different way to load a UML profile in a {@link NotLoadingResourceSet}.
55 * @author <a href="mailto:arthur.daussy@obeo.fr">Arthur Daussy</a>
57 public class ProfileLoadingTest {
59 /**
60 * Qualified name of the profile St1.
62 private static final String ST1_PROFILE_QUALIFIED_NAME = "profile::St1"; //$NON-NLS-1$
64 /**
65 * Base URI for used referencing data.
67 private static final String BASE_URI = "platform:/plugin/org.eclipse.emf.compare.uml2.ide.tests/src/org/eclipse/emf/compare/uml2/ide/tests/profile/data/loading/"; //$NON-NLS-1$
69 /**
70 * URI of the profile used in most tests.
72 private static final String PROFILE_URI = BASE_URI + "model.profile.uml"; //$NON-NLS-1$
74 private static final Function<Resource, String> TO_STRING = new Function<Resource, String>() {
75 public String apply(Resource input) {
76 return input.getURI().toString();
80 @SuppressWarnings("restriction")
81 @BeforeClass
82 public static void fillRegistries() {
83 if (!EMFPlugin.IS_ECLIPSE_RUNNING) {
84 EPackage.Registry.INSTANCE.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);
85 EPackage.Registry.INSTANCE.put(ComparePackage.eNS_URI, ComparePackage.eINSTANCE);
86 EPackage.Registry.INSTANCE.put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
88 Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("ecore",
89 new EcoreResourceFactoryImpl());
90 Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("uml", //$NON-NLS-1$
91 new UMLResourceFactoryImpl());
95 @AfterClass
96 public static void resetRegistries() {
97 if (!EMFPlugin.IS_ECLIPSE_RUNNING) {
98 Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().remove("uml");
99 Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().remove("ecore");
101 EPackage.Registry.INSTANCE.remove(UML2CompareTestProfilePackage.eNS_URI);
102 EPackage.Registry.INSTANCE.remove(ComparePackage.eNS_URI);
103 EPackage.Registry.INSTANCE.remove(EcorePackage.eNS_URI);
107 @After
108 public void after() {
109 if (resourceSet != null) {
110 for (Resource resource : resourceSet.getResources()) {
111 resource.unload();
116 private NotLoadingResourceSet resourceSet;
119 * Tests loading a dynamic profile referenced using a relative path. This profile is not registered in the
120 * platform but uses the xxx.profile.uml convention.
122 @Test
123 public void loadDynamicRelativeProfile() {
124 String umlResourceURI = BASE_URI + "relative/model.uml"; //$NON-NLS-1$
125 resourceSet = createNotLoadingResourceSet(umlResourceURI);
126 Set<String> expectedLoadedResource = Sets.newHashSet(PROFILE_URI, umlResourceURI);
127 assertLoadeResources(expectedLoadedResource);
128 EObject stereotypeApplication = getStereotype(umlResourceURI + "#_XzoewEIyEeSXpd1NJW5urA", //$NON-NLS-1$
129 ST1_PROFILE_QUALIFIED_NAME);
130 assertTrue(stereotypeApplication instanceof DynamicEObjectImpl);
135 * Tests loading a dynamic profile referenced using a platform:/plugin URI. It is not registered in the
136 * platform but uses the xxx.profile.uml convention.
138 @Test
139 public void loadDynamicPlatformProfile() {
140 String umlResourceURI = BASE_URI + "platform/model.uml"; //$NON-NLS-1$
141 resourceSet = createNotLoadingResourceSet(umlResourceURI);
142 Set<String> expectedLoadedResource = Sets.newHashSet(PROFILE_URI, umlResourceURI);
143 assertLoadeResources(expectedLoadedResource);
144 EObject stereotypeApplication = getStereotype(umlResourceURI + "#_XzoewEIyEeSXpd1NJW5urA", //$NON-NLS-1$
145 ST1_PROFILE_QUALIFIED_NAME);
146 assertTrue(stereotypeApplication instanceof DynamicEObjectImpl);
151 * Tests loading a dynamic profile referenced using a platform:/plugin URI. This profile is registered in
152 * the platform against org.eclipse.uml2.uml.dynamic_package UML extension point. It does not respect the
153 * convention xxx.profile.uml.
155 @Test
156 public void loadDynamicRegisteredProfile() {
157 String umlResourceURI = BASE_URI + "registered/model.uml"; //$NON-NLS-1$
158 resourceSet = createNotLoadingResourceSet(umlResourceURI);
159 Set<String> expectedLoadedResource = Sets.newHashSet(BASE_URI + "registered/model_profile.uml", //$NON-NLS-1$
160 umlResourceURI);
161 assertLoadeResources(expectedLoadedResource);
162 EObject stereotypeApplication = getStereotype(umlResourceURI + "#_XzoewEIyEeSXpd1NJW5urA", //$NON-NLS-1$
163 ST1_PROFILE_QUALIFIED_NAME);
164 assertTrue(stereotypeApplication instanceof DynamicEObjectImpl);
169 * Tests loading a dynamic profile referenced using a pathmap URI. This profile is not registered in
170 * platform but uses the xxx.profile.uml convention.
172 @Test
173 public void loadDynamicPathmapProfile() {
174 String umlResourceURI = BASE_URI + "pathmap/model.uml"; //$NON-NLS-1$
175 resourceSet = createNotLoadingResourceSet(umlResourceURI);
176 Set<String> expectedLoadedResource = Sets.newHashSet(
177 "pathmap://ProfileLoadingTest/model.profile.uml", umlResourceURI); //$NON-NLS-1$
178 assertLoadeResources(expectedLoadedResource);
179 EObject stereotypeApplication = getStereotype(umlResourceURI + "#_XzoewEIyEeSXpd1NJW5urA", //$NON-NLS-1$
180 ST1_PROFILE_QUALIFIED_NAME);
181 assertTrue(stereotypeApplication instanceof DynamicEObjectImpl);
186 * Tests loading a static profile referenced using a pathmap URI. This profile is registered in the
187 * platform against org.eclipse.uml2.uml.generated_package UML extension point.
189 @Test
190 public void loadStaticProfile() {
191 String umlResourceURI = BASE_URI + "static_/model.uml"; //$NON-NLS-1$
192 resourceSet = createNotLoadingResourceSet(umlResourceURI);
193 Set<String> expectedLoadedResource = Sets
194 .newHashSet("pathmap://UML_PROFILES/Ecore.profile.uml", //$NON-NLS-1$
195 umlResourceURI, BASE_URI + "static_/model.uml", //$NON-NLS-1$
196 "platform:/plugin/org.eclipse.emf.compare.uml2.tests/model/uml2.compare.testprofile.profile.uml"); //$NON-NLS-1$
197 assertLoadeResources(expectedLoadedResource);
198 EObject stereotypeApplication = getStereotype(umlResourceURI + "#_mg1YkEJqEeSsE-f8QjqLJA", //$NON-NLS-1$
199 "UML2CompareTestProfile::ACliche"); //$NON-NLS-1$
200 assertTrue(stereotypeApplication instanceof ACliche);
204 * Asserts that all (and only) expected resources are loaded. It normalizes the URI in the resource set in
205 * order to resolve pathmaps.
207 * @param expectedLoadedResource
208 * @param resourceSet
210 private void assertLoadeResources(Set<String> expectedLoadedResource) {
211 assertEquals(expectedLoadedResource.size(), resourceSet.getResources().size());
212 Set<String> actualURIs = Sets.newHashSet(Collections2
213 .transform(resourceSet.getResources(), TO_STRING));
214 assertEquals(expectedLoadedResource, actualURIs);
219 * Creates a {@link NotLoadingResourceSet}.
221 * @param stringURIs
222 * Uris of the resources to load.
223 * @return {@link NotLoadingResourceSet} with loaded resources.
225 public static NotLoadingResourceSet createNotLoadingResourceSet(String... stringURIs) {
226 Collection<IStorage> storages = Collections2.transform(Lists.newArrayList(stringURIs),
227 toStorage(new ExtensibleURIConverterImpl()));
228 StorageTraversal storageTraversal = new StorageTraversal(Sets.newHashSet(storages));
229 NotLoadingResourceSet newResourceSet = NotLoadingResourceSet.create(storageTraversal,
230 new NullProgressMonitor());
232 return newResourceSet;
237 * Returns the stereoptype application on the eObject.
238 * <p>
239 * This method assert that the Element exists and that it has the matching stereotyped applied on it
240 * </p>
242 * @param eObjectURI
243 * uri pointing to a {@link NamedElement} in the resource set. (NamedElement has been choosen
244 * for printing convenience).
245 * @param stereotypeName
246 * Qualified name of the seteotype.
248 private EObject getStereotype(String eObjectURI, String stereotypeName) {
249 EObject eObject = resourceSet.getEObject(URI.createURI(eObjectURI), false);
250 assertTrue(eObject instanceof NamedElement);
251 NamedElement elem = (NamedElement)eObject;
252 StringBuilder errorMessage = new StringBuilder();
253 errorMessage.append("The stereotype ").append(stereotypeName).append(" is not applied on ").append( //$NON-NLS-1$ //$NON-NLS-2$
254 elem.getName());
255 Stereotype stereotype = elem.getAppliedStereotype(stereotypeName);
256 assertNotNull(errorMessage.toString(), stereotype);
257 EObject stereotypeApplication = elem.getStereotypeApplication(stereotype);
258 assertNotNull(stereotypeApplication);
259 return stereotypeApplication;
262 public static final Function<String, IStorage> toStorage(final URIConverter uriConverter) {
263 return new Function<String, IStorage>() {
264 public IStorage apply(String input) {
265 URI uri = URI.createURI(input, true);
266 return new URIStorage(uri, uriConverter.getURIHandler(uri), uriConverter);