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