Search in sources :

Example 26 with NarClassLoader

use of org.apache.pulsar.common.nar.NarClassLoader in project pulsar by yahoo.

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 27 with NarClassLoader

use of org.apache.pulsar.common.nar.NarClassLoader in project pulsar by yahoo.

the class ProxyExtensionUtilsTest method testLoadProtocolHandlerBlankHandlerClass.

@Test
public void testLoadProtocolHandlerBlankHandlerClass() throws Exception {
    ProxyExtensionDefinition def = new ProxyExtensionDefinition();
    def.setDescription("test-ext");
    String archivePath = "/path/to/ext/nar";
    ProxyExtensionMetadata metadata = new ProxyExtensionMetadata();
    metadata.setDefinition(def);
    metadata.setArchivePath(Paths.get(archivePath));
    NarClassLoader mockLoader = mock(NarClassLoader.class);
    when(mockLoader.getServiceDefinition(eq(PROXY_EXTENSION_DEFINITION_FILE))).thenReturn(ObjectMapperFactory.getThreadLocalYaml().writeValueAsString(def));
    Class handlerClass = MockProxyExtension.class;
    when(mockLoader.loadClass(eq(MockProxyExtension.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);
        try {
            ProxyExtensionsUtils.load(metadata, "");
            fail("Should not reach here");
        } catch (IOException ioe) {
        // expected
        }
    }
}
Also used : NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) IOException(java.io.IOException) NarClassLoaderBuilder(org.apache.pulsar.common.nar.NarClassLoaderBuilder) Test(org.testng.annotations.Test)

Example 28 with NarClassLoader

use of org.apache.pulsar.common.nar.NarClassLoader in project pulsar by yahoo.

the class ProxyExtensionUtilsTest method testLoadProtocolHandlerWrongHandlerClass.

@Test
public void testLoadProtocolHandlerWrongHandlerClass() throws Exception {
    ProxyExtensionDefinition def = new ProxyExtensionDefinition();
    def.setExtensionClass(Runnable.class.getName());
    def.setDescription("test-ext");
    String archivePath = "/path/to/ext/nar";
    ProxyExtensionMetadata metadata = new ProxyExtensionMetadata();
    metadata.setDefinition(def);
    metadata.setArchivePath(Paths.get(archivePath));
    NarClassLoader mockLoader = mock(NarClassLoader.class);
    when(mockLoader.getServiceDefinition(eq(PROXY_EXTENSION_DEFINITION_FILE))).thenReturn(ObjectMapperFactory.getThreadLocalYaml().writeValueAsString(def));
    Class handlerClass = Runnable.class;
    when(mockLoader.loadClass(eq(Runnable.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);
        try {
            ProxyExtensionsUtils.load(metadata, "");
            fail("Should not reach here");
        } catch (IOException ioe) {
        // expected
        }
    }
}
Also used : NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) IOException(java.io.IOException) NarClassLoaderBuilder(org.apache.pulsar.common.nar.NarClassLoaderBuilder) Test(org.testng.annotations.Test)

Example 29 with NarClassLoader

use of org.apache.pulsar.common.nar.NarClassLoader in project pulsar by yahoo.

the class ProxyExtensionWithClassLoaderTest method testWrapper.

@Test
public void testWrapper() throws Exception {
    ProxyExtension h = mock(ProxyExtension.class);
    NarClassLoader loader = mock(NarClassLoader.class);
    ProxyExtensionWithClassLoader wrapper = new ProxyExtensionWithClassLoader(h, loader);
    String protocol = "kafka";
    when(h.extensionName()).thenReturn(protocol);
    assertEquals(protocol, wrapper.extensionName());
    verify(h, times(1)).extensionName();
    when(h.accept(eq(protocol))).thenReturn(true);
    assertTrue(wrapper.accept(protocol));
    verify(h, times(1)).accept(same(protocol));
    ProxyConfiguration conf = new ProxyConfiguration();
    wrapper.initialize(conf);
    verify(h, times(1)).initialize(same(conf));
    ProxyService service = mock(ProxyService.class);
    wrapper.start(service);
    verify(h, times(1)).start(service);
}
Also used : ProxyConfiguration(org.apache.pulsar.proxy.server.ProxyConfiguration) ProxyService(org.apache.pulsar.proxy.server.ProxyService) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) Test(org.testng.annotations.Test)

Example 30 with NarClassLoader

use of org.apache.pulsar.common.nar.NarClassLoader in project pulsar by yahoo.

the class ProxyExtensionWithClassLoaderTest method testClassLoaderSwitcher.

@Test
public void testClassLoaderSwitcher() throws Exception {
    NarClassLoader loader = mock(NarClassLoader.class);
    String protocol = "test-protocol";
    ProxyExtension h = new ProxyExtension() {

        @Override
        public String extensionName() {
            assertEquals(Thread.currentThread().getContextClassLoader(), loader);
            return protocol;
        }

        @Override
        public boolean accept(String protocol) {
            assertEquals(Thread.currentThread().getContextClassLoader(), loader);
            return true;
        }

        @Override
        public void initialize(ProxyConfiguration conf) throws Exception {
            assertEquals(Thread.currentThread().getContextClassLoader(), loader);
            throw new Exception("test exception");
        }

        @Override
        public void start(ProxyService service) {
            assertEquals(Thread.currentThread().getContextClassLoader(), loader);
        }

        @Override
        public Map<InetSocketAddress, ChannelInitializer<SocketChannel>> newChannelInitializers() {
            assertEquals(Thread.currentThread().getContextClassLoader(), loader);
            return null;
        }

        @Override
        public void close() {
            assertEquals(Thread.currentThread().getContextClassLoader(), loader);
        }
    };
    ProxyExtensionWithClassLoader wrapper = new ProxyExtensionWithClassLoader(h, loader);
    ClassLoader curClassLoader = Thread.currentThread().getContextClassLoader();
    assertEquals(wrapper.extensionName(), protocol);
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    assertTrue(wrapper.accept(protocol));
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    ProxyConfiguration conf = new ProxyConfiguration();
    expectThrows(Exception.class, () -> wrapper.initialize(conf));
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    ProxyService service = mock(ProxyService.class);
    wrapper.start(service);
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    assertNull(wrapper.newChannelInitializers());
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    wrapper.close();
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
}
Also used : ProxyConfiguration(org.apache.pulsar.proxy.server.ProxyConfiguration) ProxyService(org.apache.pulsar.proxy.server.ProxyService) InetSocketAddress(java.net.InetSocketAddress) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) ChannelInitializer(io.netty.channel.ChannelInitializer) Test(org.testng.annotations.Test)

Aggregations

NarClassLoader (org.apache.pulsar.common.nar.NarClassLoader)126 Test (org.testng.annotations.Test)84 IOException (java.io.IOException)53 NarClassLoaderBuilder (org.apache.pulsar.common.nar.NarClassLoaderBuilder)48 File (java.io.File)39 FileInputStream (java.io.FileInputStream)15 UtilityClass (lombok.experimental.UtilityClass)15 Namespace (org.apache.distributedlog.api.namespace.Namespace)15 HashMap (java.util.HashMap)14 Map (java.util.Map)12 ServiceConfiguration (org.apache.pulsar.broker.ServiceConfiguration)12 Lists (com.google.common.collect.Lists)9 InputStream (java.io.InputStream)9 Files (java.nio.file.Files)9 Paths (java.nio.file.Paths)9 StandardCopyOption (java.nio.file.StandardCopyOption)9 LinkedList (java.util.LinkedList)9 List (java.util.List)9 Consumer (java.util.function.Consumer)9 Response (javax.ws.rs.core.Response)9