/* MOFScript: version 1.1.4 To deal with some limitations faced with this version of MOFScript, following three java methods are used withing these rules. 1. writeHeader(): it simply copies the "CodeHeader" file to the top of the output file (the test case). 1. writeText(): it simply writes the test to the output file (the test case). 2. writeTestData(): it reads data from the "TestData" file and appends it in the output file (the generated test case). */ texttransformation testM2T (in model:xUnit) /*************************************************************************** Rule 1: main( ) Description: This initiates the model-to-text transformation. Invoked from: N/A ***************************************************************************/ model.TestSuite::main() { smsName = self.name paramHashtable.put("OutputFilePath", outputFilePath) paramHashtable.put("OutputFileName", "Test_" + self.testCase.first().name) paramHashtable.put("OutputFileExtension", outputFileExtension) paramHashtable.put("TestDataFilePath", testDataFilePath) paramHashtable.put("TestDataFileName", testDataFileName + "_" + self.testCase.first().name) paramHashtable.put("TestDataFileExtension", testDataFileExtension) paramHashtable.put("HeaderFilePath", headerFilePath) paramHashtable.put("HeaderFileName", headerFileName) paramHashtable.put("HeaderFileExtension", headerFileExtension) // Copy Code Header file at the top of the Test Case java ("com.m2t.CreateTemplate", "writeHeader", paramHashtable, "C:/eclipse/project/xunit2text") text = text + "\npublic class Test_" + self.testCase.first().name + " extends TestCase { \n\n" text = text + "\tpublic static void main( String args[] ) { \n" text = text + "\t\t TestSuite testSuite = new TestSuite(Test_" + self.testCase.first().name + ".class); \n" text = text + "\t\t testSuite.run( new TestResult( ) ); \n\t} \n\t" paramHashtable.put("Text", text) java ("com.m2t.CreateTemplate", "writeText", paramHashtable, "C:/eclipse/project/xunit2text") // Generate code for each test case self.testCase->forEach(tc:model.TestCase) { tc.mapTestCase() } text = "\n\r} //End of TestCase\n"; paramHashtable.put("Text", text) java ("com.m2t.CreateTemplate", "writeText", paramHashtable, "C:/eclipse/project/xunit2text") } /*************************************************************************** Rule 2: mapTestCase( ) Description: This generates code for a test case. Invoked from: main () ***************************************************************************/ model.TestCase::mapTestCase() { text = "\n\tpublic void test" + self.name + "( ) {\n\r\t try {" paramHashtable.put("Text", text) java ("com.m2t.CreateTemplate", "writeText", paramHashtable, "C:/eclipse/project/xunit2text") paramHashtable.put("sms", self.name) // The version of Tefkat used int this project was losing order of // assertions in the test case. The following code is added as a work around. counter = self.assertion.size() counter->forEach(c) { self.assertion->forEach(a:model.Assertion) { if(a.order.trim().equals(c) ) { a.mapAssertion() } } } text = "\n\r\t} catch ( Exception exp ) {" text = text + "\n\t\t\tSystem.out.println( exp.toString() );" text = text + "\r\t\t\tfail(\"Exception occured during test case execution\");\n\t}" text = text + "\n\r\t} //End of Test Method"; paramHashtable.put("Text", text) java ("com.m2t.CreateTemplate", "writeText", paramHashtable, "C:/eclipse/project/xunit2text") } /*************************************************************************** Rule 3: mapAssertion( ) Description: This rules creates text for assertion in the test case. Invoked from: mapTestCase () ***************************************************************************/ model.Assertion::mapAssertion() { isEmptyExpected = self.expectedValue.isEmpty() isEmptyExpectedSimpleAtt = self.expectedValue.first().simpleAttribute.isEmpty() isEmptyExpectedComplexAtt = self.expectedValue.first().complexAttribute.isEmpty() returnType = self.expectedValue.first().type.trim() self.method->forEach( m:model.Method | m = self.method.first()) { methodName = m.name.trim() assertionType = m.owner.assertionType isSetup = m.name.startsWith("SETUP_") isStatic = m.static.equalsIgnoreCase("yes") className = m.ownerClass.first().name classInstance = m.ownerClass.first().name.firstToLower().trim() m.mapMethod() self.expectedValue->forEach(p:model.ExpectedValue) { p.mapExpectedValue() } if( isStatic ) classInstance = className text = "" if( not isSetup ) { if( isEmptyExpected ) { text = text + "\r\t\t" + classInstance + "." + methodName + "("+parameterText+");" } else { if( isEmptyExpectedSimpleAtt and isEmptyExpectedComplexAtt and ( not returnType.equalsIgnoreCase("String")) ) { if( not isStatic ) { text = "\r\t\t"+ returnType + " " + returnVariable +" = " + classInstance + "." + methodName + "("+parameterText+");" } text = text + "\n\t\t"+ assertionType + "( "+ expectedVariable +" == " + returnVariable + " );" } else { text = "\r\t\t"+ returnType + " " + returnVariable + " = (" + returnType + ") " + classInstance +"." + methodName + "("+parameterText+");" text = text + "\n\t\t"+ assertionType + "( "+ expectedVariable +".equals( " + returnVariable + " ) );" } } paramHashtable.put("Text", text) java ("com.m2t.CreateTemplate", "writeText", paramHashtable, "C:/eclipse/project/xunit2text") } } methodName = "" isSetup = false isStatic = false isEmptyExpected = false isEmptyExpectedSimpleAtt = false isEmptyExpectedComplexAtt = false className = "" classInstance = "" returnType = "" returnVariable = "" text = "" } /*************************************************************************** Rule 4: mapMethod( ) Description: This rules creates text for method invocation. Invoked from: mapAssertion() ***************************************************************************/ model.Method::mapMethod(){ paramHashtable.put("Message", self.ownerClass.first().name + "::"+ methodName.normalizeSpace()) storedClassInstance="" key = self.ownerClass.first().name.firstToLower() if( not key.equals("") ) storedClassInstance = variableMap.get( key.trim() ) // A method belongs to only one class but due to some technical issue with the version // of transformation engine bing used, we need to declare its multiciplicity ‘n’ instead of 1 if( storedClassInstance.equals("") ) { self.ownerClass->forEach(cn:model.OwnerClass | cn = self.ownerClass.first()) { cn.mapOwnerClass() } if( isStatic == false) { text = text + "\n\r\t\t" + className + " " + classInstance + " = new " + className + "(" + cParameterText + ");" } paramHashtable.put("Text", text) java ("com.m2t.CreateTemplate", "writeText", paramHashtable, "C:/eclipse/project/xunit2text") variableMap.put(classInstance.trim(), classInstance.trim() ) } else { classInstance = storedClassInstance } text = "" parameterText = "" parameterNumber = 1 if( not isSetup ) { // we do not generate method call fro setup methods self.parameter->forEach(p:model.Parameter) { p.mapParameter( parameterNumber ) parameterNumber = parameterNumber + 1 } } } /*************************************************************************** Rule 5: mapOwnerClass( ) Description: This rules creates an object on which the method is invoked. Invoked from: mapMethod() ***************************************************************************/ model.OwnerClass::mapOwnerClass ( ) { cParameterText = "" cParameterNumber = 1 self.constructorParameter->forEach(cp:model.ConstructorParameter) { cp.mapConstructorParameter( cParameterNumber ) cParameterNumber = cParameterNumber + 1 } } /*************************************************************************** Rule 6: mapConstructorParameter( ) Description: This rules creates constructor parameter. Invoked from: mapMethod() ***************************************************************************/ model.ConstructorParameter::mapConstructorParameter( cParameterNumberStr:String ) { depth = "0"; cParameterSeperator = ", " if( cParameterNumber == 1 ) cParameterSeperator = "" cParameterType = self.type cParameterVariable = self.name storedParamVariable = variableMap.get( "return_" + cParameterVariable.trim() ) if ( storedParamVariable.trim().equals("") ) { storedParamVariable = variableMap.get( cParameterVariable.trim() ) } if ( storedParamVariable.trim().equals("") ) { cParameterVariable = self.name variableMap.put( cParameterVariable.trim(), cParameterVariable.trim() ) } else { cParameterVariable = storedParamVariable } cParameterText = cParameterText + cParameterSeperator + self.name if ( storedParamVariable.trim().equals("") ) { paramHashtable.put("Flag", "ConstructorParameter") text = "\n\n\t\t//Prepare constructor parameter object" if ( self.simpleAttribute.isEmpty() && self.complexAttribute.isEmpty() ) { text = text + "\n\t\t"+ cParameterType + " " + cParameterVariable + " = " if (self.type.equalsIgnoreCase("String")) text = text + "\"" paramHashtable.put("Text", text) java ("com.m2t.CreateTemplate", "writeText", paramHashtable, "C:/eclipse/project/xunit2text") text = "\n" paramHashtable.put("ParameterNumber", "parameter" + cParameterNumberStr) paramHashtable.put("Depth", "0") java ("com.m2t.CreateTemplate", "writeTestData", paramHashtable, "C:/eclipse/project/xunit2text") text = ";" if (self.type.equalsIgnoreCase("String") ) text = "\";" paramHashtable.put("Text", text) java ("com.m2t.CreateTemplate", "writeText", paramHashtable, "C:/eclipse/project/xunit2text") text= "" } else { text = text + "\n\t\t"+ cParameterType + " "+ cParameterVariable + " = new " + cParameterType + "();" paramHashtable.put("ParameterNumber", "parameter" + cParameterNumberStr) paramHashtable.put("Text", text) java ("com.m2t.CreateTemplate", "writeText", paramHashtable, "C:/eclipse/project/xunit2text") text = "\n" } complexAttributeCounter = 1 self.complexAttribute->forEach( ca:model.ComplexAttribute ) { ca.mapComplexAttribute(cParameterNumberStr, cParameterVariable, complexAttributeCounter, depth) complexAttributeCounter = complexAttributeCounter + 1 } simpleAttributeCounter = 1 self.simpleAttribute->forEach( sa:model.SimpleAttribute ) { sa.mapSimpleAttribute(cParameterNumberStr, cParameterVariable, simpleAttributeCounter, depth) simpleAttributeCounter = simpleAttributeCounter + 1 } } } /*************************************************************************** Rule 7: mapExpectedValue( ) Description: This rules creates expected value for a method call. Invoked from: mapAssertion() ***************************************************************************/ model.ExpectedValue::mapExpectedValue() { var depth:String = "0"; expectedType = self.type expectedVariable = self.name returnVariable = "return_" + expectedVariable variableMap.put( returnVariable.trim(), returnVariable.trim() ) paramHashtable.put("Flag", "ExpectedValue") text= "\n\n\t\t//Prepare expected object" if ( self.simpleAttribute.isEmpty() && self.complexAttribute.isEmpty()) { text = text + "\n\t\t"+ expectedType + " " + self.name + " = " if (self.type.equalsIgnoreCase("String")) text = text + "\"" paramHashtable.put("Text", text) java ("com.m2t.CreateTemplate", "writeText", paramHashtable, "C:/eclipse/project/xunit2text") paramHashtable.put("Depth", "0") java ("com.m2t.CreateTemplate", "writeTestData", paramHashtable, "C:/eclipse/project/xunit2text") text = ";" if (self.type.equalsIgnoreCase("String")) text = "\";" paramHashtable.put("Text", text) java ("com.m2t.CreateTemplate", "writeText", paramHashtable, "C:/eclipse/project/xunit2text") } else { text = text + "\n\t\t"+ expectedType + " " + self.name + " = new " + expectedType + "();" paramHashtable.put("Text", text) java ("com.m2t.CreateTemplate", "writeText", paramHashtable, "C:/eclipse/project/xunit2text") } complexAttributeCounter = 1 self.complexAttribute->forEach( ca:model.ComplexAttribute ) { ca.mapComplexAttribute("1", expectedVariable, complexAttributeCounter, depth) complexAttributeCounter = complexAttributeCounter + 1 } simpleAttributeCounter = 1 self.simpleAttribute->forEach( sa:model.SimpleAttribute ) { sa.mapSimpleAttribute("1", expectedVariable, simpleAttributeCounter, depth) simpleAttributeCounter = simpleAttributeCounter + 1 } } /*************************************************************************** Rule 8: mapParameter( ) Description: This rules creates parameter for a method call. Invoked from: mapMethod() ***************************************************************************/ model.Parameter::mapParameter( parameterNumberStr:String ) { depth = "0"; parameterSeperator = ", " if( parameterNumber == 1 ) parameterSeperator = "" storedParamVariable = "" parameterVariable = "" parameterType = self.type storedParamVariable = variableMap.get( parameterVariable.trim() ) if ( storedParamVariable.equals("") ) { parameterVariable = self.name storedParamVariable = variableMap.get( parameterVariable.trim() ) } if ( storedParamVariable.equals("") ) { parameterVariable = self.name variableMap.put( parameterVariable.trim(), parameterVariable.trim()) parameterText = parameterText + parameterSeperator + parameterVariable } else { parameterVariable = storedParamVariable parameterText = parameterText + parameterSeperator + parameterVariable } if ( storedParamVariable.trim().equals("") ) { paramHashtable.put("Flag", "Parameter") text = "\n\n\t\t//Prepare parameter object" if ( self.simpleAttribute.isEmpty() && self.complexAttribute.isEmpty() ) { text = text + "\n\t\t"+ parameterType + " " + parameterVariable + " = " if (self.type.equalsIgnoreCase("String")) text = text + "\"" paramHashtable.put("Text", text) java ("com.m2t.CreateTemplate", "writeText", paramHashtable, "C:/eclipse/project/xunit2text") text = "\n" paramHashtable.put("ParameterNumber", "parameter" + parameterNumberStr) paramHashtable.put("Depth", "0") java ("com.m2t.CreateTemplate", "writeTestData", paramHashtable, "C:/eclipse/project/xunit2text") text = ";" if (self.type.equalsIgnoreCase("String")) text = "\";" paramHashtable.put("Text", text) java ("com.m2t.CreateTemplate", "writeText", paramHashtable, "C:/eclipse/project/xunit2text") text= "" } else { text = text + "\n\t\t"+ parameterType + " "+ parameterVariable + " = new " + parameterType + "();" paramHashtable.put("ParameterNumber", "parameter" + parameterNumberStr) paramHashtable.put("Text", text) java ("com.m2t.CreateTemplate", "writeText", paramHashtable, "C:/eclipse/project/xunit2text") text = "\n" } complexAttributeCounter = 1 self.complexAttribute->forEach( ca:model.ComplexAttribute ) { ca.mapComplexAttribute(parameterNumberStr, parameterVariable, complexAttributeCounter, depth) complexAttributeCounter = complexAttributeCounter + 1 } simpleAttributeCounter = 1 self.simpleAttribute->forEach( sa:model.SimpleAttribute ) { sa.mapSimpleAttribute(parameterNumberStr, parameterVariable, simpleAttributeCounter, depth) simpleAttributeCounter = simpleAttributeCounter + 1 } } } /*************************************************************************** Rule 9: mapComplexAttribute( ) Description: This rules creates complex objects. Invoked from: mapParameter(), mapExpectedValue(), mapConstructorParameter() ***************************************************************************/ model.ComplexAttribute::mapComplexAttribute( parameterNumberStr:String, parentVariable:String, complexAttributeCounterStr:String, depthStr:String ) { text = "" depthStr = depthStr + "C" + complexAttributeCounterStr complexAttributeType = self.type complexAttributeVariable = parentVariable + complexAttributeType.firstToUpper() + complexAttributeCounterStr text = text + "\n\t\t"+ complexAttributeType + " "+ complexAttributeVariable + " = new " + complexAttributeType + "();" paramHashtable.put("Text", text) java ("com.m2t.CreateTemplate", "writeText", paramHashtable, "C:/eclipse/project/xunit2text") complexAttributeCounter2 = 1 self.complexAttribute->forEach( sa:model.ComplexAttribute ) { sa.mapComplexAttribute(parameterNumberStr, parentVariable, complexAttributeCounter2, depthStr) complexAttributeCounter2 = complexAttributeCounter2 + 1 } simpleAttributeCounter = 1 self.simpleAttribute->forEach( sa:model.SimpleAttribute ) { sa.mapSimpleAttribute(parameterNumberStr, complexAttributeVariable, simpleAttributeCounter, depthStr) simpleAttributeCounter = simpleAttributeCounter + 1 } if( self.setter==null ) { setter = "set" + self.name.firstToUpper() } else { setter = self.setter } text = "\t\t" + parentVariable + "." + setter + "(" + complexAttributeVariable + ");" paramHashtable.put("Text", text) java ("com.m2t.CreateTemplate", "writeText", paramHashtable, "C:/eclipse/project/xunit2text") text = "" } /*************************************************************************** Rule 10: mapSimpleAttribute( ) Description: This rules creates simple objects. Invoked from: mapParameter(), mapExpectedValue(), mapConstructorParameter(), mapComplexParameter() ***************************************************************************/ model.SimpleAttribute::mapSimpleAttribute( parameterNumberStr:String, parentVariable:String, simpleAttributeCounterStr:String, depthStr:String ) { text = "" simpleAttributeType = self.type simpleAttributeVariable = parentVariable + simpleAttributeType.firstToUpper()+ simpleAttributeCounterStr text = text + "\n\t\t"+ simpleAttributeType + " "+ simpleAttributeVariable + " = " if (self.type.equalsIgnoreCase("String") ) text = text + "\"" paramHashtable.put("Text", text) java ("com.m2t.CreateTemplate", "writeText", paramHashtable, "C:/eclipse/project/xunit2text") paramHashtable.put("ParameterNumber", "parameter" + parameterNumberStr) paramHashtable.put("Depth", depthStr + "S" + simpleAttributeCounterStr) java ("com.m2t.CreateTemplate", "writeTestData", paramHashtable, "C:/eclipse/project/xunit2text") text = ";" if (self.type.equalsIgnoreCase("String")) text = "\";" paramHashtable.put("Text", text) java ("com.m2t.CreateTemplate", "writeText", paramHashtable, "C:/eclipse/project/xunit2text") if( self.setter==null ) { setter = "set" + self.name.firstToUpper() } else { setter = self.setter } text = "\n\t\t" + parentVariable + "." + setter + "(" + simpleAttributeVariable + ");\n" paramHashtable.put("Text", text) java ("com.m2t.CreateTemplate", "writeText", paramHashtable, "C:/eclipse/project/xunit2text") text = "" }