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