Created
October 24, 2017 15:19
-
-
Save bmaggi/8523564340eec9dc86ba1f34b26bb048 to your computer and use it in GitHub Desktop.
A Parameterized Junit test used in an eclipse test fragment to check that all emf models are valid.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.Collection; | |
import org.eclipse.emf.common.util.Diagnostic; | |
import org.eclipse.emf.common.util.URI; | |
import org.eclipse.emf.ecore.resource.Resource; | |
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; | |
import org.eclipse.emf.ecore.util.Diagnostician; | |
import org.junit.Assert; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.junit.runners.Parameterized; | |
import org.junit.runners.Parameterized.Parameters; | |
/** | |
* This is a parameterized test to validate all model in the plugin | |
* | |
* @author Benoit Maggi | |
*/ | |
@SuppressWarnings("nls") | |
@RunWith(Parameterized.class) | |
public class ModelValidationTest { | |
private static final String PLUGIN_ID = "my.plugin.id"; | |
@Parameters | |
public static Collection<Object[]> data() { | |
return Arrays.asList(new Object[][] { | |
{"/resources/myModel1.emf"}, | |
{"/resources/myModel2.emf"} | |
}); | |
} | |
private String modelPath; | |
public ModelValidationTest(String modelPath) { | |
this.modelPath = modelPath; | |
} | |
/** | |
* Validate the model with the rules defined in the meta-model tooling | |
*/ | |
@Test | |
public void validateModel() { | |
String fullPath = PLUGIN_ID + this.modelPath; | |
URI modelPlatformURI = URI.createPlatformPluginURI(fullPath, true); | |
Resource resource = new ResourceSetImpl().getResource(modelPlatformURI, true); | |
Diagnostic diagnostic = Diagnostician.INSTANCE.validate(resource.getContents().get(0)); | |
Assert.assertEquals("The "+modelPath+" model is invalid ", Diagnostic.OK, diagnostic.getSeverity()); | |
} | |
} |
Pretty print diagnostics
\\ Assert.assertEquals("The "+modelPath+" model is invalid "+print(diagnostic), Diagnostic.OK, diagnostic.getSeverity());
private String print(Diagnostic diagnostic) {
List<Diagnostic> children = diagnostic.getChildren();
StringBuilder stringBuilder = new StringBuilder(diagnostic.getMessage());
for (Diagnostic diagnosticChildren : children) {
stringBuilder.append("\n"); //$NON-NLS-1$
stringBuilder.append(diagnosticChildren.getMessage());
}
return stringBuilder.toString();
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To pretty print the models in the resources directory
Use
ls | awk '{print "{\"/resources/"$1"\"},"}'