Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b6484a2b10d31a5ac84fa2774fc44eb9c9981040 (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
/*******************************************************************************
 * <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:
 *    Stefan Dimov - initial API, implementation and documentation
 *
 * </copyright>
 *
 *******************************************************************************/
package org.eclipse.jpt.jpadiagrameditor.ui.tests.internal.editor;

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.assertNotNull;
import org.easymock.EasyMock;
import org.eclipse.gef.EditPart;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jpt.jpa.core.JpaStructureNode;
import org.eclipse.jpt.jpa.ui.selection.JpaSelectionManager;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.JPADiagramEditor;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.JPADiagramEditorPlugin;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.provider.IJPAEditorFeatureProvider;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.IWorkbenchWindow;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleListener;

@SuppressWarnings("restriction")
public class EditorTest {

	@Test
	public void testJPADiagramEditorCreation() {
		JPADiagramEditor ed = new JPADiagramEditor();
		assertNotNull(ed);
	}
	
	
	@Test
	public void testJPADiagramEditorPlugin() {
		JPADiagramEditorPlugin plugin = new JPADiagramEditorPlugin();
		BundleContext bc = EasyMock.createMock(BundleContext.class);
		Bundle bundle = EasyMock.createMock(Bundle.class);
		EasyMock.expect(bundle.getSymbolicName()).andStubReturn("");
		EasyMock.expect(bc.getBundle()).andStubReturn(bundle);
		bc.addBundleListener(EasyMock.isA(BundleListener.class));
		bc.removeBundleListener(EasyMock.isA(BundleListener.class));
		EasyMock.replay(bc, bundle);
		try {
			plugin.start(bc);
			plugin.stop(bc);			
		} catch (Exception e) {
			e.printStackTrace();
		}
		EasyMock.verify(bc);
	}	
	
	@Test
	public void testSelectionChanged() {
		final IJPAEditorFeatureProvider provider = EasyMock
				.createMock(IJPAEditorFeatureProvider.class);

		IWorkbenchPart part = replayPart();

		PictogramElement pe = EasyMock.createMock(PictogramElement.class);
		replay(pe);
		EditPart editPart = EasyMock.createMock(EditPart.class);
		expect(editPart.getModel()).andReturn(pe);
		replay(editPart);

		JpaStructureNode jpaNodeToBeSelected = EasyMock
				.createMock(JpaStructureNode.class);
		expect(provider.getBusinessObjectForPictogramElement(pe)).andReturn(
				jpaNodeToBeSelected);
		replay(provider);

		// test
		JpaSelectionManager manager = EasyMock
				.createMock(JpaSelectionManager.class);
		manager.setSelection(isA(JpaStructureNode.class));
		replay(manager);

		JPADiagramEditor.ISelectionManagerFactory factory = EasyMock
				.createMock(JPADiagramEditor.ISelectionManagerFactory.class);
		expect(factory.getSelectionManager(isA(IWorkbenchWindow.class)))
				.andReturn(manager);
		replay(factory);
		JPADiagramEditor editor = createEditor(provider, factory);
		// test
		editor.selectionChanged(part, new StructuredSelection(editPart));
		verify(manager);
	}

	private JPADiagramEditor createEditor(final IJPAEditorFeatureProvider provider,
			JPADiagramEditor.ISelectionManagerFactory factory) {
		IWorkbenchPage page = EasyMock.createMock(IWorkbenchPage.class);
		expect(page.isPartVisible(isA(IWorkbenchPart.class)))
				.andReturn(false);
		expect(page.getActiveEditor()).andStubReturn(null);
		replay(page);
		final IWorkbenchPartSite site = EasyMock
				.createMock(IWorkbenchPartSite.class);
		expect(site.getPage()).andStubReturn(page);
		replay(site);

		JPADiagramEditor editor = new JPADiagramEditor(factory) {
			@Override
			public IJPAEditorFeatureProvider getFeatureProvider() {
				return provider;
			}

			public IWorkbenchPartSite getSite() {
				return site;
			}
		};
		return editor;
	}

	private IWorkbenchPart replayPart() {
		IWorkbenchWindow window = EasyMock.createMock(IWorkbenchWindow.class);
		replay(window);
		IWorkbenchPage page = EasyMock.createMock(IWorkbenchPage.class);
		IWorkbenchPartSite site = EasyMock.createMock(IWorkbenchPartSite.class);
		expect(site.getWorkbenchWindow()).andReturn(window);
		expect(site.getPage()).andStubReturn(page);
		replay(site);
		IWorkbenchPart part = EasyMock.createMock(IWorkbenchPart.class);
		expect(part.getSite()).andStubReturn(site);
		expect(page.isPartVisible(isA(IWorkbenchPart.class)))
				.andReturn(false);
		expect(page.getActiveEditor()).andReturn(null);
		replay(page);
		replay(part);
		return part;
	}

	@Test
	public void testSelectionChangedTextSelection() {
		JPADiagramEditor editor = createEditor(null, null);
		editor.selectionChanged(null, new ISelection() {
			public boolean isEmpty() {
				return false;
			}
		});
	}

	@Test
	public void testSelectionChangedNotEditPart() {
		JPADiagramEditor editor = createEditor(null, null);
		editor.selectionChanged(null, new StructuredSelection(new Object()));
	}

}

Back to the top