Added
Link Here
|
1 |
/** |
2 |
* Copyright (c) 2007 Borland Software Corporation |
3 |
* |
4 |
* All rights reserved. This program and the accompanying materials |
5 |
* are made available under the terms of the Eclipse Public License v1.0 |
6 |
* which accompanies this distribution, and is available at |
7 |
* http://www.eclipse.org/legal/epl-v10.html |
8 |
* |
9 |
* Contributors: |
10 |
* Alexander Fedorov (Borland) - initial API and implementation |
11 |
*/ |
12 |
package org.eclipse.gmf.tests.tr; |
13 |
|
14 |
import java.io.IOException; |
15 |
import java.util.Collections; |
16 |
|
17 |
import org.eclipse.core.resources.IFile; |
18 |
import org.eclipse.core.resources.IFolder; |
19 |
import org.eclipse.core.resources.IProject; |
20 |
import org.eclipse.core.resources.ResourcesPlugin; |
21 |
import org.eclipse.core.runtime.CoreException; |
22 |
import org.eclipse.core.runtime.IPath; |
23 |
import org.eclipse.core.runtime.IStatus; |
24 |
import org.eclipse.core.runtime.NullProgressMonitor; |
25 |
import org.eclipse.emf.codegen.ecore.genmodel.GenModel; |
26 |
import org.eclipse.emf.common.util.URI; |
27 |
import org.eclipse.emf.ecore.EObject; |
28 |
import org.eclipse.emf.ecore.resource.Resource; |
29 |
import org.eclipse.emf.ecore.resource.ResourceSet; |
30 |
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
31 |
import org.eclipse.gmf.gmfgraph.Canvas; |
32 |
import org.eclipse.gmf.gmfgraph.Connection; |
33 |
import org.eclipse.gmf.gmfgraph.DiagramLabel; |
34 |
import org.eclipse.gmf.gmfgraph.Node; |
35 |
import org.eclipse.gmf.internal.bridge.transform.GenModelDetector; |
36 |
import org.eclipse.gmf.internal.bridge.transform.TransformToGenModelOperation; |
37 |
import org.eclipse.gmf.internal.common.URIUtil; |
38 |
import org.eclipse.gmf.mappings.CanvasMapping; |
39 |
import org.eclipse.gmf.mappings.GMFMapFactory; |
40 |
import org.eclipse.gmf.mappings.LinkMapping; |
41 |
import org.eclipse.gmf.mappings.Mapping; |
42 |
import org.eclipse.gmf.mappings.NodeMapping; |
43 |
import org.eclipse.gmf.mappings.TopNodeReference; |
44 |
import org.eclipse.gmf.tests.ConfiguredTestCase; |
45 |
import org.eclipse.gmf.tests.setup.DiaDefSetup; |
46 |
import org.eclipse.gmf.tests.setup.DiaDefSource; |
47 |
import org.eclipse.gmf.tests.setup.MapDefSource; |
48 |
import org.eclipse.gmf.tests.setup.ToolDefSource; |
49 |
|
50 |
|
51 |
public class TransformToGenModelOperationTest extends ConfiguredTestCase { |
52 |
|
53 |
public TransformToGenModelOperationTest(String name) { |
54 |
super(name); |
55 |
} |
56 |
|
57 |
private static String FOLDER_MODELS = "models"; //$NON-NLS-1$ |
58 |
private static String FILE_BASE = "test"; //$NON-NLS-1$ |
59 |
private static String FILE_EXT_ECORE = "ecore"; //$NON-NLS-1$ |
60 |
private static String FILE_EXT_GMFGEN = "gmfgen"; //$NON-NLS-1$ |
61 |
private static String FILE_EXT_GMFGRAPH = "gmfgraph"; //$NON-NLS-1$ |
62 |
private static String FILE_EXT_GMFMAP = "gmfmap"; //$NON-NLS-1$ |
63 |
private static String FILE_EXT_GMFTOOL = "gmftool"; //$NON-NLS-1$ |
64 |
|
65 |
private IProject myProject; |
66 |
private TransformToGenModelOperation myOperation; |
67 |
private ResourceSet myRS; |
68 |
|
69 |
/* (non-Javadoc) |
70 |
* @see junit.framework.TestCase#setUp() |
71 |
*/ |
72 |
@Override |
73 |
protected void setUp() throws Exception { |
74 |
super.setUp(); |
75 |
myOperation = new TransformToGenModelOperation(); |
76 |
myRS = new ResourceSetImpl(); |
77 |
myProject = createProject(); |
78 |
IFolder models = myProject.getFolder(FOLDER_MODELS); |
79 |
models.create(true, false, new NullProgressMonitor()); |
80 |
} |
81 |
|
82 |
/* (non-Javadoc) |
83 |
* @see org.eclipse.gmf.tests.ConfiguredTestCase#tearDown() |
84 |
*/ |
85 |
@Override |
86 |
protected void tearDown() throws Exception { |
87 |
deleteProject(); |
88 |
myProject = null; |
89 |
myOperation = null; |
90 |
super.tearDown(); |
91 |
} |
92 |
|
93 |
public void testErrorArguments() { |
94 |
URI toolURI = prepareResource(myRS, FILE_EXT_GMFTOOL, getSetup().getToolDefModel().getRegistry()); |
95 |
URI mapURI = prepareResource(myRS, FILE_EXT_GMFMAP, getSetup().getMapModel().getMapping()); |
96 |
try { |
97 |
myOperation.loadMappingModel(myRS, toolURI, null); |
98 |
fail("Invalid MappingModel should not be accepted"); |
99 |
} catch (CoreException e) { |
100 |
IStatus loadStatus = myOperation.getLoadMappingStatus(); |
101 |
assertNotNull("Load MappingModel status should be set", loadStatus); |
102 |
assertFalse("Load MappingModel status should differ from OK value", IStatus.OK == loadStatus.getSeverity()); |
103 |
assertNull("GenModelDetector should be reset", myOperation.getGenModelDetector()); |
104 |
assertNull("GenModel should be reset", myOperation.getGenModel()); |
105 |
} |
106 |
try { |
107 |
myOperation.loadMappingModel(myRS, null, null); |
108 |
fail("null gmfmap URI should not be accepted"); |
109 |
} catch (CoreException e) { |
110 |
assertIllegalArgument(e); |
111 |
} |
112 |
try { |
113 |
myOperation.loadMappingModel(null, mapURI, null); |
114 |
fail("null ResorceSet should not be accepted"); |
115 |
} catch (CoreException e) { |
116 |
assertIllegalArgument(e); |
117 |
} |
118 |
try { |
119 |
myOperation.findGenmodel(null); |
120 |
fail("null ResorceSet should not be accepted"); |
121 |
} catch (CoreException e) { |
122 |
assertIllegalArgument(e); |
123 |
} |
124 |
try { |
125 |
myOperation.loadGenModel(null, null, null); |
126 |
fail("null ResorceSet should not be accepted"); |
127 |
} catch (CoreException e) { |
128 |
assertIllegalArgument(e); |
129 |
} |
130 |
IStatus s = myOperation.executeTransformation(null, null); |
131 |
assertFalse("null ResorceSet should not be accepted", s.isOK()); |
132 |
} |
133 |
|
134 |
public void testErrorState() { |
135 |
ResourceSetImpl rs = new ResourceSetImpl(); |
136 |
IStatus s = myOperation.executeTransformation(rs, null); |
137 |
assertFalse("Target gmfgen URI should be specified", s.isOK()); |
138 |
myOperation.setGenURI(createURI(FILE_EXT_GMFGEN)); |
139 |
s = myOperation.executeTransformation(rs, null); |
140 |
assertFalse("Mapping model should be loaded before calling executeTransformation()", s.isOK()); |
141 |
|
142 |
try { |
143 |
myOperation.loadGenModel(rs, null, null); |
144 |
fail("MappingModel should be loaded before calling loadGenModel()"); |
145 |
} catch (CoreException e) { |
146 |
assertIllegalState(e); |
147 |
} |
148 |
try { |
149 |
myOperation.findGenmodel(rs); |
150 |
fail("MappingModel should be loaded before calling findGenmodel()"); |
151 |
} catch (CoreException e) { |
152 |
assertIllegalState(e); |
153 |
} |
154 |
} |
155 |
|
156 |
public void testTransformMappingModel() { |
157 |
URI mapURI = prepareResources(); |
158 |
loadMappingModel(mapURI); |
159 |
findAbsentGenmodel(); |
160 |
URI genmodelURI = createDefaultGenModel(mapURI); |
161 |
GenModel genModel = null; |
162 |
try { |
163 |
genModel = myOperation.loadGenModel(myRS, genmodelURI, null); |
164 |
assertNotNull("Failed to load GenModel", genModel); |
165 |
} catch (CoreException e) { |
166 |
assertNotNull("Failed to load GenModel", genModel); |
167 |
} |
168 |
myOperation.setGenURI(createURI(FILE_EXT_GMFGEN)); |
169 |
IStatus status = myOperation.executeTransformation(myRS, null); |
170 |
assertTrue(status.isOK()); |
171 |
} |
172 |
|
173 |
public void testTransfromDesignMapping() { |
174 |
URI mapURI = prepareDesignResources(); |
175 |
loadMappingModel(mapURI); |
176 |
myOperation.setGenURI(createURI(FILE_EXT_GMFGEN)); |
177 |
IStatus status = myOperation.executeTransformation(myRS, null); |
178 |
assertTrue(status.isOK()); |
179 |
} |
180 |
|
181 |
private void loadMappingModel(URI mapURI) { |
182 |
Mapping mapping = null; |
183 |
try { |
184 |
mapping = myOperation.loadMappingModel(myRS, mapURI, null); |
185 |
assertNotNull("Mapping should be resolved", mapping); |
186 |
IStatus loadStatus = myOperation.getLoadMappingStatus(); |
187 |
assertNotNull("Load MappingModel status should be set", loadStatus); |
188 |
assertFalse("Load MappingModel status should differ from CANCEL value", IStatus.CANCEL == loadStatus.getSeverity()); |
189 |
assertFalse("Load MappingModel status should differ from ERROR value", IStatus.ERROR == loadStatus.getSeverity()); |
190 |
assertNotNull("GenModelDetector should be initialized", myOperation.getGenModelDetector()); |
191 |
assertNull("GenModel should be reset", myOperation.getGenModel()); |
192 |
} catch (CoreException e) { |
193 |
fail(e.getStatus().getMessage()); |
194 |
} |
195 |
} |
196 |
|
197 |
private void findAbsentGenmodel() { |
198 |
try { |
199 |
myOperation.findGenmodel(myRS); |
200 |
fail("GenModel should not be found"); |
201 |
} catch (CoreException e) { |
202 |
assertNull("GenModel should not be found", myOperation.getGenModel()); |
203 |
} |
204 |
} |
205 |
|
206 |
private URI createDefaultGenModel(URI mapURI) { |
207 |
URI genmodelURI = null; |
208 |
try { |
209 |
GenModelDetector gmd = myOperation.getGenModelDetector(); |
210 |
assertNotNull("GenModelDetector should be initialized", gmd); |
211 |
genmodelURI = gmd.createDefault(getProject().getName(), URIUtil.getFile(mapURI)); |
212 |
assertNotNull("GenModel URI should be created", genmodelURI); |
213 |
IFile file = URIUtil.getFile(mapURI); |
214 |
assertNotNull("GenModel file should be created", file); |
215 |
assertTrue("GenModel file should exists", file.exists()); |
216 |
} catch (CoreException e) { |
217 |
fail("Failed to create default GenModel: " + e.getMessage()); |
218 |
} |
219 |
return genmodelURI; |
220 |
} |
221 |
|
222 |
protected IProject createProject() { |
223 |
String projectName = "TransformToGenModelOperationTest_" + String.valueOf(System.currentTimeMillis()); //$NON-NLS-1$ |
224 |
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); |
225 |
assertFalse("Project with this name already present in the workspace: " + projectName, project.exists()); |
226 |
try { |
227 |
project.create(new NullProgressMonitor()); |
228 |
project.open(new NullProgressMonitor()); |
229 |
} catch (CoreException e) { |
230 |
fail(e.getMessage()); |
231 |
} |
232 |
assertTrue("Project was not created: " + projectName, project.exists()); |
233 |
return project; |
234 |
} |
235 |
|
236 |
protected void deleteProject() throws CoreException { |
237 |
if (getProject() != null) { |
238 |
if (getProject().isOpen()) { |
239 |
getProject().close(new NullProgressMonitor()); |
240 |
} |
241 |
if (getProject().exists()) { |
242 |
getProject().delete(true, new NullProgressMonitor()); |
243 |
} |
244 |
} |
245 |
} |
246 |
|
247 |
protected IProject getProject() { |
248 |
return myProject; |
249 |
} |
250 |
|
251 |
private URI prepareResources() { |
252 |
ResourceSet rs = new ResourceSetImpl(); |
253 |
prepareResource(rs, FILE_EXT_ECORE, getSetup().getDomainModel().getModel()); |
254 |
prepareResource(rs, FILE_EXT_GMFGRAPH, getSetup().getGraphDefModel().getCanvasDef()); |
255 |
prepareResource(rs, FILE_EXT_GMFTOOL, getSetup().getToolDefModel().getRegistry()); |
256 |
URI mapURI = prepareResource(rs, FILE_EXT_GMFMAP, getSetup().getMapModel().getMapping()); |
257 |
return mapURI; |
258 |
} |
259 |
|
260 |
private URI prepareDesignResources() { |
261 |
ResourceSet rs = new ResourceSetImpl(); |
262 |
DiaDefSource ddSource = getSetup().getGraphDefModel(); |
263 |
prepareResource(rs, FILE_EXT_GMFGRAPH, ddSource.getCanvasDef()); |
264 |
ToolDefSource toolDef = getSetup().getToolDefModel(); |
265 |
prepareResource(rs, FILE_EXT_GMFTOOL, toolDef.getRegistry()); |
266 |
URI mapURI = prepareResource(rs, FILE_EXT_GMFMAP, new DesignMapDefSource().init(ddSource, toolDef).getMapping()); |
267 |
return mapURI; |
268 |
} |
269 |
|
270 |
private URI prepareResource(ResourceSet rs, String extension, EObject root) { |
271 |
URI uri = createURI(extension); |
272 |
Resource resource = rs.createResource(uri); |
273 |
resource.getContents().add(root); |
274 |
try { |
275 |
resource.save(Collections.EMPTY_MAP); |
276 |
} catch (IOException e) { |
277 |
fail("Failed to save " + uri.toFileString() + " :" + e.getMessage()); |
278 |
} |
279 |
return uri; |
280 |
} |
281 |
|
282 |
private URI createURI(String extension) { |
283 |
String pathName = getProject().getName() + |
284 |
IPath.SEPARATOR + FOLDER_MODELS + |
285 |
IPath.SEPARATOR + FILE_BASE + "." + extension; //$NON-NLS-1$ |
286 |
URI uri = URI.createPlatformResourceURI(pathName, true); |
287 |
return uri; |
288 |
} |
289 |
|
290 |
private void assertIllegalArgument(CoreException e) { |
291 |
Throwable cause = e.getStatus().getException(); |
292 |
if (!(cause instanceof IllegalArgumentException)) { |
293 |
fail(e.getMessage()); |
294 |
} |
295 |
} |
296 |
|
297 |
private void assertIllegalState(CoreException e) { |
298 |
Throwable cause = e.getStatus().getException(); |
299 |
if (!(cause instanceof IllegalStateException)) { |
300 |
fail(e.getMessage()); |
301 |
} |
302 |
} |
303 |
|
304 |
private class DesignMapDefSource implements MapDefSource { |
305 |
|
306 |
private Mapping myMap; |
307 |
|
308 |
private NodeMapping myNodeA; |
309 |
|
310 |
private NodeMapping myNodeB; |
311 |
|
312 |
private LinkMapping myClassLink; |
313 |
|
314 |
private LinkMapping myRefLink; |
315 |
|
316 |
|
317 |
public DesignMapDefSource() { |
318 |
// just create |
319 |
} |
320 |
|
321 |
public DesignMapDefSource init(DiaDefSource ddSource, ToolDefSource toolDef) { |
322 |
initCanvasMapping(ddSource.getCanvasDef()); |
323 |
if (toolDef.getMainMenu() != null) { |
324 |
myMap.getDiagram().getMenuContributions().add(toolDef.getMainMenu()); |
325 |
} |
326 |
myMap.getDiagram().setPalette(toolDef.getPalette()); |
327 |
|
328 |
initCommonMapping(ddSource, toolDef); |
329 |
|
330 |
return this; |
331 |
} |
332 |
|
333 |
private void initCanvasMapping(Canvas canvas) { |
334 |
Mapping m = GMFMapFactory.eINSTANCE.createMapping(); |
335 |
|
336 |
CanvasMapping cme = GMFMapFactory.eINSTANCE.createCanvasMapping(); |
337 |
cme.setDiagramCanvas(canvas); |
338 |
m.setDiagram(cme); |
339 |
myMap = m; |
340 |
} |
341 |
|
342 |
private void initCommonMapping(DiaDefSource ddSource, ToolDefSource toolDef) { |
343 |
myNodeA = createNodeMapping(ddSource.getNodeDef(), ddSource.getLabelDef(), true); |
344 |
myNodeA.setContextMenu(toolDef.getNodeContextMenu()); |
345 |
myNodeA.setTool(toolDef.getNodeCreationTool()); |
346 |
|
347 |
DiaDefSetup ddSetup = ddSource instanceof DiaDefSetup ? (DiaDefSetup) ddSource : null; |
348 |
Node graphNode = ddSetup != null ? ddSetup.getColoredNodeDef() : ddSource.getNodeDef(); |
349 |
DiagramLabel graphLabel = ddSetup != null ? ddSetup.getDecoratedLabelDef() : ddSource.getLabelDef(); |
350 |
myNodeB = createNodeMapping(graphNode, graphLabel, true); |
351 |
|
352 |
myClassLink = createLinkMapping(ddSource.getLinkDef()); |
353 |
myClassLink.setTool(toolDef.getLinkCreationTool()); |
354 |
myRefLink = createLinkMapping(ddSetup != null ? ddSetup.getColoredLinkDef() : ddSource.getLinkDef()); |
355 |
} |
356 |
|
357 |
private final NodeMapping createNodeMapping(Node nodeDef, DiagramLabel labelDef, boolean addTopNodeReference) { |
358 |
NodeMapping nme = GMFMapFactory.eINSTANCE.createNodeMapping(); |
359 |
nme.setDiagramNode(nodeDef); |
360 |
|
361 |
if (addTopNodeReference){ |
362 |
TopNodeReference tnr = GMFMapFactory.eINSTANCE.createTopNodeReference(); |
363 |
tnr.setOwnedChild(nme); |
364 |
myMap.getNodes().add(tnr); |
365 |
} |
366 |
return nme; |
367 |
} |
368 |
|
369 |
private LinkMapping createLinkMapping(Connection link) { |
370 |
LinkMapping lme = GMFMapFactory.eINSTANCE.createLinkMapping(); |
371 |
lme.setDiagramLink(link); |
372 |
myMap.getLinks().add(lme); |
373 |
return lme; |
374 |
} |
375 |
|
376 |
public final Mapping getMapping() { |
377 |
return myMap; |
378 |
} |
379 |
|
380 |
public NodeMapping getNodeA() { |
381 |
return myNodeA; |
382 |
} |
383 |
|
384 |
public NodeMapping getNodeB() { |
385 |
return myNodeB; |
386 |
} |
387 |
|
388 |
public LinkMapping getClassLink() { |
389 |
return myClassLink; |
390 |
} |
391 |
|
392 |
public LinkMapping getReferenceLink() { |
393 |
return myRefLink; |
394 |
} |
395 |
|
396 |
} |
397 |
} |