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.writer;
18
19 import net.sf.ehcache.CacheEntry;
20 import net.sf.ehcache.Element;
21 import net.sf.ehcache.writer.writebehind.CastingOperationConverter;
22 import net.sf.ehcache.writer.writebehind.CoalesceKeysFilter;
23 import net.sf.ehcache.writer.writebehind.operations.DeleteOperation;
24 import net.sf.ehcache.writer.writebehind.operations.KeyBasedOperation;
25 import net.sf.ehcache.writer.writebehind.operations.WriteOperation;
26 import org.junit.Test;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.assertTrue;
33
34 /***
35 * Tests for a key-based operations coalescing
36 *
37 * @author Geert Bevin
38 * @version $Id: CoalesceKeysFilterTest.java 2168 2010-04-07 17:54:15Z asingh $
39 */
40 public class CoalesceKeysFilterTest {
41
42 @Test
43 public void testFilter() {
44 List<KeyBasedOperation> operations = new ArrayList<KeyBasedOperation>();
45 operations.add(new WriteOperation(new Element("key1", "value1"), 10));
46 operations.add(new WriteOperation(new Element("key2", "value2"), 10));
47 operations.add(new WriteOperation(new Element("key1", "value3"), 30));
48 operations.add(new WriteOperation(new Element("key1", "value4"), 20));
49 operations.add(new DeleteOperation(new CacheEntry("key3", new Element("key3", "value5")), 30));
50 operations.add(new WriteOperation(new Element("key4", "value6"), 40));
51 operations.add(new DeleteOperation(new CacheEntry("key2", new Element("key2", "value7")), 20));
52 operations.add(new DeleteOperation(new CacheEntry("key4", new Element("key4", "value8")), 30));
53 operations.add(new WriteOperation(new Element("key4", "value9"), 20));
54 operations.add(new WriteOperation(new Element("key5", "value10"), 50));
55
56 new CoalesceKeysFilter().filter(operations, CastingOperationConverter.getInstance());
57
58 assertEquals(5, operations.size());
59 assertEquals("key1", operations.get(0).getKey());
60 assertEquals("value3", ((WriteOperation)operations.get(0)).getElement().getValue());
61
62 assertTrue(operations.get(1) instanceof DeleteOperation);
63 assertEquals("key3", operations.get(1).getKey());
64
65 assertEquals("key4", operations.get(2).getKey());
66 assertEquals("value6", ((WriteOperation)operations.get(2)).getElement().getValue());
67
68 assertTrue(operations.get(3) instanceof DeleteOperation);
69 assertEquals("key2", operations.get(3).getKey());
70
71 assertEquals("key5", operations.get(4).getKey());
72 assertEquals("value10", ((WriteOperation)operations.get(4)).getElement().getValue());
73 }
74 }