Search in sources :

Example 61 with IEclipseContext

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);
}
Also used : IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 62 with IEclipseContext

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());
}
Also used : IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 63 with IEclipseContext

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);
}
Also used : IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 64 with IEclipseContext

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);
}
Also used : IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 65 with IEclipseContext

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