Bug 510916 - AdaptValueSupplier does not work for Command-Arguments
diff --git a/bundles/runtime/org.eclipse.fx.core.di.context.tests/src/org/eclipse/fx/core/di/context/tests/AdaptSupplierTestCase.java b/bundles/runtime/org.eclipse.fx.core.di.context.tests/src/org/eclipse/fx/core/di/context/tests/AdaptSupplierTestCase.java
index c0e89f2..1704643 100644
--- a/bundles/runtime/org.eclipse.fx.core.di.context.tests/src/org/eclipse/fx/core/di/context/tests/AdaptSupplierTestCase.java
+++ b/bundles/runtime/org.eclipse.fx.core.di.context.tests/src/org/eclipse/fx/core/di/context/tests/AdaptSupplierTestCase.java
@@ -29,7 +29,11 @@
 import org.eclipse.fx.core.adapter.Adapt;
 import org.eclipse.fx.core.adapter.AdapterProvider;
 import org.eclipse.fx.core.adapter.AdapterService.ValueAccess;
+import org.junit.After;
+import org.junit.AfterClass;
 import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.FrameworkUtil;
@@ -39,6 +43,11 @@
  *
  */
 public class AdaptSupplierTestCase {
+	private ServiceRegistration<AdapterProvider> r1;
+	private ServiceRegistration<AdapterProvider> r2;
+	private IEclipseContext testContext;
+	private static IEclipseContext SERVICE_CONTEXT;
+
 	static class Bean {
 		@Inject
 		@Adapt
@@ -72,8 +81,22 @@
 
 		int executeCalled;
 
+		Double callMeValue;
+
 		@Execute
 		public void callMe(@Adapt @Named("test") Double doubleValue) {
+			this.callMeValue = doubleValue;
+			this.executeCalled++;
+		}
+	}
+
+	public static class Bug510916 {
+		int executeCalled;
+		Double callMeValue;
+
+		@Execute
+		public void callMe(@Adapt @Named("bug510916") Double doubleValue) {
+			this.callMeValue = doubleValue;
 			this.executeCalled++;
 		}
 	}
@@ -115,26 +138,47 @@
 		}
 	}
 
