Search in sources :

Example 6 with ComponentContext

use of org.osgi.service.component.ComponentContext in project jackrabbit-oak by apache.

the class OsgiUtilTest method testComponentLookupWithStringValue.

@Test
public void testComponentLookupWithStringValue() {
    Dictionary properties = mock(Dictionary.class);
    doReturn("  value   ").when(properties).get("name");
    ComponentContext context = mock(ComponentContext.class);
    doReturn(properties).when(context).getProperties();
    assertEquals("value", lookup(context, "name"));
}
Also used : Dictionary(java.util.Dictionary) ComponentContext(org.osgi.service.component.ComponentContext) Test(org.junit.Test)

Example 7 with ComponentContext

use of org.osgi.service.component.ComponentContext in project jackrabbit-oak by apache.

the class OsgiUtilTest method testFallbackLookupWithValueInComponent.

@Test
public void testFallbackLookupWithValueInComponent() {
    Dictionary dictionary = mock(Dictionary.class);
    doReturn("value").when(dictionary).get("cname");
    BundleContext bundleContext = mock(BundleContext.class);
    doReturn(null).when(bundleContext).getProperty("fname");
    ComponentContext componentContext = mock(ComponentContext.class);
    doReturn(dictionary).when(componentContext).getProperties();
    doReturn(bundleContext).when(componentContext).getBundleContext();
    assertEquals("value", lookupConfigurationThenFramework(componentContext, "cname", "fname"));
    assertEquals("value", lookupFrameworkThenConfiguration(componentContext, "cname", "fname"));
}
Also used : Dictionary(java.util.Dictionary) ComponentContext(org.osgi.service.component.ComponentContext) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 8 with ComponentContext

use of org.osgi.service.component.ComponentContext in project sling by apache.

the class AdapterManagerTest method createComponentContext.

/**
     * Helper method to create a mock component context
     */
protected ComponentContext createComponentContext() throws Exception {
    final BundleContext bundleCtx = this.context.mock(BundleContext.class);
    final Filter filter = this.context.mock(Filter.class);
    final ComponentContext ctx = this.context.mock(ComponentContext.class);
    this.context.checking(new Expectations() {

        {
            allowing(ctx).locateService(with(any(String.class)), with(any(ServiceReference.class)));
            will(returnValue(new MockAdapterFactory()));
            allowing(ctx).getBundleContext();
            will(returnValue(bundleCtx));
            allowing(bundleCtx).createFilter(with(any(String.class)));
            will(returnValue(filter));
            allowing(bundleCtx).addServiceListener(with(any(ServiceListener.class)), with(any(String.class)));
            allowing(bundleCtx).getServiceReferences(with(any(String.class)), with(any(String.class)));
            will(returnValue(null));
            allowing(bundleCtx).removeServiceListener(with(any(ServiceListener.class)));
            allowing(bundleCtx).registerService(with(Adaption.class), with(AdaptionImpl.INSTANCE), with(any(Dictionary.class)));
            will(returnValue(null));
        }
    });
    return ctx;
}
Also used : Expectations(org.jmock.Expectations) Dictionary(java.util.Dictionary) ServiceListener(org.osgi.framework.ServiceListener) ComponentContext(org.osgi.service.component.ComponentContext) Filter(org.osgi.framework.Filter) MockAdapterFactory(org.apache.sling.adapter.mock.MockAdapterFactory) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Example 9 with ComponentContext

use of org.osgi.service.component.ComponentContext in project sling by apache.

the class MockFactory method mockComponentContext.

