001: package org.andromda.repositories.mdr.uml14;
002:
003: import java.net.URL;
004: import java.util.Collection;
005: import java.util.Iterator;
006:
007: import junit.framework.TestCase;
008:
009: import org.andromda.core.common.AndroMDALogger;
010: import org.andromda.core.metafacade.ModelAccessFacade;
011: import org.andromda.core.namespace.NamespaceComponents;
012: import org.andromda.core.repository.Repositories;
013: import org.andromda.repositories.mdr.MDRepositoryFacade;
014: import org.omg.uml.UmlPackage;
015: import org.omg.uml.foundation.core.Attribute;
016: import org.omg.uml.foundation.core.ModelElement;
017: import org.omg.uml.foundation.core.Namespace;
018: import org.omg.uml.foundation.core.UmlClass;
019: import org.omg.uml.modelmanagement.Model;
020: import org.omg.uml.modelmanagement.ModelManagementPackage;
021:
022: /**
023: * @author <A HREF="httplo://www.amowers.com">Anthony Mowers</A>
024: * @author Chad Brandon
025: */
026: public class MDRepositoryTransformationTest extends TestCase {
027: private URL modelURL = null;
028: private MDRepositoryFacade repository = null;
029:
030: /**
031: * Constructor for MDRepositoryTransformationTest.
032: *
033: * @param name
034: */
035: public MDRepositoryTransformationTest(String name) {
036: super (name);
037: }
038:
039: /**
040: * @see TestCase#setUp()
041: */
042: protected void setUp() throws Exception {
043: super .setUp();
044: AndroMDALogger.initialize();
045: if (modelURL == null) {
046: modelURL = TestModel.getModel();
047: NamespaceComponents.instance().discover();
048: Repositories.instance().initialize();
049: repository = (MDRepositoryFacade) Repositories.instance()
050: .getImplementation("netBeansMDR");
051: repository.open();
052: }
053: }
054:
055: /**
056: * Demonstrates how to dynamically add an attribute onto a class in a model.
057: * <p/>
058: * It loads a model from XMI file, looks a class with a particular fully qualified name and adds an attribute onto
059: * that class.
060: *
061: * @throws Exception
062: */
063: public void testTransformModel() throws Exception {
064: repository
065: .readModel(new String[] { modelURL.toString() }, null);
066: final ModelAccessFacade modelFacade = repository.getModel();
067: UmlPackage umlPackage = (UmlPackage) modelFacade.getModel();
068: ModelManagementPackage modelManagementPackage = umlPackage
069: .getModelManagement();
070:
071: // A given XMI file can contain multiptle models.
072: // Use the first model in the XMI file
073: Model model = (Model) (modelManagementPackage.getModel()
074: .refAllOfType().iterator().next());
075:
076: // look for a class with the name 'org.EntityBean'
077: String[] fullyQualifiedName = { "org", "andromda", "ClassA" };
078:
079: UmlClass umlClass = (UmlClass) getModelElement(model,
080: fullyQualifiedName, 0);
081:
082: // create an attribute
083: Attribute attribute = umlPackage.getCore().getAttribute()
084: .createAttribute();
085: attribute.setName("attributeAA");
086:
087: // assign the attribute to the class
088: attribute.setOwner(umlClass);
089: }
090:
091: private static ModelElement getModelElement(Namespace namespace,
092: String[] fullyQualifiedName, int pos) {
093: if ((namespace == null) || (fullyQualifiedName == null)
094: || (pos > fullyQualifiedName.length)) {
095: return null;
096: }
097:
098: if (pos == fullyQualifiedName.length) {
099: return namespace;
100: }
101:
102: Collection elements = namespace.getOwnedElement();
103: for (final Iterator iterator = elements.iterator(); iterator
104: .hasNext();) {
105: ModelElement element = (ModelElement) iterator.next();
106: if (element.getName().equals(fullyQualifiedName[pos])) {
107: int nextPos = pos + 1;
108:
109: if (nextPos == fullyQualifiedName.length) {
110: return element;
111: }
112:
113: if (element instanceof Namespace) {
114: return getModelElement((Namespace) element,
115: fullyQualifiedName, nextPos);
116: }
117:
118: return null;
119: }
120: }
121:
122: return null;
123: }
124:
125: /**
126: * @see TestCase#tearDown()
127: */
128: protected void tearDown() throws Exception {
129: this.repository.clear();
130: this.repository = null;
131: super.tearDown();
132: }
133: }
|