View Javadoc

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  public class CacheTest extends TestCase {
32  
33      public static final String MAP_CACHE_LEVEL = "org.apache.chemistry.opencmis.client.bindings.cache.impl.MapCacheLevelImpl";
34      public static final String LRU_CACHE_LEVEL = "org.apache.chemistry.opencmis.client.bindings.cache.impl.LruCacheLevelImpl";
35  
36      public void testCache() {
37          Cache cache;
38  
39          cache = new CacheImpl();
40          cache.initialize(new String[] { MAP_CACHE_LEVEL, LRU_CACHE_LEVEL, MAP_CACHE_LEVEL, MAP_CACHE_LEVEL });
41  
42          String value1 = "value1";
43          String value2 = "value2";
44          String value3 = "value3";
45          Object valueObj;
46  
47          // put and get
48          cache.put(value1, "l1", "l2a", "l3", "l4");
49          cache.put(value2, "l1", "l2b", "l3", "l4");
50          cache.put(value3, "l1", "l2c", "l3", "l4");
51  
52          valueObj = cache.get("l1", "l2a", "l3", "l4");
53          assertTrue(valueObj instanceof String);
54          assertSame(value1, valueObj);
55  
56          valueObj = cache.get("l1", "l2b", "l3", "l4");
57          assertTrue(valueObj instanceof String);
58          assertSame(value2, valueObj);
59  
60          valueObj = cache.get("l1", "l2c", "l3", "l4");
61          assertTrue(valueObj instanceof String);
62          assertSame(value3, valueObj);
63  
64          // remove leaf
65          cache.remove("l1", "l2", "l3", "l4");
66          valueObj = cache.get("l1", "l2", "l3", "l4");
67          assertNull(valueObj);
68  
69          // put and get
70          cache.put(value1, "l1", "l2", "l3", "l4");
71          valueObj = cache.get("l1", "l2", "l3", "l4");
72          assertTrue(valueObj instanceof String);
73          assertSame(value1, valueObj);
74  
75          // remove branch
76          cache.remove("l1", "l2");
77          valueObj = cache.get("l1", "l2", "l3", "l4");
78          assertNull(valueObj);
79      }
80  
81      public void testCacheBadUsage() {
82          Cache cache;
83  
84          cache = new CacheImpl();
85          cache.initialize(new String[] { MAP_CACHE_LEVEL, LRU_CACHE_LEVEL, MAP_CACHE_LEVEL, MAP_CACHE_LEVEL });
86  
87          // insufficient number of keys
88          try {
89              cache.put("value", "l1", "l2", "l3");
90          } catch (IllegalArgumentException e) {
91          }
92  
93          // too many number of keys
94          try {
95              cache.put("value", "l1", "l2", "l3", "l4", "l5");
96          } catch (IllegalArgumentException e) {
97          }
98  
99          // no keys
100         assertNull(cache.get((String[]) null));
101     }
102 
103     public void testCacheConfig() {
104         Cache cache;
105 
106         // empty config
107         try {
108             cache = new CacheImpl();
109             cache.initialize(new String[] {});
110         } catch (IllegalArgumentException e) {
111         }
112 
113         // null config
114         try {
115             cache = new CacheImpl();
116             cache.initialize(null);
117         } catch (IllegalArgumentException e) {
118         }
119 
120         // unknown class
121         try {
122             cache = new CacheImpl();
123             cache.initialize(new String[] { "this.is.not.a.valid.class" });
124         } catch (IllegalArgumentException e) {
125         }
126 
127         // not a CacheLevel class
128         try {
129             cache = new CacheImpl();
130             cache.initialize(new String[] { "org.apache.chemistry.opencmis.client.provider.cache.CacheTest" });
131         } catch (IllegalArgumentException e) {
132         }
133     }
134 
135     public void testMapCache() {
136         Cache cache;
137 
138         cache = new CacheImpl();
139         cache.initialize(new String[] { MAP_CACHE_LEVEL + " " + MapCacheLevelImpl.CAPACITY + "=10,"
140                 + MapCacheLevelImpl.LOAD_FACTOR + "=0.5" });
141 
142         for (int i = 0; i < 100; i++) {
143             cache.put("value" + i, "key" + i);
144         }
145 
146         for (int i = 0; i < 100; i++) {
147             Object valueObj = cache.get("key" + i);
148             assertTrue(valueObj instanceof String);
149             assertEquals("value" + i, valueObj);
150         }
151     }
152 
153     public void testURLCache() {
154         Cache cache;
155 
156         cache = new CacheImpl();
157         cache.initialize(new String[] { LRU_CACHE_LEVEL + " " + LruCacheLevelImpl.MAX_ENTRIES + "=10" });
158 
159         for (int i = 0; i < 100; i++) {
160             cache.put("value" + i, "key" + i);
161         }
162 
163         for (int i = 0; i < 90; i++) {
164             Object valueObj = cache.get("key" + i);
165             assertNull(valueObj);
166         }
167 
168         for (int i = 90; i < 100; i++) {
169             Object valueObj = cache.get("key" + i);
170             assertTrue(valueObj instanceof String);
171             assertEquals("value" + i, valueObj);
172         }
173     }
174 
175     public void XtestFallback() {
176         Cache cache;
177 
178         cache = new CacheImpl();
179         cache.initialize(new String[] { MAP_CACHE_LEVEL + " " + MapCacheLevelImpl.CAPACITY + "=10,"
180                 + MapCacheLevelImpl.LOAD_FACTOR + "=0.5" });
181 
182         cache.put("value1", new String[] { null });
183         cache.put("value2", "key2");
184 
185         assertEquals("value1", cache.get(new String[] { null }));
186         assertEquals("value2", cache.get("key2"));
187         assertEquals("value1", cache.get("key3"));
188     }
189 
190     public void testContentTypeCache() {
191         ContentTypeCacheLevelImpl cl = new ContentTypeCacheLevelImpl();
192         cl.initialize(null);
193 
194         String type1 = "type1";
195 
196         cl.put(type1, "text/plain; param1=test; charset=UTF-8");
197 
198         assertEquals(type1, cl.get("text/plain; param1=test; charset=UTF-8"));
199         assertEquals(type1, cl.get("text/plain; param1=test; charset=utf-8"));
200         assertEquals(type1, cl.get("text/plain; charset=utf-8; param1=test"));
201         assertEquals(type1, cl.get("text/plain; charset=utf-8; param1=test;"));
202         assertEquals(type1, cl.get("text/plain;charset=utf-8;param1=test"));
203         assertEquals(type1, cl.get("text/plain;\tcharset=utf-8;     param1=test"));
204         assertEquals(type1, cl.get("text/plain; charset=\"utf-8\"; param1=test;"));
205 
206         assertNull(cl.get("text/plain; param1=blah; charset=UTF-8"));
207         assertNull(cl.get("text/plain; param1=test; charset=us-ascii"));
208     }
209 }