Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: aba4c027e98a43ba39236a4833048225c2077fdb (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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*****************************************************************************
 * Copyright (c) 2016 CEA LIST and others.
 * 
 * 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:
 *   Nicolas FAUVERGUE (ALL4TEC) nicolas.fauvergue@all4tec.net - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.uml.nattable.clazz.config.tests.bugs;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.papyrus.infra.core.sashwindows.di.service.IPageManager;
import org.eclipse.papyrus.infra.nattable.common.editor.NatTableEditor;
import org.eclipse.papyrus.infra.nattable.manager.axis.IAxisManager;
import org.eclipse.papyrus.infra.nattable.manager.table.INattableModelManager;
import org.eclipse.papyrus.infra.nattable.manager.table.TreeNattableModelManager;
import org.eclipse.papyrus.infra.nattable.model.nattable.Table;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxis.IAxis;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxisconfiguration.TreeFillingConfiguration;
import org.eclipse.papyrus.infra.nattable.tree.CollapseAndExpandActionsEnum;
import org.eclipse.papyrus.junit.framework.classification.tests.AbstractPapyrusTest;
import org.eclipse.papyrus.junit.utils.TableUtils;
import org.eclipse.papyrus.junit.utils.rules.PapyrusEditorFixture;
import org.eclipse.papyrus.junit.utils.rules.PluginResource;
import org.eclipse.ui.IEditorPart;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.UMLPackage;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

/**
 * This class allows to test the delete row elements action.
 */
@PluginResource("resources/bugs/bug487496/DeleteRowElement.di") //$NON-NLS-1$
public class DeleteRowElementTest extends AbstractPapyrusTest {

	/**
	 * The class name in the model.
	 */
	private static final String CLASS_NAME = "Class1"; //$NON-NLS-1$

	/**
	 * The attribute name in the model.
	 */
	private static final String ATTRIBUTE_NAME = "Attribute1"; //$NON-NLS-1$

	/**
	 * The operation name in the model.
	 */
	private static final String OPERATION_NAME = "Operation1"; //$NON-NLS-1$

	/**
	 * The parameter name in the model.
	 */
	private static final String PARAMETER_NAME = "Parameter1"; //$NON-NLS-1$


	/**
	 * The table name.
	 */
	private static final String TABLE_NAME = "ClassTreeTable0"; //$NON-NLS-1$

	/**
	 * The papyrus fixture.
	 */
	@Rule
	public final PapyrusEditorFixture fixture = new PapyrusEditorFixture();

	/**
	 * The current model.
	 */
	public Model model = null;

	/**
	 * The nattable model manager.
	 */
	public INattableModelManager currentManager = null;

	/**
	 * Constructor.
	 */
	public DeleteRowElementTest() {
		super();
	}

	/**
	 * Initialize.
	 */
	@Before
	public void init() {
		// Get the model
		model = (Model) fixture.getModel();
		Assert.assertNotNull("RootModel is null", model); //$NON-NLS-1$

		// Get the table and open it
		final Table mainTable = TableUtils.getNotationFirstTable(fixture.getModelSet(), TABLE_NAME);
		fixture.getPageManager().openPage(mainTable);
		fixture.flushDisplayEvents();

		// Get the table and its manager
		final IPageManager pageManager = fixture.getPageManager();
		final List<Object> pages = pageManager.allPages();
		pageManager.openPage(pages.get(0));
		final IEditorPart part = fixture.getEditor().getActiveEditor();
		Assert.assertTrue(part instanceof NatTableEditor);
		final NatTableEditor editor = (NatTableEditor) part;
		currentManager = (INattableModelManager) editor.getAdapter(INattableModelManager.class);
		Assert.assertTrue("The table manage must be a tree table manager", currentManager instanceof TreeNattableModelManager); //$NON-NLS-1$

		((TreeNattableModelManager) currentManager).doCollapseExpandAction(CollapseAndExpandActionsEnum.EXPAND_ALL, null);
		fixture.flushDisplayEvents();
	}

	/**
	 * This allows to test the deletion of the class row element.
	 * 
	 * @throws Exception
	 *             The caught exception.
	 */
	@Test
	public void testClassDestroyRowElement() throws Exception {
		checkInitialContent();

		// Get the editing domain
		final TransactionalEditingDomain editingDomain = fixture.getEditingDomain();
		Assert.assertNotNull("The editing domain cannot be null", editingDomain); //$NON-NLS-1$

		// Destroy the row element
		final IAxisManager axisManager = currentManager.getRowAxisManager();
		Assert.assertNotNull(axisManager);

		// The list of indexes to delete
		final List<Integer> indexes = new ArrayList<Integer>();

		// Test that this is not possible to destroy row element on tree filling
		indexes.add(0);
		axisManager.destroyAxisElement(indexes);

		Assert.assertNull("The command stack must be null because the destroy of tree filling musn't be possible", editingDomain.getCommandStack().getMostRecentCommand()); //$NON-NLS-1$

		// Destroy class
		indexes.clear();
		indexes.add(1);
		axisManager.destroyAxisElement(indexes);
		fixture.flushDisplayEvents();
		checkWithoutClass();

		undo(editingDomain);
		checkInitialContent();

		redo(editingDomain);
		checkWithoutClass();
	}

	/**
	 * This allows to test the deletion of the attribute row element.
	 * 
	 * @throws Exception
	 *             The caught exception.
	 */
	@Test
	public void testAttributeDestroyRowElement() throws Exception {
		checkInitialContent();

		// Get the editing domain
		final TransactionalEditingDomain editingDomain = fixture.getEditingDomain();
		Assert.assertNotNull("The editing domain cannot be null", editingDomain); //$NON-NLS-1$

		// Destroy the row element
		final IAxisManager axisManager = currentManager.getRowAxisManager();
		Assert.assertNotNull(axisManager);

		// The list of indexes to delete
		final List<Integer> indexes = new ArrayList<Integer>();

		// Test that this is not possible to destroy row element on tree filling
		indexes.add(2);
		axisManager.destroyAxisElement(indexes);

		Assert.assertNull("The command stack must be null because the destroy of tree filling musn't be possible", editingDomain.getCommandStack().getMostRecentCommand()); //$NON-NLS-1$

		// Destroy attribute
		indexes.clear();
		indexes.add(3);
		axisManager.destroyAxisElement(indexes);
		fixture.flushDisplayEvents();
		checkWithoutAttribute();

		undo(editingDomain);
		checkInitialContent();

		redo(editingDomain);
		checkWithoutAttribute();
	}

	/**
	 * This allows to test the deletion of the operation row element.
	 * 
	 * @throws Exception
	 *             The caught exception.
	 */
	@Test
	public void testOperationDestroyRowElement() throws Exception {
		checkInitialContent();

		// Get the editing domain
		final TransactionalEditingDomain editingDomain = fixture.getEditingDomain();
		Assert.assertNotNull("The editing domain cannot be null", editingDomain); //$NON-NLS-1$

		// Destroy the row element
		final IAxisManager axisManager = currentManager.getRowAxisManager();
		Assert.assertNotNull(axisManager);

		// The list of indexes to delete
		final List<Integer> indexes = new ArrayList<Integer>();

		// Test that this is not possible to destroy row element on tree filling
		indexes.add(4);
		axisManager.destroyAxisElement(indexes);

		Assert.assertNull("The command stack must be null because the destroy of tree filling musn't be possible", editingDomain.getCommandStack().getMostRecentCommand()); //$NON-NLS-1$

		// Destroy operation
		indexes.clear();
		indexes.add(5);
		axisManager.destroyAxisElement(indexes);
		fixture.flushDisplayEvents();
		checkWithoutOperation();

		undo(editingDomain);
		checkInitialContent();

		redo(editingDomain);
		checkWithoutOperation();
	}

	/**
	 * This allows to test the parameter of the class row element.
	 * 
	 * @throws Exception
	 *             The caught exception.
	 */
	@Test
	public void testParameterDestroyRowElement() throws Exception {
		checkInitialContent();

		// Get the editing domain
		final TransactionalEditingDomain editingDomain = fixture.getEditingDomain();
		Assert.assertNotNull("The editing domain cannot be null", editingDomain); //$NON-NLS-1$

		// Destroy the row element
		final IAxisManager axisManager = currentManager.getRowAxisManager();
		Assert.assertNotNull(axisManager);

		// The list of indexes to delete
		final List<Integer> indexes = new ArrayList<Integer>();

		// Test that this is not possible to destroy row element on tree filling
		indexes.add(6);
		axisManager.destroyAxisElement(indexes);

		Assert.assertNull("The command stack must be null because the destroy of tree filling musn't be possible", editingDomain.getCommandStack().getMostRecentCommand()); //$NON-NLS-1$

		// Destroy parameter
		indexes.clear();
		indexes.add(7);
		axisManager.destroyAxisElement(indexes);
		fixture.flushDisplayEvents();
		checkWithoutParameter();

		undo(editingDomain);
		checkInitialContent();

		redo(editingDomain);
		checkWithoutParameter();
	}

	/**
	 * This allows to undo the previous command.
	 * 
	 * @param editingDomain
	 *            The current editing domain.
	 * @throws Exception
	 *             The caught exception.
	 */
	protected void undo(final TransactionalEditingDomain editingDomain) throws Exception {
		editingDomain.getCommandStack().undo();
		((TreeNattableModelManager) currentManager).doCollapseExpandAction(CollapseAndExpandActionsEnum.EXPAND_ALL, null);
		fixture.flushDisplayEvents();
	}

	/**
	 * This allows to redo the previous command.
	 * 
	 * @param editingDomain
	 *            The current editing domain.
	 * @throws Exception
	 *             The caught exception.
	 */
	protected void redo(final TransactionalEditingDomain editingDomain) throws Exception {
		editingDomain.getCommandStack().redo();
		((TreeNattableModelManager) currentManager).doCollapseExpandAction(CollapseAndExpandActionsEnum.EXPAND_ALL, null);
		fixture.flushDisplayEvents();
	}

	/**
	 * This allows to test the initial content of the table (size and row elements).
	 * 
	 * @throws Exception
	 *             The caught exception.
	 */
	protected void checkInitialContent() throws Exception {
		final List<Object> rowElements = currentManager.getRowElementsList();
		Assert.assertEquals("The table content size is not the correct one", 8, rowElements.size()); //$NON-NLS-1$

		checkTableContent(true, true, true);
	}

	/**
	 * This allows to test the content of the table (size and row elements) when the class is removed.
	 * 
	 * @throws Exception
	 *             The caught exception.
	 */
	protected void checkWithoutClass() throws Exception {
		final List<Object> rowElements = currentManager.getRowElementsList();
		Assert.assertEquals("The table content size is not the correct one", 0, rowElements.size()); //$NON-NLS-1$
	}

	/**
	 * This allows to test the content of the table (size and row elements) when the attribute is removed.
	 * 
	 * @throws Exception
	 *             The caught exception.
	 */
	protected void checkWithoutAttribute() throws Exception {
		final List<Object> rowElements = currentManager.getRowElementsList();
		Assert.assertEquals("The table content size is not the correct one", 6, rowElements.size()); //$NON-NLS-1$

		checkTableContent(false, true, true);
	}

	/**
	 * This allows to test the content of the table (size and row elements) when the operation is removed.
	 * 
	 * @throws Exception
	 *             The caught exception.
	 */
	protected void checkWithoutOperation() throws Exception {
		final List<Object> rowElements = currentManager.getRowElementsList();
		Assert.assertEquals("The table content size is not the correct one", 4, rowElements.size()); //$NON-NLS-1$

		checkTableContent(true, false, false);
	}

	/**
	 * This allows to test the content of the table (size and row elements) when the parameter is removed.
	 * 
	 * @throws Exception
	 *             The caught exception.
	 */
	protected void checkWithoutParameter() throws Exception {
		final List<Object> rowElements = currentManager.getRowElementsList();
		Assert.assertEquals("The table content size is not the correct one", 6, rowElements.size()); //$NON-NLS-1$

		checkTableContent(true, true, false);
	}

	/**
	 * This allows to check the table content with/without attribute, operation and parameter.
	 * 
	 * @param hasAttribute
	 *            Boolean to determinate if the attribute must be available.
	 * @param hasOperation
	 *            Boolean to determinate if the operation must be available.
	 * @param hasParameter
	 *            Boolean to determinate if the parameter must be available.
	 * @throws Exception
	 *             The caught exception.
	 */
	protected void checkTableContent(final boolean hasAttribute, final boolean hasOperation, final boolean hasParameter) throws Exception {
		int index = 0;

		final List<Object> rowElements = currentManager.getRowElementsList();

		final IAxis firstRow = (IAxis) rowElements.get(index++);
		final Object firstRowRepresentedElement = firstRow.getElement();
		Assert.assertTrue("The first row must be a tree filling configuration", firstRowRepresentedElement instanceof TreeFillingConfiguration); //$NON-NLS-1$

		final IAxis secondRow = (IAxis) rowElements.get(index++);
		final Object secondRowRepresentedElement = secondRow.getElement();
		Assert.assertEquals("The first element must be a class", UMLPackage.eINSTANCE.getClass_(), ((EObject) secondRowRepresentedElement).eClass()); //$NON-NLS-1$
		Assert.assertEquals("The name of the first element is not corresponding to the class", CLASS_NAME, ((org.eclipse.uml2.uml.Class) secondRowRepresentedElement).getName()); //$NON-NLS-1$

		if (hasAttribute) {
			final IAxis thirdRow = (IAxis) rowElements.get(index++);
			final Object thirdRowRepresentedElement = thirdRow.getElement();
			Assert.assertTrue("The third row must be a tree filling configuration", thirdRowRepresentedElement instanceof TreeFillingConfiguration); //$NON-NLS-1$

			final IAxis fourthRow = (IAxis) rowElements.get(index++);
			final Object fourthRowRepresentedElement = fourthRow.getElement();
			Assert.assertEquals("The second element must be a class", UMLPackage.eINSTANCE.getProperty(), ((EObject) fourthRowRepresentedElement).eClass()); //$NON-NLS-1$
			Assert.assertEquals("The name of the second element is not corresponding to the attribute", ATTRIBUTE_NAME, ((org.eclipse.uml2.uml.Property) fourthRowRepresentedElement).getName()); //$NON-NLS-1$
		}

		if (hasOperation) {
			final IAxis fifthRow = (IAxis) rowElements.get(index++);
			final Object fifthRowRepresentedElement = fifthRow.getElement();
			Assert.assertTrue("The fifth row must be a tree filling configuration", fifthRowRepresentedElement instanceof TreeFillingConfiguration); //$NON-NLS-1$

			final IAxis sixthRow = (IAxis) rowElements.get(index++);
			final Object sixthRowRepresentedElement = sixthRow.getElement();
			Assert.assertEquals("The third element must be a package", UMLPackage.eINSTANCE.getOperation(), ((EObject) sixthRowRepresentedElement).eClass()); //$NON-NLS-1$
			Assert.assertEquals("The name of the third element is not corresponding to the operation", OPERATION_NAME, ((org.eclipse.uml2.uml.Operation) sixthRowRepresentedElement).getName()); //$NON-NLS-1$

			if (hasParameter) {
				final IAxis seventhRow = (IAxis) rowElements.get(index++);
				final Object seventhRowRepresentedElement = seventhRow.getElement();
				Assert.assertTrue("The seventh row must be a tree filling configuration", seventhRowRepresentedElement instanceof TreeFillingConfiguration); //$NON-NLS-1$

				final IAxis eighthRow = (IAxis) rowElements.get(index++);
				final Object eighthRowRepresentedElement = eighthRow.getElement();
				Assert.assertEquals("The fourth element must be a package", UMLPackage.eINSTANCE.getParameter(), ((EObject) eighthRowRepresentedElement).eClass()); //$NON-NLS-1$
				Assert.assertEquals("The name of the fourth element is not corresponding to the parameter", PARAMETER_NAME, ((org.eclipse.uml2.uml.Parameter) eighthRowRepresentedElement).getName()); //$NON-NLS-1$
			}
		}
	}
}

Back to the top