Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5238006842b593851eab679c3aa7a86dcaa0d11f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*****************************************************************************
 * Copyright (c) 2019 CEA LIST, and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Nicolas FAUVERGUE (CEA LIST) nicolas.fauvergue@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.toolsmiths.validation.profile.tests;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.papyrus.junit.framework.classification.tests.AbstractPapyrusTest;
import org.eclipse.papyrus.junit.utils.rules.ProjectFixture;
import org.eclipse.papyrus.toolsmiths.validation.profile.checkers.ProfilePluginCheckerService;
import org.eclipse.papyrus.toolsmiths.validation.profile.constants.ProfilePluginValidationConstants;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

/**
 * This class allows to test the profile plug-in validation.
 * This will check the markers resulting from a failing profile plug-in definition.
 */
public class ProfilePluginValidationTest extends AbstractPapyrusTest {

	/**
	 * The plug-in path to copy into the project fixture
	 */
	private static final String PLUGIN_PATH = "resources/org.eclipse.papyrus.toolsmiths.validation.profile.example";//$NON-NLS-1$

	/**
	 * The severity attribute identifier.
	 */
	private static final String SEVERITY_ID = "severity"; //$NON-NLS-1$

	/**
	 * The project fixture to manage easily the project.
	 */
	@Rule
	public final ProjectFixture fixture = new ProjectFixture();

	/**
	 * This allows to copy the project into the fixture project.
	 */
	@Before
	public void init() {
		try {
			fixture.copyFolder(ProfilePluginValidationTest.class, PLUGIN_PATH);
		} catch (IOException e) {
			Assert.fail("Error while copying project"); //$NON-NLS-1$
		}
	}

	/**
	 * This allows to test the profile plug-in validation.
	 */
	@Test
	public void testProfilePluginValidation() {
		// First, run the validation
		ProfilePluginCheckerService.checkProfilePlugin(fixture.getProject());

		// Get the markers
		List<IMarker> markers = null;
		try {
			markers = Arrays.asList(fixture.getProject().findMarkers(ProfilePluginValidationConstants.PROFILE_PLUGIN_VALIDATION_TYPE, true, IResource.DEPTH_INFINITE));
		} catch (CoreException e) {
			Assert.fail("Error with resource"); //$NON-NLS-1$
		}

		// Now check the markers
		Assert.assertNotNull("The markers have to be found", markers); //$NON-NLS-1$
		Assert.assertEquals("The number of markers is not correct", 5, markers.size()); //$NON-NLS-1$

		// Check the profile.uml markers
		final List<IMarker> profileFileMarkers = markers.stream().filter(marker -> marker.getResource().getFullPath().toString().endsWith("bookstore.profile.uml")).collect(Collectors.toList()); //$NON-NLS-1$
		Assert.assertNotNull("Profile file markers are not found", profileFileMarkers); //$NON-NLS-1$
		Assert.assertEquals("The number of markers for profile file is not correct", 1, profileFileMarkers.size()); //$NON-NLS-1$
		Assert.assertTrue("The severity of profile marker is not correct", isMarkerSeverity(profileFileMarkers.get(0), IMarker.SEVERITY_ERROR)); //$NON-NLS-1$

		// Check the dependencies markers
		final List<IMarker> manifestMarkers = markers.stream().filter(marker -> marker.getResource().getFullPath().toString().endsWith("MANIFEST.MF")).collect(Collectors.toList()); //$NON-NLS-1$
		Assert.assertNotNull("Dependencies markers are not found", manifestMarkers); //$NON-NLS-1$
		Assert.assertEquals("The number of markers for dependencies is not correct", 1, manifestMarkers.size()); //$NON-NLS-1$
		Assert.assertTrue("The severity of profile marker is not correct", isMarkerSeverity(manifestMarkers.get(0), IMarker.SEVERITY_WARNING)); //$NON-NLS-1$

		// Check the build markers
		final List<IMarker> buildMarkers = markers.stream().filter(marker -> marker.getResource().getFullPath().toString().endsWith("build.properties")).collect(Collectors.toList()); //$NON-NLS-1$
		Assert.assertNotNull("Build markers are not found", buildMarkers); //$NON-NLS-1$
		Assert.assertEquals("The number of markers for build is not correct", 1, buildMarkers.size()); //$NON-NLS-1$
		Assert.assertTrue("The severity of profile marker is not correct", isMarkerSeverity(buildMarkers.get(0), IMarker.SEVERITY_ERROR)); //$NON-NLS-1$

		// Check the extensions markers
		final List<IMarker> extensionsMarkers = markers.stream().filter(marker -> marker.getResource().getFullPath().toString().endsWith("plugin.xml")).collect(Collectors.toList()); //$NON-NLS-1$
		Assert.assertNotNull("Extensions markers are not found", extensionsMarkers); //$NON-NLS-1$
		Assert.assertEquals("The number of markers for extensions is not correct", 2, extensionsMarkers.size()); //$NON-NLS-1$
		final List<IMarker> warningExtensionsMarkers = extensionsMarkers.stream().filter(marker -> isMarkerSeverity(marker, IMarker.SEVERITY_WARNING)).collect(Collectors.toList());
		Assert.assertEquals("The number of warning markers for extensions is not correct", 1, warningExtensionsMarkers.size()); //$NON-NLS-1$
		final List<IMarker> errorExtensionsMarkers = extensionsMarkers.stream().filter(marker -> isMarkerSeverity(marker, IMarker.SEVERITY_ERROR)).collect(Collectors.toList());
		Assert.assertEquals("The number of error markers for extensions is not correct", 1, errorExtensionsMarkers.size()); //$NON-NLS-1$

	}

	/**
	 * This allows to determinate if a marker got the correct severity.
	 *
	 * @param marker
	 *            the marker.
	 * @param severity
	 *            The severity to get.
	 * @return <code>true</code> if the marker got the correct severity, <code>false</code> otherwise.
	 */
	private boolean isMarkerSeverity(final IMarker marker, final int severity) {
		try {
			return severity == (int) marker.getAttribute(SEVERITY_ID);
		} catch (CoreException e) {
			Assert.fail("Error while getting " + SEVERITY_ID); //$NON-NLS-1$
			return false;
		}
	}

}

Back to the top