优雅的写init方法

2024-09-04 20:58
文章标签 init 方法 优雅

本文主要是介绍优雅的写init方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

怎么写出高质量的init方法,以下是demo可参考实现,代码实现。
在这里插入图片描述在这里插入图片描述

public class BaseTestController {private UnitTest unitTest;public void init(String url, String accessToken){unitTest = UnitTest.getNewInstance();unitTest.setHost(url);unitTest.setClientKey(accessToken);}public void init(String url,String archiveId,String userMobile){unitTest = UnitTest.getNewInstance();unitTest.setHost(url);unitTest.setArchiveId(archiveId);unitTest.setUserMobile(userMobile);setClientKey();}public void init(String url,String simulatedLandingUrl,String archiveId,String userMobile){unitTest = UnitTest.getNewInstance();unitTest.setHost(url);unitTest.setArchiveId(archiveId);unitTest.setUserMobile(userMobile);unitTest.setSimulatedLandingUrl(simulatedLandingUrl);setClientKey();}public void initNew(String url,String serverAddr,String userMobile, String password){unitTest = UnitTest.getNewInstance();unitTest.setHost(url);unitTest.setPassword(password);unitTest.setUserMobile(userMobile);unitTest.setSimulatedLandingUrl(serverAddr);unitTest.setClientKey(loginNew(unitTest.getUserMobile(), unitTest.getPassword()));}public void init(String url,String simulatedLandingUrl,String archiveId,String userMobile,String password){unitTest = UnitTest.getNewInstance();unitTest.setHost(url);unitTest.setArchiveId(archiveId);unitTest.setUserMobile(userMobile);unitTest.setPassword(password);unitTest.setSimulatedLandingUrl(simulatedLandingUrl);setClientKey();}public void initKdb(String url, String simulatedLandingUrl,String userMobile,String password){unitTest = UnitTest.getNewInstance();unitTest.setHost(url);unitTest.setSimulatedLandingUrl(simulatedLandingUrl);unitTest.setUserMobile(userMobile);unitTest.setPassword(password);setKdbClientKey();}private void setKdbClientKey() {unitTest.setClientKey(kdbLogin(unitTest.getUserMobile(), unitTest.getPassword()));}private String kdbLogin(String userMobile,String password){Map<String,String> paramMap = new HashMap<String,String>();paramMap.put("userMobile",userMobile);paramMap.put("loginPassword",password);String clientKey= null;try {JSONObject jsonObject = JSONObject.parseObject(HttpUtilNew.postJson(unitTest.getSimulatedLandingUrl(), paramMap));clientKey = jsonObject.getJSONObject("data").getString("clientKey");} catch (Exception e) {e.printStackTrace();}return clientKey;}private String login(String userMobile, String archiveId, String password) throws Exception {Map<String,String> paramMap = new HashMap<String,String>();paramMap.put("USER_MOBILE",userMobile);paramMap.put("LOGIN_PASSWORD",password);paramMap.put("TOKEN", createToken());paramMap.put("action","simulatedLanding");String result = HttpUtil.post(unitTest.getSimulatedLandingUrl(), paramMap);try {unitTest.setClientKey(result);return result;} catch (Exception e) {e.printStackTrace();return null;}}private String createToken(){String tokenCreate = EnCodeHelper.encode(unitTest.getUserMobile()+"HFT_OPEN_API_"+new Date().getDay());return tokenCreate;}public String getClientKey() {return unitTest.getClientKey();}private void setClientKey() {try {unitTest.setClientKey(login(unitTest.getUserMobile(), unitTest.getArchiveId(),unitTest.getPassword()));} catch (Exception e) {e.printStackTrace();}}public String getResult(){return unitTest.getResult();}public String postNewErp(Object param,String interfaceName) throws Exception {URL url = new URL(unitTest.getHost()+"/"+interfaceName);System.out.println(unitTest.getHost()+"/"+interfaceName);URLConnection urlConnection = url.openConnection();// 设置doOutput属性为true表示将使用此urlConnection写入数据urlConnection.setDoOutput(true);// 定义待写入数据的内容类型,我们设置为application/x-www-form-urlencoded类型urlConnection.setRequestProperty("content-type", "application/json");urlConnection.setRequestProperty("CLIENTKEY", "d48311413b02b444d5904d2690bb0eae");urlConnection.setRequestProperty("deviceType", "1");// 得到请求的输出流对象OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());// 把数据写入请求的Bodyif(param instanceof BaseMapParam){BaseMapParam paramMap = (BaseMapParam)param;paramMap.setInteger("pageOffset", paramMap.getPageOffset());paramMap.setInteger("pageRows", paramMap.getPageRows());param = paramMap.getMap();}String jsonStr = JSON.toJSONString(param);out.write(jsonStr);out.flush();out.close();// 从服务器读取响应InputStream inputStream =  urlConnection.getInputStream();String encoding = urlConnection.getContentEncoding();String body = IOUtils.toString(inputStream, encoding);unitTest.setResult(body);return body;}public String postNewErp(Object param,String interfaceName,String clientKey) throws Exception {URL url = new URL(unitTest.getHost()+"/"+interfaceName);URLConnection urlConnection = url.openConnection();// 设置doOutput属性为true表示将使用此urlConnection写入数据urlConnection.setDoOutput(true);// 定义待写入数据的内容类型,我们设置为application/x-www-form-urlencoded类型urlConnection.setRequestProperty("content-type", "application/encrypt-json");urlConnection.setRequestProperty("CLIENTKEY", clientKey);urlConnection.setRequestProperty("deviceType", "1");// 得到请求的输出流对象OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());// 把数据写入请求的Bodyif(param instanceof BaseMapParam){BaseMapParam paramMap = (BaseMapParam)param;paramMap.setInteger("pageOffset", paramMap.getPageOffset());paramMap.setInteger("pageRows", paramMap.getPageRows());param = paramMap.getMap();}String jsonStr = JSON.toJSONString(param);System.out.println("===========请求参数:" + jsonStr);out.write(AESHelper.encode(jsonStr));out.flush();out.close();// 从服务器读取响应InputStream inputStream =  urlConnection.getInputStream();String encoding = urlConnection.getContentEncoding();String body = IOUtils.toString(inputStream, encoding);unitTest.setResult(body);return body;}public String sendGet(String url, String param) {StringBuilder result = new StringBuilder();BufferedReader in = null;try {String urlNameString = url + "?" + param;URL realUrl = new URL(urlNameString);// 打开和URL之间的连接URLConnection connection = realUrl.openConnection();// 设置通用的请求属性connection.setRequestProperty("accept", "*/*");connection.setRequestProperty("connection", "Keep-Alive");connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");// 建立实际的连接connection.connect();// 定义 BufferedReader输入流来读取URL的响应in = new BufferedReader(new InputStreamReader(connection.getInputStream()));String line = null;while ((line = in.readLine()) != null) {result.append(line);}} catch (Exception e) {System.out.println("发送GET请求出现异常!" + e);e.printStackTrace();}// 使用finally块来关闭输入流finally {try {if (in != null) {in.close();}} catch (Exception e2) {e2.printStackTrace();}}return result.toString();}public String getNewErp(String interfaceName) throws Exception {URL url = new URL(unitTest.getHost()+"/"+interfaceName);URLConnection urlConnection = url.openConnection();// 定义待写入数据的内容类型,我们设置为application/x-www-form-urlencoded类型urlConnection.setRequestProperty("CLIENTKEY", unitTest.getClientKey());urlConnection.connect();// 从服务器读取响应BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));return in.lines().collect(Collectors.joining(""));}private String loginNew(String userMobile,String password){Map<String,Object> paramMap = new HashMap<String,Object>();paramMap.put("passwordType",0);paramMap.put("userMobile",userMobile);paramMap.put("loginPassword",password);String loginResult = HttpUtil.postJson(unitTest.getSimulatedLandingUrl(), paramMap);JSONObject loginJosnObject = JSONObject.parseObject(loginResult);Integer errcode = loginJosnObject.getInteger("errCode");if (Const.ResponseCode.SUCCESS != errcode) {if (errcode.equals("1")) {throw new BusinessException("系统错误");} else {throw new BusinessException(loginJosnObject.getString("errMsg"));}}String dataStr = loginJosnObject.getString("data");JSONObject dataJsonObject = JSONObject.parseObject(dataStr);return dataJsonObject.getString("clientKey");}
}

这篇关于优雅的写init方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1137011

相关文章

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

mysql出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)的解决方法

