Skip to content

Commit 9cbdcf8

Browse files
Add unit tests for ClearCache, verifying deletion of old cache files and handling of empty directories to ensure correct cache management behavior and enhance test coverage.
1 parent b5af4f2 commit 9cbdcf8

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.contentstack.sdk;
2+
3+
import android.content.Context;
4+
import android.content.Intent;
5+
6+
import org.json.JSONObject;
7+
import org.junit.Test;
8+
9+
import java.io.File;
10+
import java.io.FileWriter;
11+
import java.util.Calendar;
12+
import java.util.TimeZone;
13+
import java.util.concurrent.TimeUnit;
14+
15+
import static org.junit.Assert.*;
16+
import static org.mockito.Mockito.*;
17+
18+
public class TestClearCache {
19+
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+
}
27+
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());
34+
}
35+
return file;
36+
}
37+
38+
@Test
39+
public void testOnReceive_deletesOldFilesAndKeepsRecent() throws Exception {
40+
// Mock Context
41+
Context context = mock(Context.class);
42+
43+
// Use a temp directory to simulate ContentstackCache
44+
File cacheDir = createTempDir();
45+
when(context.getDir("ContentstackCache", 0)).thenReturn(cacheDir);
46+
47+
// current time (UTC aligned like ClearCache)
48+
Calendar cal = Calendar.getInstance();
49+
cal.setTimeZone(TimeZone.getTimeZone("UTC"));
50+
long nowMillis = cal.getTimeInMillis();
51+
52+
long twentyFiveHoursAgo = nowMillis - TimeUnit.HOURS.toMillis(25);
53+
long oneHourAgo = nowMillis - TimeUnit.HOURS.toMillis(1);
54+
55+
// old file: should be deleted
56+
File oldFile = writeCacheFile(cacheDir, "old_response.json", twentyFiveHoursAgo);
57+
58+
// recent file: should be kept
59+
File recentFile = writeCacheFile(cacheDir, "recent_response.json", oneHourAgo);
60+
61+
// session and installation files: never deleted
62+
File sessionFile = writeCacheFile(cacheDir, "Session", twentyFiveHoursAgo);
63+
File installationFile = writeCacheFile(cacheDir, "Installation", twentyFiveHoursAgo);
64+
65+
ClearCache clearCache = new ClearCache();
66+
clearCache.onReceive(context, new Intent("test.intent.CLEAR_CACHE"));
67+
68+
// Old file should be gone
69+
assertFalse("Old cache file should be deleted", oldFile.exists());
70+
71+
// Recent file should still be there
72+
assertTrue("Recent cache file should not be deleted", recentFile.exists());
73+
74+
// Session and Installation should not be deleted
75+
assertTrue("Session file should not be deleted", sessionFile.exists());
76+
assertTrue("Installation file should not be deleted", installationFile.exists());
77+
}
78+
79+
@Test
80+
public void testOnReceive_handlesEmptyDirectoryGracefully() {
81+
Context context = mock(Context.class);
82+
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+
}
94+
95+
ClearCache clearCache = new ClearCache();
96+
clearCache.onReceive(context, new Intent("test.intent.CLEAR_CACHE"));
97+
98+
// No crash is success; directory should still exist
99+
assertTrue(cacheDir.exists());
100+
}
101+
}

0 commit comments

Comments
 (0)