diff --git a/mvvmfx-cdi/src/main/java/de/saxsys/jfx/mvvm/cdi/internal/WeldStartupHelper.java b/mvvmfx-cdi/src/main/java/de/saxsys/jfx/mvvm/cdi/internal/WeldStartupHelper.java index bc3186c8f..1423c5baa 100644 --- a/mvvmfx-cdi/src/main/java/de/saxsys/jfx/mvvm/cdi/internal/WeldStartupHelper.java +++ b/mvvmfx-cdi/src/main/java/de/saxsys/jfx/mvvm/cdi/internal/WeldStartupHelper.java @@ -16,6 +16,7 @@ package de.saxsys.jfx.mvvm.cdi.internal; import javafx.application.Application; +import javafx.application.HostServices; import javafx.stage.Stage; import org.jboss.weld.environment.se.Weld; @@ -23,6 +24,8 @@ import de.saxsys.jfx.mvvm.api.MvvmFX; +import javax.enterprise.inject.Produces; + /** * The class is instantiated by the javafx framework. The purpose of this class is to startup the weld container and * setup the {@link de.saxsys.jfx.mvvm.cdi.internal.CdiInjector} for the mvvmFX framework. @@ -62,4 +65,9 @@ public void start(Stage stage) throws Exception { weldContainer.event().fire(new StartupEvent(stage, this.getParameters())); } + + @Produces + HostServices produceHostServices(){ + return getHostServices(); + } } diff --git a/mvvmfx-cdi/src/test/java/de/saxsys/jfx/mvvm/cdi/internal/CdiInjectorTest.java b/mvvmfx-cdi/src/test/java/de/saxsys/jfx/mvvm/cdi/internal/CdiInjectorTest.java index 27d155915..ebb7163b0 100644 --- a/mvvmfx-cdi/src/test/java/de/saxsys/jfx/mvvm/cdi/internal/CdiInjectorTest.java +++ b/mvvmfx-cdi/src/test/java/de/saxsys/jfx/mvvm/cdi/internal/CdiInjectorTest.java @@ -17,6 +17,7 @@ import javax.inject.Inject; +import javafx.application.HostServices; import org.jboss.weld.environment.se.Weld; import org.jboss.weld.environment.se.WeldContainer; import org.junit.Assert; @@ -25,6 +26,8 @@ import de.saxsys.jfx.mvvm.notifications.NotificationCenter; import de.saxsys.jfx.mvvm.viewloader.ViewLoader; +import static org.assertj.core.api.Assertions.assertThat; + /** * This test verifies that a simple application can be started with the CDI/Weld extension. When there is a problem with * the cdi configuration, f.e. ambiguous dependencies for an internal class of mvvmfx, this kind of bug should be found @@ -38,6 +41,9 @@ static class Example { @Inject ViewLoader viewLoader; + + @Inject + HostServices hostServices; } /** @@ -52,14 +58,12 @@ public void testCdiInjector() { CdiInjector cdiInjector = weldContainer.instance().select(CdiInjector.class).get(); Object exampleObject = cdiInjector.call(Example.class); - Assert.assertNotNull(exampleObject); - - Assert.assertTrue(exampleObject instanceof Example); + assertThat(exampleObject).isNotNull().isInstanceOf(Example.class); Example example = (Example) exampleObject; - - Assert.assertNotNull(example.notificationCenter); - Assert.assertNotNull(example.viewLoader); - + + assertThat(example.notificationCenter).isNotNull(); + assertThat(example.viewLoader).isNotNull(); + assertThat(example.hostServices).isNotNull(); } } diff --git a/mvvmfx-guice/src/main/java/de/saxsys/jfx/mvvm/guice/MvvmfxGuiceApplication.java b/mvvmfx-guice/src/main/java/de/saxsys/jfx/mvvm/guice/MvvmfxGuiceApplication.java index 6ed4b5c17..9d5804a37 100644 --- a/mvvmfx-guice/src/main/java/de/saxsys/jfx/mvvm/guice/MvvmfxGuiceApplication.java +++ b/mvvmfx-guice/src/main/java/de/saxsys/jfx/mvvm/guice/MvvmfxGuiceApplication.java @@ -16,6 +16,11 @@ package de.saxsys.jfx.mvvm.guice; import java.util.List; + +import com.google.inject.AbstractModule; +import com.google.inject.Binder; +import com.google.inject.Provider; +import javafx.application.HostServices; import javafx.stage.Stage; import com.cathive.fx.guice.GuiceApplication; @@ -38,6 +43,17 @@ public abstract class MvvmfxGuiceApplication extends GuiceApplication { public final void init(List modules) throws Exception { modules.add(new MvvmfxModule()); + modules.add(new AbstractModule() { + @Override + protected void configure() { + bind(HostServices.class).toProvider(new Provider() { + @Override public HostServices get() { + return getHostServices(); + } + }); + } + }); + this.initGuiceModules(modules); } diff --git a/mvvmfx-guice/src/test/java/de/saxsys/jfx/mvvm/guice/MvvmfxGuiceIntegrationTest.java b/mvvmfx-guice/src/test/java/de/saxsys/jfx/mvvm/guice/MvvmfxGuiceIntegrationTest.java index 13825dfa5..7b11223d1 100644 --- a/mvvmfx-guice/src/test/java/de/saxsys/jfx/mvvm/guice/MvvmfxGuiceIntegrationTest.java +++ b/mvvmfx-guice/src/test/java/de/saxsys/jfx/mvvm/guice/MvvmfxGuiceIntegrationTest.java @@ -18,7 +18,11 @@ import static org.assertj.core.api.Assertions.*; import java.util.List; + +import de.saxsys.jfx.mvvm.notifications.NotificationCenter; +import de.saxsys.jfx.mvvm.viewloader.ViewLoader; import javafx.application.Application; +import javafx.application.HostServices; import javafx.application.Platform; import javafx.stage.Stage; @@ -26,6 +30,8 @@ import com.google.inject.Module; +import javax.inject.Inject; + public class MvvmfxGuiceIntegrationTest { /** @@ -41,6 +47,13 @@ public static class MyApplication extends MvvmfxGuiceApplication { public static Stage stage; public static Application.Parameters parameters; + + + @Inject + private GuiceInjector guiceInjector; + + //This is needed to be able to access the initialized guice injector from the outside + public static GuiceInjector staticInjector; public static void main(String... args) { launch(args); @@ -48,6 +61,8 @@ public static void main(String... args) { @Override public void startMvvmfx(Stage stage) throws Exception { + MyApplication.staticInjector = guiceInjector; + MyApplication.stage = stage; MyApplication.parameters = getParameters(); @@ -60,18 +75,46 @@ public void startMvvmfx(Stage stage) throws Exception { public void initGuiceModules(List modules) throws Exception { } } - + + /** + * This class is used to test the injection of some classes. + */ + static class Example { + @Inject + NotificationCenter notificationCenter; + + @Inject + ViewLoader viewLoader; + + @Inject + HostServices hostServices; + } /** * Verify that after running the application there is a valid stage. */ @Test - public void testApplicationWasStartedWithAStage() { + public void testApplicationStartAndInjection() { MyApplication.main("test"); assertThat(MyApplication.stage).isNotNull(); assertThat(MyApplication.parameters).isNotNull(); assertThat(MyApplication.parameters.getUnnamed()).contains("test"); + + + + + GuiceInjector injector = MyApplication.staticInjector; + assertThat(injector).isNotNull(); + + Object exampleObject = injector.call(Example.class); + assertThat(exampleObject).isNotNull().isInstanceOf(Example.class); + + Example example = (Example) exampleObject; + + assertThat(example.notificationCenter).isNotNull(); + assertThat(example.viewLoader).isNotNull(); + assertThat(example.hostServices).isNotNull(); } - + }