Skip to content

Instantly share code, notes, and snippets.

@bmaggi
Created October 27, 2017 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmaggi/2cac5e3529fc0d0314a80ee8e6e6fa1a to your computer and use it in GitHub Desktop.
Save bmaggi/2cac5e3529fc0d0314a80ee8e6e6fa1a to your computer and use it in GitHub Desktop.
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
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Benoit Maggi (CEA LIST) benoit.maggi@cea.fr - 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
*/
@SuppressWarnings("nls")
public class ArchitectureValidationTest {
public static final String ARCHITECTURE_PATH = "org.eclipse.papyrus.sysml14.architecture/resources/sysml14.architecture";
@BeforeClass
public static void loadElementTypeSet() {
ElementTypeSetConfigurationRegistry.getInstance();
}
/**
* Check that if an element has a field id then xmi:id should be the same
*/
@Test
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) allContents.next();
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