1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
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
17 package net.sf.ehcache.store;
18
19 import net.sf.ehcache.AbstractCacheTest;
20 import net.sf.ehcache.Element;
21 import net.sf.ehcache.MemoryStoreTester;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26
27 import org.junit.Before;
28 import org.junit.Test;
29
30 import java.io.IOException;
31
32 /***
33 * Test class for FifoMemoryStore
34 * <p/>
35 *
36 * @author <a href="ssuravarapu@users.sourceforge.net">Surya Suravarapu</a>
37 * @author Greg Luck
38 * @version $Id: FifoMemoryStoreTest.html 13146 2011-08-01 17:12:39Z oletizi $
39 */
40 public class FifoMemoryStoreTest extends MemoryStoreTester {
41
42 /***
43 * setup test
44 */
45 @Override
46 @Before
47 public void setUp() throws Exception {
48 super.setUp();
49 createMemoryOnlyStore(MemoryStoreEvictionPolicy.FIFO);
50 }
51
52 /***
53 * Tests adding an entry.
54 */
55 @Override
56 @Test
57 public void testPut() throws Exception {
58 putTest();
59 }
60
61 /***
62 * Tests put by using the parameters specified in the config file
63 */
64 @Test
65 public void testPutFromConfig() throws Exception {
66 createMemoryStore(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache-policy-test.xml", "sampleFIFOCache2");
67 putTest();
68 }
69
70 /***
71 * Put test and check policy
72 *
73 * @throws IOException
74 */
75 @Override
76 protected void putTest() throws IOException {
77 Element element;
78
79
80 assertEquals(0, store.getSize());
81
82
83 element = new Element("key1", "value1");
84 store.put(element);
85
86
87 store.put(new Element("key2", "value2"));
88 assertEquals(2, store.getSize());
89
90
91 element = store.get("key1");
92 assertNotNull(element);
93
94 assertEquals("value1", element.getObjectValue());
95 assertEquals(2, store.getSize());
96
97
98 }
99
100 /***
101 * Can we deal with NonSerializable objects?
102 */
103 @Test
104 public void testNonSerializable() {
105 /***
106 * Non-serializable test class
107 */
108 class NonSerializable {
109
110 }
111 NonSerializable key = new NonSerializable();
112 store.put(new Element(key, new NonSerializable()));
113 store.get(key);
114 }
115
116 /***
117 * Tests removing the entries
118 */
119 @Override
120 @Test
121 public void testRemove() throws Exception {
122 removeTest();
123 }
124
125 /***
126 * Tests remove by using the parameters specified in the config file
127 */
128 @Test
129 public void testRemoveFromConfig() throws Exception {
130 createMemoryStore(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache-policy-test.xml", "sampleFIFOCache2");
131 removeTest();
132 }
133
134
135 /***
136 * Remove test and check policy
137 *
138 * @throws IOException
139 */
140 @Override
141 protected void removeTest() throws IOException {
142 Element element;
143
144
145 assertEquals(0, store.getSize());
146
147
148 element = new Element("key1", "value1");
149 store.put(element);
150
151 element = new Element("key2", "value2");
152 store.put(element);
153
154 element = new Element("key3", "value3");
155 store.put(element);
156
157
158 assertEquals(3, store.getSize());
159
160 store.remove("key1");
161 assertEquals(2, store.getSize());
162
163 store.remove("key2");
164 assertEquals(1, store.getSize());
165
166 store.remove("key3");
167 assertEquals(0, store.getSize());
168 }
169
170 /***
171 * Test the policy
172 */
173 @Test
174 public void testFifoPolicy() throws Exception {
175 createMemoryOnlyStore(MemoryStoreEvictionPolicy.FIFO, 5);
176 fifoPolicyTest();
177 }
178
179 /***
180 * Test the ploicy by using the parameters specified in the config file
181 */
182 @Test
183 public void testFifoPolicyFromConfig() throws Exception {
184 createMemoryStore(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache-policy-test.xml", "sampleFIFOCache2");
185 fifoPolicyTest();
186 }
187
188 private void fifoPolicyTest() throws IOException, InterruptedException {
189
190 assertEquals(0, cache.getSize());
191
192
193 Element element = new Element("key1", "value1");
194 cache.put(element);
195 Thread.sleep(1015);
196 assertEquals(1, store.getSize());
197
198 element = new Element("key2", "value2");
199 cache.put(element);
200 Thread.sleep(1015);
201 assertEquals(2, store.getSize());
202
203 element = new Element("key3", "value3");
204 cache.put(element);
205 Thread.sleep(1015);
206 assertEquals(3, store.getSize());
207
208 element = new Element("key4", "value4");
209 cache.put(element);
210 Thread.sleep(1015);
211 assertEquals(4, store.getSize());
212
213 element = new Element("key5", "value5");
214 cache.put(element);
215 Thread.sleep(1015);
216 assertEquals(5, store.getSize());
217
218
219
220 cache.get("key1");
221 cache.get("key1");
222 cache.get("key3");
223 cache.get("key3");
224 cache.get("key3");
225 cache.get("key4");
226
227
228 element = new Element("key6", "value6");
229 cache.put(element);
230 Thread.sleep(1015);
231
232
233 assertEquals(5, store.getSize());
234
235
236
237 assertNull(store.get("key1"));
238
239
240 cache.get("key5");
241 cache.get("key5");
242 Thread.sleep(1015);
243
244
245 element = new Element("key7", "value7");
246 cache.put(element);
247 assertEquals(5, store.getSize());
248 assertNull(store.get("key2"));
249 }
250 }