private HashMap<String, String> errorInfo = new HashMap();
时间: 2024-04-26 18:20:24 浏览: 16
这是一个 Java 代码片段,定义了一个名为 errorInfo 的 HashMap 对象,其中键和值都是 String 类型。这个对象可以用来存储错误信息,键值对应了错误的类型和错误的具体信息。这个 HashMap 可以通过 put 方法添加键值对,通过 get 方法获取键对应的值。例如,可以使用 errorInfo.put("NullPointer", "空指针异常") 来添加一个键为 "NullPointer",值为 "空指针异常" 的键值对。
相关问题
package com.zte.ums.cnms.pm.datfileupload.ranomm.clean; import com.zte.ums.cnms.pm.datfileupload.ranomm.util.Utils; import lombok.extern.slf4j.Slf4j; import javax.inject.Inject; import java.util.HashMap; import java.util.Map; import java.util.List; import java.nio.file.Paths; @Slf4j public class FileCleanRunnable implements Runnable{ // @Inject // private FileCleanTask fileCleanTask; // @Inject // private CleanTaskPollService cleanTaskPollService; private FileCleanParaQueryService fileCleanParaQueryService=new FileCleanParaQueryService(); // private FileCleanTaskResponse fileCleanTaskResponse; private Map<String, Integer> cleanPathRetainMap=new HashMap<>(); private String dir; public FileCleanRunnable(String dir){ this.dir = dir; } @Override public void run() { //do cleaning // neIdDirs=fileCleanTask.getCleanDirs(); // log.info("prepared Clean Dir:"+dir); try { cleanDir(); } catch (Exception e) { log.error("File clean me dir: failed",e); } } private void cleanDir() { long startTimeMills = System.currentTimeMillis(); String tmpPath; log.info("fileCleanParaQueryService:"+fileCleanParaQueryService); Map<String, Integer> pmParas= fileCleanParaQueryService.getCleanParas(); cleanPathRetainMap.put(CleanUtil.FTP, pmParas.get(FileCleanParaQueryService.LOCAL_FTP)); cleanPathRetainMap.put(CleanUtil.BAK, pmParas.get(FileCleanParaQueryService.LOCAL_BAK)); cleanPathRetainMap.put(CleanUtil.PARSE_ERR, pmParas.get(FileCleanParaQueryService.LOCAL_PARSE_ERR)); log.info("File clean thread start, clean dir : {} ", dir); // for (String neIdDir : dir) { for (Map.Entry<String, Integer> entry : cleanPathRetainMap.entrySet()) { tmpPath = Paths.get(dir, entry.getKey()).toString(); log.info("tmpPath:"+tmpPath); Utils.deleleFilesOfDir(tmpPath, entry.getValue()); } // } log.info("File clean thread finish,dir size: {},cost: {} ms", dir, (System.currentTimeMillis() - startTimeMills)); } }写单元测试,框架为junit3
好的,以下是一个可能的JUnit3测试类的示例,用于测试上述代码中的FileCleanRunnable类:
```
import junit.framework.TestCase;
public class FileCleanRunnableTest extends TestCase {
private FileCleanRunnable fileCleanRunnable;
private String dir = "C:/clean";
public void setUp() {
fileCleanRunnable = new FileCleanRunnable(dir);
}
public void testCleanDir() {
fileCleanRunnable.run();
// 在这里编写断言,验证代码的正确性
// ...
}
}
```
在上面的示例代码中,我们首先创建了一个FileCleanRunnable类的实例fileCleanRunnable,并将需要清理的目录传递给它。然后在setUp方法中初始化该实例。
在testCleanDir方法中,我们调用了fileCleanRunnable的run方法,这样就可以执行清理逻辑。然后,在这个方法中,我们需要编写断言来验证代码的正确性。具体的断言逻辑取决于您的代码实现和测试需求。
希望这可以帮助您编写单元测试。
优化这段代码: @AfterThrowing(value = "execution(* com.dianda.change.service..*.*(..))", throwing = "ex") public void afterThrowingAdvice(JoinPoint joinPoint, Exception ex) { try { logger.info("进入异常通知方法..."); Object targetObject = joinPoint.getTarget(); Signature signature = joinPoint.getSignature(); // System.out.println("ExceptionClassName={} ===" + ex.getClass().getName()); // // System.out.println("ExceptionClassName={} ===" + ex.getClass().getName()); Map<String, Object> map = new HashMap<>(); map.put("id", UUID.randomUUID().toString()); map.put("JKMC", targetObject.getClass().getName() + " signatureName=" + signature.getName()); map.put("CWMX", ex.getMessage()); map.put("ADDTIME", Format.getDateTime()); InsertParams insertParams = InsertParams.createInsertParams("T_RUN_ERROR_LOG"); insertParams.addParamsForMap(map); baseDaoComponent.insertDataByParams(insertParams); } catch (Exception e) { e.printStackTrace(); } // System.out.println("exception occurred at class "+targetObject.getClass().getName()+ // "\n signatureName="+signature.getName()); //// System.out.println("ExceptionClassName={}"+ ex.getClass().getName()); // System.out.println("message:{}"+ ex.getMessage()); }
这段代码可以进行如下优化:
1. 将 try-catch 语句块中的代码提取到一个方法中,避免重复代码:
```java
private void saveErrorLog(Object targetObject, Signature signature, Exception ex) {
try {
logger.info("进入异常通知方法...");
Map<String, Object> map = new HashMap<>();
map.put("id", UUID.randomUUID().toString());
map.put("JKMC", targetObject.getClass().getName() + " signatureName=" + signature.getName());
map.put("CWMX", ex.getMessage());
map.put("ADDTIME", Format.getDateTime());
InsertParams insertParams = InsertParams.createInsertParams("T_RUN_ERROR_LOG");
insertParams.addParamsForMap(map);
baseDaoComponent.insertDataByParams(insertParams);
} catch (Exception e) {
e.printStackTrace();
}
}
```
2. 使用 @Slf4j 注解代替手动创建 logger 对象,简化代码:
```java
@AfterThrowing(value = "execution(* com.dianda.change.service..*.*(..))", throwing = "ex")
public void afterThrowingAdvice(JoinPoint joinPoint, Exception ex) {
Object targetObject = joinPoint.getTarget();
Signature signature = joinPoint.getSignature();
saveErrorLog(targetObject, signature, ex);
}
```
通过上述优化,代码变得更加简洁易读,也更加易于维护。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)