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类的更多相关文章

  1. DolphinScheduler 源码分析之 DAG类

    1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license ...

  2. DolphinScheduler源码分析

    DolphinScheduler源码分析 本博客是基于1.2.0版本进行分析,与最新版本的实现有一些出入,还请读者辩证的看待本源码分析.具体细节可能描述的不是很准确,仅供参考 源码版本 1.2.0 技 ...

  3. JUC源码分析-其它工具类(一)ThreadLocalRandom

    JUC源码分析-其它工具类(一)ThreadLocalRandom ThreadLocalRandom 是 JDK7 在 JUC 包下新增的随机数生成器,它解决了 Random 在多线程下多个线程竞争 ...

  4. DolphinScheduler源码分析之任务日志

    DolphinScheduler源码分析之任务日志 任务日志打印在调度系统中算是一个比较重要的功能,下面就简要分析一下其打印的逻辑和前端页面查询的流程. AbstractTask 所有的任务都会继承A ...

  5. Struts2 源码分析——Action代理类的工作

    章节简言 上一章笔者讲到关于如何加载配置文件里面的package元素节点信息.相信读者到这里心里面对struts2在启动的时候加载相关的信息有了一定的了解和认识.而本章将讲到关于struts2启动成功 ...

  6. 转:Ogre源码分析之Root类、Facade模式

    Ogre源码分析(一)Root类,Facade模式 Ogre中的Root对象是一个Ogre应用程序的主入口点.因为它是整个Ogre引擎的外观(Façade)类.通过Root对象来开启和停止Ogre是最 ...

  7. 源码分析——Action代理类的工作

     Action代理类的新建 通过<Struts2 源码分析——调结者(Dispatcher)之执行action>章节我们知道执行action请求,最后会落到Dispatcher类的serv ...

  8. 精尽Spring Boot源码分析 - SpringApplication 启动类的启动过程

    该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...

  9. mybatis源码分析(3)——SqlSessionManager类

    从上图可能看出,在 mybatis中,SqlSession的实现类有两个,其中SqlSessionManager类不但实现了SqlSession接口,同时也实现了SqlSessionFactory接口 ...

随机推荐

  1. Phoneix(一)简介及常用命令

    一.简介 Apache Phoneix是运行在HBase之上的高性能关系型数据库,通过Phoneix可以像使用jdbc访问关系型数据库一样访问HBase. Phoneix操作的表以及数据存储在HBas ...

  2. hive之Json解析(普通Json和Json数组)

    一.数据准备 现准备原始json数据(test.json)如下: {"movie":"1193","rate":"5", ...

  3. 容器编排系统K8s之Pod Affinity

    前文我们了解了k8s上的NetworkPolicy资源的使用和工作逻辑,回顾请参考:https://www.cnblogs.com/qiuhom-1874/p/14227660.html:今天我们来聊 ...

  4. 关于es6 let var const 以及Symbol的总结

    ```javascript //es6新增块级作用域.声明变量用关键字let const , // es5中只有函数作用域和全局作用域,声明变量用关键字var  // let 和const 声明的变量 ...

  5. navicat for mysql 破解版

    Navicat for MySQL下载地址:Navicat for MySQL 软件和破解程序 第1步.安装Navicat软件,最后点击完成 第2步.安装成功之后还要进行破解.点击patchNavic ...

  6. LeetCode145 二叉树的后序遍历

    给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? /** * Defin ...

  7. 开源:AspNetCore 应用程序热更新升级工具(全网第一份公开的解决方案)

    1:下载.开源.使用教程 下载地址:Github 下载 .其它下载 开源地址:https://github.com/cyq1162/AspNetCoreUpdater 使用教程: 解压AspNetCo ...

  8. Java Mybatis快速入门之基本使用

    目录 搭建环境 编写 Mybatis 核心配置文件 pom导出资源失败 测试 搭建环境 新建Maven项目 导入Maven依赖 <dependencies> <!--mysql驱动- ...

  9. fsutil比较有用的几个命令

    Fsutil:fsinfo 主要由专业支持者使用.列出所有驱动器,查询驱动器类型,查询卷信息,查询特定的 卷信息或文件系统统计信息. 语法参数 drives 列出计算机中所有的驱动器. drivety ...

  10. 前端开发好帮手,eslint配置全知道

    eslint让人又爱又恨,原因在于它的默认配置非常严格,动则一个小提示就直接报错不给运行.而在开发调试的过程中,我们想时时得到运行效果,它的严格又很烦. 在安装eslint后,我们可以在package ...