Skip to content

Package: ValidationService_PTest$2

ValidationService_PTest$2

nameinstructionbranchcomplexitylinemethod
validate(EObject)
M: 0 C: 14
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
{...}
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%

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: * Stefan Dirix - initial API and implementation
13: * Christian W. Damus - bugs 533522, 552127
14: ******************************************************************************/
15: package org.eclipse.emf.ecp.view.validation.test;
16:
17: import static org.hamcrest.CoreMatchers.equalTo;
18: import static org.hamcrest.CoreMatchers.is;
19: import static org.hamcrest.CoreMatchers.notNullValue;
20: import static org.hamcrest.MatcherAssert.assertThat;
21: import static org.junit.Assert.assertFalse;
22: import static org.junit.Assert.assertTrue;
23: import static org.mockito.Mockito.mock;
24:
25: import java.util.ArrayList;
26: import java.util.Arrays;
27: import java.util.Collection;
28: import java.util.Collections;
29: import java.util.List;
30: import java.util.concurrent.atomic.AtomicReference;
31:
32: import org.eclipse.emf.common.util.Diagnostic;
33: import org.eclipse.emf.ecore.EObject;
34: import org.eclipse.emf.ecp.common.spi.UniqueSetting;
35: import org.eclipse.emf.ecp.test.common.DefaultRealm;
36: import org.eclipse.emf.ecp.view.internal.context.ViewModelContextImpl;
37: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
38: import org.eclipse.emf.ecp.view.spi.context.ViewModelContextFactory;
39: import org.eclipse.emf.ecp.view.spi.model.VControl;
40: import org.eclipse.emf.ecp.view.spi.model.VFeaturePathDomainModelReference;
41: import org.eclipse.emf.ecp.view.spi.model.VView;
42: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
43: import org.eclipse.emf.ecp.view.spi.validation.ValidationProvider;
44: import org.eclipse.emf.ecp.view.spi.validation.ValidationService;
45: import org.eclipse.emf.ecp.view.spi.validation.ViewValidationListener;
46: import org.eclipse.emf.ecp.view.validation.test.model.CrossReferenceContainer;
47: import org.eclipse.emf.ecp.view.validation.test.model.CrossReferenceContent;
48: import org.eclipse.emf.ecp.view.validation.test.model.TableContentWithInnerChild;
49: import org.eclipse.emf.ecp.view.validation.test.model.TestFactory;
50: import org.eclipse.emf.ecp.view.validation.test.model.TestPackage;
51: import org.eclipse.emfforms.spi.common.report.AbstractReport;
52: import org.eclipse.emfforms.spi.common.report.ReportService;
53: import org.eclipse.emfforms.spi.common.report.ReportServiceConsumer;
54: import org.eclipse.emfforms.spi.core.services.mappingprovider.EMFFormsMappingProviderManager;
55: import org.junit.After;
56: import org.junit.Before;
57: import org.junit.Test;
58: import org.mockito.Mockito;
59: import org.osgi.framework.BundleContext;
60: import org.osgi.framework.FrameworkUtil;
61: import org.osgi.framework.ServiceReference;
62:
63: /**
64: * @author Stefan Dirix
65: *
66: */
67: @SuppressWarnings("restriction")
68: public class ValidationService_PTest {
69:
70:         private DefaultRealm defaultRealm;
71:
72:         private VControl control;
73:         private ValidationService validationService;
74:
75:         private CrossReferenceContainer container;
76:         private CrossReferenceContent content;
77:
78:         private CrossReferenceContainer otherContainer;
79:
80:         private BundleContext bundleContext;
81:
82:         private ServiceReference<ReportService> reportServiceReference;
83:
84:         private ReportService reportService;
85:
86:         /**
87:          * @throws java.lang.Exception
88:          */
89:         @Before
90:         public void setUp() throws Exception {
91:                 defaultRealm = new DefaultRealm();
92:
93:                 bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
94:                 reportServiceReference = bundleContext.getServiceReference(ReportService.class);
95:                 reportService = bundleContext.getService(reportServiceReference);
96:         }
97:
98:         /**
99:          * @throws java.lang.Exception
100:          */
101:         @After
102:         public void tearDown() throws Exception {
103:                 defaultRealm.dispose();
104:                 bundleContext.ungetService(reportServiceReference);
105:         }
106:
107:         private void setupContent() {
108:                 final VView view = VViewFactory.eINSTANCE.createView();
109:                 view.setRootEClass(TestPackage.eINSTANCE.getContent());
110:
111:                 control = VViewFactory.eINSTANCE.createControl();
112:
113:                 final VFeaturePathDomainModelReference domainModelReference = VViewFactory.eINSTANCE
114:                         .createFeaturePathDomainModelReference();
115:                 domainModelReference.setDomainModelEFeature(TestPackage.eINSTANCE.getCrossReferenceContent_Parent());
116:                 control.setDomainModelReference(domainModelReference);
117:
118:                 view.getChildren().add(control);
119:
120:                 container = TestFactory.eINSTANCE.createCrossReferenceContainer();
121:                 otherContainer = TestFactory.eINSTANCE.createCrossReferenceContainer();
122:                 content = TestFactory.eINSTANCE.createCrossReferenceContent();
123:                 final ViewModelContext vmc = ViewModelContextFactory.INSTANCE.createViewModelContext(view, content);
124:                 validationService = vmc.getService(ValidationService.class);
125:         }
126:
127:         /**
128:          * There should be no validation on an unset crossreferenced element.
129:          */
130:         @Test
131:         public void testNoValidationOnUnsetElements() {
132:                 setupContent();
133:                 container.setSingleContent(content);
134:                 final List<Boolean> called = new ArrayList<Boolean>(1);
135:                 called.add(false);
136:                 validationService.addValidationProvider(new ValidationProvider() {
137:                         @Override
138:                         public List<Diagnostic> validate(EObject eObject) {
139:                                 if (content == eObject) {
140:                                         called.set(0, true);
141:                                 }
142:                                 return Collections.emptyList();
143:                         }
144:                 });
145:                 called.set(0, false);
146:                 container.setSingleContent(null);
147:
148:                 assertFalse(called.get(0));
149:         }
150:
151:         /**
152:          * There should be a validation on a set crossreferenced element.
153:          */
154:         @Test
155:         public void testValidationOnSetElements() {
156:                 setupContent();
157:                 container.setSingleContent(content);
158:                 final List<Boolean> called = new ArrayList<Boolean>(1);
159:                 called.add(false);
160:                 validationService.addValidationProvider(new ValidationProvider() {
161:                         @Override
162:                         public List<Diagnostic> validate(EObject eObject) {
163:•                                if (content == eObject) {
164:                                         called.set(0, true);
165:                                 }
166:                                 return Collections.emptyList();
167:                         }
168:                 });
169:                 called.set(0, false);
170:                 otherContainer.setSingleContent(content);
171:
172:                 assertTrue(called.get(0));
173:         }
174:
175:         /**
176:          * There should be no validation on a removed crossreferenced element.
177:          */
178:         @Test
179:         public void testNoValidationOnRemovedElements() {
180:                 setupContent();
181:                 container.getContents().add(content);
182:
183:                 final List<Boolean> called = new ArrayList<Boolean>(1);
184:                 called.add(false);
185:                 validationService.addValidationProvider(new ValidationProvider() {
186:                         @Override
187:                         public List<Diagnostic> validate(EObject eObject) {
188:                                 if (content == eObject) {
189:                                         called.set(0, true);
190:                                 }
191:                                 return Collections.emptyList();
192:                         }
193:                 });
194:                 called.set(0, false);
195:                 container.getContents().remove(content);
196:
197:                 assertFalse(called.get(0));
198:         }
199:
200:         /**
201:          * There should be a validation on a moved crossreferenced element.
202:          */
203:         @Test
204:         public void testValidationOnMovedElements() {
205:                 setupContent();
206:                 container.getContents().add(content);
207:
208:                 final List<Boolean> called = new ArrayList<Boolean>(1);
209:                 called.add(false);
210:                 validationService.addValidationProvider(new ValidationProvider() {
211:                         @Override
212:                         public List<Diagnostic> validate(EObject eObject) {
213:                                 if (content == eObject) {
214:                                         called.set(0, true);
215:                                 }
216:                                 return Collections.emptyList();
217:                         }
218:                 });
219:                 called.set(0, false);
220:                 otherContainer.getContents().add(content);
221:
222:                 assertTrue(called.get(0));
223:         }
224:
225:         @Test
226:         public void testValidationTimeoutReport2000() {
227:                 setupContent();
228:                 container.getContents().add(content);
229:
230:                 final List<Boolean> called = new ArrayList<Boolean>(1);
231:                 called.add(false);
232:                 final ReportServiceConsumer reportServiceConsumer = new ReportServiceConsumer() {
233:
234:                         @Override
235:                         public void reported(AbstractReport reportEntity) {
236:                                 assertTrue(reportEntity.getMessage().startsWith("Validation took longer than expected for"));
237:                                 called.set(0, true);
238:                         }
239:                 };
240:                 reportService.addConsumer(reportServiceConsumer);
241:                 validationService.addValidationProvider(new ValidationProvider() {
242:                         @Override
243:                         public List<Diagnostic> validate(EObject eObject) {
244:                                 try {
245:                                         Thread.sleep(3000);
246:                                 } catch (final InterruptedException ex) {
247:                                 }
248:                                 return Collections.emptyList();
249:                         }
250:                 });
251:
252:                 validationService.validate(Arrays.asList(content.eContainer()));
253:                 assertTrue("Validation report missing", called.get(0));
254:                 reportService.removeConsumer(reportServiceConsumer);
255:         }
256:
257:         @Test
258:         public void testValidationTimeoutReport1000() {
259:                 setupContent();
260:                 container.getContents().add(content);
261:
262:                 final List<Boolean> called = new ArrayList<Boolean>(1);
263:                 called.add(false);
264:                 final ReportServiceConsumer reportServiceConsumer = new ReportServiceConsumer() {
265:
266:                         @Override
267:                         public void reported(AbstractReport reportEntity) {
268:                                 assertTrue("Real Message:" + reportEntity.getMessage(),
269:                                         reportEntity.getMessage().startsWith("Validation took longer than expected for"));
270:                                 called.set(0, true);
271:
272:                         }
273:                 };
274:                 reportService.addConsumer(reportServiceConsumer);
275:                 validationService.addValidationProvider(new ValidationProvider() {
276:                         @Override
277:                         public List<Diagnostic> validate(EObject eObject) {
278:                                 try {
279:                                         Thread.sleep(2000);
280:                                 } catch (final InterruptedException ex) {
281:                                 }
282:                                 return Collections.emptyList();
283:                         }
284:                 });
285:
286:                 validationService.validate(Arrays.asList(content.eContainer()));
287:                 assertTrue("Validation report missing", called.get(0));
288:                 reportService.removeConsumer(reportServiceConsumer);
289:         }
290:
291:         @Test
292:         public void testValidationTimeoutReportNoDelay() {
293:                 setupContent();
294:                 container.getContents().add(content);
295:
296:                 final List<Boolean> called = new ArrayList<Boolean>(1);
297:                 called.add(false);
298:                 final ReportServiceConsumer reportServiceConsumer = new ReportServiceConsumer() {
299:
300:                         @Override
301:                         public void reported(AbstractReport reportEntity) {
302:                                 assertTrue(reportEntity.getMessage().startsWith("Validation took longer than expected for"));
303:                                 called.set(0, true);
304:
305:                         }
306:                 };
307:                 reportService.addConsumer(reportServiceConsumer);
308:                 validationService.addValidationProvider(new ValidationProvider() {
309:                         @Override
310:                         public List<Diagnostic> validate(EObject eObject) {
311:                                 return Collections.emptyList();
312:                         }
313:                 });
314:
315:                 validationService.validate(Arrays.asList(content.eContainer()));
316:                 assertFalse("Validation report present", called.get(0));
317:
318:                 reportService.removeConsumer(reportServiceConsumer);
319:         }
320:
321:         @Test
322:         public void testViewModelChangeListenerCutOffDMR() {
323:                 /* setup domain */
324:                 final TableContentWithInnerChild parent = TestFactory.eINSTANCE.createTableContentWithInnerChild();
325:                 final TableContentWithInnerChild middle = TestFactory.eINSTANCE.createTableContentWithInnerChild();
326:                 final TableContentWithInnerChild child = TestFactory.eINSTANCE.createTableContentWithInnerChild();
327:                 parent.setInnerChild(middle);
328:                 middle.setInnerChild(child);
329:
330:                 /* setup view */
331:                 final VView view = VViewFactory.eINSTANCE.createView();
332:
333:                 final VControl vControl = VViewFactory.eINSTANCE.createControl();
334:                 view.getChildren().add(vControl);
335:                 vControl.setDomainModelReference(
336:                         TestPackage.eINSTANCE.getTableContentWithInnerChild_Stuff(),
337:                         Arrays.asList(
338:                                 TestPackage.eINSTANCE.getTableContentWithInnerChild_InnerChild(),
339:                                 TestPackage.eINSTANCE.getTableContentWithInnerChild_InnerChild()));
340:
341:                 /* setup rendering */
342:                 ViewModelContextFactory.INSTANCE.createViewModelContext(view, parent);
343:
344:                 /* act */
345:                 parent.setInnerChild(null); /* cut off */
346:                 vControl.setEnabled(false); /* produce view notifications */
347:                 vControl.setEnabled(true);/* produce view notifications */
348:
349:                 /* assert */
350:                 /* cutting of should not produce NPEs as this is legit */
351:         }
352:
353:         @Test
354:         public void testBug529403controlAdded() {
355:                 /* setup domain */
356:                 final TableContentWithInnerChild parent = TestFactory.eINSTANCE.createTableContentWithInnerChild();
357:                 final TableContentWithInnerChild middle = TestFactory.eINSTANCE.createTableContentWithInnerChild();
358:                 final TableContentWithInnerChild child = TestFactory.eINSTANCE.createTableContentWithInnerChild();
359:                 parent.setInnerChild(middle);
360:                 middle.setInnerChild(child);
361:
362:                 /* setup view */
363:                 final VView view = VViewFactory.eINSTANCE.createView();
364:
365:                 /* setup control to add */
366:                 final VControl vControl = VViewFactory.eINSTANCE.createControl();
367:                 vControl.setDomainModelReference(
368:                         TestPackage.eINSTANCE.getTableContentWithInnerChild_Stuff(),
369:                         Arrays.asList(
370:                                 TestPackage.eINSTANCE.getTableContentWithInnerChild_InnerChild(),
371:                                 TestPackage.eINSTANCE.getTableContentWithInnerChild_InnerChild()));
372:
373:                 /*
374:                  * construct a situation where the observed object is null. This is valid according to
375:                  * org.eclipse.core.databinding.observable.IObserving.getObserved() javadoc, so we have to prepare for this.
376:                  * Easiest way to achieve this for testing is to mock the mapping provider
377:                  */
378:                 new ViewModelContextImpl(view, parent) {
379:
380:                         @SuppressWarnings("unchecked")
381:                         @Override
382:                         public <T> T getService(Class<T> serviceType) {
383:                                 if (EMFFormsMappingProviderManager.class == serviceType) {
384:                                         final EMFFormsMappingProviderManager mock = Mockito.mock(EMFFormsMappingProviderManager.class);
385:                                         Mockito.doReturn(Collections.singleton(UniqueSetting.createSetting(null, null))).when(mock)
386:                                                 .getAllSettingsFor(vControl.getDomainModelReference(), parent);
387:                                         return (T) mock;
388:                                 }
389:                                 return super.getServiceWithoutLog(serviceType);
390:                         }
391:                 };
392:                 ViewModelContextFactory.INSTANCE.createViewModelContext(view, parent);
393:
394:                 /* act */
395:                 view.getChildren().add(vControl);
396:
397:                 /* assert no NPE */
398:         }
399:
400:         @Test
401:         public void testValidate_Iterable() {
402:                 final AtomicReference<Collection<EObject>> validated = new AtomicReference<>();
403:
404:                 // CHECKSTYLE.OFF: AnonInnerLength - it's a test
405:                 final ValidationService validationService = new ValidationService() {
406:
407:                         @Override
408:                         public void validate(Collection<EObject> eObjects) {
409:                                 validated.set(eObjects);
410:                         }
411:
412:                         @Override
413:                         public void instantiate(ViewModelContext context) {
414:                                 // Not interesting
415:                         }
416:
417:                         @Override
418:                         public int getPriority() {
419:                                 // Not interesting
420:                                 return 0;
421:                         }
422:
423:                         @Override
424:                         public void dispose() {
425:                                 // Not interesting
426:                         }
427:
428:                         @Override
429:                         public void childViewModelContextAdded(ViewModelContext childContext) {
430:                                 // Not interesting
431:                         }
432:
433:                         @Override
434:                         public void removeValidationProvider(ValidationProvider validationProvider, boolean revalidate) {
435:                                 // Not interesting
436:                         }
437:
438:                         @Override
439:                         public void removeValidationProvider(ValidationProvider validationProvider) {
440:                                 // Not interesting
441:                         }
442:
443:                         @Override
444:                         public void registerValidationListener(ViewValidationListener listener) {
445:                                 // TODO Auto-generated method stub
446:                         }
447:
448:                         @Override
449:                         public void deregisterValidationListener(ViewValidationListener listener) {
450:                                 // Not interesting
451:                         }
452:
453:                         @Override
454:                         public void addValidationProvider(ValidationProvider validationProvider, boolean revalidate) {
455:                                 // Not interesting
456:                         }
457:
458:                         @Override
459:                         public void addValidationProvider(ValidationProvider validationProvider) {
460:                                 // Not interesting
461:                         }
462:                 };
463:                 // CHECKSTYLE.ON: AnonInnerLength
464:
465:                 final List<EObject> eObjects = Arrays.asList(
466:                         mock(EObject.class, "a"),
467:                         mock(EObject.class, "b"),
468:                         mock(EObject.class, "c"));
469:
470:                 final Iterable<EObject> iterable = eObjects::iterator;
471:                 validationService.validate(iterable);
472:
473:                 assertThat("No collection passed to validation service", validated.get(), notNullValue());
474:                 assertThat("Collection has wrong size", validated.get().size(), is(eObjects.size()));
475:                 assertThat("Collection has wrong elements", new ArrayList<>(validated.get()), equalTo(eObjects));
476:         }
477:
478: }