Search in sources :

Example 1 with NarClassLoaderBuilder

use of org.apache.pulsar.common.nar.NarClassLoaderBuilder in project pulsar by apache.

the class AdditionalServletUtilsTest method testLoadEventListenerWithBlankListerClass.

@Test(expectedExceptions = IOException.class)
public void testLoadEventListenerWithBlankListerClass() throws Exception {
    AdditionalServletDefinition def = new AdditionalServletDefinition();
    def.setDescription("test-proxy-listener");
    String archivePath = "/path/to/proxy/listener/nar";
    AdditionalServletMetadata metadata = new AdditionalServletMetadata();
    metadata.setDefinition(def);
    metadata.setArchivePath(Paths.get(archivePath));
    NarClassLoader mockLoader = mock(NarClassLoader.class);
    when(mockLoader.getServiceDefinition(eq(AdditionalServletUtils.ADDITIONAL_SERVLET_FILE))).thenReturn(ObjectMapperFactory.getThreadLocalYaml().writeValueAsString(def));
    Class listenerClass = MockAdditionalServlet.class;
    when(mockLoader.loadClass(eq(MockAdditionalServlet.class.getName()))).thenReturn(listenerClass);
    final NarClassLoaderBuilder mockedBuilder = mock(NarClassLoaderBuilder.class, RETURNS_SELF);
    when(mockedBuilder.build()).thenReturn(mockLoader);
    try (MockedStatic<NarClassLoaderBuilder> builder = Mockito.mockStatic(NarClassLoaderBuilder.class)) {
        builder.when(() -> NarClassLoaderBuilder.builder()).thenReturn(mockedBuilder);
        AdditionalServletUtils.load(metadata, "");
    }
}
Also used : NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) NarClassLoaderBuilder(org.apache.pulsar.common.nar.NarClassLoaderBuilder) Test(org.testng.annotations.Test)

Example 2 with NarClassLoaderBuilder

use of org.apache.pulsar.common.nar.NarClassLoaderBuilder in project pulsar by apache.

the class AdditionalServletUtilsTest method testLoadEventListenerWithWrongListerClass.

@Test(expectedExceptions = IOException.class)
public void testLoadEventListenerWithWrongListerClass() throws Exception {
    AdditionalServletDefinition def = new AdditionalServletDefinition();
    def.setAdditionalServletClass(Runnable.class.getName());
    def.setDescription("test-proxy-listener");
    String archivePath = "/path/to/proxy/listener/nar";
    AdditionalServletMetadata metadata = new AdditionalServletMetadata();
    metadata.setDefinition(def);
    metadata.setArchivePath(Paths.get(archivePath));
    NarClassLoader mockLoader = mock(NarClassLoader.class);
    when(mockLoader.getServiceDefinition(eq(AdditionalServletUtils.ADDITIONAL_SERVLET_FILE))).thenReturn(ObjectMapperFactory.getThreadLocalYaml().writeValueAsString(def));
    Class listenerClass = Runnable.class;
    when(mockLoader.loadClass(eq(Runnable.class.getName()))).thenReturn(listenerClass);
    final NarClassLoaderBuilder mockedBuilder = mock(NarClassLoaderBuilder.class, RETURNS_SELF);
    when(mockedBuilder.build()).thenReturn(mockLoader);
    try (MockedStatic<NarClassLoaderBuilder> builder = Mockito.mockStatic(NarClassLoaderBuilder.class)) {
        builder.when(() -> NarClassLoaderBuilder.builder()).thenReturn(mockedBuilder);
        AdditionalServletUtils.load(metadata, "");
    }
}
Also used : NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) NarClassLoaderBuilder(org.apache.pulsar.common.nar.NarClassLoaderBuilder) Test(org.testng.annotations.Test)

Example 3 with NarClassLoaderBuilder

use of org.apache.pulsar.common.nar.NarClassLoaderBuilder in project pulsar by apache.

the class AdditionalServletWithPulsarServiceTest method testLoadAdditionalServlet.

@Test
public void testLoadAdditionalServlet() throws Exception {
    AdditionalServletDefinition def = new AdditionalServletDefinition();
    def.setAdditionalServletClass(MockAdditionalServletWithClassLoader.class.getName());
    def.setDescription("test-additional-servlet");
    String archivePath = "/path/to/additional/servlet/nar";
    AdditionalServletMetadata metadata = new AdditionalServletMetadata();
    metadata.setDefinition(def);
    metadata.setArchivePath(Paths.get(archivePath));
    NarClassLoader mockLoader = mock(NarClassLoader.class);
    when(mockLoader.getServiceDefinition(eq(AdditionalServletUtils.ADDITIONAL_SERVLET_FILE))).thenReturn(ObjectMapperFactory.getThreadLocalYaml().writeValueAsString(def));
    Class additionalServletClass = MockAdditionalServletWithClassLoader.class;
    when(mockLoader.loadClass(eq(MockAdditionalServletWithClassLoader.class.getName()))).thenReturn(additionalServletClass);
    final NarClassLoaderBuilder mockedBuilder = mock(NarClassLoaderBuilder.class, RETURNS_SELF);
    when(mockedBuilder.build()).thenReturn(mockLoader);
    try (MockedStatic<NarClassLoaderBuilder> builder = Mockito.mockStatic(NarClassLoaderBuilder.class)) {
        builder.when(() -> NarClassLoaderBuilder.builder()).thenReturn(mockedBuilder);
        AdditionalServletWithClassLoader returnedASWithCL = AdditionalServletUtils.load(metadata, "");
        AdditionalServlet returnedPh = returnedASWithCL.getServlet();
        assertSame(mockLoader, returnedASWithCL.getClassLoader());
        assertTrue(returnedPh instanceof MockAdditionalServletWithClassLoader);
    }
}
Also used : NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) NarClassLoaderBuilder(org.apache.pulsar.common.nar.NarClassLoaderBuilder) Test(org.testng.annotations.Test)

