package jp.fkmsoft.libs.sqlitekvs.json;

import android.test.AndroidTestCase;

import org.json.JSONObject;

import jp.fkmsoft.libs.sqlitekvs.KVSException;
import jp.fkmsoft.libs.sqlitekvs.util.DBUtils;

/**
 * Test for JsonKVS
 */
public class TestJsonKVS extends AndroidTestCase {
    private static final String TABLE_NAME = "data1";

    private JsonKVS mKvs;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        DBUtils.deleteDB(getContext(), TABLE_NAME);
        mKvs = new JsonKVS(getContext(), TABLE_NAME);
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
        mKvs.close();
    }

    public void test_0000_put_get() throws Exception {
        JSONObject json0 = mKvs.get("item");
        assertNull(json0);

        JSONObject json1 = new JSONObject();
        json1.put("name", "fkm");

        mKvs.put("item", json1);
        mKvs.commit();

        JSONObject json2 = mKvs.get("item");
        assertNotNull(json2);
        assertEquals(1, json2.length());
        assertTrue(json2.has("name"));
        assertEquals("fkm", json2.getString("name"));
    }

    public void test_0001_put_update() throws Exception {
        test_0000_put_get();

        JSONObject json1 = new JSONObject();
        json1.put("name", "newFKM");

        mKvs.put("item", json1);
        mKvs.commit();

        JSONObject json2 = mKvs.get("item");
        assertNotNull(json2);
        assertEquals(1, json2.length());
        assertTrue(json2.has("name"));
        assertEquals("newFKM", json2.getString("name"));
    }

    public void test_0002_put_delete() throws Exception {
        test_0000_put_get();

        mKvs.put("item", null);
        mKvs.commit();

        JSONObject json2 = mKvs.get("item");
        assertNull(json2);
    }

    public void test_0003_put_put() throws Exception {
        test_0000_put_get();

        JSONObject json1 = new JSONObject();
        json1.put("name", "newFKM");

        mKvs.put("item2", json1);
        mKvs.commit();

        JSONObject json2 = mKvs.get("item");
        assertNotNull(json2);
        assertEquals(1, json2.length());
        assertTrue(json2.has("name"));
        assertEquals("fkm", json2.getString("name"));

        json2 = mKvs.get("item2");
        assertNotNull(json2);
        assertEquals(1, json2.length());
        assertTrue(json2.has("name"));
        assertEquals("newFKM", json2.getString("name"));
    }

    public void test_0010_put_null() throws Exception {
        JSONObject json1 = new JSONObject();
        json1.put("name", "fkm");

        try {
            mKvs.put(null, json1);
            fail("KVSException must be thrown");
        } catch (KVSException e) {
            // ok
        }
    }

    public void test_0011_get_null() throws Exception {
        try {
            mKvs.get(null);
            fail("KVSException must be thrown");
        } catch (KVSException e) {
            // ok
        }
    }
}

@@ -101,16 +101,78 @@
 bject;%0A%0A
+import java.util.List;%0A%0Aimport jp.fkmsoft.libs.sqlitekvs.KVS;%0A
 import j
@@ -404,23 +404,31 @@
 private 
-JsonKVS
+KVS%3CJSONObject%3E
  mKvs;%0A%0A
@@ -3056,11 +3056,573 @@
  %7D%0A    %7D
+%0A%0A    public void test_0100_getAll() throws Exception %7B%0A        for (int i = 0 ; i %3C 5 ; ++i) %7B%0A            JSONObject json = new JSONObject();%0A            json.put(%22name%22, %22fkm%22 + i);%0A%0A            mKvs.put(%22item%22 + i, json);%0A        %7D%0A        mKvs.commit();%0A%0A        List%3CJSONObject%3E items = mKvs.getAll();%0A        assertNotNull(items);%0A        assertEquals(5, items.size());%0A    %7D%0A%0A    public void test_0101_getAll_0() throws Exception %7B%0A        List%3CJSONObject%3E items = mKvs.getAll();%0A        assertNotNull(items);%0A        assertEquals(0, items.size());%0A    %7D
 %0A%7D%0A

Add tests for getAll()