Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenrik Rentz-Reichert2011-03-04 12:04:37 +0000
committerHenrik Rentz-Reichert2011-03-04 12:04:37 +0000
commit1ca373ad91c635f9ed5895be8e9b8e601d7500ad (patch)
tree44bc852616d7f06cdc9ef6d60fc19a00de012784 /tests/org.eclipse.etrice.core.room.tests/src/org/eclipse/etrice
parentbeb62ca5645b91fb0dadaaaaffd76e56981f2ed2 (diff)
downloadorg.eclipse.etrice-1ca373ad91c635f9ed5895be8e9b8e601d7500ad.tar.gz
org.eclipse.etrice-1ca373ad91c635f9ed5895be8e9b8e601d7500ad.tar.xz
org.eclipse.etrice-1ca373ad91c635f9ed5895be8e9b8e601d7500ad.zip
core.room.tests: new test plugin with sample test for model
validation
Diffstat (limited to 'tests/org.eclipse.etrice.core.room.tests/src/org/eclipse/etrice')
-rw-r--r--tests/org.eclipse.etrice.core.room.tests/src/org/eclipse/etrice/core/CoreTestsActivator.java47
-rw-r--r--tests/org.eclipse.etrice.core.room.tests/src/org/eclipse/etrice/core/TestBase.java73
-rw-r--r--tests/org.eclipse.etrice.core.room.tests/src/org/eclipse/etrice/core/TestCases.java75
3 files changed, 195 insertions, 0 deletions
diff --git a/tests/org.eclipse.etrice.core.room.tests/src/org/eclipse/etrice/core/CoreTestsActivator.java b/tests/org.eclipse.etrice.core.room.tests/src/org/eclipse/etrice/core/CoreTestsActivator.java
new file mode 100644
index 000000000..92a9bea90
--- /dev/null
+++ b/tests/org.eclipse.etrice.core.room.tests/src/org/eclipse/etrice/core/CoreTestsActivator.java
@@ -0,0 +1,47 @@
+/*******************************************************************************
+ * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
+ * 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:
+ * Henrik Rentz-Reichert (initial contribution)
+ *
+ *******************************************************************************/
+
+package org.eclipse.etrice.core;
+import org.eclipse.core.runtime.Plugin;
+import org.eclipse.emf.ecore.util.Diagnostician;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+
+
+public class CoreTestsActivator extends Plugin implements BundleActivator {
+
+ private static CoreTestsActivator instance = null;
+
+ @Inject
+ private Diagnostician diagnostician;
+
+ public static CoreTestsActivator getInstance() {
+ return instance;
+ }
+
+ @Override
+ public void start(BundleContext context) throws Exception {
+ super.start(context);
+
+ instance = this;
+
+ Injector injector = RoomStandaloneSetup.doSetup();
+ injector.injectMembers(this);
+ }
+
+ public Diagnostician getDiagnostician() {
+ return diagnostician;
+ }
+}
diff --git a/tests/org.eclipse.etrice.core.room.tests/src/org/eclipse/etrice/core/TestBase.java b/tests/org.eclipse.etrice.core.room.tests/src/org/eclipse/etrice/core/TestBase.java
new file mode 100644
index 000000000..0f05efbad
--- /dev/null
+++ b/tests/org.eclipse.etrice.core.room.tests/src/org/eclipse/etrice/core/TestBase.java
@@ -0,0 +1,73 @@
+/*******************************************************************************
+ * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
+ * 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:
+ * Henrik Rentz-Reichert (initial contribution)
+ *
+ *******************************************************************************/
+
+package org.eclipse.etrice.core;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Map;
+
+import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.emf.common.util.Diagnostic;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EValidator;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.xtext.resource.XtextResource;
+import org.eclipse.xtext.resource.XtextResourceSet;
+import org.eclipse.xtext.util.CancelIndicator;
+import org.eclipse.xtext.validation.CancelableDiagnostician;
+import org.eclipse.xtext.validation.CheckMode;
+import org.eclipse.xtext.validation.impl.ConcreteSyntaxEValidator;
+
+import com.google.common.collect.Maps;
+
+/**
+ * Base class for tests helps with getting diagnostics from a model.
+ *
+ * @author Henrik Rentz-Reichert initial contribution and API
+ *
+ */
+public class TestBase {
+
+ private String basePath;
+
+ protected void prepare() {
+ try {
+ URL modelsDir = CoreTestsActivator.getInstance().getBundle().getEntry("models");
+ URL fileURL = FileLocator.toFileURL(modelsDir);
+ basePath = fileURL.getFile();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ protected Resource getResource(String modelName) {
+ XtextResourceSet rs = new XtextResourceSet();
+ rs.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
+ String path = basePath + modelName;
+ URI uri = URI.createFileURI(path);
+ return rs.getResource(uri, true);
+ }
+
+ public Diagnostic getDiag(EObject ele) {
+ Map<Object, Object> options = Maps.newHashMap();
+ options.put(CheckMode.KEY, CheckMode.ALL);
+ options.put(CancelableDiagnostician.CANCEL_INDICATOR, new CancelIndicator.NullImpl());
+ // disable concrete syntax validation, since a semantic model that has been parsed
+ // from the concrete syntax always complies with it - otherwise there are parse errors.
+ options.put(ConcreteSyntaxEValidator.DISABLE_CONCRETE_SYNTAX_EVALIDATOR, Boolean.TRUE);
+ // see EObjectValidator.getRootEValidator(Map<Object, Object>)
+ options.put(EValidator.class, CoreTestsActivator.getInstance().getDiagnostician());
+ return CoreTestsActivator.getInstance().getDiagnostician().validate(ele, options);
+ }
+}
diff --git a/tests/org.eclipse.etrice.core.room.tests/src/org/eclipse/etrice/core/TestCases.java b/tests/org.eclipse.etrice.core.room.tests/src/org/eclipse/etrice/core/TestCases.java
new file mode 100644
index 000000000..a16c22f0c
--- /dev/null
+++ b/tests/org.eclipse.etrice.core.room.tests/src/org/eclipse/etrice/core/TestCases.java
@@ -0,0 +1,75 @@
+/*******************************************************************************
+ * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
+ * 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
+ *******************************************************************************/
+
+package org.eclipse.etrice.core;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.HashMap;
+
+import org.eclipse.emf.common.util.Diagnostic;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Example test to illustrate validation unit tests.
+ *
+ * @author Henrik Rentz-Reichert initial contribution and API
+ *
+ */
+public class TestCases extends TestBase {
+
+ /**
+ *
+ */
+ private static final String MSG1 = "connected sub component ports must be conjugated to each other";
+ private Resource resource;
+
+ @Before
+ public void setUp() {
+ prepare();
+
+ resource = getResource("test.room");
+ }
+
+ @Test
+ public void testSomething() {
+ Diagnostic diag = getDiag(resource.getContents().get(0));
+ HashMap<String, Diagnostic> msg2diag = getMappedDiagnostics(diag);
+ assertEquals("number of problems", 1, msg2diag.size());
+ assertNotNull(MSG1, msg2diag.get(MSG1));
+ }
+
+ /**
+ * @param diag
+ * @return
+ */
+ private HashMap<String, Diagnostic> getMappedDiagnostics(Diagnostic diag) {
+ HashMap<String, Diagnostic> msg2diag = new HashMap<String, Diagnostic>();
+ recursivlyCollectErrors(diag, msg2diag);
+ return msg2diag;
+ }
+
+ /**
+ * @param diag
+ * @param msg2diag
+ */
+ private void recursivlyCollectErrors(Diagnostic diag, HashMap<String, Diagnostic> msg2diag) {
+ for (Diagnostic d : diag.getChildren()) {
+ if ((d.getSeverity() & Diagnostic.ERROR) != 0) {
+ msg2diag.put(d.getMessage(), d);
+ }
+ }
+ for (Diagnostic d : diag.getChildren()) {
+ if (!d.getChildren().isEmpty())
+ recursivlyCollectErrors(d, msg2diag);
+ }
+ }
+}

Back to the top