IpAddressServiceImplTest的一些準備

一曲微茫發表於2024-11-15

AllIpAddressCheckRequest 類只有一個屬性,List ipAddressList,AllIpAddressCheckResponse 類有兩個屬性,Boolean result和HashMap<String,Boolean> map,RespUtils定義如下public class RespUtils{
private static final Logger log=LoggerFactory. getLogger(RespUtils. class);
private RespUtils(){
}
public static void setSuccess(BaseResponse response){
response. setSuccess();

public static void setError( Exception e, ErrCodeBaseEnum errCode, BaseResponse response){
if (e instanceof BaseBizException){
BaseBizException exception=(BaseBizException)e;
response. setErrCode( exception. getErrCode());
response. setErrMsg( exception. getErrMsg());
} else if (errCode!= null){
response. setErrCode(errCode. getErrCode());
response. setErrMsg(errCode. getErrMsg());
} else{
log. error("errCode為空, 設定預設錯誤");
response. setErrCode(HadesErrCodeEnum. BIZ_UNKNOWN_ERROR. getErrCode());
response. setErrMsg(HadesErrCodeEnum. BIZ_UNKNOWN_ERROR. getErrMsg());
}
)
public static void setError(ErrCodeBaseEnum errCode, BaseResponse response){setError(( Exception) null, errCode, response);
}
public static Object newReturn( Class returnType, String errCode, String errMsg){ if (returnType == null){ return null; } else{ Object ret= null; try{ret=returnType. newInstance(); if ( ret instanceof BaseResponse){ BaseResponse response=(BaseResponse) ret; response. setErrCode(errCode); response. setErrMsg(errMsg); ret= response; } } catch (IllegalAccessException| InstantiationException var5){ log.info ((String)null,var5); } return ret; } ) public static Object newReturn( Class returnType, BaseBizExceptione){
return newReturn(returnType, e. getErrCode(), e. getErrMsg());
}
public static Object newReturn( Class<?> returnType, ErrCodeBaseEnum
errCodeBaseEnum){
return newReturn(returnType, errCodeBaseEnum. getErrCode(),
errCodeBaseEnum. getErrMsg());
}
)
TitanException定義如下public class Titan Exception extends RuntimeException{
public TitanException(){
super();}
public TitanException(String message){
super(message);}
public TitanException(String message,Throwable cause){
super(message,cause);}
public TitanException(Throwable cause){
super(cause);}

}IpCheckService 介面及其方法 asyncIpCheck 的定義如下public class IpCheckService{
@Async(value="asyncIpCheckExecutor")
public void asyncIpCheck(String ipAdress,Map<String,Boolean> map,CountDownLatch latch){
try{
InetAdress adress=InetAdress.getByName(ipAdress);
map.put(ipAdress,true);
if(address.isReachable(3000)){
map.put(ipAdress,false);}

}catch(Exception e){
log.error("IP校驗異常",e);
throw new TitanException("IP校驗異常",e);
}finally{
latch.countDown();
}
}
}Maps類就採用常用情況即可。請你重新進行單元測試

相關文章