Skip to content

Commit

Permalink
Merge pull request #245 from sialcasa/235_included_resource_bundle
Browse files Browse the repository at this point in the history
#235 included resource bundle
  • Loading branch information
manuel-mauky committed May 15, 2015
2 parents 0b10dd3 + 89225a7 commit ae18401
Show file tree
Hide file tree
Showing 17 changed files with 204 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.saxsys.mvvmfx.contacts;

import java.util.Locale;
import java.util.ResourceBundle;

import de.saxsys.mvvmfx.MvvmFX;
Expand Down Expand Up @@ -27,6 +28,9 @@ public class App extends MvvmfxCdiApplication {
private static final Logger LOG = LoggerFactory.getLogger(App.class);

public static void main(String... args) {

Locale.setDefault(Locale.ENGLISH);

launch(args);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public class ResourceProvider {
* Due to the @Produces annotation this resource bundle can be injected in all views.
*/
@Produces
private ResourceBundle defaultResourceBundle = ResourceBundle.getBundle("default", Locale.ENGLISH);
private ResourceBundle defaultResourceBundle = ResourceBundle.getBundle("default");

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="de.saxsys.mvvmfx.contacts.ui.main.MainView">
<children>
<fx:include source="../menu/MenuView.fxml"/>
<fx:include source="../menu/MenuView.fxml" resources="menu"/>
<fx:include source="../toolbar/ToolbarView.fxml"/>
<SplitPane dividerPositions="0.7" VBox.vgrow="ALWAYS">
<items>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ master.table.city=Stadt
master.table.street=Straße
master.table.postalcode=PLZ

menu.file=Datei
menu.file.close=Schließen
menu.edit=Bearbeiten
menu.edit.removecontact=Kontakt entfernen

menu.help=Hilfe
menu.help.about=Über


toolbar.addcontactbutton=Kontakt hinzufügen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ master.table.city=City
master.table.street=Street
master.table.postalcode=ZIP code

menu.file=File
menu.file.close=Close
menu.edit=Edit
menu.edit.removecontact=Remove contact
menu.help=Help
menu.help.about=About

toolbar.addcontactbutton=Add contact
addressform.subdivision.label=Federal state\:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
menu.file=Datei
menu.file.close=Schließen
menu.edit=Bearbeiten
menu.edit.removecontact=Kontakt entfernen

menu.help=Hilfe
menu.help.about=Über
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

menu.file=File
menu.file.close=Close
menu.edit=Edit
menu.edit.removecontact=Remove contact
menu.help=Help
menu.help.about=About
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.ListResourceBundle;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.ResourceBundle;
Expand Down Expand Up @@ -51,13 +52,13 @@ public ResourceBundle getGlobalResourceBundle() {
public ResourceBundle mergeWithGlobal(ResourceBundle resourceBundle) {
if (globalResourceBundle == null) {
if (resourceBundle == null) {
return null;
return createEmptyBundle();
} else {
return resourceBundle;
return new ResourceBundleWrapper(resourceBundle);
}
} else {
if (resourceBundle == null) {
return globalResourceBundle;
return new ResourceBundleWrapper(globalResourceBundle);
} else {
return merge(resourceBundle, globalResourceBundle);
}
Expand Down Expand Up @@ -120,5 +121,51 @@ public String nextElement() {
return iterator.next();
}
}

/**
* This wrapper is needed due to a bug in FXMLLoader (https://javafx-jira.kenai.com/browse/RT-33764).
*
* With this class we make sure that there is a classLoader defined for this class.
*/
private static class ResourceBundleWrapper extends ResourceBundle {
private final ResourceBundle bundle;

ResourceBundleWrapper(ResourceBundle bundle) {
this.bundle = bundle;
}

@Override
protected Object handleGetObject(String key) {
return bundle.getObject(key);
}

@Override
public Enumeration<String> getKeys() {
return bundle.getKeys();
}

@Override
public boolean containsKey(String key) {
return bundle.containsKey(key);
}

@Override
public Locale getLocale() {
return bundle.getLocale();
}

@Override
public Set<String> keySet() {
return bundle.keySet();
}
}

private ResourceBundle createEmptyBundle() {
return new ListResourceBundle() {
@Override
protected Object[][] getContents() {
return new Object[0][];
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.ResourceBundle;

import static org.assertj.core.api.Assertions.assertThat;
import static de.saxsys.mvvmfx.internal.viewloader.ResourceBundleAssert.assertThat;
import static org.assertj.core.api.Assertions.fail;

/**
Expand Down Expand Up @@ -66,7 +67,7 @@ public void testLoadFxmlViewTuple() throws IOException {

final TestFxmlView codeBehind = viewTuple.getCodeBehind();
assertThat(codeBehind.getViewModel()).isNotNull();
assertThat(codeBehind.resourceBundle).isEqualTo(resourceBundle);
assertThat(codeBehind.resourceBundle).hasSameContent(resourceBundle);

assertThat(codeBehind.viewModelWasNull).isFalse();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package de.saxsys.mvvmfx.internal.viewloader;

import static org.assertj.core.api.Assertions.*;

import static de.saxsys.mvvmfx.internal.viewloader.ResourceBundleAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;

import java.io.StringReader;
import java.lang.reflect.Constructor;
Expand Down Expand Up @@ -365,7 +368,7 @@ class TestView extends VBox implements JavaView<TestViewModel> {
TestView loadedView = FluentViewLoader.javaView(TestView.class).resourceBundle(resourceBundle).load()
.getCodeBehind();

assertThat(loadedView.resources).isEqualTo(resourceBundle);
assertThat(loadedView.resources).hasSameContent(resourceBundle);
}


Expand Down Expand Up @@ -421,7 +424,9 @@ class TestView extends VBox implements JavaView<TestViewModel> {
TestView loadedView = FluentViewLoader.javaView(TestView.class).resourceBundle(resourceBundle).load()
.getCodeBehind();

assertThat(loadedView.resources).isEqualTo(resourceBundle);
assertThat(loadedView.resources).isInstanceOf(ResourceBundle.class);
ResourceBundle resourceBundle = (ResourceBundle)loadedView.resources;
assertThat(resourceBundle).isNotNull().hasSameContent(resourceBundle);
}

/**
Expand Down Expand Up @@ -455,7 +460,7 @@ public void initialize(URL location, ResourceBundle resources) {
TestView loadedView = FluentViewLoader.javaView(TestView.class).resourceBundle(resourceBundle).load()
.getCodeBehind();

assertThat(loadedView.resourceBundle).isEqualTo(resourceBundle);
assertThat(loadedView.resourceBundle).hasSameContent(resourceBundle);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package de.saxsys.mvvmfx.internal.viewloader;

import static org.assertj.core.api.Assertions.*;
import static de.saxsys.mvvmfx.internal.viewloader.ResourceBundleAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.StringReader;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import de.saxsys.mvvmfx.internal.viewloader.example.TestFxmlViewResourceBundleWithoutController;
import javafx.scene.layout.VBox;

import org.junit.Before;
Expand All @@ -17,8 +17,10 @@
import de.saxsys.mvvmfx.JavaView;
import de.saxsys.mvvmfx.ViewTuple;
import de.saxsys.mvvmfx.internal.viewloader.example.TestFxmlViewResourceBundle;
import de.saxsys.mvvmfx.internal.viewloader.example.TestFxmlViewResourceBundleWithoutController;
import de.saxsys.mvvmfx.internal.viewloader.example.TestViewModelWithResourceBundle;


/**
* This test focuses on the handling of {@link ResourceBundle}s.
*
Expand Down Expand Up @@ -46,10 +48,10 @@ public void success_fxml_injectionOfResourceBundles() {
final TestViewModelWithResourceBundle viewModel = viewTuple.getViewModel();
final TestFxmlViewResourceBundle view = viewTuple.getCodeBehind();

assertThat(view.resources).isNotNull().isEqualTo(resourceBundle);
assertThat(view.resourceBundle).isNotNull().isEqualTo(resourceBundle);
assertThat(view.resources).isNotNull().hasSameContent(resourceBundle);
assertThat(view.resourceBundle).isNotNull().hasSameContent(resourceBundle);

assertThat(viewModel.resourceBundle).isNotNull().isEqualTo(resourceBundle);
assertThat(viewModel.resourceBundle).isNotNull().hasSameContent(resourceBundle);
}

@Test
Expand All @@ -66,8 +68,8 @@ public void success_fxml_injectionWithExistingViewModel() {
final TestFxmlViewResourceBundle view = viewTuple.getCodeBehind();


assertThat(view.resourceBundle).isNotNull().isEqualTo(resourceBundle);
assertThat(viewModel.resourceBundle).isNotNull().isEqualTo(resourceBundle);
assertThat(view.resourceBundle).isNotNull().hasSameContent(resourceBundle);
assertThat(viewModel.resourceBundle).isNotNull().hasSameContent(resourceBundle);
}


Expand All @@ -85,8 +87,8 @@ public void success_fxml_existingCodeBehind() {
assertThat(viewTuple.getCodeBehind()).isEqualTo(codeBehind);
final TestViewModelWithResourceBundle viewModel = viewTuple.getViewModel();

assertThat(viewModel.resourceBundle).isEqualTo(resourceBundle);
assertThat(codeBehind.resourceBundle).isEqualTo(resourceBundle);
assertThat(viewModel.resourceBundle).hasSameContent(resourceBundle);
assertThat(codeBehind.resourceBundle).hasSameContent(resourceBundle);
}

@Test
Expand All @@ -107,24 +109,10 @@ public void success_fxml_existingCodeBehind_and_existingViewModel() {
assertThat(viewTuple.getCodeBehind()).isEqualTo(codeBehind);
assertThat(viewTuple.getViewModel()).isEqualTo(viewModel);

assertThat(viewModel.resourceBundle).isEqualTo(resourceBundle);
assertThat(codeBehind.resourceBundle).isEqualTo(resourceBundle);
}


@Test
public void fail_fxml_noResourceBundleProvidedOnLoad() {
try {
FluentViewLoader
.fxmlView(TestFxmlViewResourceBundle.class)
.load();
fail("Expected an IllegalStateException");
} catch (Exception e) {
assertThat(e).hasRootCauseInstanceOf(IllegalStateException.class);
}
assertThat(viewModel.resourceBundle).hasSameContent(resourceBundle);
assertThat(codeBehind.resourceBundle).hasSameContent(resourceBundle);
}


public static class TestJavaView extends VBox implements JavaView<TestViewModelWithResourceBundle> {
@InjectResourceBundle
ResourceBundle resourceBundle;
Expand All @@ -142,9 +130,9 @@ public void success_java_injectionOfResourceBundles() {
final TestViewModelWithResourceBundle viewModel = viewTuple.getViewModel();
final TestJavaView view = viewTuple.getCodeBehind();

assertThat(view.resourceBundle).isNotNull().isEqualTo(resourceBundle);
assertThat(view.resourceBundle).isNotNull().hasSameContent(resourceBundle);

assertThat(viewModel.resourceBundle).isNotNull().isEqualTo(resourceBundle);
assertThat(viewModel.resourceBundle).isNotNull().hasSameContent(resourceBundle);
}

@Test
Expand All @@ -161,20 +149,7 @@ public void success_java_injectionWithExistingViewModel() {
final TestJavaView view = viewTuple.getCodeBehind();


assertThat(view.resourceBundle).isNotNull().isEqualTo(resourceBundle);
assertThat(viewModel.resourceBundle).isNotNull().isEqualTo(resourceBundle);
}

@Test
public void fail_java_noResourceBundleProvidedOnLoad() {
try {
FluentViewLoader
.javaView(TestJavaView.class)
.load();
fail("Expected an IllegalStateException");
} catch (Exception e) {
assertThat(e).isInstanceOf(IllegalStateException.class);
}
assertThat(view.resourceBundle).isNotNull().hasSameContent(resourceBundle);
assertThat(viewModel.resourceBundle).isNotNull().hasSameContent(resourceBundle);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package de.saxsys.mvvmfx.internal.viewloader;

import org.assertj.core.api.AbstractAssert;

import java.util.ResourceBundle;

/**
* @author manuel.mauky
*/
public class ResourceBundleAssert extends AbstractAssert<ResourceBundleAssert, ResourceBundle> {

private ResourceBundleAssert(ResourceBundle actual) {
super(actual, ResourceBundleAssert.class);
}

public static ResourceBundleAssert assertThat(ResourceBundle resourceBundle){
return new ResourceBundleAssert(resourceBundle);
}

public ResourceBundleAssert hasSameContent(ResourceBundle other){
isNotNull();

if(other == null) {
failWithMessage("The given ResourceBundle may not be null");
return this;
}

if(actual.equals(other)) {
return this;
}

for (String key : actual.keySet()) {
final Object actualValue = actual.getObject(key);
if(!other.containsKey(key)){
failWithMessage("Expected given ResourceBundle to contain the key <%s>", key);
}

final Object otherValue = other.getObject(key);

if(!actualValue.equals(otherValue)){
failWithMessage("For the key <%s> the given ResourceBundle has a value of <%s> but a value of <%s> like in the actual ResourceBundle was expected", key, otherValue, actualValue);
}
}

return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ public void otherBundleIsNull() {
public void bothBundlesAreNull() {
final ResourceBundle merged = manager.mergeWithGlobal(null);

assertThat(merged).isNull();
assertThat(merged).isNotNull();
assertThat(merged.keySet()).isEmpty();
}


Expand Down
Loading

0 comments on commit ae18401

Please sign in to comment.