Search in sources :

Example 16 with HttpServiceRuntime

use of org.osgi.service.http.runtime.HttpServiceRuntime in project felix by apache.

the class HttpServiceRuntimeTest method hiddenDefaultContextAppearsAsFailure.

// As specified in OSGi Compendium Release 6, Chapter 140.1 (TODO : exact version)
@Test
public void hiddenDefaultContextAppearsAsFailure() throws InterruptedException {
    registerContext("default", "");
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
    assertEquals(1, runtimeDTO.failedServletContextDTOs.length);
    assertEquals("default", runtimeDTO.failedServletContextDTOs[0].name);
    assertDefaultContext(runtimeDTO);
}
Also used : HttpServiceRuntime(org.osgi.service.http.runtime.HttpServiceRuntime) RuntimeDTO(org.osgi.service.http.runtime.dto.RuntimeDTO) Test(org.junit.Test)

Example 17 with HttpServiceRuntime

use of org.osgi.service.http.runtime.HttpServiceRuntime in project felix by apache.

the class HttpServiceRuntimeTest method exceptionInServletInitDuringServletRemovalAppearsAsFailure.

@Test
public void exceptionInServletInitDuringServletRemovalAppearsAsFailure() throws ServletException, InterruptedException {
    Dictionary<String, ?> properties1 = createDictionary(HTTP_WHITEBOARD_SERVLET_PATTERN, "/servlet1", HTTP_WHITEBOARD_SERVLET_NAME, "servlet1");
    final CountDownLatch initLatch1 = new CountDownLatch(1);
    @SuppressWarnings("serial") Servlet failingServlet1 = new TestServlet(initLatch1, null) {

        @Override
        public void init() throws ServletException {
            // fail when initialized the second time
            if (initLatch1.getCount() == 0) {
                throw new ServletException();
            }
            super.init();
        }
    };
    Dictionary<String, ?> properties2 = createDictionary(HTTP_WHITEBOARD_SERVLET_PATTERN, "/servlet2", HTTP_WHITEBOARD_SERVLET_NAME, "servlet2");
    final CountDownLatch initLatch2 = new CountDownLatch(1);
    @SuppressWarnings("serial") Servlet failingServlet2 = new TestServlet(initLatch2, null) {

        @Override
        public void init() throws ServletException {
            // fail when initialized the second time
            if (initLatch2.getCount() == 0) {
                throw new ServletException();
            }
            super.init();
        }
    };
    Dictionary<String, ?> propertiesShadowing = createDictionary(HTTP_WHITEBOARD_SERVLET_PATTERN, asList("/servlet1", "/servlet2"), HTTP_WHITEBOARD_SERVLET_NAME, "servletShadowing", SERVICE_RANKING, Integer.MAX_VALUE);
    CountDownLatch initLatchShadowing = new CountDownLatch(1);
    Servlet servletShadowing = new TestServlet(initLatchShadowing, null);
    registrations.add(m_context.registerService(Servlet.class.getName(), failingServlet1, properties1));
    registrations.add(m_context.registerService(Servlet.class.getName(), failingServlet2, properties2));
    awaitServiceRegistration(initLatch1);
    awaitServiceRegistration(initLatch2);
    ServiceRegistration<?> shadowingRegistration = m_context.registerService(Servlet.class.getName(), servletShadowing, propertiesShadowing);
    registrations.add(shadowingRegistration);
    awaitServiceRegistration(initLatchShadowing);
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
    assertEquals(2, runtimeDTO.failedServletDTOs.length);
    assertEquals(FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE, runtimeDTO.failedServletDTOs[0].failureReason);
    assertEquals(FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE, runtimeDTO.failedServletDTOs[1].failureReason);
    shadowingRegistration.unregister();
    runtimeDTO = serviceRuntime.getRuntimeDTO();
    assertEquals(2, runtimeDTO.failedServletDTOs.length);
    assertEquals(FAILURE_REASON_EXCEPTION_ON_INIT, runtimeDTO.failedServletDTOs[0].failureReason);
    assertEquals(FAILURE_REASON_EXCEPTION_ON_INIT, runtimeDTO.failedServletDTOs[1].failureReason);
}
Also used : ServletException(javax.servlet.ServletException) HttpServiceRuntime(org.osgi.service.http.runtime.HttpServiceRuntime) Servlet(javax.servlet.Servlet) RuntimeDTO(org.osgi.service.http.runtime.dto.RuntimeDTO) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 18 with HttpServiceRuntime

use of org.osgi.service.http.runtime.HttpServiceRuntime in project felix by apache.

the class HttpServiceRuntimeTest method invalidContextHelperPathAppearsAsFailure.

// As specified in OSGi Compendium Release 6, Chapter 140.1
@Test
public void invalidContextHelperPathAppearsAsFailure() throws InterruptedException {
    registerContext("contextA", "#");
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
    assertEquals(1, runtimeDTO.failedServletContextDTOs.length);
    assertEquals("#", runtimeDTO.failedServletContextDTOs[0].contextPath);
    assertEquals(FAILURE_REASON_VALIDATION_FAILED, runtimeDTO.failedServletContextDTOs[0].failureReason);
}
Also used : HttpServiceRuntime(org.osgi.service.http.runtime.HttpServiceRuntime) RuntimeDTO(org.osgi.service.http.runtime.dto.RuntimeDTO) Test(org.junit.Test)

Example 19 with HttpServiceRuntime

use of org.osgi.service.http.runtime.HttpServiceRuntime in project felix by apache.

the class HttpServiceRuntimeTest method testCombinedServletAndResourceRegistration.

