@Override public String getIdentifier(String relativePath, boolean directory) { return createFile(config.getPath(), relativePath).getPath(); }
@Override public String getRelativePath(String identifier, boolean directory) { String relativePath = createFile(identifier).getAbsolutePath(); File rootFile = createFile(config.getPath()); if (!config.isUseAbsolutePath() && relativePath.startsWith(rootFile.getAbsolutePath())) { relativePath = relativePath.substring(rootFile.getAbsolutePath().length()); } if (File.separatorChar == '\\') { relativePath = relativePath.replace('\\', '/'); } if (relativePath.startsWith("/")) { relativePath = relativePath.substring(1); } return relativePath; }
@Override public Iterable<ObjectSummary> allObjects() { ObjectSummary rootSummary = createSummary(config.getPath()); if (rootSummary.isDirectory() && !config.isIncludeBaseDir()) return children(rootSummary); else return Collections.singletonList(rootSummary); }
@Override public void configure(SyncStorage source, Iterator<SyncFilter> filters, SyncStorage target) { super.configure(source, filters, target); File rootFile = createFile(config.getPath()); if (source == this) { if (!rootFile.exists()) throw new ConfigurationException("the source " + rootFile + " does not exist"); if (config.getModifiedSince() != null) { modifiedSince = Iso8601Util.parse(config.getModifiedSince()); if (modifiedSince == null) throw new ConfigurationException("could not parse modified-since"); } if (config.getDeleteCheckScript() != null) { File deleteCheckScript = new File(config.getDeleteCheckScript()); if (!deleteCheckScript.exists()) throw new ConfigurationException("delete check script " + deleteCheckScript + " does not exist"); } if (config.getExcludedPaths() != null) { excludedPathPatterns = new ArrayList<>(); for (String pattern : config.getExcludedPaths()) { excludedPathPatterns.add(Pattern.compile(pattern)); } } } }
@Test public void testFilesystemUriParsing() { FilesystemConfig fsConfig = new FilesystemConfig(); String uri = "file:///foo/bar", path = "/foo/bar", newUri = "file:/foo/bar"; ConfigUtil.parseUri(fsConfig, uri); Assert.assertEquals(path, fsConfig.getPath()); Assert.assertEquals(newUri, ConfigUtil.generateUri(fsConfig)); uri = "file://foo/bar"; path = "foo/bar"; newUri = "file:foo/bar"; ConfigUtil.parseUri(fsConfig, uri); Assert.assertEquals(path, fsConfig.getPath()); Assert.assertEquals(newUri, ConfigUtil.generateUri(fsConfig)); } }
@Test public void testFilesystemCli() throws Exception { File sourceFile = new File("/tmp/foo"); File targetFile = new File("/tmp/bar"); String[] args = new String[]{ "-source", "file://" + sourceFile, "-target", "file://" + targetFile, "--source-use-absolute-path", "--target-include-base-dir" }; CliConfig cliConfig = CliHelper.parseCliConfig(args); SyncConfig syncConfig = CliHelper.parseSyncConfig(cliConfig, args); Object source = syncConfig.getSource(); Assert.assertNotNull("source is null", source); Assert.assertTrue("source is not FilesystemSource", source instanceof FilesystemConfig); FilesystemConfig fsSource = (FilesystemConfig) source; Object target = syncConfig.getTarget(); Assert.assertNotNull("target is null", target); Assert.assertTrue("target is not FilesystemTarget", target instanceof FilesystemConfig); FilesystemConfig fsTarget = (FilesystemConfig) target; Assert.assertEquals("source file mismatch", sourceFile.getPath(), fsSource.getPath()); Assert.assertTrue("source use-absolute-path should be enabled", fsSource.isUseAbsolutePath()); Assert.assertEquals("target file mismatch", targetFile.getPath(), fsTarget.getPath()); Assert.assertTrue("target include-base-dir should be enabled", fsTarget.isIncludeBaseDir()); }