Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Model building using only Java code
Model building using only Java code [message #415998] Tue, 15 January 2008 22:08 Go to next message
Eclipse UserFriend
Originally posted by: m_lehmeier.gmx.de

Hello!

I am trying to create a model from an existing Java object using
reflection mechanism.
First I thought about creating an XMI file manually (with ordinary XML
tools) and then load the file. For this I used Dennis Wagelaar's ATL
command line tool. It works. (save for a strange but apparently harmless
exception)

However, I would like to build a model directly without having to use
ordinary XML.

I got this far:

AtlModelHandler amh = AtlModelHandler.getDefault("EMF");
ModelLoader ml = new EMFModelLoader();
ml.addInjector("xml", XMLInjector.class);
ml.addInjector("ebnf2", TCSInjector.class); // necessary?
ASMModel metaModel = amh.getMof();
ASMModel model = ASMEMFModel.newASMEMFModel(sModel, sMetamodel,
(ASMEMFModel)metaModel, ml);
XMIResource extent = (XMIResource)((ASMEMFModel)model).getExtent();

// Saving
amh.saveModel(model, "file:/tmp/out.ecore");

But I don't know how to continue.
I don't know how to add EPackages or all other child elements in that
model.

I would be very thankful if anybody can point me to a code example so
that I can continue from there.

--
Lehmeier Michael
Re: Model building using only Java code [message #415999 is a reply to message #415998] Tue, 15 January 2008 23:52 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
Michael,

Unfortunately I don't know much about ATL. :-(

But they have an extremely active newgroup, so I've added it to the "to"
list of my reply...


Michael Lehmeier wrote:
> Hello!
>
> I am trying to create a model from an existing Java object using
> reflection mechanism.
> First I thought about creating an XMI file manually (with ordinary XML
> tools) and then load the file. For this I used Dennis Wagelaar's ATL
> command line tool. It works. (save for a strange but apparently harmless
> exception)
>
> However, I would like to build a model directly without having to use
> ordinary XML.
>
> I got this far:
>
> AtlModelHandler amh = AtlModelHandler.getDefault("EMF");
> ModelLoader ml = new EMFModelLoader();
> ml.addInjector("xml", XMLInjector.class);
> ml.addInjector("ebnf2", TCSInjector.class); // necessary?
> ASMModel metaModel = amh.getMof();
> ASMModel model = ASMEMFModel.newASMEMFModel(sModel, sMetamodel,
> (ASMEMFModel)metaModel, ml);
> XMIResource extent = (XMIResource)((ASMEMFModel)model).getExtent();
>
> // Saving
> amh.saveModel(model, "file:/tmp/out.ecore");
>
> But I don't know how to continue.
> I don't know how to add EPackages or all other child elements in that
> model.
>
> I would be very thankful if anybody can point me to a code example so
> that I can continue from there.
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Model building using only Java code [message #416000 is a reply to message #415999] Wed, 16 January 2008 00:23 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: m_lehmeier.gmx.de

On 2008-01-15, Ed Merks <merks@ca.ibm.com> wrote:
> Michael,
>
> Unfortunately I don't know much about ATL. :-(
>
> But they have an extremely active newgroup, so I've added it to the "to"
> list of my reply...

The Problem isn't ATL-specific at all, so I thought the emf newsgroup
was the right one, because it only touches emf-concerns.

Once I have my model ready, I can deal with the ATL-part myself. (I
hope)

--
Lehmeier Michael
Re: Model building using only Java code [message #416001 is a reply to message #416000] Wed, 16 January 2008 01:11 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------000003050301090002020102
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Michael,

I didn't get the EMF part though. Here's an example of a JUnit test
that creates a Ecore model. Maybe that helps...

package org.eclipse.emf.test.core.dynamic;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.eclipse.emf.common.EMFPlugin;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EFactory;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.ETypedElement;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceImpl;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.eclipse.emf.test.common.TestUtil;
import org.eclipse.emf.test.core.AllSuites;


public class SimpleModelTest extends TestCase
{
private EPackage companyPackage;

private EClass employeeClass;

private EAttribute employeeName;

private EAttribute employeeManager;

private EClass departmentClass;

private EAttribute departmentName;

private EAttribute departmentNumber;

private EReference departmentEmployees;

public SimpleModelTest(String name)
{
super(name);
}

public static Test suite()
{
TestSuite ts = new TestSuite("SimpleModelTest");
ts.addTest(new SimpleModelTest("testPackageAndFactory"));
ts.addTest(new SimpleModelTest("testAttributes"));
ts.addTest(new SimpleModelTest("testReference"));
ts.addTest(new SimpleModelTest("testMetaData"));
ts.addTest(new SimpleModelTest("testSaveAndLoad"));
ts.addTest(new SimpleModelTest("testSaveAndLoadZip"));
ts.addTest(new SimpleModelTest("testProxy"));
ts.addTest(new SimpleModelTest("testTrackingModificaiton"));
ts.addTest(new SimpleModelTest("testAddingDuplicates"));
ts.addTest(new SimpleModelTest("testRenamingStructuralFeature"));
ts.addTest(new SimpleModelTest("testRenamingClassifier"));
return ts;
}

/**
* @see junit.framework.TestCase#setUp()
*/
@Override
protected void setUp() throws Exception
{
EcoreFactory ecoreFactory = EcoreFactory.eINSTANCE;
EcorePackage ecorePackage = EcorePackage.eINSTANCE;

employeeClass = ecoreFactory.createEClass();
employeeClass.setName("Employee");

employeeName = ecoreFactory.createEAttribute();
employeeName.setName("name");
employeeName.setEType(ecorePackage.getEString());
employeeClass.getEStructuralFeatures().add(employeeName);

employeeManager = ecoreFactory.createEAttribute();
employeeManager.setName("manager");
employeeManager.setEType(ecorePackage.getEBoolean());
employeeClass.getEStructuralFeatures().add(employeeManager);

departmentClass = ecoreFactory.createEClass();
departmentClass.setName("Department");

departmentName = ecoreFactory.createEAttribute();
departmentName.setName("name");
departmentName.setEType(ecorePackage.getEString());
departmentClass.getEStructuralFeatures().add(departmentName) ;

departmentNumber = ecoreFactory.createEAttribute();
departmentNumber.setName("number");
departmentNumber.setEType(ecorePackage.getEInt());
departmentClass.getEStructuralFeatures().add(departmentNumbe r);

departmentEmployees = ecoreFactory.createEReference();
departmentEmployees.setName("employees");
departmentEmployees.setEType(employeeClass);

departmentEmployees.setUpperBound(ETypedElement.UNBOUNDED_MU LTIPLICITY);
departmentEmployees.setContainment(true);
departmentClass.getEStructuralFeatures().add(departmentEmplo yees);

companyPackage = ecoreFactory.createEPackage();
companyPackage.setName("company");
companyPackage.setNsPrefix("company");
companyPackage.setNsURI("http:///com.example.company.ecore");
companyPackage.getEClassifiers().add(employeeClass);
companyPackage.getEClassifiers().add(departmentClass);
EPackage.Registry.INSTANCE.put(companyPackage.getNsURI(),
companyPackage);
}

/**
* @see junit.framework.TestCase#tearDown()
*/
@Override
protected void tearDown() throws Exception
{
employeeName = null;
employeeManager = null;
employeeClass.getEStructuralFeatures().clear();
employeeClass = null;

departmentEmployees = null;

departmentName = null;
departmentNumber = null;
departmentClass.getEStructuralFeatures().clear();
departmentClass = null;

companyPackage = null;
}

public void testPackageAndFactory()
{
assertNotNull(companyPackage);
assertEquals("company", companyPackage.getName());
assertEquals("company", companyPackage.getNsPrefix());
assertEquals("http:///com.example.company.ecore",
companyPackage.getNsURI());

EFactory companyFactory = companyPackage.getEFactoryInstance();
assertNotNull(companyFactory);
assertEquals(companyPackage, companyFactory.getEPackage());

EObject employee = companyFactory.create(employeeClass);
assertNotNull(employee);
assertEquals(companyPackage, employee.eClass().getEPackage());
}

public void testAttributes()
{
EFactory companyFactory = companyPackage.getEFactoryInstance();

EObject employee1 = companyFactory.create(employeeClass);
employee1.eSet(employeeName, "John");
assertEquals("John", employee1.eGet(employeeName));
assertEquals(Boolean.FALSE, employee1.eGet(employeeManager));

EObject employee2 = companyFactory.create(employeeClass);
employee2.eSet(employeeName, "Katherine");
assertEquals("Katherine", employee2.eGet(employeeName));
employee2.eSet(employeeManager, Boolean.TRUE);
assertEquals(Boolean.TRUE, employee2.eGet(employeeManager));

EObject department = companyFactory.create(departmentClass);
department.eSet(departmentName, "ABC");
assertEquals("ABC", department.eGet(departmentName));
department.eSet(departmentNumber, new Integer(123));
assertEquals(new Integer(123), department.eGet(departmentNumber));
}

public void testReference()
{
EFactory companyFactory = companyPackage.getEFactoryInstance();

EObject employee1 = companyFactory.create(employeeClass);
EObject employee2 = companyFactory.create(employeeClass);

EObject department1 = companyFactory.create(departmentClass);

@SuppressWarnings("unchecked")
List<EObject> department1Employees =
(List<EObject>)department1.eGet(departmentEmployees);
assertNotNull(department1Employees);

department1Employees.add(employee1);
assertEquals(1, department1Employees.size());
assertEquals(employee1, department1Employees.get(0));
assertEquals(department1Employees,
department1.eGet(departmentEmployees));

department1Employees.add(employee2);
assertEquals(2, department1Employees.size());
assertEquals(employee2, department1Employees.get(1));
assertEquals(department1Employees,
department1.eGet(departmentEmployees));

//Should not affect the list
department1Employees.add(employee2);
assertEquals(2, department1Employees.size());
assertEquals(employee2, department1Employees.get(1));
assertEquals(department1Employees,
department1.eGet(departmentEmployees));

EObject department2 = companyFactory.create(departmentClass);

@SuppressWarnings("unchecked")
List<EObject> department2Employees =
(List<EObject>)department2.eGet(departmentEmployees);
assertNotNull(department2Employees);

department2Employees.add(employee1);
assertEquals(1, department2Employees.size());
assertEquals(employee1, department2Employees.get(0));
assertEquals(department2Employees,
department2.eGet(departmentEmployees));

//since departmentEmployees is an aggregation, employee1 should
be removed
//from department1Employees
assertEquals(1, department1Employees.size());
assertEquals(employee2, department1Employees.get(0));
assertEquals(department1Employees,
department1.eGet(departmentEmployees));
}

public void testMetaData()
{
EFactory companyFactory = companyPackage.getEFactoryInstance();

EObject employee1 = companyFactory.create(employeeClass);
EObject employee2 = companyFactory.create(employeeClass);

EObject department1 = companyFactory.create(departmentClass);
@SuppressWarnings("unchecked")
List<EObject> department1Employees =
(List<EObject>)department1.eGet(departmentEmployees);
EObject department2 = companyFactory.create(departmentClass);
@SuppressWarnings("unchecked")
List<EObject> department2Employees =
(List<EObject>)department2.eGet(departmentEmployees);

department1Employees.add(employee1);
assertEquals(department1, employee1.eContainer());
assertEquals(department1Employees.size(),
department1.eContents().size());

department1Employees.add(employee2);
assertEquals(department1, employee1.eContainer());
assertEquals(department1Employees.size(),
department1.eContents().size());

department2Employees.add(employee1);
assertEquals(department2, employee1.eContainer());
assertEquals(department1Employees.size(),
department1.eContents().size());
assertEquals(department2Employees.size(),
department2.eContents().size());
}

public void testSaveAndLoadZip() throws Exception
{
//Instanciating the model
EFactory companyFactory = companyPackage.getEFactoryInstance();

EObject employee1 = companyFactory.create(employeeClass);
employee1.eSet(employeeName, "John");
EObject employee2 = companyFactory.create(employeeClass);
employee2.eSet(employeeName, "Jane");

EObject department = companyFactory.create(departmentClass);
department.eSet(departmentName, "ACME1");
@SuppressWarnings("unchecked")
List<EObject> employeesList =
(List<EObject>)department.eGet(departmentEmployees);
employeesList.add(employee1);
employeesList.add(employee2);

URI departmentsURI =
URI.createFileURI(TestUtil.getPluginDirectory(AllSuites.PLUG IN_ID) +
"/departments.dept");
XMLResource departmentsResource = (XMLResource)new
XMIResourceFactoryImpl().createResource(departmentsURI);
departmentsResource.setUseZip(true);
departmentsResource.getContents().add(department);

//Saving
departmentsResource.save(Collections.EMPTY_MAP);
assertTrue(new File(departmentsURI.toFileString()).exists());

//Loading department in ResourceSet
ResourceSet resourceSet = new ResourceSetImpl();

resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put( "dept",
new XMIResourceFactoryImpl()
{
/* (non-Javadoc)
* @see
org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl#create Resource(org.eclipse.emf.common.util.URI)
*/
@Override
public Resource createResource(URI uri)
{
XMLResource result = (XMLResource)super.createResource(uri);
result.setUseZip(true);
return result;
}
});

Resource loadedResource =
resourceSet.getResource(departmentsURI, true);
assertEquals(1, resourceSet.getResources().size());
assertEquals(1, loadedResource.getContents().size());

EObject loadedDepartment = loadedResource.getContents().get(0);
assertEquals(department.eClass(), loadedDepartment.eClass());
assertEquals(department.eGet(departmentName),
loadedDepartment.eGet(departmentName));

//Get Employess
@SuppressWarnings("unchecked")
List<EObject> loadedEmployees =
(List<EObject>)department.eGet(departmentEmployees);
assertEquals(2, loadedEmployees.size());
EObject loadedEmployee = loadedEmployees.get(0);
assertEquals(employee1.eClass(), loadedEmployee.eClass());
assertEquals(employee1.eGet(employeeName),
loadedEmployee.eGet(employeeName));
loadedEmployee = loadedEmployees.get(1);
assertEquals(employee2.eClass(), loadedEmployee.eClass());
assertEquals(employee2.eGet(employeeName),
loadedEmployee.eGet(employeeName));

//Deleting created files
new File(departmentsURI.toFileString()).delete();
assertFalse(new File(departmentsURI.toFileString()).exists());
}

public void testSaveAndLoad() throws Exception
{
//Instanciating the model
EFactory companyFactory = companyPackage.getEFactoryInstance();

EObject employee1 = companyFactory.create(employeeClass);
employee1.eSet(employeeName, "John");
EObject employee2 = companyFactory.create(employeeClass);
employee2.eSet(employeeName, "Jane");

EObject department = companyFactory.create(departmentClass);
department.eSet(departmentName, "ACME1");
@SuppressWarnings("unchecked")
List<EObject> employeesList =
(List<EObject>)department.eGet(departmentEmployees);
employeesList.add(employee1);
employeesList.add(employee2);

URI departmentsURI =
URI.createFileURI(TestUtil.getPluginDirectory(AllSuites.PLUG IN_ID) +
"/departments.xmi");
Resource departmentsResource = new
XMIResourceFactoryImpl().createResource(departmentsURI);
departmentsResource.getContents().add(department);

//Saving
Map<Object, Object> options = new HashMap<Object, Object>();
options.put(XMLResource.OPTION_USE_FILE_BUFFER, Boolean.TRUE);
departmentsResource.save(options);
assertTrue(new File(departmentsURI.toFileString()).exists());

//Loading department in ResourceSet
ResourceSet resourceSet = new ResourceSetImpl();
if (!EMFPlugin.IS_ECLIPSE_RUNNING)
{

resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put( "xmi",
new XMIResourceFactoryImpl());
}

Resource loadedResource =
resourceSet.getResource(departmentsURI, true);
assertEquals(1, resourceSet.getResources().size());
assertEquals(1, loadedResource.getContents().size());

EObject loadedDepartment = loadedResource.getContents().get(0);
assertEquals(department.eClass(), loadedDepartment.eClass());
assertEquals(department.eGet(departmentName),
loadedDepartment.eGet(departmentName));

//Get Employess
@SuppressWarnings("unchecked")
List<EObject> loadedEmployees =
(List<EObject>)department.eGet(departmentEmployees);
assertEquals(2, loadedEmployees.size());
EObject loadedEmployee = loadedEmployees.get(0);
assertEquals(employee1.eClass(), loadedEmployee.eClass());
assertEquals(employee1.eGet(employeeName),
loadedEmployee.eGet(employeeName));
loadedEmployee = loadedEmployees.get(1);
assertEquals(employee2.eClass(), loadedEmployee.eClass());
assertEquals(employee2.eGet(employeeName),
loadedEmployee.eGet(employeeName));

//Deleting created files
new File(departmentsURI.toFileString()).delete();
assertFalse(new File(departmentsURI.toFileString()).exists());
}

public void testProxy() throws Exception
{
EFactory companyFactory = companyPackage.getEFactoryInstance();

//Adding a not-contained association
EReference associateDepartments =
EcoreFactory.eINSTANCE.createEReference();
associateDepartments.setName("associateDepartments");
associateDepartments.setContainment(false);
associateDepartments.setEType(departmentClass);

associateDepartments.setUpperBound(ETypedElement.UNBOUNDED_M ULTIPLICITY);
departmentClass.getEStructuralFeatures().add(associateDepart ments);

//Instanciating the model
EObject department = companyFactory.create(departmentClass);
department.eSet(departmentName, "ACME1");
@SuppressWarnings("unchecked")
List<EObject> associateDepartmentsList =
(List<EObject>)department.eGet(associateDepartments);
URI departmentURI =
URI.createFileURI(TestUtil.getPluginDirectory(AllSuites.PLUG IN_ID) +
"/department.xmi");
Resource departmentResource = new
XMIResourceFactoryImpl().createResource(departmentURI);
departmentResource.getContents().add(department);

EObject department1 = companyFactory.create(departmentClass);
department1.eSet(departmentName, "ACME1");
associateDepartmentsList.add(department1);
URI department1URI =
URI.createFileURI(TestUtil.getPluginDirectory(AllSuites.PLUG IN_ID) +
"/department1.xmi");
Resource department1Resource = new
XMIResourceFactoryImpl().createResource(department1URI);
department1Resource.getContents().add(department1);

EObject department2 = companyFactory.create(departmentClass);
department2.eSet(departmentName, "ACME2");
associateDepartmentsList.add(department2);
URI department2URI =
URI.createFileURI(TestUtil.getPluginDirectory(AllSuites.PLUG IN_ID) +
"/department2.xmi");
Resource department2Resource = new
XMIResourceFactoryImpl().createResource(department2URI);
department2Resource.getContents().add(department2);

//Saving
departmentResource.save(Collections.EMPTY_MAP);
assertTrue(new File(departmentURI.toFileString()).exists());
department1Resource.save(Collections.EMPTY_MAP);
assertTrue(new File(department1URI.toFileString()).exists());
department2Resource.save(Collections.EMPTY_MAP);
assertTrue(new File(department2URI.toFileString()).exists());

//Loading department into a resource set
ResourceSet resourceSet = new ResourceSetImpl();
if (!EMFPlugin.IS_ECLIPSE_RUNNING)
{

resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put( "xmi",
new XMIResourceFactoryImpl());
}

Resource loadedResource = resourceSet.getResource(departmentURI,
true);
assertEquals(1, loadedResource.getContents().size());

EObject loadedDepartment = loadedResource.getContents().get(0);
@SuppressWarnings("unchecked")
BasicEList<EObject> loadedAssociateDepartmentsList =
(BasicEList<EObject>)loadedDepartment.eGet(associateDepartments, false);
assertEquals(2, loadedAssociateDepartmentsList.size());

EObject loadedDepartment1 =
loadedAssociateDepartmentsList.basicGet(0);
EObject loadedDepartment2 =
loadedAssociateDepartmentsList.basicGet(1);

assertTrue(loadedDepartment1.eIsProxy());
assertEquals(department1URI.toFileString(),
((InternalEObject)loadedDepartment1).eProxyURI().toFileStrin g());
assertNull(loadedDepartment1.eGet(departmentName));
assertTrue(loadedDepartment2.eIsProxy());
assertEquals(department2URI.toFileString(),
((InternalEObject)loadedDepartment2).eProxyURI().toFileStrin g());
assertNull(loadedDepartment2.eGet(departmentName));

//Resolving Proxy
loadedDepartment1 = EcoreUtil.resolve(loadedDepartment1,
resourceSet);
assertFalse(loadedDepartment1.eIsProxy());
assertEquals(department1.eGet(departmentName),
loadedDepartment1.eGet(departmentName));
loadedDepartment2 = EcoreUtil.resolve(loadedDepartment2,
resourceSet);
assertFalse(loadedDepartment2.eIsProxy());
assertEquals(department2.eGet(departmentName),
loadedDepartment2.eGet(departmentName));

//Deleting created files
new File(departmentURI.toFileString()).delete();
assertFalse(new File(departmentURI.toFileString()).exists());
new File(department1URI.toFileString()).delete();
assertFalse(new File(department1URI.toFileString()).exists());
new File(department2URI.toFileString()).delete();
assertFalse(new File(department2URI.toFileString()).exists());
}

/*
* Bugzilla 80110
*/
public void testTrackingModificaiton() throws Exception
{
Resource resource = new ResourceImpl()
{
@Override
protected void doSave(OutputStream outputStream, Map<?, ?>
options) throws IOException
{
// Ignore
}
};
assertFalse(resource.isTrackingModification());
assertFalse(resource.isModified());

EFactory companyFactory = companyPackage.getEFactoryInstance();
EObject employee = companyFactory.create(employeeClass);
EObject department = companyFactory.create(departmentClass);
@SuppressWarnings("unchecked")
List<EObject> employess =
((List<EObject>)department.eGet(departmentEmployees));
employess.add(employee);

resource.getContents().add(department);
assertFalse(resource.isTrackingModification());
assertFalse(resource.isModified());

resource.setTrackingModification(true);
assertTrue(resource.isTrackingModification());
assertFalse(resource.isModified());

employee.eSet(employeeName, "John");
assertTrue(resource.isTrackingModification());
assertTrue(resource.isModified());

resource.save(new ByteArrayOutputStream(), null);
assertTrue(resource.isTrackingModification());
assertFalse(resource.isModified());

resource.setTrackingModification(false);
assertFalse(resource.isTrackingModification());
assertFalse(resource.isModified());

employee.eSet(employeeName, "Joe");
assertEquals(resource, employee.eResource());
assertFalse(resource.isTrackingModification());
assertFalse(resource.isModified());

resource.setTrackingModification(true);
assertTrue(resource.isTrackingModification());
assertFalse(resource.isModified());

EObject employee1 = companyFactory.create(employeeClass);
EObject department1 = companyFactory.create(departmentClass);
@SuppressWarnings("unchecked")
List<EObject> department1Employees =
((List<EObject>)department1.eGet(departmentEmployees));
department1Employees.add(employee1);
resource.getContents().add(department1);
assertTrue(resource.isTrackingModification());
assertTrue(resource.isModified());

resource.save(new ByteArrayOutputStream(), null);
assertTrue(resource.isTrackingModification());
assertFalse(resource.isModified());

employee1.eSet(employeeName, "Mike");
assertTrue(resource.isTrackingModification());
assertTrue(resource.isModified());

resource.save(new ByteArrayOutputStream(), null);
assertTrue(resource.isTrackingModification());
assertFalse(resource.isModified());

resource.setTrackingModification(false);
assertFalse(resource.isTrackingModification());
assertFalse(resource.isModified());

employee1.eSet(employeeName, "Mark");
assertEquals(resource, employee1.eResource());
assertFalse(resource.isTrackingModification());
assertFalse(resource.isModified());
}

/*
* Bugzilla 106702
*/
public void testAddingDuplicates() throws Exception
{
EFactory companyFactory = companyPackage.getEFactoryInstance();

EObject department1 = companyFactory.create(departmentClass);
@SuppressWarnings("unchecked")
List<EObject> department1Employees =
(List<EObject>)department1.eGet(departmentEmployees);
assertEquals(0, department1Employees.size());

EObject employee1 = companyFactory.create(employeeClass);
department1Employees.add(employee1);

Exception exception = null;
try
{
department1Employees.add(0, null);
}
catch(IllegalArgumentException iae)
{
exception = iae;
}
assertNotNull(exception);

assertEquals(1, department1Employees.size());
assertEquals(0, department1Employees.indexOf(employee1));

Object[] data = ((BasicEList<?>)department1Employees).data();
assertEquals(employee1, data[0]);
assertNull(data[1]);
}

public void testRenamingStructuralFeature()
{
String COST_CENTER_NAME = "costCenter";
String CHANGED_COST_CENTER_NAME = "changedCostCenter";

EAttribute costCenterAttribute =
EcoreFactory.eINSTANCE.createEAttribute();
costCenterAttribute.setName( COST_CENTER_NAME );

departmentClass.getEStructuralFeatures().add( costCenterAttribute );

assertNotNull( departmentClass.getEStructuralFeature(
COST_CENTER_NAME ) );

costCenterAttribute.setName( CHANGED_COST_CENTER_NAME );

assertEquals( CHANGED_COST_CENTER_NAME,
costCenterAttribute.getName() );
assertNotNull( departmentClass.getEStructuralFeature(
CHANGED_COST_CENTER_NAME ) );
}

public void testRenamingClassifier()
{
assertNotNull(companyPackage.getEClassifier("Employee"));
employeeClass.setName("Employee1");
assertNotNull(companyPackage.getEClassifier("Employee1"));
}
}




Michael Lehmeier wrote:
> On 2008-01-15, Ed Merks <merks@ca.ibm.com> wrote:
>
>> Michael,
>>
>> Unfortunately I don't know much about ATL. :-(
>>
>> But they have an extremely active newgroup, so I've added it to the "to"
>> list of my reply...
>>
>
> The Problem isn't ATL-specific at all, so I thought the emf newsgroup
> was the right one, because it only touches emf-concerns.
>
> Once I have my model ready, I can deal with the ATL-part myself. (I
> hope)
>
>


--------------000003050301090002020102
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Michael,<br>
<br>
I didn't get the EMF part though.&nbsp; Here's an example of a JUnit test
that creates a Ecore model.&nbsp; Maybe that helps...<small></small><br>
<blockquote><small>package org.eclipse.emf.test.core.dynamic;</small><br>
<br>
<small>import java.io.ByteArrayOutputStream;</small><br>
<small>import java.io.File;</small><br>
<small>import java.io.IOException;</small><br>
<small>import java.io.OutputStream;</small><br>
<small>import java.util.Collections;</small><br>
<small>import java.util.HashMap;</small><br>
<small>import java.util.List;</small><br>
<small>import java.util.Map;</small><br>
<br>
<small>import junit.framework.Test;</small><br>
<small>import junit.framework.TestCase;</small><br>
<small>import junit.framework.TestSuite;</small><br>
<br>
<small>import org.eclipse.emf.common.EMFPlugin;</small><br>
<small>import org.eclipse.emf.common.util.BasicEList;</small><br>
<small>import org.eclipse.emf.common.util.URI;</small><br>
<small>import org.eclipse.emf.ecore.EAttribute;</small><br>
<small>import org.eclipse.emf.ecore.EClass;</small><br>
<small>import org.eclipse.emf.ecore.EFactory;</small><br>
<small>import org.eclipse.emf.ecore.EObject;</small><br>
<small>import org.eclipse.emf.ecore.EPackage;</small><br>
<small>import org.eclipse.emf.ecore.EReference;</small><br>
<small>import org.eclipse.emf.ecore.ETypedElement;</small><br>
<small>import org.eclipse.emf.ecore.EcoreFactory;</small><br>
<small>import org.eclipse.emf.ecore.EcorePackage;</small><br>
<small>import org.eclipse.emf.ecore.InternalEObject;</small><br>
<small>import org.eclipse.emf.ecore.resource.Resource;</small><br>
<small>import org.eclipse.emf.ecore.resource.ResourceSet;</small><br>
<small>import org.eclipse.emf.ecore.resource.impl.ResourceImpl;</small><br >
<small>import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;</small ><br>
<small>import org.eclipse.emf.ecore.util.EcoreUtil;</small><br>
<small>import org.eclipse.emf.ecore.xmi.XMLResource;</small><br>
<small>import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;</small ><br>
<small>import org.eclipse.emf.test.common.TestUtil;</small><br>
<small>import org.eclipse.emf.test.core.AllSuites;</small><br>
<br>
<br>
<small>public class SimpleModelTest extends TestCase</small><br>
<small>{</small><br>
<small>&nbsp; private EPackage companyPackage;</small><br>
<br>
<small>&nbsp; private EClass employeeClass;</small><br>
<br>
<small>&nbsp; private EAttribute employeeName;</small><br>
<br>
<small>&nbsp; private EAttribute employeeManager;</small><br>
<br>
<small>&nbsp; private EClass departmentClass;</small><br>
<br>
<small>&nbsp; private EAttribute departmentName;</small><br>
<br>
<small>&nbsp; private EAttribute departmentNumber;</small><br>
<br>
<small>&nbsp; private EReference departmentEmployees;</small><br>
<br>
<small>&nbsp; public SimpleModelTest(String name)</small><br>
<small>&nbsp; {</small><br>
<small>&nbsp;&nbsp;&nbsp; super(name);</small><br>
<small>&nbsp; }</small><br>
<br>
<small>&nbsp; public static Test suite()</small><br>
<small>&nbsp; {</small><br>
<small>&nbsp;&nbsp;&nbsp; TestSuite ts = new TestSuite("SimpleModelTest");</small><br>
<small>&nbsp;&nbsp;&nbsp; ts.addTest(new SimpleModelTest("testPackageAndFactory"));</small><br>
<small>&nbsp;&nbsp;&nbsp; ts.addTest(new SimpleModelTest("testAttributes"));</small><br>
<small>&nbsp;&nbsp;&nbsp; ts.addTest(new SimpleModelTest("testReference"));</small><br>
<small>&nbsp;&nbsp;&nbsp; ts.addTest(new SimpleModelTest("testMetaData"));</small><br>
<small>&nbsp;&nbsp;&nbsp; ts.addTest(new SimpleModelTest("testSaveAndLoad"));</small><br>
<small>&nbsp;&nbsp;&nbsp; ts.addTest(new SimpleModelTest("testSaveAndLoadZip"));</small><br>
<small>&nbsp;&nbsp;&nbsp; ts.addTest(new SimpleModelTest("testProxy"));</small><br>
<small>&nbsp;&nbsp;&nbsp; ts.addTest(new
SimpleModelTest("testTrackingModificaiton"));</small><br >
<small>&nbsp;&nbsp;&nbsp; ts.addTest(new SimpleModelTest("testAddingDuplicates"));</small><br>
<small>&nbsp;&nbsp;&nbsp; ts.addTest(new
SimpleModelTest("testRenamingStructuralFeature"));</small ><br>
<small>&nbsp;&nbsp;&nbsp; ts.addTest(new SimpleModelTest("testRenamingClassifier"));</small><br>
<small>&nbsp;&nbsp;&nbsp; return ts;</small><br>
<small>&nbsp; }</small><br>
<br>
<small>&nbsp; /**</small><br>
<small>&nbsp;&nbsp; * @see junit.framework.TestCase#setUp()</small><br>
<small>&nbsp;&nbsp; */</small><br>
<small>&nbsp; @Override</small><br>
<small>&nbsp; protected void setUp() throws Exception</small><br>
<small>&nbsp; {</small><br>
<small>&nbsp;&nbsp;&nbsp; EcoreFactory ecoreFactory = EcoreFactory.eINSTANCE;</small><br>
<small>&nbsp;&nbsp;&nbsp; EcorePackage ecorePackage = EcorePackage.eINSTANCE;</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; employeeClass = ecoreFactory.createEClass();</small><br>
<small>&nbsp;&nbsp;&nbsp; employeeClass.setName("Employee");</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; employeeName = ecoreFactory.createEAttribute();</small><br>
<small>&nbsp;&nbsp;&nbsp; employeeName.setName("name");</small><br>
<small>&nbsp;&nbsp;&nbsp; employeeName.setEType(ecorePackage.getEString());</small><br >
<small>&nbsp;&nbsp;&nbsp; employeeClass.getEStructuralFeatures().add(employeeName);</small ><br>
<br>
<small>&nbsp;&nbsp;&nbsp; employeeManager = ecoreFactory.createEAttribute();</small><br>
<small>&nbsp;&nbsp;&nbsp; employeeManager.setName("manager");</small><br>
<small>&nbsp;&nbsp;&nbsp; employeeManager.setEType(ecorePackage.getEBoolean());</small ><br>
<small>&nbsp;&nbsp;&nbsp;
employeeClass.getEStructuralFeatures().add(employeeManager); </small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; departmentClass = ecoreFactory.createEClass();</small><br>
<small>&nbsp;&nbsp;&nbsp; departmentClass.setName("Department");</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; departmentName = ecoreFactory.createEAttribute();</small><br>
<small>&nbsp;&nbsp;&nbsp; departmentName.setName("name");</small><br>
<small>&nbsp;&nbsp;&nbsp; departmentName.setEType(ecorePackage.getEString());</small> <br>
<small>&nbsp;&nbsp;&nbsp;
departmentClass.getEStructuralFeatures().add(departmentName) ; </small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; departmentNumber = ecoreFactory.createEAttribute();</small><br>
<small>&nbsp;&nbsp;&nbsp; departmentNumber.setName("number");</small><br>
<small>&nbsp;&nbsp;&nbsp; departmentNumber.setEType(ecorePackage.getEInt());</small><br >
<small>&nbsp;&nbsp;&nbsp;
departmentClass.getEStructuralFeatures().add(departmentNumbe r); </small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; departmentEmployees = ecoreFactory.createEReference();</small><br>
<small>&nbsp;&nbsp;&nbsp; departmentEmployees.setName("employees");</small><br>
<small>&nbsp;&nbsp;&nbsp; departmentEmployees.setEType(employeeClass);</small><br>
<small>&nbsp;&nbsp;&nbsp;
departmentEmployees.setUpperBound(ETypedElement.UNBOUNDED_MU LTIPLICITY); </small><br>
<small>&nbsp;&nbsp;&nbsp; departmentEmployees.setContainment(true);</small><br>
<small>&nbsp;&nbsp;&nbsp;
departmentClass.getEStructuralFeatures().add(departmentEmplo yees); </small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; companyPackage = ecoreFactory.createEPackage();</small><br>
<small>&nbsp;&nbsp;&nbsp; companyPackage.setName("company");</small><br>
<small>&nbsp;&nbsp;&nbsp; companyPackage.setNsPrefix("company");</small><br>
<small>&nbsp;&nbsp;&nbsp;
companyPackage.setNsURI(<a class="moz-txt-link-rfc2396E" href="http:///com.example.company.ecore">"http:///com.example.company.ecore"</a>);</small><br>
<small>&nbsp;&nbsp;&nbsp; companyPackage.getEClassifiers().add(employeeClass);</small ><br>
<small>&nbsp;&nbsp;&nbsp; companyPackage.getEClassifiers().add(departmentClass);</small ><br>
<small>&nbsp;&nbsp;&nbsp; EPackage.Registry.INSTANCE.put(companyPackage.getNsURI(),
companyPackage);</small><br>
<small>&nbsp; }</small><br>
<br>
<small>&nbsp; /**</small><br>
<small>&nbsp;&nbsp; * @see junit.framework.TestCase#tearDown()</small><br>
<small>&nbsp;&nbsp; */</small><br>
<small>&nbsp; @Override</small><br>
<small>&nbsp; protected void tearDown() throws Exception</small><br>
<small>&nbsp; {</small><br>
<small>&nbsp;&nbsp;&nbsp; employeeName = null;</small><br>
<small>&nbsp;&nbsp;&nbsp; employeeManager = null;</small><br>
<small>&nbsp;&nbsp;&nbsp; employeeClass.getEStructuralFeatures().clear();</small><br >
<small>&nbsp;&nbsp;&nbsp; employeeClass = null;</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; departmentEmployees = null;</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; departmentName = null;</small><br>
<small>&nbsp;&nbsp;&nbsp; departmentNumber = null;</small><br>
<small>&nbsp;&nbsp;&nbsp; departmentClass.getEStructuralFeatures().clear();</small><br >
<small>&nbsp;&nbsp;&nbsp; departmentClass = null;</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; companyPackage = null;</small><br>
<small>&nbsp; }</small><br>
<br>
<small>&nbsp; public void testPackageAndFactory()</small><br>
<small>&nbsp; {</small><br>
<small>&nbsp;&nbsp;&nbsp; assertNotNull(companyPackage);</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals("company", companyPackage.getName());</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals("company", companyPackage.getNsPrefix());</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(<a class="moz-txt-link-rfc2396E" href="http:///com.example.company.ecore">"http:///com.example.company.ecore"</a>,
companyPackage.getNsURI());</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; EFactory companyFactory =
companyPackage.getEFactoryInstance();</small><br>
<small>&nbsp;&nbsp;&nbsp; assertNotNull(companyFactory);</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(companyPackage, companyFactory.getEPackage());</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; EObject employee = companyFactory.create(employeeClass);</small><br>
<small>&nbsp;&nbsp;&nbsp; assertNotNull(employee);</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(companyPackage,
employee.eClass().getEPackage());</small><br>
<small>&nbsp; }</small><br>
<br>
<small>&nbsp; public void testAttributes()</small><br>
<small>&nbsp; {</small><br>
<small>&nbsp;&nbsp;&nbsp; EFactory companyFactory =
companyPackage.getEFactoryInstance();</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; EObject employee1 = companyFactory.create(employeeClass);</small><br>
<small>&nbsp;&nbsp;&nbsp; employee1.eSet(employeeName, "John");</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals("John", employee1.eGet(employeeName));</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(Boolean.FALSE,
employee1.eGet(employeeManager));</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; EObject employee2 = companyFactory.create(employeeClass);</small><br>
<small>&nbsp;&nbsp;&nbsp; employee2.eSet(employeeName, "Katherine");</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals("Katherine", employee2.eGet(employeeName));</small><br>
<small>&nbsp;&nbsp;&nbsp; employee2.eSet(employeeManager, Boolean.TRUE);</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(Boolean.TRUE,
employee2.eGet(employeeManager));</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; EObject department =
companyFactory.create(departmentClass);</small><br>
<small>&nbsp;&nbsp;&nbsp; department.eSet(departmentName, "ABC");</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals("ABC", department.eGet(departmentName));</small><br>
<small>&nbsp;&nbsp;&nbsp; department.eSet(departmentNumber, new Integer(123));</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(new Integer(123),
department.eGet(departmentNumber));</small><br>
<small>&nbsp; }</small><br>
<br>
<small>&nbsp; public void testReference()</small><br>
<small>&nbsp; {</small><br>
<small>&nbsp;&nbsp;&nbsp; EFactory companyFactory =
companyPackage.getEFactoryInstance();</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; EObject employee1 = companyFactory.create(employeeClass);</small><br>
<small>&nbsp;&nbsp;&nbsp; EObject employee2 = companyFactory.create(employeeClass);</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; EObject department1 =
companyFactory.create(departmentClass);</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; @SuppressWarnings("unchecked")</small><br>
<small>&nbsp;&nbsp;&nbsp; List&lt;EObject&gt; department1Employees =
(List&lt;EObject&gt;)department1.eGet(departmentEmpl oyees); </small><br>
<small>&nbsp;&nbsp;&nbsp; assertNotNull(department1Employees);</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; department1Employees.add(employee1);</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(1, department1Employees.size());</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(employee1, department1Employees.get(0));</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(department1Employees,
department1.eGet(departmentEmployees));</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; department1Employees.add(employee2);</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(2, department1Employees.size());</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(employee2, department1Employees.get(1));</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(department1Employees,
department1.eGet(departmentEmployees));</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; //Should not affect the list</small><br>
<small>&nbsp;&nbsp;&nbsp; department1Employees.add(employee2);</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(2, department1Employees.size());</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(employee2, department1Employees.get(1));</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(department1Employees,
department1.eGet(departmentEmployees));</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; EObject department2 =
companyFactory.create(departmentClass);</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; @SuppressWarnings("unchecked")</small><br>
<small>&nbsp;&nbsp;&nbsp; List&lt;EObject&gt; department2Employees =
(List&lt;EObject&gt;)department2.eGet(departmentEmpl oyees); </small><br>
<small>&nbsp;&nbsp;&nbsp; assertNotNull(department2Employees);</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; department2Employees.add(employee1);</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(1, department2Employees.size());</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(employee1, department2Employees.get(0));</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(department2Employees,
department2.eGet(departmentEmployees));</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; //since departmentEmployees is an aggregation, employee1
should be removed</small><br>
<small>&nbsp;&nbsp;&nbsp; //from department1Employees</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(1, department1Employees.size());</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(employee2, department1Employees.get(0));</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(department1Employees,
department1.eGet(departmentEmployees));</small><br>
<small>&nbsp; }</small><br>
<br>
<small>&nbsp; public void testMetaData()</small><br>
<small>&nbsp; {</small><br>
<small>&nbsp;&nbsp;&nbsp; EFactory companyFactory =
companyPackage.getEFactoryInstance();</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; EObject employee1 = companyFactory.create(employeeClass);</small><br>
<small>&nbsp;&nbsp;&nbsp; EObject employee2 = companyFactory.create(employeeClass);</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; EObject department1 =
companyFactory.create(departmentClass);</small><br>
<small>&nbsp;&nbsp;&nbsp; @SuppressWarnings("unchecked")</small><br>
<small>&nbsp;&nbsp;&nbsp; List&lt;EObject&gt; department1Employees =
(List&lt;EObject&gt;)department1.eGet(departmentEmpl oyees); </small><br>
<small>&nbsp;&nbsp;&nbsp; EObject department2 =
companyFactory.create(departmentClass);</small><br>
<small>&nbsp;&nbsp;&nbsp; @SuppressWarnings("unchecked")</small><br>
<small>&nbsp;&nbsp;&nbsp; List&lt;EObject&gt; department2Employees =
(List&lt;EObject&gt;)department2.eGet(departmentEmpl oyees); </small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; department1Employees.add(employee1);</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(department1, employee1.eContainer());</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(department1Employees.size(),
department1.eContents().size());</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; department1Employees.add(employee2);</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(department1, employee1.eContainer());</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(department1Employees.size(),
department1.eContents().size());</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; department2Employees.add(employee1);</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(department2, employee1.eContainer());</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(department1Employees.size(),
department1.eContents().size());</small><br>
<small>&nbsp;&nbsp;&nbsp; assertEquals(department2Employees.size(),
department2.eContents().size());</small><br>
<small>&nbsp; }</small><br>
<small>&nbsp; </small><br>
<small>&nbsp; public void testSaveAndLoadZip() throws Exception</small><br>
<small>&nbsp; {</small><br>
<small>&nbsp;&nbsp;&nbsp; //Instanciating the model</small><br>
<small>&nbsp;&nbsp;&nbsp; EFactory companyFactory =
companyPackage.getEFactoryInstance();</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; EObject employee1 = companyFactory.create(employeeClass);</small><br>
<small>&nbsp;&nbsp;&nbsp; employee1.eSet(employeeName, "John");</small><br>
<small>&nbsp;&nbsp;&nbsp; EObject employee2 = companyFactory.create(employeeClass);</small><br>
<small>&nbsp;&nbsp;&nbsp; employee2.eSet(employeeName, "Jane");</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; EObject department =
companyFactory.create(departmentClass);</small><br>
<small>&nbsp;&nbsp;&nbsp; department.eSet(departmentName, "ACME1");</small><br>
<small>&nbsp;&nbsp;&nbsp; @SuppressWarnings("unchecked")</small><br>
<small>&nbsp;&nbsp;&nbsp; List&lt;EObject&gt; employeesList =
(List&lt;EObject&gt;)department.eGet(departmentEmplo yees); </small><br>
<small>&nbsp;&nbsp;&nbsp; employeesList.add(employee1);</small><br>
<small>&nbsp;&nbsp;&nbsp; employeesList.add(employee2);</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; URI departmentsURI =
URI.createFileURI(TestUtil.getPluginDirectory(AllSuites.PLUG IN_ID) +
"/departments.dept");</small><br>
<small>&nbsp;&nbsp;&nbsp; XMLResource departmentsResource = (XMLResource)new
XMIResourceFactoryImpl().createResource(departmentsURI);</small ><br>
<small>&nbsp;&nbsp;&nbsp; departmentsResource.setUseZip(true);</small><br>
<small>&nbsp;&nbsp;&nbsp; departmentsResource.getContents().add(department);</small><br >
<br>
<small>&nbsp;&nbsp;&nbsp; //Saving</small><br>
<small>&nbsp;&nbsp;&nbsp; departmentsResource.save(Collections.EMPTY_MAP);</small><br >
<small>&nbsp;&nbsp;&nbsp; assertTrue(new
File(departmentsURI.toFileString()).exists());</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; //Loading department in ResourceSet</small><br>
<small>&nbsp;&nbsp;&nbsp; ResourceSet resourceSet = new ResourceSetImpl();</small><br>
<small>&nbsp;&nbsp;&nbsp;
resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put( "dept",
new XMIResourceFactoryImpl()</small><br>
<small> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; {</small><br>
<small> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; /* (non-Javadoc)</small><br>
<small> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; * @see
org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl#create Resource(org.eclipse.emf.common.util.URI) </small><br>
<small> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; */</small><br>
<small> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; @Override</small><br>
<small> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; public Resource createResource(URI uri)</small><br>
<small> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</small><br>
<small> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XMLResource result =
(XMLResource)super.createResource(uri);</small><br>
<small> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result.setUseZip(true);</small><br>
<small> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return result;</small><br>

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Model building using only Java code [message #416020 is a reply to message #416001] Wed, 16 January 2008 13:01 Go to previous message
Eclipse UserFriend
Originally posted by: m_lehmeier.gmx.de

On 2008-01-16, Ed Merks <merks@ca.ibm.com> wrote:
> Michael,
>
> I didn't get the EMF part though. Here's an example of a JUnit test
> that creates a Ecore model. Maybe that helps...

Thank you very much!

This is exactly what I needed!

--
Lehmeier Michael
Previous Topic:SetCommand fails for EMap ?
Next Topic:Cannot modify resource set without a write transaction
Goto Forum:
  


Current Time: Thu Apr 25 18:56:40 GMT 2024

Powered by FUDForum. Page generated in 0.03224 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top