1
|
package eu.bcvsolutions.idm.core.script;
|
2
|
|
3
|
import org.junit.Test;
|
4
|
|
5
|
import groovy.lang.ExpandoMetaClass;
|
6
|
import groovy.lang.GroovyClassLoader;
|
7
|
import groovy.lang.GroovySystem;
|
8
|
import groovy.lang.MetaClass;
|
9
|
import groovy.lang.MetaClassRegistry;
|
10
|
|
11
|
public class GroovyScriptTest {
|
12
|
|
13
|
@Test
|
14
|
public void testForLeak() throws Exception {
|
15
|
for (int i = 1; i <= 10000; i++) {
|
16
|
GroovyClassLoader classLoader = new GroovyClassLoader(Thread.currentThread().getContextClassLoader());
|
17
|
Class<?> clazz = classLoader.parseClass("print 'hello world'");
|
18
|
ExpandoMetaClass metaClass = getExpandoMetaClass(clazz);
|
19
|
System.gc();
|
20
|
Thread.sleep(50);
|
21
|
System.out.println("Done " + i + " iterations");
|
22
|
}
|
23
|
}
|
24
|
|
25
|
// --Begin copied unmodified from GrailsMetaClassUtils--//
|
26
|
public static ExpandoMetaClass getExpandoMetaClass(Class<?> aClass) {
|
27
|
MetaClassRegistry registry = getRegistry();
|
28
|
|
29
|
MetaClass mc = registry.getMetaClass(aClass);
|
30
|
if (mc instanceof ExpandoMetaClass) {
|
31
|
ExpandoMetaClass emc = (ExpandoMetaClass) mc;
|
32
|
registry.setMetaClass(aClass, emc); // make permanent
|
33
|
return emc;
|
34
|
}
|
35
|
|
36
|
registry.removeMetaClass(aClass);
|
37
|
mc = registry.getMetaClass(aClass);
|
38
|
if (mc instanceof ExpandoMetaClass) {
|
39
|
return (ExpandoMetaClass) mc;
|
40
|
}
|
41
|
|
42
|
ExpandoMetaClass emc = new ExpandoMetaClass(aClass, true, true);
|
43
|
emc.initialize();
|
44
|
registry.setMetaClass(aClass, emc);
|
45
|
return emc;
|
46
|
}
|
47
|
|
48
|
public static MetaClassRegistry getRegistry() {
|
49
|
return GroovySystem.getMetaClassRegistry();
|
50
|
}
|
51
|
// --End copied unmodified from GrailsMetaClassUtils--//
|
52
|
}
|
53
|
|