use of org.eclipse.e4.core.contexts.IEclipseContext in project eclipse.platform.runtime by eclipse.
the class ServiceContextTest method testServiceContextAsParent.
/**
* Tests accessing OSGi services through a child context that is not aware of them.
*/
@Test
public void testServiceContextAsParent() {
IEclipseContext child = context.createChild("child");
DebugOptions service = (DebugOptions) child.get(DebugOptions.class.getName());
assertNotNull(service);
}
use of org.eclipse.e4.core.contexts.IEclipseContext in project eclipse.platform.runtime by eclipse.
the class ServiceContextTest method testServiceRemovalOnContextDispose.
/**
* Tests that OSGi services are released when their context is disposed.
*/
@Test
public void testServiceRemovalOnContextDispose() {
StringPrintService stringPrint1 = new StringPrintService();
BundleContext bundleContext = CoreTestsActivator.getDefault().getBundleContext();
Bundle otherBundle = null;
for (Bundle b : bundleContext.getBundles()) {
if (b.getSymbolicName().equals("org.eclipse.core.tests.harness")) {
otherBundle = b;
break;
}
}
assertNotNull(otherBundle);
IEclipseContext otherServiceContext = EclipseContextFactory.getServiceContext(otherBundle.getBundleContext());
ServiceRegistration<?> reg1 = bundleContext.registerService(PrintService.SERVICE_NAME, stringPrint1, null);
try {
ServiceReference<?> ref = reg1.getReference();
PrintService service = (PrintService) otherServiceContext.get(PrintService.SERVICE_NAME);
assertEquals("1.0", stringPrint1, service);
assertEquals("1.1", 1, ref.getUsingBundles().length);
service = null;
otherServiceContext.dispose();
assertNull("2.0", ref.getUsingBundles());
} finally {
reg1.unregister();
}
}
use of org.eclipse.e4.core.contexts.IEclipseContext in project eclipse.platform.runtime by eclipse.
the class TestConstructorInjection method testConstructorInjection.
@Test
public void testConstructorInjection() throws InvocationTargetException, InstantiationException {
IEclipseContext context = EclipseContextFactory.create();
Integer intValue = Integer.valueOf(123);
context.set(Integer.class.getName(), intValue);
Boolean boolValue = Boolean.TRUE;
context.set(Boolean.class.getName(), boolValue);
Double doubleValue = Double.valueOf(1.23);
context.set(Double.class.getName(), doubleValue);
Float floatValue = Float.valueOf(12.3f);
context.set(Float.class.getName(), floatValue);
Character charValue = Character.valueOf('v');
context.set(Character.class.getName(), charValue);
Object result = ContextInjectionFactory.make(TestConstructorObject.class, context);
assertNotNull(result);
assertTrue(result instanceof TestConstructorObject);
TestConstructorObject testObject = ((TestConstructorObject) result);
assertFalse(testObject.defaultConstructorCalled);
assertFalse(testObject.constructorIntStrCalled);
assertTrue(testObject.constructorIntBoolCalled);
assertTrue(testObject.injectedMethodCalled);
assertFalse(testObject.nonInjectedMethodCalled);
assertTrue(testObject.orderCorrect);
assertEquals(intValue, testObject.i);
assertEquals(boolValue, testObject.b);
assertEquals(doubleValue, testObject.d);
assertEquals(floatValue, testObject.f);
assertEquals(charValue, testObject.getChar());
assertNull(testObject.s);
}
use of org.eclipse.e4.core.contexts.IEclipseContext in project eclipse.platform.runtime by eclipse.
the class InjectionOSGiTest method tearDown.
@After
public void tearDown() throws Exception {
bundle.start();
final BundleContext bundleContext = CoreTestsActivator.getDefault().getBundleContext();
final IEclipseContext localContext = EclipseContextFactory.getServiceContext(bundleContext);
ContextInjectionFactory.uninject(target, localContext);
}
use of org.eclipse.e4.core.contexts.IEclipseContext in project eclipse.platform.runtime by eclipse.
the class ContextExample method run.
/**
*/
public void run() {
IEclipseContext parent = EclipseContextFactory.create();
parent.set("complement", new ComplementaryColor());
IEclipseContext context = parent.createChild();
context.set("color", Color.YELLOW);
Crayon crayon = new Crayon();
ContextInjectionFactory.inject(crayon, context);
crayon.draw();
}
Aggregations