-
Notifications
You must be signed in to change notification settings - Fork 11
/
TextualScenarioTestFramework.java
303 lines (269 loc) · 11.7 KB
/
TextualScenarioTestFramework.java
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*******************************************************************************
* Copyright (c) 2020 THALES GLOBAL SERVICES.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Thales - initial API and implementation
*******************************************************************************/
package org.polarsys.capella.scenario.editor.ju.util;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.osgi.util.NLS;
import org.eclipse.sirius.business.api.session.Session;
import org.eclipse.sirius.diagram.DDiagramElement;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.polarsys.capella.test.diagram.common.ju.context.DiagramContext;
import org.polarsys.capella.test.diagram.common.ju.step.crud.OpenDiagramStep;
import org.polarsys.capella.test.framework.api.BasicTestCase;
import org.polarsys.capella.test.framework.context.SessionContext;
import org.polarsys.capella.common.data.modellingcore.AbstractType;
import org.polarsys.capella.common.helpers.EObjectLabelProviderHelper;
import org.polarsys.capella.core.data.information.AbstractInstance;
import org.polarsys.capella.core.data.interaction.InstanceRole;
import org.polarsys.capella.core.data.interaction.SequenceMessage;
import org.polarsys.capella.core.data.interaction.StateFragment;
import org.polarsys.capella.scenario.editor.embeddededitor.actions.RefreshAction;
import org.polarsys.capella.scenario.editor.embeddededitor.actions.SaveAction;
import org.polarsys.capella.scenario.editor.embeddededitor.helper.XtextEditorHelper;
import org.polarsys.capella.scenario.editor.embeddededitor.views.EmbeddedEditorView;
import org.polarsys.capella.scenario.editor.helper.EmbeddedEditorInstanceHelper;
public abstract class TextualScenarioTestFramework extends BasicTestCase {
public static final String simpleDiagramName = "[IS] CapabilityRealization 1 (Simple)";
public static final String simpleFileContent = "[IS]_CapabilityRealization_1_Simple";
protected String invalidContentErrMsg = "Invalid content in diagram {0}.\n Text expected in editor:\n{1}\nFound:\n{2}";
protected String modelName = "TextualEditor";
protected Session session;
protected SessionContext context;
protected void init() {
session = getSession(modelName);
context = new SessionContext(session);
IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
EmbeddedEditorView eeView = XtextEditorHelper.getActiveEmbeddedEditorView();
if (eeView == null) {
// Show it if not found.
try {
eeView = (EmbeddedEditorView) activePage.showView(EmbeddedEditorView.ID);
} catch (PartInitException e) {
System.err.println("Cannot open Textual Editor View");
}
activePage.activate(eeView);
}
}
/**
* test function, it checks the text editor associated to the diagram diagramName is updated and contains the correct
* data
*
* @param diagramName
* : the name of the diagram that is checked
* @param fileContentExpected
* : the path to a file that contains the content that is expected to be found also in the text editor
* associated to diagramName
* @return
*
*/
protected void testLoadDiagram(String diagramName, String fileContentExpected) {
openDiagram(diagramName);
reloadDataFromDiagram();
String expectedContent = readFile(fileContentExpected);
// check the content of the text editor
checkValidContent(diagramName, expectedContent);
// switch to a simple diagram to test that the editor is updated
DiagramContext simpleDiagram = new OpenDiagramStep(context, simpleDiagramName).run();
assertTrue(simpleDiagram != null);
String simpleExpectedContent = readFile(simpleFileContent);
// check the content of the text editor
checkValidContent(simpleDiagramName, simpleExpectedContent);
}
/**
* test function, it checks the text editor associated to the diagram diagramName is updated and contains the correct
* data and that after updating the text content and saving the data, the diagram is updated according to the text
* commands
*
* @param diagramName
* : the name of the diagram that is checked
* @param fileContent
* : the path to a file that contains the current content in the text editor associated to diagramName
* @param fileNewContent
* : the path to a file that contains the new content that is expected in the text editor after we update it
* @param expectedElements
* : a list of expected elements that shall be created in diagram after updating and saving the model
* @return
*
*/
protected void testUpdateDiagram(String diagramName, String fileContent, String fileNewContent,
List<String> expectedElements) {
DiagramContext diagram = openDiagram(diagramName);
// get the diagram elements before saving
EList<DDiagramElement> elementsBeforeUpdate = diagram.getDiagram().getDiagramElements();
String contentBeforeUpdate = readFile(fileContent);
String newContent = readFile(fileNewContent);
// update data in text editor
EmbeddedEditorInstanceHelper.updateModel(newContent);
// if not save, data is not yet updated
reloadDataFromDiagram();
checkValidContent(diagramName, contentBeforeUpdate);
// do the change again, update the text view
EmbeddedEditorInstanceHelper.updateModel(newContent);
// save data to diagram
saveDataToDiagram();
reloadDataFromDiagram();
checkValidContent(diagramName, newContent);
// get the elements in diagram and check that new ones have been inserted
List<DDiagramElement> afterUpdateElements = new ArrayList<DDiagramElement>();
afterUpdateElements.addAll(diagram.getDiagram().getDiagramElements());
afterUpdateElements.removeAll(elementsBeforeUpdate);
List<String> newElements = new ArrayList<String>();
afterUpdateElements.forEach(x -> {
EObject target = x.getTarget();
if (target instanceof InstanceRole) {
if (((InstanceRole) target).getRepresentedInstance().getAbstractType() != null) {
AbstractType type = ((InstanceRole) target).getRepresentedInstance().getAbstractType();
newElements.add(EObjectLabelProviderHelper.getMetaclassLabel(type, false) + ":"
+ EObjectLabelProviderHelper.getText(type));
} else {
AbstractInstance instance = ((InstanceRole) target).getRepresentedInstance();
newElements.add(EObjectLabelProviderHelper.getMetaclassLabel(instance, false) + ":"
+ EObjectLabelProviderHelper.getText(instance));
}
} else if (target instanceof SequenceMessage) {
SequenceMessage message = (SequenceMessage) target;
InstanceRole sourceIr = message.getSendingEnd().getCovered();
InstanceRole targetIr = message.getReceivingEnd().getCovered();
switch (message.getKind()) {
case CREATE:
newElements.add(sourceIr.getName() + " ->+ " + targetIr.getName() + " : " + message.getName());
break;
case DELETE:
newElements.add(sourceIr.getName() + " ->x " + targetIr.getName() + " : " + message.getName());
break;
case TIMER:
newElements.add("->> " + targetIr.getName() + " : " + message.getName());
break;
default:
newElements.add(sourceIr.getName() + " -> " + targetIr.getName() + " : " + message.getName());
}
} else if (target instanceof StateFragment) {
StateFragment fragment = (StateFragment) target;
InstanceRole sourceIr = fragment.getStart().getCoveredInstanceRoles().get(0);
String keyword = EmbeddedEditorInstanceHelper.getStateFragmentType(fragment);
String stateName = fragment.getRelatedAbstractFunction() == null ?
fragment.getRelatedAbstractState().getName() : fragment.getRelatedAbstractFunction().getName();
String newElement = "on " + sourceIr.getName() + " " + keyword + " " + stateName;
newElements.add(newElement);
}
});
expectedElements.removeAll(newElements);
StringBuilder sb = new StringBuilder();
for (String data : expectedElements) {
sb.append(data);
sb.append("\n");
}
assertTrue("The following elements were not created:\n" + sb, sb.length() == 0);
resetContent(diagramName, contentBeforeUpdate);
}
@Override
public List<String> getRequiredTestModels() {
return Arrays.asList(modelName);
}
public void reloadDataFromDiagram() {
RefreshAction action = new RefreshAction();
action.run();
}
public void saveDataToDiagram() {
SaveAction action = new SaveAction();
action.run();
}
/**
* reads the content of a file in the input folder; the input folder contains files with content of the text editor
* that are checked
*
* @param fileName
* : the name a file in the input folder
* @return String : the content of the file as strings
*
*/
protected String readFile(String fileName) {
String fileAsString = "";
try {
URL resolvedUrl = FileLocator.toFileURL(getClass().getResource("input/" + fileName));
InputStream is = new FileInputStream(resolvedUrl.getPath());
BufferedReader buffer = new BufferedReader(new InputStreamReader(is));
String line = buffer.readLine();
StringBuilder sb = new StringBuilder();
while (line != null) {
sb.append(line).append("\r\n");
line = buffer.readLine();
}
sb.setLength(sb.length() - 2);
fileAsString = sb.toString();
buffer.close();
} catch (IOException e) {
e.printStackTrace();
}
return fileAsString;
}
/**
* opens a diagram using the given diagram name
*
* @param diagramName
* : the name of the diagram that is checked
* @return
*
*/
protected DiagramContext openDiagram(String diagramName) {
DiagramContext diagram = new OpenDiagramStep(context, diagramName).run();
assertTrue(diagram != null);
EmbeddedEditorView textEditorView = XtextEditorHelper.getActiveEmbeddedEditorView();
assertTrue(textEditorView != null);
return diagram;
}
/**
* test function, it checks the text editor content contains the expectedContent
*
* @param diagramName
* : the name of the diagram that is checked
* @param expectedContent
* : the expected content of the text editor
* @return
*
*/
protected void checkValidContent(String diagramName, String expectedContent) {
String textEditorContent = EmbeddedEditorInstanceHelper.getModelContent();
assertTrue(NLS.bind(invalidContentErrMsg, new String[] { diagramName, expectedContent, textEditorContent }),
expectedContent.equals(textEditorContent));
}
/**
* Reset text content to an initial value (content)
*
* @param diagramName
* : the name of the diagram that is checked
* @param content
* : the initial content
* @return
*
*/
protected void resetContent(String diagramName, String content) {
// redo the changes
EmbeddedEditorInstanceHelper.updateModel(content);
saveDataToDiagram();
reloadDataFromDiagram();
checkValidContent(diagramName, content);
}
}