日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

synology smb_用于在Synology NAS上测试Spring Boot Web应用程序的JUnit模拟文件

發布時間:2023/12/3 javascript 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 synology smb_用于在Synology NAS上测试Spring Boot Web应用程序的JUnit模拟文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

synology smb

對于將在Synology RS815 + NAS上檢查備份文件的Spring Boot應用程序,我們希望能夠輕松測試此NAS上存儲的文件,而不必復制存儲在其上的7TB。

理想情況下,我們希望創建相同的文件結構以在Spring開發配置文件中使用Web應用程序,并在JUnit測試中使用這些文件結構。

介紹FileStructureCreator

我們首先創建一個新的類FileStructureCreator ,如下所示:

@Getter @Setter public class FileStructureCreator implements Closeable {public static final Path baseTestPath = Paths.get("testFiles");private Path fileStructureBasePath;public static FileStructureCreator create(Path file) {return createStructure(file, false);}public static FileStructureCreator createTempDirectory(Path file) {return createStructure(file, true);}@SneakyThrowsprivate static FileStructureCreator createStructure(Path file, boolean createTempDirectory) {FileStructureCreator fileStructureCreator = new FileStructureCreator();if (!Files.exists(baseTestPath)) {Files.createDirectory(baseTestPath);}String path = baseTestPath.toString() + (createTempDirectory ? "/" + UUID.randomUUID().toString() : "")+ "/";Path basePath = Paths.get(path);fileStructureCreator.setFileStructureBasePath(basePath);FileUtils.forceMkdir(basePath.toFile());try (Stream<String> stream = Files.lines(file)) {stream.forEach(line -> {Metadata fileMetaData = Metadata.from(line);Path fileEntry = Paths.get(path + fileMetaData.getWindowsSafeFilename());try {FileUtils.forceMkdir(fileEntry.getParent().toFile());if (!Files.exists(fileEntry)) {Files.write(fileEntry, line.getBytes());Files.setLastModifiedTime(fileEntry, FileTime.from(fileMetaData.getModificationTime()));}} catch (IOException ignore) {throw new RuntimeException("Exception creating directory: " + fileEntry.getParent());}});}return fileStructureCreator;}@Override@SneakyThrowspublic void close() {if (fileStructureBasePath != null) {FileUtils.deleteDirectory(fileStructureBasePath.toFile());}} }

基本上,這將創建整個目錄結構和必要的文件。 我們只需要傳遞一個包含文件結構元數據的基本文件即可。

元數據保存時間戳,文件大小和該文件的路徑。 看起來像這樣:

2016-04-05T10:30:15.012345678 ??5120backupftp/@eaDir/sharesnap_share_configuration/SYNO@.quota2018-02-26T00:00:09.012345678 ?169backupftp/@eaDir/sharesnap_share_configuration/share_configuration

然后,在Synology NAS上,我們可以通過執行以下命令輕松生成具有(特定)目錄的整個樹結構的文件:

find backupftp -type f -printf "%TY-%Tm-%TdT%TH:%TM:%.12TS\t%s\t%p\n">test/backupftp.files.txt

將生成的文件從您的Synology NAS復制到您的項目。

在JUnit測試中,我們使用FileStructureCreator類,如下面的示例所示。 請注意, FileStructureCreator實現了AutoCloseable ,因此我們可以在測試完成后使用try / catch塊來清理文件。

@Value("classpath:/TestDiskConsistencyPolicy-notEnoughFileSets.txt") private Path notEnoughFileSets;@Test(expected = RuntimeException.class) public void backupSetWithNoFileSetsThrowException() {try( FileStructureCreator creator = FileStructureCreator.createTempDirectory(notEnoughFileSets) ) {BackupSet backupSet = BackupSet.builder().uri(creator.getFileStructureBasePath().toString()).build();new DiskConsistencyPolicy(backupSet).execute();assertTrue( "Expecting a RuntimeException here", false);} }

對于Spring Boot應用程序,我們只定義一個@Configuration類,它將為Synology NAS上定義的文件共享創建數據結構。

@Configuration @Profile("dev") public class TestFilesInstaller {@Beanpublic FileStructureCreator ftpFiles(@Value("classpath:/backupftp.files.txt") Path file) {return FileStructureCreator.create(file);}@Beanpublic FileStructureCreator nfsFiles(@Value("classpath:/backupnfs.files.txt") Path file) {return FileStructureCreator.create(file);} }

因為它們被定義為@Bean ,所以在應用程序關閉時將自動調用close()方法,并在Spring Boot應用程序停止時從磁盤上刪除所有文件。
只是……不要在生產環境中運行開發人員資料; 我讓你知道會發生什么。 ;-)
將來,我們將向您展示如何構建備份檢查器以監視和驗證NAS上的備份。

翻譯自: https://www.javacodegeeks.com/2018/04/mocking-files-for-junit-testing-a-spring-boot-web-application-on-synology-nas.html

synology smb

總結

以上是生活随笔為你收集整理的synology smb_用于在Synology NAS上测试Spring Boot Web应用程序的JUnit模拟文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。