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      /**
24       * Determines whether clover was used. Clover will instrument the classes and therefore
25       * increase the number of imports/other classes referenced by the analyzed class...
26       * @param imports the map of imported classes
27       * @return <code>true</code> if clover is found, <code>false</code> otherwise.
28       */
29      private boolean isClover(Map<String, String> imports) {
30      	return imports.values().contains("com_cenqua_clover.Clover");
31      }
32      
33      @Test
34      public void testLoadClassWithImportOnDemand() throws Exception {
35          String className = "net.sourceforge.pmd.typeresolution.ClassWithImportOnDemand";
36          Class<?> clazz = cl.loadClass(className);
37          assertNotNull(clazz);
38          Map<String, String> imports = cl.getImportedClasses(className);
39          assertNotNull(imports);
40          if (isClover(imports)) {
41          	assertEquals(22, imports.size());
42          } else {
43          	assertEquals(4, imports.size());
44          }
45          assertEquals("java.util.List", imports.get("List"));
46          assertEquals("java.util.ArrayList", imports.get("ArrayList"));
47          assertEquals("java.lang.Object", imports.get("Object"));
48          assertEquals("net.sourceforge.pmd.typeresolution.ClassWithImportOnDemand", imports.get("ClassWithImportOnDemand"));
49      }
50      
51      @Test
52      public void testClassWithImportInnerOnDemand() throws Exception {
53          String className = "net.sourceforge.pmd.typeresolution.ClassWithImportInnerOnDemand";
54          Class<?> clazz = cl.loadClass(className);
55          assertNotNull(clazz);
56          Map<String, String> imports = cl.getImportedClasses(className);
57          assertNotNull(imports);
58          if (isClover(imports)) {
59          	assertEquals(26, imports.size());
60          } else {
61          	assertEquals(8, imports.size());
62          }
63          assertEquals("java.util.Iterator", imports.get("Iterator"));
64          assertEquals("java.util.Map", imports.get("Map"));
65          assertEquals("java.util.Set", imports.get("Set"));
66          assertEquals("java.util.Map$Entry", imports.get("Entry"));
67          assertEquals("java.util.Map$Entry", imports.get("Map$Entry"));
68          assertEquals("java.util.Map$Entry", imports.get("Map$Entry"));
69          assertEquals("java.lang.Object", imports.get("Object"));
70          assertEquals("net.sourceforge.pmd.typeresolution.ClassWithImportInnerOnDemand", imports.get("ClassWithImportInnerOnDemand"));
71      }
72  
73      /**
74       * Unit test for bug 3546093.
75       *
76       * @throws Exception any error
77       */
78      @Test
79      public void testCachingOfNotFoundClasses() throws Exception {
80  	MockedClassLoader mockedClassloader = new MockedClassLoader();
81  	PMDASMClassLoader cl = PMDASMClassLoader.getInstance(mockedClassloader);
82  	String notExistingClassname = "that.clazz.doesnot.Exist";
83  	try {
84  	    cl.loadClass(notExistingClassname);
85  	    fail();
86  	} catch (ClassNotFoundException e) {
87  	    // expected
88  	}
89  
90  	try {
91  	    cl.loadClass(notExistingClassname);
92  	    fail();
93  	} catch (ClassNotFoundException e) {
94  	    // expected
95  	}
96  
97  	assertEquals(1, mockedClassloader.findClassCalls);
98      }
99  
100     private static class MockedClassLoader extends ClassLoader {
101 	int findClassCalls = 0;
102 
103 	@Override
104 	protected Class<?> findClass(String name) throws ClassNotFoundException {
105 	    findClassCalls++;
106 	    return super.findClass(name);
107 	}
108     }
109 
110     /**
111      * With this test you can verify, how much memory could be consumed
112      * by the dontBother cache.
113      * @throws Exception any error
114      */
115     @Ignore
116     @Test
117     public void testCachingMemoryConsumption() throws Exception {
118 	MockedClassLoader mockedClassLoader = new MockedClassLoader();
119 	PMDASMClassLoader cl = PMDASMClassLoader.getInstance(mockedClassLoader);
120 
121 	Runtime runtime = Runtime.getRuntime();
122 	System.gc();
123 
124 	long usedBytesBefore = runtime.totalMemory() - runtime.freeMemory();
125 
126 	for (long i = 0; i < 3000; i++) {
127 	    try {
128 		cl.loadClass("com.very.long.package.name.and.structure.MyClass" + i);
129 	    } catch (ClassNotFoundException e) {
130 		// expected
131 	    }
132 	}
133 
134 	long usedBytesAfter = runtime.totalMemory() - runtime.freeMemory();
135 
136 	System.out.println((usedBytesAfter - usedBytesBefore)/(1024.0*1024.0) + " mb needed");
137     }
138 
139     public static junit.framework.Test suite() {
140         return new junit.framework.JUnit4TestAdapter(PMDASMClassLoaderTest.class);
141     }
142 }