diff options
author | Christian W. Damus | 2021-04-01 15:33:20 +0000 |
---|---|---|
committer | Patrick Tessier | 2021-04-12 09:23:18 +0000 |
commit | 5f60acc76eb5f16c35933809251f32349a5d5e62 (patch) | |
tree | e082e9398fc3979e1e5aea4537a5d6642530e62b | |
parent | 1098a37bdff21130b69f36329934ea63df14fa71 (diff) | |
download | org.eclipse.papyrus-5f60acc76eb5f16c35933809251f32349a5d5e62.tar.gz org.eclipse.papyrus-5f60acc76eb5f16c35933809251f32349a5d5e62.tar.xz org.eclipse.papyrus-5f60acc76eb5f16c35933809251f32349a5d5e62.zip |
Bug 572532: [Tests] UML validateCamelCaseRule test fails consistently
OCL Pivot maintains environments on a per-thread basis, which means the
Modal Context thread cannot use an environment set up by pre-validation
hooks on the main thread. Thus this pre-processing needs to be performed
on the Modal Context thread also and the test needs to be updated to
use the hook mechanism to inject its OCL constraints from the file.
Change-Id: Id3fdb3b9f8ab876d9ad6a7b39500684c18133f28
Signed-off-by: Christian W. Damus <give.a.damus@gmail.com>
4 files changed, 103 insertions, 36 deletions
diff --git a/plugins/infra/services/org.eclipse.papyrus.infra.services.validation/src/org/eclipse/papyrus/infra/services/validation/commands/AbstractValidateCommand.java b/plugins/infra/services/org.eclipse.papyrus.infra.services.validation/src/org/eclipse/papyrus/infra/services/validation/commands/AbstractValidateCommand.java index caf672b1a7d..5a37c81c93c 100644 --- a/plugins/infra/services/org.eclipse.papyrus.infra.services.validation/src/org/eclipse/papyrus/infra/services/validation/commands/AbstractValidateCommand.java +++ b/plugins/infra/services/org.eclipse.papyrus.infra.services.validation/src/org/eclipse/papyrus/infra/services/validation/commands/AbstractValidateCommand.java @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2010, 2018 CEA LIST, Christian W. Damus, and others. + * Copyright (c) 2010, 2021 CEA LIST, Christian W. Damus, and others. * * * All rights reserved. This program and the accompanying materials @@ -14,7 +14,7 @@ * Christian W. Damus (CEA) - refactor for non-workspace abstraction of problem markers (CDO) * Patrick Tessier (CEA LIST) refacor to add allowing adding validation specific to UML * Christian W. Damus (CEA) - bug 432813 - * Christian W. Damus - bugs 497379, 533676 + * Christian W. Damus - bugs 497379, 533676, 572532 * *****************************************************************************/ package org.eclipse.papyrus.infra.services.validation.commands; @@ -137,15 +137,25 @@ public abstract class AbstractValidateCommand extends AbstractTransactionalComma protected void runValidation(final EObject validateElement) { final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); - ValidationRegistry.executeHooks(selectedElement, HookType.BEFORE); - if (diagnostician == null) { - diagnostician = ValidationRegistry.getDiagnostician(selectedElement); - } - ValidationOperation runValidationWithProgress = new ValidationOperation(validateElement, this); + ValidationOperation runValidationWithProgress = new ValidationOperation(validateElement, this) { + @Override + public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { + // bug 572532: The OCL environment and the diagnostician that uses it need to be initialized + // on the same thread as performs validation (that being the Modal Context thread) because + // since the 2021-03 release, OCL Pivot maintains the environments on a per-thread basis + ValidationRegistry.executeHooks(selectedElement, HookType.BEFORE); + if (diagnostician == null) { + diagnostician = ValidationRegistry.getDiagnostician(selectedElement); + } + + super.run(monitor); + } + }; IRunnableWithProgress createMarkersWithProgress = new IRunnableWithProgress() { + @Override public void run(final IProgressMonitor progressMonitor) throws InvocationTargetException, InterruptedException { try { handleDiagnostic(progressMonitor, diagnostic, validateElement, shell); diff --git a/tests/junit/plugins/uml/validation/org.eclipse.papyrus.uml.validation.tests/build.properties b/tests/junit/plugins/uml/validation/org.eclipse.papyrus.uml.validation.tests/build.properties index 4d4e8d3850e..8016c52afa4 100644 --- a/tests/junit/plugins/uml/validation/org.eclipse.papyrus.uml.validation.tests/build.properties +++ b/tests/junit/plugins/uml/validation/org.eclipse.papyrus.uml.validation.tests/build.properties @@ -4,7 +4,8 @@ bin.includes = .,\ resources/,\ META-INF/,\ plugin.properties,\ - about.html + about.html,\ + plugin.xml jars.compile.order = . source.. = src/ output.. = bin/ diff --git a/tests/junit/plugins/uml/validation/org.eclipse.papyrus.uml.validation.tests/plugin.xml b/tests/junit/plugins/uml/validation/org.eclipse.papyrus.uml.validation.tests/plugin.xml new file mode 100644 index 00000000000..325f22b86bf --- /dev/null +++ b/tests/junit/plugins/uml/validation/org.eclipse.papyrus.uml.validation.tests/plugin.xml @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="UTF-8"?> +<?eclipse version="3.4"?> +<plugin> + <extension + point="org.eclipse.papyrus.infra.services.validation.validationHooks"> + <validationHook + filter="org.eclipse.papyrus.uml.validation.tests.rules.TestOCLValidationRuleFromFile$HookFilter" + hook="org.eclipse.papyrus.uml.validation.tests.rules.TestOCLValidationRuleFromFile$Hook" + id="org.eclipse.papyrus.uml.validation.tests.OCLRulesFromFile"> + </validationHook> + </extension> + +</plugin> diff --git a/tests/junit/plugins/uml/validation/org.eclipse.papyrus.uml.validation.tests/src/org/eclipse/papyrus/uml/validation/tests/rules/TestOCLValidationRuleFromFile.java b/tests/junit/plugins/uml/validation/org.eclipse.papyrus.uml.validation.tests/src/org/eclipse/papyrus/uml/validation/tests/rules/TestOCLValidationRuleFromFile.java index 6f3a5470bbb..3aee7c57961 100644 --- a/tests/junit/plugins/uml/validation/org.eclipse.papyrus.uml.validation.tests/src/org/eclipse/papyrus/uml/validation/tests/rules/TestOCLValidationRuleFromFile.java +++ b/tests/junit/plugins/uml/validation/org.eclipse.papyrus.uml.validation.tests/src/org/eclipse/papyrus/uml/validation/tests/rules/TestOCLValidationRuleFromFile.java @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2014 CEA LIST. + * Copyright (c) 2014, 2021 CEA LIST, Christian W. Damus, and others. * * * All rights reserved. This program and the accompanying materials @@ -12,6 +12,7 @@ * Contributors: * Benoit Maggi (CEA LIST) benoit.maggi@cea.fr - Initial API and implementation * Ansgar Radermacher (CEA) ansgar.radermacher@cea.fr - Extension to validation test suite + * Christian W. Damus - bug 572532 * *****************************************************************************/ package org.eclipse.papyrus.uml.validation.tests.rules; @@ -20,12 +21,15 @@ import java.util.List; 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.resource.ResourceSet; import org.eclipse.emf.edit.domain.EditingDomain; import org.eclipse.emf.transaction.util.TransactionUtil; import org.eclipse.ocl.pivot.internal.resource.OCLAdapter; import org.eclipse.ocl.xtext.completeocl.utilities.CompleteOCLLoader; import org.eclipse.papyrus.infra.emf.gmf.command.GMFtoEMFCommandWrapper; +import org.eclipse.papyrus.infra.services.validation.IValidationFilter; +import org.eclipse.papyrus.infra.services.validation.IValidationHook; import org.eclipse.papyrus.infra.services.validation.commands.ValidateModelCommand; import org.eclipse.papyrus.uml.validation.tests.Activator; import org.eclipse.papyrus.uml.validation.tests.Messages; @@ -33,6 +37,7 @@ import org.eclipse.swt.widgets.Display; import org.eclipse.uml2.uml.Class; import org.eclipse.uml2.uml.Model; import org.eclipse.uml2.uml.Package; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -53,47 +58,25 @@ public class TestOCLValidationRuleFromFile extends AbstractValidationEditorTest protected final static String CLASS_LOWER_CASE = "lowerCaseClass"; //$NON-NLS-1$ + private static volatile boolean testRunning; + /* class starting with a lower case name (violates constraint in external OCL file) */ protected Class lowerCaseClass; @Before public void initModelForValidationTest() throws Exception { + testRunning = true; initModel(PROJECT_PREFIX + MODEL_NAME, MODEL_NAME, Activator.getDefault().getBundle()); Model model = (Model) getRootUMLModel(); Package pkg = (Package) model.getPackagedElement(PKG_OCL_FROM_FILE); Assert.assertNotNull(String.format(CAN_NOT_FIND_ELEMENT, PKG_OCL_FROM_FILE, model), pkg); - + lowerCaseClass = (Class) pkg.getPackagedElement(CLASS_LOWER_CASE); Assert.assertNotNull(String.format(CAN_NOT_FIND_ELEMENT, CLASS_LOWER_CASE, model), lowerCaseClass); - // Registering OCL components - // - ResourceSet modelResources = model.eResource().getResourceSet(); - OCLAdapter oclAdapter = OCLAdapter.getAdapter(modelResources); - CompleteOCLLoader helper = new CompleteOCLLoader(oclAdapter.getEnvironmentFactory()) { - @Override - protected boolean error(String primaryMessage, String detailMessage) { - Assert.fail(Messages.TestOCLValidationRuleFromFile_CanNotGetEnvFactory); - return false; - } - }; - Assert.assertTrue(Messages.TestOCLValidationRuleFromFile_OCLCanNotLoadMM, helper.loadMetamodels()); - - // load OCL file - URI oclURI = URI.createPlatformPluginURI(Activator.PLUGIN_ID + RESOURCES_PATH + EXTRA_UML_VALIDATION_OCL, false); - try { - if (!helper.loadDocument(oclURI)) { - Assert.fail("Can not load OCL document with URI: " + oclURI.path()); //$NON-NLS-1$ - } - } catch (Throwable e) { - Assert.fail(String.format("Exception %s during loading of OCL document with URI: %s", e.getMessage(), oclURI.path())); //$NON-NLS-1$ - } - helper.installPackages(); - - // it's important to create the validate model command after the OCL file has been loaded final ValidateModelCommand validateModelCommand = new ValidateModelCommand(model); - + final EditingDomain domain = TransactionUtil.getEditingDomain(model); Display.getDefault().syncExec(new Runnable() { @Override @@ -104,6 +87,11 @@ public class TestOCLValidationRuleFromFile extends AbstractValidationEditorTest globalDiagnostic = validateModelCommand.getDiagnostic(); } + @After + public void afterTest() { + testRunning = false; + } + /** * Failing validation for rule defined in ExtraUMLValidation */ @@ -115,4 +103,59 @@ public class TestOCLValidationRuleFromFile extends AbstractValidationEditorTest Messages.TestOCLValidationRuleFromFile_CamelCaseRule, EXTRA_UML_VALIDATION_OCL), 1, diagnostics.size()); } + + // + // Nested types + // + + /** + * A validation filter that matches while our tests are running. + */ + public static final class HookFilter implements IValidationFilter { + + @Override + public boolean isApplicable(EObject element) { + return testRunning; + } + + } + + /** + * A validation hook that installs our test OCL resource. + */ + public static final class Hook implements IValidationHook { + + @Override + public void beforeValidation(EObject element) { + // Registering OCL components + // + ResourceSet modelResources = element.eResource().getResourceSet(); + OCLAdapter oclAdapter = OCLAdapter.getAdapter(modelResources); + CompleteOCLLoader helper = new CompleteOCLLoader(oclAdapter.getEnvironmentFactory()) { + @Override + protected boolean error(String primaryMessage, String detailMessage) { + Assert.fail(Messages.TestOCLValidationRuleFromFile_CanNotGetEnvFactory); + return false; + } + }; + Assert.assertTrue(Messages.TestOCLValidationRuleFromFile_OCLCanNotLoadMM, helper.loadMetamodels()); + + // load OCL file + URI oclURI = URI.createPlatformPluginURI(Activator.PLUGIN_ID + RESOURCES_PATH + EXTRA_UML_VALIDATION_OCL, false); + try { + if (!helper.loadDocument(oclURI)) { + Assert.fail("Can not load OCL document with URI: " + oclURI.path()); //$NON-NLS-1$ + } + } catch (Throwable e) { + Assert.fail(String.format("Exception %s during loading of OCL document with URI: %s", e.getMessage(), oclURI.path())); //$NON-NLS-1$ + } + helper.installPackages(); + } + + @Override + public void afterValidation(EObject element) { + // Not needed + } + + } } |