a mop based dsl for testing java programs using ocl
DESCRIPTION
A MOP Based DSL for Testing Java Programs using OCL. Tony Clark http://itcentre.tvu.ac.uk/~clark/ Centre for Model Driven Software Engineering School of Computing Thames Valley University London, UK. context SalesSystem :: placeOrder ( name:String,amount:int ) - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: A MOP Based DSL for Testing Java Programs using OCL](https://reader036.vdocument.in/reader036/viewer/2022062305/568160ae550346895dcfcfc2/html5/thumbnails/1.jpg)
A MOP Based DSL for Testing Java Programs using OCL
Tony Clarkhttp://itcentre.tvu.ac.uk/~clark/
Centre for Model Driven Software EngineeringSchool of Computing
Thames Valley UniversityLondon, UK
![Page 2: A MOP Based DSL for Testing Java Programs using OCL](https://reader036.vdocument.in/reader036/viewer/2022062305/568160ae550346895dcfcfc2/html5/thumbnails/2.jpg)
OCL 09 2
context SalesSystem::placeOrder(name:String,amount:int) pre: accountsSystem.accounts->exists(a | a.cid = name) post: accountsSystem.getAccount(name).items->exists(item | item.amount = amount) and accountsSystem.getAccount(name).items->size = [email protected](name).items->size + 1
![Page 3: A MOP Based DSL for Testing Java Programs using OCL](https://reader036.vdocument.in/reader036/viewer/2022062305/568160ae550346895dcfcfc2/html5/thumbnails/3.jpg)
OCL 09 3
Aim
• To extend XMF with a language feature for testing Java implementations.
• To use OCL as the constraint language.
Problem: how to deal with all the possible implementation strategies for the model?
![Page 4: A MOP Based DSL for Testing Java Programs using OCL](https://reader036.vdocument.in/reader036/viewer/2022062305/568160ae550346895dcfcfc2/html5/thumbnails/4.jpg)
OCL 09 4
A Little OCL-based Language@MSpec <name> [<method-name>] (<args>) pre <pre-condition> do <body> post <post-condition>end
@Test <name> <spec>*end
![Page 5: A MOP Based DSL for Testing Java Programs using OCL](https://reader036.vdocument.in/reader036/viewer/2022062305/568160ae550346895dcfcfc2/html5/thumbnails/5.jpg)
OCL 09 5
Example Test Scenariocontext SalesSystem
@MSpec successfulPlaceOrder[placeOrder](name,amount) pre accountsSystem.accounts->exists(a | a.cid = name) do run post accountsSystem.getAccount(name).items->exists(item | item.amount = amount) and accountsSystem.getAccount(name).items->size = preSelf.accountsSystem.getAccount(name).items->size + 1end
@Test Test0 successfulContact("fred") successfulRegister("fred") successfulPlaceOrder("fred",100)end
![Page 6: A MOP Based DSL for Testing Java Programs using OCL](https://reader036.vdocument.in/reader036/viewer/2022062305/568160ae550346895dcfcfc2/html5/thumbnails/6.jpg)
OCL 09 6
Generate Reports
![Page 7: A MOP Based DSL for Testing Java Programs using OCL](https://reader036.vdocument.in/reader036/viewer/2022062305/568160ae550346895dcfcfc2/html5/thumbnails/7.jpg)
OCL 09 7
XMF and OCL@Class Account @Attribute cid : String end @Constructor(cid) endend@Class AccountsSystem @Attribute accounts : Set(Account) (+) endend @Class SalesSystem @Attribute accountsSystem : AccountsSystem = AccountsSystem() end @Operation addAccount(cid:String) accountsSystem.addToAccounts(Account(cid)) end @Operation checkNameExists(name:String):Boolean accountsSystem.accounts->exists(a | a.cid = name) endend
![Page 8: A MOP Based DSL for Testing Java Programs using OCL](https://reader036.vdocument.in/reader036/viewer/2022062305/568160ae550346895dcfcfc2/html5/thumbnails/8.jpg)
OCL 09 8
Java Implementationpublic class Account { public String cid; ...}public class AccountsSystem { public Vector<Account> accounts = new Vector<Account>(); ...}public class SalesSystem { public AccountsSystem accountsSystem = new AccountsSystem(); public ContactsDatabase contactsDatabase = new ContactsDatabase(); public void addAccount(String cid) { accountsSystem.addToAccounts(new Account(cid)); } ...}
![Page 9: A MOP Based DSL for Testing Java Programs using OCL](https://reader036.vdocument.in/reader036/viewer/2022062305/568160ae550346895dcfcfc2/html5/thumbnails/9.jpg)
OCL 09 9
Meta Classes in XMF
![Page 10: A MOP Based DSL for Testing Java Programs using OCL](https://reader036.vdocument.in/reader036/viewer/2022062305/568160ae550346895dcfcfc2/html5/thumbnails/10.jpg)
OCL 09 10
Foreign Objects
![Page 11: A MOP Based DSL for Testing Java Programs using OCL](https://reader036.vdocument.in/reader036/viewer/2022062305/568160ae550346895dcfcfc2/html5/thumbnails/11.jpg)
OCL 09 11
Java Classes in XMF
![Page 12: A MOP Based DSL for Testing Java Programs using OCL](https://reader036.vdocument.in/reader036/viewer/2022062305/568160ae550346895dcfcfc2/html5/thumbnails/12.jpg)
OCL 09 12
A Metaclass: JavaClass@Class JavaClass extends Class @Attribute descriptor : JavaDescriptor (?,!) end
@Operation addDescriptor(d:JavaDescriptor) xmf.foreignTypeMapping().put(d.type(),self); xmf.foreignMOPMapping().put(d.type(),d.mopName()); self.setDescriptor(d) end
@Operation invoke(target,args) let class = xmf.javaClass(descriptor.type(),descriptor.paths()) in class.invoke(target,args) end end ...end
![Page 13: A MOP Based DSL for Testing Java Programs using OCL](https://reader036.vdocument.in/reader036/viewer/2022062305/568160ae550346895dcfcfc2/html5/thumbnails/13.jpg)
OCL 09 13
A Java MOPpublic class ForeignObjectMOP {
public void dot(Machine m, int obj, int name) { Object o = m.getForeignObject(obj); Class<?> c = o.getClass(); Field f = c.getField(m.getString(name)); m.pushStack(f.get(o)); } public void send(Machine m, int o, int m, int args) { // Send message and push return value... } public boolean hasSlot(Machine m, int o, int name) { // Return true when object has named slot } public void set(Machine m, int o, int name, int value) { // Set named slot... }}
![Page 14: A MOP Based DSL for Testing Java Programs using OCL](https://reader036.vdocument.in/reader036/viewer/2022062305/568160ae550346895dcfcfc2/html5/thumbnails/14.jpg)
OCL 09 14
XMF access to Java@Class Account metaclass JavaClass JavaDescriptor("Account","",Seq{"@/bin"})end@Class AccountsSystem metaclass JavaClass JavaDescriptor("AccountsSystem","",Seq{"@/bin"})end@Class SalesSystem metaclass JavaClass JavaDescriptor("SalesSystem","",Seq{"@/bin"}) @Operation checkNameExists(name:String):Boolean accountsSystem.accounts->exists(a | a.cid = name) endend
![Page 15: A MOP Based DSL for Testing Java Programs using OCL](https://reader036.vdocument.in/reader036/viewer/2022062305/568160ae550346895dcfcfc2/html5/thumbnails/15.jpg)
OCL 09 15
Making the Default MOP Explicit@Class Account metaclass JavaClass JavaDescriptor("Account",“foreignobj.ForeignObjectMOP",Seq{"@/bin"})endcontext Root@Class AccountsSystem metaclass JavaClass JavaDescriptor("AccountsSystem",“foreignobj.ForeignObjectMOP",Seq{"@/bin"})endcontext Root@Class SalesSystem metaclass JavaClass JavaDescriptor("SalesSystem","foreignobj.ForeignObjectMOP",Seq{"@/bin"}) @Operation checkNameExists(name:String):Boolean accountsSystem.accounts->exists(a | a.cid = name) endend
![Page 16: A MOP Based DSL for Testing Java Programs using OCL](https://reader036.vdocument.in/reader036/viewer/2022062305/568160ae550346895dcfcfc2/html5/thumbnails/16.jpg)
OCL 09 16
A Different Implementation: EMF
![Page 17: A MOP Based DSL for Testing Java Programs using OCL](https://reader036.vdocument.in/reader036/viewer/2022062305/568160ae550346895dcfcfc2/html5/thumbnails/17.jpg)
OCL 09 17
An EMF MOP
![Page 18: A MOP Based DSL for Testing Java Programs using OCL](https://reader036.vdocument.in/reader036/viewer/2022062305/568160ae550346895dcfcfc2/html5/thumbnails/18.jpg)
OCL 09 18
A Metaclass: EMFClass@Class EMFClass extends JavaClass
@Operation invoke(target,args) let factory = descriptor.getFactory() then package = descriptor.getPackage() then class = package.send("get" + name,Seq{}) then object = factory.create(class)
in // Set fields from args using constructor... object end end ... end
![Page 19: A MOP Based DSL for Testing Java Programs using OCL](https://reader036.vdocument.in/reader036/viewer/2022062305/568160ae550346895dcfcfc2/html5/thumbnails/19.jpg)
OCL 09 19
EMFMoppublic class EObjectMOP extends foreignobj.ForeignObjectMOP {
public void dot(Machine machine, int object, int name) { EObject eobject = (Eobject)machine.getForeignObject(object); EClass eclass = eobject.eClass(); String string = machine.getString(name); EStructuralFeature feature = eclass.getEStructuralFeature(string); machine.pushStack(eobject.eGet(feature)); }
public boolean hasSlot(Machine machine, int obj, int name) { ... }
public void set(Machine machine, int obj, int name, int value) { ... }}
![Page 20: A MOP Based DSL for Testing Java Programs using OCL](https://reader036.vdocument.in/reader036/viewer/2022062305/568160ae550346895dcfcfc2/html5/thumbnails/20.jpg)
OCL 09 20
XMF access to EMF@Class Account metaclass EMFClass EMFDescriptor("sales.impl.AccountImpl","sales","sales","test.EObjectMOP") @Constructor(cid) endend@Class AccountsSystem metaclass EMFClass EMFDescriptor("sales.impl.AccountsSystemImpl","sales","sales","test.EObjectMOP")end@Class SalesSystem metaclass EMFClass EMFDescriptor("sales.impl.SalesSystemImpl","sales","sales","test.EObjectMOP") @Operation init() self.accountsSystem := AccountsSystem() end @Operation addAccount(cid:String) accountsSystem.getAccounts().add(Account(cid)) end @Operation checkNameExists(name:String):Boolean accountsSystem.accounts->exists(a | a.cid = name) endend
![Page 21: A MOP Based DSL for Testing Java Programs using OCL](https://reader036.vdocument.in/reader036/viewer/2022062305/568160ae550346895dcfcfc2/html5/thumbnails/21.jpg)
OCL 09 21
Implementing the DSL
![Page 22: A MOP Based DSL for Testing Java Programs using OCL](https://reader036.vdocument.in/reader036/viewer/2022062305/568160ae550346895dcfcfc2/html5/thumbnails/22.jpg)
OCL 09 22
@Operation successfulPlaceOrder (.args) let name = args->at(0) amount = args->at(1) in let preSelf = self.deepCopy() in if accountsSystem.accounts->exists(a | a.cid = name) then let result = self.send(“placeOrder”,Seq{name,amount}) in if accountsSystem.getAccount(name).items->exists(...) and ... then CallSucceeds(result,”placeOrder”,args) else PostFails(“placeOrder”,args) end end else PreFails(“placeOrder”,args) end end endend
@MSpec successfulPlaceOrder[placeOrder](name,amount) pre accountsSystem.accounts->exists(a | a.cid = name) do run post accountsSystem.getAccount(name).items->exists(...) and ... end
Example