View Javadoc
1   package org.apache.commons.jcs3.utils.access;
2   
3   
4   
5   /*
6    * Licensed to the Apache Software Foundation (ASF) under one
7    * or more contributor license agreements.  See the NOTICE file
8    * distributed with this work for additional information
9    * regarding copyright ownership.  The ASF licenses this file
10   * to you under the Apache License, Version 2.0 (the
11   * "License"); you may not use this file except in compliance
12   * with the License.  You may obtain a copy of the License at
13   *
14   *   http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing,
17   * software distributed under the License is distributed on an
18   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19   * KIND, either express or implied.  See the License for the
20   * specific language governing permissions and limitations
21   * under the License.
22   */
23  
24  import junit.framework.TestCase;
25  
26  /**
27   * Test cases for the JCS worker.
28   */
29  public class JCSWorkerUnitTest
30      extends TestCase
31  {
32  
33      /**
34       * Test basic worker functionality.  This is a serial not a concurrent test.
35       * <p>
36       * Just verify that the worker will go to the cache before asking the helper.
37       *
38       * @throws Exception
39       *
40       */
41      public void testSimpleGet()
42          throws Exception
43      {
44          final JCSWorker<String, Long> cachingWorker = new JCSWorker<>( "example region" );
45  
46          // This is the helper.
47          final JCSWorkerHelper<Long> helper = new AbstractJCSWorkerHelper<Long>()
48          {
49              int timesCalled;
50  
51              @Override
52              public Long doWork()
53              {
54                  return Long.valueOf( ++timesCalled );
55              }
56          };
57  
58          final String key = "abc";
59  
60          final Long result = cachingWorker.getResult( key, helper );
61          assertEquals( "Called the wrong number of times", Long.valueOf( 1 ), result );
62  
63          // should get it from the cache.
64          final Long result2 = cachingWorker.getResult( key, helper );
65          assertEquals( "Called the wrong number of times", Long.valueOf( 1 ), result2 );
66      }
67  
68  }