michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: package org.mozilla.gecko.background.common; michael@0: michael@0: import java.io.File; michael@0: import java.io.IOException; michael@0: import java.util.ArrayList; michael@0: import java.util.Collection; michael@0: import java.util.Collections; michael@0: import java.util.HashSet; michael@0: import java.util.List; michael@0: import java.util.Set; michael@0: michael@0: import org.mozilla.gecko.background.helpers.AndroidSyncTestCase; michael@0: import org.mozilla.gecko.sync.Utils; michael@0: michael@0: import android.os.Bundle; michael@0: michael@0: public class TestUtils extends AndroidSyncTestCase { michael@0: protected static void assertStages(String[] all, String[] sync, String[] skip, String[] expected) { michael@0: final Set sAll = new HashSet(); michael@0: for (String s : all) { michael@0: sAll.add(s); michael@0: } michael@0: List sSync = null; michael@0: if (sync != null) { michael@0: sSync = new ArrayList(); michael@0: for (String s : sync) { michael@0: sSync.add(s); michael@0: } michael@0: } michael@0: List sSkip = null; michael@0: if (skip != null) { michael@0: sSkip = new ArrayList(); michael@0: for (String s : skip) { michael@0: sSkip.add(s); michael@0: } michael@0: } michael@0: List stages = new ArrayList(Utils.getStagesToSync(sAll, sSync, sSkip)); michael@0: Collections.sort(stages); michael@0: List exp = new ArrayList(); michael@0: for (String e : expected) { michael@0: exp.add(e); michael@0: } michael@0: assertEquals(exp, stages); michael@0: } michael@0: michael@0: public void testGetStagesToSync() { michael@0: final String[] all = new String[] { "other1", "other2", "skip1", "skip2", "sync1", "sync2" }; michael@0: assertStages(all, null, null, all); michael@0: assertStages(all, new String[] { "sync1" }, null, new String[] { "sync1" }); michael@0: assertStages(all, null, new String[] { "skip1", "skip2" }, new String[] { "other1", "other2", "sync1", "sync2" }); michael@0: assertStages(all, new String[] { "sync1", "sync2" }, new String[] { "skip1", "skip2" }, new String[] { "sync1", "sync2" }); michael@0: } michael@0: michael@0: protected static void assertStagesFromBundle(String[] all, String[] sync, String[] skip, String[] expected) { michael@0: final Set sAll = new HashSet(); michael@0: for (String s : all) { michael@0: sAll.add(s); michael@0: } michael@0: final Bundle bundle = new Bundle(); michael@0: Utils.putStageNamesToSync(bundle, sync, skip); michael@0: michael@0: Collection ss = Utils.getStagesToSyncFromBundle(sAll, bundle); michael@0: List stages = new ArrayList(ss); michael@0: Collections.sort(stages); michael@0: List exp = new ArrayList(); michael@0: for (String e : expected) { michael@0: exp.add(e); michael@0: } michael@0: assertEquals(exp, stages); michael@0: } michael@0: michael@0: public void testGetStagesToSyncFromBundle() { michael@0: final String[] all = new String[] { "other1", "other2", "skip1", "skip2", "sync1", "sync2" }; michael@0: assertStagesFromBundle(all, null, null, all); michael@0: assertStagesFromBundle(all, new String[] { "sync1" }, null, new String[] { "sync1" }); michael@0: assertStagesFromBundle(all, null, new String[] { "skip1", "skip2" }, new String[] { "other1", "other2", "sync1", "sync2" }); michael@0: assertStagesFromBundle(all, new String[] { "sync1", "sync2" }, new String[] { "skip1", "skip2" }, new String[] { "sync1", "sync2" }); michael@0: } michael@0: michael@0: public static void deleteDirectoryRecursively(final File dir) throws IOException { michael@0: if (!dir.isDirectory()) { michael@0: throw new IllegalStateException("Given directory, " + dir + ", is not a directory!"); michael@0: } michael@0: michael@0: for (File f : dir.listFiles()) { michael@0: if (f.isDirectory()) { michael@0: deleteDirectoryRecursively(f); michael@0: } else if (!f.delete()) { michael@0: // Since this method is for testing, we assume we should be able to do this. michael@0: throw new IOException("Could not delete file, " + f.getAbsolutePath() + ". Permissions?"); michael@0: } michael@0: } michael@0: michael@0: if (!dir.delete()) { michael@0: throw new IOException("Could not delete dir, " + dir.getAbsolutePath() + "."); michael@0: } michael@0: } michael@0: michael@0: public void testDeleteDirectoryRecursively() throws Exception { michael@0: final String TEST_DIR = getApplicationContext().getCacheDir().getAbsolutePath() + michael@0: "-testDeleteDirectory-" + System.currentTimeMillis(); michael@0: michael@0: // Non-existent directory. michael@0: final File nonexistent = new File("nonexistentDirectory"); // Hopefully. ;) michael@0: assertFalse(nonexistent.exists()); michael@0: try { michael@0: deleteDirectoryRecursively(nonexistent); michael@0: fail("deleteDirectoryRecursively on a nonexistent directory should throw Exception"); michael@0: } catch (IllegalStateException e) { } michael@0: michael@0: // Empty dir. michael@0: File dir = mkdir(TEST_DIR); michael@0: deleteDirectoryRecursively(dir); michael@0: assertFalse(dir.exists()); michael@0: michael@0: // Filled dir. michael@0: dir = mkdir(TEST_DIR); michael@0: populateDir(dir); michael@0: deleteDirectoryRecursively(dir); michael@0: assertFalse(dir.exists()); michael@0: michael@0: // Filled dir with empty dir. michael@0: dir = mkdir(TEST_DIR); michael@0: populateDir(dir); michael@0: File subDir = new File(TEST_DIR + File.separator + "subDir"); michael@0: assertTrue(subDir.mkdir()); michael@0: deleteDirectoryRecursively(dir); michael@0: assertFalse(subDir.exists()); // For short-circuiting errors. michael@0: assertFalse(dir.exists()); michael@0: michael@0: // Filled dir with filled dir. michael@0: dir = mkdir(TEST_DIR); michael@0: populateDir(dir); michael@0: subDir = new File(TEST_DIR + File.separator + "subDir"); michael@0: assertTrue(subDir.mkdir()); michael@0: populateDir(subDir); michael@0: deleteDirectoryRecursively(dir); michael@0: assertFalse(subDir.exists()); // For short-circuiting errors. michael@0: assertFalse(dir.exists()); michael@0: } michael@0: michael@0: private File mkdir(final String name) { michael@0: final File dir = new File(name); michael@0: assertTrue(dir.mkdir()); michael@0: return dir; michael@0: } michael@0: michael@0: private void populateDir(final File dir) throws IOException { michael@0: assertTrue(dir.isDirectory()); michael@0: final String dirPath = dir.getAbsolutePath(); michael@0: for (int i = 0; i < 3; i++) { michael@0: final File f = new File(dirPath + File.separator + i); michael@0: assertTrue(f.createNewFile()); // Throws IOException if file could not be created. michael@0: } michael@0: } michael@0: }