/**
 * Test for FELIX-5319
 * @throws Exception
 */
@Test
public void testCombinedServletAndResourceRegistration() throws Exception {
    // register single component as Servlet and Resource
    final String servletPath = "/hello/sayHello";
    final String servletName = "Hello World";
    final String rsrcPattern = "/hello/static/*";
    final String rsrcPrefix = "/static";
    CountDownLatch initLatch = new CountDownLatch(1);
    List<Object> propertyEntries = Arrays.<Object>asList(HTTP_WHITEBOARD_SERVLET_PATTERN, servletPath, HTTP_WHITEBOARD_SERVLET_NAME, servletName, HTTP_WHITEBOARD_RESOURCE_PATTERN, rsrcPattern, HTTP_WHITEBOARD_RESOURCE_PREFIX, rsrcPrefix);
    Dictionary<String, ?> properties = createDictionary(propertyEntries.toArray());
    registrations.add(m_context.registerService(Servlet.class.getName(), new TestServlet(initLatch, null), properties));
    awaitServiceRegistration(initLatch);
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
    assertEquals(0, runtimeDTO.failedServletDTOs.length);
    assertEquals(0, runtimeDTO.failedResourceDTOs.length);
    // check servlet registration
    ServletContextDTO contextDTO = assertDefaultContext(runtimeDTO);
    assertEquals(1, contextDTO.servletDTOs.length);
    assertEquals(servletName, contextDTO.servletDTOs[0].name);
    assertEquals(1, contextDTO.servletDTOs[0].patterns.length);
    assertEquals(servletPath, contextDTO.servletDTOs[0].patterns[0]);
    // check resource registration
    assertEquals(1, contextDTO.resourceDTOs.length);
    assertEquals(1, contextDTO.resourceDTOs[0].patterns.length);
    assertEquals(rsrcPattern, contextDTO.resourceDTOs[0].patterns[0]);
    assertEquals(rsrcPrefix, contextDTO.resourceDTOs[0].prefix);
}
Also used : HttpServiceRuntime(org.osgi.service.http.runtime.HttpServiceRuntime) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) RuntimeDTO(org.osgi.service.http.runtime.dto.RuntimeDTO) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 20 with HttpServiceRuntime

use of org.osgi.service.http.runtime.HttpServiceRuntime in project felix by apache.

the class JettyServiceTest method setUp.

@Before
public void setUp() throws Exception {
    // Setup Mocks
    mockBundleContext = mock(BundleContext.class);
    mockBundle = mock(Bundle.class);
    // Setup Behaviors
    when(mockBundleContext.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.getSymbolicName()).thenReturn("main");
    when(mockBundle.getVersion()).thenReturn(new Version("1.0.0"));
    when(mockBundle.getHeaders()).thenReturn(new Hashtable<String, String>());
    final ServiceReference ref = mock(ServiceReference.class);
    when(ref.getProperty(Constants.SERVICE_ID)).thenReturn(1L);
    final ServiceRegistration reg = mock(ServiceRegistration.class);
    when(reg.getReference()).thenReturn(ref);
    when(mockBundleContext.registerService((Class<ServletContextHelper>) Matchers.isNotNull(), (ServiceFactory<ServletContextHelper>) Matchers.any(ServiceFactory.class), Matchers.any(Dictionary.class))).thenReturn(reg);
    when(mockBundleContext.registerService(Matchers.<String[]>any(), Matchers.any(ServiceFactory.class), Matchers.any(Dictionary.class))).thenReturn(reg);
    when(mockBundleContext.registerService((Class<HttpServiceRuntime>) Matchers.isNotNull(), Matchers.any(HttpServiceRuntime.class), Matchers.any(Dictionary.class))).thenReturn(reg);
    httpServiceController = new HttpServiceController(mockBundleContext);
    jettyService = new JettyService(mockBundleContext, httpServiceController);
    jettyService.start();
}
Also used : Dictionary(java.util.Dictionary) ServiceFactory(org.osgi.framework.ServiceFactory) HttpServiceRuntime(org.osgi.service.http.runtime.HttpServiceRuntime) HttpServiceController(org.apache.felix.http.base.internal.HttpServiceController) Bundle(org.osgi.framework.Bundle) Matchers.anyString(org.mockito.Matchers.anyString) ServiceReference(org.osgi.framework.ServiceReference) ServletContextHelper(org.osgi.service.http.context.ServletContextHelper) Version(org.osgi.framework.Version) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) Before(org.junit.Before)

Aggregations

HttpServiceRuntime (org.osgi.service.http.runtime.HttpServiceRuntime)34 Test (org.junit.Test)32 RuntimeDTO (org.osgi.service.http.runtime.dto.RuntimeDTO)31 ServletContextDTO (org.osgi.service.http.runtime.dto.ServletContextDTO)20 CountDownLatch (java.util.concurrent.CountDownLatch)12 ServletException (javax.servlet.ServletException)5 Servlet (javax.servlet.Servlet)4 HashSet (java.util.HashSet)2 NoSuchElementException (java.util.NoSuchElementException)2 ServletRequestListener (javax.servlet.ServletRequestListener)2 ServletContextHelper (org.osgi.service.http.context.ServletContextHelper)2 FailedServletDTO (org.osgi.service.http.runtime.dto.FailedServletDTO)2 RequestInfoDTO (org.osgi.service.http.runtime.dto.RequestInfoDTO)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Dictionary (java.util.Dictionary)1 Filter (javax.servlet.Filter)1 FilterConfig (javax.servlet.FilterConfig)1 ServletContextAttributeListener (javax.servlet.ServletContextAttributeListener)1