Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | package org.mozilla.gecko.background.common; |
michael@0 | 5 | |
michael@0 | 6 | import java.io.File; |
michael@0 | 7 | import java.io.IOException; |
michael@0 | 8 | import java.util.ArrayList; |
michael@0 | 9 | import java.util.Collection; |
michael@0 | 10 | import java.util.Collections; |
michael@0 | 11 | import java.util.HashSet; |
michael@0 | 12 | import java.util.List; |
michael@0 | 13 | import java.util.Set; |
michael@0 | 14 | |
michael@0 | 15 | import org.mozilla.gecko.background.helpers.AndroidSyncTestCase; |
michael@0 | 16 | import org.mozilla.gecko.sync.Utils; |
michael@0 | 17 | |
michael@0 | 18 | import android.os.Bundle; |
michael@0 | 19 | |
michael@0 | 20 | public class TestUtils extends AndroidSyncTestCase { |
michael@0 | 21 | protected static void assertStages(String[] all, String[] sync, String[] skip, String[] expected) { |
michael@0 | 22 | final Set<String> sAll = new HashSet<String>(); |
michael@0 | 23 | for (String s : all) { |
michael@0 | 24 | sAll.add(s); |
michael@0 | 25 | } |
michael@0 | 26 | List<String> sSync = null; |
michael@0 | 27 | if (sync != null) { |
michael@0 | 28 | sSync = new ArrayList<String>(); |
michael@0 | 29 | for (String s : sync) { |
michael@0 | 30 | sSync.add(s); |
michael@0 | 31 | } |
michael@0 | 32 | } |
michael@0 | 33 | List<String> sSkip = null; |
michael@0 | 34 | if (skip != null) { |
michael@0 | 35 | sSkip = new ArrayList<String>(); |
michael@0 | 36 | for (String s : skip) { |
michael@0 | 37 | sSkip.add(s); |
michael@0 | 38 | } |
michael@0 | 39 | } |
michael@0 | 40 | List<String> stages = new ArrayList<String>(Utils.getStagesToSync(sAll, sSync, sSkip)); |
michael@0 | 41 | Collections.sort(stages); |
michael@0 | 42 | List<String> exp = new ArrayList<String>(); |
michael@0 | 43 | for (String e : expected) { |
michael@0 | 44 | exp.add(e); |
michael@0 | 45 | } |
michael@0 | 46 | assertEquals(exp, stages); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | public void testGetStagesToSync() { |
michael@0 | 50 | final String[] all = new String[] { "other1", "other2", "skip1", "skip2", "sync1", "sync2" }; |
michael@0 | 51 | assertStages(all, null, null, all); |
michael@0 | 52 | assertStages(all, new String[] { "sync1" }, null, new String[] { "sync1" }); |
michael@0 | 53 | assertStages(all, null, new String[] { "skip1", "skip2" }, new String[] { "other1", "other2", "sync1", "sync2" }); |
michael@0 | 54 | assertStages(all, new String[] { "sync1", "sync2" }, new String[] { "skip1", "skip2" }, new String[] { "sync1", "sync2" }); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | protected static void assertStagesFromBundle(String[] all, String[] sync, String[] skip, String[] expected) { |
michael@0 | 58 | final Set<String> sAll = new HashSet<String>(); |
michael@0 | 59 | for (String s : all) { |
michael@0 | 60 | sAll.add(s); |
michael@0 | 61 | } |
michael@0 | 62 | final Bundle bundle = new Bundle(); |
michael@0 | 63 | Utils.putStageNamesToSync(bundle, sync, skip); |
michael@0 | 64 | |
michael@0 | 65 | Collection<String> ss = Utils.getStagesToSyncFromBundle(sAll, bundle); |
michael@0 | 66 | List<String> stages = new ArrayList<String>(ss); |
michael@0 | 67 | Collections.sort(stages); |
michael@0 | 68 | List<String> exp = new ArrayList<String>(); |
michael@0 | 69 | for (String e : expected) { |
michael@0 | 70 | exp.add(e); |
michael@0 | 71 | } |
michael@0 | 72 | assertEquals(exp, stages); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | public void testGetStagesToSyncFromBundle() { |
michael@0 | 76 | final String[] all = new String[] { "other1", "other2", "skip1", "skip2", "sync1", "sync2" }; |
michael@0 | 77 | assertStagesFromBundle(all, null, null, all); |
michael@0 | 78 | assertStagesFromBundle(all, new String[] { "sync1" }, null, new String[] { "sync1" }); |
michael@0 | 79 | assertStagesFromBundle(all, null, new String[] { "skip1", "skip2" }, new String[] { "other1", "other2", "sync1", "sync2" }); |
michael@0 | 80 | assertStagesFromBundle(all, new String[] { "sync1", "sync2" }, new String[] { "skip1", "skip2" }, new String[] { "sync1", "sync2" }); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | public static void deleteDirectoryRecursively(final File dir) throws IOException { |
michael@0 | 84 | if (!dir.isDirectory()) { |
michael@0 | 85 | throw new IllegalStateException("Given directory, " + dir + ", is not a directory!"); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | for (File f : dir.listFiles()) { |
michael@0 | 89 | if (f.isDirectory()) { |
michael@0 | 90 | deleteDirectoryRecursively(f); |
michael@0 | 91 | } else if (!f.delete()) { |
michael@0 | 92 | // Since this method is for testing, we assume we should be able to do this. |
michael@0 | 93 | throw new IOException("Could not delete file, " + f.getAbsolutePath() + ". Permissions?"); |
michael@0 | 94 | } |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | if (!dir.delete()) { |
michael@0 | 98 | throw new IOException("Could not delete dir, " + dir.getAbsolutePath() + "."); |
michael@0 | 99 | } |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | public void testDeleteDirectoryRecursively() throws Exception { |
michael@0 | 103 | final String TEST_DIR = getApplicationContext().getCacheDir().getAbsolutePath() + |
michael@0 | 104 | "-testDeleteDirectory-" + System.currentTimeMillis(); |
michael@0 | 105 | |
michael@0 | 106 | // Non-existent directory. |
michael@0 | 107 | final File nonexistent = new File("nonexistentDirectory"); // Hopefully. ;) |
michael@0 | 108 | assertFalse(nonexistent.exists()); |
michael@0 | 109 | try { |
michael@0 | 110 | deleteDirectoryRecursively(nonexistent); |
michael@0 | 111 | fail("deleteDirectoryRecursively on a nonexistent directory should throw Exception"); |
michael@0 | 112 | } catch (IllegalStateException e) { } |
michael@0 | 113 | |
michael@0 | 114 | // Empty dir. |
michael@0 | 115 | File dir = mkdir(TEST_DIR); |
michael@0 | 116 | deleteDirectoryRecursively(dir); |
michael@0 | 117 | assertFalse(dir.exists()); |
michael@0 | 118 | |
michael@0 | 119 | // Filled dir. |
michael@0 | 120 | dir = mkdir(TEST_DIR); |
michael@0 | 121 | populateDir(dir); |
michael@0 | 122 | deleteDirectoryRecursively(dir); |
michael@0 | 123 | assertFalse(dir.exists()); |
michael@0 | 124 | |
michael@0 | 125 | // Filled dir with empty dir. |
michael@0 | 126 | dir = mkdir(TEST_DIR); |
michael@0 | 127 | populateDir(dir); |
michael@0 | 128 | File subDir = new File(TEST_DIR + File.separator + "subDir"); |
michael@0 | 129 | assertTrue(subDir.mkdir()); |
michael@0 | 130 | deleteDirectoryRecursively(dir); |
michael@0 | 131 | assertFalse(subDir.exists()); // For short-circuiting errors. |
michael@0 | 132 | assertFalse(dir.exists()); |
michael@0 | 133 | |
michael@0 | 134 | // Filled dir with filled dir. |
michael@0 | 135 | dir = mkdir(TEST_DIR); |
michael@0 | 136 | populateDir(dir); |
michael@0 | 137 | subDir = new File(TEST_DIR + File.separator + "subDir"); |
michael@0 | 138 | assertTrue(subDir.mkdir()); |
michael@0 | 139 | populateDir(subDir); |
michael@0 | 140 | deleteDirectoryRecursively(dir); |
michael@0 | 141 | assertFalse(subDir.exists()); // For short-circuiting errors. |
michael@0 | 142 | assertFalse(dir.exists()); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | private File mkdir(final String name) { |
michael@0 | 146 | final File dir = new File(name); |
michael@0 | 147 | assertTrue(dir.mkdir()); |
michael@0 | 148 | return dir; |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | private void populateDir(final File dir) throws IOException { |
michael@0 | 152 | assertTrue(dir.isDirectory()); |
michael@0 | 153 | final String dirPath = dir.getAbsolutePath(); |
michael@0 | 154 | for (int i = 0; i < 3; i++) { |
michael@0 | 155 | final File f = new File(dirPath + File.separator + i); |
michael@0 | 156 | assertTrue(f.createNewFile()); // Throws IOException if file could not be created. |
michael@0 | 157 | } |
michael@0 | 158 | } |
michael@0 | 159 | } |