Search in sources :

Example 46 with IEclipseContext

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

the class InvokeInRATTest method testStaticInvoke.

@Test
public void testStaticInvoke() {
    IEclipseContext context = EclipseContextFactory.create();
    final int[] count = new int[1];
    context.runAndTrack(new RunAndTrack() {

        @Override
        public boolean changed(IEclipseContext context) {
            TestHandler handler = (TestHandler) context.get("handlerA");
            if (handler != null) {
                ContextInjectionFactory.invoke(handler, CanExecute.class, context);
                count[0]++;
            }
            // continue to be notified
            return true;
        }
    });
    // check that updates are propagated
    context.set("active", Integer.valueOf(123));
    context.set("selected", "abc");
    TestHandler handler = new TestHandler();
    context.set("handlerA", handler);
    assertEquals(Integer.valueOf(123), handler.active);
    assertEquals("abc", handler.selected);
    // check handler replacement
    count[0] = 0;
    TestHandler newHandler = new TestHandler();
    context.set("handlerA", newHandler);
    assertEquals(1, count[0]);
    assertEquals(Integer.valueOf(123), newHandler.active);
    assertEquals("abc", newHandler.selected);
    // change values in the context; values should not be propagated to handlers
    context.set("active", Integer.valueOf(456));
    context.set("selected", "xyz");
    assertEquals(Integer.valueOf(123), handler.active);
    assertEquals("abc", handler.selected);
    assertEquals(Integer.valueOf(123), newHandler.active);
    assertEquals("abc", newHandler.selected);
}
Also used : RunAndTrack(org.eclipse.e4.core.contexts.RunAndTrack) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) CanExecute(org.eclipse.e4.core.di.annotations.CanExecute) Test(org.junit.Test)

Example 47 with IEclipseContext

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

the class GenericsListTest method testTypeErasure.

@Test(expected = ClassCastException.class)
public void testTypeErasure() {
    List<Integer> list = new ArrayList<>();
    list.add(1);
    // create context
    IEclipseContext context = EclipseContextFactory.create();
    context.set(List.class, list);
    TestNamedObject userObject = new TestNamedObject();
    ContextInjectionFactory.inject(userObject, context);
    // check field injection, should be successful because
    // of the type erasure
    assertEquals(list, userObject.field);
    userObject.combineIt();
}
Also used : ArrayList(java.util.ArrayList) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 48 with IEclipseContext

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

the class InjectStaticContextTest method testStaticInvoke.

@Test
public void testStaticInvoke() {
    IEclipseContext trackedContext = EclipseContextFactory.create("main");
    trackedContext.set("a", "abc");
    IEclipseContext staticContext = EclipseContextFactory.create("static");
    staticContext.set("b", "123");
    TestInvokeClass testObject = new TestInvokeClass();
    assertNull(testObject.aString);
    assertNull(testObject.bString);
    Object result = ContextInjectionFactory.invoke(testObject, Execute.class, trackedContext, staticContext, null);
    assertEquals("abc123", result);
    assertEquals("abc", testObject.aString);
    assertEquals("123", testObject.bString);
    assertEquals(trackedContext, testObject.context);
}
Also used : IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 49 with IEclipseContext

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

the class InjectStaticContextTest method testStaticMake.

@Test
public void testStaticMake() {
    IEclipseContext trackedContext = EclipseContextFactory.create();
    trackedContext.set("a", "abc");
    trackedContext.set("aConstructor", "abcConstructor");
    trackedContext.set("b", "bbc");
    IEclipseContext staticContext = EclipseContextFactory.create();
    // local values override
    staticContext.set("b", "123");
    staticContext.set("bConstructor", "123Constructor");
    staticContext.set("c", "xyz");
    TestClass testObject = ContextInjectionFactory.make(TestClass.class, trackedContext, staticContext);
    assertEquals(trackedContext, testObject.injectedContext);
    assertEquals("abcConstructor", testObject.aConstructorString);
    assertEquals("123Constructor", testObject.bConstructorString);
    assertEquals("abc", testObject.aString);
    assertEquals("123", testObject.bString);
    assertEquals("xyz", testObject.cString);
    assertEquals(1, testObject.postConstructCalled);
    assertEquals(0, testObject.preDestroyCalled);
    // modify local context -> should have no effect
    staticContext.set("b", "_123_");
    staticContext.set("bConstructor", "_123Constructor_");
    staticContext.set("c", "_xyz_");
    assertEquals("abcConstructor", testObject.aConstructorString);
    assertEquals("123Constructor", testObject.bConstructorString);
    assertEquals("abc", testObject.aString);
    assertEquals("123", testObject.bString);
    assertEquals("xyz", testObject.cString);
    assertEquals(1, testObject.postConstructCalled);
    assertEquals(0, testObject.preDestroyCalled);
    // dispose local context -> should have no effect
    staticContext.dispose();
    assertEquals("abcConstructor", testObject.aConstructorString);
    assertEquals("123Constructor", testObject.bConstructorString);
    assertEquals("abc", testObject.aString);
    assertEquals("123", testObject.bString);
    assertEquals("xyz", testObject.cString);
    assertEquals(1, testObject.postConstructCalled);
    assertEquals(0, testObject.preDestroyCalled);
    // modify parent context -> should propagate
    trackedContext.set("a", "_abc_");
    trackedContext.set("b", "_bbc_");
    assertEquals("_abc_", testObject.aString);
    assertEquals("123", testObject.bString);
    // uninject from the parent context
    ContextInjectionFactory.uninject(testObject, trackedContext);
    assertNull(testObject.injectedContext);
    assertNull(testObject.aString);
    assertEquals(1, testObject.postConstructCalled);
    assertEquals(1, testObject.preDestroyCalled);
    // further changes should have no effect
    trackedContext.set("a", "+abc+");
    assertNull(testObject.aString);
    trackedContext.dispose();
    assertEquals(1, testObject.postConstructCalled);
    assertEquals(1, testObject.preDestroyCalled);
}
Also used : IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 50 with IEclipseContext

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

the class InjectionUpdateTest method testNestedUpdatesConstructor.

@Test
public void testNestedUpdatesConstructor() throws Exception {
    IEclipseContext appContext = EclipseContextFactory.create();
    appContext.set(InjectTarget2.class.getName(), new ContextFunction() {

        @Override
        public Object compute(IEclipseContext context, String contextKey) {
            return ContextInjectionFactory.make(InjectTarget2.class, context);
        }
    });
    InjectTarget2 targetA = appContext.get(InjectTarget2.class);
    targetA.modify();
    InjectTarget2 targetB = appContext.get(InjectTarget2.class);
    assertEquals(targetA, targetB);
    assertSame(targetA, targetB);
}
Also used : ContextFunction(org.eclipse.e4.core.contexts.ContextFunction) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Aggregations

IEclipseContext (org.eclipse.e4.core.contexts.IEclipseContext)197 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 MApplication (org.eclipse.e4.ui.model.application.MApplication)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