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, "");
}
}
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
}
}
}
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
}
}
}
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);
}
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);
}
Aggregations