Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 982e983abe67ba0f3c958c5591d1c4dee436190a (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
/*****************************************************************************
 * 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.uml.textedit.parameter.tests.suites;


import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.papyrus.junit.utils.rules.PluginResource;
import org.eclipse.papyrus.uml.textedit.parameter.xtext.ui.contributions.ParameterXtextDirectEditorConfiguration;
import org.eclipse.papyrus.uml.textedit.tests.AbstractGrammarTest;
import org.eclipse.papyrus.uml.xtext.integration.DefaultXtextDirectEditorConfiguration;
import org.eclipse.uml2.uml.Component;
import org.eclipse.uml2.uml.DataType;
import org.eclipse.uml2.uml.LiteralReal;
import org.eclipse.uml2.uml.LiteralString;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.Operation;
import org.eclipse.uml2.uml.Parameter;
import org.eclipse.uml2.uml.ParameterDirectionKind;
import org.eclipse.uml2.uml.ParameterEffectKind;
import org.eclipse.uml2.uml.PrimitiveType;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.UMLFactory;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.VisibilityKind;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

@PluginResource("/model/xtextParameterModel.uml")
public class ParameterGrammarTests extends AbstractGrammarTest<Parameter> {

	protected PrimitiveType type2; /* QName = model::Package1::type2 */

	protected DataType type1; /* QName = model::type1 */

	protected Model rootModel;

	protected Parameter testedParameter;

	@Before
	public void loadTestModel() {
		type2 = findElement(PrimitiveType.class, "type2");
		type1 = findElement(DataType.class, "type1");
		rootModel = findElement(Model.class, "model");

		Component component = (Component) rootModel.createPackagedElement("Component1", UMLPackage.eINSTANCE.getComponent());
		Operation operation = component.createOwnedOperation("op1", new BasicEList<String>(), new BasicEList<Type>());
		testedParameter = operation.createOwnedParameter("p1", null);
	}

	@Test
	public void testParser() throws Exception {
		tester.parseText(testedParameter, "~in p1: <Undefined>");
		Assert.assertEquals(ParameterDirectionKind.IN_LITERAL, testedParameter.getDirection());
		Assert.assertEquals(VisibilityKind.PACKAGE_LITERAL, testedParameter.getVisibility());

		tester.parseText(testedParameter, "p2");
		Assert.assertEquals(ParameterDirectionKind.IN_LITERAL, testedParameter.getDirection()); // Direction is optional; should not change
		Assert.assertEquals(VisibilityKind.PACKAGE_LITERAL, testedParameter.getVisibility()); // Visibility is optional; should not change
		Assert.assertEquals("p2", testedParameter.getName());

		tester.parseText(testedParameter, "p2: model::type1");
		Assert.assertTrue(testedParameter.getType() == type1);

		tester.parseText(testedParameter, "p2 {exception, ordered, stream, unique}");
		Assert.assertTrue(testedParameter.isException());
		Assert.assertTrue(testedParameter.isOrdered());
		Assert.assertTrue(testedParameter.isStream());
		Assert.assertTrue(testedParameter.isUnique());

		tester.parseText(testedParameter, "p2 {}");
		Assert.assertFalse(testedParameter.isException());
		Assert.assertFalse(testedParameter.isOrdered());
		Assert.assertFalse(testedParameter.isStream());
		Assert.assertFalse(testedParameter.isUnique());

		tester.parseText(testedParameter, "p2 = \"Hello\"");
		Assert.assertNotNull(testedParameter.getDefaultValue());
		Assert.assertTrue(testedParameter.getDefaultValue() instanceof LiteralString);
		Assert.assertEquals("Hello", ((LiteralString) testedParameter.getDefaultValue()).getValue());
	}

	@Test
	public void testInitialText() {
		Assert.assertEquals("+ in p1 : <Undefined> {unique}{effect: create}", tester.getInitialText(testedParameter));

		testedParameter.setIsUnique(false);
		Assert.assertEquals("+ in p1 : <Undefined> {effect: create}", tester.getInitialText(testedParameter));

		testedParameter.setIsStream(true);
		testedParameter.setIsOrdered(true);
		testedParameter.setVisibility(VisibilityKind.PROTECTED_LITERAL);
		testedParameter.setType(type1);
		testedParameter.setEffect(ParameterEffectKind.DELETE_LITERAL);
		testedParameter.setDirection(ParameterDirectionKind.RETURN_LITERAL);
		Assert.assertEquals("# return p1 : model::type1 {ordered, stream}{effect: delete}", tester.getInitialText(testedParameter));
	}

	@Test
	public void testDefaultValues() throws Exception {
		testedParameter.setIsUnique(false);

		LiteralReal defaultRealValue = UMLFactory.eINSTANCE.createLiteralReal();
		defaultRealValue.setValue(123.54);
		testedParameter.setDefaultValue(defaultRealValue);

		Assert.assertEquals("+ in p1 : <Undefined> {effect: create} = 123.54", tester.getInitialText(testedParameter));
		tester.parseText(testedParameter, "p1 = .2");
		Assert.assertEquals("The instance of ValueSpecification should not change when compatible types are used", defaultRealValue, testedParameter.getDefaultValue());
		Assert.assertEquals(.2, defaultRealValue.getValue(), 0.001);
	}

	@Override
	public DefaultXtextDirectEditorConfiguration getEditor() {
		return new ParameterXtextDirectEditorConfiguration();
	}
}

Back to the top