|
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2010 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.e4.core.tests.services.internal.annotations; |
| 12 |
|
| 13 |
import java.lang.reflect.InvocationTargetException; |
| 14 |
|
| 15 |
import javax.inject.Inject; |
| 16 |
import javax.inject.Named; |
| 17 |
|
| 18 |
import junit.framework.TestCase; |
| 19 |
|
| 20 |
import org.eclipse.e4.core.contexts.ContextInjectionFactory; |
| 21 |
import org.eclipse.e4.core.contexts.EclipseContextFactory; |
| 22 |
import org.eclipse.e4.core.contexts.IEclipseContext; |
| 23 |
import org.eclipse.e4.core.di.IDisposable; |
| 24 |
import org.eclipse.e4.core.di.annotations.PreDestroy; |
| 25 |
|
| 26 |
public class ExtraDependenciesTest extends TestCase { |
| 27 |
|
| 28 |
static public class TestObject { |
| 29 |
|
| 30 |
public String string; |
| 31 |
public Integer integer; |
| 32 |
public String other; |
| 33 |
|
| 34 |
public boolean disposed = false; |
| 35 |
|
| 36 |
@Inject |
| 37 |
public void injectedMethod(@Named("arg1") String strValue, @Named("arg2") Integer intValue, IEclipseContext context) { |
| 38 |
string = strValue; |
| 39 |
integer = intValue; |
| 40 |
if (context == null) { |
| 41 |
other = null; |
| 42 |
return; |
| 43 |
} |
| 44 |
IEclipseContext otherContext = (IEclipseContext) context.get("otherContext"); |
| 45 |
if (otherContext == null) |
| 46 |
other = null; |
| 47 |
else |
| 48 |
other = (String) otherContext.get("arg3"); |
| 49 |
} |
| 50 |
|
| 51 |
@PreDestroy |
| 52 |
public void finita() { |
| 53 |
disposed = true; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
public void testExtraDependencies() throws InvocationTargetException, InstantiationException { |
| 58 |
IEclipseContext context = EclipseContextFactory.create(); |
| 59 |
context.set("arg1", "abc"); |
| 60 |
context.set("arg2", new Integer (123)); |
| 61 |
|
| 62 |
IEclipseContext otherContext = EclipseContextFactory.create(); |
| 63 |
otherContext.set("arg3", "other"); |
| 64 |
|
| 65 |
context.set("otherContext", otherContext); |
| 66 |
|
| 67 |
TestObject object = (TestObject) ContextInjectionFactory.make(TestObject.class, context); |
| 68 |
|
| 69 |
// check that initial values are properly injected |
| 70 |
assertEquals("abc", object.string); |
| 71 |
assertEquals(new Integer(123), object.integer); |
| 72 |
assertEquals("other", object.other); |
| 73 |
|
| 74 |
// modify argument value to cause update - bug 308650 |
| 75 |
context.set("arg2", new Integer (789)); |
| 76 |
|
| 77 |
// change the "other" value; should not be propagated |
| 78 |
otherContext.set("arg3", "wrong"); |
| 79 |
assertEquals("other", object.other); |
| 80 |
|
| 81 |
// dispose the other context; should not cause disposal of the test object |
| 82 |
((IDisposable)otherContext).dispose(); |
| 83 |
assertEquals("other", object.other); |
| 84 |
assertFalse(object.disposed); |
| 85 |
|
| 86 |
// remove "other" context, should not be propagated |
| 87 |
context.remove("otherContext"); |
| 88 |
assertEquals("other", object.other); |
| 89 |
|
| 90 |
// check that changes in the method arguments are propagated |
| 91 |
context.set("arg1", "xyz"); |
| 92 |
context.set("arg2", new Integer (456)); |
| 93 |
assertEquals("xyz", object.string); |
| 94 |
assertEquals(new Integer(456), object.integer); |
| 95 |
assertNull(object.other); |
| 96 |
|
| 97 |
// check that disposal of the injected context causes disposal of the injected object |
| 98 |
((IDisposable)context).dispose(); |
| 99 |
assertTrue(object.disposed); |
| 100 |
assertNull(object.string); |
| 101 |
assertNull(object.integer); |
| 102 |
assertNull(object.other); |
| 103 |
} |
| 104 |
|
| 105 |
} |