Search in sources :

Example 86 with IEclipseContext

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

the class ContextDynamicTest method testAddRemove.

/**
 * Tests objects being added and removed from the context
 */
@Test
public synchronized void testAddRemove() {
    Integer testInt = Integer.valueOf(123);
    String testString = "abc";
    Double testDouble = Double.valueOf(1.23);
    Float testFloat = Float.valueOf(12.3f);
    Character testChar = Character.valueOf('v');
    // create original context
    IEclipseContext context = EclipseContextFactory.create();
    context.set(Integer.class.getName(), testInt);
    context.set(String.class.getName(), testString);
    context.set(Double.class.getName(), testDouble);
    context.set(Float.class.getName(), testFloat);
    context.set(Character.class.getName(), testChar);
    ObjectBasic userObject = new ObjectBasic();
    ContextInjectionFactory.inject(userObject, context);
    // check basic injection
    assertEquals(testString, userObject.injectedString);
    assertEquals(testInt, userObject.getInt());
    assertEquals(context, userObject.context);
    assertEquals(1, userObject.setMethodCalled);
    assertEquals(1, userObject.setMethodCalled2);
    assertEquals(testDouble, userObject.d);
    assertEquals(testFloat, userObject.f);
    assertEquals(testChar, userObject.c);
    // change value
    Double testDouble2 = Double.valueOf(3.45);
    Integer testInt2 = Integer.valueOf(123);
    context.set(Double.class.getName(), testDouble2);
    context.set(Integer.class.getName(), testInt2);
    // and check
    assertEquals(testString, userObject.injectedString);
    assertEquals(testInt2, userObject.getInt());
    assertEquals(context, userObject.context);
    assertEquals(2, userObject.setMethodCalled);
    assertEquals(1, userObject.setMethodCalled2);
    assertEquals(testDouble2, userObject.d);
    assertEquals(testFloat, userObject.f);
    assertEquals(testChar, userObject.c);
    // remove element
    context.remove(String.class.getName());
    context.remove(Character.class.getName());
    // and check
    assertNull(userObject.injectedString);
    assertEquals(testInt2, userObject.getInt());
    assertEquals(context, userObject.context);
    assertEquals(2, userObject.setMethodCalled);
    assertEquals(2, userObject.setMethodCalled2);
    assertEquals(testDouble2, userObject.d);
    assertEquals(testFloat, userObject.f);
    assertNull(userObject.c);
}
Also used : ObjectBasic(org.eclipse.e4.core.internal.tests.contexts.inject.ObjectBasic) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 87 with IEclipseContext

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

the class ContextDynamicTest method testParentAddRemove.

/**
 * Tests objects being added and removed from the context
 */
@Test
public synchronized void testParentAddRemove() {
    Integer testInt = Integer.valueOf(123);
    String testString = "abc";
    Double testDouble = Double.valueOf(1.23);
    Float testFloat = Float.valueOf(12.3f);
    Character testChar = Character.valueOf('v');
    // create original context
    IEclipseContext parentContext = EclipseContextFactory.create();
    parentContext.set(Integer.class.getName(), testInt);
    parentContext.set(String.class.getName(), testString);
    parentContext.set(Double.class.getName(), testDouble);
    parentContext.set(Float.class.getName(), testFloat);
    parentContext.set(Character.class.getName(), testChar);
    IEclipseContext context = parentContext.createChild();
    ObjectBasic userObject = new ObjectBasic();
    ContextInjectionFactory.inject(userObject, context);
    // check basic injection
    assertEquals(testString, userObject.injectedString);
    assertEquals(testInt, userObject.getInt());
    assertEquals(context, userObject.context);
    assertEquals(1, userObject.setMethodCalled);
    assertEquals(1, userObject.setMethodCalled2);
    assertEquals(testDouble, userObject.d);
    assertEquals(testFloat, userObject.f);
    assertEquals(testChar, userObject.c);
    // change value
    Double testDouble2 = Double.valueOf(3.45);
    Integer testInt2 = Integer.valueOf(123);
    context.set(Double.class.getName(), testDouble2);
    context.set(Integer.class.getName(), testInt2);
    // and check
    assertEquals(testString, userObject.injectedString);
    assertEquals(testInt2, userObject.getInt());
    assertEquals(context, userObject.context);
    assertEquals(2, userObject.setMethodCalled);
    assertEquals(1, userObject.setMethodCalled2);
    assertEquals(testDouble2, userObject.d);
    assertEquals(testFloat, userObject.f);
    assertEquals(testChar, userObject.c);
    // remove element
    parentContext.remove(String.class.getName());
    parentContext.remove(Character.class.getName());
    // and check
    assertNull(userObject.injectedString);
    assertEquals(testInt2, userObject.getInt());
    assertEquals(context, userObject.context);
    assertEquals(2, userObject.setMethodCalled);
    assertEquals(2, userObject.setMethodCalled2);
    assertEquals(testDouble2, userObject.d);
    assertEquals(testFloat, userObject.f);
    assertNull(userObject.c);
}
Also used : ObjectBasic(org.eclipse.e4.core.internal.tests.contexts.inject.ObjectBasic) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 88 with IEclipseContext

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

the class ContextDynamicTest method testReplaceFunctionWithStaticValue.

@Test
public void testReplaceFunctionWithStaticValue() {
    IEclipseContext parent = EclipseContextFactory.create();
    IEclipseContext context = parent.createChild();
    assertNull(context.getLocal("bar"));
    context.set("bar", "baz1");
    context.set("bar", new ContextFunction() {

        @Override
        public Object compute(IEclipseContext context, String contextKey) {
            return "baz1";
        }
    });
    parent.set("bar", "baz2");
    assertEquals("baz1", context.get("bar"));
    context.set("bar", "baz3");
    assertEquals("baz3", context.get("bar"));
}
Also used : ContextFunction(org.eclipse.e4.core.contexts.ContextFunction) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 89 with IEclipseContext

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

the class EclipseContextTest method testContextFunctionInParent.

/**
 * Tests handling of a context function defined in the parent that uses values defined in the
 * child
 */
@Test
public void testContextFunctionInParent() {
    IEclipseContext parent = EclipseContextFactory.create();
    final IEclipseContext child = parent.createChild();
    parent.set("sum", new AddContextFunction());
    parent.set("x", Integer.valueOf(3));
    parent.set("y", Integer.valueOf(3));
    child.set("x", Integer.valueOf(1));
    child.set("y", Integer.valueOf(1));
    assertEquals(6, ((Integer) parent.get("sum")).intValue());
    assertEquals(2, ((Integer) child.get("sum")).intValue());
    child.set("x", Integer.valueOf(5));
    assertEquals(6, ((Integer) parent.get("sum")).intValue());
    assertEquals(6, ((Integer) child.get("sum")).intValue());
    child.remove("x");
    assertEquals(6, ((Integer) parent.get("sum")).intValue());
    assertEquals(4, ((Integer) child.get("sum")).intValue());
    parent.set("x", Integer.valueOf(10));
    assertEquals(13, ((Integer) parent.get("sum")).intValue());
    assertEquals(11, ((Integer) child.get("sum")).intValue());
}
Also used : IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 90 with IEclipseContext

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

the class EclipseContextTest method testContextFunctionOrdering.

@Test
public void testContextFunctionOrdering() {
    IEclipseContext osgiContext = EclipseContextFactory.getServiceContext(FrameworkUtil.getBundle(getClass()).getBundleContext());
    assertEquals("High", osgiContext.get("test.contextfunction.ranking"));
}
Also used : 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