summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMoritz Eysholdt2011-08-03 06:15:00 (EDT)
committerEd Merks2011-08-03 06:15:50 (EDT)
commit6607c4e1867e7051975930eb6bb10d2bcac31dc7 (patch)
tree900c7af7b0f5da315832283b572e712504e9580f
parentec1738a677979928fddb03a4883b87db90d9795a (diff)
downloadorg.eclipse.emf-6607c4e1867e7051975930eb6bb10d2bcac31dc7.zip
org.eclipse.emf-6607c4e1867e7051975930eb6bb10d2bcac31dc7.tar.gz
org.eclipse.emf-6607c4e1867e7051975930eb6bb10d2bcac31dc7.tar.bz2
[tests] created test framework to ensure sanity of our test data
-rw-r--r--org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/XcoreModelSanityTest.java25
-rw-r--r--org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/util/AbstractModelSanityTest.java181
-rw-r--r--org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/util/LabelledParameterized.java117
-rw-r--r--org.eclipse.emf.ecore.xcore.tests/xcore-src/test.xcore3
4 files changed, 326 insertions, 0 deletions
diff --git a/org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/XcoreModelSanityTest.java b/org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/XcoreModelSanityTest.java
new file mode 100644
index 0000000..03dd208
--- /dev/null
+++ b/org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/XcoreModelSanityTest.java
@@ -0,0 +1,25 @@
+package org.eclipse.emf.ecore.xcore.tests;
+
+import java.util.Collection;
+
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.xcore.XcoreStandaloneSetup;
+import org.eclipse.emf.ecore.xcore.tests.util.AbstractModelSanityTest;
+import org.eclipse.emf.ecore.xcore.tests.util.LabelledParameterized.LabelledParameters;
+
+public class XcoreModelSanityTest extends AbstractModelSanityTest {
+
+ static {
+ XcoreStandaloneSetup.doSetup();
+ }
+
+ public XcoreModelSanityTest(String label, Resource res) throws Exception {
+ super(label, res);
+ }
+
+ @LabelledParameters
+ public static Collection<Object[]> generateData() {
+ return collectTestFiles("xcore-src", "xcore");
+ }
+
+}
diff --git a/org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/util/AbstractModelSanityTest.java b/org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/util/AbstractModelSanityTest.java
new file mode 100644
index 0000000..5ecffa9
--- /dev/null
+++ b/org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/util/AbstractModelSanityTest.java
@@ -0,0 +1,181 @@
+package org.eclipse.emf.ecore.xcore.tests.util;
+
+import java.io.File;
+import java.util.Collection;
+import java.util.List;
+
+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.ResourceSet;
+import org.eclipse.emf.mwe.utils.StandaloneSetup;
+import org.eclipse.xtext.diagnostics.ExceptionDiagnostic;
+import org.eclipse.xtext.junit.serializer.SerializerTester;
+import org.eclipse.xtext.resource.XtextResource;
+import org.eclipse.xtext.resource.XtextResourceSet;
+import org.eclipse.xtext.util.CancelIndicator;
+import org.eclipse.xtext.validation.CheckMode;
+import org.eclipse.xtext.validation.IResourceValidator;
+import org.eclipse.xtext.validation.Issue;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import com.google.common.base.Joiner;
+import com.google.common.base.Predicate;
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Multimap;
+
+@RunWith(LabelledParameterized.class)
+public abstract class AbstractModelSanityTest {
+
+ public static class FilePredicate implements Predicate<File> {
+ private final String[] fileExts;
+
+ public FilePredicate(String... fileExts) {
+ this.fileExts = fileExts;
+ }
+
+ public boolean apply(File input) {
+ for (String ext : fileExts)
+ if (input.getName().endsWith("." + ext))
+ return true;
+ return false;
+ }
+ }
+
+ protected static List<File> files;
+
+ protected static void collectFiles(File dir, List<File> result,
+ Predicate<File> shouldLoad) {
+ for (File child : dir.listFiles()) {
+ if (shouldLoad.apply(child))
+ result.add(child);
+ if (child.isDirectory())
+ collectFiles(child, result, shouldLoad);
+ }
+ }
+
+ public static List<File> collectFiles(String directory,
+ Predicate<File> shouldCollect) {
+ File dir = new File(directory);
+ if (!dir.isDirectory())
+ throw new RuntimeException("Directory not found: " + directory);
+ List<File> result = Lists.newArrayList();
+ collectFiles(new File(directory), result, shouldCollect);
+ return result;
+ }
+
+ protected static List<File> collectFiles(String directory,
+ String... fileExtensions) {
+ return collectFiles(directory, new FilePredicate(fileExtensions));
+ }
+
+ protected static Collection<Object[]> collectTestFiles(String root,
+ String... fileExts) {
+ init();
+ List<Object[]> result = Lists.newArrayList();
+ ResourceSet rs = new XtextResourceSet();
+ URI rootURI = URI.createFileURI(new File(root).getAbsolutePath());
+ for (File file : collectFiles(root, fileExts)) {
+ URI uri = URI.createFileURI(file.getAbsolutePath());
+ Resource res = null;
+ try {
+ res = rs.getResource(uri, true);
+ } catch (Exception t) {
+ if (res == null)
+ res = rs.createResource(uri);
+ res.getErrors().add(new ExceptionDiagnostic(t));
+ }
+ List<String> segmentsList = uri.deresolve(rootURI).segmentsList();
+ String name = Joiner.on('/').join(
+ segmentsList.subList(1, segmentsList.size()));
+ result.add(new Object[] { name, res });
+ }
+ return result;
+ }
+
+ private static void init() {
+ new StandaloneSetup().setPlatformUri(".");
+ }
+
+ protected String label;
+
+ protected XtextResource resource;
+
+ public AbstractModelSanityTest(String label, Resource res) throws Exception {
+ this.label = label;
+ this.resource = (XtextResource) res;
+ }
+
+ protected String annotateDocumentWithIssues(String document,
+ List<Issue> issues) {
+ String[] lines = document.split("\n");
+ Multimap<Integer, Issue> issueByLine = HashMultimap.create();
+ for (Issue issue : issues)
+ issueByLine.put(issue.getLineNumber(), issue);
+ List<String> result = Lists.newArrayList();
+ for (int i = 0; i < lines.length; i++) {
+ Collection<Issue> lineIssues = issueByLine.get(i);
+ if (lineIssues.isEmpty())
+ result.add(lines[i]);
+ else {
+ StringBuilder line = new StringBuilder(lines[i]);
+ for (Issue issue : lineIssues) {
+ line.append("\n");
+ line.append("// " + formatIssue(issue));
+ }
+ result.add(line.toString());
+ issueByLine.removeAll(line);
+ }
+ }
+ for (Issue issue : issues)
+ if (issue.getLineNumber() >= lines.length)
+ result.add("// " + formatIssue(issue));
+ return Joiner.on('\n').join(result);
+ }
+
+ protected String formatIssue(Issue issue) {
+ StringBuilder result = new StringBuilder();
+ result.append(issue.getSeverity());
+ result.append(": ");
+ result.append(issue.getMessage());
+ result.append(" (");
+ result.append(issue.getCode());
+ result.append(" )");
+ return result.toString();
+ }
+
+ protected String getIssuesText(List<Issue> issues) {
+ List<String> result = Lists.newArrayList();
+ for (Issue issue : issues)
+ result.add(formatIssue(issue));
+ return Joiner.on('\n').join(result);
+ }
+
+ @Test
+ public void serialize() throws Exception {
+ Assert.assertTrue(resource.getContents().size() > 0);
+ EObject root = resource.getContents().get(0);
+ SerializerTester tester = resource.getResourceServiceProvider().get(
+ SerializerTester.class);
+ tester.assertSerializeWithNodeModel(root);
+ tester.assertSerializeWithoutNodeModel(root);
+ }
+
+ @Test
+ public void validate() {
+ IResourceValidator validator = resource.getResourceServiceProvider()
+ .get(IResourceValidator.class);
+ List<Issue> issues = validator.validate(resource, CheckMode.ALL,
+ CancelIndicator.NullImpl);
+ String expectedDocument = resource.getParseResult().getRootNode()
+ .getText();
+ String actualDocument = annotateDocumentWithIssues(expectedDocument,
+ issues);
+ Assert.assertEquals(getIssuesText(issues), expectedDocument,
+ actualDocument);
+ }
+
+}
diff --git a/org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/util/LabelledParameterized.java b/org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/util/LabelledParameterized.java
new file mode 100644
index 0000000..b872125
--- /dev/null
+++ b/org.eclipse.emf.ecore.xcore.tests/src/org/eclipse/emf/ecore/xcore/tests/util/LabelledParameterized.java
@@ -0,0 +1,117 @@
+package org.eclipse.emf.ecore.xcore.tests.util;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.junit.runner.Runner;
+import org.junit.runner.notification.RunNotifier;
+import org.junit.runners.BlockJUnit4ClassRunner;
+import org.junit.runners.Suite;
+import org.junit.runners.model.FrameworkMethod;
+import org.junit.runners.model.InitializationError;
+import org.junit.runners.model.Statement;
+import org.junit.runners.model.TestClass;
+
+public class LabelledParameterized extends Suite {
+ @Retention(RetentionPolicy.RUNTIME)
+ @Target(ElementType.METHOD)
+ public static @interface LabelledParameters {
+ }
+
+ private class TestClassRunnerForParameters extends BlockJUnit4ClassRunner {
+ private final int parameterSetNumber;
+
+ private final List<Object[]> parameters;
+
+ TestClassRunnerForParameters(Class<?> type,
+ List<Object[]> parameterList, int i) throws InitializationError {
+ super(type);
+ parameters = parameterList;
+ parameterSetNumber = i;
+ }
+
+ @Override
+ public Object createTest() throws Exception {
+ return getTestClass().getOnlyConstructor().newInstance(
+ computeParams());
+ }
+
+ protected Object[] computeParams() throws Exception {
+ try {
+ return parameters.get(parameterSetNumber);
+ } catch (ClassCastException e) {
+ throw new Exception(String.format(
+ "%s.%s() must return a Collection of arrays.",
+ getTestClass().getName(),
+ getParametersMethod(getTestClass()).getName()));
+ }
+ }
+
+ @Override
+ protected String getName() {
+ return String.format("%s", parameters.get(parameterSetNumber)[0]);
+ }
+
+ @Override
+ protected String testName(final FrameworkMethod method) {
+ return String.format("%s -> %s",
+ parameters.get(parameterSetNumber)[0], method.getName());
+ }
+
+ @Override
+ protected void validateZeroArgConstructor(List<Throwable> errors) {
+ // super.validateZeroArgConstructor(errors);
+ }
+
+ @Override
+ protected Statement classBlock(RunNotifier notifier) {
+ return childrenInvoker(notifier);
+ }
+ }
+
+ protected final ArrayList<Runner> runners = new ArrayList<Runner>();
+
+ /**
+ * Only called reflectively. Do not use programmatically.
+ */
+ public LabelledParameterized(Class<?> klass) throws Throwable {
+ super(klass, Collections.<Runner> emptyList());
+ List<Object[]> parametersList = getParametersList(getTestClass());
+ for (int i = 0; i < parametersList.size(); i++)
+ runners.add(new TestClassRunnerForParameters(getTestClass()
+ .getJavaClass(), parametersList, i));
+ }
+
+ @Override
+ protected List<Runner> getChildren() {
+ return runners;
+ }
+
+ @SuppressWarnings("unchecked")
+ protected List<Object[]> getParametersList(TestClass klass)
+ throws Throwable {
+ return (List<Object[]>) getParametersMethod(klass).invokeExplosively(
+ null);
+ }
+
+ protected FrameworkMethod getParametersMethod(TestClass testClass)
+ throws Exception {
+ List<FrameworkMethod> methods = testClass
+ .getAnnotatedMethods(LabelledParameters.class);
+ for (FrameworkMethod each : methods) {
+ int modifiers = each.getMethod().getModifiers();
+ if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))
+ return each;
+ }
+
+ throw new Exception("No public static parameters method on class "
+ + testClass.getName());
+ }
+
+} \ No newline at end of file
diff --git a/org.eclipse.emf.ecore.xcore.tests/xcore-src/test.xcore b/org.eclipse.emf.ecore.xcore.tests/xcore-src/test.xcore
new file mode 100644
index 0000000..cd4be96
--- /dev/null
+++ b/org.eclipse.emf.ecore.xcore.tests/xcore-src/test.xcore
@@ -0,0 +1,3 @@
+package foo
+
+class Bar {} \ No newline at end of file