《mysql出现ERROR2003(HY000):Can‘tconnecttoMySQLserveron‘localhost‘(10061)的解决方法》本文主要介绍了mysql出现... 目录前言:第一步:第二步:第三步:总结:前言:当你想通过命令窗口想打开mysql时候发现提http://www.cpp

Mysql删除几亿条数据表中的部分数据的方法实现

《Mysql删除几亿条数据表中的部分数据的方法实现》在MySQL中删除一个大表中的数据时,需要特别注意操作的性能和对系统的影响,本文主要介绍了Mysql删除几亿条数据表中的部分数据的方法实现,具有一定... 目录1、需求2、方案1. 使用 DELETE 语句分批删除2. 使用 INPLACE ALTER T

MySQL INSERT语句实现当记录不存在时插入的几种方法

《MySQLINSERT语句实现当记录不存在时插入的几种方法》MySQL的INSERT语句是用于向数据库表中插入新记录的关键命令,下面:本文主要介绍MySQLINSERT语句实现当记录不存在时... 目录使用 INSERT IGNORE使用 ON DUPLICATE KEY UPDATE使用 REPLACE

CentOS 7部署主域名服务器 DNS的方法

《CentOS7部署主域名服务器DNS的方法》文章详细介绍了在CentOS7上部署主域名服务器DNS的步骤,包括安装BIND服务、配置DNS服务、添加域名区域、创建区域文件、配置反向解析、检查配置... 目录1. 安装 BIND 服务和工具2.  配置 BIND 服务3 . 添加你的域名区域配置4.创建区域

