Search in sources :

Example 1 with PulsarConfiguration

use of org.apache.pulsar.common.configuration.PulsarConfiguration in project pulsar by yahoo.

the class BrokerAdditionalServletTest method mockAdditionalServlet.

private void mockAdditionalServlet(PulsarService pulsar) {
    Servlet servlet = new OrdinaryServlet();
    AdditionalServlet brokerAdditionalServlet = Mockito.mock(AdditionalServlet.class);
    Mockito.when(brokerAdditionalServlet.getBasePath()).thenReturn(BASE_PATH);
    Mockito.when(brokerAdditionalServlet.getServletHolder()).thenReturn(new ServletHolder(servlet));
    AdditionalServletWithPulsarService brokerAdditionalServletWithPulsarService = new AdditionalServletWithPulsarService() {

        private PulsarService pulsarService;

        @Override
        public void setPulsarService(PulsarService pulsarService) {
            this.pulsarService = pulsarService;
        }

        @Override
        public void loadConfig(PulsarConfiguration pulsarConfiguration) {
        // No-op
        }

        @Override
        public String getBasePath() {
            return WITH_PULSAR_SERVICE_BASE_PATH;
        }

        @Override
        public ServletHolder getServletHolder() {
            return new ServletHolder(new WithPulsarServiceServlet(pulsarService));
        }

        @Override
        public void close() {
        // No-op
        }
    };
    AdditionalServlets brokerAdditionalServlets = Mockito.mock(AdditionalServlets.class);
    Map<String, AdditionalServletWithClassLoader> map = new HashMap<>();
    map.put("broker-additional-servlet", new AdditionalServletWithClassLoader(brokerAdditionalServlet, null));
    map.put("broker-additional-servlet-with-pulsar-service", new AdditionalServletWithClassLoader(brokerAdditionalServletWithPulsarService, null));
    Mockito.when(brokerAdditionalServlets.getServlets()).thenReturn(map);
    Mockito.when(pulsar.getBrokerAdditionalServlets()).thenReturn(brokerAdditionalServlets);
}
Also used : AdditionalServletWithPulsarService(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithPulsarService) HashMap(java.util.HashMap) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) PulsarConfiguration(org.apache.pulsar.common.configuration.PulsarConfiguration) AdditionalServletWithPulsarService(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithPulsarService) AdditionalServlet(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServlet) Servlet(javax.servlet.Servlet) AdditionalServlet(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServlet) AdditionalServletWithClassLoader(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithClassLoader) AdditionalServlets(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServlets)

Example 2 with PulsarConfiguration

use of org.apache.pulsar.common.configuration.PulsarConfiguration in project pulsar by yahoo.

the class AdditionalServletWithClassLoaderTest method testClassLoaderSwitcher.

@Test
public void testClassLoaderSwitcher() throws Exception {
    NarClassLoader narLoader = mock(NarClassLoader.class);
    AdditionalServlet servlet = new AdditionalServlet() {

        @Override
        public void loadConfig(PulsarConfiguration pulsarConfiguration) {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
        }

        @Override
        public String getBasePath() {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
            return "base-path";
        }

        @Override
        public ServletHolder getServletHolder() {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
            return null;
        }

        @Override
        public void close() {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
        }
    };
    AdditionalServletWithClassLoader additionalServletWithClassLoader = new AdditionalServletWithClassLoader(servlet, narLoader);
    ClassLoader curClassLoader = Thread.currentThread().getContextClassLoader();
    // test class loader
    assertEquals(additionalServletWithClassLoader.getClassLoader(), narLoader);
    // test getBasePath
    assertEquals(additionalServletWithClassLoader.getBasePath(), "base-path");
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    // test loadConfig
    ServiceConfiguration conf = new ServiceConfiguration();
    additionalServletWithClassLoader.loadConfig(conf);
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    // test getServletHolder
    assertNull(additionalServletWithClassLoader.getServletHolder());
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    // test getServlet
    assertEquals(additionalServletWithClassLoader.getServlet(), servlet);
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    // test close
    additionalServletWithClassLoader.close();
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
}
Also used : ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) PulsarConfiguration(org.apache.pulsar.common.configuration.PulsarConfiguration) Test(org.testng.annotations.Test)

