Search in sources :

Example 1 with Deployment

use of org.apache.felix.http.jetty.internal.JettyService.Deployment in project felix by apache.

the class JettyServiceTest method testInitBundleContextDeployIT.

/**
 * Tests to ensure the osgi-bundlecontext is available for init methods.
 *
 * @throws MalformedURLException
 * @throws InterruptedException
 */
@Test
public void testInitBundleContextDeployIT() throws Exception {
    // Setup mocks
    Deployment mockDeployment = mock(Deployment.class);
    Bundle mockBundle = mock(Bundle.class);
    BundleContext mockBundleContext = mock(BundleContext.class);
    // Setup behaviors
    when(mockDeployment.getBundle()).thenReturn(mockBundle);
    final org.osgi.framework.Filter f = mock(org.osgi.framework.Filter.class);
    when(f.toString()).thenReturn("(prop=*)");
    when(mockBundleContext.createFilter(anyString())).thenReturn(f);
    when(mockBundle.getBundleContext()).thenReturn(mockBundleContext);
    when(mockBundle.getSymbolicName()).thenReturn("test");
    when(mockBundle.getVersion()).thenReturn(new Version("0.0.1"));
    Dictionary<String, String> headerProperties = new Hashtable<String, String>();
    headerProperties.put("Web-ContextPath", "test");
    when(mockBundle.getHeaders()).thenReturn(headerProperties);
    when(mockDeployment.getContextPath()).thenReturn("test");
    when(mockBundle.getEntry("/")).thenReturn(new URL("http://www.apache.com"));
    when(mockBundle.getState()).thenReturn(Bundle.ACTIVE);
    EnumSet<DispatcherType> dispatcherSet = EnumSet.allOf(DispatcherType.class);
    dispatcherSet.add(DispatcherType.REQUEST);
    WebAppBundleContext webAppBundleContext = new WebAppBundleContext("/", mockBundle, this.getClass().getClassLoader());
    final CountDownLatch testLatch = new CountDownLatch(2);
    // Add a Filter to test whether the osgi-bundlecontext is available at init
    webAppBundleContext.addServlet(new ServletHolder(new Servlet() {

        @Override
        public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
        // Do Nothing
        }

        @Override
        public void init(ServletConfig config) throws ServletException {
            ServletContext context = config.getServletContext();
            assertNotNull(context.getAttribute(OSGI_BUNDLECONTEXT));
            testLatch.countDown();
        }

        @Override
        public String getServletInfo() {
            return null;
        }

        @Override
        public ServletConfig getServletConfig() {
            return null;
        }

        @Override
        public void destroy() {
        // Do Nothing
        }
    }), "/test1");
    webAppBundleContext.addFilter(new FilterHolder(new Filter() {

        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            ServletContext context = filterConfig.getServletContext();
            assertNotNull(context.getAttribute(OSGI_BUNDLECONTEXT));
            testLatch.countDown();
        }

        @Override
        public void doFilter(ServletRequest arg0, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // Do Nothing
        }

        @Override
        public void destroy() {
        // Do Nothing
        }
    }), "/test2", dispatcherSet);
    jettyService.deploy(mockDeployment, webAppBundleContext);
    // Fail if takes too long.
    if (!testLatch.await(10, TimeUnit.SECONDS)) {
        fail("Test Was not asserted");
    }
}
Also used : ServletRequest(javax.servlet.ServletRequest) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) FilterChain(javax.servlet.FilterChain) Deployment(org.apache.felix.http.jetty.internal.JettyService.Deployment) Matchers.anyString(org.mockito.Matchers.anyString) URL(java.net.URL) Version(org.osgi.framework.Version) Servlet(javax.servlet.Servlet) ServletContext(javax.servlet.ServletContext) FilterConfig(javax.servlet.FilterConfig) DispatcherType(javax.servlet.DispatcherType) ServletResponse(javax.servlet.ServletResponse) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) ServletConfig(javax.servlet.ServletConfig) CountDownLatch(java.util.concurrent.CountDownLatch) Filter(javax.servlet.Filter) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Aggregations

URL (java.net.URL)1 Hashtable (java.util.Hashtable)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 DispatcherType (javax.servlet.DispatcherType)1 Filter (javax.servlet.Filter)1 FilterChain (javax.servlet.FilterChain)1 FilterConfig (javax.servlet.FilterConfig)1 Servlet (javax.servlet.Servlet)1 ServletConfig (javax.servlet.ServletConfig)1 ServletContext (javax.servlet.ServletContext)1 ServletRequest (javax.servlet.ServletRequest)1 ServletResponse (javax.servlet.ServletResponse)1 Deployment (org.apache.felix.http.jetty.internal.JettyService.Deployment)1 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)1 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1 Bundle (org.osgi.framework.Bundle)1 BundleContext (org.osgi.framework.BundleContext)1 Version (org.osgi.framework.Version)1