Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1e9839b3f7d9fd5cd24d3aed96b4162d32bf25c0 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*****************************************************************************
 * Copyright (c) 2014 CEA LIST.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * (CEA LIST) - Initial API and implementation
 *****************************************************************************/

package org.eclipse.papyrus.moka.tests.semantics;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.Launch;
import org.eclipse.papyrus.infra.core.resource.AbstractBaseModel;
import org.eclipse.papyrus.infra.core.resource.IModel;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.junit.utils.tests.AbstractEditorTest;
import org.eclipse.papyrus.moka.MokaConstants;
import org.eclipse.papyrus.moka.launch.MokaLaunchDelegate;
import org.eclipse.papyrus.moka.tests.AbstractMokaLaunchConfigurationDelegate;
import org.eclipse.papyrus.moka.tests.Activator;
import org.eclipse.swt.widgets.Display;
import org.eclipse.uml2.uml.Activity;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Stereotype;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * @author AC221913
 *
 */
public abstract class AbstractMokaTest extends AbstractEditorTest {

	public static final String RESOURCES_PATH = "/resources"; //$NON-NLS-1$

	public static final String MODEL_NAME = "/TestSuite"; //$NON-NLS-1$

	public static final String PROJECT_NAME = "org.eclipse.papyrus.moka.tests"; //$NON-NLS-1$

	public Model model;

	public Activity activity;

	/**
	 * This method initialize the model element from the project.
	 * First, it creates a new project and copy papyrus file (.di, .notation, .uml).
	 * Then, it opens the model.
	 *
	 */
	@Before
	public void initModelForTestReport() {
		try {
			MokaConstants.MOKA_AUTOMATIC_ANIMATION = false;
			MokaConstants.SILENT_MODE = true;
			initModel(PROJECT_NAME, MODEL_NAME, Activator.getDefault().getBundle());
		} catch (Exception e) {
			Assert.fail(e.getMessage());
		}
	}

	@Override
	protected String getSourcePath() {
		// TODO Auto-generated method stub
		return RESOURCES_PATH;
	}

	@Test
	/**
	 * This is the "@Test" method.
	 * First, gets the model loaded in the model explorer,
	 * Then, gets the activity to execute,
	 * At the end, creates a launch configuration and executes it by the Moka engine.
	 *
	 * @throws CoreException
	 * @throws InterruptedException
	 */
	public void runTest() throws CoreException, InterruptedException {
		/* 1. Get Activity to Execute */
		//get the rootModel
		Assert.assertNotNull("RootModel is null", getRootUMLModel()); //$NON-NLS-1$
		//get all semantic element that will handled
		model = (Model)getRootUMLModel();

		//get activity to execute
		activity = (org.eclipse.uml2.uml.Activity)model.getPackagedElement(this.getActivityName());

		/* 2. Execute it */
		// define launch configuration
		ILaunchConfiguration configurations[] = getLaunchConfgurations(project);
		ILaunch launch = new Launch(configurations[0], "debug", null);

		// Run Moka Execution with launch configuration
		AbstractMokaLaunchConfigurationDelegate mokaExecution = this.getLaunchDelegate();
		mokaExecution.launch(configurations[0], "debug", launch, new NullProgressMonitor());

		// Wait till moka execution thread is terminated
		while(!launch.isTerminated()) {
			if(Display.getCurrent() != null) {
				try {
					if(!Display.getCurrent().readAndDispatch()) {
						Thread.sleep(100);
					}
				} catch (InterruptedException ex) {
					throw ex;
				} catch (Exception ex) {
					Activator.log.error(ex); //Exception in a runnable. Log and keep going
				}

			} else {
				Thread.sleep(100);
			}
		}
	}

	/**
	 * This method returns the root package element of the model.
	 *
	 */
	@Override
	protected Package getRootUMLModel() {
		IModel umlIModel;
		try {
			umlIModel = getModelSet().getModel("org.eclipse.papyrus.infra.core.resource.uml.UmlModel");

			AbstractBaseModel umlModel = null;
			if(umlIModel instanceof AbstractBaseModel) {
				umlModel = (AbstractBaseModel)umlIModel;
			}

			Assert.assertFalse("umlRessource contains nothing", umlModel.getResource().getContents().size() < 1);
			Object root = umlModel.getResource().getContents().get(0);
			Assert.assertFalse("the root of UML model is not a package", root instanceof Stereotype);
			Assert.assertTrue("the root of UML model is not a stereotype", root instanceof Package);
			return (org.eclipse.uml2.uml.Package)root;
		} catch (ServiceException e) {
			Activator.log.error(e);
		}
		return null;

	}

	/**
	 * This method create a launch configuration for selected model
	 *
	 * @param resource
	 *        : the model
	 * @return list of launch configuration
	 * @throws CoreException
	 */
	protected ILaunchConfiguration[] getLaunchConfgurations(IResource resource) throws CoreException {
		ILaunchConfiguration configurations[];
		ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
		ILaunchConfigurationType type = manager.getLaunchConfigurationType("org.eclipse.papyrus.moka.launchConfiguration");

		ILaunchConfigurationWorkingCopy configuration = type.newInstance(null, "MOKA JUNIT TESTS");
		configuration.setAttribute(MokaLaunchDelegate.URI_ATTRIBUTE_NAME, model.eResource().getURI().toString());
		configuration.setAttribute(MokaLaunchDelegate.FRAGMENT_ATTRIBUTE_NAME, activity.eResource().getURIFragment(activity));
		configuration.setAttribute(MokaLaunchDelegate.ARGS_ATTRIBUTE_NAME, "");

		// save and return new configuration
		configuration.doSave();
		configurations = new ILaunchConfiguration[]{ configuration };

		return configurations;
	}

	/**
	 * Returns the name of the main activity to execute
	 *
	 * @return the name of the main activity to execute
	 */
	public abstract String getActivityName();


	/**
	 * Returns the launch configuration delegate to be used for this test
	 *
	 * @return the launch configuration delegate to be used for this test
	 */
	public abstract AbstractMokaLaunchConfigurationDelegate getLaunchDelegate();
}

Back to the top