PageRenderTime 545ms CodeModel.GetById 201ms app.highlight 141ms RepoModel.GetById 202ms app.codeStats 0ms

/src/test/java/com/google/ie/common/cache/CacheHelperTest.java

http://thoughtsite.googlecode.com/
Java | 100 lines | 71 code | 21 blank | 8 comment | 0 complexity | e2df868f65bb907d9a293bfa8f5a6396 MD5 | raw file
  1// Copyright 2009 Google Inc. All Rights Reserved.
  2
  3package com.google.ie.common.cache;
  4
  5import static org.junit.Assert.assertEquals;
  6import static org.junit.Assert.assertNotNull;
  7import static org.junit.Assert.fail;
  8
  9import com.google.appengine.tools.development.ApiProxyLocalImpl;
 10import com.google.apphosting.api.ApiProxy;
 11import com.google.ie.test.TestEnvironment;
 12
 13import org.junit.After;
 14import org.junit.Before;
 15import org.junit.Test;
 16
 17import java.io.File;
 18
 19/**
 20 * Test cases to test each cases of methods in CustomCacheManager class.
 21 * 
 22 * @author asirohi
 23 * 
 24 */
 25public class CacheHelperTest {
 26    private static final String TEST_NAMESPACE = "test";
 27
 28    @Before
 29    public void setUp() throws Exception {
 30        ApiProxy.setEnvironmentForCurrentThread(new TestEnvironment());
 31        ApiProxy.setDelegate(new ApiProxyLocalImpl(new File(".")) {
 32        });
 33    }
 34
 35    @After
 36    public void tearDown() throws Exception {
 37        // not strictly necessary to null these out but there's no harm either
 38        ApiProxy.setDelegate(null);
 39        ApiProxy.setEnvironmentForCurrentThread(null);
 40    }
 41
 42    @Test
 43    public void putObject() {
 44
 45        try {
 46            CacheHelper.putObject(TEST_NAMESPACE, "key1", "value1");
 47            CacheHelper.putObject(TEST_NAMESPACE, "key2", "value2");
 48
 49        } catch (IllegalArgumentException iae) {
 50            fail("Non serializable object: Should not occur");
 51        }
 52        assertEquals(true, CacheHelper.containsObject(TEST_NAMESPACE, "key1"));
 53    }
 54
 55    @Test
 56    public void putObjectWithExpiration() {
 57
 58        try {
 59            CacheHelper.putObject(TEST_NAMESPACE, "key1", "value1", 100);
 60        } catch (IllegalArgumentException iae) {
 61            fail("Non serializable object: Should not occur");
 62        }
 63        assertEquals(true, CacheHelper.containsObject(TEST_NAMESPACE, "key1"));
 64    }
 65
 66    @Test
 67    public void getObject() {
 68        CacheHelper.putObject(TEST_NAMESPACE, "key1", "value1");
 69
 70        assertNotNull("key1 should not be null", CacheHelper.getObject(TEST_NAMESPACE, "key1"));
 71        assertEquals("Wrong name value", "value1", CacheHelper.getObject(TEST_NAMESPACE, "key1"));
 72    }
 73
 74    @Test
 75    public void contains() {
 76        CacheHelper.putObject(TEST_NAMESPACE, "key1", "value1");
 77        assertEquals(true, CacheHelper.containsObject(TEST_NAMESPACE, "key1"));
 78    }
 79
 80    @Test
 81    public void delete() {
 82        CacheHelper.putObject(TEST_NAMESPACE, "key1", "value1");
 83        CacheHelper.deleteObject(TEST_NAMESPACE, "key1");
 84        assertEquals(false, CacheHelper.containsObject(TEST_NAMESPACE, "key1"));
 85    }
 86
 87    @Test
 88    public void clearAll() {
 89        CacheHelper.putObject(TEST_NAMESPACE, "key1", "value1");
 90        CacheHelper.putObject(TEST_NAMESPACE, "key2", "value2");
 91        CacheHelper.putObject(TEST_NAMESPACE, "key3", "value3");
 92
 93        CacheHelper.clearAllObjects();
 94
 95        assertEquals(false, CacheHelper.containsObject(TEST_NAMESPACE, "key1"));
 96        assertEquals(false, CacheHelper.containsObject(TEST_NAMESPACE, "key2"));
 97        assertEquals(false, CacheHelper.containsObject(TEST_NAMESPACE, "key3"));
 98    }
 99
100}