package de.voelter.emf.dynamic;
import java.io.IOException;
import org.eclipse.emf.ecore.EAttribute;
import junit.framework.TestCase;
import dataPackage.Attribute;
import dataPackage.DataPackageFactory;
import dataPackage.DataPackagePackage;
import dataPackage.Entity;
import de.voelter.emf.dynamic.util.IOHelper;
/**
* this test instantiates a model of the metamodel previously built.
* Note that here we use the specific API generated from the metamodel
* (through the genmodel->generate model feature. So for each element
* in the metamodel we now have corresponding Java classes.
* @author MarkusVoelter
*/
public class ModelCreationTest extends TestCase {
public void testCreateModel() {
// as before, we acquire the respective factory and
// package.
DataPackageFactory df = DataPackageFactory.eINSTANCE;
DataPackagePackage dp = DataPackagePackage.eINSTANCE;
// we use the factory to create an Entity,
// whose name we subsequently set.
Entity customerEntity = df.createEntity();
customerEntity.setName( "Customer" );
// then we create two attributes and add them
// to the entity.
Attribute numberAttr = df.createAttribute();
numberAttr.setName("number");
customerEntity.getAttributes().add( numberAttr );
Attribute addressAttr = df.createAttribute();
addressAttr.setName("address");
customerEntity.getAttributes().add( addressAttr );
// you can use the EMF reflection API to access properties
// of the elements
EAttribute nameAttr = (EAttribute) customerEntity.eClass().getEStructuralFeature("name");
String theName = (String) customerEntity.eGet( nameAttr );
assertEquals("Customer", theName);
// and then, we save that model to an XMI file.
// if you also generated the .editor project, you
// can use the editors in it to browse the model
try {
IOHelper.storeAsXMI( customerEntity, "l:/exampleWorkspace-v4-emf/emfdynamic/model.xmi" );
} catch (IOException e1) {
fail( e1.getMessage() );
}
}
}
|