public static ComponentContext mockComponentContext() {
    Mockery context = new JUnit4Mockery();
    final BundleContext bc = context.mock(BundleContext.class);
    context.checking(new Expectations() {

        {
            allowing(bc).registerService(with(any(String.class)), with(any(Object.class)), with(any(Dictionary.class)));
            will(VoidAction.INSTANCE);
            allowing(bc).getProperty(with(any(String.class)));
            will(new ReturnValueAction("foo"));
        }
    });
    final ComponentContext cc = context.mock(ComponentContext.class);
    context.checking(new Expectations() {

        {
            allowing(cc).getProperties();
            will(returnValue(new Properties()));
            allowing(cc).getBundleContext();
            will(returnValue(bc));
        }
    });
    return cc;
}
Also used : Expectations(org.jmock.Expectations) Dictionary(java.util.Dictionary) ReturnValueAction(org.jmock.lib.action.ReturnValueAction) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) ComponentContext(org.osgi.service.component.ComponentContext) Properties(java.util.Properties) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) Mockery(org.jmock.Mockery) BundleContext(org.osgi.framework.BundleContext)

Example 10 with ComponentContext

use of org.osgi.service.component.ComponentContext in project sling by apache.

the class VirtualInstance method startJetty.

public synchronized void startJetty() throws Throwable {
    if (jettyServer != null) {
        return;
    }
    servletContext = new ServletContextHandler(ServletContextHandler.NO_SECURITY);
    servletContext.setContextPath("/");
    TopologyConnectorServlet servlet = new TopologyConnectorServlet();
    PrivateAccessor.setField(servlet, "config", config);
    PrivateAccessor.setField(servlet, "clusterViewService", clusterViewService);
    PrivateAccessor.setField(servlet, "announcementRegistry", announcementRegistry);
    Mockery context = new JUnit4Mockery();
    final HttpService httpService = context.mock(HttpService.class);
    context.checking(new Expectations() {

        {
            allowing(httpService).registerServlet(with(any(String.class)), with(any(Servlet.class)), with(any(Dictionary.class)), with(any(HttpContext.class)));
        }
    });
    PrivateAccessor.setField(servlet, "httpService", httpService);
    ComponentContext cc = null;
    PrivateAccessor.invoke(servlet, "activate", new Class[] { ComponentContext.class }, new Object[] { cc });
    ServletHolder holder = new ServletHolder(servlet);
    servletContext.addServlet(holder, "/system/console/topology/*");
    jettyServer = new Server();
    jettyServer.setHandler(servletContext);
    Connector connector = new SelectChannelConnector();
    jettyServer.setConnectors(new Connector[] { connector });
    jettyServer.start();
}
Also used : Expectations(org.jmock.Expectations) Dictionary(java.util.Dictionary) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) Connector(org.eclipse.jetty.server.Connector) ComponentContext(org.osgi.service.component.ComponentContext) Server(org.eclipse.jetty.server.Server) TopologyConnectorServlet(org.apache.sling.discovery.base.connectors.ping.TopologyConnectorServlet) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpContext(org.osgi.service.http.HttpContext) Mockery(org.jmock.Mockery) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) HttpService(org.osgi.service.http.HttpService) TopologyConnectorServlet(org.apache.sling.discovery.base.connectors.ping.TopologyConnectorServlet) Servlet(javax.servlet.Servlet) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Aggregations

ComponentContext (org.osgi.service.component.ComponentContext)35 Test (org.junit.Test)16 Dictionary (java.util.Dictionary)14 BundleContext (org.osgi.framework.BundleContext)11 Hashtable (java.util.Hashtable)5 Expectations (org.jmock.Expectations)4 HashMap (java.util.HashMap)3 ContentItem (org.apache.stanbol.enhancer.servicesapi.ContentItem)3 StringSource (org.apache.stanbol.enhancer.servicesapi.impl.StringSource)3 BeforeClass (org.junit.BeforeClass)3 ServiceReference (org.osgi.framework.ServiceReference)3 IRI (org.apache.clerezza.commons.rdf.IRI)2 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)2 LuceneLabelTokenizer (org.apache.stanbol.enhancer.engines.entitylinking.labeltokenizer.lucene.LuceneLabelTokenizer)2 Mockery (org.jmock.Mockery)2 JUnit4Mockery (org.jmock.integration.junit4.JUnit4Mockery)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 InputStream (java.io.InputStream)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1