Example 3 with PulsarConfiguration

use of org.apache.pulsar.common.configuration.PulsarConfiguration in project incubator-pulsar by apache.

the class BrokerAdditionalServletTest method mockAdditionalServlet.

private void mockAdditionalServlet(PulsarService pulsar) {
    Servlet servlet = new OrdinaryServlet();
    AdditionalServlet brokerAdditionalServlet = Mockito.mock(AdditionalServlet.class);
    Mockito.when(brokerAdditionalServlet.getBasePath()).thenReturn(BASE_PATH);
    Mockito.when(brokerAdditionalServlet.getServletHolder()).thenReturn(new ServletHolder(servlet));
    AdditionalServletWithPulsarService brokerAdditionalServletWithPulsarService = new AdditionalServletWithPulsarService() {

        private PulsarService pulsarService;

        @Override
        public void setPulsarService(PulsarService pulsarService) {
            this.pulsarService = pulsarService;
        }

        @Override
        public void loadConfig(PulsarConfiguration pulsarConfiguration) {
        // No-op
        }

        @Override
        public String getBasePath() {
            return WITH_PULSAR_SERVICE_BASE_PATH;
        }

        @Override
        public ServletHolder getServletHolder() {
            return new ServletHolder(new WithPulsarServiceServlet(pulsarService));
        }

        @Override
        public void close() {
        // No-op
        }
    };
    AdditionalServlets brokerAdditionalServlets = Mockito.mock(AdditionalServlets.class);
    Map<String, AdditionalServletWithClassLoader> map = new HashMap<>();
    map.put("broker-additional-servlet", new AdditionalServletWithClassLoader(brokerAdditionalServlet, null));
    map.put("broker-additional-servlet-with-pulsar-service", new AdditionalServletWithClassLoader(brokerAdditionalServletWithPulsarService, null));
    Mockito.when(brokerAdditionalServlets.getServlets()).thenReturn(map);
    Mockito.when(pulsar.getBrokerAdditionalServlets()).thenReturn(brokerAdditionalServlets);
}
Also used : AdditionalServletWithPulsarService(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithPulsarService) HashMap(java.util.HashMap) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) PulsarConfiguration(org.apache.pulsar.common.configuration.PulsarConfiguration) AdditionalServletWithPulsarService(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithPulsarService) AdditionalServlet(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServlet) Servlet(javax.servlet.Servlet) AdditionalServlet(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServlet) AdditionalServletWithClassLoader(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithClassLoader) AdditionalServlets(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServlets)

Example 4 with PulsarConfiguration

use of org.apache.pulsar.common.configuration.PulsarConfiguration in project incubator-pulsar by apache.

the class AdditionalServletWithClassLoaderTest method testClassLoaderSwitcher.

@Test
public void testClassLoaderSwitcher() throws Exception {
    NarClassLoader narLoader = mock(NarClassLoader.class);
    AdditionalServlet servlet = new AdditionalServlet() {

        @Override
        public void loadConfig(PulsarConfiguration pulsarConfiguration) {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
        }

        @Override
        public String getBasePath() {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
            return "base-path";
        }

        @Override
        public ServletHolder getServletHolder() {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
            return null;
        }

        @Override
        public void close() {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
        }
    };
    AdditionalServletWithClassLoader additionalServletWithClassLoader = new AdditionalServletWithClassLoader(servlet, narLoader);
    ClassLoader curClassLoader = Thread.currentThread().getContextClassLoader();
    // test class loader
    assertEquals(additionalServletWithClassLoader.getClassLoader(), narLoader);
    // test getBasePath
    assertEquals(additionalServletWithClassLoader.getBasePath(), "base-path");
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    // test loadConfig
    ServiceConfiguration conf = new ServiceConfiguration();
    additionalServletWithClassLoader.loadConfig(conf);
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    // test getServletHolder
    assertNull(additionalServletWithClassLoader.getServletHolder());
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    // test getServlet
    assertEquals(additionalServletWithClassLoader.getServlet(), servlet);
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    // test close
    additionalServletWithClassLoader.close();
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
}
Also used : ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) PulsarConfiguration(org.apache.pulsar.common.configuration.PulsarConfiguration) Test(org.testng.annotations.Test)

