Difference between revisions of "Entitymock DSL"

From OS.bee documentation
Jump to: navigation, search
(Mock entitymodel)
(Documentation)
Line 11: Line 11:
 
In object-oriented programming, mock objects are simulated objects that mimic the behavior of real objects in controlled ways.
 
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.  
 
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.
 
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.
  
Line 32: Line 33:
 
mock entitymodel defines a set of mocking rules for an <imported entity model>, which has one or more entity definitions.
 
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”.
 
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:
 
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.
 
1. EntityMockGenerator.java file.
 
2. For each resource, a file named as “Resource”+<resource name>+“.java” will be generated.
 
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.
 
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.
 
4. For each element in mock entities, a file named as “Entity”+<mocking name>+“.java” will be generated.
 +
  
 
► '''Syntax''':
 
► '''Syntax''':
Line 59: Line 63:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
* The orientation entity model is defined as <imported entity model>, for which your mock entitymodel will fill the mocking data.
 
* 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.
 
* 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.
 
* 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.
 
* 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''':
 
► '''Example''':
Line 76: Line 82:
 
'''Notes:'''
 
'''Notes:'''
 
*In this example, the mock entitymodel name is org.osbp.myfirstshop.entitiesmock.
 
*In this example, the mock entitymodel name is org.osbp.myfirstshop.entitiesmock.
 +
 +
====Important definition====
 +
 +
Some important definitions will be described here.
 +
 +
The first 2 definitions are basic definitions, which will be used in other definitions; the next 3 definitions are used in objects and the last 3 definitions are used in mock entities.
 +
 +
=====PropertyFillerType=====
 +
 +
PropertyFillerType is used to generate random mock data.
 +
 +
It could be a random date
 +
► '''Syntax 1''':
 +
<syntaxhighlight lang="java">future date <furtureYear number> years </syntaxhighlight>
 +
<syntaxhighlight lang="java">past date <pastYear number> years </syntaxhighlight>
 +
<syntaxhighlight lang="java">date in range <beginYear SingedNumber> up to and including <endYear SingedNumber> years </syntaxhighlight>
 +
 +
► '''Example 1''':
 +
<syntaxhighlight lang="java">date in range -50 up to and including -20 years</syntaxhighlight>
 +
generates a random date in range from  last 50 years to last 20 years
 +
 +
 +
or a random text
 +
► '''Syntax 2''':
 +
<syntaxhighlight lang="java">text from (<text1> [<text2> . . .] )</syntaxhighlight>
 +
<syntaxhighlight lang="java">paragraphs [<count number>] </syntaxhighlight>
 +
<syntaxhighlight lang="java">sentences [<count number>] </syntaxhighlight>
 +
<syntaxhighlight lang="java">words [<count number>] </syntaxhighlight>
 +
► '''Example 2''':
 +
<syntaxhighlight lang="java">sentences 5 </syntaxhighlight> generates random 5 sentences.
 +
 +
or a random number
 +
► '''Syntax 3''':
 +
<syntaxhighlight lang="java">boolean</syntaxhighlight>
 +
<syntaxhighlight lang="java">
 +
signed double in range
 +
  [[<beginRange SingedDouble> | <entity attribute>] up to and including
 +
  <endRange SingedDouble> | <entity attribute>]
 +
  with <decimal number> decimals [round to <round double>]
 +
</syntaxhighlight>
 +
<syntaxhighlight lang="java">signed double from (<signed double1> [<signed double2> . . .] )</syntaxhighlight>
 +
<syntaxhighlight lang="java">
 +
signed integer in range
 +
  [[<beginRange SingedNumber> | <entity attribute>] up to and including
 +
  <endRange SingedNumber> |<entity attribute> ][round to <round number>]
 +
</syntaxhighlight>
 +
<syntaxhighlight lang="java">signed integer from (<signed number1> [<signed number2> . . .] )</syntaxhighlight>
 +
<syntaxhighlight lang="java">
 +
unsigned double in range [[<beginRange double> | <entity attribute>]
 +
  up to and including <endRange double> | <entity attribute>]
 +
  with <decimal number> decimals [round to <round double>]
 +
</syntaxhighlight>
 +
<syntaxhighlight lang="java">unsigned double from (<double1> [<double2> . . .] )</syntaxhighlight>
 +
<syntaxhighlight lang="java">
 +
unsigned integer in range [[<beginRange number> | <entity attribute>]
 +
  up to and including <endRange number> | <entity attribute> ]
 +
  [round to <round number>]
 +
</syntaxhighlight>
 +
<syntaxhighlight lang="java">unsigned integer from (<number1> [<number2> . . .] )
 +
 +
► '''Example 3''':
 +
<syntaxhighlight lang="java">unsigned double in range 10000.0 up to and including 30000.0 with 2 decimals round to 500.0</syntaxhighlight>
 +
generates a random unsigned double number  between 9999  to 30001, with 2 decimal digits and round to 500, such as 13500.00 or 24500.00 .
 +
<syntaxhighlight lang="java">signed integer from( 3 8 11 ) </syntaxhighlight>
 +
generates a random  integer number  from the numbers in the set “( )”,  in this case they are 3, 8 and 11.
 +
<syntaxhighlight lang="java">signed double in range -10 up to and including 10 with 2 decimals</syntaxhighlight>
 +
generates a random signed double number from -10.00 to 10.00 with 2 decimal digits and with no round, such as 3.15 or  -7.98.

Revision as of 13:12, 30 March 2017

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.

Important definition

Some important definitions will be described here.

The first 2 definitions are basic definitions, which will be used in other definitions; the next 3 definitions are used in objects and the last 3 definitions are used in mock entities.

PropertyFillerType

PropertyFillerType is used to generate random mock data.

It could be a random date ► Syntax 1:

future date <furtureYear number> years
past date <pastYear number> years
date in range <beginYear SingedNumber> up to and including <endYear SingedNumber> years

Example 1:

date in range -50 up to and including -20 years

generates a random date in range from last 50 years to last 20 years


or a random text ► Syntax 2:

text from (<text1> [<text2> . . .] )
paragraphs [<count number>]
sentences [<count number>]
words [<count number>]

Example 2:

sentences 5
generates random 5 sentences.

or a random number ► Syntax 3:

boolean
signed double in range
  [[<beginRange SingedDouble> | <entity attribute>] up to and including 
  <endRange SingedDouble> | <entity attribute>]
  with <decimal number> decimals [round to <round double>]
signed double from (<signed double1> [<signed double2> . . .] )
signed integer in range
  [[<beginRange SingedNumber> | <entity attribute>] up to and including
  <endRange SingedNumber> |<entity attribute> ][round to <round number>]
signed integer from (<signed number1> [<signed number2> . . .] )
unsigned double in range [[<beginRange double> | <entity attribute>] 
  up to and including <endRange double> | <entity attribute>]
  with <decimal number> decimals [round to <round double>]
unsigned double from (<double1> [<double2> . . .] )
unsigned integer in range [[<beginRange number> | <entity attribute>] 
  up to and including <endRange number> | <entity attribute> ]
  [round to <round number>]
unsigned integer from (<number1> [<number2> . . .] )

 '''Example 3''':
<syntaxhighlight lang="java">unsigned double in range 10000.0 up to and including 30000.0 with 2 decimals round to 500.0

generates a random unsigned double number between 9999 to 30001, with 2 decimal digits and round to 500, such as 13500.00 or 24500.00 .

signed integer from( 3 8 11 )

generates a random integer number from the numbers in the set “( )”, in this case they are 3, 8 and 11.

signed double in range -10 up to and including 10 with 2 decimals

generates a random signed double number from -10.00 to 10.00 with 2 decimal digits and with no round, such as 3.15 or -7.98.