use of org.eclipse.e4.core.contexts.IEclipseContext in project eclipse.platform.runtime by eclipse.
the class ContextInjectionDisposeTest method testDisposeContext.
@Test
public void testDisposeContext() {
class Injected {
boolean disposeInvoked = false;
@Inject
Object Field;
String methodValue;
@PreDestroy
public void dispose() {
disposeInvoked = true;
}
@Inject
public void InjectedMethod(String arg) {
methodValue = arg;
}
}
IEclipseContext context = EclipseContextFactory.create();
Object fieldValue = new Object();
Object methodValue = "abc";
context.set(Object.class, fieldValue);
context.set(String.class.getName(), methodValue);
Injected object = new Injected();
ContextInjectionFactory.inject(object, context);
assertEquals(fieldValue, object.Field);
assertEquals(methodValue, object.methodValue);
// disposing context calls @PreDestory, but does not clear injected
// values
context.dispose();
assertNotNull(object.Field);
assertNotNull(object.methodValue);
assertTrue(object.disposeInvoked);
}
use of org.eclipse.e4.core.contexts.IEclipseContext in project eclipse.platform.runtime by eclipse.
the class ContextInjectionTest method testInjectionAndInheritance.
/**
* Tests injection into classes with inheritance
*/
@Test
public synchronized void testInjectionAndInheritance() {
Integer testInt = Integer.valueOf(123);
String testString = "abc";
Float testFloat = Float.valueOf(12.3f);
// create context
IEclipseContext context = EclipseContextFactory.create();
context.set(Integer.class.getName(), testInt);
context.set(String.class.getName(), testString);
context.set(Float.class.getName(), testFloat);
ObjectSubClass userObject = new ObjectSubClass();
ContextInjectionFactory.inject(userObject, context);
// check inherited portion
assertEquals(testString, userObject.getString());
// assertEquals(context, userObject.getContext());
assertEquals(testString, userObject.getStringViaMethod());
assertEquals(1, userObject.setStringCalled);
// check declared portion
assertEquals(testInt, userObject.getInteger());
assertEquals(testFloat, userObject.getObjectViaMethod());
assertEquals(1, userObject.setObjectCalled);
// make sure overridden injected method was called only once
assertEquals(1, userObject.setOverriddenCalled);
// check post processing
assertEquals(1, userObject.getFinalizedCalled());
}
use of org.eclipse.e4.core.contexts.IEclipseContext in project eclipse.platform.runtime by eclipse.
the class ContextInjectionTest method testInjectionCloseOverride.
/**
* Tests injection of similar, but not overridden methods
*/
@Test
public synchronized void testInjectionCloseOverride() {
Integer testInt = Integer.valueOf(123);
String testString = "abc";
Double testDouble = Double.valueOf(12.3);
Boolean testBoolean = Boolean.TRUE;
// create context
IEclipseContext context = EclipseContextFactory.create();
context.set(Integer.class, testInt);
context.set(String.class, testString);
context.set(Double.class, testDouble);
context.set(Boolean.class, testBoolean);
OverrideTest userObject = new OverrideTest();
ContextInjectionFactory.inject(userObject, context);
// check inherited portion
assertEquals(testString, userObject.selectionString);
assertEquals(testString, userObject.inputString);
assertTrue(userObject.finishCalled);
// check similar methods portion
assertEquals(testInt, userObject.selectionNum);
assertEquals(testString, userObject.inputStringSubclass);
assertEquals(testDouble, userObject.inputDouble);
assertTrue(userObject.finishOverrideCalled);
}
use of org.eclipse.e4.core.contexts.IEclipseContext in project eclipse.platform.runtime by eclipse.
the class ContextInjectionTest method testContextSetZeroArgs.
/**
* Test filnalize method - no args
*/
@Test
public void testContextSetZeroArgs() {
class TestData {
}
class Injected {
int contextSetCalled = 0;
int setMethodCalled = 0;
public TestData value;
@Inject
public void contextSet() {
contextSetCalled++;
}
@Inject
public void InjectedMethod(TestData arg) {
setMethodCalled++;
value = arg;
}
}
IEclipseContext context = EclipseContextFactory.create();
TestData methodValue = new TestData();
context.set(TestData.class.getName(), methodValue);
Injected object = new Injected();
ContextInjectionFactory.inject(object, context);
assertEquals(1, object.setMethodCalled);
assertEquals(1, object.contextSetCalled);
TestData methodValue2 = new TestData();
context.set(TestData.class.getName(), methodValue2);
assertEquals(2, object.setMethodCalled);
assertEquals(methodValue2, object.value);
assertEquals(1, object.contextSetCalled);
}
use of org.eclipse.e4.core.contexts.IEclipseContext in project eclipse.platform.runtime by eclipse.
the class ContextInjectionTest method testInjection.
/**
* Tests basic context injection
*/
@Test
public synchronized void testInjection() {
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 context
IEclipseContext context = EclipseContextFactory.create();
context.set(Integer.class, testInt);
context.set(String.class, testString);
context.set(Double.class, testDouble);
context.set(Float.class, testFloat);
context.set(Character.class, testChar);
ObjectBasic userObject = new ObjectBasic();
ContextInjectionFactory.inject(userObject, context);
// check field injection
assertEquals(testString, userObject.injectedString);
assertEquals(testInt, userObject.getInt());
// assertEquals(context, userObject.context);
// check method injection
assertEquals(1, userObject.setMethodCalled);
assertEquals(1, userObject.setMethodCalled2);
assertEquals(testDouble, userObject.d);
assertEquals(testFloat, userObject.f);
assertEquals(testChar, userObject.c);
// check post processing
assertTrue(userObject.finalized);
}
Aggregations