mss32.dll文件丢失怎么办? 电脑提示mss32.dll丢失的多种修复方法

《mss32.dll文件丢失怎么办?电脑提示mss32.dll丢失的多种修复方法》最近,很多电脑用户可能遇到了mss32.dll文件丢失的问题,导致一些应用程序无法正常启动,那么,如何修复这个问题呢... 在电脑常年累月的使用过程中,偶尔会遇到一些问题令人头疼。像是某个程序尝试运行时,系统突然弹出一个错误提

电脑提示找不到openal32.dll文件怎么办? openal32.dll丢失完美修复方法

《电脑提示找不到openal32.dll文件怎么办?openal32.dll丢失完美修复方法》openal32.dll是一种重要的系统文件,当它丢失时,会给我们的电脑带来很大的困扰,很多人都曾经遇到... 在使用电脑过程中,我们常常会遇到一些.dll文件丢失的问题,而openal32.dll的丢失是其中比较

python中字符串拼接的几种方法及优缺点对比详解

《python中字符串拼接的几种方法及优缺点对比详解》在Python中,字符串拼接是常见的操作,Python提供了多种方法来拼接字符串,每种方法有其优缺点和适用场景,以下是几种常见的字符串拼接方法,需... 目录1. 使用 + 运算符示例:优缺点:2. 使用&nbsjsp;join() 方法示例:优缺点:3

Mysql中深分页的五种常用方法整理

《Mysql中深分页的五种常用方法整理》在数据量非常大的情况下,深分页查询则变得很常见,这篇文章为大家整理了5个常用的方法,文中的示例代码讲解详细,大家可以根据自己的需求进行选择... 目录方案一:延迟关联 (Deferred Join)方案二:有序唯一键分页 (Cursor-based Paginatio

SpringBoot项目启动报错"找不到或无法加载主类"的解决方法

《SpringBoot项目启动报错找不到或无法加载主类的解决方法》在使用IntelliJIDEA开发基于SpringBoot框架的Java程序时,可能会出现找不到或无法加载主类com.example.... 目录一、问题描述二、排查过程三、解决方案一、问题描述在使用 IntelliJ IDEA 开发基于