Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 66252d9f94fe0f9abb7ed913a2e527d23f09d26f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*****************************************************************************
 * Copyright (c) 2014 CEA LIST.
 *
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * 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
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.validation.tests.rules;

import java.util.List;

import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.emf.validation.service.ConstraintRegistry;
import org.eclipse.emf.validation.service.IConstraintDescriptor;
import org.eclipse.papyrus.infra.emf.gmf.command.GMFtoEMFCommandWrapper;
import org.eclipse.papyrus.infra.services.validation.commands.ValidateModelCommand;
import org.eclipse.papyrus.junit.framework.classification.FailingTest;
import org.eclipse.papyrus.uml.validation.tests.Activator;
import org.eclipse.papyrus.uml.validation.tests.Messages;
import org.eclipse.swt.widgets.Display;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Model;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * Test suite for validation rules. It tests
 * Model with OCL constraints in plugin.xml (EMFv)
 * Model with Java constraints in plugin.xml (EMFv)
 */
public class TestValidationRulesInPluginXML extends AbstractValidationEditorTest {

	public static final String CONSTRAINT_PLUGIN = "org.eclipse.papyrus.uml.validation.tests.genvalidation"; //$NON-NLS-1$

	public static final String CONSTRAINT_ID_ALWAYS_ACTIVE = "profile.AlwaysActive.ConstraintAlwaysActive"; //$NON-NLS-1$
	
	public static final String MODEL_NAME = "active-tst.rule-in-plugin"; //$NON-NLS-1$

	/**
	 * An active class applying a stereotype with an attached OCL constraint requiring that the class is active
	 */
	public static final String INACTIVE_OCL_NAME = "ActiveOCL"; //$NON-NLS-1$
	protected Class activeOCL;

	/**
	 * A non active class applying a stereotype with an attached OCL constraint requiring that the class is active
	 */
	public static final String ACTIVE_OCL_NAME = "InactiveOCL"; //$NON-NLS-1$
	protected Class inactiveOCL;

	/**
	 * A non active class applying a stereotype with an attached Java constraint requiring that the class is passive
	 */
	public static final String INACTIVE_JAVA_NAME = "InactiveJava"; //$NON-NLS-1$
	protected Class inactiveJava;

	/**
	 * An active class applying a stereotype with an attached Java constraint requiring that the class is passive
	 */
	public static final String ACTIVE_JAVA_NAME = "ActiveJava"; //$NON-NLS-1$
	protected Class activeJava;

	@Before
	public void initModelForValidationTest() throws Exception {
		initModel(PROJECT_PREFIX + MODEL_NAME, MODEL_NAME, Activator.getDefault().getBundle());

		Model model = (Model) getRootUMLModel();

		inactiveOCL = (Class) model.getPackagedElement(INACTIVE_OCL_NAME);
		Assert.assertNotNull(String.format(CAN_NOT_FIND_ELEMENT, INACTIVE_OCL_NAME, model), inactiveOCL);

		activeOCL = (Class) model.getPackagedElement(ACTIVE_OCL_NAME);
		Assert.assertNotNull(String.format(CAN_NOT_FIND_ELEMENT, ACTIVE_OCL_NAME, model), activeOCL);

		inactiveJava = (Class) model.getPackagedElement(INACTIVE_JAVA_NAME);
		Assert.assertNotNull(String.format(CAN_NOT_FIND_ELEMENT, INACTIVE_JAVA_NAME, model), inactiveJava);

		activeJava = (Class) model.getPackagedElement(ACTIVE_JAVA_NAME);
		Assert.assertNotNull(String.format(CAN_NOT_FIND_ELEMENT, ACTIVE_JAVA_NAME, model), activeJava);
		
		final EditingDomain domain = TransactionUtil.getEditingDomain(model);
		final ValidateModelCommand validateModelCommand = new ValidateModelCommand(model);
		Display.getDefault().syncExec(new Runnable() {
			@Override
			public void run() {
				domain.getCommandStack().execute(GMFtoEMFCommandWrapper.wrap(validateModelCommand));
			}
		});

		// check that the constraint exists
		ConstraintRegistry instance = ConstraintRegistry.getInstance();
		IConstraintDescriptor descriptor = instance.getDescriptor(CONSTRAINT_PLUGIN, CONSTRAINT_ID_ALWAYS_ACTIVE);
		Assert.assertNotNull(Messages.TestValidationRulesInPluginXML_ConstraintIsMissing, descriptor);

		globalDiagnostic = validateModelCommand.getDiagnostic();
	}

	/**
	 * Failing validation for IsActiveEntityRule (OCL rule, on inactive class)
	 */
	@Test
	public void validateIsActiveOCLRule_inactiveClass() throws Exception {
		// get the diagnostic and check for the given class
		List<Diagnostic> diagnostics = filterDiagnosticsByElement(globalDiagnostic.getChildren(), inactiveOCL);
		Assert.assertEquals(String.format(Messages.TestValidationRulesInPluginXML_IsActiveShouldTriggerIssue, inactiveOCL), 1, diagnostics.size());
	}

	/**
	 * Successful validation for IsActiveEntityRule (OCL rule, on active class)
	 */
	@FailingTest
	public void validateIsActiveOCLRule_activeClass() throws Exception {
		// get the diagnostic and check for the given class
		List<Diagnostic> diagnostics = filterDiagnosticsByElement(globalDiagnostic.getChildren(), activeOCL);
		Assert.assertEquals(String.format(Messages.TestValidationRulesInPluginXML_IsActiveShouldNotTriggerIssue, activeOCL), 0, diagnostics.size());
	}

	/**
	 * Successful validation for IsPassiveEntityRule (Java rule, on inactive class)
	 */
	@Test
	public void validateIsPassiveJavaRule_inactiveClass() throws Exception {
		// get the diagnostic and check for the given class
		List<Diagnostic> diagnostics = filterDiagnosticsByElement(globalDiagnostic.getChildren(), inactiveJava);
		Assert.assertEquals(String.format(Messages.TestValidationRulesInPluginXML_IsPassiveShouldTriggerIssue, inactiveJava), 0, diagnostics.size());
	}
	
	/**
	 * Failing validation for IsPassiveEntityRule (Java rule, on active class)
	 */
	@Test
	public void validateIsPassiveJavaRule_activeClass() throws Exception {
		// get the diagnostic and check for the given class
		List<Diagnostic> diagnostics = filterDiagnosticsByElement(globalDiagnostic.getChildren(), activeJava);
		Assert.assertEquals(String.format(Messages.TestValidationRulesInPluginXML_IsPassiveShouldNotTriggerIssue, activeJava), 1, diagnostics.size());
	}
}

Back to the top