View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.jcs.jcache.extras.loader;
20  
21  import org.apache.commons.jcs.jcache.extras.InternalCacheRule;
22  import org.junit.Rule;
23  import org.junit.Test;
24  
25  import javax.cache.Cache;
26  import javax.cache.configuration.Configuration;
27  import javax.cache.configuration.MutableConfiguration;
28  import javax.cache.integration.CacheLoaderException;
29  import java.util.concurrent.atomic.AtomicInteger;
30  
31  import static org.junit.Assert.assertEquals;
32  
33  public class CompositeCacheLoaderTest
34  {
35      @Rule
36      public final InternalCacheRule rule = new InternalCacheRule(this);
37  
38      private final AtomicInteger count = new AtomicInteger(0);
39  
40      private final CacheLoaderAdapter<String, String> loader1 = new CacheLoaderAdapter<String, String>()
41      {
42          @Override
43          public String load(String key) throws CacheLoaderException
44          {
45              count.incrementAndGet();
46              return null;
47          }
48      };
49      private final CacheLoaderAdapter<String, String> loader2 = new CacheLoaderAdapter<String, String>()
50      {
51          @Override
52          public String load(String key) throws CacheLoaderException
53          {
54              count.incrementAndGet();
55              return null;
56          }
57      };
58      private final Configuration<?, ?> config = new MutableConfiguration<String, String>()
59              .setStoreByValue(false)
60              .setReadThrough(true)
61              .setCacheLoaderFactory(new CompositeCacheLoader<String, String>(loader1, loader2));
62      private Cache<String, String> cache;
63  
64      @Test
65      public void checkComposite()
66      {
67          cache.get("foo");
68          assertEquals(2, count.get());
69      }
70  }