1
1
package com .parse ;
2
2
3
3
import android .content .Context ;
4
+
4
5
import androidx .test .platform .app .InstrumentationRegistry ;
6
+
5
7
import org .junit .After ;
6
8
import org .junit .Before ;
7
9
import org .junit .Test ;
8
10
import org .junit .runner .RunWith ;
9
11
import org .robolectric .RobolectricTestRunner ;
12
+
10
13
import java .io .File ;
11
14
import java .util .ArrayList ;
12
15
13
16
@ RunWith (RobolectricTestRunner .class )
14
- public class ParseCacheDirMigrationTest {
17
+ public class ParseCacheDirMigrationUtilsTest {
15
18
ArrayList <File > writtenFiles = new ArrayList <>();
19
+ private ParseCacheDirMigrationUtils utils ;
16
20
17
21
@ Before
18
22
public void setUp () throws Exception {
19
- Context context = InstrumentationRegistry .getInstrumentation ().getContext ();
20
- ParsePlugins .reset ();
21
- Parse .Configuration configuration =
22
- new Parse .Configuration .Builder (context ).applicationId ("1234" ).build ();
23
- ParsePlugins .initialize (context , configuration );
23
+ utils = new ParseCacheDirMigrationUtils (InstrumentationRegistry .getInstrumentation ().getContext ());
24
24
writtenFiles .clear ();
25
25
}
26
26
27
27
@ After
28
28
public void tearDown () throws Exception {
29
- ParsePlugins .reset ();
30
29
writtenFiles .clear ();
31
30
}
32
31
33
32
@ Test
34
- public void manualMigrationBeforeAccessNewCacheAPIs () {
33
+ public void testMigrationOnParseSDKInitialization () {
34
+ prepareForMockFilesWriting ();
35
+ writtenFiles .addAll (writeSomeMockFiles (true ));
36
+ Parse .Configuration configuration =
37
+ new Parse .Configuration .Builder (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 () {
35
47
prepareForMockFilesWriting ();
36
- writtenFiles .addAll (writeSomeMockFiles (false ));
48
+ writtenFiles .addAll (writeSomeMockFiles (true ));
37
49
38
- //Run migration manually .
39
- ParsePlugins . get (). runSilentMigration ();
50
+ //Run migration.
51
+ utils . runMigrations ();
40
52
41
53
//Check for cache file after migration.
42
- File cacheDir = ParsePlugins . get ().getCacheDir ();
54
+ File cacheDir = InstrumentationRegistry . getInstrumentation (). getContext ().getCacheDir ();
43
55
ArrayList <File > migratedCaches = new ArrayList <>();
44
56
ParseFileUtils .getAllNestedFiles (cacheDir .getAbsolutePath (), migratedCaches );
45
57
46
58
//Check for files file after migration.
47
- File filesDir = ParsePlugins . get ().getFilesDir ();
59
+ File filesDir = InstrumentationRegistry . getInstrumentation (). getContext ().getFilesDir ();
48
60
ArrayList <File > migratedFiles = new ArrayList <>();
49
61
ParseFileUtils .getAllNestedFiles (filesDir .getAbsolutePath (), migratedFiles );
50
62
@@ -57,62 +69,21 @@ public void manualMigrationBeforeAccessNewCacheAPIs() {
57
69
assert sizeBeforeMigrations == sizeAfterMigration ;
58
70
}
59
71
60
- @ Test
61
- public void autoMigrationBeforeAccessFilesDir () {
62
- prepareForMockFilesWriting ();
63
- writtenFiles .addAll (writeSomeMockFiles (false ));
64
- ArrayList <File > migratedFiles = new ArrayList <>();
65
- //Auto migration
66
- File filesDir = ParsePlugins .get ().getFilesDir (true );
67
- ParseFileUtils .getAllNestedFiles (filesDir .getAbsolutePath (), migratedFiles );
68
- assert !migratedFiles .isEmpty ();
69
- }
70
-
71
- @ Test
72
- public void autoMigrationBeforeAccessCacheDir () {
73
- prepareForMockFilesWriting ();
74
- writtenFiles .addAll (writeSomeMockFiles (false ));
75
- ArrayList <File > migratedCaches = new ArrayList <>();
76
- //Auto migration
77
- File cacheDir = ParsePlugins .get ().getCacheDir (true );
78
- ParseFileUtils .getAllNestedFiles (cacheDir .getAbsolutePath (), migratedCaches );
79
- assert !migratedCaches .isEmpty ();
80
- }
81
-
82
- @ Test
83
- public void autoMigrationBeforeAccessCacheOrFilesDirBySolvingFileConflicts () {
84
- prepareForMockFilesWriting ();
85
- //Set some existing files in new location so that there could be file conflict.
86
- writtenFiles .addAll (writeSomeMockFiles (true ));
87
-
88
- //Auto migration for files
89
- ArrayList <File > migratedFiles = new ArrayList <>();
90
- File filesDir = ParsePlugins .get ().getFilesDir (true );
91
- ParseFileUtils .getAllNestedFiles (filesDir .getAbsolutePath (), migratedFiles );
92
-
93
- /*
94
- Auto migration for caches.
95
- Although migration already completed when accessed `ParsePlugins.get().getFilesDir(true)` or `ParsePlugins.get().getCacheDir(true)` API.
96
- */
97
- ArrayList <File > migratedCaches = new ArrayList <>();
98
- File cacheDir = ParsePlugins .get ().getCacheDir (true );
99
- ParseFileUtils .getAllNestedFiles (cacheDir .getAbsolutePath (), migratedCaches );
100
-
101
- assert !migratedFiles .isEmpty ();
102
- assert !migratedCaches .isEmpty ();
103
- }
104
-
105
72
private void prepareForMockFilesWriting () {
106
73
//Delete `"app_Parse"` dir including nested dir and files.
107
- ParsePlugins .get ().deleteOldParseDirSilently ();
74
+ try {
75
+ ParseFileUtils .deleteDirectory (InstrumentationRegistry .getInstrumentation ().getContext ().getDir ("Parse" , Context .MODE_PRIVATE ));
76
+ } catch (Exception e ) {
77
+ e .printStackTrace ();
78
+ }
108
79
writtenFiles .clear ();
109
80
//Create new `"app_Parse"` dir to write some files.
110
- createFileDir (ParsePlugins . get ().getCacheDir ());
81
+ createFileDir (InstrumentationRegistry . getInstrumentation (). getContext ().getCacheDir ());
111
82
}
112
83
113
84
private ArrayList <File > writeSomeMockFiles (Boolean checkForExistingFile ) {
114
85
ArrayList <File > fileToReturn = new ArrayList <>();
115
- File oldRef = ParsePlugins . get ().getOldParseDir ( );
86
+ File oldRef = InstrumentationRegistry . getInstrumentation ().getContext (). getDir ( "Parse" , Context . MODE_PRIVATE );
116
87
117
88
//Writing some config & random files for migration process.
118
89
File config = new File (oldRef + "/config/" , "config" );
@@ -147,7 +118,7 @@ private ArrayList<File> writeSomeMockFiles(Boolean checkForExistingFile) {
147
118
//To create a file conflict scenario during migration by creating an existing file to the new files dir ("*/files/com.parse/*").
148
119
if (checkForExistingFile ) {
149
120
try {
150
- ParseFileUtils .writeStringToFile (new File (ParsePlugins . get ().getFilesDir () + "/CommandCache/" , "installationId" ), "gger" , "UTF-8" );
121
+ ParseFileUtils .writeStringToFile (new File (InstrumentationRegistry . getInstrumentation ().getContext (). getFilesDir () + "/com.parse /CommandCache/" , "installationId" ), "gger" , "UTF-8" );
151
122
} catch (Exception e ) {
152
123
e .printStackTrace ();
153
124
}
0 commit comments