123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- /*
- * Copyright (c) Brett Randall 2004. All rights reserved.
- *
- * Created on Jul 20, 2004
- */
-
- package javassist;
-
- import junit.framework.TestCase;
-
- /**
- * @author brandall
- */
- @SuppressWarnings({"rawtypes","unused"})
- public class LoaderTestByRandall extends TestCase {
-
- ClassPool cp;
- Loader loader;
-
- public LoaderTestByRandall(String name) {
- super(name);
- }
-
- public void setUp() {
- cp = new ClassPool();
- cp.appendSystemPath();
- loader = new Loader(cp);
- }
-
- public void testLoadGoodClass() throws Exception {
- String name = "javassist.LoaderTestByRandall";
- cp.get(name);
- Class clazz = loader.loadClass(name);
- assertEquals("Class not loaded by loader",
- loader, clazz.getClassLoader());
- }
-
- public void testLoadGoodClassByDelegation() throws Exception {
- Class clazz = loader.loadClass("java.lang.String");
- }
-
- public void testLoadBadClass() {
- try {
- Class clazz = loader.loadClass("never.going.to.find.Class");
- fail("Expected ClassNotFoundException to be thrown");
- } catch (ClassNotFoundException e) {
- // expected
- }
- }
-
- public void testLoadBadClassByDelegation() {
- try {
- Class clazz = loader.loadClass("java.never.going.to.find.Class");
- fail("Expected ClassNotFoundException to be thrown");
- } catch (ClassNotFoundException e) {
- // expected
- }
- }
-
- public void testLoadBadCodeModification() throws Exception {
- String classname = "javassist.LoaderTestByRandall";
-
- Translator trans = new Translator() {
- public void start(ClassPool pool)
- throws NotFoundException, CannotCompileException
- {
- }
-
- public void onLoad(ClassPool pool, String classname)
- throws NotFoundException, CannotCompileException
- {
- String body = new String("this will never compile");
- CtClass clazz = pool.get(classname);
- CtMethod newMethod = CtNewMethod.make(CtClassType.voidType,
- "wontCompileMethod",
- new CtClass[] {},
- new CtClass[] {},
- body,
- clazz);
- clazz.addMethod(newMethod);
- }
- };
-
- loader.addTranslator(cp, trans);
-
- try {
- Class clazz = loader.loadClass(classname);
- fail("Expected loader to throw ClassNotFoundException " +
- "caused by CannotCompileException");
- } catch (ClassNotFoundException e) {
- // expected
- System.out.println(e);
- assertEquals("ClassNotFoundException was not caused "
- + "by CannotCompileException",
- CannotCompileException.class,
- e.getCause().getClass());
- }
- }
- }
|