diff --git a/mvvmfx/src/main/java/de/saxsys/mvvmfx/ViewModel.java b/mvvmfx/src/main/java/de/saxsys/mvvmfx/ViewModel.java index f465a65bc..86cba9294 100644 --- a/mvvmfx/src/main/java/de/saxsys/mvvmfx/ViewModel.java +++ b/mvvmfx/src/main/java/de/saxsys/mvvmfx/ViewModel.java @@ -15,6 +15,7 @@ ******************************************************************************/ package de.saxsys.mvvmfx; +import de.saxsys.mvvmfx.utils.notifications.NotificationTestHelper; import javafx.application.Platform; import de.saxsys.mvvmfx.internal.viewloader.View; import de.saxsys.mvvmfx.utils.notifications.NotificationCenter; @@ -46,7 +47,15 @@ public interface ViewModel { /** * Publishes a notification to the subscribers of the messageName. This notification will be send to the - * UI-Thread. + * UI-Thread. + *
+ * + * This notification mechanism uses the {@link NotificationCenter} internally with the difference that messages send + * by this method aren't globally available. Instead they can only be received by this viewModels {@link #subscribe(String, NotificationObserver)} + * method or when using this viewModel instance as argument to the {@link NotificationCenter#subscribe(ViewModel, String, NotificationObserver)} method. + *
+ * + * See {@link NotificationTestHelper} for a utility that's purpose is to simplify unit tests with notifications. * * @param messageName * of the notification diff --git a/mvvmfx/src/main/java/de/saxsys/mvvmfx/utils/notifications/NotificationCenter.java b/mvvmfx/src/main/java/de/saxsys/mvvmfx/utils/notifications/NotificationCenter.java index 4724860b3..3d50d0509 100644 --- a/mvvmfx/src/main/java/de/saxsys/mvvmfx/utils/notifications/NotificationCenter.java +++ b/mvvmfx/src/main/java/de/saxsys/mvvmfx/utils/notifications/NotificationCenter.java @@ -24,6 +24,11 @@ * * {@code Object[]} with a notification. * + *
+ * + * There is a util {@link NotificationTestHelper} that's purpose is to simplify testing of notifications in Unit-Tests. + * + * * @author sialcasa * */ @@ -99,7 +104,7 @@ void unsubscribe(String messageName, * @param observer * which should execute when the notification occurs */ - void subscribe(ViewModel view, String messageName, + void subscribe(ViewModel viewModel, String messageName, NotificationObserver observer); /** @@ -118,7 +123,7 @@ void unsubscribe(ViewModel viewModel, String messageName, * @param viewModel * @param observer */ - void unsubscribe(ViewModel view, + void unsubscribe(ViewModel viewModel, NotificationObserver observer); } diff --git a/mvvmfx/src/main/java/de/saxsys/mvvmfx/utils/notifications/NotificationObserver.java b/mvvmfx/src/main/java/de/saxsys/mvvmfx/utils/notifications/NotificationObserver.java index 6aa4963ac..a800c1e9e 100644 --- a/mvvmfx/src/main/java/de/saxsys/mvvmfx/utils/notifications/NotificationObserver.java +++ b/mvvmfx/src/main/java/de/saxsys/mvvmfx/utils/notifications/NotificationObserver.java @@ -31,5 +31,5 @@ public interface NotificationObserver { * @param payload * which are passed */ - public void receivedNotification(String key, Object... payload); + void receivedNotification(String key, Object... payload); } diff --git a/mvvmfx/src/main/java/de/saxsys/mvvmfx/utils/notifications/NotificationTestHelper.java b/mvvmfx/src/main/java/de/saxsys/mvvmfx/utils/notifications/NotificationTestHelper.java index 9a9c06a8c..513274ebd 100644 --- a/mvvmfx/src/main/java/de/saxsys/mvvmfx/utils/notifications/NotificationTestHelper.java +++ b/mvvmfx/src/main/java/de/saxsys/mvvmfx/utils/notifications/NotificationTestHelper.java @@ -1,5 +1,6 @@ package de.saxsys.mvvmfx.utils.notifications; +import de.saxsys.mvvmfx.ViewModel; import javafx.application.Platform; import javafx.embed.swing.JFXPanel; import javafx.util.Pair; @@ -12,27 +13,102 @@ import java.util.concurrent.TimeoutException; /** + * The {@link NotificationTestHelper} is used to simplify the testing of + * notifications. It is especially useful when notifications are send from + * different thread and when testing the direct notification between a viewModel and the View + * (via {@link ViewModel#publish(String, Object...)} and {@link ViewModel#subscribe(String, NotificationObserver)}) + *
+ * This class implements {@link NotificationObserver} and therefore can be added as subscriber. It will record + * every received notification and can be tested afterwards. + *
+ * + * The {@link ViewModel#publish(String, Object...)} method will send all notifications on the JavaFX UI thread. + * Therefore when testing the publishing of notifications JavaFX has to be running which isn't the case + * with plain JUnit tests. The {@link NotificationTestHelper} will take care for thread handling. + * + *
+ * Example: + *
+ * + *
+ * + * public class MyViewModel implements ViewModel { + * public static final String ACTION_KEY = "my-action"; + * + * public void someAction() { + * ... + * publish(ACTION_KEY); + * } + * } + * + * // unit test + * {@code @Test} + * public void testSomething() { + * MyViewModel viewModel = new MyViewModel(); + * + * NotificationTestHelper helper = new NotificationTestHelper(); + * viewModel.subscribe(MyViewModel.ACTION_KEY, helper); + * + * + * viewModel.someAction(); + * + * assertEquals(1, helper.numberOfReceivedNotifications()); + * } + *+ * + * + * + * You can provide a timeout as constructor parameter. + * This is useful in case of asynchronous code (f.e. when notifications are send from another Thread). + * + * By default the timeout is set to {@value #DEFAULT_TIMEOUT}. When you have a long running thread + * you should use a higher timeout. + * * @author manuel.mauky */ public class NotificationTestHelper implements NotificationObserver { + public static final long DEFAULT_TIMEOUT = 0l; + private List