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 ClassPathRepository}, {@link MemorySensitiveClassPathRepository}, and
33   * {@link LruCacheClassPathRepository} for their common attributes of caching.
34   *
35   * <p>
36   * Without memory scarcity, these classes behave in the same manner.
37   */
38  class ClassPathRepositoryTest {
39  
40      @Test
41      void testClassPath() throws IOException {
42          try (ClassPath classPath = new ClassPath("")) {
43              final ClassPathRepository repository = new ClassPathRepository(classPath);
44              try (ClassPath repoCp = repository.getClassPath()) {
45                  assertEquals(classPath, repoCp);
46              }
47          }
48      }
49  
50      @Test
51      void testClassPathRepository() throws ClassNotFoundException, IOException {
52          try (ClassPath classPath = new ClassPath("")) {
53              verifyCaching(new ClassPathRepository(classPath));
54          }
55      }
56  
57      @Test
58      void testClassWithoutPackage() throws IOException {
59          try (ClassPath classPath = new ClassPath("")) {
60              final ClassPathRepository repository = new ClassPathRepository(classPath);
61              assertThrows(ClassNotFoundException.class, () -> repository.loadClass("ClassXYZ"));
62          }
63      }
64  
65      @Test
66      void testEmptyInput() throws IOException {
67          try (ClassPath classPath = new ClassPath("")) {
68              final ClassPathRepository repository = new ClassPathRepository(classPath);
69              assertThrows(IllegalArgumentException.class, () -> repository.loadClass(""));
70          }
71      }
72  
73      @Test
74      void testLruCacheClassPathRepository() throws ClassNotFoundException, IOException {
75          try (ClassPath classPath = new ClassPath("")) {
76              verifyCaching(new LruCacheClassPathRepository(classPath, 10));
77          }
78      }
79  
80      @Test
81      void testMemorySensitiveClassPathRepository() throws ClassNotFoundException, IOException {
82          try (ClassPath classPath = new ClassPath("")) {
83              verifyCaching(new MemorySensitiveClassPathRepository(classPath));
84          }
85      }
86  
87      @Test
88      void testNoClassNotFound() throws IOException {
89          try (ClassPath classPath = new ClassPath("")) {
90              final ClassPathRepository repository = new ClassPathRepository(classPath);
91              assertThrows(ClassNotFoundException.class, () -> repository.loadClass("no.such.Class"));
92          }
93      }
94  
95      @Test
96      void testNullInput() throws IOException {
97          try (ClassPath classPath = new ClassPath("")) {
98              final ClassPathRepository repository = new ClassPathRepository(classPath);
99              assertThrows(IllegalArgumentException.class, () -> repository.loadClass((String) null));
100         }
101     }
102 
103     private void verifyCaching(final AbstractClassPathRepository repository) throws ClassNotFoundException {
104         // Tests loadClass()
105         final JavaClass class1 = repository.loadClass("java.lang.String");
106         assertNotNull(class1);
107         final JavaClass class2 = repository.loadClass("java/lang/Long"); // Slashes should work
108         assertNotNull(class2);
109 
110         // Tests findClass()
111         assertEquals(class1, repository.findClass("java.lang.String"));
112         assertEquals(class2, repository.findClass("java.lang.Long"));
113 
114         // Tests removeClass()
115         repository.removeClass(class1);
116         assertNull(repository.findClass("java.lang.String"));
117 
118         // Tests clear()
119         repository.clear();
120         assertNull(repository.findClass("java.lang.Long"));
121     }
122 }