View Javadoc
1   package org.apache.commons.jcs3.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 java.util.List;
23  
24  import org.apache.commons.jcs3.JCS;
25  import org.apache.commons.jcs3.access.CacheAccess;
26  
27  
28  /*
29   * Licensed to the Apache Software Foundation (ASF) under one
30   * or more contributor license agreements.  See the NOTICE file
31   * distributed with this work for additional information
32   * regarding copyright ownership.  The ASF licenses this file
33   * to you under the Apache License, Version 2.0 (the
34   * "License"); you may not use this file except in compliance
35   * with the License.  You may obtain a copy of the License at
36   *
37   *   http://www.apache.org/licenses/LICENSE-2.0
38   *
39   * Unless required by applicable law or agreed to in writing,
40   * software distributed under the License is distributed on an
41   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
42   * KIND, either express or implied.  See the License for the
43   * specific language governing permissions and limitations
44   * under the License.
45   */
46  
47  import junit.framework.TestCase;
48  
49  /**
50   * Test the admin bean that is used by the JCSAdmin.jsp
51   */
52  public class AdminBeanUnitTest
53      extends TestCase
54  {
55  
56      /**
57       * Create a test region and then verify that we get it from the list.
58       *
59       * @throws Exception
60       *
61       */
62      public void testGetRegionInfo()
63          throws Exception
64      {
65          final String regionName = "myRegion";
66          final CacheAccess<String, String> cache = JCS.getInstance( regionName );
67  
68          cache.put( "key", "value" );
69  
70          final JCSAdminBean admin = new JCSAdminBean();
71  
72          final List<CacheRegionInfo> regions = admin.buildCacheInfo();
73  
74          boolean foundRegion = false;
75  
76          for (final CacheRegionInfo info : regions)
77          {
78  
79              if ( info.getCacheName().equals( regionName ) )
80              {
81                  foundRegion = true;
82  
83                  assertTrue( "Byte count should be greater than 5.", info.getByteCount() > 5 );
84  
85                  assertNotNull( "Should have stats.", info.getCacheStatistics() );
86              }
87          }
88  
89          assertTrue( "Should have found the region we just created.", foundRegion );
90      }
91  
92      /**
93       * Put a value in a region and verify that it shows up.
94       *
95       * @throws Exception
96       */
97      public void testGetElementForRegionInfo()
98          throws Exception
99      {
100         final String regionName = "myRegion";
101         final CacheAccess<String, String> cache = JCS.getInstance( regionName );
102 
103         // clear the region
104         cache.clear();
105 
106         final String key = "myKey";
107         cache.put( key, "value" );
108 
109         final JCSAdminBean admin = new JCSAdminBean();
110 
111         final List<CacheElementInfo> elements = admin.buildElementInfo( regionName );
112         assertEquals( "Wrong number of elements in the region.", 1, elements.size() );
113 
114         final CacheElementInfo elementInfo = elements.get(0);
115         assertEquals( "Wrong key." + elementInfo, key, elementInfo.getKey() );
116     }
117 
118     /**
119      * Remove an item via the remove method.
120      *
121      * @throws Exception
122      */
123     public void testRemove()
124         throws Exception
125     {
126         final JCSAdminBean admin = new JCSAdminBean();
127 
128         final String regionName = "myRegion";
129         final CacheAccess<String, String> cache = JCS.getInstance( regionName );
130 
131         // clear the region
132         cache.clear();
133         admin.clearRegion( regionName );
134 
135         final String key = "myKey";
136         cache.put( key, "value" );
137 
138         final List<CacheElementInfo> elements = admin.buildElementInfo( regionName );
139         assertEquals( "Wrong number of elements in the region.", 1, elements.size() );
140 
141         final CacheElementInfo elementInfo = elements.get(0);
142         assertEquals( "Wrong key.", key, elementInfo.getKey() );
143 
144         admin.removeItem( regionName, key );
145 
146         final List<CacheElementInfo> elements2 = admin.buildElementInfo( regionName );
147         assertEquals( "Wrong number of elements in the region after remove.", 0, elements2.size() );
148     }
149 
150     /**
151      * Add an item to a region. Call clear all and verify that it doesn't exist.
152      *
153      * @throws Exception
154      */
155     public void testClearAll()
156         throws Exception
157     {
158         final JCSAdminBean admin = new JCSAdminBean();
159 
160         final String regionName = "myRegion";
161         final CacheAccess<String, String> cache = JCS.getInstance( regionName );
162 
163         final String key = "myKey";
164         cache.put( key, "value" );
165 
166         admin.clearAllRegions();
167 
168         final List<CacheElementInfo> elements2 = admin.buildElementInfo( regionName );
169         assertEquals( "Wrong number of elements in the region after remove.", 0, elements2.size() );
170     }
171 }