View Javadoc
1   package org.apache.commons.jcs.utils.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  /**
25   * Test cases for the JCS worker.
26   *
27   * @author Aaron Smuts
28   *
29   */
30  public class JCSWorkerUnitTest
31      extends TestCase
32  {
33  
34      /**
35       * Test basic worker functionality.  This is a serial not a concurrent test.
36       * <p>
37       * Just verify that the worker will go to the cache before asking the helper.
38       *
39       * @throws Exception
40       *
41       */
42      public void testSimpleGet()
43          throws Exception
44      {
45          JCSWorker<String, Long> cachingWorker = new JCSWorker<String, Long>( "example region" );
46  
47          // This is the helper.
48          JCSWorkerHelper<Long> helper = new AbstractJCSWorkerHelper<Long>()
49          {
50              int timesCalled = 0;
51  
52              @Override
53              public Long doWork()
54              {
55                  return Long.valueOf( ++timesCalled );
56              }
57          };
58  
59          String key = "abc";
60  
61          Long result = cachingWorker.getResult( key, helper );
62          assertEquals( "Called the wrong number of times", Long.valueOf( 1 ), result );
63  
64          // should get it from the cache.
65          Long result2 = cachingWorker.getResult( key, helper );
66          assertEquals( "Called the wrong number of times", Long.valueOf( 1 ), result2 );
67      }
68  
69  }