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.hc.client5.http.impl.cache;
28  
29  import java.util.LinkedList;
30  import java.util.Queue;
31  
32  import org.apache.hc.client5.http.cache.HttpCacheEntry;
33  import org.junit.jupiter.api.Assertions;
34  import org.junit.jupiter.api.Test;
35  
36  public class TestInternalCacheStorage {
37  
38      @Test
39      public void testCacheBasics() {
40          final InternalCacheStorage storage = new InternalCacheStorage();
41          final String key1 = "some-key-1";
42          Assertions.assertNull(storage.get(key1));
43          Assertions.assertNull(storage.remove(key1));
44          final HttpCacheEntry entry1 = HttpTestUtils.makeCacheEntry();
45          storage.put(key1, entry1);
46          Assertions.assertSame(entry1, storage.get(key1));
47          Assertions.assertSame(entry1, storage.remove(key1));
48          Assertions.assertNull(storage.get(key1));
49          Assertions.assertNull(storage.remove(key1));
50          final String key2 = "some-key-2";
51          final HttpCacheEntry entry2 = HttpTestUtils.makeCacheEntry();
52          final String key3 = "some-key-3";
53          final HttpCacheEntry entry3 = HttpTestUtils.makeCacheEntry();
54          storage.put(key2, entry2);
55          storage.put(key3, entry3);
56          Assertions.assertSame(entry2, storage.get(key2));
57          Assertions.assertSame(entry3, storage.get(key3));
58          storage.clear();
59          Assertions.assertNull(storage.get(key2));
60          Assertions.assertNull(storage.get(key3));
61      }
62  
63      @Test
64      public void testCacheEviction() {
65          final Queue<HttpCacheEntry> evictedEntries = new LinkedList<>();
66          final InternalCacheStorage storage = new InternalCacheStorage(2, e -> evictedEntries.add(e.getContent()));
67          final String key1 = "some-key-1";
68          final String key2 = "some-key-2";
69          final String key3 = "some-key-3";
70          final String key4 = "some-key-4";
71          final HttpCacheEntry entry1 = HttpTestUtils.makeCacheEntry();
72          final HttpCacheEntry entry2 = HttpTestUtils.makeCacheEntry();
73          final HttpCacheEntry entry3 = HttpTestUtils.makeCacheEntry();
74          final HttpCacheEntry entry4 = HttpTestUtils.makeCacheEntry();
75  
76          storage.put(key1, entry1);
77          storage.put(key2, entry2);
78          storage.put(key3, entry3);
79          storage.put(key4, entry4);
80          Assertions.assertSame(entry1, evictedEntries.poll());
81          Assertions.assertSame(entry2, evictedEntries.poll());
82          Assertions.assertNull(evictedEntries.poll());
83  
84          storage.clear();
85          storage.put(key1, entry1);
86          storage.put(key2, entry2);
87          storage.get(key1);
88          storage.put(key3, entry3);
89          storage.get(key1);
90          storage.put(key4, entry4);
91  
92          Assertions.assertSame(entry2, evictedEntries.poll());
93          Assertions.assertSame(entry3, evictedEntries.poll());
94          Assertions.assertNull(evictedEntries.poll());
95      }
96  
97  }