Example 4 with NarClassLoaderBuilder

use of org.apache.pulsar.common.nar.NarClassLoaderBuilder in project pulsar by apache.

the class BrokerInterceptorUtilsTest method testLoadBrokerEventListenerWithBlankListerClass.

@Test(expectedExceptions = IOException.class)
public void testLoadBrokerEventListenerWithBlankListerClass() throws Exception {
    BrokerInterceptorDefinition def = new BrokerInterceptorDefinition();
    def.setDescription("test-broker-listener");
    String archivePath = "/path/to/broker/listener/nar";
    BrokerInterceptorMetadata metadata = new BrokerInterceptorMetadata();
    metadata.setDefinition(def);
    metadata.setArchivePath(Paths.get(archivePath));
    NarClassLoader mockLoader = mock(NarClassLoader.class);
    when(mockLoader.getServiceDefinition(eq(BrokerInterceptorUtils.BROKER_INTERCEPTOR_DEFINITION_FILE))).thenReturn(ObjectMapperFactory.getThreadLocalYaml().writeValueAsString(def));
    Class listenerClass = MockBrokerInterceptor.class;
    when(mockLoader.loadClass(eq(MockBrokerInterceptor.class.getName()))).thenReturn(listenerClass);
    final NarClassLoaderBuilder mockedBuilder = mock(NarClassLoaderBuilder.class, RETURNS_SELF);
    when(mockedBuilder.build()).thenReturn(mockLoader);
    try (MockedStatic<NarClassLoaderBuilder> builder = Mockito.mockStatic(NarClassLoaderBuilder.class)) {
        builder.when(() -> NarClassLoaderBuilder.builder()).thenReturn(mockedBuilder);
        BrokerInterceptorUtils.load(metadata, "");
    }
}
Also used : NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) NarClassLoaderBuilder(org.apache.pulsar.common.nar.NarClassLoaderBuilder) Test(org.testng.annotations.Test)

Example 5 with NarClassLoaderBuilder

use of org.apache.pulsar.common.nar.NarClassLoaderBuilder in project pulsar by apache.

the class ProtocolHandlerUtilsTest method testLoadProtocolHandler.

@Test
public void testLoadProtocolHandler() throws Exception {
    ProtocolHandlerDefinition def = new ProtocolHandlerDefinition();
    def.setHandlerClass(MockProtocolHandler.class.getName());
    def.setDescription("test-protocol-handler");
    String archivePath = "/path/to/protocol/handler/nar";
    ProtocolHandlerMetadata metadata = new ProtocolHandlerMetadata();
    metadata.setDefinition(def);
    metadata.setArchivePath(Paths.get(archivePath));
    NarClassLoader mockLoader = mock(NarClassLoader.class);
    when(mockLoader.getServiceDefinition(eq(PULSAR_PROTOCOL_HANDLER_DEFINITION_FILE))).thenReturn(ObjectMapperFactory.getThreadLocalYaml().writeValueAsString(def));
    Class handlerClass = MockProtocolHandler.class;
    when(mockLoader.loadClass(eq(MockProtocolHandler.class.getName()))).thenReturn(handlerClass);
    final NarClassLoaderBuilder mockedBuilder = mock(NarClassLoaderBuilder.class, RETURNS_SELF);
    when(mockedBuilder.build()).thenReturn(mockLoader);
    try (MockedStatic<NarClassLoaderBuilder> builder = Mockito.mockStatic(NarClassLoaderBuilder.class)) {
        builder.when(() -> NarClassLoaderBuilder.builder()).thenReturn(mockedBuilder);
        ProtocolHandlerWithClassLoader returnedPhWithCL = ProtocolHandlerUtils.load(metadata, "");
        ProtocolHandler returnedPh = returnedPhWithCL.getHandler();
        assertSame(mockLoader, returnedPhWithCL.getClassLoader());
        assertTrue(returnedPh instanceof MockProtocolHandler);
    }
}
Also used : NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) NarClassLoaderBuilder(org.apache.pulsar.common.nar.NarClassLoaderBuilder) Test(org.testng.annotations.Test)

Aggregations

NarClassLoader (org.apache.pulsar.common.nar.NarClassLoader)39 NarClassLoaderBuilder (org.apache.pulsar.common.nar.NarClassLoaderBuilder)39 Test (org.testng.annotations.Test)39 IOException (java.io.IOException)12