All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.googlecode.objectify.test.CachingDatastoreTests Maven / Gradle / Ivy

Go to download

*** THIS VERSION UPLOADED FOR USE WITH CEDAR-COMMON, TO AVOID DEPENDENCIES ON GOOGLE CODE-BASED MAVEN REPOSITORIES. *** The simplest convenient interface to the Google App Engine datastore

The newest version!
/*
 */

package com.googlecode.objectify.test;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Future;
import java.util.logging.Logger;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.googlecode.objectify.cache.CachingAsyncDatastoreService;
import com.googlecode.objectify.cache.EntityMemcache;
import com.googlecode.objectify.test.util.MockAsyncDatastoreService;

/**
 * Tests of the caching datastore directly, outside of the rest of Objectify. Tries to create as few
 * future wrappers as possible so we can easily see what is going on.
 *
 * @author Jeff Schnitzer 
 */
public class CachingDatastoreTests extends TestBase
{
	/** */
	@SuppressWarnings("unused")
	private static Logger log = Logger.getLogger(CachingDatastoreTests.class.getName());
	
	/** Caching */
	CachingAsyncDatastoreService cads;
	
	/** No datastore */
	CachingAsyncDatastoreService nods;

	/** Simple bits of data to use */
	Key key;
	Set keyInSet;
	Entity entity;
	List entityInList;
	
	/**
	 */
	@BeforeMethod
	public void setUp()
	{
		super.setUp();
		
		EntityMemcache mc = new EntityMemcache(null);
		cads = new CachingAsyncDatastoreService(DatastoreServiceFactory.getAsyncDatastoreService(), mc);
		nods = new CachingAsyncDatastoreService(new MockAsyncDatastoreService(), mc);
		
		key = KeyFactory.createKey("thing", 1);
		keyInSet = Collections.singleton(key);
		entity = new Entity(key);
		entity.setProperty("foo", "bar");
		entityInList = Collections.singletonList(entity);
	}
	
	/** */
	@Test
	public void testNegativeCache() throws Exception
	{
		Future> fent = cads.get(null, keyInSet);
		assert fent.get().isEmpty();
		
		// Now that it's called, make sure we have a negative cache entry
		Future> cached = nods.get(null, keyInSet);
		assert cached.get().isEmpty();
	}

	/** */
	@Test
	public void testBasicCache() throws Exception
	{
		Future> fkey = cads.put(null, entityInList);
		List putResult = fkey.get();
		
		Future> fent = cads.get(null, putResult);
		assert fent.get().values().iterator().next().getProperty("foo").equals("bar");
		
		// Now make sure it is in the cache
		Future> cached = nods.get(null, putResult);
		assert cached.get().values().iterator().next().getProperty("foo").equals("bar");
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy