1 16 package cintoo.messages.context; 17 18 import api.cintoo.messages.bundle.BaseBundle; 19 import api.cintoo.messages.context.Context; 20 import api.cintoo.messages.context.ContextCache; 21 import cintoo.messages.bundle.BaseResourceBundle; 22 import org.testng.Assert; 23 import org.testng.annotations.Configuration; 24 import org.testng.annotations.Test; 25 26 import java.util.Locale ; 27 import java.util.ResourceBundle ; 28 29 public class TestContextCache { 30 private Context context; 31 private ContextCache cache; 32 private BaseBundle bundle; 33 private Locale locale; 34 35 @Configuration(beforeTestMethod = true) 36 protected void setUp() throws Exception { 37 locale = new Locale ("en", ""); 38 bundle = new BaseResourceBundle(locale); 39 40 cache = new DefaultContextCache(); 41 context = new Context() { 42 public boolean matches(Context context) { 43 return false; 44 } 45 46 public boolean equals(Object o) { 47 return true; 48 } 49 50 public int hashCode() { 51 return 1; 52 } 53 54 public int compareTo(Object o) { 55 return 0; 56 } 57 58 public String toString() { 59 return "1"; 60 } 61 }; 62 } 63 64 @Test 65 public void testIsCached() { 66 cache.addToCache(context, locale, bundle); 67 Assert.assertTrue(cache.isCached(context, locale), "Cache hit after add"); 68 } 69 70 @Test 71 public void testNotCached() { 72 Assert.assertTrue(!(cache.isCached(context, locale)), "Cache miss for context if not in cache"); 73 } 74 75 @Test 76 public void testNotCachedAfterClear() { 77 cache.addToCache(context, locale, bundle); 78 cache.clear(); 79 Assert.assertTrue(!(cache.isCached(context, locale)), "Cache miss for context after clear"); 80 } 81 82 @Test 83 public void testGetFromCache() { 84 cache.addToCache(context, locale, bundle); 85 ResourceBundle bundle = cache.getFromCache(context, locale); 86 Assert.assertEquals(bundle, bundle, "Get from cache returns correct value after add"); 87 } 88 89 @Test 90 public void testGetFromCacheWithMiss() { 91 Assert.assertTrue(cache.getFromCache(context, locale) == null, "Get from cache for miss returns null"); 92 } 93 } 94 | Popular Tags |