Skip to content

Package: AbstractBuilderTest$1

AbstractBuilderTest$1

nameinstructionbranchcomplexitylinemethod
featureValueOf(IMarker)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
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) 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, 548592
14: ******************************************************************************/
15: package org.eclipse.emfforms.ide.builder;
16:
17: import static org.junit.Assert.fail;
18:
19: import java.io.File;
20: import java.io.FileInputStream;
21: import java.io.IOException;
22: import java.io.InputStream;
23:
24: import org.eclipse.core.resources.IFile;
25: import org.eclipse.core.resources.IMarker;
26: import org.eclipse.core.resources.IProject;
27: import org.eclipse.core.resources.IResource;
28: import org.eclipse.core.resources.IWorkspaceDescription;
29: import org.eclipse.core.resources.IWorkspaceRoot;
30: import org.eclipse.core.resources.ResourcesPlugin;
31: import org.eclipse.core.runtime.CoreException;
32: import org.eclipse.core.runtime.IProgressMonitor;
33: import org.eclipse.core.runtime.NullProgressMonitor;
34: import org.eclipse.core.runtime.OperationCanceledException;
35: import org.eclipse.core.runtime.Path;
36: import org.eclipse.core.runtime.jobs.Job;
37: import org.hamcrest.FeatureMatcher;
38: import org.hamcrest.Matcher;
39: import org.junit.Before;
40:
41: /**
42: * Abstract framework for builder tests.
43: */
44: public abstract class AbstractBuilderTest {
45:
46:         @Before
47:         public void deleteProjects() throws CoreException {
48:                 final IProgressMonitor monitor = new NullProgressMonitor();
49:                 final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
50:                 for (final IProject project : root.getProjects()) {
51:                         project.delete(true, monitor);
52:                 }
53:         }
54:
55:         protected static void setAutoBuild(boolean autoBuild) throws CoreException {
56:                 final IWorkspaceDescription description = ResourcesPlugin.getWorkspace().getDescription();
57:                 description.setAutoBuilding(autoBuild);
58:                 ResourcesPlugin.getWorkspace().setDescription(description);
59:         }
60:
61:         protected IMarker[] findMarkersOnResource(IResource resource) {
62:                 IMarker[] problems = null;
63:                 final int depth = IResource.DEPTH_INFINITE;
64:                 try {
65:                         problems = resource.findMarkers(IMarker.PROBLEM, true, depth);
66:                 } catch (final CoreException e) {
67:                         // something went wrong
68:                 }
69:                 return problems;
70:         }
71:
72:         protected static IProject createProject(String name, IProgressMonitor monitor) throws CoreException {
73:                 final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
74:                 final IProject project = root.getProject(name);
75:                 project.create(monitor);
76:                 project.open(monitor);
77:                 return project;
78:         }
79:
80:         protected static IFile importFileIntoProject(IProject project, File file, IProgressMonitor monitor)
81:                 throws CoreException, IOException {
82:                 final IFile targetResource = project.getFile(new Path(file.getName()));
83:                 final InputStream contentStream = new FileInputStream(file);
84:                 targetResource.create(contentStream, false, monitor);
85:                 contentStream.close();
86:                 return targetResource;
87:
88:         }
89:
90:         protected static IProject createAndPopulateProject(String projectName, IProgressMonitor monitor)
91:                 throws CoreException, IOException {
92:                 final IProject project = createProject(projectName, monitor);
93:                 // copy content of the resources equivalent folder
94:                 final String folderName = String.format("/resources/%s/", projectName);//$NON-NLS-1$
95:                 final String resourceFolderPath = new File(".").getAbsolutePath() + folderName;//$NON-NLS-1$
96:                 final File resourceFolder = new File(resourceFolderPath);
97:                 for (final File file : resourceFolder.listFiles()) {
98:                         importFileIntoProject(project, file, monitor);
99:                 }
100:                 return project;
101:         }
102:
103:         protected static void waitForAutoBuild() {
104:                 try {
105:                         Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_BUILD, null);
106:                 } catch (InterruptedException | OperationCanceledException e) {
107:                         e.printStackTrace();
108:                         fail("Test interrupted waiting for workspace build: " + e.getMessage()); //$NON-NLS-1$
109:                 }
110:         }
111:
112:         /**
113:          * Obtain a matcher that asserts the value of an attribute of a marker.
114:          *
115:          * @param name the name of the attribute to verify
116:          * @param matcher a matcher to test the attribute value
117:          * @return the marker matcher
118:          */
119:         protected static Matcher<IMarker> hasAttributeThat(String name, Matcher<String> matcher) {
120:                 return new FeatureMatcher<IMarker, String>(matcher, String.format("marker with attribute '%s' that", name),
121:                         name) {
122:                         @Override
123:                         protected String featureValueOf(IMarker actual) {
124:                                 return actual.getAttribute(name, null);
125:                         }
126:                 };
127:         }
128:
129: }