/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

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