Skip to main content
summaryrefslogtreecommitdiffstats
blob: d7ee1a4a927ecd507757125167408e5bd82d122c (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*****************************************************************************
 * Copyright (c) 2013 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:
 *  Remi Schnekenburger (CEA LIST) - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.profile.externalresource.tests;

import java.util.Collection;
import java.util.HashSet;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.papyrus.infra.core.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.infra.core.resource.ModelMultiException;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.resource.ModelsReader;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServiceMultiException;
import org.eclipse.papyrus.infra.core.services.ServiceNotFoundException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.core.utils.ServiceUtils;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForResourceInitializerService;
import org.eclipse.pde.core.plugin.IPluginModelBase;
import org.eclipse.pde.internal.core.PDECore;
import org.eclipse.pde.internal.ui.wizards.imports.PluginImportOperation;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.NamedElement;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;

/**
 * Abstract class for all tests in this plugin
 */
public abstract class AbstractExternalResourcesTest {

	/** services registry for the model under test */
	protected ServicesRegistry servicesRegistry;

	/** model set that contains the model under test */
	protected ModelSet modelSet;

	/** The papyrus editor. */
	protected IMultiDiagramEditor papyrusEditor;

	public static boolean isInitialized = false;

	public static IEditorPart editor = null;

	public static String editorID = "org.eclipse.papyrus.infra.core.papyrusEditor";

	public static final String EXTERNAL_RESOURCES_TEST_PROFILE_SUB_PROFILE = "ExternalResourcesTestProfile::SubProfile";
	public static final String EXTERNAL_RESOURCES_TEST_PROFILE = "ExternalResourcesTestProfile";
	public static final String MODEL_CLASS1 = "Class1";
	public static final String CLASS_STEREOTYPE_NAME = "ClassStereotype";
	public static final String CLASS_STEREOTYPE_QN = EXTERNAL_RESOURCES_TEST_PROFILE+"::"+CLASS_STEREOTYPE_NAME;

	public static final String MODEL_CLASS2 = "Class2";
	public static final String ELEMENT_STEREOTYPE_NAME = "ElementStereotype";
	public static final String ELEMENT_STEREOTYPE_QN = EXTERNAL_RESOURCES_TEST_PROFILE+NamedElement.SEPARATOR+EXTERNAL_RESOURCES_TEST_PROFILE_SUB_PROFILE+NamedElement.SEPARATOR+ELEMENT_STEREOTYPE_NAME;
	
	@Before
	public void initializeRegistry() {
		// first of all, delete existing model
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		IWorkspaceRoot root = workspace.getRoot();
		IProject project = root.getProject(Activator.PLUGIN_ID);
		//at this point, no resources have been created
			if(project.exists()) {
				try {
					project.delete(true, true, new NullProgressMonitor());
				} catch (CoreException e) {
					Assert.fail(e.getMessage());
				}
			}
		
		// import model files.
		PluginImportOperation operation = new PluginImportOperation(new IPluginModelBase[] {PDECore.getDefault().getModelManager().findModel(Activator.PLUGIN_ID)} , PluginImportOperation.IMPORT_WITH_SOURCE, false);
		IStatus status = operation.run(new NullProgressMonitor());
		if(IStatus.ERROR == status.getSeverity()) {
			Assert.fail(status.getMessage());
		}
		servicesRegistry = getServicesRegistry();
		modelSet = getModelSet();
	}

	@After
	public void tearDownRegistry() {
		if(modelSet != null) {
			modelSet.unload();
			modelSet = null;
		}
		if(servicesRegistry != null) {
			try {
				servicesRegistry.disposeRegistry();
				servicesRegistry = null;
			} catch (ServiceMultiException e) {
				Activator.log.error(e);
				org.junit.Assert.fail(e.getMessage());
			}
		}
	}

	public ModelSet createModelSet(URI uri) throws ModelMultiException {
		ModelSet modelSet = new ModelSet();
		ModelsReader reader = new ModelsReader();
		reader.readModel(modelSet);
		modelSet.loadModels(uri);
		return modelSet;
	}

	protected ServicesRegistry getUpdatedServiceRegistry() {
		if(papyrusEditor != null) {
			ServicesRegistry registry = (ServicesRegistry)papyrusEditor.getAdapter(ServicesRegistry.class);
			return registry;
		}
		return servicesRegistry;
	}

	protected Collection<IEditorPart> getEditors() {
		Collection<IEditorPart> results = new HashSet<IEditorPart>();
		IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
		for(IWorkbenchWindow iWorkbenchWindow : windows) {
			IWorkbenchPage[] pages = iWorkbenchWindow.getPages();
			for(IWorkbenchPage iWorkbenchPage : pages) {
				IEditorReference[] references = iWorkbenchPage.getEditorReferences();
				for(IEditorReference ref : references) {
					IEditorPart editor = ref.getEditor(true);
					results.add(editor);
				}
			}
		}
		return results;
	}

	public ServicesRegistry getServicesRegistry() {
		if(servicesRegistry == null) {
			//Try to find existing
			ServicesRegistry registry = getUpdatedServiceRegistry();
			if(registry != null) {
				servicesRegistry = registry;
			} else {
				servicesRegistry = createServicesRegistry();
			}
		}
		return servicesRegistry;
	}

	/**
	 * Starts a new Service registry
	 * 
	 * @return
	 */
	protected ServicesRegistry createServicesRegistry() {
		try {
			ServicesRegistry serviceRegistry = new ServicesRegistry();
			serviceRegistry.startRegistry();
			return serviceRegistry;
		} catch (ServiceException e) {
			Activator.log.error(e);
		}
		return null;
	}

	public ModelSet getModelSet() {
		//If null, try to find one or create one
		if(modelSet == null) {
			try {
				modelSet = ServiceUtils.getInstance().getModelSet(getServicesRegistry());
			} catch (ServiceException e) {
				//Create one
				try {
					modelSet = createModelSet(getURI());
					getServicesRegistry().add(ModelSet.class, 10, modelSet);
					getServicesRegistry().add(ServiceUtilsForResourceInitializerService.class, 10, new ServiceUtilsForResourceInitializerService());
					getServicesRegistry().startServicesByClassKeys(ModelSet.class, ServiceUtilsForResourceInitializerService.class);
				} catch (ModelMultiException modelMultiException) {
					Activator.log.error(modelMultiException);
				} catch (ServiceMultiException e1) {
					Activator.log.error(e1);
				} catch (ServiceNotFoundException e1) {
					Activator.log.error(e1);
				}
			}
		}
		return modelSet;
	}

	/**
	 * @return the uri
	 */
	public abstract  URI getURI();
}

Back to the top