Package jp.honestyworks.pbcache

Source Code of jp.honestyworks.pbcache.CacheServiceTest

package jp.honestyworks.pbcache;

import java.util.Date;

import org.junit.Test;
import org.slim3.tester.AppEngineTestCase;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Query;
import com.google.gdata.util.common.util.Base64;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

public class CacheServiceTest extends AppEngineTestCase {
   
    private CacheService cacheService = CacheContext.getInstance().getCacheService();
   
    @Override
    public void setUp() throws Exception {
        super.setUp();
        cacheService.clear();
    }
   
    @Test
    public void testCache() throws Exception {
       
        byte[] data = StreamUtil.toBytes(this);
        cacheService.put("key1", data);
        assertTrue(cacheService.containsKey("key1"));
       
        byte[] cachedData = (byte[])cacheService.get("key1");
        assertEquals(data, cachedData);
       
        cacheService.remove("key1");
        assertFalse(cacheService.containsKey("key1"));
       
        Object check = cacheService.get("key1");
        assertNull(check);
       
    }
   
    @Test
    public void testResetDate() throws Exception {
       
        FetchOptions fo = FetchOptions.Builder.withDefaults();
       
        cacheService.putResetDate("SomeKind");
        Date resetDate = cacheService.getResetDate("SomeKind");
        assertNotNull(resetDate);
        Thread.sleep(2000);
       
        DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
        Query q = new Query(CacheService.RESET_DATE_KIND);
        int count = ds.prepare(q).countEntities(fo);
        assertEquals(1, count); // it may fail because of HRD consistency...
       
        assertNotNull(cacheService.get(CacheService.KEY_RESET_DATE + "SomeKind"));
       
        cacheService.clear();
        Thread.sleep(2000);
       
        count = ds.prepare(q).countEntities(fo);
        assertEquals(0, count); // it may fail because of HRD consistency...
       
        assertNull(cacheService.get(CacheService.KEY_RESET_DATE + "SomeKind"));
    }
   
    @Test
    public void testBlob() throws Exception {
       
        byte[] data = getBigData();
        System.out.println("data size: " + data.length);
        try {
            cacheService.put("bigdata", data);
            //fail("exception must be thrown.");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
       
        cacheService.putBlob("bigdata", data);
       
        byte[] cachedData = cacheService.getBlob("bigdata");
        assertEquals(data.length, cachedData.length);
        //assertEquals(Base64.encode(data), Base64.encode(cachedData));
        assertEquals(new String(data), new String(cachedData));
    }
   
    private byte[] getBigData() {
       
        String seed = String.valueOf(System.currentTimeMillis());
        StringBuffer buf = new StringBuffer();
        for (int i=0; i<CacheService.CACHE_SIZE_LIMIT;i++) {
            buf.append(seed);
        }
        return buf.toString().getBytes();
    }
   
}
TOP

Related Classes of jp.honestyworks.pbcache.CacheServiceTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.