View Javadoc
1   package org.apache.commons.jcs.admin;
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  import org.apache.commons.jcs.JCS;
24  import org.apache.commons.jcs.access.CacheAccess;
25  
26  /**
27   * Test the admin bean that is used by the JCSAdmin.jsp
28   *
29   * @author Aaron Smuts
30   *
31   */
32  public class AdminBeanUnitTest
33      extends TestCase
34  {
35  
36      /**
37       * Create a test region and then verify that we get it from the list.
38       *
39       * @throws Exception
40       *
41       */
42      public void testGetRegionInfo()
43          throws Exception
44      {
45          String regionName = "myRegion";
46          CacheAccess<String, String> cache = JCS.getInstance( regionName );
47  
48          cache.put( "key", "value" );
49  
50          JCSAdminBean admin = new JCSAdminBean();
51  
52          CacheRegionInfo[] regions = admin.buildCacheInfo();
53  
54          boolean foundRegion = false;
55  
56          for (CacheRegionInfo info : regions)
57          {
58  
59              if ( info.getCacheName().equals( regionName ) )
60              {
61                  foundRegion = true;
62  
63                  assertTrue( "Byte count should be greater than 5.", info.getByteCount() > 5 );
64  
65                  assertNotNull( "Should have stats.", info.getCacheStatistics() );
66              }
67          }
68  
69          assertTrue( "Should have found the region we just created.", foundRegion );
70      }
71  
72      /**
73       * Put a value in a region and verify that it shows up.
74       *
75       * @throws Exception
76       */
77      public void testGetElementForRegionInfo()
78          throws Exception
79      {
80          String regionName = "myRegion";
81          CacheAccess<String, String> cache = JCS.getInstance( regionName );
82  
83          // clear the region
84          cache.clear();
85  
86          String key = "myKey";
87          cache.put( key, "value" );
88  
89          JCSAdminBean admin = new JCSAdminBean();
90  
91          CacheElementInfo[] elements = admin.buildElementInfo( regionName );
92  
93          assertEquals( "Wrong number of elements in the region.", 1, elements.length );
94  
95          CacheElementInfo elementInfo = elements[0];
96          assertEquals( "Wrong key." + elementInfo, key, elementInfo.getKey() );
97      }
98  
99      /**
100      * Remove an item via the remove method.
101      *
102      * @throws Exception
103      */
104     public void testRemove()
105         throws Exception
106     {
107         JCSAdminBean admin = new JCSAdminBean();
108 
109         String regionName = "myRegion";
110         CacheAccess<String, String> cache = JCS.getInstance( regionName );
111 
112         // clear the region
113         cache.clear();
114         admin.clearRegion( regionName );
115 
116         String key = "myKey";
117         cache.put( key, "value" );
118 
119         CacheElementInfo[] elements = admin.buildElementInfo( regionName );
120 
121         assertEquals( "Wrong number of elements in the region.", 1, elements.length );
122 
123         CacheElementInfo elementInfo = elements[0];
124 
125         assertEquals( "Wrong key.", key, elementInfo.getKey() );
126 
127         admin.removeItem( regionName, key );
128 
129         CacheElementInfo[] elements2 = admin.buildElementInfo( regionName );
130         assertEquals( "Wrong number of elements in the region after remove.", 0, elements2.length );
131     }
132 
133     /**
134      * Add an item to a region. Call clear all and verify that it doesn't exist.
135      *
136      * @throws Exception
137      */
138     public void testClearAll()
139         throws Exception
140     {
141         JCSAdminBean admin = new JCSAdminBean();
142 
143         String regionName = "myRegion";
144         CacheAccess<String, String> cache = JCS.getInstance( regionName );
145 
146         String key = "myKey";
147         cache.put( key, "value" );
148 
149         admin.clearAllRegions();
150 
151         CacheElementInfo[] elements2 = admin.buildElementInfo( regionName );
152         assertEquals( "Wrong number of elements in the region after remove.", 0, elements2.length );
153     }
154 }