|
1 | 1 | package com.contentstack.sdk; |
2 | 2 |
|
| 3 | +import static org.junit.Assert.*; |
| 4 | +import static org.mockito.Mockito.*; |
| 5 | + |
3 | 6 | import android.content.Context; |
4 | 7 | import android.content.Intent; |
5 | 8 |
|
6 | 9 | import org.json.JSONObject; |
| 10 | +import org.junit.Before; |
7 | 11 | import org.junit.Test; |
| 12 | +import org.junit.runner.RunWith; |
| 13 | +import org.robolectric.RobolectricTestRunner; |
| 14 | +import org.robolectric.RuntimeEnvironment; |
8 | 15 |
|
9 | 16 | import java.io.File; |
10 | 17 | import java.io.FileWriter; |
11 | | -import java.util.Calendar; |
12 | | -import java.util.TimeZone; |
13 | 18 | import java.util.concurrent.TimeUnit; |
14 | 19 |
|
15 | | -import static org.junit.Assert.*; |
16 | | -import static org.mockito.Mockito.*; |
17 | | - |
| 20 | +@RunWith(RobolectricTestRunner.class) |
18 | 21 | public class TestClearCache { |
19 | 22 |
|
20 | | - private File createTempDir() { |
21 | | - File dir = new File(System.getProperty("java.io.tmpdir"), |
22 | | - "ContentstackCacheTest_" + System.nanoTime()); |
23 | | - //noinspection ResultOfMethodCallIgnored |
24 | | - dir.mkdirs(); |
25 | | - return dir; |
26 | | - } |
| 23 | + private Context context; |
| 24 | + private File cacheDir; |
| 25 | + |
| 26 | + @Before |
| 27 | + public void setUp() { |
| 28 | + context = RuntimeEnvironment.getApplication(); |
27 | 29 |
|
28 | | - private File writeCacheFile(File dir, String name, long timestampMillis) throws Exception { |
29 | | - File file = new File(dir, name); |
30 | | - JSONObject json = new JSONObject(); |
31 | | - json.put("timestamp", String.valueOf(timestampMillis)); |
32 | | - try (FileWriter writer = new FileWriter(file)) { |
33 | | - writer.write(json.toString()); |
| 30 | + // This will be something like /data/data/.../app_ContentstackCache-test |
| 31 | + cacheDir = context.getDir("ContentstackCache", 0); |
| 32 | + |
| 33 | + // Clean it before each test |
| 34 | + File[] files = cacheDir.listFiles(); |
| 35 | + if (files != null) { |
| 36 | + for (File f : files) { |
| 37 | + // Best-effort cleanup |
| 38 | + f.delete(); |
| 39 | + } |
34 | 40 | } |
35 | | - return file; |
36 | 41 | } |
37 | 42 |
|
| 43 | + private File createJsonCacheFile(String name, long timestampMillis) throws Exception { |
| 44 | + File f = new File(cacheDir, name); |
| 45 | + JSONObject obj = new JSONObject(); |
| 46 | + obj.put("timestamp", String.valueOf(timestampMillis)); |
| 47 | + FileWriter writer = new FileWriter(f); |
| 48 | + writer.write(obj.toString()); |
| 49 | + writer.flush(); |
| 50 | + writer.close(); |
| 51 | + return f; |
| 52 | + } |
| 53 | + |
| 54 | + private File createPlainFile(String name) throws Exception { |
| 55 | + File f = new File(cacheDir, name); |
| 56 | + FileWriter writer = new FileWriter(f); |
| 57 | + writer.write("dummy"); |
| 58 | + writer.flush(); |
| 59 | + writer.close(); |
| 60 | + return f; |
| 61 | + } |
| 62 | + |
| 63 | + private long now() { |
| 64 | + return System.currentTimeMillis(); |
| 65 | + } |
| 66 | + |
| 67 | + // ---------------------------------------------------- |
| 68 | + // 1. Old file (>=24h) should be deleted |
| 69 | + // ---------------------------------------------------- |
38 | 70 | @Test |
39 | | - public void testOnReceive_deletesOldFilesAndKeepsRecent() throws Exception { |
40 | | - // Mock Context |
41 | | - Context context = mock(Context.class); |
| 71 | + public void testOnReceive_deletesOldFile() throws Exception { |
| 72 | + long twentyFiveHoursAgo = now() - TimeUnit.HOURS.toMillis(25); |
| 73 | + |
| 74 | + File oldFile = createJsonCacheFile("old_response.json", twentyFiveHoursAgo); |
42 | 75 |
|
43 | | - // Use a temp directory to simulate ContentstackCache |
44 | | - File cacheDir = createTempDir(); |
45 | | - when(context.getDir("ContentstackCache", 0)).thenReturn(cacheDir); |
| 76 | + assertTrue("Old file should exist before onReceive", oldFile.exists()); |
46 | 77 |
|
47 | | - // current time (UTC aligned like ClearCache) |
48 | | - Calendar cal = Calendar.getInstance(); |
49 | | - cal.setTimeZone(TimeZone.getTimeZone("UTC")); |
50 | | - long nowMillis = cal.getTimeInMillis(); |
| 78 | + ClearCache clearCache = new ClearCache(); |
| 79 | + clearCache.onReceive(context, new Intent("com.contentstack.sdk.CLEAR_CACHE")); |
51 | 80 |
|
52 | | - long twentyFiveHoursAgo = nowMillis - TimeUnit.HOURS.toMillis(25); |
53 | | - long oneHourAgo = nowMillis - TimeUnit.HOURS.toMillis(1); |
| 81 | + assertFalse("Old file should be deleted", oldFile.exists()); |
| 82 | + } |
54 | 83 |
|
55 | | - // old file: should be deleted |
56 | | - File oldFile = writeCacheFile(cacheDir, "old_response.json", twentyFiveHoursAgo); |
| 84 | + // ---------------------------------------------------- |
| 85 | + // 2. Recent file (<24h) should NOT be deleted |
| 86 | + // ---------------------------------------------------- |
| 87 | + @Test |
| 88 | + public void testOnReceive_keepsRecentFile() throws Exception { |
| 89 | + long oneHourAgo = now() - TimeUnit.HOURS.toMillis(1); |
57 | 90 |
|
58 | | - // recent file: should be kept |
59 | | - File recentFile = writeCacheFile(cacheDir, "recent_response.json", oneHourAgo); |
| 91 | + File recentFile = createJsonCacheFile("recent_response.json", oneHourAgo); |
60 | 92 |
|
61 | | - // session and installation files: never deleted |
62 | | - File sessionFile = writeCacheFile(cacheDir, "Session", twentyFiveHoursAgo); |
63 | | - File installationFile = writeCacheFile(cacheDir, "Installation", twentyFiveHoursAgo); |
| 93 | + assertTrue("Recent file should exist before onReceive", recentFile.exists()); |
64 | 94 |
|
65 | 95 | ClearCache clearCache = new ClearCache(); |
66 | | - clearCache.onReceive(context, new Intent("test.intent.CLEAR_CACHE")); |
| 96 | + clearCache.onReceive(context, new Intent("com.contentstack.sdk.CLEAR_CACHE")); |
| 97 | + |
| 98 | + assertTrue("Recent file should NOT be deleted", recentFile.exists()); |
| 99 | + } |
| 100 | + |
| 101 | + // ---------------------------------------------------- |
| 102 | + // 3. Session and Installation files are ignored |
| 103 | + // ---------------------------------------------------- |
| 104 | + @Test |
| 105 | + public void testOnReceive_ignoresSessionAndInstallationFiles() throws Exception { |
| 106 | + // Even if they look old, code explicitly ignores them |
| 107 | + |
| 108 | + long twentyFiveHoursAgo = now() - TimeUnit.HOURS.toMillis(25); |
67 | 109 |
|
68 | | - // Old file should be gone |
69 | | - assertFalse("Old cache file should be deleted", oldFile.exists()); |
| 110 | + File sessionFile = createJsonCacheFile("Session", twentyFiveHoursAgo); |
| 111 | + File installationFile = createJsonCacheFile("Installation", twentyFiveHoursAgo); |
70 | 112 |
|
71 | | - // Recent file should still be there |
72 | | - assertTrue("Recent cache file should not be deleted", recentFile.exists()); |
| 113 | + assertTrue(sessionFile.exists()); |
| 114 | + assertTrue(installationFile.exists()); |
| 115 | + |
| 116 | + ClearCache clearCache = new ClearCache(); |
| 117 | + clearCache.onReceive(context, new Intent("com.contentstack.sdk.CLEAR_CACHE")); |
73 | 118 |
|
74 | | - // Session and Installation should not be deleted |
| 119 | + // They should still exist because of the name-based skip condition |
75 | 120 | assertTrue("Session file should not be deleted", sessionFile.exists()); |
76 | 121 | assertTrue("Installation file should not be deleted", installationFile.exists()); |
77 | 122 | } |
78 | 123 |
|
| 124 | + // ---------------------------------------------------- |
| 125 | + // 4. File without valid JSON or timestamp should be ignored (no crash) |
| 126 | + // ---------------------------------------------------- |
79 | 127 | @Test |
80 | | - public void testOnReceive_handlesEmptyDirectoryGracefully() { |
81 | | - Context context = mock(Context.class); |
| 128 | + public void testOnReceive_invalidJsonOrNoTimestamp_doesNotCrashAndKeepsFile() throws Exception { |
| 129 | + File invalidFile = createPlainFile("invalid.json"); |
82 | 130 |
|
83 | | - File cacheDir = createTempDir(); |
84 | | - when(context.getDir("ContentstackCache", 0)).thenReturn(cacheDir); |
85 | | - |
86 | | - // Ensure directory is empty |
87 | | - File[] existing = cacheDir.listFiles(); |
88 | | - if (existing != null) { |
89 | | - for (File f : existing) { |
90 | | - //noinspection ResultOfMethodCallIgnored |
91 | | - f.delete(); |
92 | | - } |
93 | | - } |
| 131 | + assertTrue(invalidFile.exists()); |
94 | 132 |
|
95 | 133 | ClearCache clearCache = new ClearCache(); |
96 | | - clearCache.onReceive(context, new Intent("test.intent.CLEAR_CACHE")); |
| 134 | + clearCache.onReceive(context, new Intent("com.contentstack.sdk.CLEAR_CACHE")); |
97 | 135 |
|
98 | | - // No crash is success; directory should still exist |
99 | | - assertTrue(cacheDir.exists()); |
| 136 | + // Since getJsonFromCacheFile likely returns null or throws handled internally, |
| 137 | + // the file should not be deleted by our logic. |
| 138 | + assertTrue("Invalid file should still exist", invalidFile.exists()); |
100 | 139 | } |
101 | 140 | } |
0 commit comments