View Javadoc

1   package net.sourceforge.pmd.typeresolution;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertNotNull;
5   import static org.junit.Assert.fail;
6   
7   import java.util.Map;
8   
9   import net.sourceforge.pmd.lang.java.typeresolution.PMDASMClassLoader;
10  
11  import org.junit.Before;
12  import org.junit.Ignore;
13  import org.junit.Test;
14  public class PMDASMClassLoaderTest {
15  
16      private PMDASMClassLoader cl;
17      
18      @Before
19      public void setUp() throws Exception {
20          cl = PMDASMClassLoader.getInstance(getClass().getClassLoader());
21      }
22  
23      @Test
24      public void testLoadClassWithImportOnDemand() throws Exception {
25          String className = "net.sourceforge.pmd.typeresolution.ClassWithImportOnDemand";
26          Class<?> clazz = cl.loadClass(className);
27          assertNotNull(clazz);
28          Map<String, String> imports = cl.getImportedClasses(className);
29          assertNotNull(imports);
30          assertEquals("java.util.List", imports.get("List"));
31          assertEquals("java.util.ArrayList", imports.get("ArrayList"));
32          assertEquals("java.lang.Object", imports.get("Object"));
33          assertEquals("net.sourceforge.pmd.typeresolution.ClassWithImportOnDemand", imports.get("ClassWithImportOnDemand"));
34      }
35      
36      @Test
37      public void testClassWithImportInnerOnDemand() throws Exception {
38          String className = "net.sourceforge.pmd.typeresolution.ClassWithImportInnerOnDemand";
39          Class<?> clazz = cl.loadClass(className);
40          assertNotNull(clazz);
41          Map<String, String> imports = cl.getImportedClasses(className);
42          assertNotNull(imports);
43          assertEquals("java.util.Iterator", imports.get("Iterator"));
44          assertEquals("java.util.Map", imports.get("Map"));
45          assertEquals("java.util.Set", imports.get("Set"));
46          assertEquals("java.util.Map$Entry", imports.get("Entry"));
47          assertEquals("java.util.Map$Entry", imports.get("Map$Entry"));
48          assertEquals("java.util.Map$Entry", imports.get("Map$Entry"));
49          assertEquals("java.lang.Object", imports.get("Object"));
50          assertEquals("net.sourceforge.pmd.typeresolution.ClassWithImportInnerOnDemand", imports.get("ClassWithImportInnerOnDemand"));
51      }
52  
53      /**
54       * Unit test for bug 3546093.
55       *
56       * @throws Exception any error
57       */
58      @Test
59      public void testCachingOfNotFoundClasses() throws Exception {
60  	MockedClassLoader mockedClassloader = new MockedClassLoader();
61  	PMDASMClassLoader cl = PMDASMClassLoader.getInstance(mockedClassloader);
62  	String notExistingClassname = "that.clazz.doesnot.Exist";
63  	try {
64  	    cl.loadClass(notExistingClassname);
65  	    fail();
66  	} catch (ClassNotFoundException e) {
67  	    // expected
68  	}
69  
70  	try {
71  	    cl.loadClass(notExistingClassname);
72  	    fail();
73  	} catch (ClassNotFoundException e) {
74  	    // expected
75  	}
76  
77  	assertEquals(1, mockedClassloader.findClassCalls);
78      }
79  
80      private static class MockedClassLoader extends ClassLoader {
81  	int findClassCalls = 0;
82  
83  	@Override
84  	protected Class<?> findClass(String name) throws ClassNotFoundException {
85  	    findClassCalls++;
86  	    return super.findClass(name);
87  	}
88      }
89  
90      /**
91       * With this test you can verify, how much memory could be consumed
92       * by the dontBother cache.
93       * @throws Exception any error
94       */
95      @Ignore
96      @Test
97      public void testCachingMemoryConsumption() throws Exception {
98  	MockedClassLoader mockedClassLoader = new MockedClassLoader();
99  	PMDASMClassLoader cl = PMDASMClassLoader.getInstance(mockedClassLoader);
100 
101 	Runtime runtime = Runtime.getRuntime();
102 	System.gc();
103 
104 	long usedBytesBefore = runtime.totalMemory() - runtime.freeMemory();
105 
106 	for (long i = 0; i < 3000; i++) {
107 	    try {
108 		cl.loadClass("com.very.long.package.name.and.structure.MyClass" + i);
109 	    } catch (ClassNotFoundException e) {
110 		// expected
111 	    }
112 	}
113 
114 	long usedBytesAfter = runtime.totalMemory() - runtime.freeMemory();
115 
116 	System.out.println((usedBytesAfter - usedBytesBefore)/(1024.0*1024.0) + " mb needed");
117     }
118 
119     public static junit.framework.Test suite() {
120         return new junit.framework.JUnit4TestAdapter(PMDASMClassLoaderTest.class);
121     }
122 }