PageRenderTime 20ms CodeModel.GetById 14ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/test/kilim/test/TestDynamicWeaver.java

http://github.com/kilim/kilim
Java | 97 lines | 73 code | 14 blank | 10 comment | 6 complexity | 24d6c3191777958ca3ab48d5fc434eba MD5 | raw file
 1package kilim.test;
 2
 3import java.util.Arrays;
 4import java.util.HashSet;
 5import java.util.List;
 6
 7import junit.framework.TestCase;
 8import kilim.analysis.ClassInfo;
 9import kilim.tools.Javac;
10import kilim.tools.Weaver;
11
12public class TestDynamicWeaver extends TestCase {
13    /**
14     * Sample code to test a wide range of functionality: separate packages, import statements, 
15     * mutually recursive classes across packages, Pausable methods, inner classes, public
16     * and non-public classes, etc.
17     */
18    String code1 = 
19        "package code1;" + 
20        "import java.io.IOException;" + 
21        "import kilim.*;" +
22        "public class A {" + 
23         "    code2.B bar;" +
24         "    class Inner {" +
25         "       void foo() throws Pausable, IOException {" +
26         "           for (int i = 0; i < 10; i++) {" +
27         "                Outer.xxx();" +
28         "           }" +
29         "       }" +
30         "    }" +
31        "}" +
32        "class Outer { " + 
33        "   static void xxx() throws Pausable, java.io.IOException {}" +
34        "}";
35
36    String code2 = 
37        "package code2;" + 
38        "public class B { " +
39        "    code1.A foo;" + 
40        "}";
41
42
43    public List<ClassInfo> compile() throws Exception {
44        List<ClassInfo> classes = Javac.compile(Arrays.asList(code1, code2));
45        assertTrue(classes.size() == 4);
46        HashSet<String> expectedClasses = new HashSet<String>(
47                Arrays.asList("code1.A", "code1.A$Inner", "code1.Outer", "code2.B"));
48
49        for (ClassInfo cl : classes) {
50            assertTrue(expectedClasses.contains(cl.className));
51            assertTrue(cl.bytes.length > 200);
52        }
53        return classes;
54    }
55
56    public void testWeave() throws Exception {
57        List<ClassInfo> classes = compile();
58
59        classes = new Weaver(null).weave(classes);
60        
61
62        HashSet<String> expectedClasses = new HashSet<String>(
63                Arrays.asList("kilim.S_I", "code1.A$Inner", "code1.Outer"));
64
65        assertTrue(expectedClasses.size() == classes.size());
66
67        for (ClassInfo cl : classes) {
68            assertTrue(expectedClasses.contains(cl.className));
69            assertTrue(cl.bytes != null && cl.bytes.length > 0);
70            // ensure classes are loadable
71            TestClassLoader cll = new TestClassLoader();
72            Class<?> c = null;
73            try {
74                c = cll.loadClass(cl.className);
75                // The only class that should be loadable is "kilim.S_I"
76                assertTrue(c.getName().startsWith("kilim"));
77            } catch (ClassNotFoundException ignore) {
78                // the new classes should not have been in the classpath, and 
79                // ClassNotFoundException is thrown as expected
80                assertTrue("class not found: " + cl.className,cl.className.startsWith("code"));
81                // define these classes
82                try {
83                    cll.load(cl);
84                } catch (Throwable t) {
85                    fail(t.getMessage());
86                }
87            }
88        }
89    }
90    
91    static class TestClassLoader extends ClassLoader {
92        public void load(ClassInfo cl) {
93            Class<?> c = super.defineClass(cl.className, cl.bytes, 0, cl.bytes.length);
94            super.resolveClass(c);
95        }
96    }
97}