Skip to content

Package: ViewProvider_Test

ViewProvider_Test

nameinstructionbranchcomplexitylinemethod
ViewProvider_Test()
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%
assertView(VView)
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
controls(VElement)
M: 0 C: 20
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
controlsHaveUniqueUUIDs()
M: 0 C: 158
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 31
100%
M: 0 C: 1
100%
lambda$0(Set, VControl)
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
lambda$1(int[], VControl)
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(VControl)
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%
provideViewModel_attribute()
M: 0 C: 66
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 17
100%
M: 0 C: 1
100%
provideViewModel_multiContainmentReference()
M: 0 C: 84
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 23
100%
M: 0 C: 1
100%
provideViewModel_multiCrossReference()
M: 0 C: 73
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 19
100%
M: 0 C: 1
100%
provideViewModel_singleReference()
M: 0 C: 67
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 17
100%
M: 0 C: 1
100%
readOnlyControls()
M: 0 C: 88
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 20
100%
M: 0 C: 1
100%
setUp()
M: 0 C: 50
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 13
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: * Lucas Koehler - initial API and implementation
13: * Christian W. Damus - bug 547271
14: ******************************************************************************/
15: package org.eclipse.emf.ecp.view.model.generator;
16:
17: import static org.hamcrest.CoreMatchers.is;
18: import static org.hamcrest.CoreMatchers.not;
19: import static org.hamcrest.CoreMatchers.notNullValue;
20: import static org.hamcrest.MatcherAssert.assertThat;
21: import static org.junit.Assert.assertEquals;
22: import static org.junit.Assert.assertFalse;
23: import static org.junit.Assert.assertNotNull;
24: import static org.junit.Assert.assertNull;
25: import static org.junit.Assert.assertSame;
26: import static org.junit.Assert.assertTrue;
27: import static org.mockito.Mockito.mock;
28: import static org.mockito.Mockito.when;
29: import static org.mockito.Mockito.withSettings;
30:
31: import java.util.Collections;
32: import java.util.HashMap;
33: import java.util.HashSet;
34: import java.util.Map;
35: import java.util.Set;
36: import java.util.Spliterator;
37: import java.util.Spliterators;
38: import java.util.stream.Stream;
39: import java.util.stream.StreamSupport;
40:
41: import org.eclipse.emf.common.command.BasicCommandStack;
42: import org.eclipse.emf.common.notify.AdapterFactory;
43: import org.eclipse.emf.common.util.ECollections;
44: import org.eclipse.emf.common.util.EList;
45: import org.eclipse.emf.common.util.URI;
46: import org.eclipse.emf.ecore.EAttribute;
47: import org.eclipse.emf.ecore.EClass;
48: import org.eclipse.emf.ecore.EObject;
49: import org.eclipse.emf.ecore.EPackage;
50: import org.eclipse.emf.ecore.EReference;
51: import org.eclipse.emf.ecore.EStructuralFeature;
52: import org.eclipse.emf.ecore.EcoreFactory;
53: import org.eclipse.emf.ecore.EcorePackage;
54: import org.eclipse.emf.ecore.InternalEObject;
55: import org.eclipse.emf.ecore.resource.Resource;
56: import org.eclipse.emf.ecore.resource.ResourceSet;
57: import org.eclipse.emf.ecore.resource.impl.ResourceImpl;
58: import org.eclipse.emf.ecore.util.BasicExtendedMetaData.EClassifierExtendedMetaData;
59: import org.eclipse.emf.ecore.util.EObjectContainmentWithInverseEList;
60: import org.eclipse.emf.ecore.util.EObjectEList;
61: import org.eclipse.emf.ecore.util.EcoreUtil;
62: import org.eclipse.emf.ecp.view.spi.model.VControl;
63: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
64: import org.eclipse.emf.ecp.view.spi.model.VElement;
65: import org.eclipse.emf.ecp.view.spi.model.VFeaturePathDomainModelReference;
66: import org.eclipse.emf.ecp.view.spi.model.VView;
67: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
68: import org.eclipse.emf.ecp.view.spi.model.VViewModelLoadingProperties;
69: import org.eclipse.emf.ecp.view.spi.table.model.VTableControl;
70: import org.eclipse.emf.ecp.view.spi.table.model.VTableDomainModelReference;
71: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
72: import org.eclipse.emf.edit.domain.EditingDomain;
73: import org.junit.Before;
74: import org.junit.Test;
75:
76: /**
77: * Unit tests for {@link ViewProvider}.
78: *
79: * @author Lucas Koehler
80: */
81: public class ViewProvider_Test {
82:
83:         private EClass eClass;
84:         private EClass refType;
85:         private ViewProvider viewProvider;
86:         private VViewModelLoadingProperties viewProperties;
87:
88:         @Before
89:         public void setUp() {
90:                 viewProvider = new ViewProvider();
91:                 viewProperties = VViewFactory.eINSTANCE.createViewModelLoadingProperties();
92:
93:                 eClass = EcoreFactory.eINSTANCE.createEClass();
94:                 eClass.setName("Test");
95:                 refType = EcoreFactory.eINSTANCE.createEClass();
96:                 refType.setName("RefType");
97:
98:                 final EPackage p = EcoreFactory.eINSTANCE.createEPackage();
99:                 p.setName("p");
100:                 p.setNsPrefix("p");
101:                 p.setNsURI("p");
102:                 p.getEClassifiers().add(eClass);
103:                 p.getEClassifiers().add(refType);
104:         }
105:
106:         @Test
107:         public void provideViewModel_attribute() {
108:                 final EAttribute attribute = EcoreFactory.eINSTANCE.createEAttribute();
109:                 attribute.setName("attribute");
110:                 attribute.setEType(EcorePackage.Literals.ESTRING);
111:                 eClass.getEStructuralFeatures().add(attribute);
112:
113:                 final EObject object = EcoreUtil.create(eClass);
114:                 final VView view = viewProvider.provideViewModel(object, viewProperties);
115:                 assertView(view);
116:
117:                 assertTrue(view.getChildren().get(0) instanceof VControl);
118:                 final VControl control = (VControl) view.getChildren().get(0);
119:                 assertNotNull(control.getUuid());
120:                 assertFalse(control.getUuid().isEmpty());
121:                 final VDomainModelReference dmr = control.getDomainModelReference();
122:                 assertTrue(dmr instanceof VFeaturePathDomainModelReference);
123:                 final VFeaturePathDomainModelReference featureDmr = (VFeaturePathDomainModelReference) dmr;
124:                 assertTrue(featureDmr.getDomainModelEReferencePath().isEmpty());
125:                 assertSame(attribute, featureDmr.getDomainModelEFeature());
126:         }
127:
128:         @Test
129:         public void provideViewModel_singleReference() {
130:                 final EReference singleRef = EcoreFactory.eINSTANCE.createEReference();
131:                 singleRef.setName("singleRef");
132:                 singleRef.setEType(refType);
133:                 eClass.getEStructuralFeatures().add(singleRef);
134:
135:                 final EObject object = EcoreUtil.create(eClass);
136:                 final VView view = viewProvider.provideViewModel(object, viewProperties);
137:                 assertView(view);
138:
139:                 assertTrue(view.getChildren().get(0) instanceof VControl);
140:                 final VControl control = (VControl) view.getChildren().get(0);
141:                 assertNotNull(control.getUuid());
142:                 assertFalse(control.getUuid().isEmpty());
143:                 final VDomainModelReference dmr = control.getDomainModelReference();
144:                 assertTrue(dmr instanceof VFeaturePathDomainModelReference);
145:                 final VFeaturePathDomainModelReference featureDmr = (VFeaturePathDomainModelReference) dmr;
146:                 assertTrue(featureDmr.getDomainModelEReferencePath().isEmpty());
147:                 assertSame(singleRef, featureDmr.getDomainModelEFeature());
148:         }
149:
150:         @Test
151:         public void provideViewModel_multiCrossReference() {
152:                 final EReference multiCrossRef = EcoreFactory.eINSTANCE.createEReference();
153:                 multiCrossRef.setName("multiCrossRef");
154:                 multiCrossRef.setEType(refType);
155:                 multiCrossRef.setUpperBound(-1); // multi ref
156:                 multiCrossRef.setContainment(false);
157:                 eClass.getEStructuralFeatures().add(multiCrossRef);
158:
159:                 final EObject object = EcoreUtil.create(eClass);
160:                 final VView view = viewProvider.provideViewModel(object, viewProperties);
161:                 assertView(view);
162:
163:                 assertTrue(view.getChildren().get(0) instanceof VControl);
164:                 final VControl control = (VControl) view.getChildren().get(0);
165:                 assertNotNull(control.getUuid());
166:                 assertFalse(control.getUuid().isEmpty());
167:                 final VDomainModelReference dmr = control.getDomainModelReference();
168:                 assertTrue(dmr instanceof VFeaturePathDomainModelReference);
169:                 final VFeaturePathDomainModelReference featureDmr = (VFeaturePathDomainModelReference) dmr;
170:                 assertTrue(featureDmr.getDomainModelEReferencePath().isEmpty());
171:                 assertSame(multiCrossRef, featureDmr.getDomainModelEFeature());
172:         }
173:
174:         /** Verify that the generator generates a table control for multi containment refs. */
175:         @Test
176:         public void provideViewModel_multiContainmentReference() {
177:                 final EReference multiContainmentRef = EcoreFactory.eINSTANCE.createEReference();
178:                 multiContainmentRef.setName("multiContainmentRef");
179:                 multiContainmentRef.setEType(refType);
180:                 multiContainmentRef.setUpperBound(-1); // multi ref
181:                 multiContainmentRef.setContainment(true);
182:                 eClass.getEStructuralFeatures().add(multiContainmentRef);
183:
184:                 final EObject object = EcoreUtil.create(eClass);
185:                 final VView view = viewProvider.provideViewModel(object, viewProperties);
186:                 assertView(view);
187:
188:                 assertTrue(view.getChildren().get(0) instanceof VTableControl);
189:                 final VTableControl control = (VTableControl) view.getChildren().get(0);
190:                 assertNotNull(control.getUuid());
191:                 assertFalse(control.getUuid().isEmpty());
192:                 final VDomainModelReference controlDmr = control.getDomainModelReference();
193:                 assertTrue(controlDmr instanceof VTableDomainModelReference);
194:                 final VTableDomainModelReference tableDmr = (VTableDomainModelReference) controlDmr;
195:                 assertTrue(tableDmr.getDomainModelEReferencePath().isEmpty());
196:                 assertNull(tableDmr.getDomainModelEFeature());
197:
198:                 assertTrue(tableDmr.getDomainModelReference() instanceof VFeaturePathDomainModelReference);
199:                 final VFeaturePathDomainModelReference featureDmr = (VFeaturePathDomainModelReference) tableDmr
200:                         .getDomainModelReference();
201:
202:                 assertSame(multiContainmentRef, featureDmr.getDomainModelEFeature());
203:         }
204:
205:         /**
206:          * Verify that controls' generated UUIDs actually are unique within the view (there is no
207:          * guarantee that they are unique globally, but the {@link ViewProvider} does not need that).
208:          */
209:         @Test
210:         public void controlsHaveUniqueUUIDs() {
211:                 final EClass eClass = mock(EClass.class,
212:                         withSettings().extraInterfaces(InternalEObject.class, EClassifierExtendedMetaData.Holder.class));
213:                 when(eClass.eContainer()).thenReturn(this.eClass.eContainer());
214:                 when(eClass.getEPackage()).thenReturn(this.eClass.getEPackage());
215:                 when(eClass.getName()).thenReturn("Mock");
216:                 when(eClass.getESuperTypes()).thenReturn(ECollections.emptyEList());
217:                 when(((EClassifierExtendedMetaData.Holder) eClass).getExtendedMetaData())
218:                         .thenReturn(mock(EClassifierExtendedMetaData.class));
219:
220:                 // Repeat the exact same feature multiple times to create controls
221:                 final EList<EStructuralFeature> features = new EObjectContainmentWithInverseEList<>(EStructuralFeature.class,
222:                         (InternalEObject) eClass, EcorePackage.ECLASS__ESTRUCTURAL_FEATURES,
223:                         EcorePackage.ESTRUCTURAL_FEATURE__ECONTAINING_CLASS);
224:                 final EReference ref = EcoreFactory.eINSTANCE.createEReference();
225:                 ref.setEType(refType);
226:                 ref.setName("ref");
227:                 final EList<EReference> references = new EObjectEList<>(EReference.class, (InternalEObject) eClass,
228:                         EcorePackage.ECLASS__EREFERENCES);
229:                 references.addAll(Collections.nCopies(500, ref));
230:                 features.addAll(references);
231:                 when(eClass.getEStructuralFeatures()).thenReturn(features);
232:                 when(eClass.getEAllStructuralFeatures()).thenReturn(ECollections.unmodifiableEList(features));
233:                 when(eClass.getEReferences()).thenReturn(references);
234:                 when(eClass.getEAllReferences()).thenReturn(references);
235:                 when(eClass.getEAttributes()).thenReturn(ECollections.emptyEList());
236:                 when(eClass.getEAllAttributes()).thenReturn(ECollections.emptyEList());
237:                 final EObject object = EcoreUtil.create(eClass);
238:
239:                 final VView view = viewProvider.provideViewModel(object, viewProperties);
240:
241:                 final Set<String> uuids = new HashSet<>();
242:                 controls(view).forEach(control -> {
243:                         final String uuid = control.getUuid();
244:                         assertThat("No UUID generated", uuid, notNullValue());
245:                         assertThat("Non-unique UUID: " + uuid, uuids.add(uuid));
246:                 });
247:
248:                 assertThat("Didn't get a control for each occurrence of the feature", uuids.size(),
249:                         is(eClass.getEAllStructuralFeatures().size()));
250:         }
251:
252:         /**
253:          * Verify that the item provider is used to determine read-only state of controls.
254:          */
255:         @Test
256:         public void readOnlyControls() {
257:                 final EReference ref = EcoreFactory.eINSTANCE.createEReference();
258:                 ref.setEType(refType);
259:                 ref.setName("ref");
260:                 eClass.getEStructuralFeatures().add(ref);
261:
262:                 final Map<Resource, Boolean> readOnly = new HashMap<>();
263:                 final EditingDomain domain = new AdapterFactoryEditingDomain(mock(AdapterFactory.class),
264:                         new BasicCommandStack(), readOnly);
265:                 final ResourceSet rset = domain.getResourceSet();
266:                 final Resource resource = new ResourceImpl(URI.createURI("test:/resource"));
267:                 final EObject object = EcoreUtil.create(eClass);
268:                 resource.getContents().add(object);
269:                 rset.getResources().add(resource);
270:                 readOnly.put(resource, true);
271:
272:                 final VView view = viewProvider.provideViewModel(object, viewProperties);
273:                 final int[] count = { 0 };
274:                 controls(view)
275:                         .peek(__ -> count[0]++)
276:                         .forEach(control -> assertThat("Control not read-only", control.isEffectivelyReadonly(), is(true)));
277:                 assertThat("No controls found", count[0], not(0));
278:         }
279:
280:         private void assertView(final VView view) {
281:                 assertNotNull(view);
282:                 assertNotNull(view.getUuid());
283:                 assertFalse(view.getUuid().isEmpty());
284:                 assertEquals(1, view.getChildren().size());
285:         }
286:
287:         private Stream<VControl> controls(VElement viewModel) {
288:                 return StreamSupport
289:                         .stream(Spliterators.spliteratorUnknownSize(EcoreUtil.getAllContents(Collections.singleton(viewModel)),
290:                                 Spliterator.DISTINCT | Spliterator.NONNULL | Spliterator.ORDERED), false)
291:                         .filter(VControl.class::isInstance).map(VControl.class::cast);
292:         }
293: }