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(三)HBase集成Phoenix创建二级索引

    一.Hbase集成Phoneix 1.下载 在官网http://www.apache.org/dyn/closer.lua/phoenix/中选择提供的镜像站点中下载与安装的HBase版本对应的版本. ...

  2. react项目中实现搜索关键字呈现高亮状态(一)

    最近有个需求,在一个react项目中,实现搜索关键字呈现高亮状态.这个在普通的html文件中还好操作些,在react项目中有点懵逼了,因为react项目中很少操作dom,有点无从下手.但最后还是实现了 ...

  3. git的基础知识

    git 分布式版本控制工具 具备的功能 协同开发 多人并行不悖修改服务器端的同一个文件 数据备份 不仅保持目录和文件当前状态,还能保存每一个提交的历史版本 版本管理 保存每一个版本的文件信息的时候做到 ...

  4. Go从入门到放弃(笔记存档)

    前言 考虑到印象笔记以后不续费了,这里转存到博客园一份 因内容是自己写的笔记, 未作任何润色, 所以看着很精简, 请见谅 查看官方文档 在新的go安装包中,为了减小体积默认去除了go doc 安装go ...

  5. PAT天梯赛练习 L3-003 社交集群 (30分) DFS搜索

    题目分析: 一共有N个编号为1~1000的人,以及一共有编号为1~1000种不同的兴趣,在题目给出1~N编号的人员每个人喜欢的兴趣的id后,要求统计出不同的人员集合的个数以及每个人员几个的人数从大到小 ...

  6. 使用nodejs和express搭建http web服务

    目录 简介 使用nodejs搭建HTTP web服务 请求nodejs服务 第三方lib请求post 获取http请求的正文 Express和使用express搭建http web服务 express ...

  7. cmd的终结工具cmder

    常用快捷键 win+alt+t  打开任务设置窗口 win+alt+k 打开快捷键设置窗口 自定义屏幕分割窗口快捷键: ctl+shift+s 水平按50%比例分割 ctl+shift+v 垂直按50 ...

  8. os.system('cmd')在linux和windows系统下返回值的差异

    今天,用os.system('cmd')分别在windows和linux平台上执行同一ping命令,命令执行失败时返回码不同,windows为1,而linux下返回为256,如下: linux下: & ...

  9. 02--Docker配置阿里云镜像加速器

    1.登录阿里云控制台,在产品与服务中收索 "容器镜像服务" 2.点击镜像加速器,CentOS 3.在路径 /etc/docker/daemon.json 下配置加速器地址 4.重新 ...

  10. CSS响应式布局学习笔记(多种方法解决响应式问题)

    在做web开发的工作中,会遇到需要我给页面根据设计的要求,进行响应式布局,这里跟大家分享下我对于响应式布局的解决方法: 我主要利用的是CSS3 媒体查询,即media queries,可以针对不同的媒 ...