Search in sources :

Example 1 with SimplePlugin

use of org.hotswap.agent.testData.SimplePlugin in project HotswapAgent by HotswapProjects.

the class InitHandlerTest method testInitMethod.

@Test
public void testInitMethod() throws Exception {
    PluginManager pluginManager = PluginManager.getInstance();
    SimplePlugin simplePlugin = new SimplePlugin();
    // register the plugin
    pluginManager.getPluginRegistry().getRegisteredPlugins().put(SimplePlugin.class, Collections.<ClassLoader, Object>singletonMap(getClass().getClassLoader(), simplePlugin));
    InitHandler initHandler = new InitHandler(pluginManager);
    Method method = SimplePlugin.class.getMethod("initPlugin", PluginManager.class);
    PluginAnnotation<Init> pluginAnnotation = new PluginAnnotation<Init>(SimplePlugin.class, simplePlugin, method.getAnnotation(Init.class), method);
    assertTrue("Init successful", initHandler.initMethod(pluginAnnotation));
}
Also used : PluginManager(org.hotswap.agent.config.PluginManager) Init(org.hotswap.agent.annotation.Init) SimplePlugin(org.hotswap.agent.testData.SimplePlugin) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 2 with SimplePlugin

use of org.hotswap.agent.testData.SimplePlugin in project HotswapAgent by HotswapProjects.

the class OnClassLoadEventHandlerTest method testInitMethod.

@Test
public void testInitMethod() throws Exception {
    final ClassLoader appClassLoader = getClass().getClassLoader();
    context.checking(new Expectations() {

        {
            allowing(pluginManager).getHotswapTransformer();
            will(returnValue(hotswapTransformer));
            allowing(pluginManager).getPluginRegistry();
            will(returnValue(pluginRegistry));
            allowing(pluginRegistry).getAppClassLoader(with(any(Object.class)));
            will(returnValue(appClassLoader));
            oneOf(hotswapTransformer).registerTransformer(with(appClassLoader), with("org.hotswap.example.type"), with(any(ClassFileTransformer.class)));
        }
    });
    OnClassLoadedHandler onClassLoadedHandler = new OnClassLoadedHandler(pluginManager);
    SimplePlugin simplePlugin = new SimplePlugin();
    Method method = SimplePlugin.class.getMethod("transform");
    PluginAnnotation<OnClassLoadEvent> pluginAnnotation = new PluginAnnotation<OnClassLoadEvent>(SimplePlugin.class, simplePlugin, method.getAnnotation(OnClassLoadEvent.class), method);
    assertTrue("Init successful", onClassLoadedHandler.initMethod(pluginAnnotation));
}
Also used : Expectations(org.jmock.Expectations) ClassFileTransformer(java.lang.instrument.ClassFileTransformer) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent) SimplePlugin(org.hotswap.agent.testData.SimplePlugin) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 3 with SimplePlugin

use of org.hotswap.agent.testData.SimplePlugin in project HotswapAgent by HotswapProjects.

the class AnnotationProcessorTest method testProcess.

@Test
public void testProcess() throws Exception {
    context.checking(new Expectations() {

        {
            allowing(pluginManager).getHotswapTransformer();
            will(returnValue(hotswapTransformer));
            // two @Init annotations in SimplePlugin
            exactly(2).of(initHandler).initMethod(with(any(PluginAnnotation.class)));
            will(returnValue(true));
            // @OnClassLoadEvent
            exactly(1).of(onClassLoadedHandler).initMethod(with(any(PluginAnnotation.class)));
            will(returnValue(true));
        // Not satisfied, because invoked via reflection
        // oneOf(simplePlugin).initPlugin();
        // oneOf(simplePlugin).initPlugin(pluginManager);
        }
    });
    final AnnotationProcessor annotationProcessor = new AnnotationProcessor(pluginManager);
    annotationProcessor.addAnnotationHandler(Init.class, initHandler);
    annotationProcessor.addAnnotationHandler(OnClassLoadEvent.class, onClassLoadedHandler);
    annotationProcessor.processAnnotations(new SimplePlugin());
    context.assertIsSatisfied();
}
Also used : Expectations(org.jmock.Expectations) SimplePlugin(org.hotswap.agent.testData.SimplePlugin) AnnotationProcessor(org.hotswap.agent.annotation.handler.AnnotationProcessor) PluginAnnotation(org.hotswap.agent.annotation.handler.PluginAnnotation) Test(org.junit.Test)

Example 4 with SimplePlugin

use of org.hotswap.agent.testData.SimplePlugin in project HotswapAgent by HotswapProjects.

the class PluginManagerInvokerTest method testBuildCallPluginMethod.

@Test
public void testBuildCallPluginMethod() throws Exception {
    SimplePlugin plugin = new SimplePlugin();
    registerPlugin(plugin);
    // plugin.init(PluginManager.getInstance());
    String s = PluginManagerInvoker.buildCallPluginMethod(plugin.getClass(), "callPluginMethod", "Boolean.TRUE", "java.lang.Boolean");
    ClassPool classPool = ClassPool.getDefault();
    classPool.appendSystemPath();
    CtClass clazz = classPool.makeClass("Test");
    clazz.addMethod(CtNewMethod.make("public void test() {" + s + "}", clazz));
    Class<?> testClass = clazz.toClass();
    Method testMethod = testClass.getDeclaredMethod("test");
    testMethod.invoke(testClass.newInstance());
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) ClassPool(org.hotswap.agent.javassist.ClassPool) SimplePlugin(org.hotswap.agent.testData.SimplePlugin) CtNewMethod(org.hotswap.agent.javassist.CtNewMethod) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

SimplePlugin (org.hotswap.agent.testData.SimplePlugin)4 Test (org.junit.Test)4 Method (java.lang.reflect.Method)3 Expectations (org.jmock.Expectations)2 ClassFileTransformer (java.lang.instrument.ClassFileTransformer)1 Init (org.hotswap.agent.annotation.Init)1 OnClassLoadEvent (org.hotswap.agent.annotation.OnClassLoadEvent)1 AnnotationProcessor (org.hotswap.agent.annotation.handler.AnnotationProcessor)1 PluginAnnotation (org.hotswap.agent.annotation.handler.PluginAnnotation)1 PluginManager (org.hotswap.agent.config.PluginManager)1 ClassPool (org.hotswap.agent.javassist.ClassPool)1 CtClass (org.hotswap.agent.javassist.CtClass)1 CtNewMethod (org.hotswap.agent.javassist.CtNewMethod)1