You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LoaderTestByRandall.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) Brett Randall 2004. All rights reserved.
  3. *
  4. * Created on Jul 20, 2004
  5. */
  6. package javassist;
  7. import junit.framework.TestCase;
  8. /**
  9. * @author brandall
  10. */
  11. @SuppressWarnings({"rawtypes","unused"})
  12. public class LoaderTestByRandall extends TestCase {
  13. ClassPool cp;
  14. Loader loader;
  15. public LoaderTestByRandall(String name) {
  16. super(name);
  17. }
  18. public void setUp() {
  19. cp = new ClassPool();
  20. cp.appendSystemPath();
  21. loader = new Loader(cp);
  22. }
  23. public void testLoadGoodClass() throws Exception {
  24. String name = "javassist.LoaderTestByRandall";
  25. cp.get(name);
  26. Class clazz = loader.loadClass(name);
  27. assertEquals("Class not loaded by loader",
  28. loader, clazz.getClassLoader());
  29. }
  30. public void testLoadGoodClassByDelegation() throws Exception {
  31. Class clazz = loader.loadClass("java.lang.String");
  32. }
  33. public void testLoadBadClass() {
  34. try {
  35. Class clazz = loader.loadClass("never.going.to.find.Class");
  36. fail("Expected ClassNotFoundException to be thrown");
  37. } catch (ClassNotFoundException e) {
  38. // expected
  39. }
  40. }
  41. public void testLoadBadClassByDelegation() {
  42. try {
  43. Class clazz = loader.loadClass("java.never.going.to.find.Class");
  44. fail("Expected ClassNotFoundException to be thrown");
  45. } catch (ClassNotFoundException e) {
  46. // expected
  47. }
  48. }
  49. public void testLoadBadCodeModification() throws Exception {
  50. String classname = "javassist.LoaderTestByRandall";
  51. Translator trans = new Translator() {
  52. public void start(ClassPool pool)
  53. throws NotFoundException, CannotCompileException
  54. {
  55. }
  56. public void onLoad(ClassPool pool, String classname)
  57. throws NotFoundException, CannotCompileException
  58. {
  59. String body = new String("this will never compile");
  60. CtClass clazz = pool.get(classname);
  61. CtMethod newMethod = CtNewMethod.make(CtClassType.voidType,
  62. "wontCompileMethod",
  63. new CtClass[] {},
  64. new CtClass[] {},
  65. body,
  66. clazz);
  67. clazz.addMethod(newMethod);
  68. }
  69. };
  70. loader.addTranslator(cp, trans);
  71. try {
  72. Class clazz = loader.loadClass(classname);
  73. fail("Expected loader to throw ClassNotFoundException " +
  74. "caused by CannotCompileException");
  75. } catch (ClassNotFoundException e) {
  76. // expected
  77. System.out.println(e);
  78. assertEquals("ClassNotFoundException was not caused "
  79. + "by CannotCompileException",
  80. CannotCompileException.class,
  81. e.getCause().getClass());
  82. }
  83. }
  84. }