Skip to content

Package: IDEViewModelRegistry_PTest

IDEViewModelRegistry_PTest

nameinstructionbranchcomplexitylinemethod
IDEViewModelRegistry_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%
cleanupAfterClass()
M: 0 C: 7
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 4
100%
M: 0 C: 1
100%
getFieldByName(Object, String)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
installResourcesProject()
M: 3 C: 11
79%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 1 C: 4
80%
M: 0 C: 1
100%
loadEcore(IFile)
M: 0 C: 26
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
setUp()
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
setUpBeforeClass()
M: 3 C: 17
85%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 7
88%
M: 0 C: 1
100%
tearDownAfterClass()
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%
testRegisterAndUnregisterOneViewPerEcore()
M: 3 C: 72
96%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 1 C: 25
96%
M: 0 C: 1
100%
testRegisterAndUnregisterTwoViewsPerEcore()
M: 3 C: 124
98%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 1 C: 42
98%
M: 0 C: 1
100%
testRegisterUnregisterViewModelEditor()
M: 6 C: 48
89%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 13
87%
M: 0 C: 1
100%
testRegisterViewModel()
M: 0 C: 32
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
testRegisterViewModelEditorWithEcoreChange()
M: 0 C: 107
100%
M: 1 C: 7
88%
M: 1 C: 4
80%
M: 0 C: 19
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2014 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: ******************************************************************************/
14: package org.eclipse.emf.ecp.ide.view.service.test;
15:
16: import static org.junit.Assert.assertEquals;
17: import static org.junit.Assert.assertFalse;
18: import static org.junit.Assert.assertTrue;
19:
20: import java.io.IOException;
21: import java.lang.reflect.Field;
22: import java.util.Map;
23: import java.util.Set;
24: import java.util.concurrent.CountDownLatch;
25: import java.util.concurrent.TimeUnit;
26:
27: import org.eclipse.core.resources.IFile;
28: import org.eclipse.core.resources.IProject;
29: import org.eclipse.core.resources.IResourceChangeListener;
30: import org.eclipse.core.resources.IWorkspaceRoot;
31: import org.eclipse.core.resources.ResourcesPlugin;
32: import org.eclipse.core.runtime.NullProgressMonitor;
33: import org.eclipse.emf.common.util.URI;
34: import org.eclipse.emf.ecore.resource.Resource;
35: import org.eclipse.emf.ecore.resource.ResourceSet;
36: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
37: import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
38: import org.eclipse.emf.ecp.ide.view.internal.service.IDEViewModelRegistryImpl;
39: import org.eclipse.emf.ecp.ide.view.service.IDEViewModelRegistry;
40: import org.eclipse.emf.ecp.ide.view.service.ViewModelEditorCallback;
41: import org.eclipse.emf.ecp.view.spi.model.VView;
42: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
43: import org.eclipse.jface.resource.JFaceResources;
44: import org.eclipse.swt.widgets.Shell;
45: import org.junit.AfterClass;
46: import org.junit.Before;
47: import org.junit.BeforeClass;
48: import org.junit.Test;
49:
50: /**
51: * @author Alexandra Buzila
52: *
53: */
54: @SuppressWarnings("restriction")
55: public class IDEViewModelRegistry_PTest {
56:
57:         private static Shell SHELL;
58:
59:         /**
60:          * @throws java.lang.Exception
61:          */
62:         @BeforeClass
63:         public static void setUpBeforeClass() throws Exception {
64:                 try {
65:                         JFaceResources.getImageRegistry();
66:                 } catch (final RuntimeException e) {
67:                         // expected fail, some strange initialization error is happing
68:                 }
69:                 final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
70:                 final IProject project = root.getProject("TestEcoreHelperProjectResources");
71:                 // create resources to register and unregister
72:•                if (!project.exists()) {
73:                         SHELL = new Shell();
74:                         installResourcesProject();
75:                 }
76:         }
77:
78:         @AfterClass
79:         public static void cleanupAfterClass() throws Exception {
80:•                if (SHELL != null) {
81:                         SHELL.dispose();
82:                         SHELL = null;
83:                 }
84:         }
85:
86:         private static void installResourcesProject() {
87:                 final ProjectInstallerWizard wiz = new ProjectInstallerWizard();
88:                 try {
89:                         wiz.installExample(new NullProgressMonitor());
90:                 } catch (final Exception e) {
91:                         // TODO Auto-generated catch block
92:                         e.printStackTrace();
93:                 }
94:         }
95:
96:         /**
97:          * @throws java.lang.Exception
98:          */
99:         @AfterClass
100:         public static void tearDownAfterClass() throws Exception {
101:         }
102:
103:         private VView view;
104:
105:         /**
106:          * @throws java.lang.Exception
107:          */
108:         @Before
109:         public void setUp() throws Exception {
110:                 view = VViewFactory.eINSTANCE.createView();
111:                 view.getEcorePaths().add("/TestIDEViewRegistryProjectResources/task.ecore");
112:         }
113:
114:         /**
115:          * Test method for
116:          * {@link org.eclipse.emf.ecp.ide.view.internal.service.IDEViewModelRegistryImpl#register(java.lang.String, org.eclipse.emf.ecp.view.spi.model.VView)}
117:          * .
118:          */
119:         @SuppressWarnings("unchecked")
120:         @Test
121:         public void testRegisterAndUnregisterOneViewPerEcore() {
122:                 final IDEViewModelRegistry registry = new IDEViewModelRegistryImpl();
123:                 final String ecorePath = "test/ecore/path";
124:                 Map<String, Set<VView>> ecoreViewMapping = null;
125:                 Map<String, IResourceChangeListener> resourceChangeListeners = null;
126:                 try {
127:                         ecoreViewMapping = (Map<String, Set<VView>>) getFieldByName(registry, "ecoreViewMapping");
128:                         resourceChangeListeners = (Map<String, IResourceChangeListener>) getFieldByName(registry,
129:                                 "resourceChangeListeners");
130:                 } catch (final Exception e) {
131:                         // TODO Auto-generated catch block
132:                         e.printStackTrace();
133:                 }
134:
135:                 assertFalse("Registry already contains ecore path in ecoreViewMapping",
136:                         ecoreViewMapping.containsKey(ecorePath));
137:                 assertFalse("Registry already contains ecore path in resourceChangeListeners",
138:                         resourceChangeListeners.containsKey(ecorePath));
139:
140:                 registry.register(ecorePath, view);
141:
142:                 assertTrue("Registering of ecore failed: ecoreViewMapping does not contain the registered view.",
143:                         ecoreViewMapping.get(ecorePath).contains(view));
144:                 assertTrue(
145:                         "Registering of ecore failed: resourceChangeListeners map does not contain the registered ecorePath.",
146:                         resourceChangeListeners.containsKey(ecorePath));
147:
148:                 registry.unregister(ecorePath, view);
149:
150:                 assertFalse("Unregistering of ecore failed: ecoreViewMapping still contains the registered view.",
151:                         ecoreViewMapping.get(ecorePath).contains(view));
152:                 assertFalse(
153:                         "Unregistering of ecore failed: resourceChangeListeners map still contains the registered ecorePath.",
154:                         resourceChangeListeners.containsKey(ecorePath));
155:
156:         }
157:
158:         /**
159:          * Test method for
160:          * {@link org.eclipse.emf.ecp.ide.view.internal.service.IDEViewModelRegistryImpl#register(java.lang.String, org.eclipse.emf.ecp.view.spi.model.VView)}
161:          * .
162:          */
163:         @SuppressWarnings("unchecked")
164:         @Test
165:         public void testRegisterAndUnregisterTwoViewsPerEcore() {
166:
167:                 final IDEViewModelRegistry registry = new IDEViewModelRegistryImpl();
168:                 final String ecorePath = "test/ecore/path";
169:
170:                 final VView view1 = VViewFactory.eINSTANCE.createView();
171:                 final VView view2 = VViewFactory.eINSTANCE.createView();
172:
173:                 Map<String, Set<VView>> ecoreViewMapping = null;
174:                 Map<String, IResourceChangeListener> resourceChangeListeners = null;
175:                 try {
176:                         ecoreViewMapping = (Map<String, Set<VView>>) getFieldByName(registry, "ecoreViewMapping");
177:                         resourceChangeListeners = (Map<String, IResourceChangeListener>) getFieldByName(registry,
178:                                 "resourceChangeListeners");
179:                 } catch (final Exception e) {
180:                         // TODO Auto-generated catch block
181:                         e.printStackTrace();
182:                 }
183:
184:                 assertFalse("Registry already contains ecore path in ecoreViewMapping",
185:                         ecoreViewMapping.containsKey(ecorePath));
186:                 assertFalse("Registry already contains ecore path in resourceChangeListeners",
187:                         resourceChangeListeners.containsKey(ecorePath));
188:
189:                 registry.register(ecorePath, view1);
190:                 assertTrue("Registering of ecore failed: ecoreViewMapping does not contain the registered view.",
191:                         ecoreViewMapping.get(ecorePath).contains(view1));
192:                 assertTrue(
193:                         "Registering of ecore failed: resourceChangeListeners map does not contain the registered ecorePath.",
194:                         resourceChangeListeners.containsKey(ecorePath));
195:
196:                 registry.register(ecorePath, view2);
197:                 assertTrue("Registering of ecore failed: ecoreViewMapping does not contain the registered view.",
198:                         ecoreViewMapping.get(ecorePath).contains(view2));
199:                 assertTrue(
200:                         "Registering of ecore failed: resourceChangeListeners map does not contain the registered ecorePath.",
201:                         resourceChangeListeners.containsKey(ecorePath));
202:
203:                 registry.unregister(ecorePath, view1);
204:                 assertFalse("Unregistering of ecore failed: ecoreViewMapping still contains the registered view.",
205:                         ecoreViewMapping.get(ecorePath).contains(view1));
206:                 assertTrue("Unregistering failed: incorrect view was removed from ecoreViewMapping.",
207:                         ecoreViewMapping.get(ecorePath).contains(view2));
208:                 assertTrue("Unregistering failed: incorrect ecorePath was removed from resourceChangeListeners map.",
209:                         resourceChangeListeners.containsKey(ecorePath));
210:
211:                 registry.unregister(ecorePath, view2);
212:                 assertFalse("Unregistering of ecore failed: ecoreViewMapping still contains the registered view.",
213:                         ecoreViewMapping.get(ecorePath).contains(view1));
214:                 assertFalse("Unregistering of ecore failed: ecoreViewMapping still contains the registered view.",
215:                         ecoreViewMapping.get(ecorePath).contains(view2));
216:                 assertFalse(
217:                         "Unregistering of ecore failed: resourceChangeListeners map still contains the registered ecorePath.",
218:                         resourceChangeListeners.containsKey(ecorePath));
219:
220:         }
221:
222:         /**
223:          * Test method for
224:          * {@link org.eclipse.emf.ecp.ide.view.internal.service.IDEViewModelRegistryImpl#registerViewModelEditor(org.eclipse.emf.ecp.view.spi.model.VView, org.eclipse.emf.ecp.ide.view.service.ViewModelEditorCallback)}
225:          * .
226:          */
227:         @SuppressWarnings("unchecked")
228:         @Test
229:         public void testRegisterUnregisterViewModelEditor() {
230:                 final IDEViewModelRegistry registry = new IDEViewModelRegistryImpl();
231:                 Map<VView, ViewModelEditorCallback> viewModelViewModelEditorMapping = null;
232:                 try {
233:                         viewModelViewModelEditorMapping = (Map<VView, ViewModelEditorCallback>) getFieldByName(registry,
234:                                 "viewModelViewModelEditorMapping");
235:                 } catch (final Exception e) {
236:                         // TODO Auto-generated catch block
237:                         e.printStackTrace();
238:                 }
239:
240:                 assertFalse("", viewModelViewModelEditorMapping.containsKey(view));
241:
242:                 final ViewModelEditorCallback viewModelEditor = new ViewModelEditorCallback() {
243:
244:                         @Override
245:                         public void signalEcoreOutOfSync() {
246:                                 // TODO Auto-generated method stub
247:
248:                         }
249:
250:                         @Override
251:                         public void reloadViewModel() {
252:                                 // TODO Auto-generated method stub
253:
254:                         }
255:                 };
256:                 try {
257:                         registry.registerViewModelEditor(view, viewModelEditor);
258:                 } catch (final IOException e) {
259:                         // TODO Auto-generated catch block
260:                         e.printStackTrace();
261:                 }
262:
263:                 assertTrue("", viewModelViewModelEditorMapping.containsKey(view));
264:
265:                 registry.unregisterViewModelEditor(view, viewModelEditor);
266:
267:                 assertFalse("", viewModelViewModelEditorMapping.containsKey(view));
268:         }
269:
270:         @Test
271:         public void testRegisterViewModelEditorWithEcoreChange() throws IOException, InterruptedException {
272:                 // setup
273:                 final CountDownLatch latch = new CountDownLatch(1);
274:                 final IDEViewModelRegistry registry = new IDEViewModelRegistryImpl();
275:                 final ViewModelEditorCallback callback = new ViewModelEditorCallback() {
276:
277:                         @Override
278:                         public void signalEcoreOutOfSync() {
279:                                 latch.countDown();
280:                         }
281:
282:                         @Override
283:                         public void reloadViewModel() {
284:                                 // not tested
285:                         }
286:                 };
287:•                for (final String ecorePath : view.getEcorePaths()) {
288:                         registry.register(ecorePath, view);
289:                 }
290:                 registry.registerViewModelEditor(view, callback);
291:
292:                 // act
293:                 final IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
294:                 IProject project = null;
295:•                for (final IProject p : projects) {
296:•                        if ("TestIDEViewRegistryProjectResources".equals(p.getName())) {
297:                                 project = p;
298:                         }
299:                 }
300:                 final IFile file = project.getFile("task.ecore");
301:                 final Resource resource = loadEcore(file);
302:                 // calling save without a change will trigger listener
303:                 resource.save(null);
304:
305:                 // assert
306:                 assertTrue("signalEcoreOutOfSync has not been called", latch.await(1, TimeUnit.SECONDS));
307:                 registry.unregisterViewModelEditor(view, callback);
308:
309:•                for (final String ecorePath : view.getEcorePaths()) {
310:                         registry.unregister(ecorePath, view);
311:                 }
312:         }
313:
314:         @SuppressWarnings("unchecked")
315:         @Test
316:         public void testRegisterViewModel() throws Exception {
317:                 final IDEViewModelRegistry registry = new IDEViewModelRegistryImpl();
318:                 final Map<VView, String> viewModelviewModelFileMapping = (Map<VView, String>) getFieldByName(registry,
319:                         "viewModelviewModelFileMapping");
320:
321:                 assertTrue(viewModelviewModelFileMapping.isEmpty());
322:                 final String path = "path1234";
323:                 registry.registerViewModel(view, path);
324:                 assertTrue(viewModelviewModelFileMapping.containsKey(view));
325:                 assertEquals(path, viewModelviewModelFileMapping.get(view));
326:         }
327:
328:         private Object getFieldByName(Object instance, String fieldName) throws Exception {
329:                 final Field f = instance.getClass().getDeclaredField(fieldName);
330:                 f.setAccessible(true);
331:
332:                 return f.get(instance);
333:         }
334:
335:         private static Resource loadEcore(IFile file) {
336:                 final ResourceSet resourceSet = new ResourceSetImpl();
337:                 final Map<String, Object> extensionMap = resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap();
338:                 extensionMap.put("*", new XMIResourceFactoryImpl());
339:                 final URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(), false);
340:                 return resourceSet.getResource(uri, true);
341:         }
342:
343: }