Example 5 with PulsarConfiguration

use of org.apache.pulsar.common.configuration.PulsarConfiguration in project pulsar by apache.

the class BrokerAdditionalServletTest method mockAdditionalServlet.

private void mockAdditionalServlet(PulsarService pulsar) {
    Servlet servlet = new OrdinaryServlet();
    AdditionalServlet brokerAdditionalServlet = Mockito.mock(AdditionalServlet.class);
    Mockito.when(brokerAdditionalServlet.getBasePath()).thenReturn(BASE_PATH);
    Mockito.when(brokerAdditionalServlet.getServletHolder()).thenReturn(new ServletHolder(servlet));
    AdditionalServletWithPulsarService brokerAdditionalServletWithPulsarService = new AdditionalServletWithPulsarService() {

        private PulsarService pulsarService;

        @Override
        public void setPulsarService(PulsarService pulsarService) {
            this.pulsarService = pulsarService;
        }

        @Override
        public void loadConfig(PulsarConfiguration pulsarConfiguration) {
        // No-op
        }

        @Override
        public String getBasePath() {
            return WITH_PULSAR_SERVICE_BASE_PATH;
        }

        @Override
        public ServletHolder getServletHolder() {
            return new ServletHolder(new WithPulsarServiceServlet(pulsarService));
        }

        @Override
        public void close() {
        // No-op
        }
    };
    AdditionalServlets brokerAdditionalServlets = Mockito.mock(AdditionalServlets.class);
    Map<String, AdditionalServletWithClassLoader> map = new HashMap<>();
    map.put("broker-additional-servlet", new AdditionalServletWithClassLoader(brokerAdditionalServlet, null));
    map.put("broker-additional-servlet-with-pulsar-service", new AdditionalServletWithClassLoader(brokerAdditionalServletWithPulsarService, null));
    Mockito.when(brokerAdditionalServlets.getServlets()).thenReturn(map);
    Mockito.when(pulsar.getBrokerAdditionalServlets()).thenReturn(brokerAdditionalServlets);
}
Also used : AdditionalServletWithPulsarService(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithPulsarService) HashMap(java.util.HashMap) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) PulsarConfiguration(org.apache.pulsar.common.configuration.PulsarConfiguration) AdditionalServletWithPulsarService(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithPulsarService) AdditionalServlet(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServlet) Servlet(javax.servlet.Servlet) AdditionalServlet(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServlet) AdditionalServletWithClassLoader(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithClassLoader) AdditionalServlets(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServlets)

Aggregations

PulsarConfiguration (org.apache.pulsar.common.configuration.PulsarConfiguration)6 HashMap (java.util.HashMap)3 Servlet (javax.servlet.Servlet)3 ServiceConfiguration (org.apache.pulsar.broker.ServiceConfiguration)3 AdditionalServlet (org.apache.pulsar.broker.web.plugin.servlet.AdditionalServlet)3 AdditionalServletWithClassLoader (org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithClassLoader)3 AdditionalServletWithPulsarService (org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithPulsarService)3 AdditionalServlets (org.apache.pulsar.broker.web.plugin.servlet.AdditionalServlets)3 NarClassLoader (org.apache.pulsar.common.nar.NarClassLoader)3 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)3 Test (org.testng.annotations.Test)3