Search in sources :

Example 16 with ContextFunction

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

the class InjectionUpdateTest method testPropagation.

@Test
public void testPropagation() {
    c1.set("base", "abc");
    c21.set("derived1", new ContextFunction() {

        @Override
        public Object compute(IEclipseContext context, String contextKey) {
            String baseString = (String) context.get("base");
            return baseString.charAt(0) + "_";
        }
    });
    c22.set("derived2", new ContextFunction() {

        @Override
        public Object compute(IEclipseContext context, String contextKey) {
            String baseString = (String) context.get("base");
            return "_" + baseString.charAt(baseString.length() - 1);
        }
    });
    c1.set("calculated", new ContextFunction() {

        @Override
        public Object compute(IEclipseContext context, String contextKey) {
            IEclipseContext context21 = (IEclipseContext) context.get("c21");
            String derived1 = (String) context21.get("derived1");
            IEclipseContext context22 = (IEclipseContext) context.get("c22");
            String derived2 = (String) context22.get("derived2");
            return derived1 + derived2;
        }
    });
    PropagationTest testObject = ContextInjectionFactory.make(PropagationTest.class, c1);
    assertNotNull(testObject);
    assertEquals(1, testObject.called);
    assertEquals("a__c", testObject.in);
    // this should result in only one injection call
    c1.set("base", "123");
    assertEquals(2, testObject.called);
    assertEquals("1__3", testObject.in);
    c1.set("base", "xyz");
    assertEquals(3, testObject.called);
    assertEquals("x__z", testObject.in);
}
Also used : ContextFunction(org.eclipse.e4.core.contexts.ContextFunction) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 17 with ContextFunction

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

the class InjectionUpdateTest method testNestedUpdatesPostConstruct.

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

        @Override
        public Object compute(IEclipseContext context, String contextKey) {
            return ContextInjectionFactory.make(InjectTarget.class, context);
        }
    });
    InjectTarget targetA = appContext.get(InjectTarget.class);
    targetA.modify();
    InjectTarget targetB = appContext.get(InjectTarget.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

ContextFunction (org.eclipse.e4.core.contexts.ContextFunction)17 IEclipseContext (org.eclipse.e4.core.contexts.IEclipseContext)17 Test (org.junit.Test)14 PerformanceTestRunner (org.eclipse.core.tests.harness.PerformanceTestRunner)3 RunAndTrack (org.eclipse.e4.core.contexts.RunAndTrack)2