Constructor.
38 | *
39 | * @param mediatorName mediator name
40 | * @param viewComponent view component
41 | */
42 | public Mediator(String mediatorName, Object viewComponent) {
43 | this.mediatorName = mediatorName != null ? mediatorName : NAME;
44 | this.viewComponent = viewComponent;
45 | }
46 |
47 | /**
48 | * Constructor.
49 | *
50 | * @param mediatorName mediator name
51 | */
52 | public Mediator(String mediatorName) {
53 | this(mediatorName, null);
54 | }
55 |
56 | /**
57 | * Constructor.
58 | */
59 | public Mediator() {
60 | this(null, null);
61 | }
62 |
63 | /**
64 | * Typically this will be handled in a switch statement,
77 | * with one 'case' entry per INotification
78 | * the Mediator
is interested in.
79 | */
80 | public void handleNotification(INotification notification) {
81 |
82 | }
83 |
84 | /**
85 | * Additionally, an implicit getter will usually
111 | * be defined in the subclass that casts the view
112 | * object to a type, like this:
113 | *
114 | * {@code
115 | * public javax.swing.JComboBox getViewComponent()
116 | * {
117 | * return viewComponent;
118 | * }
119 | *}
120 | *
121 | * @return the view component
122 | */
123 | public Object getViewComponent() {
124 | return viewComponent;
125 | }
126 |
127 | /**
128 | *
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.patterns.observer;
9 |
10 | import org.puremvc.java.multicore.interfaces.INotification;
11 |
12 | /**
13 | * A base INotification
implementation.
14 | *
15 | * PureMVC does not rely upon underlying event models such
16 | * as the one provided with Flash, and ActionScript 3 does
17 | * not have an inherent event model.
18 | *
19 | * The Observer Pattern as implemented within PureMVC exists
20 | * to support event-driven communication between the
21 | * application and the actors of the MVC triad.
22 | *
23 | * Notifications are not meant to be a replacement for Events
24 | * in Flex/Flash/Apollo. Generally, IMediator
implementors
25 | * place event listeners on their view components, which they
26 | * then handle in the usual way. This may lead to the broadcast of Notification
s to
27 | * trigger ICommand
s or to communicate with other IMediators
. IProxy
and ICommand
28 | * instances communicate with each other and IMediator
s
29 | * by broadcasting INotification
s.
30 | *
31 | * A key difference between Flash Event
s and PureMVC
32 | * Notification
s is that Event
s follow the
33 | * 'Chain of Responsibility' pattern, 'bubbling' up the display hierarchy
34 | * until some parent component handles the Event
, while
35 | * PureMVC Notification
s follow a 'Publish/Subscribe'
36 | * pattern. PureMVC classes need not be related to each other in a
37 | * parent/child relationship in order to communicate with one another
38 | * using Notification
s.
39 | *
40 | * @see Observer Observer
41 | *
42 | */
43 | public class Notification implements INotification {
44 |
45 | // the name of the notification instance
46 | private String name;
47 |
48 | // the type of the notification instance
49 | private String type;
50 |
51 | // the body of the notification instance
52 | private Object body;
53 |
54 | /**
55 | * Constructor.
56 | *
57 | * @param name name of the Notification
instance. (required)
58 | * @param body the Notification
body.
59 | * @param type the type of the Notification
60 | */
61 | public Notification(String name, Object body, String type) {
62 | this.name = name;
63 | this.body = body;
64 | this.type = type;
65 | }
66 |
67 | /**
68 | * Constructor.
69 | *
70 | * @param name name of the Notification
instance. (required)
71 | * @param body the Notification
body.
72 | */
73 | public Notification(String name, Object body) {
74 | this(name, body, null);
75 | }
76 |
77 | /**
78 | * Constructor.
79 | *
80 | * @param name name of the Notification
instance. (required)
81 | */
82 | public Notification(String name) {
83 | this(name, null, null);
84 | }
85 |
86 | /**
87 | * Get the name of the Notification
instance.
88 | *
89 | * @return the name of the Notification
instance.
90 | */
91 | public String getName() {
92 | return name;
93 | }
94 |
95 | /**
96 | * Set the body of the Notification
instance.
97 | */
98 | public void setBody(Object body) {
99 | this.body = body;
100 | }
101 |
102 | /**
103 | * Get the body of the Notification
instance.
104 | *
105 | * @return the body object.
106 | */
107 | public Object getBody() {
108 | return body;
109 | }
110 |
111 | /**
112 | * Set the type of the Notification
instance.
113 | */
114 | public void setType(String type) {
115 | this.type = type;
116 | }
117 |
118 | /**
119 | * Get the type of the Notification
instance.
120 | *
121 | * @return the type
122 | */
123 | public String getType() {
124 | return type;
125 | }
126 |
127 | /**
128 | * Get the string representation of the Notification
instance.
129 | *
130 | * @return the string representation of the Notification
instance.
131 | */
132 | public String toString() {
133 | return new StringBuilder("Notification Name: " + getName())
134 | .append("\nBody:" + ((body == null) ? "null" : body.toString()))
135 | .append("\nType:" + ((type == null) ? "null" : type))
136 | .toString();
137 | }
138 |
139 | }
140 |
--------------------------------------------------------------------------------
/src/main/java/org/puremvc/java/multicore/patterns/observer/Notifier.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.patterns.observer;
9 |
10 | import org.puremvc.java.multicore.interfaces.IFacade;
11 | import org.puremvc.java.multicore.interfaces.INotifier;
12 | import org.puremvc.java.multicore.patterns.facade.Facade;
13 |
14 | /**
15 | * A Base INotifier
implementation.
16 | *
17 | * MacroCommand, Command, Mediator
and Proxy
18 | * all have a need to send Notifications
.
19 | *
20 | * The INotifier
interface provides a common method called
21 | * sendNotification
that relieves implementation code of
22 | * the necessity to actually construct Notifications
.
23 | *
24 | * The Notifier
class, which all of the above mentioned classes
25 | * extend, provides an initialized reference to the Facade
26 | * Multiton, which is required for the convienience method
27 | * for sending Notifications
, but also eases implementation as these
28 | * classes have frequent Facade
interactions and usually require
29 | * access to the facade anyway.
30 | *
31 | * NOTE: In the MultiCore version of the framework, there is one caveat to
32 | * notifiers, they cannot send notifications or reach the facade until they
33 | * have a valid multitonKey.
34 | *
35 | * The multitonKey is set:
36 | * * on a Command when it is executed by the Controller
37 | * * on a Mediator is registered with the View
38 | * * on a Proxy is registered with the Model.
39 | *
40 | * @see org.puremvc.java.multicore.patterns.proxy.Proxy Proxy
41 | * @see Facade Facade
42 | * @see org.puremvc.java.multicore.patterns.mediator.Mediator Mediator
43 | * @see org.puremvc.java.multicore.patterns.command.MacroCommand MacroCommand
44 | * @see org.puremvc.java.multicore.patterns.command.SimpleCommand SimpleCommand
45 | */
46 | public class Notifier implements INotifier {
47 |
48 | protected String multitonKey;
49 |
50 | protected final String MULTITON_MSG = "multitonKey for this Notifier not yet initialized!";
51 |
52 | protected IFacade getFacade() {
53 | if(multitonKey == null) throw new RuntimeException(MULTITON_MSG);
54 | return Facade.getInstance(multitonKey, key -> new Facade(key));
55 | }
56 |
57 | /**
58 | * Create and send an INotification
.
59 | *
60 | * Keeps us from having to construct new INotification
61 | * instances in our implementation code.
62 | *
63 | * @param notificationName the name of the notiification to send
64 | * @param body the body of the notification
65 | * @param type the type of the notification
66 | */
67 | public void sendNotification(String notificationName, Object body, String type) {
68 | getFacade().sendNotification(notificationName, body, type);
69 | }
70 |
71 | /**
72 | * Create and send an INotification
.
73 | *
74 | * Keeps us from having to construct new INotification
75 | * instances in our implementation code.
76 | *
77 | * @param notificationName the name of the notiification to send
78 | * @param body the body of the notification
79 | */
80 | public void sendNotification(String notificationName, Object body) {
81 | getFacade().sendNotification(notificationName, body);
82 | }
83 |
84 | /**
85 | * Create and send an INotification
.
86 | *
87 | * Keeps us from having to construct new INotification
88 | * instances in our implementation code.
89 | *
90 | * @param notificationName the name of the notiification to send
91 | */
92 | public void sendNotification(String notificationName) {
93 | getFacade().sendNotification(notificationName);
94 | }
95 |
96 | /**
97 | * Initialize this INotifier instance.
98 | *
99 | * This is how a Notifier gets its multitonKey.
100 | * Calls to sendNotification or to access the
101 | * facade will fail until after this method
102 | * has been called.
103 | *
104 | * Mediators, Commands or Proxies may override
105 | * this method in order to send notifications
106 | * or access the Multiton Facade instance as
107 | * soon as possible. They CANNOT access the facade
108 | * in their constructors, since this method will not
109 | * yet have been called.
110 | *
111 | * @param key the multitonKey for this INotifier to use
112 | */
113 | public void initializeNotifier(String key) {
114 | multitonKey = key;
115 | }
116 | }
117 |
--------------------------------------------------------------------------------
/src/main/java/org/puremvc/java/multicore/patterns/observer/Observer.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.patterns.observer;
9 |
10 | import org.puremvc.java.multicore.interfaces.INotification;
11 | import org.puremvc.java.multicore.interfaces.IObserver;
12 |
13 | import java.util.function.Consumer;
14 |
15 | /**
16 | * A base IObserver
implementation.
17 | *
18 | * An Observer
is an object that encapsulates information
19 | * about an interested object with a method that should
20 | * be called when a particular INotification
is broadcast.
21 | *
22 | * In PureMVC, the Observer
class assumes these responsibilities:
23 | *
24 | *
25 | * - Encapsulate the notification (callback) method of the interested object.
26 | * - Encapsulate the notification context (this) of the interested object.
27 | * - Provide methods for setting the notification method and context.
28 | * - Provide a method for notifying the interested object.
29 | *
30 | *
31 | * @see org.puremvc.java.multicore.core.View View
32 | * @see org.puremvc.java.multicore.patterns.observer.Notification Notification
33 | */
34 | public class Observer implements IObserver {
35 |
36 | private Object context;
37 | private Consumer notify;
38 |
39 | /**
40 | * Constructor.
41 | *
42 | * The notification method on the interested object should take
43 | * one parameter of type INotification
44 | *
45 | * @param notifyMethod the notification method of the interested object
46 | * @param notifyContext the notification context of the interested object
47 | */
48 | public Observer(Consumer notifyMethod, Object notifyContext) {
49 | setNotifyMethod(notifyMethod);
50 | setNotifyContext(notifyContext);
51 | }
52 |
53 | /**
54 | * Compare an object to the notification context.
55 | *
56 | * @param object the object to compare
57 | * @return boolean indicating if the object and the notification context are the same
58 | */
59 | public boolean compareNotifyContext(Object object) {
60 | return object == this.context;
61 | }
62 |
63 | /**
64 | * Notify the interested object.
65 | *
66 | * @param notification the INotification
to pass to the interested object's notification method.
67 | */
68 | public void notifyObserver(INotification notification) {
69 | notify.accept(notification);
70 | }
71 |
72 | /**
73 | * Get the notification context.
74 | *
75 | * @return the notification context (this
) of the interested object.
76 | */
77 | protected Object getNotifyContext() {
78 | return context;
79 | }
80 |
81 | /**
82 | * Set the notification context.
83 | *
84 | * @param notifyContext the notification context (this) of the interested object.
85 | */
86 | public void setNotifyContext(Object notifyContext) {
87 | this.context = notifyContext;
88 | }
89 |
90 | /**
91 | * Get the notification method.
92 | *
93 | * @return the notification consumer of the interested object.
94 | */
95 | protected Consumer getNotifyMethod() {
96 | return notify;
97 | }
98 |
99 | /**
100 | * Set the notification method.
101 | *
102 | * The notification method should take one parameter of type INotification
.
103 | *
104 | * @param notify the notification (callback) method of the interested object.
105 | */
106 | public void setNotifyMethod(Consumer notify) {
107 | this.notify = notify;
108 | }
109 |
110 | }
111 |
--------------------------------------------------------------------------------
/src/main/java/org/puremvc/java/multicore/patterns/proxy/Proxy.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.patterns.proxy;
9 |
10 | import org.puremvc.java.multicore.interfaces.IProxy;
11 | import org.puremvc.java.multicore.patterns.observer.Notifier;
12 |
13 | /**
14 | * A base IProxy
implementation.
15 | *
16 | * In PureMVC, Proxy
classes are used to manage parts of the
17 | * application's data model.
18 | *
19 | * A Proxy
might simply manage a reference to a local data object,
20 | * in which case interacting with it might involve setting and
21 | * getting of its data in synchronous fashion.
22 | *
23 | * Proxy
classes are also used to encapsulate the application's
24 | * interaction with remote services to save or retrieve data, in which case,
25 | * we adopt an asyncronous idiom; setting data (or calling a method) on the
26 | * Proxy
and listening for a Notification
to be sent
27 | * when the Proxy
has retrieved the data from the service.
28 | *
29 | * @see org.puremvc.java.multicore.core.Model Model
30 | */
31 | public class Proxy extends Notifier implements IProxy {
32 |
33 | public static final String NAME = "Proxy";
34 |
35 | protected String proxyName;
36 |
37 | protected Object data;
38 |
39 | /**
40 | * Constructor.
41 | *
42 | * @param proxyName proxy name
43 | * @param data data
44 | */
45 | public Proxy(String proxyName, Object data) {
46 | this.proxyName = (proxyName != null) ? proxyName : NAME;
47 | if(data != null) setData(data);
48 | }
49 |
50 | /**
51 | * Constructor.
52 | *
53 | * @param proxyName proxy name
54 | */
55 | public Proxy(String proxyName) {
56 | this(proxyName, null);
57 | }
58 |
59 | /**
60 | * Constructor.
61 | */
62 | public Proxy(){
63 | this(null, null);
64 | }
65 |
66 | /**
67 | * Called by the Model when the Proxy is registered
68 | */
69 | public void onRegister() {
70 |
71 | }
72 |
73 | /**
74 | * Called by the Model when the Proxy is removed
75 | */
76 | public void onRemove() {
77 |
78 | }
79 |
80 | /**
81 | * Get the proxy name
82 | */
83 | public String getProxyName() {
84 | return proxyName;
85 | }
86 |
87 | /**
88 | * Get the data object
89 | */
90 | public Object getData() {
91 | return data;
92 | }
93 |
94 | /**
95 | * Set the data object
96 | */
97 | public void setData(Object data) {
98 | this.data = data;
99 | }
100 |
101 | }
102 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/core/ControllerTestCommand.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.core;
9 |
10 | import org.puremvc.java.multicore.interfaces.INotification;
11 | import org.puremvc.java.multicore.patterns.command.SimpleCommand;
12 |
13 | /**
14 | * A SimpleCommand subclass used by ControllerTest.
15 | *
16 | * @see ControllerTest ControllerTest
17 | * @see ControllerTestVO ControllerTestVO
18 | */
19 | public class ControllerTestCommand extends SimpleCommand {
20 |
21 | /**
22 | * Fabricate a result by multiplying the input by 2
23 | *
24 | * @param notification the note carrying the ControllerTestVO
25 | */
26 | public void execute(INotification notification) {
27 | ControllerTestVO vo = (ControllerTestVO)notification.getBody();
28 |
29 | // Fabricate a result
30 | vo.result = 2 * vo.input;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/core/ControllerTestCommand2.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.core;
9 |
10 | import org.puremvc.java.multicore.interfaces.INotification;
11 | import org.puremvc.java.multicore.patterns.command.SimpleCommand;
12 |
13 | public class ControllerTestCommand2 extends SimpleCommand {
14 |
15 | /**
16 | * Fabricate a result by multiplying the input by 2 and adding to the existing result
17 | *
18 | * This tests accumulation effect that would show if the command were executed more than once.
19 | *
20 | * @param notification the note carrying the ControllerTestVO
21 | */
22 | public void execute(INotification notification) {
23 | ControllerTestVO vo = (ControllerTestVO)notification.getBody();
24 |
25 | // Fabricate a result
26 | vo.result = vo.result + (2 * vo.input);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/core/ControllerTestVO.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.core;
9 |
10 | /**
11 | * A utility class used by ControllerTest.
12 | *
13 | * @see ControllerTest ControllerTest
14 | * @see org.puremvc.java.multicore.core.ControllerTestCommand ControllerTestCommand
15 | */
16 | public class ControllerTestVO {
17 |
18 | int input = 0;
19 | int result = 0;
20 |
21 | ControllerTestVO(int input) {
22 | this.input = input;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/core/ModelTest.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.core;
9 |
10 | import org.junit.jupiter.api.Assertions;
11 | import org.junit.jupiter.api.Test;
12 | import org.puremvc.java.multicore.interfaces.IModel;
13 | import org.puremvc.java.multicore.interfaces.IProxy;
14 | import org.puremvc.java.multicore.patterns.proxy.Proxy;
15 |
16 | /**
17 | * Test the PureMVC Model class.
18 | */
19 | public class ModelTest {
20 |
21 | /**
22 | * Tests the Model Multiton Factory Method
23 | */
24 | public void testGetInstance() {
25 | // Test Factory Method
26 | IModel model = Model.getInstance("ModelTestKey1", key -> new Model(key));
27 |
28 | // test assertions
29 | Assertions.assertNotNull(model, "Expecting instance not null");
30 | Assertions.assertNotNull((IModel) model, "Expecting instance implements IModel");
31 | }
32 |
33 | /**
34 | * Tests the proxy registration and retrieval methods.
35 | *
36 | * Tests registerProxy
and retrieveProxy
in the same test.
37 | * These methods cannot currently be tested separately
38 | * in any meaningful way other than to show that the
39 | * methods do not throw exception when called.
40 | */
41 | public void testRegisterAndRetrieveProxy() {
42 | // register a proxy and retrieve it.
43 | IModel model = Model.getInstance("ModelTestKey2", key -> new Model(key));
44 | model.registerProxy(new Proxy("colors", new String[]{"red", "green", "blue"}));
45 | IProxy proxy = model.retrieveProxy("colors");
46 | String[] data = (String[]) proxy.getData();
47 |
48 | // test assertions
49 | Assertions.assertNotNull(data, "Expecting data not null");
50 | Assertions.assertNotNull((String[])data, "Expecting data type is Array");
51 | Assertions.assertTrue(data.length == 3, "Expecting data.length == 3");
52 | Assertions.assertTrue(data[0] == "red", "Expecting data[0] == 'red'");
53 | Assertions.assertTrue(data[1] == "green", "Expecting data[1] == 'green'");
54 | Assertions.assertTrue(data[2] == "blue", "Expecting data[2] == 'blue'");
55 | }
56 |
57 | /**
58 | * Tests the proxy removal method.
59 | */
60 | @Test
61 | public void testRegisterAndRemoveProxy() {
62 | // register a proxy, remove it, then try to retrieve it
63 | IModel model = Model.getInstance("ModelTestKey4", key -> new Model(key));
64 | IProxy proxy = new Proxy("sizes", new String[]{"7", "13", "21"});
65 | model.registerProxy(proxy);
66 |
67 | // remove the proxy
68 | IProxy removedProxy = model.removeProxy("sizes");
69 |
70 | // assert that we removed the appropriate proxy
71 | Assertions.assertTrue(removedProxy.getProxyName() == "sizes", "Expecting removedProxy.getProxyName() == 'sizes'");
72 |
73 | // ensure that the proxy is no longer retrievable from the model
74 | proxy = model.retrieveProxy("sizes");
75 |
76 | // test assertions
77 | Assertions.assertNull(proxy, "Expecting proxy is null");
78 | }
79 |
80 | /**
81 | * Tests the hasProxy Method
82 | */
83 | @Test
84 | public void testHasProxy() {
85 | // register a proxy
86 | IModel model = Model.getInstance("ModelTestKey4", key -> new Model(key));
87 | IProxy proxy = new Proxy("aces", new String[]{"clubs", "spades", "hearts", "diamonds"});
88 | model.registerProxy(proxy);
89 |
90 | // assert that the model.hasProxy method returns true
91 | // for that proxy name
92 | Assertions.assertTrue(model.hasProxy("aces") == true, "Expecting model.hasProxy('aces') == true");
93 |
94 | // remove the proxy
95 | model.removeProxy("aces");
96 |
97 | // assert that the model.hasProxy method returns false
98 | // for that proxy name
99 | Assertions.assertTrue(model.hasProxy("aces") == false, "Expecting model.hasProxy('aces') == false");
100 | }
101 |
102 | /**
103 | * Tests that the Model calls the onRegister and onRemove methods
104 | */
105 | @Test
106 | public void testOnRegisterAndOnRemove() {
107 | // Get a Multiton View instance
108 | IModel model = Model.getInstance("ModelTestKey4", key -> new Model(key));
109 |
110 | // Create and register the test mediator
111 | IProxy proxy = new ModelTestProxy();
112 | model.registerProxy(proxy);
113 |
114 | // assert that onRegsiter was called, and the proxy responded by setting its data accordingly
115 | Assertions.assertTrue(proxy.getData() == ModelTestProxy.ON_REGISTER_CALLED, "Expecting proxy.getData() == ModelTestProxy.ON_REGISTER_CALLED");
116 |
117 | // Remove the component
118 | model.removeProxy(ModelTestProxy.NAME);
119 |
120 | // assert that onRemove was called, and the proxy responded by setting its data accordingly
121 | Assertions.assertTrue(proxy.getData() == ModelTestProxy.ON_REMOVE_CALLED, "Expecting proxy.getData() == ModelTestProxy.ON_REMOVE_CALLED");
122 | }
123 |
124 | }
125 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/core/ModelTestProxy.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.core;
9 |
10 | import org.puremvc.java.multicore.patterns.proxy.Proxy;
11 |
12 | public class ModelTestProxy extends Proxy {
13 |
14 | public static final String NAME = "ModelTestProxy";
15 | public static final String ON_REGISTER_CALLED = "onRegister Called";
16 | public static final String ON_REMOVE_CALLED = "onRemove Called";
17 |
18 | public ModelTestProxy() {
19 | super(NAME, "");
20 | }
21 |
22 | @Override
23 | public void onRegister() {
24 | setData(ON_REGISTER_CALLED);
25 | }
26 |
27 | public void onRemove() {
28 | setData(ON_REMOVE_CALLED);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/core/ViewTestMediator.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.core;
9 |
10 | import org.puremvc.java.multicore.interfaces.IMediator;
11 | import org.puremvc.java.multicore.patterns.mediator.Mediator;
12 |
13 | /**
14 | * A Mediator class used by ViewTest.
15 | *
16 | * @see ViewTest ViewTest
17 | */
18 | public class ViewTestMediator extends Mediator implements IMediator {
19 |
20 | /**
21 | * The Mediator name
22 | */
23 | public static final String NAME = "ViewTestMediator";
24 |
25 | /**
26 | * Constructor
27 | */
28 | public ViewTestMediator(Object view) {
29 | super (NAME, view);
30 | }
31 |
32 | public String[] listNotificationInterests() {
33 | // be sure that the mediator has some Observers created
34 | // in order to test removeMediator
35 | return new String[] { "ABC", "DEF", "GHI"};
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/core/ViewTestMediator2.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.core;
9 |
10 | import org.puremvc.java.multicore.interfaces.IMediator;
11 | import org.puremvc.java.multicore.interfaces.INotification;
12 | import org.puremvc.java.multicore.patterns.mediator.Mediator;
13 |
14 | /**
15 | * A Mediator class used by ViewTest.
16 | *
17 | * @see ViewTest ViewTest
18 | */
19 | public class ViewTestMediator2 extends Mediator implements IMediator {
20 |
21 | /**
22 | * The Mediator name
23 | */
24 | public static final String NAME = "ViewTestMediator2";
25 |
26 | /**
27 | * Constructor
28 | */
29 | public ViewTestMediator2(Object view) {
30 | super(NAME, view);
31 | }
32 |
33 | public String[] listNotificationInterests() {
34 | // be sure that the mediator has some Observers created
35 | // in order to test removeMediator
36 | return new String[] {ViewTest.NOTE1, ViewTest.NOTE2};
37 | }
38 |
39 | public void handleNotification(INotification notification) {
40 | getViewTest().lastNotification = notification.getName();
41 | }
42 |
43 | public ViewTest getViewTest() {
44 | return (ViewTest) viewComponent;
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/core/ViewTestMediator3.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.core;
9 |
10 | import org.puremvc.java.multicore.interfaces.IMediator;
11 | import org.puremvc.java.multicore.interfaces.INotification;
12 | import org.puremvc.java.multicore.patterns.mediator.Mediator;
13 |
14 | /**
15 | * A Mediator class used by ViewTest.
16 | *
17 | * @see ViewTest ViewTest
18 | */
19 | public class ViewTestMediator3 extends Mediator implements IMediator {
20 |
21 | /**
22 | * The Mediator name
23 | */
24 | public static final String NAME = "ViewTestMediator3";
25 |
26 | /**
27 | * Constructor
28 | */
29 | public ViewTestMediator3(Object view) {
30 | super(NAME, view);
31 | }
32 |
33 | @Override
34 | public String[] listNotificationInterests() {
35 | // be sure that the mediator has some Observers created
36 | // in order to test removeMediator
37 | return new String[] {ViewTest.NOTE3};
38 | }
39 |
40 | @Override
41 | public void handleNotification(INotification notification) {
42 | getViewTest().lastNotification = notification.getName();
43 | }
44 |
45 | public ViewTest getViewTest() {
46 | return (ViewTest) viewComponent;
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/core/ViewTestMediator4.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.core;
9 |
10 | import org.puremvc.java.multicore.interfaces.IMediator;
11 | import org.puremvc.java.multicore.patterns.mediator.Mediator;
12 |
13 | /**
14 | * A Mediator class used by ViewTest.
15 | *
16 | * @see ViewTest ViewTest
17 | */
18 | public class ViewTestMediator4 extends Mediator implements IMediator {
19 |
20 | /**
21 | * The Mediator name
22 | */
23 | public static final String NAME = "ViewTestMediator4";
24 |
25 | /**
26 | * Constructor
27 | */
28 | public ViewTestMediator4(Object view) {
29 | super(NAME, view);
30 | }
31 |
32 | public ViewTest getViewTest() {
33 | return (ViewTest) viewComponent;
34 | }
35 |
36 | @Override
37 | public void onRegister() {
38 | getViewTest().onRegisterCalled = true;
39 | }
40 |
41 | @Override
42 | public void onRemove() {
43 | getViewTest().onRemoveCalled = true;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/core/ViewTestMediator5.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.core;
9 |
10 | import org.puremvc.java.multicore.interfaces.IMediator;
11 | import org.puremvc.java.multicore.interfaces.INotification;
12 | import org.puremvc.java.multicore.patterns.mediator.Mediator;
13 |
14 | /**
15 | * A Mediator class used by ViewTest.
16 | *
17 | * @see ViewTest ViewTest
18 | */
19 | public class ViewTestMediator5 extends Mediator implements IMediator {
20 |
21 | /**
22 | * The Mediator name
23 | */
24 | public static final String NAME = "ViewTestMediator5";
25 |
26 | /**
27 | * Constructor
28 | */
29 | public ViewTestMediator5(Object view) {
30 | super(NAME, view);
31 | }
32 |
33 | public String[] listNotificationInterests() {
34 | return new String[] {ViewTest.NOTE5};
35 | }
36 |
37 | public void handleNotification(INotification notification) {
38 | getViewTest().counter++;
39 | }
40 |
41 | public ViewTest getViewTest() {
42 | return (ViewTest) viewComponent;
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/core/ViewTestMediator6.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.core;
9 |
10 | import org.puremvc.java.multicore.interfaces.INotification;
11 | import org.puremvc.java.multicore.patterns.mediator.Mediator;
12 |
13 | /**
14 | * A Mediator class used by ViewTest.
15 | *
16 | * @see ViewTest ViewTest
17 | */
18 | public class ViewTestMediator6 extends Mediator {
19 |
20 | /**
21 | * The Mediator base name
22 | */
23 | public static final String NAME = "ViewTestMediator6";
24 |
25 | /**
26 | * Constructor
27 | */
28 | public ViewTestMediator6(String name, Object view) {
29 | super(name, view);
30 | }
31 |
32 | @Override
33 | public String[] listNotificationInterests() {
34 | return new String[]{ViewTest.NOTE6};
35 | }
36 |
37 | @Override
38 | public void handleNotification(INotification notification) {
39 | getFacade().removeMediator(getMediatorName());
40 | }
41 |
42 | @Override
43 | public void onRemove() {
44 | viewTest().counter++;
45 | }
46 |
47 | public ViewTest viewTest() {
48 | return (ViewTest)viewComponent;
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/core/ViewTestNote.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.core;
9 |
10 | import org.puremvc.java.multicore.interfaces.INotification;
11 | import org.puremvc.java.multicore.patterns.observer.Notification;
12 |
13 | /**
14 | * A Notification class used by ViewTest.
15 | *
16 | * @see ViewTest ViewTest
17 | */
18 | public class ViewTestNote extends Notification implements INotification {
19 |
20 | /**
21 | * The name of this Notification.
22 | */
23 | public static final String NAME = "ViewTestNote";
24 |
25 | /**
26 | * Constructor.
27 | *
28 | * @param name Ignored and forced to NAME.
29 | * @param body the body of the Notification to be constructed.
30 | */
31 | public ViewTestNote(String name, Object body) {
32 | super(name, body);
33 | }
34 |
35 | /**
36 | * Factory method.
37 | *
38 | * This method creates new instances of the ViewTestNote class,
39 | * automatically setting the note name so you don't have to. Use
40 | * this as an alternative to the constructor.
41 | *
42 | * @param body the body of the Notification to be constructed.
43 | */
44 | public static INotification create(Object body) {
45 | return new ViewTestNote(NAME, body);
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/patterns/command/MacroCommandTest.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.patterns.command;
9 |
10 | import org.junit.jupiter.api.Assertions;
11 | import org.junit.jupiter.api.Test;
12 | import org.puremvc.java.multicore.interfaces.INotification;
13 | import org.puremvc.java.multicore.patterns.observer.Notification;
14 |
15 | /**
16 | * Test the PureMVC SimpleCommand class.
17 | *
18 | * @see org.puremvc.java.multicore.patterns.command.MacroCommandTestVO MacroCommandTestVO
19 | * @see org.puremvc.java.multicore.patterns.command.MacroCommandTestCommand MacroCommandTestCommand
20 | */
21 | public class MacroCommandTest {
22 |
23 | /**
24 | * Tests operation of a MacroCommand
.
25 | *
26 | * This test creates a new Notification
, adding a
27 | * MacroCommandTestVO
as the body.
28 | * It then creates a MacroCommandTestCommand
and invokes
29 | * its execute
method, passing in the
30 | * Notification
.
31 | *
32 | *
The MacroCommandTestCommand
has defined an
33 | * initializeMacroCommand
method, which is
34 | * called automatically by its constructor. In this method
35 | * the MacroCommandTestCommand
adds 2 SubCommands
36 | * to itself, MacroCommandTestSub1Command
and
37 | * MacroCommandTestSub2Command
.
38 | *
39 | *
The MacroCommandTestVO
has 2 result properties,
40 | * one is set by MacroCommandTestSub1Command
by
41 | * multiplying the input property by 2, and the other is set
42 | * by MacroCommandTestSub2Command
by multiplying
43 | * the input property by itself.
44 | *
45 | *
Success is determined by evaluating the 2 result properties
46 | * on the MacroCommandTestVO
that was passed to
47 | * the MacroCommandTestCommand
on the Notification
48 | * body.
49 | */
50 | @Test
51 | public void testMacroCommandExecute() {
52 |
53 | // Create the VO
54 | MacroCommandTestVO vo = new MacroCommandTestVO(5);
55 |
56 | // Create the Notification (note)
57 | INotification note = new Notification("MacroCommandTest", vo, null);
58 |
59 | // Create the SimpleCommand
60 | MacroCommandTestCommand command = new MacroCommandTestCommand();
61 | command.initializeNotifier("test");
62 |
63 | // Execute the SimpleCommand
64 | command.execute(note);
65 |
66 | // test assertions
67 | Assertions.assertTrue(vo.result1 == 10, "Expecting vo.result1 == 10 " + vo.result1);
68 | Assertions.assertTrue(vo.result2 == 25, "Expecing vo.result2 == 25");
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/patterns/command/MacroCommandTestCommand.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.patterns.command;
9 |
10 | /**
11 | * A MacroCommand subclass used by MacroCommandTest.
12 | *
13 | * @see MacroCommandTest MacroCommandTest
14 | * @see org.puremvc.java.multicore.patterns.command.MacroCommandTestSub1Command MacroCommandTestSub1Command
15 | * @see org.puremvc.java.multicore.patterns.command.MacroCommandTestSub2Command MacroCommandTestSub2Command
16 | * @see MacroCommandTestVO MacroCommandTestVO
17 | */
18 | public class MacroCommandTestCommand extends MacroCommand {
19 |
20 | /**
21 | * Initialize the MacroCommandTestCommand by adding
22 | * its 2 SubCommands.
23 | */
24 | protected void initializeMacroCommand() {
25 | addSubCommand(() -> new MacroCommandTestSub1Command());
26 | addSubCommand(() -> new MacroCommandTestSub2Command());
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/patterns/command/MacroCommandTestSub1Command.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.patterns.command;
9 |
10 | import org.puremvc.java.multicore.interfaces.INotification;
11 |
12 | /**
13 | * A SimpleCommand subclass used by MacroCommandTestCommand.
14 | *
15 | * @see MacroCommandTest MacroCommandTest
16 | * @see MacroCommandTestCommand MacroCommandTestCommand
17 | * @see MacroCommandTestVO MacroCommandTestVO
18 | */
19 | public class MacroCommandTestSub1Command extends SimpleCommand {
20 |
21 | /**
22 | * Fabricate a result by multiplying the input by 2
23 | *
24 | * @param notification the IEvent
carrying the MacroCommandTestVO
25 | */
26 | public void execute(INotification notification) {
27 | MacroCommandTestVO vo = (MacroCommandTestVO)notification.getBody();
28 |
29 | // Fabricate a result
30 | vo.result1 = 2 * vo.input;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/patterns/command/MacroCommandTestSub2Command.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.patterns.command;
9 |
10 | import org.puremvc.java.multicore.interfaces.INotification;
11 |
12 | /**
13 | * A SimpleCommand subclass used by MacroCommandTestCommand.
14 | *
15 | * @see MacroCommandTest MacroCommandTest
16 | * @see MacroCommandTestCommand MacroCommandTestCommand
17 | * @see MacroCommandTestVO MacroCommandTestVO
18 | */
19 | public class MacroCommandTestSub2Command extends SimpleCommand {
20 |
21 | /**
22 | * Fabricate a result by multiplying the input by itself
23 | *
24 | * @param notification the IEvent
carrying the MacroCommandTestVO
25 | */
26 | public void execute(INotification notification) {
27 | MacroCommandTestVO vo = (MacroCommandTestVO)notification.getBody();
28 |
29 | // Fabricate a result
30 | vo.result2 = vo.input * vo.input;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/patterns/command/MacroCommandTestVO.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.patterns.command;
9 |
10 | /**
11 | * A utility class used by MacroCommandTest.
12 | *
13 | * @see MacroCommandTest MacroCommandTest
14 | * @see org.puremvc.java.multicore.patterns.command.MacroCommandTestCommand MacroCommandTestCommand
15 | * @see org.puremvc.java.multicore.patterns.command.MacroCommandTestSub1Command MacroCommandTestSub1Command
16 | * @see org.puremvc.java.multicore.patterns.command.MacroCommandTestSub2Command MacroCommandTestSub2Command
17 | */
18 | public class MacroCommandTestVO {
19 |
20 | public int input;
21 | public int result1;
22 | public int result2;
23 |
24 | /**
25 | * Constructor.
26 | *
27 | * @param input the number to be fed to the MacroCommandTestCommand
28 | */
29 | public MacroCommandTestVO(int input) {
30 | this.input = input;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/patterns/command/SimpleCommandTest.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.patterns.command;
9 |
10 | import org.junit.jupiter.api.Assertions;
11 | import org.junit.jupiter.api.Test;
12 | import org.puremvc.java.multicore.patterns.observer.Notification;
13 |
14 | /**
15 | * Test the PureMVC SimpleCommand class.
16 | *
17 | * @see org.puremvc.java.multicore.patterns.command.SimpleCommandTestVO SimpleCommandTestVO
18 | * @see org.puremvc.java.multicore.patterns.command.SimpleCommandTestCommand SimpleCommandTestCommand
19 | */
20 | public class SimpleCommandTest {
21 |
22 | /**
23 | * Tests the execute
method of a SimpleCommand
.
24 | *
25 | * This test creates a new Notification
, adding a
26 | * SimpleCommandTestVO
as the body.
27 | * It then creates a SimpleCommandTestCommand
and invokes
28 | * its execute
method, passing in the note.
29 | *
30 | * Success is determined by evaluating a property on the
31 | * object that was passed on the Notification body, which will
32 | * be modified by the SimpleCommand
.
33 | *
34 | */
35 | @Test
36 | public void testSimpleCommandExecute() {
37 | // Create the VO
38 | SimpleCommandTestVO vo = new SimpleCommandTestVO(5);
39 |
40 | // Create the Notification (note)
41 | Notification note = new Notification("SimpleCommandTestNote", vo);
42 |
43 | // Create the SimpleCommand
44 | SimpleCommandTestCommand command = new SimpleCommandTestCommand();
45 |
46 | // Execute the SimpleCommand
47 | command.execute(note);
48 |
49 | // test assertions
50 | Assertions.assertTrue(vo.result == 10, "Expecting vo.result == 10");
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/patterns/command/SimpleCommandTestCommand.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.patterns.command;
9 |
10 | import org.puremvc.java.multicore.interfaces.INotification;
11 |
12 | /**
13 | * A SimpleCommand subclass used by SimpleCommandTest.
14 | *
15 | * @see SimpleCommandTest SimpleCommandTest
16 | * @see SimpleCommandTestVO SimpleCommandTestVO
17 | */
18 | public class SimpleCommandTestCommand extends SimpleCommand {
19 |
20 | /**
21 | * Fabricate a result by multiplying the input by 2
22 | *
23 | * @param notification the INotification
carrying the SimpleCommandTestVO
24 | */
25 | public void execute(INotification notification) {
26 | SimpleCommandTestVO vo = (SimpleCommandTestVO) notification.getBody();
27 |
28 | // Fabricate a result
29 | vo.result = 2 * vo.input;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/patterns/command/SimpleCommandTestVO.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.patterns.command;
9 |
10 | /**
11 | * A utility class used by SimpleCommandTest.
12 | *
13 | * @see SimpleCommandTest SimpleCommandTest
14 | * @see org.puremvc.java.multicore.patterns.command.SimpleCommandTestCommand SimpleCommandTestCommand
15 | */
16 | public class SimpleCommandTestVO {
17 |
18 | public int input;
19 | public int result;
20 |
21 | /**
22 | * Constructor.
23 | *
24 | * @param input the number to be fed to the SimpleCommandTestCommand
25 | */
26 | public SimpleCommandTestVO(int input) {
27 | this.input = input;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/patterns/facade/FacadeTestCommand.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.patterns.facade;
9 |
10 | import org.puremvc.java.multicore.interfaces.INotification;
11 | import org.puremvc.java.multicore.patterns.command.SimpleCommand;
12 |
13 | /**
14 | * A SimpleCommand subclass used by FacadeTest.
15 | *
16 | * @see FacadeTest FacadeTest
17 | * @see FacadeTestVO FacadeTestVO
18 | */
19 | public class FacadeTestCommand extends SimpleCommand {
20 |
21 | /**
22 | * Fabricate a result by multiplying the input by 2
23 | *
24 | * @param notification the Notification carrying the FacadeTestVO
25 | */
26 | public void execute(INotification notification) {
27 | FacadeTestVO vo = (FacadeTestVO)notification.getBody();
28 |
29 | // Fabricate a result
30 | vo.result = 2 * vo.input;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/patterns/facade/FacadeTestVO.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.patterns.facade;
9 |
10 | /**
11 | * A utility class used by FacadeTest.
12 | *
13 | * @see FacadeTest FacadeTest
14 | * @see org.puremvc.java.multicore.patterns.facade.FacadeTestCommand FacadeTestCommand
15 | */
16 | public class FacadeTestVO {
17 |
18 | public int input;
19 | public int result;
20 |
21 | /**
22 | * Constructor.
23 | *
24 | * @param input the number to be fed to the FacadeTestCommand
25 | */
26 | public FacadeTestVO(int input) {
27 | this.input = input;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/patterns/mediator/MediatorTest.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.patterns.mediator;
9 |
10 | import org.junit.jupiter.api.Assertions;
11 | import org.junit.jupiter.api.Test;
12 |
13 | /**
14 | * Test the PureMVC Mediator class.
15 | *
16 | * @see org.puremvc.java.multicore.interfaces.IMediator IMediator
17 | * @see Mediator Mediator
18 | */
19 | public class MediatorTest {
20 |
21 | /**
22 | * Tests getting the name using Mediator class accessor method.
23 | */
24 | @Test
25 | public void testNameAccessor() {
26 | // Create a new Mediator and use accessors to set the mediator name
27 | Mediator mediator = new Mediator();
28 |
29 | // test assertions
30 | Assertions.assertTrue(mediator.getMediatorName() == Mediator.NAME, "Expecting mediator.getMediatorName() == Mediator.NAME");
31 | }
32 |
33 | /**
34 | * Tests getting the name using Mediator class accessor method.
35 | */
36 | @Test
37 | public void testViewAccessor() {
38 | // Create a view object
39 | Object view = new Object();
40 |
41 | // Create a new Proxy and use accessors to set the proxy name
42 | Mediator mediator = new Mediator(Mediator.NAME, view);
43 |
44 | // test assertions
45 | Assertions.assertNotNull(mediator.getViewComponent(), "Expecting mediator.getViewComponent() not null");
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/patterns/observer/NotificationTest.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.patterns.observer;
9 |
10 | import org.junit.jupiter.api.Assertions;
11 | import org.junit.jupiter.api.Test;
12 | import org.puremvc.java.multicore.interfaces.INotification;
13 |
14 | /**
15 | * Test the PureMVC Notification class.
16 | *
17 | * @see Notification Notification
18 | */
19 | public class NotificationTest {
20 |
21 | /**
22 | * Tests setting and getting the name using Notification class accessor methods.
23 | */
24 | @Test
25 | public void testNameAccessors() {
26 | // Create a new Notification and use accessors to set the note name
27 | INotification note = new Notification("TestNote");
28 |
29 | // test assertions
30 | Assertions.assertTrue(note.getName() == "TestNote", "Expecting note.getName() == 'TestNote'");
31 | }
32 |
33 | /**
34 | * Tests setting and getting the body using Notification class accessor methods.
35 | */
36 | @Test
37 | public void testBodyAccessors() {
38 | // Create a new Notification and use accessors to set the body
39 | INotification note = new Notification(null);
40 | note.setBody(5);
41 |
42 | // test assertions
43 | Assertions.assertTrue((int)note.getBody() == 5, "Expecting note.getBody() == 5");
44 | }
45 |
46 | /**
47 | * Tests setting the name and body using the Notification class Constructor.
48 | */
49 | @Test
50 | public void testConstructor() {
51 | // Create a new Notification using the Constructor to set the note name and body
52 | INotification note = new Notification("TestNote", 5, "TestNoteType");
53 |
54 | // test assertions
55 | Assertions.assertTrue(note.getName() == "TestNote", "Expecting note.getName() == 'TestNote'");
56 | Assertions.assertTrue((int)note.getBody() == 5, "Expecting note.getBody() == 5");
57 | Assertions.assertTrue(note.getType() == "TestNoteType", "Expecting note.getType() == 'TestNoteType'");
58 | }
59 |
60 | /**
61 | * Tests the toString method of the notification
62 | */
63 | @Test
64 | public void testToString() {
65 | // Create a new Notification and use accessors to set the note name
66 | INotification note = new Notification("TestNote", "1,3,5", "TestType");
67 | String ts = "Notification Name: TestNote\nBody:1,3,5\nType:TestType";
68 |
69 | // test assertions
70 | Assertions.assertTrue(note.toString().equals(ts), "Expecting note.toString() == '" + ts + "'");
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/patterns/observer/ObserverTest.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.patterns.observer;
9 |
10 | import org.junit.jupiter.api.Assertions;
11 | import org.junit.jupiter.api.Test;
12 | import org.puremvc.java.multicore.interfaces.INotification;
13 |
14 | /**
15 | * Tests PureMVC Observer class.
16 | *
17 | * Since the Observer encapsulates the interested object's
18 | * callback information, there are no getters, only setters.
19 | * It is, in effect write-only memory.
20 | *
21 | * Therefore, the only way to test it is to set the
22 | * notification method and context and call the notifyObserver
23 | * method.
24 | *
25 | */
26 | public class ObserverTest {
27 |
28 | /**
29 | * A test variable that proves the notify method was
30 | * executed with 'this' as its exectution context
31 | */
32 | private int observerTestVar;
33 |
34 | /**
35 | * Tests observer class when initialized by accessor methods.
36 | */
37 | @Test
38 | public void testObserverAccessors() {
39 | // Create observer with null args, then
40 | // use accessors to set notification method and context
41 | Observer observer = new Observer(null, null);
42 | observer.setNotifyContext(this);
43 | observer.setNotifyMethod(this::handleNotification);
44 |
45 | // create a test event, setting a payload value and notify
46 | // the observer with it. since the observer is this class
47 | // and the notification method is observerTestMethod,
48 | // successful notification will result in our local
49 | // observerTestVar being set to the value we pass in
50 | // on the note body.
51 | Notification note = new Notification("ObserverTestNote", 10);
52 | observer.notifyObserver(note);
53 |
54 | // test assertions
55 | Assertions.assertTrue(observerTestVar == 10, "Expecting observerTestVar = 10");
56 | }
57 |
58 | /**
59 | * Tests observer class when initialized by constructor.
60 | */
61 | @Test
62 | public void testObserverConstructor() {
63 | // Create observer passing in notification method and context
64 | Observer observer = new Observer(this::handleNotification, this);
65 |
66 | // create a test note, setting a body value and notify
67 | // the observer with it. since the observer is this class
68 | // and the notification method is observerTestMethod,
69 | // successful notification will result in our local
70 | // observerTestVar being set to the value we pass in
71 | // on the note body.
72 | Notification note = new Notification("ObserverTestNote", 5);
73 | observer.notifyObserver(note);
74 |
75 | // test assertions
76 | Assertions.assertTrue(observerTestVar == 5, "Expecting observerTestVar = 5");
77 | }
78 |
79 | /**
80 | * Tests the compareNotifyContext method of the Observer class
81 | */
82 | @Test
83 | public void testCompareNotifyContext() {
84 | // Create observer passing in notification method and context
85 | Observer observer = new Observer(this::handleNotification, this);
86 |
87 | Object negTestObj = new Object();
88 |
89 | // test assertions
90 | Assertions.assertFalse(observer.compareNotifyContext(negTestObj), "Expecting observer.compareNotifyContext(negTestObj) == false");
91 | Assertions.assertTrue(observer.compareNotifyContext(this), "Expecting observer.compareNotifyContext(this) == true");
92 | }
93 |
94 | /**
95 | * A function that is used as the observer notification
96 | * method. It multiplies the input number by the
97 | * observerTestVar value
98 | */
99 | public void handleNotification(INotification notification) {
100 | observerTestVar = (int)notification.getBody();
101 | }
102 |
103 | }
104 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/multicore/patterns/proxy/ProxyTest.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Multicore
3 | //
4 | // Copyright(c) 2019 Saad Shams
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.multicore.patterns.proxy;
9 |
10 | import org.junit.jupiter.api.Assertions;
11 | import org.junit.jupiter.api.Test;
12 |
13 | /**
14 | * Test the PureMVC Proxy class.
15 | *
16 | * @see org.puremvc.java.multicore.interfaces.IProxy IProxy
17 | * @see Proxy Proxy
18 | */
19 | public class ProxyTest {
20 |
21 | /**
22 | * Tests getting the name using Proxy class accessor method. Setting can only be done in constructor.
23 | */
24 | @Test
25 | public void testNameAccessory() {
26 | Proxy proxy = new Proxy("TestProxy");
27 |
28 | // test assertions
29 | Assertions.assertTrue(proxy.getProxyName() == "TestProxy", "Expecting proxy.getProxyName == 'TestProxy'");
30 | }
31 |
32 | /**
33 | * Tests setting and getting the data using Proxy class accessor methods.
34 | */
35 | @Test
36 | public void testDataAccessors() {
37 | // Create a new Proxy and use accessors to set the data
38 | Proxy proxy = new Proxy("colors");
39 | proxy.setData(new String[]{"red", "green", "blue"});
40 |
41 | String[] data = (String[]) proxy.getData();
42 |
43 | // test assertions
44 | Assertions.assertTrue(data.length == 3, "Expecting data.length == 3");
45 | Assertions.assertTrue(data[0] == "red", "Expecting data[0] == 'red'");
46 | Assertions.assertTrue(data[1] == "green", "Expecting data[1] == 'green'");
47 | Assertions.assertTrue(data[2] == "blue", "Expecting data[2] == 'blue'");
48 | }
49 |
50 | /**
51 | * Tests setting the name and body using the Notification class Constructor.
52 | */
53 | @Test
54 | public void testConstructor() {
55 | // Create a new Proxy using the Constructor to set the name and data
56 | Proxy proxy = new Proxy("colors", new String[] {"red", "green", "blue"});
57 | String[] data = (String[]) proxy.getData();
58 |
59 | // test assertions
60 | Assertions.assertNotNull(proxy, "Expecting proxy not null");
61 | Assertions.assertTrue(proxy.getProxyName() == "colors", "Expecting proxy.getProxyName() == 'color'");
62 | Assertions.assertTrue(data.length == 3, "Expecting data.length == 3");
63 | Assertions.assertTrue(data[0] == "red", "Expecting data[0] == 'red'");
64 | Assertions.assertTrue(data[1] == "green", "Expecting data[1] == 'green'");
65 | Assertions.assertTrue(data[2] == "blue", "Expecting data[2] == 'blue'");
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------