Package org.hotswap.agent.plugin.jvm

Source Code of org.hotswap.agent.plugin.jvm.AnonymousClassInfoTest

package org.hotswap.agent.plugin.jvm;

import org.hotswap.agent.javassist.CannotCompileException;
import org.hotswap.agent.javassist.ClassPool;
import org.hotswap.agent.javassist.LoaderClassPath;
import org.hotswap.agent.javassist.NotFoundException;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertEquals;

/**
* Check generated signatures same with java Class and javassist CtClass.
*/
public class AnonymousClassInfoTest {

    @Test
    public void testGetClassSignature() throws Exception {
        String classSignature = "java.lang.Object;" + AnonymousTestInterface1.class.getName();
        assertEquals(classSignature, getAnonymousClassInfo().getClassSignature());
        assertEquals(classSignature, getAnonymousCtClassInfo().getClassSignature());
    }

    @Test
    public void testGetMethodSignature() throws Exception {
        String methodsSignature = "java.lang.String test1();";
        assertEquals(methodsSignature, getAnonymousClassInfo().getMethodSignature());
        assertEquals(methodsSignature, getAnonymousCtClassInfo().getMethodSignature());
    }

    @Test
    public void testGetFieldsSignature() throws Exception {
        // default field this
        String fieldsSignature = "org.hotswap.agent.plugin.jvm.AnonymousTestClass1 this$0;";
        assertEquals(fieldsSignature, getAnonymousClassInfo().getFieldsSignature());
        assertEquals(fieldsSignature, getAnonymousCtClassInfo().getFieldsSignature());
    }

    @Test
    public void testGetEnclosingMethodSignature() throws Exception {
        String enclosingMethodSignature = "java.lang.String enclosing1();";
        assertEquals(enclosingMethodSignature, getAnonymousClassInfo().getEnclosingMethodSignature());
        assertEquals(enclosingMethodSignature, getAnonymousCtClassInfo().getEnclosingMethodSignature());
    }

    private AnonymousClassInfo getAnonymousClassInfo() throws ClassNotFoundException {
        Class clazz = getClass().getClassLoader().loadClass(AnonymousTestClass1.class.getName() + "$1");
        return new AnonymousClassInfo(clazz);
    }

    public AnonymousClassInfo getAnonymousCtClassInfo() throws NotFoundException, ClassNotFoundException, IOException, CannotCompileException {
        Class clazz = getClass().getClassLoader().loadClass(AnonymousTestClass1.class.getName() + "$1");

        ClassPool classPool = new ClassPool();
        classPool.appendClassPath(new LoaderClassPath(getClass().getClassLoader()));
        return new AnonymousClassInfo(classPool.get(clazz.getName()));
    }
}
TOP

Related Classes of org.hotswap.agent.plugin.jvm.AnonymousClassInfoTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.