我不知道如何模拟我将文件所有者从行Path path = newFile.toPath();更改到末尾的部分。
这是我的功能:
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public String uploadEndpoint(@RequestParam("file") MultipartFile file,
@RequestParam("usernameSession") String usernameSession,
@RequestHeader("current-folder") String folder) throws IOException {
String[] pathArray = file.getOriginalFilename().split("[\\\\\\/]");
String originalName = pathArray[pathArray.length-1];
LOGGER.info("Upload triggerred with : {} , filename : {}", originalName, file.getName());
String workingDir = URLDecoder.decode(folder.replace("!", "."),
StandardCharsets.UTF_8.name())
.replace("|", File.separator);
LOGGER.info("The file will be moved to : {}", workingDir);
File newFile = new File(workingDir + File.separator + originalName);
//UserPrincipal owner = Files.getOwner(newFile.toPath());
file.transferTo(newFile);
Path path = newFile.toPath();
FileOwnerAttributeView foav = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
UserPrincipal owner = foav.getOwner();
System.out.format("Original owner of %s is %s%n", path, owner.getName());
}
这是测试:
@Test
public void uploadEndpointTest() throws Exception {
PowerMockito.whenNew(File.class).withAnyArguments().thenReturn(file);
Mockito.when(multipartFile.getOriginalFilename()).thenReturn("src/test/resources/download/test.txt");
assertEquals("ok", fileExplorerController.uploadEndpoint(multipartFile, "userName", "src/test/resources/download"));
}
我遇到了一个例外,因为“userName”不是用户。我想模拟它在 Windows 用户中寻找匹配项的调用。当我设置我的窗口的用户名而不是“userName”时它可以工作,但我不能让我的窗口的用户名。
我试图嘲笑fs.getUserPrincipalLookupService();但upls.lookupPrincipalByName(usernameSession);我不知道要返回什么来模拟通话。
慕的地6264312
相关分类