SAP CDS view单元测试框架中的访问者(Visitor)设计模式使用介绍
As I mentioned in my blog My CDS view self study tutorial – Part 11 CDS view test double framework, we develop CDS view on top of various database table, and CDS view test double framework extracts all involved database table names starting from CDS view source code using Visitor design pattern.
Paul Hardy has written a series of excellent blogs about Design Patterns in ABAP – Visitor. And in my blog, I will use a real example which uses this design pattern in SAP standard implementation to demonstrate the usage of this pattern.
The source code of my CDS view, where database table MARA and MAKT are used.
@AbapCatalog.sqlViewName: 'PRODSHTEXT'
@VDM.viewType: #BASIC
@AccessControl.authorizationCheck: #NOT_REQUIRED
@ClientHandling.algorithm: #SESSION_VARIABLE
@EndUserText.label: 'Product Description'
define view ProductShortText
as select from makt
inner join mara on makt.matnr = mara.matnr
{
key mara.scm_matid_guid16 as ProductGuid,
key makt.spras as Language,
makt.maktx as ProductName,
makt.maktg as ProductNameLarge
}
The aim of CDS view test double framework: extract all database tables used in a given CDS view, let’s say this logic is implemented in a method.
So input of this method: CDS view name, in my example it isPRODUCTSHORTTEXT.
Output: an internal table containing used database table names. In my example it is [ ‘MARA’, ‘MAKT’ ]
How could this task be done by Visitor pattern?
Let’s first refresh our memory on typical roles involved in Visitor pattern.
Let’s categorize the real classes we see in SAP system according to this graph below.
( graph from post: http://www.cnblogs.com/zhenyulu/articles/79719.html )
If you follow my previous blog My CDS view self study tutorial – Part 11 CDS view test double framework to launch unit test, you can set a breakpoint on line 411 in method below, which is an entry point for Visitor pattern to start.
You might get lost in the debugging of the tremendous code in CDS view test double framework, so in order to make your debugging and learning easier, I extract the Visitor code in SAP standard implementation and simplify it into a standalone report which only contains 139 lines of code now.
You can simply set breakpoint on method accept in line 135 and run report to debug the Visitor behavior.
Execute the report and you will see MAKT and MARA are printed as output. The code is tested in my system with ABAP 7.52.
Of course at first you should have a CDS view to visit, you can create new CDS view by copying the source code I provided in the beginning of this blog or use your own CDS view instead.
The other steps that the CDS source code is loaded from DB and parsed via a Kernel module into an intermediate XML format and then into an AST ( Abstract Syntax Tree ) are not relevant to the topic of this blog so they will not be explained in detail.
Since I am only interested with involved table names, so in my local visitor implementation for method get_mapping, I only register corresponding concrete element’s class name and operation when this element is visited.
METHOD if_qlast_visitor~get_mapping.
mapping = VALUE #( ( classname = 'CL_QLAST_TABLE_DATASOURCE' method = 'VISIT_TABLE_DATASOURCE' )
).
ENDMETHOD.
The purpose of above code: When visitor actually visits the concrete element CL_QLAST_TABLE_DATASOURCE, method VISIT_TABLE_DATASOURCE will be called to record detail information of visited database table.
Although nodes in the AST will be visited one by one, since I only registered CL_QLAST_TABLE_DATASOURCE so most of them will be filtered out via IF sy-subrc = 0 in line 10, which means no actual operation will be performed on those irrelevant nodes.
Only operations on table nodes will be performed twice, since in my test CDS view there are totally two tables MAKT and MARA used.
An exercise
The requirement changes now, you are asked to also print out the annotation text specified in the header of CDS view.
The sample output should be:
Solution
(1) Since now text annotation is also concerned, we have to now register its mapping with corresponding handler method name PRINT_ANNOTATION:
(2) Implement PRINT_ANNOTATION, and that’s all.
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 【动手学深度学习笔记】之通过丢弃法缓解过拟合问题
- 【动手学深度学习笔记】之通过权重衰减法解决过拟合问题
- 【动手学深度学习笔记】之多层感知机实现
- Linux程序员效率工具:比man更好用的命令提示工具
- 我对torch中的gather函数的一点理解
- 冒泡排序的实现思路和优化方案
- Java Map转对象
- 59.Vue 使用webpack构建vue项目
- Android初学设置文字跑马灯效果
- 使用Zolom内存解析运行python脚本(不落地)
- 要点3:输入函数对比与自定义输入方式
- 性能测试必备命令(3)- lscpu
- 性能测试必备命令(2)- uptime
- Lua/luajit 点与冒号的区别
- vim的几种模式mode和按键映射map