This project has retired. For details please refer to its Attic page.
CacheTest xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.client.bindings.cache;
20  
21  import junit.framework.TestCase;
22  
23  import org.apache.chemistry.opencmis.client.bindings.cache.impl.CacheImpl;
24  import org.apache.chemistry.opencmis.client.bindings.cache.impl.ContentTypeCacheLevelImpl;
25  import org.apache.chemistry.opencmis.client.bindings.cache.impl.LruCacheLevelImpl;
26  import org.apache.chemistry.opencmis.client.bindings.cache.impl.MapCacheLevelImpl;
27  
28  /**
29   * Tests the cache implementation.
30   * 
31   * @author <a href="mailto:fmueller@opentext.com">Florian M&uuml;ller</a>
32   * 
33   */
34  public class CacheTest extends TestCase {
35  
36      public static final String MAP_CACHE_LEVEL = "org.apache.chemistry.opencmis.client.bindings.cache.impl.MapCacheLevelImpl";
37      public static final String LRU_CACHE_LEVEL = "org.apache.chemistry.opencmis.client.bindings.cache.impl.LruCacheLevelImpl";
38  
39      public void testCache() {
40          Cache cache;
41  
42          cache = new CacheImpl();
43          cache.initialize(new String[] { MAP_CACHE_LEVEL, LRU_CACHE_LEVEL, MAP_CACHE_LEVEL, MAP_CACHE_LEVEL });
44  
45          String value1 = "value1";
46          String value2 = "value2";
47          String value3 = "value3";
48          Object valueObj;
49  
50          // put and get
51          cache.put(value1, "l1", "l2a", "l3", "l4");
52          cache.put(value2, "l1", "l2b", "l3", "l4");
53          cache.put(value3, "l1", "l2c", "l3", "l4");
54  
55          valueObj = cache.get("l1", "l2a", "l3", "l4");
56          assertTrue(valueObj instanceof String);
57          assertSame(value1, valueObj);
58  
59          valueObj = cache.get("l1", "l2b", "l3", "l4");
60          assertTrue(valueObj instanceof String);
61          assertSame(value2, valueObj);
62  
63          valueObj = cache.get("l1", "l2c", "l3", "l4");
64          assertTrue(valueObj instanceof String);
65          assertSame(value3, valueObj);
66  
67          // remove leaf
68          cache.remove("l1", "l2", "l3", "l4");
69          valueObj = cache.get("l1", "l2", "l3", "l4");
70          assertNull(valueObj);
71  
72          // put and get
73          cache.put(value1, "l1", "l2", "l3", "l4");
74          valueObj = cache.get("l1", "l2", "l3", "l4");
75          assertTrue(valueObj instanceof String);
76          assertSame(value1, valueObj);
77  
78          // remove branch
79          cache.remove("l1", "l2");
80          valueObj = cache.get("l1", "l2", "l3", "l4");
81          assertNull(valueObj);
82      }
83  
84      public void testCacheBadUsage() {
85          Cache cache;
86  
87          cache = new CacheImpl();
88          cache.initialize(new String[] { MAP_CACHE_LEVEL, LRU_CACHE_LEVEL, MAP_CACHE_LEVEL, MAP_CACHE_LEVEL });
89  
90          // insufficient number of keys
91          try {
92              cache.put("value", "l1", "l2", "l3");
93          } catch (IllegalArgumentException e) {
94          }
95  
96          // too many number of keys
97          try {
98              cache.put("value", "l1", "l2", "l3", "l4", "l5");
99          } catch (IllegalArgumentException e) {
100         }
101 
102         // no keys
103         assertNull(cache.get((String[]) null));
104     }
105 
106     public void testCacheConfig() {
107         Cache cache;
108 
109         // empty config
110         try {
111             cache = new CacheImpl();
112             cache.initialize(new String[] {});
113         } catch (IllegalArgumentException e) {
114         }
115 
116         // null config
117         try {
118             cache = new CacheImpl();
119             cache.initialize(null);
120         } catch (IllegalArgumentException e) {
121         }
122 
123         // unknown class
124         try {
125             cache = new CacheImpl();
126             cache.initialize(new String[] { "this.is.not.a.valid.class" });
127         } catch (IllegalArgumentException e) {
128         }
129 
130         // not a CacheLevel class
131         try {
132             cache = new CacheImpl();
133             cache.initialize(new String[] { "org.apache.chemistry.opencmis.client.provider.cache.CacheTest" });
134         } catch (IllegalArgumentException e) {
135         }
136     }
137 
138     public void testMapCache() {
139         Cache cache;
140 
141         cache = new CacheImpl();
142         cache.initialize(new String[] { MAP_CACHE_LEVEL + " " + MapCacheLevelImpl.CAPACITY + "=10,"
143                 + MapCacheLevelImpl.LOAD_FACTOR + "=0.5" });
144 
145         for (int i = 0; i < 100; i++) {
146             cache.put("value" + i, "key" + i);
147         }
148 
149         for (int i = 0; i < 100; i++) {
150             Object valueObj = cache.get("key" + i);
151             assertTrue(valueObj instanceof String);
152             assertEquals("value" + i, valueObj);
153         }
154     }
155 
156     public void testURLCache() {
157         Cache cache;
158 
159         cache = new CacheImpl();
160         cache.initialize(new String[] { LRU_CACHE_LEVEL + " " + LruCacheLevelImpl.MAX_ENTRIES + "=10" });
161 
162         for (int i = 0; i < 100; i++) {
163             cache.put("value" + i, "key" + i);
164         }
165 
166         for (int i = 0; i < 90; i++) {
167             Object valueObj = cache.get("key" + i);
168             assertNull(valueObj);
169         }
170 
171         for (int i = 90; i < 100; i++) {
172             Object valueObj = cache.get("key" + i);
173             assertTrue(valueObj instanceof String);
174             assertEquals("value" + i, valueObj);
175         }
176     }
177 
178     public void XtestFallback() {
179         Cache cache;
180 
181         cache = new CacheImpl();
182         cache.initialize(new String[] { MAP_CACHE_LEVEL + " " + MapCacheLevelImpl.CAPACITY + "=10,"
183                 + MapCacheLevelImpl.LOAD_FACTOR + "=0.5" });
184 
185         cache.put("value1", new String[] { null });
186         cache.put("value2", "key2");
187 
188         assertEquals("value1", cache.get(new String[] { null }));
189         assertEquals("value2", cache.get("key2"));
190         assertEquals("value1", cache.get("key3"));
191     }
192 
193     public void testContentTypeCache() {
194         ContentTypeCacheLevelImpl cl = new ContentTypeCacheLevelImpl();
195         cl.initialize(null);
196 
197         String type1 = "type1";
198 
199         cl.put(type1, "text/plain; param1=test; charset=UTF-8");
200 
201         assertEquals(type1, cl.get("text/plain; param1=test; charset=UTF-8"));
202         assertEquals(type1, cl.get("text/plain; param1=test; charset=utf-8"));
203         assertEquals(type1, cl.get("text/plain; charset=utf-8; param1=test"));
204         assertEquals(type1, cl.get("text/plain; charset=utf-8; param1=test;"));
205         assertEquals(type1, cl.get("text/plain;charset=utf-8;param1=test"));
206         assertEquals(type1, cl.get("text/plain;\tcharset=utf-8;     param1=test"));
207         assertEquals(type1, cl.get("text/plain; charset=\"utf-8\"; param1=test;"));
208 
209         assertNull(cl.get("text/plain; param1=blah; charset=UTF-8"));
210         assertNull(cl.get("text/plain; param1=test; charset=us-ascii"));
211     }
212 }