SqlScriptCreator { SqlScript createScript(Location location, Resource resource) { final String separator = location.isClassPath() ? "/" : File.separator; final String relLocation = resource.getLocation().substring(location.getPath().length() + 1); final String[] paths = relLocation.split(separator); if (paths.length == 2) { return new SqlScript(resource, location, paths[0], null, paths[1]); } if (paths.length == 3) { return new SqlScript(resource, location, paths[0], paths[1], paths[2]); } throw new FlywayException( format( "Sql script location must be either in 'location-root/version-dir' " + "or in 'location-root/version-dir/provider-name', but script '%s' is not in root '%s'", resource.getLocation(), location.getPath())); }    }
@Test public void createsScript() throws Exception { final Location location = new Location("filesystem:schema"); final Resource resource = new FileSystemResource("schema/5.0.0-M7/v1__init.sql"); final SqlScriptCreator scriptsCreator = new SqlScriptCreator(); final SqlScript script = scriptsCreator.createScript(location, resource); assertEquals(script.name, "v1__init.sql"); assertEquals(script.location, location); assertEquals(script.dir, "5.0.0-M7"); assertEquals(script.resource.getLocation(), resource.getLocation()); assertNull(script.vendor); }
@Test public void createsVendorScript() throws Exception { final Location location = new Location("filesystem:schema"); final Resource resource = new FileSystemResource("schema/5.0.0-M7/postgresql/v1__init.sql"); final SqlScriptCreator scriptsCreator = new SqlScriptCreator(); final SqlScript script = scriptsCreator.createScript(location, resource); assertEquals(script.name, "v1__init.sql"); assertEquals(script.location, location); assertEquals(script.dir, "5.0.0-M7"); assertEquals(script.resource.getLocation(), resource.getLocation()); assertEquals(script.vendor, "postgresql"); }
@Test(expectedExceptions = FlywayException.class) public void failsToCreateResourceWhenPathIsInvalid() throws Exception { final Location location = new Location("filesystem:schema"); final Resource resource = new FileSystemResource("schema/v1__init.sql"); new SqlScriptCreator().createScript(location, resource); }