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);
}
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);
}
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"));
}
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());
}
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"));
}
Aggregations