@@ -19,13 +19,85 @@ import org.junit.Assert.*
1919 */
2020@RunWith(AndroidJUnit4 ::class )
2121class SimpleCacheTest {
22+
23+ private val appContext = InstrumentationRegistry .getTargetContext()!!
24+
2225 @Test
2326 @Throws(Exception ::class )
24- fun simpleCacheTest () {
27+ fun cacheWithAutoSaveTest () {
28+ val cacheManager = CacheManager .createInstance(context = appContext)
29+
30+ cacheManager.set(" String" , " Hello World" )
31+ cacheManager.set(" Int" , 255 )
32+ cacheManager.set(" Bool" , false )
33+ cacheManager.set(" float" , 5.55 )
34+ val testMap = HashMap <String , String >()
35+ testMap[" testKey" ] = " TestValue"
36+ cacheManager.set(" map" , testMap)
37+ val testObject = TestObject (69 , " Test String" , false , 6.66f , testMap)
38+ cacheManager.set(" obj" , testObject)
39+ val testObject2 = TestObject (885 , " Testing two!" , true , 3.14f , testMap)
40+
41+ cacheManager.set(" obj2" , testObject2)
42+
43+ cacheManager.get(key = " String" , success = { value, type ->
44+ assertEquals(" Hello World" , value)
45+ assert (type.isInstance(String ::class ))
46+ Log .d(" simpleCacheTest" , value.toString())
47+ })
48+
49+ cacheManager.get(key = " Int" , checkExpired = true , success = { value, type ->
50+ assertEquals(255 , value)
51+ assert (type.isInstance(Int ::class ))
52+ Log .d(" simpleCacheTest" , value.toString())
53+ })
54+
55+ cacheManager.get(key = " Bool" , success = { value, type ->
56+ assertEquals(false , value)
57+ assert (type.isInstance(Boolean ::class ))
58+ Log .d(" simpleCacheTest" , value.toString())
59+ })
60+
61+ cacheManager.get(key = " float" , success = { value, type ->
62+ assertEquals(5.55 , value)
63+ assert (type.isInstance(Float ::class ))
64+ Log .d(" simpleCacheTest" , value.toString())
65+ })
66+
67+ cacheManager.get(key = " map" , success = { value, type ->
68+ assertEquals(testMap, value)
69+ assert (type.isInstance(HashMap ::class ))
70+ Log .d(" simpleCacheTest" , value.toString())
71+ })
72+
73+ cacheManager.get(key = " obj" , success = { value, type ->
74+ assertEquals(testObject, value)
75+ assert (type.isInstance(TestObject ::class ))
76+ Log .d(" simpleCacheTest" , value.toString())
77+ })
78+
79+ cacheManager.get(key = " obj2" , success = { value, type ->
80+ assertEquals(testObject2, value)
81+ assert (type.isInstance(TestObject ::class ))
82+ Log .d(" simpleCacheTest" , value.toString())
83+ })
84+
85+ cacheManager.remove(" obj2" )
2586
26- val appContext = InstrumentationRegistry .getTargetContext()
87+ cacheManager.get(key = " obj2 " , success = { _, _ ->
2788
28- val cacheManager = CacheManager .createInstance(appContext, null )
89+ }, error = {
90+ Log .d(" simpleCacheTest" , " obj2 is not exist." )
91+ assert (true )
92+ })
93+
94+ cacheManager.removeAllElements()
95+ }
96+
97+ @Test
98+ @Throws(Exception ::class )
99+ fun cacheWithoutAutoSaveTest () {
100+ val cacheManager = CacheManager .createInstance(context = appContext, fileName = " NoAutoSave" , autoSave = false )
29101
30102 cacheManager.set(" String" , " Hello World" )
31103 cacheManager.set(" Int" , 255 )
@@ -34,12 +106,14 @@ class SimpleCacheTest {
34106 val testMap = HashMap <String , String >()
35107 testMap[" testKey" ] = " TestValue"
36108 cacheManager.set(" map" , testMap)
37- val testObject = TestObject (69 , " abababa " , false , 6.66f , testMap)
109+ val testObject = TestObject (69 , " Test String " , false , 6.66f , testMap)
38110 cacheManager.set(" obj" , testObject)
39111 val testObject2 = TestObject (885 , " Testing two!" , true , 3.14f , testMap)
40112
41113 cacheManager.set(" obj2" , testObject2)
42114
115+ cacheManager.save()
116+
43117 cacheManager.get(key = " String" , success = { value, type ->
44118 assertEquals(" Hello World" , value)
45119 assert (type.isInstance(String ::class ))
@@ -91,8 +165,46 @@ class SimpleCacheTest {
91165 assert (true )
92166 })
93167
94- Log .d(" simpleCacheTest" , CacheManager .getListOfCacheFiles(appContext).toString())
168+ cacheManager.removeAllElements()
169+ }
170+
171+ @Test
172+ fun cacheListFilesTest () {
173+ val files = CacheManager .getListOfCacheFiles(appContext)
174+ Log .d(" simpleCacheTest" ,files.toString())
175+
176+ assert (files.size == 2 )
177+ }
178+
179+ @Test
180+ fun globalInstancesTest () {
181+ val instanceName1 = " Instance1"
182+ val instanceName2 = " Instance2"
183+
184+ val instance1 = CacheManager .createGlobalInstance(context = appContext, instanceName = instanceName1, fileName = " instanceFile1" )
185+ val instance2 = CacheManager .createGlobalInstance(context = appContext, instanceName = instanceName2, fileName = " InstanceFile2" )
186+
187+ val globalInstance1 = CacheManager .getGlobalInstance(instanceName1)
188+ val globalInstance2 = CacheManager .getGlobalInstance(instanceName2)
189+
190+ assert (instance1 == globalInstance1)
191+ assert (instance1 != globalInstance2)
192+ assert (instance2 == globalInstance2)
193+
194+ var listOfIInstancesNames = CacheManager .getListOfGlobalInstanceNames()
195+
196+ assert (listOfIInstancesNames.contains(instanceName2))
197+
198+ CacheManager .removeGlobalInstance(instanceName1)
199+
200+ listOfIInstancesNames = CacheManager .getListOfGlobalInstanceNames()
201+
202+ assert (! listOfIInstancesNames.contains(instanceName1))
203+
204+ CacheManager .removeAllGlobalInstances()
205+
206+ listOfIInstancesNames = CacheManager .getListOfGlobalInstanceNames()
95207
96- assertEquals( " com.github.pcpl2.simplecache.test " , appContext.packageName )
208+ assert (listOfIInstancesNames.isEmpty() )
97209 }
98210}
0 commit comments