SAP CDS view单元测试框架中的访问者(Visitor)设计模式使用介绍-阿里云开发者社区

开发者社区> c位出道> 正文

SAP CDS view单元测试框架中的访问者(Visitor)设计模式使用介绍

简介: 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.

image.png

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.


image.png

( graph from post: http://www.cnblogs.com/zhenyulu/articles/79719.html )

image.png

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.

image.png

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.


image.png

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.


image.png

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.

image.png

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.


image.png

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.

image.png

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.

image.png

The sample output should be:

image.png

Solution

(1) Since now text annotation is also concerned, we have to now register its mapping with corresponding handler method name PRINT_ANNOTATION:


image.png

(2) Implement PRINT_ANNOTATION, and that’s all.

image.png


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Android Studio 通过一个登录功能介绍SQLite数据库的使用
前言:         SQLite简介:是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中。它是D.RichardHipp建立的公有领域项目。它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。
4230 0
Java描述设计模式(23):访问者模式
本文源码:GitHub·点这里 || GitEE·点这里 一、生活场景 1、场景描述 电竞是游戏比赛达到“竞技”层面的体育项目。利用电子设备作为运动器械进行的、人与人之间的智力对抗运动。通过电竞,可以提高人的反应能力、协调能力、团队精神等。
11551 0
Redis介绍使用及进阶
Redis介绍使用及进阶目录: 一、介绍 二、缓存问题 三、Redis内存滥用 四、键命名规范 五、Redis使用场景 六、持久化操作 七、.Net Core 使用redis 简单介绍 一、介绍 1、 高性能-- Redis能读的速度是110000次/s,写的速度是81000次/s 、c语言实现距...
1008 0
+关注
2316
文章
0
问答
来源圈子
更多
+ 订阅
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载