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