Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 966fb4fd6374b09ebc671e7cbb1c11df42a8cbc0 (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
/*
 * Copyright (c) 2015 Christian W. Damus and others.
 * 
 * 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:
 *   Christian W. Damus - Initial API and implementation
 *   
 */

package org.eclipse.papyrus.tests.framework.m2t.xtend.templates

import static extension com.google.common.base.Strings.nullToEmpty

import org.eclipse.uml2.uml.Activity
import org.eclipse.uml2.uml.Class
import org.eclipse.uml2.uml.InstanceValue
import org.eclipse.uml2.uml.Operation
import javax.inject.Inject
import org.eclipse.uml2.uml.ValuePin
import org.eclipse.uml2.uml.InstanceSpecification
import org.eclipse.uml2.uml.ValueSpecification
import org.eclipse.uml2.uml.OpaqueExpression
import org.eclipse.uml2.uml.CallOperationAction
import org.eclipse.uml2.uml.LiteralString

/**
 * Common structure for all generated test classes.  Provides several abstract or default-blank snippets
 * that subclasses should override to plug in specific code fragments.
 */
abstract class AbstractTestTemplate {
    @Inject protected extension Queries
    @Inject protected extension TemplateQueries
    @Inject protected extension Importator
    
	def generate(Class clazz) '''
		«javaHeader»
		package «clazz.package.name»;
		
		«markImports»
        «clazz.elementImports.map[alias].forEach[imported(packageRootName(clazz)+'.edit.parts.'+it)]»
		
		/**
		 * The Class «clazz.name».
		 */
		«clazz.generatedTest»
		«clazz.runWith»
		«clazz.additionalAnnotations»
		public class «clazz.name» «clazz.extendsDeclaration» {
			
			«clazz.diagramCreationMethods»
		
			«FOR Operation testCaseOperation : clazz.testCases SEPARATOR '\n'»
				«val instanceSpec = (testCaseOperation.ownedParameters.head?.defaultValue as InstanceValue)?.instance»
				/**
				 * «testCaseOperation.purpose(instanceSpec?.componentName)»
				 */
				«testCaseOperation.testAnnotations»
				«testCaseOperation.exclusionAnnotation»
				public void «testCaseOperation.name»() {
					«safeTestCaseBody(testCaseOperation.methods.head as Activity, clazz)»
				}
			«ENDFOR»
			
			«clazz.getDiagramUpdaterMethod»
			
			«clazz.additionalMethods»
		}	
	'''

    def extendsDeclaration(Class class_) '''extends «imported(class_.generals.head.qualifiedJavaName)»'''
    
    def diagramCreationMethods(Class class_) '''
        @Override
        protected «imported('org.eclipse.papyrus.commands.ICreationCommand')» getDiagramCommandCreation() {
            return new «class_.diagramCreationCommand»();
        }
         
        @Override
        protected String getProjectName() {
            return «class_.constantsInterface».PROJECT_NAME;
        }
         
        @Override
        protected String getFileName() {
            return «class_.constantsInterface».FILE_NAME;
        }
    '''
    
    def getDiagramUpdaterMethod(Class class_) '''
        @Override
        public «imported('org.eclipse.papyrus.infra.gmfdiag.common.updater.DiagramUpdater')» getDiagramUpdater() {
            return «class_.diagramUpdater».«class_.diagramUpdaterInstanceField»;
        }
    '''
    
    def additionalMethods(Class class_) ''''''

    def runWith(Class testContext) ''''''
   	
    def generatedTest(Class testContext) '''@«imported('org.eclipse.papyrus.junit.framework.classification.GeneratedTest')»'''
    
   	protected def additionalAnnotations(Class testContext) ''''''

    abstract def CharSequence purpose(Operation testCase, String componentName)
    
    def junitTest(Operation testCase) '''@«imported('org.junit.Test')»'''
    def generatedTest(Operation testCase) ''''''
    def testAnnotations(Operation testCase) '''
        «testCase.junitTest»
        «testCase.generatedTest»
    '''
    
    def exclusionAnnotation(Operation testCase) '''
    «val pre = testCase.exclusionPrecondition»
    «IF pre != null»
    	@«pre.name.imported»("«pre.specification.stringValue»")
    «ENDIF»
    '''
    
    protected def getExclusionPrecondition(Operation testCase) {
    	testCase.preconditions.findFirst[
    		(name != null) && (specification instanceof LiteralString) 
    	]
    }
    
    private def safeTestCaseBody(Activity method, Class class_) {
        try {
            testCaseBody(method, class_);
        } catch (Exception e) {
             imported('org.junit.Assert') + '.fail("Test case specification problem: ' + (e.message ?: e.class.simpleName) +'");'
        }
    }
    
    def testCaseBody(Activity method, Class class_) '''
	    «FOR action : method.ownedNodes.filter(CallOperationAction)»
	    	«action.operation.name»(«FOR pin : action.arguments.filter(ValuePin) SEPARATOR ', '»«pin.toCallArgument(class_)»«ENDFOR»);
	    «ENDFOR»
    '''
    
    protected def dispatch CharSequence toCallArgument(ValuePin pin, Class testContext)
    '''«pin.value.toCallArgument(testContext)»'''
    
    protected def dispatch toCallArgument(ValueSpecification value, Class testContext)
    '''«value.stringValue»'''
    
    protected def dispatch toCallArgument(LiteralString value, Class testContext)
    '''"«value.stringValue.nullToEmpty.replace('"', '\\"')»"'''
    
    protected def dispatch toCallArgument(InstanceValue value, Class testContext) {
        var node = value.instance as InstanceSpecification
        val umlElementTypes = imported(packageRootName(testContext) + '.providers.UMLElementTypes')
    	'''«umlElementTypes».getElementType(«node.getStringSlotValue('editPartClassName')».VISUAL_ID)'''
   	}
    
    protected def dispatch toCallArgument(OpaqueExpression value, Class testContext) {
        val index = value.languages.indexOf('Java')
        '''«value.bodies.get(index)»'''
   	}
}

Back to the top