|
| 1 | +package com.parse; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import androidx.test.platform.app.InstrumentationRegistry; |
| 5 | +import java.io.File; |
| 6 | +import java.util.ArrayList; |
| 7 | +import org.junit.After; |
| 8 | +import org.junit.Before; |
| 9 | +import org.junit.Test; |
| 10 | +import org.junit.runner.RunWith; |
| 11 | +import org.robolectric.RobolectricTestRunner; |
| 12 | + |
| 13 | +@RunWith(RobolectricTestRunner.class) |
| 14 | +public class ParseCacheDirMigrationUtilsTest { |
| 15 | + ArrayList<File> writtenFiles = new ArrayList<>(); |
| 16 | + private ParseCacheDirMigrationUtils utils; |
| 17 | + |
| 18 | + @Before |
| 19 | + public void setUp() throws Exception { |
| 20 | + utils = |
| 21 | + new ParseCacheDirMigrationUtils( |
| 22 | + InstrumentationRegistry.getInstrumentation().getContext()); |
| 23 | + writtenFiles.clear(); |
| 24 | + } |
| 25 | + |
| 26 | + @After |
| 27 | + public void tearDown() throws Exception { |
| 28 | + writtenFiles.clear(); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void testMigrationOnParseSDKInitialization() { |
| 33 | + prepareForMockFilesWriting(); |
| 34 | + writtenFiles.addAll(writeSomeMockFiles(true)); |
| 35 | + Parse.Configuration configuration = |
| 36 | + new Parse.Configuration.Builder( |
| 37 | + InstrumentationRegistry.getInstrumentation().getContext()) |
| 38 | + .applicationId(BuildConfig.LIBRARY_PACKAGE_NAME) |
| 39 | + .server("https://api.parse.com/1") |
| 40 | + .enableLocalDataStore() |
| 41 | + .build(); |
| 42 | + Parse.initialize(configuration); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testMockMigration() { |
| 47 | + prepareForMockFilesWriting(); |
| 48 | + writtenFiles.addAll(writeSomeMockFiles(true)); |
| 49 | + |
| 50 | + // Run migration. |
| 51 | + utils.runMigrations(); |
| 52 | + |
| 53 | + // Check for cache file after migration. |
| 54 | + File cacheDir = InstrumentationRegistry.getInstrumentation().getContext().getCacheDir(); |
| 55 | + ArrayList<File> migratedCaches = new ArrayList<>(); |
| 56 | + ParseFileUtils.getAllNestedFiles(cacheDir.getAbsolutePath(), migratedCaches); |
| 57 | + |
| 58 | + // Check for files file after migration. |
| 59 | + File filesDir = InstrumentationRegistry.getInstrumentation().getContext().getFilesDir(); |
| 60 | + ArrayList<File> migratedFiles = new ArrayList<>(); |
| 61 | + ParseFileUtils.getAllNestedFiles(filesDir.getAbsolutePath(), migratedFiles); |
| 62 | + |
| 63 | + // To check migrations result |
| 64 | + int sizeAfterMigration = (migratedCaches.size() + migratedFiles.size()); |
| 65 | + int sizeBeforeMigrations = writtenFiles.size(); |
| 66 | + |
| 67 | + assert (cacheDir.exists() && !migratedCaches.isEmpty()); |
| 68 | + assert (filesDir.exists() && !migratedFiles.isEmpty()); |
| 69 | + assert sizeBeforeMigrations == sizeAfterMigration; |
| 70 | + } |
| 71 | + |
| 72 | + private void prepareForMockFilesWriting() { |
| 73 | + // Delete `"app_Parse"` dir including nested dir and files. |
| 74 | + try { |
| 75 | + ParseFileUtils.deleteDirectory( |
| 76 | + InstrumentationRegistry.getInstrumentation() |
| 77 | + .getContext() |
| 78 | + .getDir("Parse", Context.MODE_PRIVATE)); |
| 79 | + } catch (Exception e) { |
| 80 | + e.printStackTrace(); |
| 81 | + } |
| 82 | + writtenFiles.clear(); |
| 83 | + // Create new `"app_Parse"` dir to write some files. |
| 84 | + createFileDir(InstrumentationRegistry.getInstrumentation().getContext().getCacheDir()); |
| 85 | + } |
| 86 | + |
| 87 | + private ArrayList<File> writeSomeMockFiles(Boolean checkForExistingFile) { |
| 88 | + ArrayList<File> fileToReturn = new ArrayList<>(); |
| 89 | + File oldRef = |
| 90 | + InstrumentationRegistry.getInstrumentation() |
| 91 | + .getContext() |
| 92 | + .getDir("Parse", Context.MODE_PRIVATE); |
| 93 | + |
| 94 | + // Writing some config & random files for migration process. |
| 95 | + File config = new File(oldRef + "/config/", "config"); |
| 96 | + fileToReturn.add(config); |
| 97 | + File installationId = new File(oldRef + "/CommandCache/", "installationId"); |
| 98 | + fileToReturn.add(installationId); |
| 99 | + File currentConfig = new File(oldRef + "/", "currentConfig"); |
| 100 | + fileToReturn.add(currentConfig); |
| 101 | + File currentInstallation = new File(oldRef + "/", "currentInstallation"); |
| 102 | + fileToReturn.add(currentInstallation); |
| 103 | + File pushState = new File(oldRef + "/push/", "pushState"); |
| 104 | + fileToReturn.add(pushState); |
| 105 | + File localId = new File(oldRef + "/LocalId/", "LocalId"); |
| 106 | + fileToReturn.add(localId); |
| 107 | + File cache = new File(oldRef + "/testcache/", "cache"); |
| 108 | + fileToReturn.add(cache); |
| 109 | + File cache1 = new File(oldRef + "/testcache/", "cache1"); |
| 110 | + fileToReturn.add(cache1); |
| 111 | + File cache2 = new File(oldRef + "/testcache/another/", "cache4"); |
| 112 | + fileToReturn.add(cache2); |
| 113 | + File user = new File(oldRef + "/user/", "user_config"); |
| 114 | + fileToReturn.add(user); |
| 115 | + |
| 116 | + // Write all listed files to the app cache ("app_Parse") directory. |
| 117 | + for (File item : fileToReturn) { |
| 118 | + try { |
| 119 | + ParseFileUtils.writeStringToFile(item, "gger", "UTF-8"); |
| 120 | + } catch (Exception e) { |
| 121 | + e.printStackTrace(); |
| 122 | + } |
| 123 | + } |
| 124 | + // To create a file conflict scenario during migration by creating an existing file to the |
| 125 | + // new files dir ("*/files/com.parse/*"). |
| 126 | + if (checkForExistingFile) { |
| 127 | + try { |
| 128 | + ParseFileUtils.writeStringToFile( |
| 129 | + new File( |
| 130 | + InstrumentationRegistry.getInstrumentation() |
| 131 | + .getContext() |
| 132 | + .getFilesDir() |
| 133 | + + "/com.parse/CommandCache/", |
| 134 | + "installationId"), |
| 135 | + "gger", |
| 136 | + "UTF-8"); |
| 137 | + } catch (Exception e) { |
| 138 | + e.printStackTrace(); |
| 139 | + } |
| 140 | + } |
| 141 | + return fileToReturn; |
| 142 | + } |
| 143 | + |
| 144 | + private File createFileDir(File file) { |
| 145 | + if (!file.exists()) { |
| 146 | + if (!file.mkdirs()) { |
| 147 | + return file; |
| 148 | + } |
| 149 | + } |
| 150 | + return file; |
| 151 | + } |
| 152 | +} |
0 commit comments