Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1b777dfb485a4aa55b42ae5199484bd8f6712bc6 (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
/*******************************************************************************
 * <copyright>
 *
 * Copyright (c) 2005, 2010 SAP AG.
 * 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:
 *    Kiril Mitov - initial API, implementation and documentation
 *
 * </copyright>
 *
 *******************************************************************************/
package org.eclipse.jpt.jpadiagrameditor.ui.tests.internal.feature;

import static org.easymock.EasyMock.aryEq;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.isA;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;

import java.util.LinkedList;
import java.util.List;

import org.easymock.EasyMock;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.BasicInternalEList;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.graphiti.dt.IDiagramTypeProvider;
import org.eclipse.graphiti.features.IAddBendpointFeature;
import org.eclipse.graphiti.features.IAddFeature;
import org.eclipse.graphiti.features.context.IAddBendpointContext;
import org.eclipse.graphiti.features.context.IAddConnectionContext;
import org.eclipse.graphiti.features.context.IAddContext;
import org.eclipse.graphiti.features.context.ILayoutContext;
import org.eclipse.graphiti.mm.algorithms.Polyline;
import org.eclipse.graphiti.mm.pictograms.Anchor;
import org.eclipse.graphiti.mm.pictograms.Connection;
import org.eclipse.graphiti.mm.pictograms.ConnectionDecorator;
import org.eclipse.graphiti.mm.pictograms.Diagram;
import org.eclipse.graphiti.mm.pictograms.FreeFormConnection;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.graphiti.services.IGaService;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.AddRelationFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.provider.IJPAEditorFeatureProvider;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.provider.IJPAEditorImageCreator;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.relations.IRelation;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.relations.IRelation.RelDir;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.relations.IRelation.RelType;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.IJPAEditorUtil;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.IPeServiceUtil;
import org.eclipse.swt.graphics.Point;
import org.junit.Before;
import org.junit.Test;

public class AddRelationFeatureTest {

	private IJPAEditorFeatureProvider featureProvider;

	private IJPAEditorImageCreator imageCreator;
	
	private IJPAEditorUtil jpaEditorUtil;

	private IDiagramTypeProvider diagramProvider;

	private Diagram diagram;

	private IAddConnectionContext context;

	private IPeServiceUtil peUtil;
	
	private IGaService gaUtil;
	
	private IAddBendpointFeature ft;
	
	private Resource resource;
	
	private static String OWNER_AT_NAME = "ownerAttr";		//$NON-NLS-1$
	
	private static String INVERSE_AT_NAME = "inverseAttr";	//$NON-NLS-1$
	
	/**
	 * @throws java.lang.Exception
	 */
	@SuppressWarnings("unchecked")
	@Before
	public void setUp() throws Exception {
		featureProvider = EasyMock.createMock(IJPAEditorFeatureProvider.class);
		imageCreator = EasyMock.createMock(IJPAEditorImageCreator.class);
		jpaEditorUtil = EasyMock.createMock(IJPAEditorUtil.class);
		diagramProvider = EasyMock.createMock(IDiagramTypeProvider.class);
		diagram = EasyMock.createMock(Diagram.class);
		context = EasyMock.createMock(IAddConnectionContext.class);
		peUtil = EasyMock.createMock(IPeServiceUtil.class);
		gaUtil = EasyMock.createMock(IGaService.class);
		ft = EasyMock.createMock(IAddBendpointFeature.class);
		
		
		TransactionalEditingDomain defaultTransEditDomain = EasyMock.createMock(TransactionalEditingDomain.class);
		ResourceSet resourceSet = EasyMock.createMock(ResourceSet.class); 
		expect(defaultTransEditDomain.getResourceSet()).andStubReturn(resourceSet);
		EList<Adapter> eAdapters = EasyMock.createMock(EList.class);
		expect(resourceSet.eAdapters()).andStubReturn(eAdapters);
		//IFile diagramFile = project.getFile(diagramFileName.removeFirstSegments(1));
		//IPath diagramFilePath = diagramFile.getFullPath();

		//String pathName = diagramFilePath.toString();
		//URI resourceURI = URI.createPlatformResourceURI(pathName, true);
		resource = EasyMock.createMock(Resource.class); 
		expect(resourceSet.getResource(isA(URI.class), EasyMock.anyBoolean())).andStubReturn(resource);
		
		expect(resource.getResourceSet()).andStubReturn(resourceSet);
		replay(defaultTransEditDomain, resourceSet, resource);
		
	}

	@Test
	public void testCanAddNotAddConnectionContext() {
		IAddContext context = EasyMock.createMock(IAddContext.class);
		verifyCanAdd(context, false);
	}

	@Test
	public void testCanAddNoRelation() throws Exception {
		IAddConnectionContext context = EasyMock.createMock(IAddConnectionContext.class);
		Object notARelation = new Object();
		expect(context.getNewObject()).andReturn(notARelation);
		verifyCanAdd(context, false);
	}

	@Test
	public void testCanAdd() throws Exception {
		IAddConnectionContext context = EasyMock.createMock(IAddConnectionContext.class);
		IRelation relation = EasyMock.createMock(IRelation.class);
		replay(relation);
		expect(context.getNewObject()).andReturn(relation);
		verifyCanAdd(context, true);
	}

	@Test
	public void testGetFeatureProvider() {
		IAddFeature fixture = createFeature();
		assertSame(featureProvider, fixture.getFeatureProvider());
	}

	@SuppressWarnings("unused")
	private void expectManyEndDecorator(double location) {
		ConnectionDecorator d = EasyMock.createMock(ConnectionDecorator.class);
		Polyline pl = EasyMock.createMock(Polyline.class);
		expect(pl.getX()).andReturn(0);
		expect(pl.getY()).andReturn(0);		
		expect(imageCreator.createManyEndDecorator(isA(Connection.class), eq(location))).andReturn(d);
		expect(d.getGraphicsAlgorithm()).andReturn(pl);
		replay(d, pl);		
	}
	
	@SuppressWarnings("unused")
	private void expectManyStartDecorator(double location) {
		ConnectionDecorator d = EasyMock.createMock(ConnectionDecorator.class);
		Polyline pl = EasyMock.createMock(Polyline.class);
		expect(pl.getX()).andReturn(0);
		expect(pl.getY()).andReturn(0);		
		expect(imageCreator.createManyStartDecorator(isA(Connection.class), eq(location))).andReturn(d);
		expect(d.getGraphicsAlgorithm()).andReturn(pl);
		replay(d, pl);		
	}	

	@SuppressWarnings("unused")
	private void expectManyEndWithArrow(final double location) {
		ConnectionDecorator d = EasyMock.createMock(ConnectionDecorator.class);
		Polyline pl = EasyMock.createMock(Polyline.class);
		expect(pl.getX()).andReturn(0);
		expect(pl.getY()).andReturn(0);		
		expect(imageCreator.createManyEndWithArrowDecorator(isA(Connection.class), eq(location))).andReturn(d);
		expect(d.getGraphicsAlgorithm()).andReturn(pl);
		replay(d, pl);		
	}

	@SuppressWarnings("unused")
	private void expectArrow(final double location) {
		ConnectionDecorator d = EasyMock.createMock(ConnectionDecorator.class);
		Polyline pl = EasyMock.createMock(Polyline.class);
		expect(pl.getX()).andReturn(0);
		expect(pl.getY()).andReturn(0);
		expect(imageCreator.createArrowConnectionDecorator(isA(Connection.class), eq(location))).andReturn(d);
		expect(d.getGraphicsAlgorithm()).andReturn(pl);
		replay(d, pl);
	}

	@SuppressWarnings("unused")
	private void expectTextConnectionDecorator(String text, double location) {
		expect(imageCreator.createCardinalityConnectionDecorator(isA(Connection.class), eq(text), eq(location))).andReturn(
				null);
	}

	@SuppressWarnings("unused")
	private void configureRelation(IRelation relation, final RelDir direction, final RelType type) {
		expect(relation.getOwnerAttributeName()).andStubReturn(OWNER_AT_NAME);
		expect(relation.getInverseAttributeName()).andStubReturn(INVERSE_AT_NAME);
		expect(relation.getRelDir()).andStubReturn(direction);
		expect(relation.getRelType()).andStubReturn(type);
	}

	private void configureProvidersForAdd(IRelation relation) {
		expect(diagramProvider.getDiagram()).andReturn(diagram);
		expect(featureProvider.getDiagramTypeProvider()).andReturn(diagramProvider);
		expect(featureProvider.getPeUtil()).andReturn(peUtil);
		expect(featureProvider.getAddBendpointFeature(isA(IAddBendpointContext.class))).andStubReturn(ft);
		expect(context.getNewObject()).andReturn(relation);

	}

	@SuppressWarnings("unused")
	private void verifyAdd(IRelation relation, boolean expectId) {
		FreeFormConnection connection = configureConnection(relation, expectId);
		replay(connection);
		PictogramElement result = callAdd();
		assertSame(connection, result);
		verify(context);
		verify(featureProvider);
		verify(imageCreator);
		verify(jpaEditorUtil);
		verify(diagram);
		verify(diagramProvider);
		verify(relation);
	}

	private PictogramElement callAdd() {
		IAddFeature fixture = createFeature();
		PictogramElement result = fixture.add(context);
		return result;
	}

	private FreeFormConnection configureConnection(IRelation relation, boolean expectId) {
		final String relationId = "someId"; //$NON-NLS-1$
		if (expectId)
			expect(relation.getId()).andStubReturn(relationId);
		replay(relation);
		configureProvidersForAdd(relation);
		Anchor startAnchor = replayAnchor();
		Anchor endAnchor = replayAnchor();
		expect(context.getSourceAnchor()).andReturn(startAnchor);
		expect(context.getTargetAnchor()).andReturn(endAnchor);
		
		gaUtil.setLocation(isA(Polyline.class), EasyMock.anyInt() , EasyMock.anyInt());
		gaUtil.setLocation(isA(Polyline.class), EasyMock.anyInt() , EasyMock.anyInt());

		FreeFormConnection connection = createConnection(startAnchor, endAnchor);
		expect(peUtil.createFreeFormConnection(diagram)).andReturn(connection);
		expect(imageCreator.createConnectionLine(diagram, connection)).andReturn(null);
		ft.addBendpoint(isA(IAddBendpointContext.class));
		
		List<Point> pts = new LinkedList<Point>();
		Point pt1 = new Point(100, 100);
		//Point pt2 = new Point(200, 200);
		pts.add(pt1); 
		//pts.add(pt2);
		expect(jpaEditorUtil.createBendPointList(connection, false)).andReturn(pts);
		featureProvider.putKeyToBusinessObject(relationId, relation);
		featureProvider.link(eq(connection), aryEq(new IRelation[] { relation }));
		expect(featureProvider.layoutIfPossible(isA(ILayoutContext.class))).andReturn(null);
		return connection;
	}

	private Anchor replayAnchor() {
		Anchor startAnchor = EasyMock.createMock(Anchor.class);
		replay(startAnchor);
		return startAnchor;
	}

	private FreeFormConnection createConnection(Anchor startAnchor, Anchor endAnchor) {
		FreeFormConnection connection = EasyMock.createMock(FreeFormConnection.class);
		connection.setStart(startAnchor);
		connection.setEnd(endAnchor);
		connection.setVisible(true);
		connection.setActive(true);
		
		org.eclipse.graphiti.mm.algorithms.styles.Point pt = EasyMock.createMock(org.eclipse.graphiti.mm.algorithms.styles.Point.class);
		expect(pt.getX()).andStubReturn(100);
		expect(pt.getY()).andStubReturn(100);
		
		org.eclipse.graphiti.mm.algorithms.styles.Point pt1 = EasyMock.createMock(org.eclipse.graphiti.mm.algorithms.styles.Point.class);
		expect(pt1.getX()).andStubReturn(200);
		expect(pt1.getY()).andStubReturn(200);
		
		
		EList<org.eclipse.graphiti.mm.algorithms.styles.Point> pts1 = new BasicInternalEList<org.eclipse.graphiti.mm.algorithms.styles.Point>(org.eclipse.graphiti.mm.algorithms.styles.Point.class);
		pts1.add(pt);	
		pts1.add(pt1);
		expect(connection.getBendpoints()).andStubReturn(pts1);
		replay(pt, pt1);
		return connection;
	}

	private void verifyCanAdd(IAddContext context, final boolean expected) {
		replay(context);
		boolean result = callCanAdd(context);
		assertEquals(expected, result);
		verify(context);
	}

	private boolean callCanAdd(IAddContext context) {
		IAddFeature fixture = createFeature();
		return fixture.canAdd(context);
	}

	private IAddFeature createFeature() {
		replay(featureProvider);
		replay(imageCreator);
		replay(jpaEditorUtil);
		replay(diagramProvider);
		replay(diagram);
		replay(context);
		replay(peUtil);
		replay(gaUtil);
		replay(ft);
		return new AddRelationFeature(featureProvider, imageCreator, jpaEditorUtil);
	}

}

Back to the top