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接口 ...
随机推荐
- window10 安装Mysql 8.0.17以及忘记密码重置密码
一.安装Mysql8.0.17 1:首先去官网下载安装包 下载地址:https://dev.mysql.com/downloads/mysql/ 2:将解压文件解压到你安装的目录:D:\mysql\m ...
- Elasticsearch分页解决方案
一.命令的方式做分页 1.常见的分页方式:from+size elasticsearch默认采用的分页方式是from+size的形式,但是在深度分页的情况下,这种使用方式的效率是非常低的,比如from ...
- java之volatile
一.谈谈对volatile的理解 volatile是java虚拟机提供的轻量级的同步机制 保证可见性.不保证原子性.禁止指令重排 1.可见性理解:所有线程存放都是主内存的副本(比如某个变量值为25), ...
- Docker环境下升级PostgreSQL
查阅PostgreSQL官方文档可以得知,官方提供了两种方式对数据库进行升级--pg_dumpall与pg_upgrade. pg_dumpall是将数据库转储成一个脚本文件,然后在新版数据库中可以直 ...
- 全网最牛X的!!! MySQL两阶段提交串讲
目录 一.吹个牛 二.事务及它的特性 三.简单看下两阶段提交的流程 四.两阶段写日志用意? 五.加餐:sync_binlog = 1 问题 六.如何判断binlog和redolog是否达成了一致 七. ...
- Linux常用命令(df&dh)
在Linux下查看磁盘空间使用情况,最常使用的就是du和df了.然而两者还是有很大区别的,有时候其输出结果甚至非常悬殊. du的工作原理 du命令会对待统计文件逐个调用fstat这个系统调用,获取文件 ...
- Head First 设计模式 —— 09. 模版方法 (Template Method) 模式
模板方法模式 在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤. P289 特点 主导算法框架,并且保护这个算法 P28 ...
- JDK1.7-HashMap原理
JDK1.7 HashMap 如何在源码上添加自己的注释 打开jdk下载位置 解压src文件夹,打开idea,ctrl+shift+alt+s打开项目配置 选择jdk版本1.7,然后点击Sourcep ...
- 【JDBC核心】操作 BLOB 类型字段
操作 BLOB 类型字段 MySQL BLOB 类型 MySQL 中,BLOB 是一个二进制大型对象,是一个可以存储大量数据的容器,它能容纳不同大小的数据. 插入 BLOB 类型的数据必须使用 Pre ...
- 【Java基础】Java8 新特性
Java8 新特性 Lambda 表达式 Lambda 是一个匿名函数,我们可以把 Lambda 表达式理解为是一段可以传递的代码(将代码像数据一样进行传递).使用它可以写出更简洁.更灵活的代码. L ...