Skip to content

Commit

Permalink
Merge pull request #100 from sialcasa/#99_hostServices_injectable
Browse files Browse the repository at this point in the history
#99 host services injectable
  • Loading branch information
manuel-mauky committed Jul 29, 2014
2 parents 2b53be4 + b61dd6c commit a9ebaef
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
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;
import org.jboss.weld.environment.se.WeldContainer;

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.
Expand Down Expand Up @@ -62,4 +65,9 @@ public void start(Stage stage) throws Exception {

weldContainer.event().fire(new StartupEvent(stage, this.getParameters()));
}

@Produces
HostServices produceHostServices(){
return getHostServices();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -38,6 +41,9 @@ static class Example {

@Inject
ViewLoader viewLoader;

@Inject
HostServices hostServices;
}

/**
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,6 +43,17 @@ public abstract class MvvmfxGuiceApplication extends GuiceApplication {
public final void init(List<Module> modules) throws Exception {
modules.add(new MvvmfxModule());

modules.add(new AbstractModule() {
@Override
protected void configure() {
bind(HostServices.class).toProvider(new Provider<HostServices>() {
@Override public HostServices get() {
return getHostServices();
}
});
}
});

this.initGuiceModules(modules);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@
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;

import org.junit.Test;

import com.google.inject.Module;

import javax.inject.Inject;

public class MvvmfxGuiceIntegrationTest {

/**
Expand All @@ -41,13 +47,22 @@ 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);
}

@Override
public void startMvvmfx(Stage stage) throws Exception {
MyApplication.staticInjector = guiceInjector;

MyApplication.stage = stage;

MyApplication.parameters = getParameters();
Expand All @@ -60,18 +75,46 @@ public void startMvvmfx(Stage stage) throws Exception {
public void initGuiceModules(List<Module> 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();
}

}

0 comments on commit a9ebaef

Please sign in to comment.