From 2705e80ef1b4f4f297cb46348896f238cbd6aa2b Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Mon, 24 Nov 2014 08:02:20 +0100 Subject: [PATCH] Fix broken tests and app exceptions due to ViewLoader changes --- src/main/java/io/bitsquare/gui/GuiModule.java | 9 +++++++- .../java/viewfx/view/support/ViewLoader.java | 23 +++++++++++++------ .../io/bitsquare/app/gui/ViewLoaderTests.java | 10 ++++++-- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/bitsquare/gui/GuiModule.java b/src/main/java/io/bitsquare/gui/GuiModule.java index 43acabac10..003fc35b4c 100644 --- a/src/main/java/io/bitsquare/gui/GuiModule.java +++ b/src/main/java/io/bitsquare/gui/GuiModule.java @@ -28,9 +28,13 @@ import io.bitsquare.gui.util.validation.BtcValidator; import io.bitsquare.gui.util.validation.FiatValidator; import io.bitsquare.gui.util.validation.InputValidator; import io.bitsquare.gui.util.validation.PasswordValidator; +import io.bitsquare.locale.BSResources; +import com.google.inject.Singleton; import com.google.inject.name.Names; +import java.util.ResourceBundle; + import viewfx.view.ViewFactory; import viewfx.view.support.ViewLoader; import viewfx.view.support.guice.GuiceViewFactory; @@ -50,7 +54,10 @@ public class GuiModule extends BitsquareModule { @Override protected void configure() { - bind(ViewFactory.class).to(GuiceViewFactory.class).asEagerSingleton(); + bind(GuiceViewFactory.class).in(Singleton.class); + bind(ViewFactory.class).to(GuiceViewFactory.class); + + bind(ResourceBundle.class).toInstance(BSResources.getResourceBundle()); bind(ViewLoader.class).asEagerSingleton(); bind(OfferBook.class).asEagerSingleton(); diff --git a/src/main/java/viewfx/view/support/ViewLoader.java b/src/main/java/viewfx/view/support/ViewLoader.java index a15db3c112..5245cadde7 100644 --- a/src/main/java/viewfx/view/support/ViewLoader.java +++ b/src/main/java/viewfx/view/support/ViewLoader.java @@ -17,10 +17,13 @@ package viewfx.view.support; +import java.io.IOException; + import java.net.URL; import java.util.HashMap; import java.util.Map; +import java.util.ResourceBundle; import javax.inject.Inject; @@ -33,10 +36,12 @@ public class ViewLoader { private final Map cache = new HashMap<>(); private final ViewFactory viewFactory; + private final ResourceBundle resourceBundle; @Inject - public ViewLoader(ViewFactory viewFactory) { + public ViewLoader(ViewFactory viewFactory, ResourceBundle resourceBundle) { this.viewFactory = viewFactory; + this.resourceBundle = resourceBundle; } public View load(URL url) { @@ -47,12 +52,16 @@ public class ViewLoader { if (useCaching && cache.containsKey(url)) return cache.get(url); - FXMLLoader loader = new FXMLLoader(url); - loader.setControllerFactory(viewFactory); - - View view = loader.getController(); - cache.put(url, view); - return view; + try { + FXMLLoader loader = new FXMLLoader(url, resourceBundle); + loader.setControllerFactory(viewFactory); + loader.load(); + View view = loader.getController(); + cache.put(url, view); + return view; + } catch (IOException ex) { + throw new RuntimeException("Failed to load View at location " + url, ex); + } } } diff --git a/src/test/java/io/bitsquare/app/gui/ViewLoaderTests.java b/src/test/java/io/bitsquare/app/gui/ViewLoaderTests.java index 4a1f5268a8..039911339d 100644 --- a/src/test/java/io/bitsquare/app/gui/ViewLoaderTests.java +++ b/src/test/java/io/bitsquare/app/gui/ViewLoaderTests.java @@ -20,12 +20,15 @@ package io.bitsquare.app.gui; import io.bitsquare.BitsquareException; import io.bitsquare.app.BitsquareEnvironment; import io.bitsquare.gui.Navigation; +import io.bitsquare.locale.BSResources; import com.google.inject.Guice; import com.google.inject.Injector; import java.net.MalformedURLException; +import java.util.ResourceBundle; + import viewfx.view.support.ViewLoader; import viewfx.view.support.guice.GuiceViewFactory; @@ -63,6 +66,7 @@ public class ViewLoaderTests { } private GuiceViewFactory viewFactory; + private ResourceBundle resourceBundle; @Before public void setUp() { @@ -71,15 +75,17 @@ public class ViewLoaderTests { Injector injector = Guice.createInjector(new BitsquareAppModule(env, TestApp.primaryStage)); viewFactory = injector.getInstance(GuiceViewFactory.class); viewFactory.setInjector(injector); + + resourceBundle = BSResources.getResourceBundle(); } @Test(expected = BitsquareException.class) public void loadingBogusFxmlResourceShouldThrow() throws MalformedURLException { - new ViewLoader(viewFactory).load(Navigation.Item.BOGUS.getFxmlUrl(), false); + new ViewLoader(viewFactory, resourceBundle).load(Navigation.Item.BOGUS.getFxmlUrl(), false); } @Test public void loadingValidFxmlResourceShouldNotThrow() { - new ViewLoader(viewFactory).load(Navigation.Item.ACCOUNT.getFxmlUrl(), false); + new ViewLoader(viewFactory, resourceBundle).load(Navigation.Item.ACCOUNT.getFxmlUrl(), false); } } \ No newline at end of file