Skip to content

Package: ValidationBuilder_PTest

ValidationBuilder_PTest

nameinstructionbranchcomplexitylinemethod
ValidationBuilder_PTest()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
delegateProviders()
M: 0 C: 58
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
deregisterServices()
M: 0 C: 15
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getBundleContext()
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
injections()
M: 0 C: 44
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
markerHelperProviders()
M: 0 C: 65
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 15
100%
M: 0 C: 1
100%
noAutoBuild()
M: 0 C: 33
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
notAModelValidationProject()
M: 0 C: 34
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
register(Class, Object)
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
registerDataTemplateDelegate()
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
registerMarkerHelper()
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
validProject()
M: 0 C: 33
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
validationErrors()
M: 0 C: 53
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
validationProblemsFixed()
M: 0 C: 70
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 20
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 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: * EclipseSource - initial API and implementation
13: * Christian W. Damus - bugs 544499, 545418
14: ******************************************************************************/
15: package org.eclipse.emfforms.ide.builder;
16:
17: import static org.hamcrest.CoreMatchers.is;
18: import static org.hamcrest.CoreMatchers.startsWith;
19: import static org.hamcrest.MatcherAssert.assertThat;
20: import static org.hamcrest.Matchers.greaterThan;
21: import static org.hamcrest.Matchers.greaterThanOrEqualTo;
22: import static org.hamcrest.Matchers.notNullValue;
23:
24: import java.io.IOException;
25: import java.io.InputStream;
26: import java.util.ArrayList;
27: import java.util.Collections;
28: import java.util.Hashtable;
29: import java.util.List;
30:
31: import javax.inject.Named;
32:
33: import org.eclipse.core.resources.IFile;
34: import org.eclipse.core.resources.IMarker;
35: import org.eclipse.core.resources.IProject;
36: import org.eclipse.core.runtime.CoreException;
37: import org.eclipse.core.runtime.IProgressMonitor;
38: import org.eclipse.core.runtime.NullProgressMonitor;
39: import org.eclipse.core.runtime.content.IContentType;
40: import org.eclipse.emf.common.ui.MarkerHelper;
41: import org.eclipse.emf.common.util.Diagnostic;
42: import org.eclipse.emfforms.bazaar.Bid;
43: import org.eclipse.emfforms.bazaar.Create;
44: import org.eclipse.emfforms.ide.internal.builder.ProjectNature;
45: import org.eclipse.emfforms.ide.internal.builder.ValidationBuilder;
46: import org.eclipse.emfforms.ide.internal.builder.ValidationNature;
47: import org.junit.After;
48: import org.junit.Assert;
49: import org.junit.Test;
50: import org.osgi.framework.BundleContext;
51: import org.osgi.framework.FrameworkUtil;
52: import org.osgi.framework.ServiceRegistration;
53:
54: /**
55: * Test for the {@link ValidationBuilder} class.
56: */
57: public class ValidationBuilder_PTest extends AbstractBuilderTest {
58:
59:         private final List<ServiceRegistration<?>> registrations = new ArrayList<>();
60:
61:         @Test
62:         public void validProject() throws CoreException, IOException {
63:                 // initial state
64:                 final String projectName = "ValidModel"; //$NON-NLS-1$
65:                 final IProgressMonitor monitor = new NullProgressMonitor();
66:                 final IProject project = createAndPopulateProject(projectName, monitor);
67:                 IMarker[] markers = findMarkersOnResource(project);
68:                 // No build yet => no markers
69:                 Assert.assertArrayEquals(Collections.<IMarker> emptyList().toArray(), markers);
70:
71:                 // trigger builder by adding nature to the project and auto-build is on
72:                 setAutoBuild(true);
73:                 ProjectNature.toggleNature(project, ValidationNature.NATURE_ID);
74:                 waitForAutoBuild();
75:
76:                 // final state
77:                 markers = findMarkersOnResource(project);
78:                 // valid Files => No marker
79:                 Assert.assertArrayEquals(Collections.<IMarker> emptyList().toArray(), markers);
80:         }
81:
82:         @Test
83:         public void notAModelValidationProject() throws CoreException, IOException {
84:                 final String projectName = "NotAViewModel"; //$NON-NLS-1$
85:                 final IProgressMonitor monitor = new NullProgressMonitor();
86:                 final IProject project = createAndPopulateProject(projectName, monitor);
87:                 IMarker[] markers = findMarkersOnResource(project);
88:                 // No build yet => no markers
89:                 Assert.assertArrayEquals(Collections.<IMarker> emptyList().toArray(), markers);
90:
91:                 // trigger builder by adding nature to the project and auto-build is on
92:                 setAutoBuild(true);
93:                 ProjectNature.toggleNature(project, ValidationNature.NATURE_ID);
94:                 waitForAutoBuild();
95:
96:                 // final state
97:                 markers = findMarkersOnResource(project);
98:                 // no view files (wrong XMI, not XML file, etc.) => Mark them with an error.
99:                 Assert.assertEquals(2, markers.length);
100:         }
101:
102:         /**
103:          * Test that problem markers are correctly created for resources that have validation errors.
104:          */
105:         @Test
106:         public void validationErrors() throws CoreException, IOException {
107:                 final String projectName = "ValidationErrors";//$NON-NLS-1$
108:                 final IProgressMonitor monitor = new NullProgressMonitor();
109:                 final IProject project = createAndPopulateProject(projectName, monitor);
110:                 IMarker[] markers = findMarkersOnResource(project);
111:                 // No build yet => no markers
112:                 Assert.assertArrayEquals(Collections.<IMarker> emptyList().toArray(), markers);
113:
114:                 // trigger builder by adding nature to the project and auto-build is on
115:                 setAutoBuild(true);
116:                 ProjectNature.toggleNature(project, ValidationNature.NATURE_ID);
117:                 waitForAutoBuild();
118:
119:                 // final state
120:                 markers = findMarkersOnResource(project);
121:
122:                 // 4 errors:
123:                 // 2 unresolved DMR and one missing DMR as ECP pure validation errros
124:                 // an annotation with a missing key as a simple EMF error
125:•                for (final IMarker marker : markers) {
126:                         System.err.println(marker);
127:                 }
128:                 Assert.assertEquals(4, markers.length);
129:         }
130:
131:         /**
132:          * Test that problem markers are correctly <em>not</em> created for resources
133:          * even that have validation errors, when auto-build is not enabled.
134:          */
135:         @Test
136:         public void noAutoBuild() throws CoreException, IOException {
137:                 final String projectName = "ValidationErrors"; //$NON-NLS-1$
138:                 final IProgressMonitor monitor = new NullProgressMonitor();
139:                 final IProject project = createAndPopulateProject(projectName, monitor);
140:                 IMarker[] markers = findMarkersOnResource(project);
141:                 // No build yet => no markers
142:                 Assert.assertArrayEquals(Collections.<IMarker> emptyList().toArray(), markers);
143:
144:                 // trigger builder by adding nature to the project and auto-build is off,
145:                 // which means that it won't do validation
146:                 setAutoBuild(false);
147:                 ProjectNature.toggleNature(project, ValidationNature.NATURE_ID);
148:                 waitForAutoBuild();
149:
150:                 // final state
151:                 markers = findMarkersOnResource(project);
152:                 // valid Files => No marker
153:                 Assert.assertArrayEquals(Collections.<IMarker> emptyList().toArray(), markers);
154:         }
155:
156:         @Test
157:         public void delegateProviders() throws CoreException, IOException {
158:                 registerDataTemplateDelegate();
159:
160:                 final String projectName = "DataTemplateErrors";//$NON-NLS-1$
161:                 final IProgressMonitor monitor = new NullProgressMonitor();
162:                 final IProject project = createAndPopulateProject(projectName, monitor);
163:                 IMarker[] markers = findMarkersOnResource(project);
164:                 // No build yet => no markers
165:                 Assert.assertArrayEquals(Collections.<IMarker> emptyList().toArray(), markers);
166:
167:                 // trigger builder by adding nature to the project and auto-build is on
168:                 setAutoBuild(true);
169:                 ProjectNature.toggleNature(project, ValidationNature.NATURE_ID);
170:                 waitForAutoBuild();
171:
172:                 // final state
173:                 markers = findMarkersOnResource(project);
174:
175:                 // 4 errors:
176:                 // 2 unresolved DMR and one missing DMR as ECP pure validation errros
177:                 // an annotation with a missing key as a simple EMF error
178:•                for (final IMarker marker : markers) {
179:                         System.err.println(marker);
180:                 }
181:                 Assert.assertThat(markers.length, greaterThanOrEqualTo(1));
182:         }
183:
184:         @Test
185:         public void markerHelperProviders() throws CoreException, IOException {
186:                 registerMarkerHelper();
187:
188:                 final String projectName = "ValidationErrors";//$NON-NLS-1$
189:                 final IProgressMonitor monitor = new NullProgressMonitor();
190:                 final IProject project = createAndPopulateProject(projectName, monitor);
191:                 IMarker[] markers = findMarkersOnResource(project);
192:                 // No build yet => no markers
193:                 Assert.assertArrayEquals(Collections.<IMarker> emptyList().toArray(), markers);
194:
195:                 // trigger builder by adding nature to the project and auto-build is on
196:                 setAutoBuild(true);
197:                 ProjectNature.toggleNature(project, ValidationNature.NATURE_ID);
198:                 waitForAutoBuild();
199:
200:                 // final state
201:                 markers = findMarkersOnResource(project);
202:
203:                 // 4 errors:
204:                 // 2 unresolved DMR and one missing DMR as ECP pure validation errros
205:                 // an annotation with a missing key as a simple EMF error
206:•                for (final IMarker marker : markers) {
207:                         System.err.println(marker);
208:
209:                         // These look like they were created by our marker helper
210:                         Assert.assertThat(marker.getAttribute(IMarker.MESSAGE, ""), startsWith("*TEST* "));
211:                 }
212:                 Assert.assertThat(markers.length, greaterThanOrEqualTo(1));
213:         }
214:
215:         @Test
216:         public void injections() throws CoreException, IOException {
217:                 class Canary implements ValidationDelegateProvider {
218:                         // BEGIN COMPLEX CODE - This is just for testing
219:                         String fileName;
220:                         IContentType contentType;
221:                         String contentTypeID;
222:                         // END COMPLEX CODE
223:
224:                         @Bid
225:                         public Double bid(IFile file, IContentType contentType,
226:                                 @Named(BuilderConstants.CONTENT_TYPE) String contentTypeID) {
227:                                 fileName = file.getName();
228:                                 this.contentType = contentType;
229:                                 this.contentTypeID = contentTypeID;
230:                                 return null;
231:                         }
232:
233:                         void dump() {
234:                                 System.err.printf("File name: %s%n", fileName);
235:                                 System.err.printf("Content-type ID: %s%n", contentTypeID);
236:                                 System.err.printf("Content-type: %s%n", contentType);
237:                         }
238:                 }
239:
240:                 final Canary canary = new Canary();
241:                 register(ValidationDelegateProvider.class, canary);
242:
243:                 final String projectName = "DataTemplateErrors";//$NON-NLS-1$
244:                 final IProgressMonitor monitor = new NullProgressMonitor();
245:                 final IProject project = createAndPopulateProject(projectName, monitor);
246:
247:                 // trigger builder by adding nature to the project and auto-build is on
248:                 setAutoBuild(true);
249:                 ProjectNature.toggleNature(project, ValidationNature.NATURE_ID);
250:                 waitForAutoBuild();
251:
252:                 canary.dump();
253:                 Assert.assertThat("No file", canary.fileName, notNullValue());
254:                 Assert.assertThat("No content-type ID", canary.contentTypeID, notNullValue());
255:                 Assert.assertThat("No content-type", canary.contentType, notNullValue());
256:         }
257:
258:         /**
259:          * Test that markers are correctly cleared on incremental validation when
260:          * problems are fixed.
261:          */
262:         @Test
263:         public void validationProblemsFixed() throws CoreException, IOException {
264:                 final String projectName = "ValidationErrors";//$NON-NLS-1$
265:                 final IProgressMonitor monitor = new NullProgressMonitor();
266:                 final IProject project = createAndPopulateProject(projectName, monitor);
267:                 IMarker[] markers = findMarkersOnResource(project);
268:                 // No build yet => no markers
269:                 assertThat("Should not have error markers, yet", markers, is(new IMarker[0]));
270:
271:                 // trigger builder by adding nature to the project and auto-build is on
272:                 setAutoBuild(true);
273:                 ProjectNature.toggleNature(project, ValidationNature.NATURE_ID);
274:                 waitForAutoBuild();
275:
276:                 // Should have some problems, now
277:                 markers = findMarkersOnResource(project);
278:                 assertThat("No problems", markers.length, greaterThan(0));
279:
280:                 // Fix them
281:                 final IFile problemFile = project.getFile("ValidationError.view");
282:                 final IFile goodFile = project.getFile("goodModel.view");
283:                 final InputStream input = goodFile.getContents();
284:                 try {
285:                         problemFile.setContents(input, true, true, null);
286:                 } finally {
287:                         input.close();
288:                 }
289:
290:                 waitForAutoBuild();
291:                 markers = findMarkersOnResource(project);
292:                 // Problems solved
293:                 assertThat("Should not have error markers", markers, is(new IMarker[0]));
294:         }
295:
296:         //
297:         // Test framework
298:         //
299:
300:         @After
301:         public void deregisterServices() {
302:•                for (final ServiceRegistration<?> next : registrations) {
303:                         next.unregister();
304:                 }
305:         }
306:
307:         BundleContext getBundleContext() {
308:                 return FrameworkUtil.getBundle(getClass()).getBundleContext();
309:         }
310:
311:         <S> ServiceRegistration<S> register(Class<S> serviceType, S service) {
312:                 final ServiceRegistration<S> result = getBundleContext().registerService(
313:                         serviceType, service, new Hashtable<>());
314:                 registrations.add(result);
315:                 return result;
316:         }
317:
318:         ServiceRegistration<ValidationDelegateProvider> registerDataTemplateDelegate() {
319:                 final ValidationDelegateProvider provider = new ValidationDelegateProvider() {
320:                         private final Double bid = Double.MAX_VALUE;
321:
322:                         @Bid
323:                         public Double bid(IFile file) {
324:                                 return "datatemplate".equals(file.getFullPath().getFileExtension())
325:                                         ? bid
326:                                         : null;
327:                         }
328:
329:                         @Create
330:                         public ValidationDelegate createDelegate() {
331:                                 return new ValidationServiceDelegate();
332:                         }
333:                 };
334:
335:                 return register(ValidationDelegateProvider.class, provider);
336:         }
337:
338:         ServiceRegistration<MarkerHelperProvider> registerMarkerHelper() {
339:                 final IFile[] fileHolder = { null };
340:                 final MarkerHelper markerHelper = new DefaultMarkerHelper() {
341:                         @Override
342:                         protected void adjustMarker(IMarker marker, Diagnostic diagnostic, Diagnostic parentDiagnostic)
343:                                 throws CoreException {
344:
345:                                 super.adjustMarker(marker, diagnostic, parentDiagnostic);
346:
347:                                 String message = marker.getAttribute(IMarker.MESSAGE, "");
348:                                 message = "*TEST* " + message;
349:                                 marker.setAttribute(IMarker.MESSAGE, message);
350:                         }
351:
352:                         @Override
353:                         protected IFile getFile(Diagnostic diagnostic) {
354:                                 return fileHolder[0];
355:                         }
356:                 };
357:
358:                 final MarkerHelperProvider provider = new MarkerHelperProvider() {
359:                         private final Double bid = Double.MAX_VALUE;
360:
361:                         @Bid
362:                         public Double bid(IFile file) {
363:                                 return "view".equals(file.getFullPath().getFileExtension())
364:                                         ? bid
365:                                         : null;
366:                         }
367:
368:                         @Create
369:                         public MarkerHelper createDelegate(IFile file) {
370:                                 fileHolder[0] = file;
371:                                 return markerHelper;
372:                         }
373:                 };
374:
375:                 return register(MarkerHelperProvider.class, provider);
376:         }
377:
378: }