Search in sources :

Example 86 with BundleContext

use of org.osgi.framework.BundleContext in project sling by apache.

the class SightlyCompiledScriptTest method testEvalSlingBindings.

/**
     * Tests that SlingBindings are correctly handled by compiled scripts, by setting them from the script context to the request
     * attributes.
     * @throws ScriptException
     */
@Test
public void testEvalSlingBindings() throws ScriptException {
    ScriptEngine scriptEngine = mock(ScriptEngine.class);
    final RenderUnit renderUnit = mock(RenderUnit.class);
    Whitebox.setInternalState(renderUnit, "subTemplates", new HashMap<String, Object>());
    final BundleContext bundleContext = MockOsgi.newBundleContext();
    bundleContext.registerService(ExtensionRegistryService.class.getName(), mock(ExtensionRegistryService.class), new Hashtable<String, Object>());
    ResourceResolver resourceResolver = MockSling.newResourceResolver(bundleContext);
    final MockSlingHttpServletRequest request = spy(new MockSlingHttpServletRequest(resourceResolver, bundleContext));
    SightlyCompiledScript compiledScript = spy(new SightlyCompiledScript(scriptEngine, renderUnit));
    ScriptContext scriptContext = mock(ScriptContext.class);
    StringWriter writer = new StringWriter();
    when(scriptContext.getWriter()).thenReturn(writer);
    Bindings scriptContextBindings = new SimpleBindings() {

        {
            put("test", "testValue");
            put(SlingBindings.REQUEST, request);
            put(SlingBindings.SLING, MockSling.newSlingScriptHelper(bundleContext));
        }
    };
    SlingBindings oldBindings = new SlingBindings();
    oldBindings.put("old", "oldValue");
    request.setAttribute(SlingBindings.class.getName(), oldBindings);
    when(scriptContext.getBindings(ScriptContext.ENGINE_SCOPE)).thenReturn(scriptContextBindings);
    compiledScript.eval(scriptContext);
    ArgumentCaptor<SlingBindings> slingBindingsArgumentCaptor = ArgumentCaptor.forClass(SlingBindings.class);
    ArgumentCaptor<String> attributeNameArgumentCaptor = ArgumentCaptor.forClass(String.class);
    // request.setAttribute should have been invoked 3 times: once here, twice in the compiled script
    verify(request, times(3)).setAttribute(attributeNameArgumentCaptor.capture(), slingBindingsArgumentCaptor.capture());
    List<SlingBindings> slingBindingsValues = slingBindingsArgumentCaptor.getAllValues();
    int invocation = 1;
    for (SlingBindings bindings : slingBindingsValues) {
        switch(invocation) {
            case 1:
                assertEquals(oldBindings, bindings);
                break;
            case 2:
                assertEquals(3, bindings.size());
                for (Map.Entry<String, Object> entry : scriptContextBindings.entrySet()) {
                    assertEquals(entry.getValue(), bindings.get(entry.getKey()));
                }
                break;
            case 3:
                assertEquals(oldBindings, bindings);
        }
        invocation++;
    }
    for (String key : attributeNameArgumentCaptor.getAllValues()) {
        assertEquals(SlingBindings.class.getName(), key);
    }
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) RenderUnit(org.apache.sling.scripting.sightly.java.compiler.RenderUnit) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SimpleBindings(javax.script.SimpleBindings) ScriptEngine(javax.script.ScriptEngine) StringWriter(java.io.StringWriter) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) SimpleBindings(javax.script.SimpleBindings) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) HashMap(java.util.HashMap) Map(java.util.Map) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 87 with BundleContext

use of org.osgi.framework.BundleContext 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 88 with BundleContext

use of org.osgi.framework.BundleContext 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 89 with BundleContext

use of org.osgi.framework.BundleContext in project sling by apache.

the class VotingHandler method registerEventHandler.

private void registerEventHandler() {
    BundleContext bundleContext = context == null ? null : context.getBundleContext();
    if (bundleContext == null) {
        logger.info("registerEventHandler: context or bundleContext is null - cannot register");
        return;
    }
    Dictionary<String, Object> properties = new Hashtable<String, Object>();
    properties.put(Constants.SERVICE_DESCRIPTION, "Voting Event Listener");
    String[] topics = new String[] { SlingConstants.TOPIC_RESOURCE_ADDED, SlingConstants.TOPIC_RESOURCE_CHANGED, SlingConstants.TOPIC_RESOURCE_REMOVED };
    properties.put(EventConstants.EVENT_TOPIC, topics);
    String path = config.getDiscoveryResourcePath();
    if (path.endsWith("/")) {
        path = path.substring(0, path.length() - 1);
    }
    path = path + "/*";
    properties.put(EventConstants.EVENT_FILTER, "(&(path=" + path + "))");
    eventHandlerRegistration = bundleContext.registerService(EventHandler.class.getName(), this, properties);
    logger.info("registerEventHandler: VotingHandler registered as EventHandler");
}
Also used : Hashtable(java.util.Hashtable) BundleContext(org.osgi.framework.BundleContext)

Example 90 with BundleContext

use of org.osgi.framework.BundleContext in project sling by apache.

the class JobConsumerManagerTest method testSimpleMappingConsumer.

@Test
public void testSimpleMappingConsumer() {
    final BundleContext bc = Mockito.mock(BundleContext.class);
    final JobConsumerManager jcs = new JobConsumerManager();
    jcs.activate(bc, getDefaultConfig());
    final JobConsumer jc1 = Mockito.mock(JobConsumer.class);
    final ServiceReference ref1 = Mockito.mock(ServiceReference.class);
    Mockito.when(ref1.getProperty(JobConsumer.PROPERTY_TOPICS)).thenReturn("a/b");
    Mockito.when(ref1.getProperty(Constants.SERVICE_RANKING)).thenReturn(1);
    Mockito.when(ref1.getProperty(Constants.SERVICE_ID)).thenReturn(1L);
    Mockito.when(bc.getService(ref1)).thenReturn(jc1);
    jcs.bindJobConsumer(ref1);
    assertNotNull(jcs.getExecutor("a/b"));
    assertNull(jcs.getExecutor("a"));
    assertNull(jcs.getExecutor("a/c"));
    assertNull(jcs.getExecutor("a/b/a"));
}
Also used : JobConsumer(org.apache.sling.event.jobs.consumer.JobConsumer) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

BundleContext (org.osgi.framework.BundleContext)524 Test (org.junit.Test)186 Bundle (org.osgi.framework.Bundle)175 ServiceReference (org.osgi.framework.ServiceReference)126 File (java.io.File)82 Hashtable (java.util.Hashtable)75 HashMap (java.util.HashMap)70 Equinox (org.eclipse.osgi.launch.Equinox)51 BundleException (org.osgi.framework.BundleException)51 ArrayList (java.util.ArrayList)50 LinkedHashMap (java.util.LinkedHashMap)45 ServiceRegistration (org.osgi.framework.ServiceRegistration)41 IOException (java.io.IOException)40 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)38 URL (java.net.URL)33 Dictionary (java.util.Dictionary)32 Matchers.anyString (org.mockito.Matchers.anyString)28 Before (org.junit.Before)26 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)26 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)18