View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.impl.client.cache.ehcache;
28  
29  import net.sf.ehcache.CacheManager;
30  import net.sf.ehcache.config.CacheConfiguration;
31  import net.sf.ehcache.config.Configuration;
32  import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
33  
34  import org.apache.http.client.cache.HttpCacheStorage;
35  import org.apache.http.impl.client.cache.CacheConfig;
36  import org.apache.http.impl.client.cache.CachingExec;
37  import org.apache.http.impl.client.cache.HeapResourceFactory;
38  import org.apache.http.impl.client.cache.TestProtocolRequirements;
39  import org.apache.http.impl.execchain.ClientExecChain;
40  import org.easymock.EasyMock;
41  import org.junit.After;
42  import org.junit.AfterClass;
43  import org.junit.Before;
44  import org.junit.BeforeClass;
45  
46  public class TestEhcacheProtocolRequirements extends TestProtocolRequirements{
47  
48      protected final String TEST_EHCACHE_NAME = "TestEhcacheProtocolRequirements-cache";
49  
50      protected static CacheManager CACHE_MANAGER;
51  
52      @BeforeClass
53      public static void setUpGlobal() {
54          final Configuration config = new Configuration();
55          config.addDefaultCache(
56                  new CacheConfiguration("default", Integer.MAX_VALUE)
57                      .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
58                      .overflowToDisk(false));
59          CACHE_MANAGER = CacheManager.create(config);
60      }
61  
62      @Override
63      @Before
64      public void setUp() {
65          super.setUp();
66          config = CacheConfig.custom().setMaxObjectSize(MAX_BYTES).build();
67  
68          if (CACHE_MANAGER.cacheExists(TEST_EHCACHE_NAME)){
69              CACHE_MANAGER.removeCache(TEST_EHCACHE_NAME);
70          }
71          CACHE_MANAGER.addCache(TEST_EHCACHE_NAME);
72          final HttpCacheStorage storage = new EhcacheHttpCacheStorage(CACHE_MANAGER.getCache(TEST_EHCACHE_NAME));
73          mockBackend = EasyMock.createNiceMock(ClientExecChain.class);
74  
75          impl = new CachingExec(mockBackend, new HeapResourceFactory(), storage, config);
76      }
77  
78      @After
79      public void tearDown(){
80          CACHE_MANAGER.removeCache(TEST_EHCACHE_NAME);
81      }
82  
83      @AfterClass
84      public static void tearDownGlobal(){
85          CACHE_MANAGER.shutdown();
86      }
87  
88  }