[本文出自天外归云的博客园]
去年产品要求上传txt文件来实现批量上传用户名单这一块,这回产品直接把txt文件放ftp了,要求后台管理系统也支持从ftp直接读取txt文件。其实就是在后台管理系统里的头图管理功能中加个ftp地址按钮,输入ftp以后确定,预期结果是和上传txt文件的按钮选择txt文本上传后实现的效果是一样的。达到的目的都是让文本中的用户名单能看到指定的头图。
看后台代码,进行对比,之前上传txt文件的按钮的Controller为:
@ResponseBody @RequestMapping("/doUploadUserList.do") public Object uploadUserList( @RequestParam(value = "file", required = false) MultipartFile file, @RequestParam(value = "bannerId", required = true) String bannerId, @RequestParam(value = "partnerId", required = false) String partnerId, HttpServletRequest request, HttpServletResponse response) throws IOException { Map result = new HashMap(); BufferedReader br = null; try { BannerResource banner = bannerResourceDao.queryById(bannerId); Date currentDate = new Date(); if (currentDate.compareTo(banner.getToDate()) > 0) { result.put(RET_CODE_PARAM, RET_INTERROR); result.put(RET_ERROR_MSG, "头图已过期!"); } br = new BufferedReader(new InputStreamReader(file.getInputStream())); String line = br.readLine(); int count = 0; HashSet firmIdSet = new HashSet(); HashSet userNameSet = new HashSet(); while (line != null) { userNameSet.add(line.trim()); line = br.readLine(); count++; if (count > 100000) { break; } } if (count > 100000) { result.put(RET_CODE_PARAM, RET_INTERROR); result.put(RET_ERROR_MSG, "上传用户数超限!"); } else { if (userNameSet.isEmpty()) { result.put(RET_CODE_PARAM, RET_INTERROR); result.put(RET_ERROR_MSG, "上传名单错误!"); return result; } advRecommendService.insertActivityUserSet(banner.getId(), userNameSet, banner.getToDate()); result.put(RET_CODE_PARAM, RET_SUCCESS); result.put(RET_HTML, getSuccessHtml("更新用户列表成功!")); } } catch (Exception e) { logger.error("[BannerController] uploadUserList ERROR, "+e.getMessage(), e); result.put(RET_CODE_PARAM, RET_INTERROR); result.put(RET_ERROR_MSG, "系统异常!"); } finally { if (br != null) { br.close(); } } return result; }
通过git工具show log发现这次开发代码的提交除了ftp操作工具类方法的编写外,对应添加的Controller为:
@ResponseBody @RequestMapping("/doUploadFtpAdrress.do") public Object doUploadFtpAdrress( @RequestParam(value = "path", required = false) String path, @RequestParam(value = "bannerId", required = true) String bannerId, @RequestParam(value = "partnerId", required = false) String partnerId, HttpServletRequest request, HttpServletResponse response) throws IOException { Map result = new HashMap(); BufferedReader br = null; try { if(StringUtils.isBlank(path)) { result.put(RET_CODE_PARAM, RET_INTERROR); result.put(RET_ERROR_MSG, "参数错误"); } path=path.substring((path.indexOf(String.valueOf(fTPsUtils.getFtpPort())))+String.valueOf(fTPsUtils.getFtpPort()).length()); BannerResource banner = bannerResourceDao.queryById(bannerId); Date currentDate = new Date(); if (currentDate.compareTo(banner.getToDate()) > 0) { result.put(RET_CODE_PARAM, RET_INTERROR); result.put(RET_ERROR_MSG, "头图已过期!"); } fTPsUtils.connect(); logger.info("ftp path: "+path); if(fTPsUtils.isFileExists(path)) { fTPsUtils.getFtpsClient().enterLocalPassiveMode(); InputStream in=fTPsUtils.getFtpsClient().retrieveFileStream(path); if(in!=null) { br = new BufferedReader(new InputStreamReader(in)); String line = br.readLine(); int count = 0; HashSet userNameSet = new HashSet(); while (line != null) { if(ValidatorUtil.isEmail(line.trim())) { logger.info("userName: "+line.trim()); userNameSet.add(line.trim()); } else{ Account account = accountService.queryAccountByPartnerIdAndFirmId(partnerId, line.trim()); if(account!=null) { userNameSet.add(account.getUsername()); } } line = br.readLine(); count++; if (count > 100000) { break; } } if (count > 100000) { result.put(RET_CODE_PARAM, RET_INTERROR); result.put(RET_ERROR_MSG, "上传用户数超限!"); } else { if (userNameSet.isEmpty()) { result.put(RET_CODE_PARAM, RET_INTERROR); result.put(RET_ERROR_MSG, "上传名单错误!"); return result; } advRecommendService.insertActivityUserSet(banner.getId(), userNameSet, banner.getToDate()); result.put(RET_CODE_PARAM, RET_SUCCESS); result.put(RET_HTML, getSuccessHtml("更新用户列表成功!")); } } } else{ result.put(RET_CODE_PARAM, RET_INTERROR); result.put(RET_ERROR_MSG, "ftp地址的文件不存在"); } } catch (Exception e) { logger.error("[BannerController] uploadUserList ERROR, "+e.getMessage(), e); result.put(RET_CODE_PARAM, RET_INTERROR); result.put(RET_ERROR_MSG, "系统异常!"); } finally { if (br != null) { br.close(); } } return result; }
对比两个Controller可以看出,之前的逻辑都是把获取到用户信息都保存到了userNameSet这个变量中,之后的逻辑都是对于这个变量做操作并没有做修改。
所以我们可以把测试范围缩小到对于这个变量的测试。在代码中加入诊断log,检测这两个Controller中对于同一个txt文本以及其对应的ftp地址所生成的userNameSet变量值是否一样。
例如:
![](https://i-blog.csdnimg.cn/blog_migrate/515d98610fe1d6d09b0ccb89ed153853.png)
然后在黑盒操作流程结束后,在后台服务器的log里可以过滤一下看一下。
如果以其他的方式验证,例如用名单中的用户登录客户端查看指定头图是否可见,首先本次并没有改动之前测试过的逻辑,这样做属于抽样测试+回归测试,并且没什么必要。精准测试能够有效锁定代码改动区域,精准测试范围,让测试的范围最小化。