1   package org.apache.jcs.access;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.TestCase;
23  
24  import org.apache.jcs.access.exception.CacheException;
25  import org.apache.jcs.access.exception.ObjectExistsException;
26  import org.apache.jcs.engine.CompositeCacheAttributes;
27  import org.apache.jcs.engine.ElementAttributes;
28  import org.apache.jcs.engine.behavior.ICacheElement;
29  import org.apache.jcs.engine.behavior.ICompositeCacheAttributes;
30  import org.apache.jcs.engine.behavior.IElementAttributes;
31  
32  /***
33   * Tests the methods of the cache access class from which the class JCS extends.
34   *
35   * @author Aaron Smuts
36   *
37   */
38  public class CacheAccessUnitTest
39      extends TestCase
40  {
41  
42      /***
43       * Verify that we get an object exists exception if the item is in the
44       * cache.
45       *
46       */
47      public void testPutSafe()
48      {
49  
50          CacheAccess access = null;
51          try
52          {
53              access = CacheAccess.getAccess( "test" );
54  
55              assertNotNull( "We should have an access class", access );
56          }
57          catch ( CacheException e )
58          {
59              fail( "Shouldn't have received an error." + e.getMessage() );
60          }
61  
62          String key = "mykey";
63          String value = "myvalue";
64  
65          try
66          {
67              access.put( key, value );
68          }
69          catch ( CacheException e )
70          {
71              fail( "Should have been able to put " + e.getMessage() );
72          }
73          String returnedValue1 = (String) access.get( key );
74          assertEquals( "Wrong value returned.", value, returnedValue1 );
75  
76          try
77          {
78              access.putSafe( key, "someothervalue" );
79              fail( "We should have received an eception since this key is alredy in the cache." );
80          }
81          catch ( CacheException e )
82          {
83              // e.printStackTrace();
84              // expected
85              assertTrue( "Wrong type of exception.", e instanceof ObjectExistsException );
86              assertTrue( "Should have the key in the error message.", e.getMessage().indexOf( "[" + key + "]" ) != -1 );
87          }
88  
89          String returnedValue2 = (String) access.get( key );
90          assertEquals( "Wrong value returned.  Shoudl still be the original.", value, returnedValue2 );
91      }
92  
93      /***
94       * Try to put a null key and verify that we get an exception.
95       *
96       */
97      public void testPutNullKey()
98      {
99  
100         CacheAccess access = null;
101         try
102         {
103             access = CacheAccess.getAccess( "test" );
104 
105             assertNotNull( "We should have an access class", access );
106         }
107         catch ( CacheException e )
108         {
109             fail( "Shouldn't have received an error." + e.getMessage() );
110         }
111 
112         String key = null;
113         String value = "myvalue";
114 
115         try
116         {
117             access.put( key, value );
118             fail( "Should not have been able to put a null key." );
119         }
120         catch ( CacheException e )
121         {
122             // expected
123             assertTrue( "Should have the work null in the error message.", e.getMessage().indexOf( "null" ) != -1 );
124         }
125     }
126 
127     /***
128      * Try to put a null value and verify that we get an exception.
129      *
130      */
131     public void testPutNullValue()
132     {
133 
134         CacheAccess access = null;
135         try
136         {
137             access = CacheAccess.getAccess( "test" );
138 
139             assertNotNull( "We should have an access class", access );
140         }
141         catch ( CacheException e )
142         {
143             fail( "Shouldn't have received an error." + e.getMessage() );
144         }
145 
146         String key = "myKey";
147         String value = null;
148 
149         try
150         {
151             access.put( key, value );
152             fail( "Should not have been able to put a null object." );
153         }
154         catch ( CacheException e )
155         {
156             // expected
157             assertTrue( "Should have the work null in the error message.", e.getMessage().indexOf( "null" ) != -1 );
158         }
159     }
160 
161     /***
162      * Verify that elements that go in the region after this call takethe new
163      * attributes.
164      *
165      * @throws Exception
166      *
167      */
168     public void testSetDefaultElementAttributes()
169         throws Exception
170     {
171 
172         CacheAccess access = null;
173 
174         access = CacheAccess.getAccess( "test" );
175 
176         assertNotNull( "We should have an access class", access );
177 
178         long maxLife = 9876;
179         IElementAttributes attr = new ElementAttributes();
180         attr.setMaxLifeSeconds( maxLife );
181 
182         access.setDefaultElementAttributes( attr );
183 
184         assertEquals( "Wrong element attributes.", attr.getMaxLifeSeconds(), access.getDefaultElementAttributes()
185             .getMaxLifeSeconds() );
186 
187         String key = "mykey";
188         String value = "myvalue";
189 
190         access.put( key, value );
191 
192         ICacheElement element = access.getCacheElement( key );
193 
194         assertEquals( "Wrong max life.  Should have the new value.", maxLife, element.getElementAttributes()
195             .getMaxLifeSeconds() );
196     }
197 
198     /***
199      * Verify that we can get a region using the define region method.
200      *
201      * @throws Exception
202      *
203      */
204     public void testRegionDefiniton()
205         throws Exception
206     {
207         CacheAccess access = CacheAccess.defineRegion( "test" );
208         assertNotNull( "We should have an access class", access );
209     }
210 
211     /***
212      * Verify that we can get a region using the define region method with cache attributes.
213      *
214      * @throws Exception
215      *
216      */
217     public void testRegionDefinitonWithAttributes()
218         throws Exception
219     {
220         ICompositeCacheAttributes ca = new CompositeCacheAttributes();
221 
222         long maxIdleTime = 8765;
223         ca.setMaxMemoryIdleTimeSeconds( maxIdleTime );
224 
225         CacheAccess access = CacheAccess.defineRegion( "testRegionDefinitonWithAttributes", ca );
226         assertNotNull( "We should have an access class", access );
227 
228         ICompositeCacheAttributes ca2 = access.getCacheAttributes();
229         assertEquals( "Wrong idle time setting.", ca.getMaxMemoryIdleTimeSeconds(), ca2.getMaxMemoryIdleTimeSeconds() );
230     }
231 
232     /***
233      * Verify that we can get a region using the define region method with cache attributes and elemetn attributes.
234      *
235      * @throws Exception
236      *
237      */
238     public void testRegionDefinitonWithBothAttributes()
239         throws Exception
240     {
241         ICompositeCacheAttributes ca = new CompositeCacheAttributes();
242 
243         long maxIdleTime = 8765;
244         ca.setMaxMemoryIdleTimeSeconds( maxIdleTime );
245 
246         long maxLife = 9876;
247         IElementAttributes attr = new ElementAttributes();
248         attr.setMaxLifeSeconds( maxLife );
249 
250         CacheAccess access = CacheAccess.defineRegion( "testRegionDefinitonWithAttributes", ca, attr );
251         assertNotNull( "We should have an access class", access );
252 
253         ICompositeCacheAttributes ca2 = access.getCacheAttributes();
254         assertEquals( "Wrong idle time setting.", ca.getMaxMemoryIdleTimeSeconds(), ca2.getMaxMemoryIdleTimeSeconds() );
255     }
256 
257 }