ResourcesFinder { Map<Location, List<Resource>> findResources(FlywayConfiguration configuration) throws IOException { final String prefix = configuration.getSqlMigrationPrefix(); final String suffix = configuration.getSqlMigrationSuffix(); final ClassPathScanner cpScanner = new ClassPathScanner(configuration.getClassLoader()); final FileSystemScanner fsScanner = new FileSystemScanner(); final Map<Location, List<Resource>> resources = new HashMap<>(); for (String rawLocation : configuration.getLocations()) { final Location location = new Location(rawLocation); if (location.isClassPath()) { resources.put(location, newArrayList(cpScanner.scanForResources(location, prefix, suffix))); } else { resources.put(location, newArrayList(fsScanner.scanForResources(location, prefix, suffix))); } } return resources; }    }
@Test public void findsScriptsInClassPath() throws Exception { flyway.setLocations("classpath:finder-sql-files"); cleanAfter.addAll( createFiles( "finder-sql-files/1.0/1.sql", "finder-sql-files/1.0/2.sql", "finder-sql-files/2.0/1.sql", "finder-sql-files/2.0/postgresql/1.sql")); final Set<String> locations = findResources(flyway).get("classpath:finder-sql-files"); assertEquals( locations, newHashSet( "finder-sql-files/1.0/1.sql", "finder-sql-files/1.0/2.sql", "finder-sql-files/2.0/1.sql", "finder-sql-files/2.0/postgresql/1.sql")); }
@Test public void findsScriptsOnFileSystem() throws Exception { final List<Path> paths = createFiles( "finder-sql-files/1.0/1.sql", "finder-sql-files/1.0/2.sql", "finder-sql-files/2.0/1.sql", "finder-sql-files/2.0/postgresql/1.sql"); cleanAfter.addAll(paths); final Path finderSqlFiles = paths.get(0).getParent().getParent(); final String fsLocation = "filesystem:" + finderSqlFiles.toAbsolutePath(); flyway.setLocations(fsLocation); final Set<String> locations = findResources(flyway).get(fsLocation); assertEquals( locations, newHashSet( finderSqlFiles.resolve("1.0").resolve("1.sql").toString(), finderSqlFiles.resolve("1.0").resolve("2.sql").toString(), finderSqlFiles.resolve("2.0").resolve("1.sql").toString(), finderSqlFiles.resolve("2.0").resolve("postgresql").resolve("1.sql").toString())); }
@Test public void findsFileSystemAndClassPathScripts() throws Exception { final List<Path> paths = createFiles( "finder-fs-sql-files/1.0/1.sql", "finder-fs-sql-files/2.0/2.sql", "finder-cp-sql-files/1.0/2.sql", "finder-cp-sql-files/2.0/postgresql/1.sql"); cleanAfter.addAll(paths); final Path finderFsSqlFiles = paths.get(0).getParent().getParent(); final String fsLocation = "filesystem:" + finderFsSqlFiles.toAbsolutePath(); final String cpLocation = "classpath:finder-cp-sql-files"; flyway.setLocations(fsLocation, cpLocation); final Map<String, Set<String>> locations = findResources(flyway); assertEquals( locations.get(fsLocation), newHashSet( finderFsSqlFiles.resolve("1.0").resolve("1.sql").toString(), finderFsSqlFiles.resolve("2.0").resolve("2.sql").toString())); assertEquals( locations.get(cpLocation), newHashSet("finder-cp-sql-files/1.0/2.sql", "finder-cp-sql-files/2.0/postgresql/1.sql")); }