Skip to main content
aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authoratikhomirov2006-01-12 20:53:06 +0000
committeratikhomirov2006-01-12 20:53:06 +0000
commit2b389a413ce603c4d69b85087f0c6c30740d741f (patch)
tree41bdd904b39796fe4db663967b48689d4f86a408 /tests
parent3432ce50fc50f3ffc7d7972984976d46a690e17e (diff)
downloadorg.eclipse.gmf-tooling-2b389a413ce603c4d69b85087f0c6c30740d741f.tar.gz
org.eclipse.gmf-tooling-2b389a413ce603c4d69b85087f0c6c30740d741f.tar.xz
org.eclipse.gmf-tooling-2b389a413ce603c4d69b85087f0c6c30740d741f.zip
[rdvorak] #123656 Added message and description to audit rule
Diffstat (limited to 'tests')
-rw-r--r--tests/org.eclipse.gmf.tests/META-INF/MANIFEST.MF3
-rw-r--r--tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/AllTests.java4
-rw-r--r--tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/gen/AuditHandcodedTest.java76
-rw-r--r--tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/rt/AuditRulesTest.java201
-rw-r--r--tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/setup/LinksSessionSetup.java17
-rw-r--r--tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/setup/MapSetup.java43
6 files changed, 330 insertions, 14 deletions
diff --git a/tests/org.eclipse.gmf.tests/META-INF/MANIFEST.MF b/tests/org.eclipse.gmf.tests/META-INF/MANIFEST.MF
index 4bc231776..1f3eaa7c2 100644
--- a/tests/org.eclipse.gmf.tests/META-INF/MANIFEST.MF
+++ b/tests/org.eclipse.gmf.tests/META-INF/MANIFEST.MF
@@ -16,6 +16,7 @@ Require-Bundle: org.eclipse.core.runtime,
org.eclipse.gmf.runtime.diagram.ui,
org.eclipse.gmf.runtime.emf.commands.core,
org.eclipse.gmf.graphdef.codegen,
- org.eclipse.gmf.tooldef
+ org.eclipse.gmf.tooldef,
+ org.eclipse.emf.validation
Bundle-Activator: org.eclipse.gmf.tests.Plugin
Eclipse-LazyStart: true
diff --git a/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/AllTests.java b/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/AllTests.java
index 958a7b4ea..88d2a5048 100644
--- a/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/AllTests.java
+++ b/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/AllTests.java
@@ -20,11 +20,13 @@ import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.eclipse.gmf.tests.gef.DiagramNodeTest;
+import org.eclipse.gmf.tests.gen.AuditHandcodedTest;
import org.eclipse.gmf.tests.gen.CompilationTest;
import org.eclipse.gmf.tests.gen.FigureCodegenTest;
import org.eclipse.gmf.tests.gen.HandcodedImplTest;
import org.eclipse.gmf.tests.gen.ToolDefHandocodedImplTest;
import org.eclipse.gmf.tests.gen.ViewmapProducersTest;
+import org.eclipse.gmf.tests.rt.AuditRulesTest;
import org.eclipse.gmf.tests.rt.LinkCreationConstraintsTest;
import org.eclipse.gmf.tests.setup.LinksSessionSetup;
import org.eclipse.gmf.tests.setup.SessionSetup;
@@ -49,6 +51,8 @@ public class AllTests {
suite.addTestSuite(FigureCodegenTest.class);
suite.addTestSuite(ViewmapProducersTest.class);
suite.addTestSuite(ToolDefHandocodedImplTest.class);
+ suite.addTestSuite(AuditHandcodedTest.class);
+ suite.addTest(feed(AuditRulesTest.class, sessionSetup2));
// fires new runtime workbench initialization
suite.addTestSuite(CompilationTest.class);
diff --git a/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/gen/AuditHandcodedTest.java b/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/gen/AuditHandcodedTest.java
new file mode 100644
index 000000000..af9cd37d9
--- /dev/null
+++ b/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/gen/AuditHandcodedTest.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2005 Borland Software Corporation
+ *
+ * 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: Radek Dvorak (Borland) - initial API and implementation
+ */
+package org.eclipse.gmf.tests.gen;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.eclipse.gmf.codegen.gmfgen.GMFGenFactory;
+import org.eclipse.gmf.codegen.gmfgen.GenAuditContainer;
+import org.eclipse.gmf.codegen.gmfgen.GenAuditRule;
+
+/**
+ * Tests hancoded method in model for audit rule definitions
+ */
+public class AuditHandcodedTest extends TestCase {
+ private List allAudits;
+ private List containers;
+ private GenAuditContainer root;
+ private GenAuditContainer child1;
+ private GenAuditContainer child2;
+
+ private GenAuditRule rule0;
+ private GenAuditRule rule1;
+ private GenAuditRule rule2;
+
+ public AuditHandcodedTest(String name) {
+ super(name);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ allAudits = new ArrayList();
+ containers = new ArrayList();
+
+ containers.add(root = GMFGenFactory.eINSTANCE.createGenAuditContainer());
+ allAudits.add(rule0 = GMFGenFactory.eINSTANCE.createGenAuditRule());
+ root.getAudits().add(rule0);
+
+ containers.add(child1 = GMFGenFactory.eINSTANCE.createGenAuditContainer());
+ root.getChildContainers().add(child1);
+ allAudits.add(rule1 = GMFGenFactory.eINSTANCE.createGenAuditRule());
+ child1.getAudits().add(rule1);
+
+ containers.add(child2 = GMFGenFactory.eINSTANCE.createGenAuditContainer());
+ allAudits.add(rule2 = GMFGenFactory.eINSTANCE.createGenAuditRule());
+ child2.getAudits().add(rule2);
+ child1.getChildContainers().add(child2);
+ }
+
+ public void testPath() throws Exception {
+ assertEquals(root.getPath().size(), 1);
+ assertEquals(root.getPath().get(0), root);
+
+ assertEquals(child1.getPath().size(), 2);
+ assertEquals(child1.getPath().get(0), root);
+ assertEquals(child1.getPath().get(1), child1);
+ }
+
+ public void testGetAllAudits() throws Exception {
+ assertEquals(allAudits, root.getAllAuditRules());
+ }
+
+ public void testGetAllContainers() throws Exception {
+ assertEquals(containers, root.getAllAuditContainers());
+ }
+}
diff --git a/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/rt/AuditRulesTest.java b/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/rt/AuditRulesTest.java
new file mode 100644
index 000000000..824e77d88
--- /dev/null
+++ b/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/rt/AuditRulesTest.java
@@ -0,0 +1,201 @@
+/*
+ * Copyright (c) 2005 Borland Software Corporation
+ *
+ * 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: Radek Dvorak (Borland) - initial API and implementation
+ */
+package org.eclipse.gmf.tests.rt;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import junit.framework.Assert;
+
+import org.eclipse.emf.codegen.ecore.genmodel.GenPackage;
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.validation.model.CategoryManager;
+import org.eclipse.emf.validation.model.EvaluationMode;
+import org.eclipse.emf.validation.model.IConstraintStatus;
+import org.eclipse.emf.validation.model.IModelConstraint;
+import org.eclipse.emf.validation.service.IConstraintDescriptor;
+import org.eclipse.emf.validation.service.IValidationListener;
+import org.eclipse.emf.validation.service.IValidator;
+import org.eclipse.emf.validation.service.ModelValidationService;
+import org.eclipse.emf.validation.service.ValidationEvent;
+import org.eclipse.gmf.mappings.AuditContainer;
+import org.eclipse.gmf.mappings.AuditRule;
+import org.eclipse.gmf.mappings.Mapping;
+import org.eclipse.gmf.tests.setup.MapDefSource;
+import org.osgi.framework.Bundle;
+
+/**
+ * Tests valid registration of audit rule definitions in emft validation framework
+ * constraint registry and correct evaluation on targeted objects.
+ *
+ * Remark:
+ * Assertion logic is separated into <code>AuditAssert</code> nested class to avoid
+ * initialization of the emft validation framework constraint registry before
+ * the gmf editor plugin is generated and installed.
+ * This would happen during reflective inspection of this testcase class by junit test
+ * runner.
+ * The reason is that the validation framework collects all constraint
+ * providers descriptor only once during its bundle activation and newly installed extensions
+ * would not be added
+ */
+public class AuditRulesTest extends RuntimeDiagramTestBase {
+ private AuditContainer audits;
+ private GenPackage domainModel;
+
+ public AuditRulesTest(String name) {
+ super(name);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ MapDefSource mapSource = getSetup().getMapModel();
+ assertNotNull("Requires MapDefSource in setup", mapSource); //$NON-NLS-1$
+ Mapping mapping = mapSource.getMapping();
+ this.audits = mapping.getAudits();
+ assertNotNull("Requires mapping with audit rules", audits); //$NON-NLS-1$
+ this.domainModel = getSetup().getGenModel().getGenDiagram().getDomainMetaModel();
+ }
+
+ public void testAuditConstraints() throws Exception {
+ new AuditAssert(gmfEditorBundle, domainModel).assertAuditContainer(audits);
+ }
+
+ private static class AuditAssert {
+ private String pluginId;
+ private GenPackage domainModel;
+
+ AuditAssert(Bundle gmfEditorBundle, GenPackage domainModel) {
+ this.pluginId = gmfEditorBundle.getSymbolicName();
+ this.domainModel = domainModel;
+ }
+
+ String constraintGlobalId(AuditRule audit) {
+ return pluginId + "." + audit.getId(); //$NON-NLS-1$
+ }
+
+ void assertAudit(AuditRule audit) {
+ EClass target = ModelValidationService.findClass(domainModel.getNSURI(), audit.getTarget().getName());
+ EObject validatedInstance = target.getEPackage().getEFactoryInstance().create(target);
+
+ assertEvaluation(audit, validatedInstance);
+ // Note: Only when the constraint is used in evaluation, its descriptor gets
+ // registered in ConstraintRegistry (lazily constructed)
+ IConstraintDescriptor descriptor = org.eclipse.emf.validation.service.ConstraintRegistry.getInstance().getDescriptor(pluginId, audit.getId());
+ assertNotNull("Audit rule must be in ConstraintRegistry", descriptor); //$NON-NLS-1$
+
+ assertTrue("The target class must be accepted", descriptor.targetsTypeOf(validatedInstance)); //$NON-NLS-1$
+
+
+ assertTrue("Descriptor shoud be valid and enabled", //$NON-NLS-1$
+ descriptor.isEnabled());
+ assertEquals("Constraint id must match", //$NON-NLS-1$
+ constraintGlobalId(audit), descriptor.getId());
+ assertEquals("Constraint Name must match", //$NON-NLS-1$
+ audit.getName(), descriptor.getName());
+ assertEquals("Severity must match", //$NON-NLS-1$
+ audit.getSeverity().getName(), descriptor.getSeverity().getName());
+ assertNotNull("target class is required", audit.getTarget()); //$NON-NLS-1$
+
+ if (audit.isUseInLiveMode()) {
+ // mixed mode expected
+ assertEquals(descriptor.getEvaluationMode(), org.eclipse.emf.validation.model.EvaluationMode.NULL);
+ } else {
+ assertEquals(descriptor.getEvaluationMode(), org.eclipse.emf.validation.model.EvaluationMode.BATCH);
+ }
+
+ assertEquals("Constraint description must match", //$NON-NLS-1$
+ audit.getDescription(), descriptor.getDescription());
+ assertEquals("Constraint message must match", //$NON-NLS-1$
+ audit.getMessage(), descriptor.getMessagePattern());
+ // check categories
+ Set categories = descriptor.getCategories();
+ assertEquals("Single category expected", 1, categories.size()); //$NON-NLS-1$
+
+ assertEquals("Constraint category must be registered", //$NON-NLS-1$
+ categories.iterator().next(), CategoryManager.getInstance().getCategory(AuditAssert.getCategoryPath(audit.getContainer())));
+ assertEvaluation(audit, validatedInstance);
+ }
+
+ /*
+ * Checks if the constraint is correctly targeted to validated instances.
+ */
+ void assertEvaluation(final AuditRule audit, final EObject validatedInstance) {
+ final IModelConstraint[] constraintFound = new IModelConstraint[1];
+ IValidationListener listener = new IValidationListener() {
+ public void validationOccurred(ValidationEvent event) {
+ boolean isTargetMatch = false;
+ for (Iterator it = event.getValidationTargets().iterator(); it.hasNext();) {
+ if(it.next() == validatedInstance) {
+ isTargetMatch = true; //
+ break;
+ }
+ }
+ Assert.assertTrue("Target object must be validated", isTargetMatch); //$NON-NLS-1$
+
+ for (Iterator it = event.getValidationResults().iterator(); it.hasNext();) {
+ IConstraintStatus status = (IConstraintStatus) it.next();
+ if (constraintGlobalId(audit).equals(status.getConstraint().getDescriptor().getId())) {
+ constraintFound[0] = status.getConstraint();
+ break;
+ }
+ }
+ }
+ };
+
+ ModelValidationService.getInstance().addValidationListener(listener);
+
+ IValidator validator = ModelValidationService.getInstance().newValidator(EvaluationMode.BATCH);
+ validator.setReportSuccesses(true);
+ validator.validate(validatedInstance);
+
+ ModelValidationService.getInstance().removeValidationListener(listener);
+
+ assertNotNull("Constraint must be involved in validation", constraintFound[0]); //$NON-NLS-1$
+ }
+
+ void assertAuditContainer(AuditContainer auditContainer) {
+ org.eclipse.emf.validation.model.Category category = org.eclipse.emf.validation.model.CategoryManager.getInstance().getCategory(getCategoryPath(auditContainer));
+ assertEquals("Category id must match", //$NON-NLS-1$
+ auditContainer.getId(), category.getId());
+ assertEquals("Category name must match", //$NON-NLS-1$
+ auditContainer.getName(), category.getName());
+ assertEquals("Category description must match", //$NON-NLS-1$
+ auditContainer.getDescription(), category.getDescription());
+
+ for (Iterator it = auditContainer.getAudits().iterator(); it.hasNext();) {
+ AuditRule nextRule = (AuditRule) it.next();
+ assertAudit(nextRule);
+ }
+ for (Iterator it = auditContainer.getChildContainers().iterator(); it.hasNext();) {
+ assertAuditContainer((AuditContainer) it.next());
+ }
+ }
+
+ static String getCategoryPath(AuditContainer container) {
+ List pathElements = new ArrayList();
+ for (AuditContainer current = container; current != null; current = current.getParentContainer()) {
+ pathElements.add(0, current);
+ }
+ StringBuffer buf = new StringBuffer();
+ for (int i = 0; i < pathElements.size(); i++) {
+ AuditContainer nextContainer = (AuditContainer) pathElements.get(i);
+ if (i > 0) {
+ buf.append('/');
+ }
+ buf.append(nextContainer.getId());
+ }
+ return buf.toString();
+ }
+ }
+}
diff --git a/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/setup/LinksSessionSetup.java b/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/setup/LinksSessionSetup.java
index c5aa5304a..ab9dfc87c 100644
--- a/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/setup/LinksSessionSetup.java
+++ b/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/setup/LinksSessionSetup.java
@@ -24,8 +24,6 @@ import org.eclipse.gmf.tests.Plugin;
public class LinksSessionSetup extends SessionSetup {
private static String modelURI = "/models/links/links.ecore"; //$NON-NLS-1$
- private MapSetup mapDefSource;
-
private LinksSessionSetup() {
}
@@ -71,18 +69,11 @@ public class LinksSessionSetup extends SessionSetup {
}
protected DiaGenSource createGenModel() {
- return new DiaGenSetup().init(getMapDefSource());
+ return new DiaGenSetup().init(getMapModel());
}
- /**
- * XXX perhaps, just move to superclass for uniformity, override createMapDefSource here.
- */
- public MapDefSource getMapDefSource() {
- if(mapDefSource != null) {
- return mapDefSource;
- }
-
- this.mapDefSource = new MapSetup() {
+ protected MapDefSource createMapModel() {
+ MapSetup mapDefSource = new MapSetup() {
protected void setupClassLinkMapping(LinkMapping lme) {
addCreationConstraints(lme, null, "self.acceptLinkKind = oppositeEnd.acceptLinkKind"); //$NON-NLS-1$
}
@@ -92,7 +83,7 @@ public class LinksSessionSetup extends SessionSetup {
"self.acceptLinkKind = oppositeEnd.acceptLinkKind"); //$NON-NLS-1$
}
};
- this.mapDefSource.init(new DiaDefSetup(null).init(), getDomainModel(), new ToolDefSetup());
+ mapDefSource.init(new DiaDefSetup(null).init(), getDomainModel(), new ToolDefSetup());
// TODO - uncomment when multiple elements with the same domainMetaElement do not cause compilation problem
//LinkMapping FIRST_CHILD_LINK_MAPPING = mapDefSource.mapClassLink("Link", "Container::firstChildNode", "Link::target"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
diff --git a/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/setup/MapSetup.java b/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/setup/MapSetup.java
index eaaedc9c0..f2bfbd05f 100644
--- a/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/setup/MapSetup.java
+++ b/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/setup/MapSetup.java
@@ -19,6 +19,8 @@ import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.gmf.gmfgraph.Canvas;
import org.eclipse.gmf.gmfgraph.Connection;
import org.eclipse.gmf.gmfgraph.Node;
+import org.eclipse.gmf.mappings.AuditContainer;
+import org.eclipse.gmf.mappings.AuditRule;
import org.eclipse.gmf.mappings.CanvasMapping;
import org.eclipse.gmf.mappings.Constraint;
import org.eclipse.gmf.mappings.GMFMapFactory;
@@ -26,6 +28,8 @@ import org.eclipse.gmf.mappings.LinkConstraints;
import org.eclipse.gmf.mappings.LinkMapping;
import org.eclipse.gmf.mappings.Mapping;
import org.eclipse.gmf.mappings.NodeMapping;
+import org.eclipse.gmf.mappings.Severity;
+import org.eclipse.gmf.tests.Plugin;
import org.eclipse.gmf.tests.setup.DomainModelSource.LinkData;
import org.eclipse.gmf.tests.setup.DomainModelSource.NodeData;
@@ -62,6 +66,7 @@ public class MapSetup implements MapDefSource {
if (domainSource.getLinkAsRef() != null) {
myRefLink = createLinkMapping(ddSource.getLinkDef(), null, domainSource.getLinkAsRef(), null);
}
+ initAudits();
return this;
}
@@ -76,6 +81,44 @@ public class MapSetup implements MapDefSource {
m.setDiagram(cme);
myMap = m;
}
+
+ private void initAudits() {
+ EClass classA = myNodeA.getDomainMetaElement();
+ EClass classB = myNodeB.getDomainMetaElement();
+ AuditContainer root = createAuditContainer(Plugin.getPluginID() + ".category1" + System.currentTimeMillis()); //$NON-NLS-1$
+ // create set of allways satisfied constraints
+ root.getAudits().add(createAudit("constraint.id1", "true", classA, Severity.ERROR_LITERAL, false)); //$NON-NLS-1$ //$NON-NLS-2$
+ root.getAudits().add(createAudit("constraint.id2", "10 > 0", classB, Severity.WARNING_LITERAL, false)); //$NON-NLS-1$ //$NON-NLS-2$
+
+ AuditContainer subCat = createAuditContainer("category2"); //$NON-NLS-1$
+ root.getChildContainers().add(subCat);
+ subCat.getAudits().add(createAudit("constraint.id3", "''<>'Foo'", classA, Severity.INFO_LITERAL, false)); //$NON-NLS-1$ //$NON-NLS-2$
+
+ myMap.setAudits(root);
+ }
+
+ private AuditRule createAudit(String id, String ruleBody, EClass target, Severity severity, boolean isLiveMode) {
+ AuditRule audit = GMFMapFactory.eINSTANCE.createAuditRule();
+ audit.setId(id);
+ audit.setName("Name of" + id); //$NON-NLS-1$
+ audit.setMessage("Violation of " + id); //$NON-NLS-1$
+ audit.setDescription("Description of " + id); //$NON-NLS-1$
+ audit.setTarget(target);
+ Constraint rule = GMFMapFactory.eINSTANCE.createConstraint();
+ rule.setBody(ruleBody);
+ audit.setRule(rule);
+
+ audit.setSeverity(severity);
+ audit.setUseInLiveMode(isLiveMode);
+ return audit;
+ }
+ private AuditContainer createAuditContainer(String id) {
+ AuditContainer container = GMFMapFactory.eINSTANCE.createAuditContainer();
+ container.setId(id);
+ container.setName("Name of " + id); //$NON-NLS-1$
+ container.setDescription("Description of " + id); //$NON-NLS-1$
+ return container;
+ }
private LinkMapping createLinkMapping(Connection link, LinkData data) {
return createLinkMapping(link, data.getEClass(), data.getTargetFeature(), data.getContainment());

Back to the top