1 /**
2 * Copyright 2003-2006 Greg Luck
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.Element;
20 import net.sf.ehcache.MemoryStoreTester;
21 import net.sf.ehcache.AbstractCacheTest;
22
23 import java.io.IOException;
24
25 /**
26 * Test class for FifoMemoryStore
27 * <p>
28 *
29 * @author <a href="ssuravarapu@users.sourceforge.net">Surya Suravarapu</a>
30 * @author Greg Luck
31 *
32 * @version $Id: FifoMemoryStoreTest.java 44 2006-04-22 06:03:33Z gregluck $
33 */
34 public class FifoMemoryStoreTest extends MemoryStoreTester {
35
36 /**
37 * setup test
38 */
39 protected void setUp() throws Exception {
40 super.setUp();
41 createMemoryStore(MemoryStoreEvictionPolicy.FIFO);
42 }
43
44 /**
45 * Tests adding an entry.
46 */
47 public void testPut() throws Exception {
48 putTest();
49 }
50
51 /**
52 * Tests put by using the parameters specified in the config file
53 */
54 public void testPutFromConfig() throws Exception {
55 createMemoryStore(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache-policy-test.xml", "sampleFIFOCache2");
56 putTest();
57 }
58
59 /**
60 * Put test and check policy
61 * @throws IOException
62 */
63 protected void putTest() throws IOException {
64 Element element;
65
66
67 assertEquals(0, store.getSize());
68
69
70 element = new Element("key1", "value1");
71 store.put(element);
72
73
74 store.put(new Element("key2", "value2"));
75 assertEquals(2, store.getSize());
76
77
78 element = store.get("key1");
79 assertNotNull(element);
80
81 assertEquals("value1", element.getObjectValue());
82 assertEquals(2, store.getSize());
83 }
84
85 /**
86 * Tests removing the entries
87 */
88 public void testRemove() throws Exception {
89 removeTest();
90 }
91
92 /**
93 * Tests remove by using the parameters specified in the config file
94 */
95 public void testRemoveFromConfig() throws Exception {
96 createMemoryStore(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache-policy-test.xml", "sampleFIFOCache2");
97 removeTest();
98 }
99
100
101 /**
102 * Benchmark to test speed.
103 * v 1.38 DiskStore 7238
104 * v 1.42 DiskStore 1907
105 */
106 public void testBenchmarkPutGetSurya() throws Exception {
107 benchmarkPutGetSuryaTest(3000);
108 }
109
110
111 /**
112 * Remove test and check policy
113 * @throws IOException
114 */
115 protected void removeTest() throws IOException {
116 Element element;
117
118
119 assertEquals(0, store.getSize());
120
121
122 element = new Element("key1", "value1");
123 store.put(element);
124
125 element = new Element("key2", "value2");
126 store.put(element);
127
128 element = new Element("key3", "value3");
129 store.put(element);
130
131
132 assertEquals(3, store.getSize());
133
134
135 store.remove("key1");
136 element = ((FifoMemoryStore)store).getFirstElement();
137 assertEquals("value2", element.getObjectValue());
138 assertEquals(2, store.getSize());
139
140 store.remove("key2");
141 element = ((FifoMemoryStore)store).getFirstElement();
142 assertEquals("value3", element.getObjectValue());
143 assertEquals(1, store.getSize());
144
145 store.remove("key3");
146 element = ((FifoMemoryStore)store).getFirstElement();
147 assertNull(element);
148 assertEquals(0, store.getSize());
149 }
150
151 /**
152 * Test the policy
153 */
154 public void testFifoPolicy() throws Exception {
155 createMemoryStore(MemoryStoreEvictionPolicy.FIFO, 5);
156 fifoPolicyTest();
157 }
158
159 /**
160 * Test the ploicy by using the parameters specified in the config file
161 */
162 public void testFifoPolicyFromConfig() throws Exception {
163 createMemoryStore(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache-policy-test.xml", "sampleFIFOCache2");
164 fifoPolicyTest();
165 }
166
167 private void fifoPolicyTest() throws IOException {
168
169 assertEquals(0, store.getSize());
170
171
172 Element element = new Element("key1", "value1");
173 store.put(element);
174 assertEquals(1, store.getSize());
175
176 element = new Element("key2", "value2");
177 store.put(element);
178 assertEquals(2, store.getSize());
179
180 element = new Element("key3", "value3");
181 store.put(element);
182 assertEquals(3, store.getSize());
183
184 element = new Element("key4", "value4");
185 store.put(element);
186 assertEquals(4, store.getSize());
187
188 element = new Element("key5", "value5");
189 store.put(element);
190 assertEquals(5, store.getSize());
191
192
193
194 store.get("key1");
195 store.get("key1");
196 store.get("key3");
197 store.get("key3");
198 store.get("key3");
199 store.get("key4");
200
201
202 element = new Element("key6", "value6");
203 store.put(element);
204
205
206 assertEquals(5, store.getSize());
207
208
209 assertNull(store.get("key1"));
210
211
212 store.get("key5");
213 store.get("key5");
214
215
216 element = new Element("key7", "value7");
217 store.put(element);
218 assertEquals(5, store.getSize());
219 assertNull(store.get("key2"));
220 }
221 }