Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 12eba538d126f9a6bfbbc11e32eb240ec2e11d89 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright (c) 2013 Eike Stepper (Berlin, Germany) 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.emf.cdo.tests.uml;

import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.tests.AbstractCDOTest;
import org.eclipse.emf.cdo.tests.config.impl.ConfigTest.Requires;
import org.eclipse.emf.cdo.tests.config.impl.ModelConfig;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EFactory;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;

import org.eclipse.uml2.common.util.UML2Util;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.UMLFactory;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.resource.UMLResource;

/**
 * Tests legacy-mode support for dynamic UML Profiles.
 */
@Requires(ModelConfig.CAPABILITY_LEGACY)
public class DynamicProfileTest extends AbstractCDOTest
{
  private final EPackage stuffPackage = EPackage.Registry.INSTANCE
      .getEPackage("http://www.eclipse.org/cdo/tests/schema/stuff/1.0");

  private final EFactory originalStuffFactory = stuffPackage.getEFactoryInstance();

  private static final String MY_PROFILE_URI = "http://www.eclipse.org/cdo/tests/schema/myprofile";

  private static final String S_CONCEPT = "Concept";

  //
  // Test cases
  //

  public void testInstancesOfRegisteredDynamicUMLProfile() throws Exception
  {
    CDOSession session = openSession();
    session.getPackageRegistry().putEPackage(UMLPackage.eINSTANCE);

    ResourceSet rset = new ResourceSetImpl();
    CDOTransaction transaction = session.openTransaction(rset);
    CDOResource res = transaction.createResource(getResourcePath("/model1.uml"));

    Model umlModel = UMLFactory.eINSTANCE.createModel();
    res.getContents().add(umlModel);

    umlModel.setName("model");
    org.eclipse.uml2.uml.Class aClass = umlModel.createOwnedClass("Fruit", true);

    Profile profile = UML2Util.load(rset, URI.createURI(MY_PROFILE_URI), UMLPackage.Literals.PROFILE);
    assertNotNull(profile);

    umlModel.applyProfile(profile);

    Stereotype conceptStereotype = profile.getOwnedStereotype(S_CONCEPT);
    aClass.applyStereotype(conceptStereotype);

    transaction.commit();

    // load the model again in a new session (hence new package registry)
    session.close();
    session = openSession();

    transaction = session.openTransaction();
    res = transaction.getResource(getResourcePath("/model1.uml"), true);

    assertEquals(true, res.isLoaded());
    assertEquals(2, res.getContents().size()); // the model and the stereotype instance

    umlModel = (Model)res.getContents().get(0);
    aClass = (org.eclipse.uml2.uml.Class)EcoreUtil.getObjectByType(umlModel.getOwnedTypes(), UMLPackage.Literals.CLASS);
    assertNotNull(aClass);

    // verify that using the UML API to access the applied stereotype works as usual
    assertEquals("Stereotype doesn't appear to be applied.", true, aClass.isStereotypeApplied(conceptStereotype));
    assertEquals("Wrong stereotype EClass.",
        transaction.getResourceSet().getPackageRegistry().getEPackage(MY_PROFILE_URI).getEClassifier(S_CONCEPT), aClass
            .getStereotypeApplication(conceptStereotype).eClass());
  }

  public void testInstancesOfLocalDynamicUMLProfile() throws Exception
  {
    CDOSession session = openSession();
    session.getPackageRegistry().putEPackage(UMLPackage.eINSTANCE);

    ResourceSet rset = new ResourceSetImpl();
    CDOTransaction transaction = session.openTransaction(rset);
    CDOResource res = transaction.createResource(getResourcePath("/model1.uml"));

    Model umlModel = UMLFactory.eINSTANCE.createModel();
    res.getContents().add(umlModel);

    umlModel.setName("model");
    org.eclipse.uml2.uml.Class aClass = umlModel.createOwnedClass("Fruit", true);

    CDOResource profileRes = transaction.createResource(getResourcePath("/profile1.profile.uml"));

    // create a local profile resource in the repository
    Profile profile = UMLFactory.eINSTANCE.createProfile();
    profileRes.getContents().add(profile);
    profile.setName("MyProfile");
    Stereotype conceptStereotype = profile.createOwnedStereotype(S_CONCEPT, false);
    Model uml = UML2Util.load(rset, URI.createURI(UMLResource.UML_METAMODEL_URI), UMLPackage.Literals.MODEL);
    profile.createMetamodelReference(uml);
    conceptStereotype.createExtension((org.eclipse.uml2.uml.Class)uml.getOwnedType("Classifier"), false);
    EPackage definition = profile.define();

    // condition the Ecore definition for CDO
    CDOUtil.prepareDynamicEPackage(definition);

    // apply the profile and stereotype to the model
    umlModel.applyProfile(profile);
    aClass.applyStereotype(conceptStereotype);
    assertEquals("Stereotype not applied.", true, aClass.isStereotypeApplied(conceptStereotype));

    transaction.commit();

    // load the model again in a new session (hence new package registry)
    session.close();
    session = openSession();

    transaction = session.openTransaction();
    res = transaction.getResource(getResourcePath("/model1.uml"), true);

    assertEquals(true, res.isLoaded());
    assertEquals(2, res.getContents().size()); // the model and the stereotype instance

    umlModel = (Model)res.getContents().get(0);
    aClass = (org.eclipse.uml2.uml.Class)EcoreUtil.getObjectByType(umlModel.getOwnedTypes(), UMLPackage.Literals.CLASS);
    assertNotNull(aClass);

    EObject stereotypeApplication = res.getContents().get(1);

    // don't have the version of UML2 API that fixes problems in name-based look-up of stereotypes.
    // The main point is that commit didn't fail on unresolved proxies in the Ecore definition
    assertSame("Wrong base element reference.", aClass,
        stereotypeApplication.eGet(stereotypeApplication.eClass().getEStructuralFeature("base_Classifier")));
  }

  //
  // Test framework
  //

  @Override
  protected void doTearDown() throws Exception
  {
    // restore the factory replaced by CDO
    stuffPackage.setEFactoryInstance(originalStuffFactory);

    super.doTearDown();
  }
}

Back to the top