HttpServiceUtil { public static int getHttpServicePort(final BundleContext bc) { return getHttpServicePortProperty(bc, "org.osgi.service.http.port"); } private  HttpServiceUtil(); static int getHttpServicePort(final BundleContext bc); static int getHttpServicePortSecure(final BundleContext bc);  }
@Test public void shouldReturnHttpServicePortFromServiceReference() { int port = HttpServiceUtil.getHttpServicePort(bundleContext); assertThat(port, is(8080)); }
@Test public void shouldReturnHttpServicePortFromSystemProperty() throws InvalidSyntaxException { when(bundleContext.getAllServiceReferences(ORG_OSGI_SERVICE_HTTP_SERVICE, null)) .thenReturn(new ServiceReference[0]); when(bundleContext.getProperty(HTTP_PORT)).thenReturn("9090"); int port = HttpServiceUtil.getHttpServicePort(bundleContext); assertThat(port, is(9090)); }
@Test public void shouldReturnUndefinedForException() throws InvalidSyntaxException { when(bundleContext.getAllServiceReferences(ORG_OSGI_SERVICE_HTTP_SERVICE, null)) .thenThrow(new InvalidSyntaxException(null, null)); int undfinedPort = HttpServiceUtil.getHttpServicePort(bundleContext); assertThat(undfinedPort, is(-1)); }