View Javadoc
1   /*
2    * Copyright (C) 2018, Konrad Windszus <konrad_w@gmx.de> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.util;
11  
12  import static org.hamcrest.MatcherAssert.assertThat;
13  
14  import java.util.LinkedHashMap;
15  import java.util.Map;
16  
17  import org.hamcrest.collection.IsIterableContainingInOrder;
18  import org.junit.Test;
19  
20  public class LRUMapTest {
21  
22  	@SuppressWarnings("boxing")
23  	@Test
24  	public void testLRUEntriesAreEvicted() {
25  		Map<Integer, Integer> map = new LRUMap<>(3, 3);
26  		for (int i = 0; i < 3; i++) {
27  			map.put(i, i);
28  		}
29  		// access the last ones
30  		map.get(2);
31  		map.get(0);
32  
33  		// put another one which exceeds the limit (entry with key "1" is
34  		// evicted)
35  		map.put(3, 3);
36  
37  		Map<Integer, Integer> expectedMap = new LinkedHashMap<>();
38  		expectedMap.put(2, 2);
39  		expectedMap.put(0, 0);
40  		expectedMap.put(3, 3);
41  
42  		assertThat(map.entrySet(), IsIterableContainingInOrder
43  				.contains(expectedMap.entrySet().toArray()));
44  	}
45  }