This project has retired. For details please refer to its
Attic page.
CacheTest xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.chemistry.opencmis.client.runtime;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.ObjectInputStream;
24 import java.io.ObjectOutputStream;
25 import java.util.HashMap;
26 import java.util.Map;
27
28
29 import org.apache.chemistry.opencmis.client.api.CmisObject;
30 import org.apache.chemistry.opencmis.client.runtime.cache.Cache;
31 import org.apache.chemistry.opencmis.client.runtime.cache.CacheImpl;
32 import org.apache.chemistry.opencmis.commons.SessionParameter;
33 import org.junit.Before;
34 import org.junit.Test;
35
36 import static junit.framework.Assert.*;
37
38 public class CacheTest {
39
40 @Before
41 public void setup() {
42 }
43
44 @Test
45 public void cacheSingleObjectTest() {
46 Cache cache = createCache(100, 3600 * 1000);
47
48 String id = "1";
49
50 String cacheKey = "key";
51
52
53 CmisObject obj1 = createCmisObject(id);
54 cache.put(obj1, cacheKey);
55
56
57 assertTrue(cache.containsId(id, cacheKey));
58
59
60 CmisObject obj2 = cache.getById(id, cacheKey);
61 assertEquals(obj1, obj2);
62
63
64 cache.clear();
65
66
67 assertFalse(cache.containsId(id, cacheKey));
68
69
70 CmisObject obj4 = cache.getById(id, cacheKey);
71 assertNull(obj4);
72 }
73
74 @Test
75 public void cacheSizeTest() {
76 int cacheSize = 50000;
77 Cache cache = createCache(cacheSize, 3600 * 1000);
78 assertEquals(cacheSize, cache.getCacheSize());
79 }
80
81 @Test
82 public void lruTest() {
83 int cacheSize = 3;
84 Cache cache = createCache(cacheSize, 3600 * 1000);
85
86 String cacheKey = "key";
87
88 for (int i = 0; i < cacheSize + 1; i++) {
89 CmisObject obj = createCmisObject("id" + i);
90 cache.put(obj, cacheKey);
91 }
92
93 assertNull(cache.getById("id0", cacheKey));
94 assertNotNull(cache.getById("id1", cacheKey));
95 assertNotNull(cache.getById("id2", cacheKey));
96 assertNotNull(cache.getById("id3", cacheKey));
97 }
98
99 @SuppressWarnings("static-access")
100 @Test
101 public void ttlTest() throws InterruptedException {
102 Cache cache = createCache(10, 500);
103
104 String cacheKey = "key";
105 String id = "id";
106
107 CmisObject obj = this.createCmisObject(id);
108 cache.put(obj, cacheKey);
109
110 assertNotNull(cache.getById(id, cacheKey));
111
112 Thread.currentThread().sleep(750);
113
114 assertNull(cache.getById(id, cacheKey));
115 }
116
117 @Test
118 public void serializationTest() throws Exception {
119 int cacheSize = 10;
120 Cache cache = createCache(cacheSize, 3600 * 1000);
121
122 String cacheKey = "key";
123
124 for (int i = 0; i < cacheSize; i++) {
125 CmisObject obj = createCmisObject("id" + i);
126 cache.put(obj, cacheKey);
127 }
128
129 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
130 ObjectOutputStream out = new ObjectOutputStream(buffer);
131 out.writeObject(cache);
132 out.close();
133
134 ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
135 Cache cache2 = (Cache) in.readObject();
136 in.close();
137
138 for (int k = 0; k < cacheSize; k++) {
139 CmisObject o1 = cache.getById("id" + k, cacheKey);
140 CmisObject o2 = cache2.getById("id" + k, cacheKey);
141 assertEquals(o1.getId(), o2.getId());
142 }
143 }
144
145
146
147
148
149
150
151 private static CmisObject createCmisObject(final String id) {
152 return new CmisObjectMock(id);
153 }
154
155 private static Cache createCache(int cacheSize, int ttl) {
156 Cache cache = new CacheImpl();
157
158 Map<String, String> parameters = new HashMap<String, String>();
159 parameters.put(SessionParameter.CACHE_SIZE_OBJECTS, "" + cacheSize);
160 parameters.put(SessionParameter.CACHE_TTL_OBJECTS, "" + ttl);
161
162 cache.initialize(null, parameters);
163
164 return cache;
165 }
166 }