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