Method: AcceleoTraceabilityLibraryOclTests()

1: /*******************************************************************************
2: * Copyright (c) 2009, 2012 Obeo.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * Obeo - initial API and implementation
10: *******************************************************************************/
11: package org.eclipse.acceleo.traceability.tests.unit.library;
12:
13: import java.util.List;
14:
15: import org.eclipse.acceleo.traceability.GeneratedFile;
16: import org.eclipse.acceleo.traceability.GeneratedText;
17: import org.eclipse.acceleo.traceability.InputElement;
18: import org.eclipse.acceleo.traceability.ModuleElement;
19: import org.eclipse.acceleo.traceability.tests.unit.AbstractTraceabilityTest;
20: import org.eclipse.acceleo.traceability.tests.unit.AcceleoTraceabilityListener;
21: import org.eclipse.emf.ecore.EClass;
22: import org.eclipse.emf.ecore.EObject;
23: import org.eclipse.ocl.ecore.StringLiteralExp;
24: import org.eclipse.ocl.utilities.ASTNode;
25: import org.junit.Test;
26:
27: import static org.junit.Assert.assertEquals;
28: import static org.junit.Assert.assertTrue;
29:
30: public class AcceleoTraceabilityLibraryOclTests extends AbstractTraceabilityTest {
31:
32:         @Test
33:         public void testTraceabilityLibraryOclIfPropertyExp() {
34:                 AcceleoTraceabilityListener traceabilityListener = this.parseAndGenerate(
35:                                 "data/library/ocl/oclIfPropertyExp.mtl", //$NON-NLS-1$
36:                                 "main", "data/library/ocl/model.ecore", true); //$NON-NLS-1$ //$NON-NLS-2$
37:                 List<GeneratedFile> generatedFiles = traceabilityListener.getGeneratedFiles();
38:                 assertEquals(4, generatedFiles.size());
39:
40:                 int cpt = 1;
41:                 for (GeneratedFile generatedFile : generatedFiles) {
42:                         List<GeneratedText> generatedRegions = generatedFile.getGeneratedRegions();
43:                         assertEquals(1, generatedRegions.size());
44:                         assertEquals("dddd".length(), generatedFile.getLength()); //$NON-NLS-1$
45:
46:                         List<InputElement> sourceElements = generatedFile.getSourceElements();
47:                         assertEquals(2, sourceElements.size()); // the class and its name
48:                         assertEquals("class" + cpt + ", feature='name'", sourceElements.get(0).toString()); //$NON-NLS-1$ //$NON-NLS-2$
49:                         assertEquals("class" + cpt, sourceElements.get(1).toString()); //$NON-NLS-1$
50:                         assertEquals("class" + cpt + ".txt", generatedFile.getPath()); //$NON-NLS-1$ //$NON-NLS-2$
51:
52:                         GeneratedText generatedText = generatedRegions.get(0);
53:                         assertEquals(0, generatedText.getStartOffset());
54:                         assertEquals("dddd".length(), generatedText.getEndOffset()); //$NON-NLS-1$
55:                         ModuleElement moduleElement = generatedText.getModuleElement();
56:                         EObject element = moduleElement.getModuleElement();
57:                         assertTrue(element instanceof ASTNode);
58:                         assertTrue(element instanceof StringLiteralExp);
59:                         StringLiteralExp stringLiteralExp = (StringLiteralExp)element;
60:                         assertEquals("dddd", stringLiteralExp.getStringSymbol()); //$NON-NLS-1$
61:                         assertEquals(240, stringLiteralExp.getStartPosition());
62:                         assertEquals(240 + "'dddd'".length(), stringLiteralExp.getEndPosition()); //$NON-NLS-1$
63:
64:                         InputElement sourceElement = generatedText.getSourceElement();
65:                         EObject modelElement = sourceElement.getModelElement();
66:                         assertTrue(modelElement instanceof EClass);
67:                         assertEquals("class" + cpt, ((EClass)modelElement).getName()); //$NON-NLS-1$
68:                         assertEquals("/plugin/org.eclipse.acceleo.traceability.tests/data/library/ocl/model.ecore", //$NON-NLS-1$
69:                                         modelElement.eResource().getURI().path());
70:                         cpt++;
71:                 }
72:         }
73:
74:         @Test
75:         public void testTraceabilityLibraryOclIfStringLiteral() {
76:                 AcceleoTraceabilityListener traceabilityListener = this.parseAndGenerate(
77:                                 "data/library/ocl/oclIfStringLiteral.mtl", //$NON-NLS-1$
78:                                 "main", "data/library/ocl/model.ecore", true); //$NON-NLS-1$ //$NON-NLS-2$
79:                 List<GeneratedFile> generatedFiles = traceabilityListener.getGeneratedFiles();
80:                 assertEquals(4, generatedFiles.size());
81:
82:                 int cpt = 1;
83:                 for (GeneratedFile generatedFile : generatedFiles) {
84:                         List<GeneratedText> generatedRegions = generatedFile.getGeneratedRegions();
85:                         assertEquals(1, generatedRegions.size());
86:                         assertEquals("dddd".length(), generatedFile.getLength()); //$NON-NLS-1$
87:
88:                         List<InputElement> sourceElements = generatedFile.getSourceElements();
89:                         assertEquals(2, sourceElements.size()); // the class and its name
90:                         assertEquals("class" + cpt + ", feature='name'", sourceElements.get(0).toString()); //$NON-NLS-1$ //$NON-NLS-2$
91:                         assertEquals("class" + cpt, sourceElements.get(1).toString()); //$NON-NLS-1$
92:                         assertEquals("class" + cpt + ".txt", generatedFile.getPath()); //$NON-NLS-1$ //$NON-NLS-2$
93:
94:                         GeneratedText generatedText = generatedRegions.get(0);
95:                         assertEquals(0, generatedText.getStartOffset());
96:                         assertEquals("dddd".length(), generatedText.getEndOffset()); //$NON-NLS-1$
97:                         ModuleElement moduleElement = generatedText.getModuleElement();
98:                         EObject element = moduleElement.getModuleElement();
99:                         assertTrue(element instanceof ASTNode);
100:                         StringLiteralExp stringLiteralExp = (StringLiteralExp)element;
101:                         assertEquals("dddd", stringLiteralExp.getStringSymbol()); //$NON-NLS-1$
102:                         assertEquals(234, stringLiteralExp.getStartPosition());
103:                         assertEquals(234 + "'dddd'".length(), stringLiteralExp.getEndPosition()); //$NON-NLS-1$
104:
105:                         InputElement sourceElement = generatedText.getSourceElement();
106:                         EObject modelElement = sourceElement.getModelElement();
107:                         assertTrue(modelElement instanceof EClass);
108:                         assertEquals("class" + cpt, ((EClass)modelElement).getName()); //$NON-NLS-1$
109:                         assertEquals("/plugin/org.eclipse.acceleo.traceability.tests/data/library/ocl/model.ecore", //$NON-NLS-1$
110:                                         modelElement.eResource().getURI().path());
111:                         cpt++;
112:                 }
113:         }
114:
115: }