Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Example of Test to check that Emf id is identical to id field (very useful when reviewing code in Gerrit)
* Copyright (c) 2017 CEA LIST 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
* Contributors:
* Benoit Maggi (CEA LIST) - Initial API and implementation
package org.eclipse.papyrus.sysml14.architecture.tests.resources;
import java.lang.reflect.Method;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.papyrus.infra.types.core.registries.ElementTypeSetConfigurationRegistry;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
* Test the architecture model : - validate the model
* @author Benoit Maggi
public class ArchitectureValidationTest {
public static final String ARCHITECTURE_PATH = "org.eclipse.papyrus.sysml14.architecture/resources/sysml14.architecture";
public static void loadElementTypeSet() {
* Check that if an element has a field id then xmi:id should be the same
public void checkXMIIds() {
URI createPlatformPluginURI = URI.createPlatformPluginURI(ARCHITECTURE_PATH, true);
ResourceSetImpl resourceSetImpl = new ResourceSetImpl();
Resource resource = resourceSetImpl.getResource(createPlatformPluginURI, true);
TreeIterator<EObject> allContents = resource.getAllContents();
while (allContents.hasNext()) {
EObject eObject = (EObject);
String id = getId(eObject);
String xmiId = ((XMIResource) resource).getID(eObject);
if (id != null) {
Assert.assertEquals("Xmi id and id field should be the same", id, xmiId);
// SMALL hack to get id if present
// TODO: Would be nice to have an IIdentifiedElement interface in the MetaModel
public static String getId(Object obj) {
try {
Method method = obj.getClass().getMethod("getId");
if (method != null) {
return method.invoke(obj).toString();
} catch (Exception e) {
// doesn't matter
return null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment