Entitymock DSL

From OS.bee documentation
Revision as of 12:58, 30 March 2017 by Yang (Talk | contribs) (Documentation)

Jump to: navigation, search

Copyright Notice

All rights are reserved by Compex Systemhaus GmbH. In particular, duplications, translations, microfilming, saving and processing in electronic systems are protected by copyright. Use of this manual is only authorized with the permission of Compex Systemhaus GmbH. Infringements of the law shall be punished in accordance with civil and penal laws. We have taken utmost care in putting together texts and images. Nevertheless, the possibility of errors cannot be completely ruled out. The Figures and information in this manual are only given as approximations unless expressly indicated as binding. Amendments to the manual due to amendments to the standard software remain reserved. Please note that the latest amendments to the manual can be accessed through our helpdesk at any time. The contractually agreed regulations of the licensing and maintenance of the standard software shall apply with regard to liability for any errors in the documentation. Guarantees, particularly guarantees of quality or durability can only be assumed for the manual insofar as its quality or durability are expressly stipulated as guaranteed. If you would like to make a suggestion, the Compex Team would be very pleased to hear from you.

(c) 2016-2024 Compex Systemhaus GmbH

Documentation

EntityMockDSL.pdf

Introduction

Mock object

In object-oriented programming, mock objects are simulated objects that mimic the behavior of real objects in controlled ways. In a unit test, mock objects can simulate the behavior of complex, real objects and are therefore useful when a real object is impractical or impossible to incorporate into a unit test. Mock objects have the same interface as the real objects they mimic, allowing a client object to remain unaware of whether it is using a real object or a mock object. Many available mock object frameworks allow the programmer to specify which, and in what order, methods will be invoked on a mock object and what parameters will be passed to them, as well as what values will be returned. Thus, the behavior of a complex object such as a network socket can be mimicked by a mock object, allowing the programmer to discover whether the object being tested responds appropriately to the wide variety of states such mock objects may be in.


EntitymockDSL

In our implementation, we use a complex database, which would have to be initialized. If you do hand-entering data one by one, you will waste so much time to build up the volume and variety of data that we need. So we design to generate all mock objects for our database by initialization using entitymockDSL.

The main semantic elements of the EntitymockDSL are:

  • “mock entitymodel” - the root element that contains all the other elements. A model can contain multiple mock entitymodels.
  • “import” declarations - used to import external models or even Java classes.
  • “datainterchanges” - define the imported datainterchange model to fill the mock data.
  • “resources” - define all availabilities of a resource definition. Resources will be used as enum in objects or directly in the definition of mock entities.
  • “objects” - contains consistent information for some kind of object definition. Objects are used as template for mock entities.
  • “mock entities” - define the mocking rules of entities.


Mock entitymodel

mock entitymodel defines a set of mocking rules for an <imported entity model>, which has one or more entity definitions. Name of the mock entitymodel is defined according to the name of <imported entity model>, by just changing “entities.*” to “entitiesmock”. A folder with the same name of the mock entitymodel name will be generated in folder /src-gen. several new java files will be auto-generated in this folder: 1. EntityMockGenerator.java file. 2. For each resource, a file named as “Resource”+<resource name>+“.java” will be generated. 3. For each object, a file named as “Object”+<object name>+“.java” will be generated. 4. For each element in mock entities, a file named as “Entity”+<mocking name>+“.java” will be generated.

Syntax:

mock entitymodel for <imported entity model> {
[run with priority <priority number>]
	import <import models/class name>
	. . .
	[datainterchanges {. . .}]
	[resources {
resource <resouce name> {. . .}
. . .}] 
	[objects {
object <object name> {. . .}

. . .}] 
	mock entities {
mocking <mocking name> . . . {. . .}

. . .}
}
  • The orientation entity model is defined as <imported entity model>, for which your mock entitymodel will fill the mocking data.
  • priority is an optional definition, if more than 1 mock entitymodel package defined in one project, you can use this option to define the priority of filling mocking data. Default value is 10.
  • As same as other dsl models, import declarations must be defined before other elements, all useful external models or java classes need to be import before using.
  • datainterchanges, resources and objects are optional be defined before the definition of mock entities. The detail description of them will be introduced one by one in the next chapters.

Example:

mock entitymodel for org.osbp.myfirstshop.entities.* {
    	run with priority 2
	import java.util.Random 
	import org.osbp.myfirstshop.datainterchange.* 
	. . .
}

Notes:

  • In this example, the mock entitymodel name is org.osbp.myfirstshop.entitiesmock.