Search in sources :

Example 16 with IEclipseContext

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

the class ServiceSupplierTestCase method testDynamicAddRemove.

@Test
public void testDynamicAddRemove() {
    BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext();
    IEclipseContext serviceContext = EclipseContextFactory.getServiceContext(context);
    TestBean bean = ContextInjectionFactory.make(TestBean.class, serviceContext);
    assertEquals(1, bean.serviceInjectionCount);
    assertEquals(1, bean.serviceListInjectionCount);
    TestService t = new TestService() {
    };
    Hashtable<String, Object> properties = new Hashtable<>();
    // $NON-NLS-1$
    properties.put("service.ranking", 52);
    this.registrations.add(context.registerService(TestService.class, t, properties));
    assertSame(t, bean.service);
    assertEquals(2, bean.serviceInjectionCount);
    assertEquals(2, bean.serviceListInjectionCount);
    assertEquals(5, bean.serviceList.size());
    assertSame(t, bean.serviceList.get(0));
    ServiceRegistration<?> registration = this.registrations.get(0);
    registration.unregister();
    this.registrations.remove(registration);
    assertEquals(3, bean.serviceInjectionCount);
    assertEquals(3, bean.serviceListInjectionCount);
    assertSame(SampleServiceA.class, bean.service.getClass());
    assertEquals(4, bean.serviceList.size());
    assertSame(SampleServiceA.class, bean.serviceList.get(0).getClass());
    assertSame(SampleServiceB.class, bean.serviceList.get(1).getClass());
}
Also used : Hashtable(java.util.Hashtable) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 17 with IEclipseContext

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

the class ServiceSupplierTestCase method testOptionalReferences.

@Test
public void testOptionalReferences() throws InterruptedException {
    BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext();
    IEclipseContext serviceContext = EclipseContextFactory.getServiceContext(context);
    TestDisabledBean bean = ContextInjectionFactory.make(TestDisabledBean.class, serviceContext);
    assertNull(bean.disabledService);
    assertEquals(0, bean.services.size());
    ServiceReference<ComponentEnabler> ref = context.getServiceReference(ComponentEnabler.class);
    ComponentEnabler enabler = context.getService(ref);
    try {
        enabler.enableDisabledServiceA();
        // give the service registry and the injection some time
        Thread.sleep(100);
        assertNotNull(bean.disabledService);
        assertEquals(1, bean.services.size());
        assertSame(DisabledServiceA.class, bean.disabledService.getClass());
        enabler.enableDisabledServiceB();
        // give the service registry and the injection some time
        Thread.sleep(100);
        assertNotNull(bean.disabledService);
        assertEquals(2, bean.services.size());
        assertSame(DisabledServiceB.class, bean.disabledService.getClass());
        enabler.disableDisabledServiceB();
        // give the service registry and the injection some time
        Thread.sleep(100);
        assertNotNull(bean.disabledService);
        assertEquals(1, bean.services.size());
        assertSame(DisabledServiceA.class, bean.disabledService.getClass());
        enabler.disableDisabledServiceA();
        // give the service registry and the injection some time
        Thread.sleep(100);
        assertNull(bean.disabledService);
        assertEquals(0, bean.services.size());
    } finally {
        enabler.disableDisabledServiceA();
        enabler.disableDisabledServiceB();
        // give the service registry and the injection some time to ensure
        // clear state after this test
        Thread.sleep(100);
    }
}
Also used : IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 18 with IEclipseContext

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

the class ComputedValueLimitationTest method testVolatileFunction.

@Test
public void testVolatileFunction() {
    IEclipseContext context = EclipseContextFactory.create();
    context.set("time", new Time());
    long time = ((Long) context.get("time")).longValue();
    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
    // 
    }
    long newTime = ((Long) context.get("time")).longValue();
    assertTrue(time != newTime);
}
Also used : IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 19 with IEclipseContext

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

the class InjectionErrorReportingTest method testFieldInjectionError.

/**
 * Shows the error message for an unresolved field value
 */
@Test(expected = InjectionException.class)
public void testFieldInjectionError() {
    IEclipseContext context = EclipseContextFactory.create();
    TestData methodValue = new TestData();
    context.set("testing", methodValue);
    InjectedField object = new InjectedField();
    ContextInjectionFactory.inject(object, context);
}
Also used : IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 20 with IEclipseContext

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

the class InjectionErrorReportingTest method testMethodInjectionError.

/**
 * Shows the error message for an unresolved method argument
 */
@Test(expected = InjectionException.class)
public void testMethodInjectionError() {
    IEclipseContext context = EclipseContextFactory.create();
    TestData methodValue = new TestData();
    context.set("testing", methodValue);
    InjectedMethod object = new InjectedMethod();
    ContextInjectionFactory.inject(object, context);
}
Also used : IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

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