(FRONT) FRONT (2025)

Return to test key-value service Simple key-value service with Local Storage (test)

   1:  package com.voipplus.mmsclient;
   2:  
   3:  import android.content.ComponentName;
   4:  import android.content.Context;
   5:  import android.content.Intent;
   6:  import android.content.ServiceConnection;
   7:  import android.os.IBinder;
   8:  
   9:  import androidx.test.core.app.ApplicationProvider;
  10:  
  11:  import com.voipplus.mmsclient.serviceLocalStorage.KeyValueService;
  12:  
  13:  import org.junit.After;
  14:  import org.junit.Before;
  15:  import org.junit.Test;
  16:  
  17:  import java.util.Set;
  18:  
  19:  import static org.junit.Assert.*;
  20:  
  21:  public class KeyValueServiceTest {
  22:  
  23:      private KeyValueService keyValueService;
  24:      private boolean isBound = false;
  25:      private ServiceConnection connection;
  26:  
  27:      @Before
  28:      public void setUp() throws InterruptedException {
  29:          Context context = ApplicationProvider.getApplicationContext();
  30:          Intent intent = new Intent(context, KeyValueService.class);
  31:          connection = new ServiceConnection() {
  32:              @Override
  33:              public void onServiceConnected(ComponentName name, IBinder service) {
  34:                  KeyValueService.LocalBinder binder = (KeyValueService.LocalBinder) service;
  35:                  keyValueService = binder.getService();
  36:                  isBound = true;
  37:              }
  38:  
  39:              @Override
  40:              public void onServiceDisconnected(ComponentName name) {
  41:                  isBound = false;
  42:              }
  43:          };
  44:          context.bindService(intent, connection, Context.BIND_AUTO_CREATE);
  45:          Thread.sleep(1000); // Wait for service to connect
  46:      }
  47:  
  48:      @After
  49:      public void tearDown() {
  50:          if (isBound) {
  51:              Context context = ApplicationProvider.getApplicationContext();
  52:              context.unbindService(connection);
  53:              isBound = false;
  54:          }
  55:      }
  56:  
  57:      @Test
  58:      public void testPutAndGet() {
  59:          keyValueService.put("testKey", "testValue");
  60:          String retrievedValue = keyValueService.get("testKey");
  61:          assertEquals("testValue", retrievedValue);
  62:      }
  63:  
  64:      @Test
  65:      public void testRemove() {
  66:          keyValueService.put("testKey", "testValue");
  67:          keyValueService.remove("testKey");
  68:          String retrievedValue = keyValueService.get("testKey");
  69:          assertNull(retrievedValue);
  70:      }
  71:  
  72:      @Test
  73:      public void testContains() {
  74:          keyValueService.put("testKey", "testValue");
  75:          assertTrue(keyValueService.contains("testKey"));
  76:          assertFalse(keyValueService.contains("nonExistentKey"));
  77:      }
  78:  
  79:      @Test
  80:      public void testList() {
  81:          keyValueService.put("key1", "value1");
  82:          keyValueService.put("key2", "value2");
  83:          Set<String> keys = keyValueService.list();
  84:          assertTrue(keys.contains("key1"));
  85:          assertTrue(keys.contains("key2"));
  86:      }
  87:  }
  88:  

Return to test key-value service




AndroidMosaic context:



Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23>  <24>  <25
Link to this page: http://www.vb-net.com/AndroidMosaic/KeyValueServiceTest.htm
<TAGS>  <ARTICLES>  <FRONT>  <CORE>  <MVC>  <ASP>  <NET>  <DATA>  <TASK>  <XML>  <KIOSK>  <NOTES>  <SQL>  <LINUX>  <MONO>  <FREEWARE>  <DOCS> <TRAVELS> <FLOWERS> <RESUME> < THANKS ME>