Search in sources :

Example 26 with ComponentContext

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

the class OsgiUtilTest method testFallbackLookupWithValueInComponentAndFramework.

@Test
public void testFallbackLookupWithValueInComponentAndFramework() {
    Dictionary dictionary = mock(Dictionary.class);
    doReturn("cvalue").when(dictionary).get("cname");
    BundleContext bundleContext = mock(BundleContext.class);
    doReturn("fvalue").when(bundleContext).getProperty("fname");
    ComponentContext componentContext = mock(ComponentContext.class);
    doReturn(dictionary).when(componentContext).getProperties();
    doReturn(bundleContext).when(componentContext).getBundleContext();
    assertEquals("cvalue", lookupConfigurationThenFramework(componentContext, "cname", "fname"));
    assertEquals("fvalue", 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 27 with ComponentContext

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

the class AdapterManagerTest method createMultipleAdaptersComponentContext.

/**
     * Helper method to create a mock component context
     */
protected ComponentContext createMultipleAdaptersComponentContext(final ServiceReference firstServiceReference, final ServiceReference secondServiceReference) 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(firstServiceReference));
            will(returnValue(new FirstImplementationAdapterFactory()));
            allowing(ctx).locateService(with(any(String.class)), with(secondServiceReference));
            will(returnValue(new SecondImplementationAdapterFactory()));
            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) BundleContext(org.osgi.framework.BundleContext)

Example 28 with ComponentContext

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

the class MockMimeTypeService method lazyInitialization.

/**
     * Do lazy initializing because reading to avoid reading all defined mime
     * types from disk if not required
     */
private void lazyInitialization() {
    if (!this.initialized) {
        this.initialized = true;
        // activate service in simulated OSGi environment
        ComponentContext componentContext = MockOsgi.newComponentContext();
        this.bindLogService(MockOsgi.newLogService(getClass()));
        activate(componentContext);
    }
}
Also used : ComponentContext(org.osgi.service.component.ComponentContext)

Example 29 with ComponentContext

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

the class LangIdEngineTest method testEngine.

/**
     * Test the engine and validates the created enhancements
     * @throws EngineException
     * @throws IOException
     * @throws ConfigurationException
     */
@Test
public void testEngine() throws EngineException, IOException, ConfigurationException {
    LangIdEnhancementEngine langIdEngine = new LangIdEnhancementEngine();
    ComponentContext context = new MockComponentContext();
    context.getProperties().put(EnhancementEngine.PROPERTY_NAME, "langid");
    langIdEngine.activate(context);
    ContentItem ci = ciFactory.createContentItem(new StringSource(text));
    langIdEngine.computeEnhancements(ci);
    HashMap<IRI, RDFTerm> expectedValues = new HashMap<IRI, RDFTerm>();
    expectedValues.put(Properties.ENHANCER_EXTRACTED_FROM, ci.getUri());
    expectedValues.put(Properties.DC_CREATOR, LiteralFactory.getInstance().createTypedLiteral(langIdEngine.getClass().getName()));
    int textAnnotationCount = validateAllTextAnnotations(ci.getMetadata(), text, expectedValues);
    assertEquals("A single TextAnnotation is expected", 1, textAnnotationCount);
    //even through this tests do not validate service quality but rather
    //the correct integration of the CELI service as EnhancementEngine
    //we expect the "en" is detected for the parsed text
    assertEquals("The detected language for text '" + text + "' MUST BE 'en'", "en", EnhancementEngineHelper.getLanguage(ci));
    int entityAnnoNum = validateAllEntityAnnotations(ci.getMetadata(), expectedValues);
    assertEquals("No EntityAnnotations are expected", 0, entityAnnoNum);
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) LangIdEnhancementEngine(org.apache.stanbol.enhancer.engines.langid.LangIdEnhancementEngine) ComponentContext(org.osgi.service.component.ComponentContext) HashMap(java.util.HashMap) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) StringSource(org.apache.stanbol.enhancer.servicesapi.impl.StringSource) ContentItem(org.apache.stanbol.enhancer.servicesapi.ContentItem) Test(org.junit.Test)

Example 30 with ComponentContext

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

the class ResourceDetectionTest method testStopAndRestart.

@Test
public void testStopAndRestart() throws Exception {
    assertRegisteredPaths(contentHelper.FAKE_RESOURCES);
    assertRegisteredPaths(contentHelper.FAKE_CONFIGS);
    final ComponentContext cc = context.componentContext();
    // With the installer deactivated, remove two resources and add some new ones 
    osgiInstaller.clearRecordedCalls();
    installer.deactivate(cc);
    assertEquals("Expected no calls to OsgiInstaller when deactivating JcrInstaller", 0, osgiInstaller.getRecordedCalls().size());
    final int toRemove = 2;
    contentHelper.delete(contentHelper.FAKE_RESOURCES[toRemove]);
    contentHelper.delete(contentHelper.FAKE_CONFIGS[toRemove]);
    final String[] toAdd = { "/libs/foo/bar/install/" + System.currentTimeMillis() + ".jar", "/libs/foo/wii/install/" + +System.currentTimeMillis() + ".properties", "/libs/foo/wii/install/" + +System.currentTimeMillis() + ".cfg" };
    for (String path : toAdd) {
        contentHelper.createOrUpdateFile(path);
    }
    // Verify that no calls have been made to OSGi installer
    eventHelper.waitForEvents(TIMEOUT);
    Thread.sleep(JcrInstaller.RUN_LOOP_DELAY_MSEC * 3);
    assertEquals("Expected no calls to OsgiInstaller while JcrInstaller is stopped", 0, osgiInstaller.getRecordedCalls().size());
    // Restart JcrInstaller and verify that all remaining resources are re-registered
    installer.activate(cc);
    MiscUtil.waitAfterContentChanges(eventHelper, installer);
    for (int i = 0; i < contentHelper.FAKE_RESOURCES.length; i++) {
        final String path = contentHelper.FAKE_RESOURCES[i];
        if (i == toRemove) {
            assertFalse("Path should be absent from recorded calls", osgiInstaller.getRecordedCalls().contains(path));
        } else {
            assertRecordedCall("register", path);
        }
    }
    for (int i = 0; i < contentHelper.FAKE_CONFIGS.length; i++) {
        final String path = contentHelper.FAKE_CONFIGS[i];
        if (i == toRemove) {
            assertFalse("Path should be absent from recorded calls", osgiInstaller.getRecordedCalls().contains(path));
        } else {
            assertRecordedCall("register", path);
        }
    }
    for (String path : toAdd) {
        assertRecordedCall("register", path);
    }
}
Also used : ComponentContext(org.osgi.service.component.ComponentContext) Test(org.junit.Test)

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