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   *   https://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.bcel.util;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.assertNull;
24  import static org.junit.jupiter.api.Assertions.assertThrows;
25  
26  import java.io.IOException;
27  
28  import org.apache.bcel.classfile.JavaClass;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Tests {@link LruCacheClassPathRepository}.
33   */
34  class LruCacheClassPathRepositoryTest {
35  
36      @Test
37      void testCacheEviction() throws ClassNotFoundException, IOException {
38          try (ClassPath classPath = new ClassPath("")) {
39              final LruCacheClassPathRepository repository = new LruCacheClassPathRepository(classPath, 2);
40              final JavaClass class1 = repository.loadClass("java.lang.String");
41              assertNotNull(class1);
42              final JavaClass class2 = repository.loadClass("java.lang.Long");
43              assertNotNull(class2);
44              final JavaClass class3 = repository.loadClass("java.lang.Integer"); // Evicts class1
45              assertNotNull(class3);
46  
47              assertNull(repository.findClass("java.lang.String"));
48              final JavaClass cachedClass2 = repository.findClass("java.lang.Long");
49              assertEquals(class2, cachedClass2);
50          }
51      }
52  
53      @Test
54      void testLeastRecentlyUsedEviction() throws ClassNotFoundException, IOException {
55          try (ClassPath classPath = new ClassPath("")) {
56              final LruCacheClassPathRepository repository = new LruCacheClassPathRepository(classPath, 2);
57              final JavaClass class1 = repository.loadClass("java.lang.String");
58              assertNotNull(class1);
59              final JavaClass class2 = repository.loadClass("java.lang.Long");
60              assertNotNull(class2);
61              repository.findClass("java.lang.String"); // Uses class1
62              final JavaClass class3 = repository.loadClass("java.lang.Integer"); // Evicts class2
63              assertNotNull(class3);
64  
65              assertNull(repository.findClass("java.lang.Long"));
66              final JavaClass cachedClass1 = repository.findClass("java.lang.String");
67              assertEquals(class1, cachedClass1);
68          }
69      }
70  
71      @Test
72      void testZeroCacheSize() throws IOException {
73          try (ClassPath classPath = new ClassPath("")) {
74              assertThrows(IllegalArgumentException.class, () -> new LruCacheClassPathRepository(classPath, 0));
75          }
76      }
77  }