KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cintoo > messages > context > TestContextCache


1 /*
2  * Copyright 2006 cintoo, Berlin, Germany
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
27 import java.util.ResourceBundle JavaDoc;
28
29 public class TestContextCache {
30   private Context context;
31   private ContextCache cache;
32   private BaseBundle bundle;
33   private Locale JavaDoc locale;
34
35   @Configuration(beforeTestMethod = true)
36   protected void setUp() throws Exception JavaDoc {
37     locale = new Locale JavaDoc("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 JavaDoc o) {
47         return true;
48       }
49
50       public int hashCode() {
51         return 1;
52       }
53
54       public int compareTo(Object JavaDoc o) {
55         return 0;
56       }
57
58       public String JavaDoc 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