DolphinScheduler源码分析之EntityTestUtils类
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.dolphinscheduler.common.task;
18
19 import java.lang.reflect.Constructor;
20 import java.lang.reflect.InvocationTargetException;
21 import java.lang.reflect.Method;
22 import java.util.*;
23
24 /**
25 * entity test utils
26 * //该类是一个工具类,可以对传入的任何其他类进行检测,会依次调用类中的所有方法,用了反射机制。
27 */
28 public class EntityTestUtils {
29 //初始化一个用来存储测试用的类型和对应的数值map数组
30 private static final Map<String, Object> OBJECT_MAP = new HashMap<>();
31 //遇到这些方法直接跳过,不测试。
32 private static final String SKIP_METHOD = "getClass,notify,notifyAll,wait,equals,hashCode,clone";
33 String
34 static {
35 //初始化参数数组
36 OBJECT_MAP.put("java.lang.Long", 1L);
37 OBJECT_MAP.put("java.lang.String", "test");
38 OBJECT_MAP.put("java.lang.Integer", 1);
39 OBJECT_MAP.put("int", 1);
40 OBJECT_MAP.put("long", 1L);
41 OBJECT_MAP.put("java.util.Date", new Date());
42 OBJECT_MAP.put("char", '1');
43 OBJECT_MAP.put("java.util.Map", new HashMap());
44 OBJECT_MAP.put("boolean", true);
45 }
46 //传入要测试的类,可以看到,入参是一个Class类型的数组,代表可以同时测试多个类
47 public static void run(List<Class> classList)
48 throws IllegalAccessException, InvocationTargetException, InstantiationException {
49 //一个个来,先取出第一个要测试的Class,比如是String.class
50 for (Class temp : classList) {
51 Object tempInstance = new Object();
52 //获取要测试的类中所有的构造方法,不管是有参构造方法还是无参构造方法,比如String.class中的String()构造方法和String(String original) 等等
53 Constructor[] constructors = temp.getConstructors();
54 //遍历所有的构造方法
55 for (Constructor constructor : constructors) {
56 //获取构造方法的参数类型,如果是无参构造方法,就会进入下面的if,如果是有参构造方法,就会进入下面的else去。
57 final Class<?>[] parameterTypes = constructor.getParameterTypes();
58 //无参构造方法,会进入这里的if
59 if (parameterTypes.length == 0) {
60 //用反射的机制,直接调用了这个无参构造方法,相当于完成了测试
61 tempInstance = constructor.newInstance();
62 } else {
63 //objects中存储的是参数的值,如果构造方法需要传入第一个是int,第二个是String,那么这个objects其实就是传入了(1,‘1’)这两个值
64 Object[] objects = new Object[parameterTypes.length];
65 //这里就是往objects中塞数据
66 for (int i = 0; i < parameterTypes.length; i++) {
67 objects[i] = OBJECT_MAP.get(parameterTypes[i].getName());
68 }
69 //用反射的机制,直接调用了这个有参数的构造方法,相当于完成了测试
70 tempInstance = constructor.newInstance(objects);
71 }
72 }
73 //这里是对类中的普通方法(除了构造方法外的所有其他方法)做测试
74 Method[] methods = temp.getMethods();
75 //遍历所有的普通方法
76 for (final Method method : methods) {
77 //如果是需要跳过不进行测试的方法,那么就直接跳过
78 if (SKIP_METHOD.contains(method.getName())) {
79 break;
80 }
81 //同样也是获取到参数的类型
82 final Class<?>[] parameterTypes = method.getParameterTypes();
83 //如果是有参方法,进入if,无参方法进入else
84 if (parameterTypes.length != 0) {
85 //objects中存储的是参数的值,如果构造方法需要传入第一个是int,第二个是String,那么这个objects其实就是传入了(1,‘1’)这两个值
86 Object[] objects = new Object[parameterTypes.length];
87 //这里就是往objects中塞数据
88 for (int i = 0; i < parameterTypes.length; i++) {
89 objects[i] = OBJECT_MAP.get(parameterTypes[i].getName());
90 }
91 //用反射的机制,直接调用了这个有参数的方法,相当于完成了测试
92 method.invoke(tempInstance, objects);
93 } else {
94 //用反射的机制,直接调用了这个无参的方法,相当于完成了测试
95 method.invoke(tempInstance);
96 }
97 }
98 }
99 }
100 }
以下是DolphinScheduler源码中对这个类进行使用的方式,可以参考:
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.dolphinscheduler.common.task;
18
19 import org.apache.dolphinscheduler.common.task.sqoop.SqoopParameters;
20 import org.apache.dolphinscheduler.common.task.sqoop.sources.SourceHdfsParameter;
21 import org.apache.dolphinscheduler.common.task.sqoop.sources.SourceHiveParameter;
22 import org.apache.dolphinscheduler.common.task.sqoop.sources.SourceMysqlParameter;
23 import org.apache.dolphinscheduler.common.task.sqoop.targets.TargetHdfsParameter;
24 import org.apache.dolphinscheduler.common.task.sqoop.targets.TargetHiveParameter;
25 import org.apache.dolphinscheduler.common.task.sqoop.targets.TargetMysqlParameter;
26 import org.junit.Assert;
27 import org.junit.Test;
28 import java.util.ArrayList;
29 import java.util.List;
30
31 /**
32 * sqoop parameter entity test
33 */
34 public class SqoopParameterEntityTest {
35
36 @Test
37 public void testEntity(){
38 try {
39 List<Class> classList = new ArrayList<>();
40 classList.add(SourceMysqlParameter.class);
41 classList.add(SourceHiveParameter.class);
42 classList.add(SourceHdfsParameter.class);
43 classList.add(SqoopParameters.class);
44 classList.add(TargetMysqlParameter.class);
45 classList.add(TargetHiveParameter.class);
46 classList.add(TargetHdfsParameter.class);
47 EntityTestUtils.run(classList);
48 } catch (Exception e) {
49 Assert.fail(e.getMessage());
50 }
51 }
52 }
DolphinScheduler源码分析之EntityTestUtils类的更多相关文章
- DolphinScheduler 源码分析之 DAG类
1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license ...
- DolphinScheduler源码分析
DolphinScheduler源码分析 本博客是基于1.2.0版本进行分析,与最新版本的实现有一些出入,还请读者辩证的看待本源码分析.具体细节可能描述的不是很准确,仅供参考 源码版本 1.2.0 技 ...
- JUC源码分析-其它工具类(一)ThreadLocalRandom
JUC源码分析-其它工具类(一)ThreadLocalRandom ThreadLocalRandom 是 JDK7 在 JUC 包下新增的随机数生成器,它解决了 Random 在多线程下多个线程竞争 ...
- DolphinScheduler源码分析之任务日志
DolphinScheduler源码分析之任务日志 任务日志打印在调度系统中算是一个比较重要的功能,下面就简要分析一下其打印的逻辑和前端页面查询的流程. AbstractTask 所有的任务都会继承A ...
- Struts2 源码分析——Action代理类的工作
章节简言 上一章笔者讲到关于如何加载配置文件里面的package元素节点信息.相信读者到这里心里面对struts2在启动的时候加载相关的信息有了一定的了解和认识.而本章将讲到关于struts2启动成功 ...
- 转:Ogre源码分析之Root类、Facade模式
Ogre源码分析(一)Root类,Facade模式 Ogre中的Root对象是一个Ogre应用程序的主入口点.因为它是整个Ogre引擎的外观(Façade)类.通过Root对象来开启和停止Ogre是最 ...
- 源码分析——Action代理类的工作
Action代理类的新建 通过<Struts2 源码分析——调结者(Dispatcher)之执行action>章节我们知道执行action请求,最后会落到Dispatcher类的serv ...
- 精尽Spring Boot源码分析 - SpringApplication 启动类的启动过程
该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...
- mybatis源码分析(3)——SqlSessionManager类
从上图可能看出,在 mybatis中,SqlSession的实现类有两个,其中SqlSessionManager类不但实现了SqlSession接口,同时也实现了SqlSessionFactory接口 ...
随机推荐
- 4.简单的 GNU 汇编语法
芯片启动时很多设备没有初始化,需要汇编语言进行准备. 简单的GNU汇编语法: 1 label: instruction @ comment label :标号,类似于外号,为所在位置做标号,可以通过这 ...
- Rejecting mapping update to [xxx] as the final mapping would have more than 1 type: [xxx, xx]
说明: 1.elasticsearch 版本 6.3.1 2.在同一个index下创建两个type时报错,信息如下: 在创建第二个type:solr时,先前已经在相同索引下创建了一个type:es [ ...
- MySQL更新勿用and
项目实战 一次错误的更新 更新前的数据 执行更新语句 然后我们查看下更新后的数据,发现居然数据为空? 使用主键id的方式查询这条数据,发现需要更新的手机号码居然变为了0 当我们把更新语句中的and ...
- Vue.nextTick()的使用
什么是Vue.nextTick()?? 定义:在下次 DOM 更新循环结束之后执行延迟回调.在修改数据之后立即使用这个方法,获取更新后的 DOM. 所以就衍生出了这个获取更新后的DOM的Vue方法.所 ...
- 【Java基础】Java8 新特性
Java8 新特性 Lambda 表达式 Lambda 是一个匿名函数,我们可以把 Lambda 表达式理解为是一段可以传递的代码(将代码像数据一样进行传递).使用它可以写出更简洁.更灵活的代码. L ...
- DTCC 2020 | 阿里云李飞飞:云原生分布式数据库与数据仓库系统点亮数据上云之路
简介: 数据库将面临怎样的变革?云原生数据库与数据仓库有哪些独特优势?在日前的 DTCC 2020大会上,阿里巴巴集团副总裁.阿里云数据库产品事业部总裁.ACM杰出科学家李飞飞就<云原生分布式数 ...
- idea中文注释出现乱码,我靠自己解决了
如果你像我一样️,查遍google百度,半天下来还是找不到解决方案,说不定这篇博客能帮助你顺利解决呢 好了,那么开始说说我是怎么解决麻烦的. 首先,我想打开一份java文稿.光预览,它是没有任何问题的 ...
- python中re模块的使用(正则表达式)
一.什么是正则表达式? 正则表达式,又称规则表达式,通常被用来检索.替换那些符合某个模式(规则)的文本. 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合, ...
- DHCP最佳实践(二)
这是Windows DHCP最佳实践和技巧的最终指南. 如果您有任何最佳做法或技巧,请在下面的评论中发布它们. 在本指南(二)中,我将分享以下DHCP最佳实践和技巧. 从DHCP作用域中排除IP 了解 ...
- 二进制部署kubernetes
Kubernetes二进制安装 环境准备: 主机环境:做好主机名hosts文件映射 硬件2cpu 2G内存 192.168.30.21 k8s-master 192.168.30.22 k8s-no ...