Search in sources :

Example 36 with IEclipseContext

use of org.eclipse.e4.core.contexts.IEclipseContext in project eclipse.platform.runtime by eclipse.

the class ServiceContextTest method testServiceContextAsParent.

/**
 * Tests accessing OSGi services through a child context that is not aware of them.
 */
@Test
public void testServiceContextAsParent() {
    IEclipseContext child = context.createChild("child");
    DebugOptions service = (DebugOptions) child.get(DebugOptions.class.getName());
    assertNotNull(service);
}
Also used : IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) DebugOptions(org.eclipse.osgi.service.debug.DebugOptions) Test(org.junit.Test)

Example 37 with IEclipseContext

use of org.eclipse.e4.core.contexts.IEclipseContext in project eclipse.platform.runtime by eclipse.

the class ServiceContextTest method testServiceRemovalOnContextDispose.

/**
 * Tests that OSGi services are released when their context is disposed.
 */
@Test
public void testServiceRemovalOnContextDispose() {
    StringPrintService stringPrint1 = new StringPrintService();
    BundleContext bundleContext = CoreTestsActivator.getDefault().getBundleContext();
    Bundle otherBundle = null;
    for (Bundle b : bundleContext.getBundles()) {
        if (b.getSymbolicName().equals("org.eclipse.core.tests.harness")) {
            otherBundle = b;
            break;
        }
    }
    assertNotNull(otherBundle);
    IEclipseContext otherServiceContext = EclipseContextFactory.getServiceContext(otherBundle.getBundleContext());
    ServiceRegistration<?> reg1 = bundleContext.registerService(PrintService.SERVICE_NAME, stringPrint1, null);
    try {
        ServiceReference<?> ref = reg1.getReference();
        PrintService service = (PrintService) otherServiceContext.get(PrintService.SERVICE_NAME);
        assertEquals("1.0", stringPrint1, service);
        assertEquals("1.1", 1, ref.getUsingBundles().length);
        service = null;
        otherServiceContext.dispose();
        assertNull("2.0", ref.getUsingBundles());
    } finally {
        reg1.unregister();
    }
}
Also used : Bundle(org.osgi.framework.Bundle) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 38 with IEclipseContext

use of org.eclipse.e4.core.contexts.IEclipseContext in project eclipse.platform.runtime by eclipse.

the class TestConstructorInjection method testConstructorInjection.

@Test
public void testConstructorInjection() throws InvocationTargetException, InstantiationException {
    IEclipseContext context = EclipseContextFactory.create();
    Integer intValue = Integer.valueOf(123);
    context.set(Integer.class.getName(), intValue);
    Boolean boolValue = Boolean.TRUE;
    context.set(Boolean.class.getName(), boolValue);
    Double doubleValue = Double.valueOf(1.23);
    context.set(Double.class.getName(), doubleValue);
    Float floatValue = Float.valueOf(12.3f);
    context.set(Float.class.getName(), floatValue);
    Character charValue = Character.valueOf('v');
    context.set(Character.class.getName(), charValue);
    Object result = ContextInjectionFactory.make(TestConstructorObject.class, context);
    assertNotNull(result);
    assertTrue(result instanceof TestConstructorObject);
    TestConstructorObject testObject = ((TestConstructorObject) result);
    assertFalse(testObject.defaultConstructorCalled);
    assertFalse(testObject.constructorIntStrCalled);
    assertTrue(testObject.constructorIntBoolCalled);
    assertTrue(testObject.injectedMethodCalled);
    assertFalse(testObject.nonInjectedMethodCalled);
    assertTrue(testObject.orderCorrect);
    assertEquals(intValue, testObject.i);
    assertEquals(boolValue, testObject.b);
    assertEquals(doubleValue, testObject.d);
    assertEquals(floatValue, testObject.f);
    assertEquals(charValue, testObject.getChar());
    assertNull(testObject.s);
}
Also used : IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 39 with IEclipseContext

use of org.eclipse.e4.core.contexts.IEclipseContext in project eclipse.platform.runtime by eclipse.

the class InjectionOSGiTest method tearDown.

@After
public void tearDown() throws Exception {
    bundle.start();
    final BundleContext bundleContext = CoreTestsActivator.getDefault().getBundleContext();
    final IEclipseContext localContext = EclipseContextFactory.getServiceContext(bundleContext);
    ContextInjectionFactory.uninject(target, localContext);
}
Also used : IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) BundleContext(org.osgi.framework.BundleContext) After(org.junit.After)

Example 40 with IEclipseContext

use of org.eclipse.e4.core.contexts.IEclipseContext in project eclipse.platform.runtime by eclipse.

the class ContextExample method run.

/**
 */
public void run() {
    IEclipseContext parent = EclipseContextFactory.create();
    parent.set("complement", new ComplementaryColor());
    IEclipseContext context = parent.createChild();
    context.set("color", Color.YELLOW);
    Crayon crayon = new Crayon();
    ContextInjectionFactory.inject(crayon, context);
    crayon.draw();
}
Also used : IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext)

Aggregations

IEclipseContext (org.eclipse.e4.core.contexts.IEclipseContext)195 Test (org.junit.Test)142 RunAndTrack (org.eclipse.e4.core.contexts.RunAndTrack)18 ContextFunction (org.eclipse.e4.core.contexts.ContextFunction)17 BundleContext (org.osgi.framework.BundleContext)15 IEntity (org.whole.lang.model.IEntity)11 IEntityPartViewer (org.whole.lang.ui.viewers.IEntityPartViewer)9 Execute (org.eclipse.e4.core.di.annotations.Execute)7 MPart (org.eclipse.e4.ui.model.application.ui.basic.MPart)6 IImportAsModelDialogFactory (org.whole.lang.ui.dialogs.IImportAsModelDialogFactory)6 Hashtable (java.util.Hashtable)5 PrimaryObjectSupplier (org.eclipse.e4.core.di.suppliers.PrimaryObjectSupplier)4 Named (javax.inject.Named)3 PerformanceTestRunner (org.eclipse.core.tests.harness.PerformanceTestRunner)3 InjectionException (org.eclipse.e4.core.di.InjectionException)3 CanExecute (org.eclipse.e4.core.di.annotations.CanExecute)3 Optional (org.eclipse.e4.core.di.annotations.Optional)3 UISynchronize (org.eclipse.e4.ui.di.UISynchronize)3 ContextMenuProvider (org.eclipse.gef.ContextMenuProvider)3 IMenuManager (org.eclipse.jface.action.IMenuManager)3