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模拟文件相关推荐
- synology_用于在Synology NAS上测试Spring Boot Web应用程序的JUnit模拟文件
synology 对于将在Synology RS815 + NAS上检查备份文件的Spring Boot应用程序,我们希望能够轻松测试此NAS上存储的文件,而不必复制存储在该NAS上的7TB. 理想情 ...
- 用于在Synology NAS上测试Spring Boot Web应用程序的JUnit模拟文件
对于将在Synology RS815 + NAS上检查备份文件的Spring Boot应用程序,我们希望能够轻松测试此NAS上存储的文件,而不必复制存储在其上的7TB. 理想情况下,我们希望创建相同的 ...
- openshift k8s_带有DIY的Openshift上的Spring Boot / Java 8 / Tomcat 8
openshift k8s DIY盒带是一种实验性盒带,提供了一种在OpenShift上测试不受支持的语言的方法. 它提供了最小限度的自由形式的支架,将墨盒的所有细节留给了应用程序开发人员 . 这篇博 ...
- 在Amazon Elastic Beanstalk上部署Spring Boot应用程序
在此博客中,我们将看到如何在Amazon ElasticBeanstalk上部署Spring Boot应用程序. Amazon ElasticBeanstalk具有一个预配置的Java环境,可用于部署 ...
- 带有DIY的Openshift上的Spring Boot / Java 8 / Tomcat 8
DIY墨盒是一种实验性墨盒,它提供了一种在OpenShift上测试不受支持的语言的方法. 它提供了最小限度的自由形式的支架,将墨盒的所有细节留给了应用程序开发人员 . 这篇博客文章说明了结合了Post ...
- Spring Boot 2.x基础教程:多文件的上传
昨天,我们介绍了如何在Spring Boot中实现文件的上传(博客地址:https://blog.didispace.com/spring-boot-learning-21-4-3/).有读者问:那么 ...
- aws 删除ec2实例_如何在AWS EC2实例上部署Spring Boot应用程序
aws 删除ec2实例 你好朋友, 在本教程中,我们将看到如何在AWS EC2实例上部署Spring Boot应用程序. 这是我们将要执行的步骤. 1.使用Spring Boot Initialise ...
- spring boot测试_测试Spring Boot有条件的合理方式
spring boot测试 如果您或多或少有经验的Spring Boot用户,那么很幸运,在某些时候您可能需要遇到必须有条件地注入特定bean或配置的情况 . 它的机制是很好理解的 ,但有时这样的测试 ...
- 如何在AWS EC2实例上部署Spring Boot应用程序
你好朋友, 在本教程中,我们将看到如何在AWS EC2实例上部署Spring Boot应用程序. 这是我们将要执行的步骤. 1.使用Spring Boot Initialiser创建一个Spring ...
最新文章
- android第八步查看与输出日志信息
- VS2010 无法计算HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\14.0@VCTargetPath处的属性表达式...
- 《C#与.net高级编程》——第一支柱:C#的封装
- python sns绘制回归线_Python数分实战:员工流失情况预测
- 极速理解设计模式系列:4.原型模式(Prototype Pattern)
- cf鼠标宏数据大全_两只“轻量、不打孔、右手工学”鼠标,DX30E和魔幻豹ULTRA
- 埃加洛尔虚拟服务器,致我终将逝去的二区:新一轮大服务器实装
- Es的mapping映射
- MIPI DSI的linux kernel驱动原理 | 基于RK3399
- CHECK约束在表继承中的使用
- 在线PS编辑器使用教程(Photoshop)
- 英语听力采用计算机化考试,高考英语听力机考特点与应对建议
- Project directory ‘x/x/x‘ is not part of the build defined by settings file ‘x/x/x‘. If this is ...
- 【愚人特稿】数学家应该重新设计角度制--(转自果壳网)
- 什么是cache?为什么需要cache?cache存在的合理性
- 有用的和不为人知的Java特性
- 浮点数在内存中的存储
- 北师大 外国教育史-7(进步主义教育运动)
- 【语音识别】基于matlab电话按键语音识别(含按键录音)【含Matlab源码 1752期】
- 《FFmpeg Basics》中文版-01-FFmpeg基本介绍