View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.bcel.util;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  
24  import java.io.IOException;
25  
26  import org.apache.bcel.classfile.JavaClass;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Tests {@link LruCacheClassPathRepository}.
31   */
32  public class LruCacheClassPathRepositoryTestCase {
33  
34      @Test
35      public void testCacheEviction() throws ClassNotFoundException, IOException {
36          try (final ClassPath classPath = new ClassPath("")) {
37              final LruCacheClassPathRepository repository = new LruCacheClassPathRepository(classPath, 2);
38              final JavaClass class1 = repository.loadClass("java.lang.String");
39              assertNotNull(class1);
40              final JavaClass class2 = repository.loadClass("java.lang.Long");
41              assertNotNull(class2);
42              final JavaClass class3 = repository.loadClass("java.lang.Integer"); // Evicts class1
43              assertNotNull(class3);
44  
45              assertNull(repository.findClass("java.lang.String"));
46              final JavaClass cachedClass2 = repository.findClass("java.lang.Long");
47              assertEquals(class2, cachedClass2);
48          }
49      }
50  
51      @Test
52      public void testLeastRecentlyUsedEviction() throws ClassNotFoundException, IOException {
53          try (final ClassPath classPath = new ClassPath("")) {
54              final LruCacheClassPathRepository repository = new LruCacheClassPathRepository(classPath, 2);
55              final JavaClass class1 = repository.loadClass("java.lang.String");
56              assertNotNull(class1);
57              final JavaClass class2 = repository.loadClass("java.lang.Long");
58              assertNotNull(class2);
59              repository.findClass("java.lang.String"); // Uses class1
60              final JavaClass class3 = repository.loadClass("java.lang.Integer"); // Evicts class2
61              assertNotNull(class3);
62  
63              assertNull(repository.findClass("java.lang.Long"));
64              final JavaClass cachedClass1 = repository.findClass("java.lang.String");
65              assertEquals(class1, cachedClass1);
66          }
67      }
68  
69      @Test
70      public void testZeroCacheSize() throws IOException {
71          try (final ClassPath classPath = new ClassPath("")) {
72              assertThrows(IllegalArgumentException.class, () -> new LruCacheClassPathRepository(classPath, 0));
73          }
74      }
75  }