Skip to main content
summaryrefslogtreecommitdiffstats
blob: 62e24727d4463cf85f8614730ee86a999a006c32 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * Copyright (c) 2004 - 2012 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 - 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.common.util.URI;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EFactory;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
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.emf.ecore.util.EcoreUtil;

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

/**
 * Tests legacy-mode support for registered dynamic UML Profiles.
 */
@Requires(ModelConfig.CAPABILITY_LEGACY)
public class RegisteredDynamicProfileTest 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);

    // condition the profile for CDO compatibility, so that we don't try to reference the UML content
    // from the EPackage in the registry. This will be implemented in the UML2 API for Kepler release
    convertDefinitionsToWeakReferences(profile);
    convertDefinitionsToWeakReferences(EPackage.Registry.INSTANCE.getEPackage(MY_PROFILE_URI));

    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);

    // changes in UML2 API are needed to recognize the EClass<-->Stereotype relationship using
    // "weak references" (the conditioning step for CDO), so this tests the low-level structure
    EObject conceptInstance = res.getContents().get(1);
    EClass conceptClass = conceptInstance.eClass();
    assertEquals("Concept", conceptClass.getName());
    assertEquals(MY_PROFILE_URI, conceptClass.getEPackage().getNsURI());

    assertEquals(aClass, conceptInstance.eGet(conceptClass.getEStructuralFeature("base_Classifier")));
  }

  //
  // Test framework
  //

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

    super.doTearDown();
  }

  /**
   * <p>
   * Converts all of the Ecore-to-UML traceability references in a dynamic
   * profile from hard references to weak references.  This is not yet
   * implemented in Eclipse UML2, but will be for Kepler release.
   * </p>
   * <p>
   * TODO: Remove this when the UML2 implementation is available
   * </p>
   *
   * @param profile
   *            a dynamic profile
   *
   * @return the same {@code profile}, for convenience of call chaining
   */
  public static Profile convertDefinitionsToWeakReferences(Profile profile)
  {
    EAnnotation annotation = profile.getEAnnotation("http://www.eclipse.org/uml2/2.0.0/UML");

    if (annotation != null)
    {
      for (EObject next : annotation.getContents())
      {
        if (next instanceof EPackage)
        {
          convertDefinitionsToWeakReferences((EPackage)next);
        }
      }
    }

    return profile;
  }

  /**
   * <p>
   * Converts all of the Ecore-to-UML traceability references in a dynamic
   * profile's Ecore definition from hard references to weak references.
   * This is not yet implemented in Eclipse UML2, but will be for Kepler release.
   * </p>
   * <p>
   * TODO: Remove this when the UML2 implementation is available
   * </p>
   *
   * @param ePackage
   *            a dynamic profile's Ecore definition
   *
   * @return the same {@code ePackage}, for convenience of call chaining
   */
  public static EPackage convertDefinitionsToWeakReferences(EPackage ePackage)
  {
    for (EClassifier next : ePackage.getEClassifiers())
    {
      EAnnotation annotation = next.getEAnnotation("http://www.eclipse.org/uml2/2.0.0/UML");
      if (annotation != null && !annotation.getReferences().isEmpty())
      {
        EObject referenced = annotation.getReferences().get(0);
        if (referenced instanceof NamedElement)
        {
          Resource res = referenced.eResource();
          if (res != null)
          {
            StringBuilder buf = new StringBuilder();
            buf.append(res.getURI()).append('#').append(((NamedElement)referenced).getQualifiedName());
            annotation.getDetails().put("href", buf.toString());
            annotation.getReferences().clear();
          }
        }
      }
    }

    return ePackage;
  }
}

Back to the top