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.
要获取更多Jerry的原创文章,请关注公众号"汪子熙":
相关文章
- SAP 电商云 Spartacus 服务器端渲染的单步调试详细步骤
- 关于 SAP UI5 所有控件的共同祖先 - sap.ui.base.ManagedObject
- SAP UI5 sap.ui.base.ManagedObject 的构造函数参数讲解
- SAP_FICO常用BAPI
- SAP UI5 sap.ui.Device.media 的使用介绍
- SAP UI5 SmartTable column 的个性化设置使用概述
- [ChatGPT 勘误] SAP UI5 的 sap.ui.base.Object
- SAP ABAP 如何为某个用户创建指定的激活变体(Activation Variant)
- SAP Fiori Belize 主题应用在 SAPGUI 里的一些要点
- 关于 SAP UI5 接口 sap.ui.core.IAsyncContentCreation 的问题讨论
- SAP UI5 里响应式表格的 minScreenWidth 属性讲解
- SAP MM 采购订单的Output Type NEU要等其它Output完成后才能成功输出
- 深入比较:SAP vs Oracle(sap和oracle)
- SAP 创建 Web Services详解编程语言
- SAP/SD – 做SD你要知道的透明表详解编程语言
- SAP NWBC for HTML and Desktop configuration steps[From sdn]详解编程语言
- [代码]如何上载图片到SAP数据库并显示详解编程语言
- 用SAP浏览网页详解编程语言
- sap alv grid 中的delete按键问题详解编程语言
- SAP可以创建预留的移动类型详解编程语言
- SAP 库存相关表格详解编程语言
- SAP Job 状态记录详解编程语言
- SAP中MTO & MTS的区别分析详解编程语言
- sap保存时小写字母自动转大写字母的解决方法详解编程语言
- Reading and Writing data on application server in SAP详解编程语言
- SAP vs Oracle:差异点与解析(sap和oracle的区别)
- 调用SAP接口:MSSQL实现自动化(mssql调用sap接口)
- 系统Oracle公司收购SAP系统开启新的商业时代(Oracle公司sap)
- Oracle与SAP金蝶谁更具竞争力(oracle sap金蝶)
- Oracle与SAP的技术潜力比较(oracle sap比较)
- 探索 Oracle 与 SAP 技术的开发可能性(oracle sap开发)
- Oracle 与 SAP深度对比分析(oracle sap对比)