ShowCaseDbInitializer { void createDateBaseFiles(Path dataBaseLocation, boolean overide) { Path hsqlScript = dataBaseLocation.resolve("wte4j-showcase.script"); if (overide || !Files.exists(hsqlScript)) { try { if (!Files.exists(dataBaseLocation)) { Files.createDirectories(dataBaseLocation); } Resource[] resources = resourceLoader.getResources("classpath:/db/*"); for (Resource resource : resources) { Path filePath = dataBaseLocation.resolve(resource.getFilename()); File source = resource.getFile(); Files.copy(source.toPath(), filePath, StandardCopyOption.REPLACE_EXISTING); } } catch (IOException e) { throw new IllegalArgumentException("can not copy database files", e); } } } ShowCaseDbInitializer(ResourcePatternResolver resourceLoader); HsqlServerBean createDatabase(Path dataBaseLocation, boolean overwrite); static void main(String... args);  }
@Test public void createDatabaseFiles() throws IOException { ApplicationContext context = new StaticApplicationContext(); Path directory = Files.createTempDirectory("database"); try { ShowCaseDbInitializer showCaseDbInitializer = new ShowCaseDbInitializer(context); showCaseDbInitializer.createDateBaseFiles(directory, false); Set<String> fileNamesInDirectory = listFiles(directory); Set<String> expectedFileNames = new HashSet<String>(); expectedFileNames.add("wte4j-showcase.lobs"); expectedFileNames.add("wte4j-showcase.properties"); expectedFileNames.add("wte4j-showcase.script"); assertEquals(expectedFileNames, fileNamesInDirectory); } finally { deleteDirectory(directory); } }
@Test public void createDatabaseFilesWithoutOveride() throws IOException, SQLException { ApplicationContext context = new StaticApplicationContext(); Path directory = Files.createTempDirectory("database"); Path dummyFile = directory.resolve("wte4j-showcase.script"); Files.createFile(dummyFile); try { ShowCaseDbInitializer showCaseDbInitializer = new ShowCaseDbInitializer(context); showCaseDbInitializer.createDateBaseFiles(directory, false); Set<String> fileNamesInDirectory = listFiles(directory); Set<String> expectedFileNames = new HashSet<String>(); expectedFileNames.add("wte4j-showcase.script"); assertEquals(expectedFileNames, fileNamesInDirectory); assertEquals(0, Files.size(dummyFile)); } finally { deleteDirectory(directory); } }
@Test public void createDatabaseFilesWithOveride() throws IOException, SQLException { ApplicationContext context = new StaticApplicationContext(); Path directory = Files.createTempDirectory("database"); Path dummyFile = directory.resolve("wte4j-showcase.script"); Files.createFile(dummyFile); try { ShowCaseDbInitializer showCaseDbInitializer = new ShowCaseDbInitializer(context); showCaseDbInitializer.createDateBaseFiles(directory, true); Set<String> fileNamesInDirectory = listFiles(directory); Set<String> expectedFileNames = new HashSet<String>(); expectedFileNames.add("wte4j-showcase.lobs"); expectedFileNames.add("wte4j-showcase.properties"); expectedFileNames.add("wte4j-showcase.script"); assertEquals(expectedFileNames, fileNamesInDirectory); assertTrue(Files.size(dummyFile) > 0); } finally { deleteDirectory(directory); } }