File local3 = File.createTempFile("three", " ثلاثة.txt", parentDir2);
String local1Path = parentDir1.getName() + File.separator + local1.getName();
String local2Path = parentDir1.getName() + File.separator + local2.getName();
String local3Path = parentDir2.getName() + File.separator + local3.getName();
FileComparer comparer = new FileComparer(new Jets3tProperties());
// Build a file map of local files
Map<String, String> objectKeyToFilepathMap = comparer.buildObjectKeyToFilepathMap(
new File[] {parentDir1, parentDir2}, "", true);
assertEquals(5, objectKeyToFilepathMap.size());
assertTrue(objectKeyToFilepathMap.keySet().contains(local1Path));
// Upload local directories and files to storage service
service.putObject(bucketName, ObjectUtils.createObjectForUpload(
parentDir1.getName() + "/", parentDir1, null, false));
service.putObject(bucketName, ObjectUtils.createObjectForUpload(
parentDir2.getName() + "/", parentDir2, null, false));
service.putObject(bucketName, ObjectUtils.createObjectForUpload(
local1Path, local1, null, false));
service.putObject(bucketName, ObjectUtils.createObjectForUpload(
local2Path, local2, null, false));
service.putObject(bucketName, ObjectUtils.createObjectForUpload(
local3Path, local3, null, false));
// Build a map of objects in storage service
Map<String, StorageObject> objectMap = comparer.buildObjectMap(
service, bucket.getName(), "", objectKeyToFilepathMap, false, false, null, null);
assertEquals(5, objectMap.size());
assertTrue(objectMap.keySet().contains(local3Path));
// Compare local and remote objects -- should be identical
FileComparerResults comparerResults =
comparer.buildDiscrepancyLists(objectKeyToFilepathMap, objectMap);
assertEquals(5, comparerResults.alreadySynchronisedKeys.size());
assertEquals(0, comparerResults.onlyOnClientKeys.size());
assertEquals(0, comparerResults.onlyOnServerKeys.size());
assertEquals(0, comparerResults.updatedOnClientKeys.size());
assertEquals(0, comparerResults.updatedOnServerKeys.size());
// Update 1 local and 1 remote file, then confirm discrepancies
byte[] data = "Updated local file".getBytes("UTF-8");
FileOutputStream local1FOS = new FileOutputStream(local1);
local1FOS.write(data);
local1FOS.close();
// Ensure local file's timestamp differs by at least 1 sec
local1.setLastModified(local1.lastModified() + 1000);
data = "Updated remote file".getBytes("UTF-8");
StorageObject remoteObject = new StorageObject(local3Path);
// Ensure remote file's JetS3t timestamp differs from local file by at least 1 sec
remoteObject.addMetadata(Constants.METADATA_JETS3T_LOCAL_FILE_DATE,
ServiceUtils.formatIso8601Date(new Date(local3.lastModified() + 1000)));
remoteObject.setDataInputStream(new ByteArrayInputStream(data));
remoteObject.setContentLength(data.length);
service.putObject(bucketName, remoteObject);
objectMap = comparer.buildObjectMap(
service, bucket.getName(), "", objectKeyToFilepathMap, false, false, null, null);
comparerResults =
comparer.buildDiscrepancyLists(objectKeyToFilepathMap, objectMap);
assertEquals(3, comparerResults.alreadySynchronisedKeys.size());
assertEquals(0, comparerResults.onlyOnClientKeys.size());
assertEquals(0, comparerResults.onlyOnServerKeys.size());
assertEquals(1, comparerResults.updatedOnClientKeys.size());
assertTrue(comparerResults.updatedOnClientKeys.contains(local1Path));
assertEquals(1, comparerResults.updatedOnServerKeys.size());
assertTrue(comparerResults.updatedOnServerKeys.contains(local3Path));
// Create new local and remote objects, then confirm discrepancies
File local4 = File.createTempFile("four", ".txt", parentDir2);
String local4Path = parentDir2.getName() + File.separator + local4.getName();
remoteObject = new StorageObject("new-on-service.txt");
service.putObject(bucketName, remoteObject);
objectKeyToFilepathMap = comparer.buildObjectKeyToFilepathMap(
new File[] {parentDir1, parentDir2}, "", true);
objectMap = comparer.buildObjectMap(
service, bucket.getName(), "", objectKeyToFilepathMap, false, false, null, null);
comparerResults = comparer.buildDiscrepancyLists(objectKeyToFilepathMap, objectMap);
assertEquals(3, comparerResults.alreadySynchronisedKeys.size());
assertTrue(comparerResults.alreadySynchronisedKeys.contains(local2Path));
assertEquals(1, comparerResults.onlyOnClientKeys.size());
assertTrue(comparerResults.onlyOnClientKeys.contains(local4Path));
assertEquals(1, comparerResults.onlyOnServerKeys.size());
assertTrue(comparerResults.onlyOnServerKeys.contains("new-on-service.txt"));
assertEquals(1, comparerResults.updatedOnClientKeys.size());
assertTrue(comparerResults.updatedOnClientKeys.contains(local1Path));
assertEquals(1, comparerResults.updatedOnServerKeys.size());
assertTrue(comparerResults.updatedOnServerKeys.contains(local3Path));
// Clean up after prior test
local4.delete();
service.deleteObject(bucketName, "new-on-service.txt");
// Remove local file and remote object, then confirm discrepancies
local3.delete();
service.deleteObject(bucketName, local1Path);
objectKeyToFilepathMap = comparer.buildObjectKeyToFilepathMap(
new File[] {parentDir1, parentDir2}, "", true);
objectMap = comparer.buildObjectMap(
service, bucket.getName(), "", objectKeyToFilepathMap, false, false, null, null);
comparerResults = comparer.buildDiscrepancyLists(objectKeyToFilepathMap, objectMap);
assertEquals(1, comparerResults.onlyOnClientKeys.size());
assertTrue(comparerResults.onlyOnClientKeys.contains(local1Path));
assertEquals(1, comparerResults.onlyOnServerKeys.size());
assertTrue(comparerResults.onlyOnServerKeys.contains(local3Path));
} finally {