Skip to content

Package: EcoreEditor_PTest

EcoreEditor_PTest

nameinstructionbranchcomplexitylinemethod
EcoreEditor_PTest()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createEcoreAndGenModelFiles()
M: 0 C: 51
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
lambda$0(InvocationOnMock)
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$1(InvocationOnMock)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$2(Map, InvocationOnMock)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$3(Map, InvocationOnMock)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
mockMarker(String, String, String[])
M: 15 C: 59
80%
M: 2 C: 2
50%
M: 1 C: 2
67%
M: 2 C: 12
86%
M: 0 C: 1
100%
setup()
M: 0 C: 144
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 24
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
tearDown()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
testNavigate(IMarker)
M: 0 C: 38
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
testNavigateEMFStyleMarker()
M: 0 C: 39
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
testNavigateGenericEditorStyleMarker()
M: 0 C: 45
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
testSave()
M: 0 C: 83
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 19
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2019 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Alexandra Buzila - initial API and implementation
13: * Christian W. Damus - bug 548592
14: ******************************************************************************/
15: package org.eclipse.emfforms.spi.editor;
16:
17: import static org.hamcrest.CoreMatchers.instanceOf;
18: import static org.hamcrest.CoreMatchers.is;
19: import static org.hamcrest.CoreMatchers.notNullValue;
20: import static org.junit.Assert.assertFalse;
21: import static org.junit.Assert.assertNotNull;
22: import static org.junit.Assert.assertThat;
23: import static org.junit.Assert.assertTrue;
24: import static org.junit.Assert.fail;
25: import static org.junit.Assume.assumeThat;
26: import static org.mockito.Matchers.any;
27: import static org.mockito.Mockito.doAnswer;
28: import static org.mockito.Mockito.doReturn;
29: import static org.mockito.Mockito.mock;
30: import static org.mockito.Mockito.verify;
31: import static org.mockito.Mockito.when;
32:
33: import java.io.IOException;
34: import java.net.URISyntaxException;
35: import java.util.HashMap;
36: import java.util.Hashtable;
37: import java.util.Map;
38:
39: import org.eclipse.core.resources.IFile;
40: import org.eclipse.core.resources.IMarker;
41: import org.eclipse.core.resources.IProject;
42: import org.eclipse.core.resources.IResource;
43: import org.eclipse.core.resources.ResourcesPlugin;
44: import org.eclipse.core.runtime.CoreException;
45: import org.eclipse.e4.core.contexts.EclipseContextFactory;
46: import org.eclipse.e4.core.contexts.IEclipseContext;
47: import org.eclipse.emf.common.util.URI;
48: import org.eclipse.emf.ecore.EClass;
49: import org.eclipse.emf.ecore.EClassifier;
50: import org.eclipse.emf.ecore.EObject;
51: import org.eclipse.emf.ecore.EPackage;
52: import org.eclipse.emf.ecore.EReference;
53: import org.eclipse.emf.ecore.EValidator;
54: import org.eclipse.emf.ecore.EcoreFactory;
55: import org.eclipse.emf.ecore.EcorePackage;
56: import org.eclipse.emf.ecore.resource.Resource;
57: import org.eclipse.emf.ecore.util.EcoreUtil;
58: import org.eclipse.emf.ecp.view.test.common.swt.spi.SWTTestUtil;
59: import org.eclipse.emf.edit.command.AddCommand;
60: import org.eclipse.emfforms.internal.editor.ecore.EcoreEditor;
61: import org.eclipse.emfforms.spi.core.services.reveal.EMFFormsRevealService;
62: import org.eclipse.emfforms.spi.core.services.view.EMFFormsViewContext;
63: import org.eclipse.emfforms.spi.core.services.view.EMFFormsViewServiceFactory;
64: import org.eclipse.emfforms.spi.core.services.view.EMFFormsViewServicePolicy;
65: import org.eclipse.emfforms.spi.core.services.view.EMFFormsViewServiceScope;
66: import org.eclipse.jface.viewers.ISelection;
67: import org.eclipse.jface.viewers.ISelectionProvider;
68: import org.eclipse.jface.viewers.IStructuredSelection;
69: import org.eclipse.swt.widgets.Shell;
70: import org.eclipse.ui.IEditorSite;
71: import org.eclipse.ui.IURIEditorInput;
72: import org.junit.After;
73: import org.junit.Before;
74: import org.junit.Test;
75: import org.junit.runner.RunWith;
76: import org.mockito.Answers;
77: import org.mockito.Mock;
78: import org.mockito.runners.MockitoJUnitRunner;
79: import org.osgi.framework.BundleContext;
80: import org.osgi.framework.FrameworkUtil;
81: import org.osgi.framework.ServiceRegistration;
82:
83: /**
84: * Tests for the {@link EcoreEditor}.
85: * This plugin is a fragment of the <b>org.eclipse.emfforms.editor</b> bundle, containing the {@link GenericEditor}
86: * class, although we are testing content from the <b>org.eclipse.emfforms.editor.ecore</b> bundle. The reason for
87: * this is that we need to call protected methods from the {@link GenericEditor}, which would not be possible otherwise.
88: *
89: * @since 1.14
90: *
91: */
92: @SuppressWarnings("restriction")
93: @RunWith(MockitoJUnitRunner.class)
94: public class EcoreEditor_PTest {
95:         private static final String EMFFORMS_EDITOR_TEST_PROJECT_NAME = "org.eclipse.emfforms.ecore.editor.test"; //$NON-NLS-1$
96:         private static final String LOCAL_TEST_DATA = "/data/"; //$NON-NLS-1$
97:         private static final String GENMODEL_FILENAME = "test.genmodel"; //$NON-NLS-1$
98:         private static final String ECORE_FILENAME = "test.ecore"; //$NON-NLS-1$
99:         private IFile ecoreFile;
100:         private EcoreEditor editor;
101:         private IFile genModelFile;
102:
103:         @Mock(answer = Answers.RETURNS_MOCKS)
104:         private IEditorSite site;
105:         private ISelectionProvider selectionProvider;
106:
107:         @Mock
108:         private EMFFormsViewServiceFactory<EMFFormsRevealService> revealServiceFactory;
109:         @Mock
110:         private EMFFormsRevealService revealService;
111:         private ServiceRegistration<?> revealServiceFactoryReg;
112:
113:         @Before
114:         public void setup() throws IOException, CoreException, URISyntaxException {
115:                 final BundleContext bundleContext = FrameworkUtil.getBundle(EcoreEditor_PTest.class).getBundleContext();
116:                 when(revealServiceFactory.getPolicy()).thenReturn(EMFFormsViewServicePolicy.IMMEDIATE);
117:                 when(revealServiceFactory.getScope()).thenReturn(EMFFormsViewServiceScope.GLOBAL);
118:                 when(revealServiceFactory.getPriority()).thenReturn(Double.MAX_VALUE);
119:                 when(revealServiceFactory.getType()).thenReturn(EMFFormsRevealService.class);
120:                 when(revealServiceFactory.createService(any(EMFFormsViewContext.class))).thenReturn(revealService);
121:                 revealServiceFactoryReg = bundleContext.registerService(EMFFormsViewServiceFactory.class, revealServiceFactory,
122:                         new Hashtable<>());
123:
124:                 doReturn(EclipseContextFactory.createServiceContext(bundleContext)).when(site)
125:                         .getService(IEclipseContext.class);
126:                 doAnswer(invocation -> selectionProvider).when(site).getSelectionProvider();
127:                 doAnswer(invocation -> selectionProvider = (ISelectionProvider) invocation.getArguments()[0])
128:                         .when(site).setSelectionProvider(any(ISelectionProvider.class));
129:                 createEcoreAndGenModelFiles();
130:
131:                 final IURIEditorInput input = mock(IURIEditorInput.class);
132:                 when(input.getName()).thenReturn("Editor Title"); //$NON-NLS-1$
133:
134:                 when(input.getURI())
135:                         .thenReturn(new java.net.URI(URI.createFileURI(ecoreFile.getLocation().toString()).toString()));
136:                 when(input.getAdapter(IFile.class)).thenReturn(ecoreFile);
137:
138:                 editor = new EcoreEditor();
139:                 editor.init(site, input);
140:                 editor.createPartControl(new Shell());
141:
142:                 assumeThat("No selection provider set", selectionProvider, notNullValue()); //$NON-NLS-1$
143:         }
144:
145:         @After
146:         public void tearDown() {
147:                 revealServiceFactoryReg.unregister();
148:         }
149:
150:         /**
151:          * Test that the contents of the Ecore Editor are saved.
152:          *
153:          * @throws IOException if anything went wrong during the execution
154:          * @throws CoreException if anything went wrong during the execution
155:          */
156:         @Test
157:         public void testSave() throws IOException, CoreException {
158:                 final Resource ecoreResource = editor.getResourceSet().getResources().get(0);
159:                 EPackage ePackage = (EPackage) ecoreResource.getContents().get(0);
160:                 final EClass class1 = EcoreFactory.eINSTANCE.createEClass();
161:                 final String className = "class1"; //$NON-NLS-1$
162:                 class1.setName(className);
163:                 editor.getCommandStack().execute(new AddCommand(editor.getEditingDomain(), ePackage.getEClassifiers(), class1));
164:                 assertTrue(editor.isDirty());
165:                 editor.doSave(null);
166:                 assertFalse(editor.isDirty());
167:
168:                 // re-load resource and test if new contents are present
169:                 ecoreResource.unload();
170:                 ecoreResource.load(null);
171:                 ePackage = (EPackage) ecoreResource.getContents().get(0);
172:                 EObject addedClass = null;
173:•                for (final EClassifier classifier : ePackage.getEClassifiers()) {
174:•                        if (classifier.getName().equals(className)) {
175:                                 addedClass = classifier;
176:                                 break;
177:                         }
178:                 }
179:                 assertNotNull(addedClass);
180:
181:         }
182:
183:         @Test
184:         public void testNavigateGenericEditorStyleMarker() {
185:                 final Resource res = editor.getResourceSet().getResources().get(0);
186:                 final URI resourceURI = res.getURI();
187:                 final String fragment = "//Element/diagnostic"; //$NON-NLS-1$
188:                 final URI featureURI = EcoreUtil.getURI(EcorePackage.Literals.ENAMED_ELEMENT__NAME);
189:
190:                 final IMarker marker = mockMarker("RESOURCE_URI", resourceURI.toString(), //$NON-NLS-1$
191:                         "FRAGMENT_URI" /* (sic) */, fragment, //$NON-NLS-1$
192:                         "FEATURE_URI", featureURI.toString()); //$NON-NLS-1$
193:
194:                 testNavigate(marker);
195:         }
196:
197:         private void testNavigate(IMarker marker) {
198:                 editor.gotoMarker(marker);
199:
200:                 SWTTestUtil.waitForUIThread();
201:
202:                 final ISelection selection = selectionProvider.getSelection();
203:                 assertThat(selection, instanceOf(IStructuredSelection.class));
204:                 final Object selected = ((IStructuredSelection) selection).getFirstElement();
205:                 assertThat(selected, instanceOf(EReference.class));
206:                 final EReference reference = (EReference) selected;
207:                 assertThat(reference.getName(), is("diagnostic")); //$NON-NLS-1$
208:
209:                 verify(revealService).reveal(reference, EcorePackage.Literals.ENAMED_ELEMENT__NAME);
210:         }
211:
212:         @Test
213:         public void testNavigateEMFStyleMarker() {
214:                 final Resource res = editor.getResourceSet().getResources().get(0);
215:                 final URI objectURI = res.getURI().appendFragment("//Element/diagnostic"); //$NON-NLS-1$
216:                 final URI featureURI = EcoreUtil.getURI(EcorePackage.Literals.ENAMED_ELEMENT__NAME);
217:
218:                 final IMarker marker = mockMarker(
219:                         EValidator.URI_ATTRIBUTE, objectURI.toString(),
220:                         EValidator.RELATED_URIS_ATTRIBUTE, URI.encodeFragment(featureURI.toString(), false));
221:
222:                 testNavigate(marker);
223:         }
224:
225:         private void createEcoreAndGenModelFiles() throws CoreException {
226:                 final IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(EMFFORMS_EDITOR_TEST_PROJECT_NAME);
227:•                if (!project.exists()) {
228:                         project.create(null);
229:                         project.open(null);
230:                 }
231:                 ecoreFile = project.getFile(ECORE_FILENAME);
232:•                if (!ecoreFile.exists()) {
233:                         ecoreFile.create(this.getClass().getResourceAsStream(LOCAL_TEST_DATA + ECORE_FILENAME), IResource.NONE,
234:                                 null);
235:                 }
236:                 genModelFile = project.getFile(GENMODEL_FILENAME);
237:•                if (!genModelFile.exists()) {
238:                         genModelFile.create(this.getClass().getResourceAsStream(LOCAL_TEST_DATA + GENMODEL_FILENAME),
239:                                 IResource.NONE, null);
240:                 }
241:         }
242:
243:         IMarker mockMarker(String key, String value, String... more) {
244:•                assert more.length % 2 == 0 : "odd number of strings for attributes"; //$NON-NLS-1$
245:                 final Map<String, String> attributes = new HashMap<String, String>();
246:                 attributes.put(key, value);
247:•                for (int i = 0; i < more.length; i = i + 2) {
248:                         attributes.put(more[i], more[i + 1]);
249:                 }
250:
251:                 final IMarker result = mock(IMarker.class);
252:
253:                 try {
254:                         when(result.getAttribute(any(String.class)))
255:                                 .then(invocation -> attributes.get(invocation.getArguments()[0]));
256:                         when(result.getAttribute(any(String.class), any(String.class))).then(
257:                                 invocation -> attributes.getOrDefault(invocation.getArguments()[0],
258:                                         (String) invocation.getArguments()[1]));
259:                 } catch (final CoreException e) {
260:                         e.printStackTrace();
261:                         fail("Mock threw during stubbing"); //$NON-NLS-1$
262:                 }
263:
264:                 return result;
265:         }
266:
267: }