+	@BeforeClass
+	public static void init() {
+		BundleContext context = FrameworkUtil.getBundle(AdaptSupplierTestCase.class).getBundleContext();
+		SERVICE_CONTEXT = EclipseContextFactory.createServiceContext(context);
+	}
+
+	@Before
+	public void setup() {
+		BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext();
+		this.r1 = context.registerService(AdapterProvider.class, new StringIntegerProvider(), new Hashtable<String,Object>());
+		this.r2 = context.registerService(AdapterProvider.class, new StringDoubleProvider(), new Hashtable<String,Object>());
+		this.testContext = SERVICE_CONTEXT.createChild("Test Context");
+	}
+
+	@After
+	public void cleanup() {
+		this.r1.unregister();
+		this.r2.unregister();
+		this.testContext.dispose();
+	}
+
+	@AfterClass
+	public static void shutdown() {
+		SERVICE_CONTEXT.dispose();
+	}
+
 	/**
 	 *
 	 */
 	@Test
 	public void testAdapt() {
-		BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext();
-		ServiceRegistration<AdapterProvider> r1 = context.registerService(AdapterProvider.class, new StringIntegerProvider(), new Hashtable<String,Object>());
-		ServiceRegistration<AdapterProvider> r2 = context.registerService(AdapterProvider.class, new StringDoubleProvider(), new Hashtable<String,Object>());
-
-		IEclipseContext serviceContext = EclipseContextFactory.getServiceContext(context);
-		ObjectSerializer serializer = serviceContext.get(ObjectSerializer.class);
+		ObjectSerializer serializer = testContext.get(ObjectSerializer.class);
 		Pojo pojo = new Pojo("Pojo 1");
-		serviceContext.set("serializedObject", serializer.serialize(pojo));
-		serviceContext.set("serializedList",serializer.serializeCollection(Arrays.asList(new Pojo("Pojo 2")), Pojo.class)); //$NON-NLS-2$
-		serviceContext.set("serializedSet", serializer.serializeCollection(Collections.singleton(new Pojo("Pojo 3")), Pojo.class)); //$NON-NLS-2$
+		testContext.set("serializedObject", serializer.serialize(pojo));
+		testContext.set("serializedList",serializer.serializeCollection(Arrays.asList(new Pojo("Pojo 2")), Pojo.class)); //$NON-NLS-2$
+		testContext.set("serializedSet", serializer.serializeCollection(Collections.singleton(new Pojo("Pojo 3")), Pojo.class)); //$NON-NLS-2$
 		LocalDateTime dateTime = LocalDateTime.now();
-		serviceContext.set("valueSerializer", dateTime.toString());
-		serviceContext.set("test", "12");  //$NON-NLS-1$//$NON-NLS-2$
+		testContext.set("valueSerializer", dateTime.toString());
+		testContext.set("test", "12");  //$NON-NLS-1$//$NON-NLS-2$
 
-		Bean bean = ContextInjectionFactory.make(Bean.class, serviceContext);
+		Bean bean = ContextInjectionFactory.make(Bean.class, testContext);
 
 		Assert.assertEquals(12,bean.integerValue.intValue());
 		Assert.assertEquals(12.0,bean.doubleValue.doubleValue(),0.0);
@@ -151,24 +195,41 @@
 		Assert.assertNotNull(bean.dateTime);
 		Assert.assertEquals(bean.dateTime, dateTime);
 
-		serviceContext.set("test", "14");
+		testContext.set("test", "14");
 
 		Assert.assertEquals(14,bean.integerValue.intValue());
 		Assert.assertEquals(14.0,bean.doubleValue.doubleValue(),0.0);
 
-		serviceContext.set("test", "15");
+		testContext.set("test", "15");
 
 		Assert.assertEquals(15,bean.integerValue.intValue());
 		Assert.assertEquals(15.0,bean.doubleValue.doubleValue(),0.0);
 
-		ContextInjectionFactory.invoke(bean, Execute.class, serviceContext);
+		ContextInjectionFactory.invoke(bean, Execute.class, testContext);
 		Assert.assertEquals(1,bean.executeCalled);
+		Assert.assertEquals(15.0, bean.callMeValue.doubleValue(),0.0);
 
-		serviceContext.set("test", "16");
+		testContext.set("test", "16");
 		Assert.assertEquals(1,bean.executeCalled);
+		Assert.assertEquals(15.0, bean.callMeValue.doubleValue(),0.0);
 
-		r1.unregister();
-		r2.unregister();
+		ContextInjectionFactory.invoke(bean, Execute.class, testContext);
+		Assert.assertEquals(2,bean.executeCalled);
+		Assert.assertEquals(16.0, bean.callMeValue.doubleValue(),0.0);
+
+	}
+
+	@Test
+	public void testBug510916() {
+		IEclipseContext staticContext = EclipseContextFactory.create("Static Context"); //$NON-NLS-1$
+		staticContext.set("bug510916", "17");  //$NON-NLS-1$//$NON-NLS-2$
+
+		Bug510916 bean = ContextInjectionFactory.make(Bug510916.class, this.testContext);
+		ContextInjectionFactory.invoke(bean, Execute.class, this.testContext, staticContext, null);
+
+		Assert.assertEquals(1,bean.executeCalled);
+		Assert.assertEquals(17.0, bean.callMeValue.doubleValue(),0.0);
+
 	}
 
 	static class StringIntegerProvider implements AdapterProvider<String, Integer> {
diff --git a/bundles/runtime/org.eclipse.fx.core.di.context/src/org/eclipse/fx/core/di/context/internal/AdaptValueSupplier.java b/bundles/runtime/org.eclipse.fx.core.di.context/src/org/eclipse/fx/core/di/context/internal/AdaptValueSupplier.java
index f4bdb5c..c0ed170 100644
--- a/bundles/runtime/org.eclipse.fx.core.di.context/src/org/eclipse/fx/core/di/context/internal/AdaptValueSupplier.java
+++ b/bundles/runtime/org.eclipse.fx.core.di.context/src/org/eclipse/fx/core/di/context/internal/AdaptValueSupplier.java
@@ -26,6 +26,7 @@
 import org.eclipse.e4.core.di.suppliers.ExtendedObjectSupplier;
 import org.eclipse.e4.core.di.suppliers.IObjectDescriptor;
 import org.eclipse.e4.core.di.suppliers.IRequestor;
+import org.eclipse.e4.core.internal.contexts.ContextObjectSupplier;
 import org.eclipse.e4.core.internal.di.Requestor;
 import org.eclipse.fx.core.ObjectSerializer;
 import org.eclipse.fx.core.adapter.Adapt;
@@ -58,9 +59,15 @@
 
 		AtomicInteger i = new AtomicInteger();
 		AtomicReference<Object> ref = new AtomicReference<>();
-		Dummy dummy = r.getInjector().make(Dummy.class, r.getPrimarySupplier());
+
+		ContextObjectSupplier primary = (ContextObjectSupplier) r.getPrimarySupplier();
+		ContextObjectSupplier temp = (ContextObjectSupplier) r.getTempSupplier();
+
+		IEclipseContext primaryContext = primary.getContext();
+		IEclipseContext tempContext = temp != null ? temp.getContext() : null;
+
 		if( track ) {
-			dummy.context.runAndTrack(new RunAndTrack() {
+			primaryContext.runAndTrack(new RunAndTrack() {
 
 				@Override
 				public boolean changed(IEclipseContext context) {
@@ -69,23 +76,28 @@
 						requestor.execute();
 						return false;
 					}
-					ref.set(dummy.context.get(key));
+					ref.set(primaryContext.get(key));
 					return true;
 				}
 			});
 		} else {
-			ref.set(dummy.context.get(key));
+			ref.set(primaryContext.get(key));
+			if( ref.get() == null && tempContext != null ) {
+				ref.set(tempContext.get(key));
+			}
 		}
 		if( ref.get() != null ) {
-			if( dummy.adapterService.canAdapt(ref.get(), desiredClass) ) {
-				return dummy.adapterService.adapt(ref.get(), desiredClass, new ValueAccessImpl(dummy.context));
+			AdapterService adapterService = primaryContext.get(AdapterService.class);
+			if( adapterService.canAdapt(ref.get(), desiredClass) ) {
+				return adapterService.adapt(ref.get(), desiredClass, new ValueAccessImpl(primaryContext));
 			}
 		}
 
 		try {
-			Object object = dummy.context.get(key);
+			Object object = ref.get();
+			System.err.println("====> value: " + object);
 			if( object instanceof String ) {
-				ObjectSerializer s = dummy.context.get(ObjectSerializer.class);
+				ObjectSerializer s = primaryContext.get(ObjectSerializer.class);
 				Type desiredType = descriptor.getDesiredType();
 				if (desiredType instanceof ParameterizedType) {
 					ParameterizedType t = (ParameterizedType) desiredType;