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
|
/*****************************************************************************
* Copyright (c) 2015 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:
* CEA LIST - Initial API and implementation
*
*****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.common.tests;
import java.util.List;
import org.eclipse.emf.common.util.URI;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequest;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequestFactory;
import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.core.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.utils.ServiceUtils;
import org.eclipse.papyrus.infra.gmfdiag.common.SynchronizableGmfDiagramEditor;
import org.eclipse.papyrus.infra.gmfdiag.common.expansion.ChildrenListRepresentation;
import org.eclipse.papyrus.infra.gmfdiag.common.expansion.DiagramExpansionSingleton;
import org.eclipse.papyrus.infra.gmfdiag.common.expansion.DiagramExpansionsRegistry;
import org.eclipse.papyrus.infra.gmfdiag.common.expansion.InducedRepresentationCreationEditPolicy;
import org.eclipse.papyrus.infra.gmfdiag.common.model.NotationModel;
import org.eclipse.papyrus.junit.utils.tests.AbstractEditorTest;
import org.junit.Assert;
import org.junit.Test;
import org.osgi.framework.Bundle;
/**
* this Test is used to test if it is possible to add compartment
* see #Test T003-Add Child Label
*
*
*/
public class ExpansionAddChildLabel extends AbstractEditorTest{
/**
*
*/
protected static final String NESTED_INTERFACE_LABEL = "Interface_Label";
public void openDiagram(IMultiDiagramEditor editor, final String name) {
try {
ModelSet modelSet = ServiceUtils.getInstance().getModelSet(editor.getServicesRegistry());
NotationModel notation = (NotationModel) modelSet.getModel(NotationModel.MODEL_ID);
Diagram diagram = notation.getDiagram(name);
ServiceUtils.getInstance().getIPageManager(editor.getServicesRegistry()).openPage(diagram);
flushDisplayEvents();
} catch (Exception e) {
throw new IllegalStateException("Cannot initialize test", e);
}
}
/**
*
*/
protected static final String CLASS_DIAGRAM_TYPE = "Class Diagram";
/**
*
*/
protected static final String CLASS_VISUALID = "2008";
/**
*
*/
protected static final String IMPLEMENTED_INTERFACES_HINT = "Implemented Interfaces";
@Test
public void load_DiagramExpansion() {
//loading
DiagramExpansionsRegistry diagramExpansionsRegistry= loadXMIExpansionModel("AddChildLabel.xmi");
Assert.assertEquals("Size ot the registry must be equals to 1",1,diagramExpansionsRegistry.getDiagramExpansions().size());
Assert.assertEquals("Size ot the map childreen must be equals to 1",1,diagramExpansionsRegistry.mapChildreen.size());
//test the data structure that is interpreted by the framework
ChildrenListRepresentation childrenListRepresentation= diagramExpansionsRegistry.mapChildreen.get(CLASS_DIAGRAM_TYPE);
System.out.println(childrenListRepresentation);
Assert.assertNotNull("A usage contex has been defined for "+CLASS_DIAGRAM_TYPE , childrenListRepresentation);
Assert.assertNotNull("The class has been redefined",childrenListRepresentation.IDMap.get(CLASS_VISUALID));
Assert.assertNotNull("The compartment of class has been added",childrenListRepresentation.IDMap.get(IMPLEMENTED_INTERFACES_HINT));
List<String> the_2008_Children=childrenListRepresentation.parentChildrenRelation.get(CLASS_VISUALID);
Assert.assertEquals("2008 can have a new compartment",1, the_2008_Children.size());
Assert.assertEquals("2008 has to contain "+IMPLEMENTED_INTERFACES_HINT,IMPLEMENTED_INTERFACES_HINT, the_2008_Children.get(0));
Assert.assertNotNull("The Nested Interface has to be added",childrenListRepresentation.IDMap.get(NESTED_INTERFACE_LABEL));
List<String> the_IMPLEMENTED_INTERFACES_Children=childrenListRepresentation.parentChildrenRelation.get(IMPLEMENTED_INTERFACES_HINT);
Assert.assertEquals("Nested Interface can have a new compartment",1, the_IMPLEMENTED_INTERFACES_Children.size());
Assert.assertEquals("Nested Interface has to contain "+NESTED_INTERFACE_LABEL,NESTED_INTERFACE_LABEL, the_IMPLEMENTED_INTERFACES_Children.get(0));
// the model is valid
//now launch a class diagram
try {
initModel("ExpansionModelProject", "ExpansionModelTest", getBundle());
openDiagram(editor, "NewDiagram");
SynchronizableGmfDiagramEditor diagramEditor = (SynchronizableGmfDiagramEditor)editor.getActiveEditor();
DiagramEditPart diagramEditPart = (DiagramEditPart)editor.getAdapter(DiagramEditPart.class);
Assert.assertNotNull("The diagram must be opened", diagramEditPart);
IGraphicalEditPart classEditPart =(IGraphicalEditPart)diagramEditPart.getChildren().get(0);
Assert.assertNotNull("A Class edit Part must exist", diagramEditPart);
//verify editpolicy
EditPolicy inducedRepresentationCreator=classEditPart.getEditPolicy(InducedRepresentationCreationEditPolicy.INDUCED_REPRESENTATION_CREATOR_EDITPOLICY);
Assert.assertNotNull("A Class must have this editpolicy", inducedRepresentationCreator);
//test in the notation
View classNotationView=classEditPart.getNotationView();
Assert.assertEquals("the Type of class editpart must be 2008", classNotationView.getType(), "2008");
Assert.assertEquals("the Type of class editpart must be 2008 must contains 2 labels and 4 compartments",classNotationView.getPersistedChildren().size(), 6);
View compartment=(View)classNotationView.getPersistedChildren().get(5);
Assert.assertEquals("The last compartment must have the type "+IMPLEMENTED_INTERFACES_HINT,IMPLEMENTED_INTERFACES_HINT , compartment.getType());
//test in the editpart is created for this notation
Assert.assertEquals("the Type of class editpart must be 2008 must contains 2 labels and 4 compartments",classEditPart.getChildren().size(), 6);
IGraphicalEditPart compartmentEdiPart=(IGraphicalEditPart)classEditPart.getChildren().get(5);
Assert.assertEquals("The last compartment must have the type "+IMPLEMENTED_INTERFACES_HINT,IMPLEMENTED_INTERFACES_HINT , compartmentEdiPart.getNotationView().getType());
//try to create a label inside this compartment.
final IElementType interfaceLabelelementType=ElementTypeRegistry.getInstance().getType("org.eclipse.papyrus.uml.diagram.testexpansion.Interface_Label");
CreateViewRequest requestcreation = CreateViewRequestFactory.getCreateShapeRequest(interfaceLabelelementType, diagramEditPart.getDiagramPreferencesHint());
Command command = compartmentEdiPart.getCommand(requestcreation);
Assert.assertNotNull("the command musbe given", command);
Assert.assertNotEquals("The command must be not equls to UnexecutableCommand", command , UnexecutableCommand.INSTANCE);
Assert.assertTrue("The command must be executable" , command.canExecute()); //$NON-NLS-1$
diagramEditor.getDiagramEditDomain().getDiagramCommandStack().execute(command);
Assert.assertTrue("The notation compartment must contain a child", compartment.getChildren().size() == 1);
EditPart createdEditPart= (EditPart)compartmentEdiPart.getChildren().get(0);
Assert.assertNotNull("the editPart child must be created", createdEditPart);
} catch (Exception e) {
e.printStackTrace();
}
}
protected DiagramExpansionsRegistry loadXMIExpansionModel(String filename) {
DiagramExpansionsRegistry diagramExpansionsRegistry= DiagramExpansionSingleton.getInstance().getDiagramExpansionRegistry();
diagramExpansionsRegistry.clear();
Assert.assertEquals("Size ot the registry must be equals to 0",0,diagramExpansionsRegistry.getDiagramExpansions().size());
Assert.assertEquals("Size ot the map childreen must be equals to 0",0,diagramExpansionsRegistry.mapChildreen.size());
URI badContextExpansion = URI.createPlatformPluginURI("org.eclipse.papyrus.infra.gmfdiag.common.tests", true);
badContextExpansion=badContextExpansion.appendSegment("models");
badContextExpansion=badContextExpansion.appendSegment(filename);
diagramExpansionsRegistry.loadExpansion(badContextExpansion);
return diagramExpansionsRegistry;
}
/**
* @see org.eclipse.papyrus.junit.utils.tests.AbstractEditorTest#getSourcePath()
*
* @return
*/
@Override
protected String getSourcePath() {
return "models/";
}
@Override
protected Bundle getBundle() {
return org.eclipse.papyrus.infra.gmfdiag.common.Activator.getInstance().getBundle();
}
}
|