blob: 0c9c281f108a963f31d46cb78dd1563f0dc75fa7 [file] [log] [blame]
/*****************************************************************************
* Copyright (c) 2018 CEA 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.iotml.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.eclipse.papyrus.iotml.architecture.Activator;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* Test the architecture model : - validate the model
*
*/
@SuppressWarnings("nls")
public class ArchitectureValidationTest {
public static final String ARCHITECTURE_PATH = Activator.PLUGIN_ID+"/resources/iotml.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
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;
}
}