The interface definition for a PureMVC Model.
12 | *
13 | *
5 | // Your reuse is governed by the Creative Commons Attribution 3.0 License
6 | //
7 |
8 | package org.puremvc.java.interfaces;
9 |
10 | /**
11 | * The interface definition for a PureMVC Notification.
12 | *
13 | * PureMVC does not rely upon underlying event models such
14 | * as the one provided with Flash, and ActionScript 3 does
15 | * not have an inherent event model.
16 | *
17 | * The Observer Pattern as implemented within PureMVC exists
18 | * to support event-driven communication between the
19 | * application and the actors of the MVC triad.
20 | *
21 | * Notifications are not meant to be a replacement for Events
22 | * in Flex/Flash/AIR. Generally, IMediator
implementors
23 | * place event listeners on their view components, which they
24 | * then handle in the usual way. This may lead to the broadcast of Notification
s to
25 | * trigger ICommand
s or to communicate with other IMediators
. IProxy
and ICommand
26 | * instances communicate with each other and IMediator
s
27 | * by broadcasting INotification
s.
28 | *
29 | * A key difference between Flash Event
s and PureMVC
30 | * Notification
s is that Event
s follow the
31 | * 'Chain of Responsibility' pattern, 'bubbling' up the display hierarchy
32 | * until some parent component handles the Event
, while
33 | * PureMVC Notification
s follow a 'Publish/Subscribe'
34 | * pattern. PureMVC classes need not be related to each other in a
35 | * parent/child relationship in order to communicate with one another
36 | * using Notification
s.
37 | *
38 | * @see IView IView
39 | * @see IObserver IObserver
40 | */
41 | public interface INotification {
42 |
43 | /**
44 | * Get the name of the INotification
instance.
45 | * No setter, should be set by constructor only
46 | *
47 | * @return notification name
48 | */
49 | String getName();
50 |
51 | /**
52 | * Set the body of the INotification
instance
53 | *
54 | * @param body notification body
55 | */
56 | void setBody(Object body);
57 |
58 | /**
59 | * Get the body of the INotification
instance
60 | *
61 | * @return notification body
62 | */
63 | Object getBody();
64 |
65 | /**
66 | * Set the type of the INotification
instance
67 | *
68 | * @param type notification type
69 | */
70 | void setType(String type);
71 |
72 | /**
73 | * Get the type of the INotification
instance
74 | *
75 | * @return notification type
76 | */
77 | String getType();
78 |
79 | /**
80 | * Get the string representation of the INotification
instance
81 | *
82 | * @return string representation of INotification
83 | */
84 | String toString();
85 |
86 | }
87 |
--------------------------------------------------------------------------------
/src/main/java/org/puremvc/java/interfaces/INotifier.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.interfaces;
9 |
10 | /**
11 | * The interface definition for a PureMVC Notifier.
12 | *
13 | * MacroCommand, Command, Mediator
and Proxy
14 | * all have a need to send Notifications
.
15 | *
16 | * The INotifier
interface provides a common method called
17 | * sendNotification
that relieves implementation code of
18 | * the necessity to actually construct Notifications
.
19 | *
20 | * The Notifier
class, which all of the above mentioned classes
21 | * extend, also provides an initialized reference to the Facade
22 | * Singleton, which is required for the convienience method
23 | * for sending Notifications
, but also eases implementation as these
24 | * classes have frequent Facade
interactions and usually require
25 | * access to the facade anyway.
26 | *
27 | * @see IFacade IFacade
28 | * @see org.puremvc.java.interfaces.INotification INotification
29 | */
30 | public interface INotifier {
31 |
32 | /**
33 | * Send a INotification
.
34 | *
35 | * Convenience method to prevent having to construct new
36 | * notification instances in our implementation code.
37 | *
38 | * @param notificationName the name of the notification to send
39 | * @param body the body of the notification
40 | * @param type the type of the notification
41 | */
42 | void sendNotification(String notificationName, Object body, String type);
43 |
44 | /**
45 | * Send a INotification
.
46 | *
47 | * Convenience method to prevent having to construct new
48 | * notification instances in our implementation code.
49 | *
50 | * @param notificationName the name of the notification to send
51 | * @param body the body of the notification
52 | */
53 | void sendNotification(String notificationName, Object body);
54 |
55 | /**
56 | * Send a INotification
.
57 | *
58 | * Convenience method to prevent having to construct new
59 | * notification instances in our implementation code.
60 | *
61 | * @param notificationName the name of the notification to send
62 | */
63 | void sendNotification(String notificationName);
64 | }
65 |
--------------------------------------------------------------------------------
/src/main/java/org/puremvc/java/interfaces/IObserver.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.interfaces;
9 |
10 | import java.util.function.Consumer;
11 |
12 | /**
13 | * The interface definition for a PureMVC Observer.
14 | *
15 | * In PureMVC, IObserver
implementors assume these responsibilities:
16 | *
17 | *
18 | * - Encapsulate the notification (callback) method of the interested object.
19 | * - Encapsulate the notification context (this) of the interested object.
20 | * - Provide methods for setting the interested object' notification method and context.
21 | * - Provide a method for notifying the interested object.
22 | *
23 | *
24 | * PureMVC does not rely upon underlying event
25 | * models such as the one provided with Flash,
26 | * and ActionScript 3 does not have an inherent
27 | * event model.
28 | *
29 | * The Observer Pattern as implemented within
30 | * PureMVC exists to support event driven communication
31 | * between the application and the actors of the
32 | * MVC triad.
33 | *
34 | * An Observer is an object that encapsulates information
35 | * about an interested object with a notification method that
36 | * should be called when an INotification
is broadcast. The Observer then
37 | * acts as a proxy for notifying the interested object.
38 | *
39 | * Observers can receive Notification
s by having their
40 | * notifyObserver
method invoked, passing
41 | * in an object implementing the INotification
interface, such
42 | * as a subclass of Notification
.
43 | *
44 | * @see IView IView
45 | * @see org.puremvc.java.interfaces.INotification INotification
46 | */
47 | public interface IObserver {
48 |
49 | /**
50 | * Set the notification method.
51 | *
52 | * The notification method should take one parameter of type INotification
53 | *
54 | * @param notifyMethod the notification (callback) method of the interested object
55 | */
56 | void setNotifyMethod(Consumer notifyMethod);
57 |
58 | /**
59 | * Set the notification context.
60 | *
61 | * @param notifyContext the notification context (this) of the interested object
62 | */
63 | void setNotifyContext(Object notifyContext);
64 |
65 | /**
66 | * Notify the interested object.
67 | *
68 | * @param notification the INotification
to pass to the interested object's notification method
69 | */
70 | void notifyObserver(INotification notification);
71 |
72 | /**
73 | * Compare the given object to the notification context object.
74 | *
75 | * @param object the object to compare.
76 | * @return boolean indicating if the notification context and the object are the same.
77 | */
78 | boolean compareNotifyContext(Object object);
79 |
80 | }
81 |
82 |
--------------------------------------------------------------------------------
/src/main/java/org/puremvc/java/interfaces/IProxy.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.interfaces;
9 |
10 | /**
11 | * The interface definition for a PureMVC Proxy.
12 | *
13 | * In PureMVC, IProxy
implementors assume these responsibilities:
14 | *
15 | *
16 | * - Implement a common method which returns the name of the Proxy.
17 | * - Provide methods for setting and getting the data object.
18 | *
19 | *
20 | * Additionally, IProxy
s typically:
21 | *
22 | *
23 | * - Maintain references to one or more pieces of model data.
24 | * - Provide methods for manipulating that data.
25 | * - Generate
INotifications
when their model data changes.
26 | * - Expose their name as a
public static const
called NAME
, if they are not instantiated multiple times.
27 | * - Encapsulate interaction with local or remote services used to fetch and persist model data.
28 | *
29 | */
30 | public interface IProxy extends INotifier {
31 |
32 | /**
33 | * Get the Proxy name
34 | *
35 | * @return the Proxy instance name
36 | */
37 | String getProxyName();
38 |
39 | /**
40 | * Set the data object
41 | *
42 | * @param data the data object
43 | */
44 | void setData(Object data);
45 |
46 | /**
47 | * Get the data object
48 | *
49 | * @return the data as type Object
50 | */
51 | Object getData();
52 |
53 | /**
54 | * Called by the Model when the Proxy is registered
55 | */
56 | void onRegister();
57 |
58 | /**
59 | * Called by the Model when the Proxy is removed
60 | */
61 | void onRemove();
62 | }
63 |
--------------------------------------------------------------------------------
/src/main/java/org/puremvc/java/interfaces/IView.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.interfaces;
9 |
10 | /**
11 | * The interface definition for a PureMVC View.
12 | *
13 | * In PureMVC, IView
implementors assume these responsibilities:
14 | *
15 | * In PureMVC, the View
class assumes these responsibilities:
16 | *
17 | *
18 | * - Maintain a cache of
IMediator
instances.
19 | * - Provide methods for registering, retrieving, and removing
IMediators
.
20 | * - Managing the observer lists for each
INotification
in the application.
21 | * - Providing a method for attaching
IObservers
to an INotification
's observer list.
22 | * - Providing a method for broadcasting an
INotification
.
23 | * - Notifying the
IObservers
of a given INotification
when it broadcast.
24 | *
25 | *
26 | * @see org.puremvc.java.interfaces.IMediator IMediator
27 | * @see org.puremvc.java.interfaces.IObserver IObserver
28 | * @see org.puremvc.java.interfaces.INotification INotification
29 | */
30 | public interface IView {
31 |
32 | /**
33 | * Register an IObserver
to be notified
34 | * of INotifications
with a given name.
35 | *
36 | * @param notificationName the name of the INotifications
to notify this IObserver
of
37 | * @param observer the IObserver
to register
38 | */
39 | void registerObserver(String notificationName, IObserver observer);
40 |
41 | /**
42 | * Remove a group of observers from the observer list for a given Notification name.
43 | *
44 | * @param notificationName which observer list to remove from
45 | * @param notifyContext removed the observers with this object as their notifyContext
46 | */
47 | void removeObserver(String notificationName, Object notifyContext);
48 |
49 | /**
50 | * Notify the IObservers
for a particular INotification
.
51 | *
52 | * All previously attached IObservers
for this INotification
's
53 | * list are notified and are passed a reference to the INotification
in
54 | * the order in which they were registered.
55 | *
56 | * @param notification the INotification
to notify IObservers
of.
57 | */
58 | void notifyObservers(INotification notification);
59 |
60 | /**
61 | * Register an IMediator
instance with the View
.
62 | *
63 | * Registers the IMediator
so that it can be retrieved by name,
64 | * and further interrogates the IMediator
for its
65 | * INotification
interests.
66 | *
67 | * If the IMediator
returns any INotification
68 | * names to be notified about, an Observer
is created encapsulating
69 | * the IMediator
instance's handleNotification
method
70 | * and registering it as an Observer
for all INotifications
the
71 | * IMediator
is interested in.
72 | *
73 | * @param mediator a reference to the IMediator
instance
74 | */
75 | void registerMediator(IMediator mediator);
76 |
77 | /**
78 | * Retrieve an IMediator
from the View
.
79 | *
80 | * @param mediatorName the name of the IMediator
instance to retrieve.
81 | * @return the IMediator
instance previously registered with the given mediatorName
.
82 | */
83 | IMediator retrieveMediator(String mediatorName);
84 |
85 | /**
86 | * Remove an IMediator
from the View
.
87 | *
88 | * @param mediatorName name of the IMediator
instance to be removed.
89 | * @return the IMediator
that was removed from the View
90 | */
91 | IMediator removeMediator(String mediatorName);
92 |
93 | /**
94 | * Check if a Mediator is registered or not
95 | *
96 | * @param mediatorName mediator name
97 | * @return whether a Mediator is registered with the given mediatorName
.
98 | */
99 | boolean hasMediator(String mediatorName);
100 | }
101 |
--------------------------------------------------------------------------------
/src/main/java/org/puremvc/java/patterns/command/MacroCommand.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.patterns.command;
9 |
10 | import org.puremvc.java.interfaces.ICommand;
11 | import org.puremvc.java.interfaces.INotification;
12 | import org.puremvc.java.patterns.observer.Notifier;
13 |
14 | import java.util.Vector;
15 | import java.util.function.Supplier;
16 |
17 | /**
18 | * A base ICommand
implementation that executes other ICommand
s.
19 | *
20 | * A MacroCommand
maintains an list of
21 | * ICommand
Class references called SubCommands.
22 | *
23 | * When execute
is called, the MacroCommand
24 | * instantiates and calls execute
on each of its SubCommands turn.
25 | * Each SubCommand will be passed a reference to the original
26 | * INotification
that was passed to the MacroCommand
's
27 | * execute
method.
28 | *
29 | * Unlike SimpleCommand
, your subclass
30 | * should not override execute
, but instead, should
31 | * override the initializeMacroCommand
method,
32 | * calling addSubCommand
once for each SubCommand
33 | * to be executed.
34 | *
35 | * @see org.puremvc.java.core.Controller Controller
36 | * @see org.puremvc.java.patterns.observer.Notification Notification
37 | * @see org.puremvc.java.patterns.command.SimpleCommand SimpleCommand
38 | */
39 | public class MacroCommand extends Notifier implements ICommand {
40 |
41 | private Vector> subCommands;
42 |
43 | /**
44 | * Constructor.
45 | *
46 | * You should not need to define a constructor,
47 | * instead, override the initializeMacroCommand
48 | * method.
49 | *
50 | * If your subclass does define a constructor, be
51 | * sure to call super()
.
52 | */
53 | public MacroCommand() {
54 | subCommands = new Vector>();
55 | initializeMacroCommand();
56 | }
57 |
58 | /**
59 | * Initialize the MacroCommand
.
60 | *
61 | * In your subclass, override this method to
62 | * initialize the MacroCommand
's SubCommand
63 | * list with ICommand
class references like
64 | * this:
65 | *
66 | *
67 | * {@code
68 | * // Initialize MyMacroCommand
69 | * protected void initializeMacroCommand( )
70 | * {
71 | * addSubCommand( () -> new com.me.myapp.controller.FirstCommand() );
72 | * addSubCommand( () -> new com.me.myapp.controller.SecondCommand() );
73 | * addSubCommand( () -> new com.me.myapp.controller.ThirdCommand() );
74 | * }
75 | * }
76 | *
77 | *
78 | * Note that SubCommands may be any ICommand
implementor,
79 | * MacroCommand
s or SimpleCommands
are both acceptable.
80 | */
81 | protected void initializeMacroCommand() {
82 | }
83 |
84 | /**
85 | * Add a SubCommand.
86 | *
87 | * The SubCommands will be called in First In/First Out (FIFO)
88 | * order.
89 | *
90 | * @param factory a reference to the factory of the ICommand
.
91 | */
92 | protected void addSubCommand(Supplier factory) {
93 | subCommands.add(factory);
94 | }
95 |
96 | /**
97 | * Execute this MacroCommand
's SubCommands.
98 | *
99 | * The SubCommands will be called in First In/First Out (FIFO)
100 | * order.
101 | *
102 | * @param notification the INotification
object to be passsed to each SubCommand.
103 | */
104 | public void execute(INotification notification) {
105 | while(!subCommands.isEmpty()) {
106 | Supplier commandSupplier = subCommands.remove(0);
107 | ICommand command = commandSupplier.get();
108 | command.execute(notification);
109 | }
110 | }
111 | }
112 |
--------------------------------------------------------------------------------
/src/main/java/org/puremvc/java/patterns/command/SimpleCommand.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.patterns.command;
9 |
10 | import org.puremvc.java.interfaces.ICommand;
11 | import org.puremvc.java.interfaces.INotification;
12 | import org.puremvc.java.patterns.observer.Notifier;
13 |
14 | /**
15 | * A base ICommand
implementation.
16 | *
17 | * Your subclass should override the execute
18 | * method where your business logic will handle the INotification
.
19 | *
20 | * @see org.puremvc.java.core.Controller Controller
21 | * @see org.puremvc.java.patterns.observer.Notification Notification
22 | * @see MacroCommand MacroCommand
23 | */
24 | public class SimpleCommand extends Notifier implements ICommand {
25 |
26 | /**
27 | * Fulfill the use-case initiated by the given INotification
.
28 | *
29 | * In the Command Pattern, an application use-case typically
30 | * begins with some user action, which results in an INotification
being broadcast, which
31 | * is handled by business logic in the execute
method of an
32 | * ICommand
.
33 | *
34 | * @param notification the INotification
to handle.
35 | */
36 | public void execute(INotification notification) {
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/main/java/org/puremvc/java/patterns/mediator/Mediator.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.patterns.mediator;
9 |
10 | import org.puremvc.java.interfaces.IMediator;
11 | import org.puremvc.java.interfaces.INotification;
12 | import org.puremvc.java.patterns.observer.Notifier;
13 |
14 | /**
15 | * A base IMediator
implementation.
16 | *
17 | * @see org.puremvc.java.core.View View
18 | */
19 | public class Mediator extends Notifier implements IMediator {
20 |
21 | /**
22 | * The name of the Mediator
.
23 | *
24 | * Typically, a Mediator
will be written to serve
25 | * one specific control or group controls and so,
26 | * will not have a need to be dynamically named.
27 | */
28 | public static final String NAME = "Mediator";
29 |
30 | // the mediator name
31 | protected String mediatorName;
32 |
33 | // The view component
34 | protected Object viewComponent;
35 |
36 | /**
37 | * 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 | * List the INotification
names this
65 | * Mediator
is interested in being notified of.
66 | *
67 | * @return Array the list of INotification
names
68 | */
69 | public String[] listNotificationInterests() {
70 | return new String[0];
71 | }
72 |
73 | /**
74 | * Handle INotification
s.
75 | *
76 | * 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 | * Called by the View when the Mediator is registered
86 | */
87 | public void onRegister() {
88 |
89 | }
90 |
91 | /**
92 | * Called by the View when the Mediator is removed
93 | */
94 | public void onRemove() {
95 |
96 | }
97 |
98 | /**
99 | * Get the name of the Mediator
.
100 | *
101 | * @return the Mediator name
102 | */
103 | public String getMediatorName() {
104 | return mediatorName;
105 | }
106 |
107 | /**
108 | * Get the Mediator
's view component.
109 | *
110 | * 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 | * Set the IMediator
's view component.
129 | *
130 | * @param viewComponent the view component
131 | */
132 | public void setViewComponent(Object viewComponent) {
133 | this.viewComponent = viewComponent;
134 | }
135 |
136 | }
137 |
--------------------------------------------------------------------------------
/src/main/java/org/puremvc/java/patterns/observer/Notification.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.patterns.observer;
9 |
10 | import org.puremvc.java.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.
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.
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 |
--------------------------------------------------------------------------------
/src/main/java/org/puremvc/java/patterns/observer/Notifier.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.patterns.observer;
9 |
10 | import org.puremvc.java.interfaces.IFacade;
11 | import org.puremvc.java.interfaces.INotifier;
12 | import org.puremvc.java.patterns.facade.Facade;
13 |
14 | /**
15 | * A Base INotifier
implementation.
16 | *
17 | * MacroCommand, Command, Mediator
and Proxy
all
18 | * have a need to send Notifications
.
19 | *
20 | * The INotifier
interface provides a common method called
21 | * sendNotification
that relieves implementation code of the
22 | * 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 | * Singleton, which is required for the convienience method for sending
27 | * Notifications
, but also eases implementation as these classes
28 | * have frequent Facade
interactions and usually require access
29 | * to the facade anyway.
30 | *
31 | * @see Facade Facade
32 | * @see org.puremvc.java.patterns.mediator.Mediator Mediator
33 | * @see org.puremvc.java.patterns.proxy.Proxy Proxy
34 | * @see org.puremvc.java.patterns.command.SimpleCommand SimpleCommand
35 | * @see org.puremvc.java.patterns.command.MacroCommand MacroCommand
36 | */
37 | public class Notifier implements INotifier {
38 |
39 | /**
40 | * Local reference to the Facade Singleton
41 | */
42 | protected IFacade facade = Facade.getInstance(()-> new Facade());
43 |
44 | /**
45 | * Send an INotification
s.
46 | *
47 | * Keeps us from having to construct new notification instances in our
48 | * implementation code.
49 | *
50 | * @param notificationName the name of the notiification to send
51 | * @param body the body of the notification
52 | * @param type the type of the notification
53 | */
54 | public void sendNotification(String notificationName, Object body, String type) {
55 | facade.sendNotification(notificationName, body, type);
56 | }
57 |
58 | /**
59 | * Send an INotification
s.
60 | *
61 | * Keeps us from having to construct new notification instances in our
62 | * implementation code.
63 | *
64 | * @param notificationName the name of the notiification to send
65 | * @param body the body of the notification
66 | */
67 | public void sendNotification(String notificationName, Object body) {
68 | facade.sendNotification(notificationName, body);
69 | }
70 |
71 | /**
72 | * Send an INotification
s.
73 | *
74 | * Keeps us from having to construct new notification instances in our
75 | * implementation code.
76 | *
77 | * @param notificationName the name of the notiification to send
78 | */
79 | public void sendNotification(String notificationName) {
80 | facade.sendNotification(notificationName);
81 | }
82 |
83 | }
84 |
--------------------------------------------------------------------------------
/src/main/java/org/puremvc/java/patterns/observer/Observer.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.patterns.observer;
9 |
10 | import org.puremvc.java.interfaces.INotification;
11 | import org.puremvc.java.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.core.View View
32 | * @see org.puremvc.java.patterns.observer.Notification Notification
33 | */
34 | public class Observer implements IObserver {
35 |
36 | private Object notifyContext;
37 |
38 | private Consumer notifyMethod;
39 |
40 | /**
41 | * Constructor.
42 | *
43 | * The notification method on the interested object should take one
44 | * parameter of type INotification
45 | *
46 | * @param notifyMethod the notification method of the interested object
47 | * @param notifyContext the notification context of the interested object
48 | */
49 | public Observer(Consumer notifyMethod, Object notifyContext) {
50 | this.notifyMethod = notifyMethod;
51 | this.notifyContext = notifyContext;
52 | }
53 |
54 | /**
55 | * Compare an object to the notification context.
56 | *
57 | * @param object the object to compare
58 | * @return boolean indicating if the object and the notification context are
59 | * the same
60 | */
61 | public boolean compareNotifyContext(Object object) {
62 | return object == this.notifyContext;
63 | }
64 |
65 | /**
66 | * Notify the interested object.
67 | *
68 | * @param notification the INotification
to pass to the interested
69 | * object's notification method.
70 | */
71 | public void notifyObserver(INotification notification) {
72 | notifyMethod.accept(notification);
73 | }
74 |
75 | /**
76 | * Get the notification context.
77 | *
78 | * @return the notification context (this
) of the
79 | * interested object.
80 | */
81 | protected Object getNotifyContext() {
82 | return notifyContext;
83 | }
84 |
85 | /**
86 | * Set the notification context.
87 | *
88 | * @param notifyContext the notification context (this) of the interested object.
89 | */
90 | public void setNotifyContext(Object notifyContext) {
91 | this.notifyContext = notifyContext;
92 | }
93 |
94 | /**
95 | * Get the notification method.
96 | *
97 | * @return the notification (callback) consumer function of the interested object.
98 | */
99 | protected Consumer getNotifyMethod() {
100 | return notifyMethod;
101 | }
102 |
103 | /**
104 | * Set the notification method.
105 | *
106 | * The notification method should take one parameter of type
107 | * INotification
.
108 | *
109 | * @param notifyMethod the notification (callback) consumer function of the interested object.
110 | */
111 | public void setNotifyMethod(Consumer notifyMethod) {
112 | this.notifyMethod = notifyMethod;
113 | }
114 |
115 | }
116 |
--------------------------------------------------------------------------------
/src/main/java/org/puremvc/java/patterns/proxy/Proxy.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.patterns.proxy;
9 |
10 | import org.puremvc.java.interfaces.IProxy;
11 | import org.puremvc.java.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
20 | * object, in which case interacting with it might involve setting and getting
21 | * 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, we
25 | * adopt an asyncronous idiom; setting data (or calling a method) on the
26 | * Proxy
and listening for a Notification
to be
27 | * sent when the Proxy
has retrieved the data from the service.
28 | *
29 | * @see org.puremvc.java.core.Model Model
30 | */
31 | public class Proxy extends Notifier implements IProxy {
32 |
33 | // the proxy name
34 | public static final String NAME = "Proxy";
35 |
36 | // the data object
37 | protected String proxyName;
38 |
39 | // the data object
40 | protected Object data;
41 |
42 | /**
43 | * Constructor
44 | *
45 | * @param proxyName proxy name
46 | * @param data data object
47 | */
48 | public Proxy(String proxyName, Object data) {
49 | this.proxyName = (proxyName != null) ? proxyName : NAME;
50 | if(data != null) setData(data);
51 | }
52 |
53 | /**
54 | * Constructor
55 | *
56 | * @param proxyName Name of the Proxy
57 | */
58 | public Proxy(String proxyName) {
59 | this(proxyName, null);
60 | }
61 |
62 | /**
63 | * Constructor
64 | */
65 | public Proxy(){
66 | this(null, null);
67 | }
68 |
69 | /**
70 | * Called by the Model when the Proxy is registered
71 | */
72 | public void onRegister() {
73 |
74 | }
75 |
76 | /**
77 | * Called by the Model when the Proxy is removed
78 | */
79 | public void onRemove() {
80 |
81 | }
82 |
83 | /**
84 | * Get the proxy name
85 | *
86 | * @return the proxy name
87 | */
88 | public String getProxyName() {
89 | return proxyName;
90 | }
91 |
92 | /**
93 | * Get the data object
94 | *
95 | * @return the data object
96 | */
97 | public Object getData() {
98 | return data;
99 | }
100 |
101 | /**
102 | * Set the data object
103 | *
104 | * @param data data object
105 | */
106 | public void setData(Object data) {
107 | this.data = data;
108 | }
109 |
110 | }
111 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/core/ControllerTestCommand.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.core;
9 |
10 | import org.puremvc.java.interfaces.INotification;
11 | import org.puremvc.java.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 | vo.result = 2 * vo.input;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/core/ControllerTestCommand2.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.core;
9 |
10 | import org.puremvc.java.interfaces.INotification;
11 | import org.puremvc.java.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 ControllerTestCommand2 extends SimpleCommand {
20 |
21 | /**
22 | * Fabricate a result by multiplying the input by 2 and adding to the existing result
23 | *
24 | * This tests accumulation effect that would show if the command were executed more than once.
25 | * @param notification the note carrying the ControllerTestVO
26 | */
27 | public void execute(INotification notification) {
28 | ControllerTestVO vo = (ControllerTestVO)notification.getBody();
29 |
30 | // Fabricate a result
31 | vo.result = vo.result + (2 * vo.input);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/core/ControllerTestVO.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.core;
9 |
10 | /**
11 | * A utility class used by ControllerTest.
12 | *
13 | * @see ControllerTest ControllerTest
14 | * @see org.puremvc.java.core.ControllerTestCommand ControllerTestCommand
15 | */
16 | public class ControllerTestVO {
17 |
18 | int input;
19 | int result;
20 |
21 | /**
22 | * Constructor.
23 | *
24 | * @param input the number to be fed to the ControllerTestCommand
25 | */
26 | ControllerTestVO(int input) {
27 | this.input = input;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/core/ModelTest.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.core;
9 |
10 | import org.junit.jupiter.api.Assertions;
11 | import org.junit.jupiter.api.Test;
12 | import org.puremvc.java.interfaces.IModel;
13 | import org.puremvc.java.interfaces.IProxy;
14 | import org.puremvc.java.patterns.proxy.Proxy;
15 |
16 | public class ModelTest {
17 |
18 | /**
19 | * Tests the Model Singleton Factory Method
20 | */
21 | @Test
22 | public void testGetInstance() {
23 | // Test Factory Method
24 | IModel model = Model.getInstance(() -> new Model());
25 |
26 | // test assertions
27 | Assertions.assertNotNull(model, "Expecting instance not null");
28 | Assertions.assertNotNull((IModel) model, "Expecting instance implements IModel");
29 | }
30 |
31 | /**
32 | * Tests the proxy registration and retrieval methods.
33 | *
34 | * Tests registerProxy
and retrieveProxy
in the same test.
35 | * These methods cannot currently be tested separately
36 | * in any meaningful way other than to show that the
37 | * methods do not throw exception when called.
38 | */
39 | @Test
40 | public void testRegisterAndRetrieveProxy() {
41 | // register a proxy and retrieve it.
42 | IModel model = Model.getInstance(() -> new Model());
43 | model.registerProxy(new Proxy("colors", new String[]{"red", "green", "blue"}));
44 | IProxy proxy = model.retrieveProxy("colors");
45 | String[] data = (String[]) proxy.getData();
46 |
47 | // test assertions
48 | Assertions.assertNotNull(data, "Expecting data not null");
49 | Assertions.assertNotNull((String[])data, "Expecting data type is Array");
50 | Assertions.assertTrue(data.length == 3, "Expecting data.length == 3");
51 | Assertions.assertTrue(data[0] == "red", "Expecting data[0] == 'red'");
52 | Assertions.assertTrue(data[1] == "green", "Expecting data[1] == 'green'");
53 | Assertions.assertTrue(data[2] == "blue", "Expecting data[2] == 'blue'");
54 | }
55 |
56 | /**
57 | * Tests the proxy removal method.
58 | */
59 | @Test
60 | public void testRegisterAndRemoveProxy() {
61 | // register a proxy, remove it, then try to retrieve it
62 | IModel model = Model.getInstance(() -> new Model());
63 | IProxy proxy = new Proxy("sizes", new String[]{"7", "13", "21"});
64 | model.registerProxy(proxy);
65 |
66 | // remove the proxy
67 | IProxy removedProxy = model.removeProxy("sizes");
68 |
69 | // assert that we removed the appropriate proxy
70 | Assertions.assertTrue(removedProxy.getProxyName() == "sizes", "Expecting removedProxy.getProxyName() == 'sizes'");
71 |
72 | // ensure that the proxy is no longer retrievable from the model
73 | proxy = model.retrieveProxy("sizes");
74 |
75 | // test assertions
76 | Assertions.assertNull(proxy, "Expecting proxy is null");
77 | }
78 |
79 | /**
80 | * Tests the hasProxy Method
81 | */
82 | @Test
83 | public void testHasProxy() {
84 | // register a proxy
85 | IModel model = Model.getInstance(() -> new Model());
86 | IProxy proxy = new Proxy("aces", new String[]{"clubs", "spades", "hearts", "diamonds"});
87 | model.registerProxy(proxy);
88 |
89 | // assert that the model.hasProxy method returns true
90 | // for that proxy name
91 | Assertions.assertTrue(model.hasProxy("aces") == true, "Expecting model.hasProxy('aces') == true");
92 |
93 | // remove the proxy
94 | model.removeProxy("aces");
95 |
96 | // assert that the model.hasProxy method returns false
97 | // for that proxy name
98 | Assertions.assertTrue(model.hasProxy("aces") == false, "Expecting model.hasProxy('aces') == false");
99 | }
100 |
101 | /**
102 | * Tests that the Model calls the onRegister and onRemove methods
103 | */
104 | @Test
105 | public void testOnRegisterAndOnRemove() {
106 | // Get the Singleton View instance
107 | IModel model = Model.getInstance(() -> new Model());
108 |
109 | // Create and register the test mediator
110 | IProxy proxy = new ModelTestProxy();
111 | model.registerProxy(proxy);
112 |
113 | // assert that onRegsiter was called, and the proxy responded by setting its data accordingly
114 | Assertions.assertTrue(proxy.getData() == ModelTestProxy.ON_REGISTER_CALLED, "Expecting proxy.getData() == ModelTestProxy.ON_REGISTER_CALLED");
115 |
116 | // Remove the component
117 | model.removeProxy(ModelTestProxy.NAME);
118 |
119 | // assert that onRemove was called, and the proxy responded by setting its data accordingly
120 | Assertions.assertTrue(proxy.getData() == ModelTestProxy.ON_REMOVE_CALLED, "Expecting proxy.getData() == ModelTestProxy.ON_REMOVE_CALLED");
121 | }
122 |
123 | }
124 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/core/ModelTestProxy.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.core;
9 |
10 | import org.puremvc.java.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 | public void onRegister() {
23 | setData(ON_REGISTER_CALLED);
24 | }
25 |
26 | public void onRemove() {
27 | setData(ON_REMOVE_CALLED);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/core/ViewTestMediator.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.core;
9 |
10 | import org.puremvc.java.interfaces.IMediator;
11 | import org.puremvc.java.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 | * @param view view object
29 | */
30 | public ViewTestMediator(Object view) {
31 | super (NAME, view);
32 | }
33 |
34 | public String[] listNotificationInterests() {
35 | // be sure that the mediator has some Observers created
36 | // in order to test removeMediator
37 | return new String[] { "ABC", "DEF", "GHI"};
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/core/ViewTestMediator2.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.core;
9 |
10 | import org.puremvc.java.interfaces.IMediator;
11 | import org.puremvc.java.interfaces.INotification;
12 | import org.puremvc.java.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 | * @param view view object
30 | */
31 | public ViewTestMediator2(Object view) {
32 | super(NAME, view);
33 | }
34 |
35 | public String[] listNotificationInterests() {
36 | // be sure that the mediator has some Observers created
37 | // in order to test removeMediator
38 | return new String[] {ViewTest.NOTE1, ViewTest.NOTE2};
39 | }
40 |
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/core/ViewTestMediator3.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.core;
9 |
10 | import org.puremvc.java.interfaces.IMediator;
11 | import org.puremvc.java.interfaces.INotification;
12 | import org.puremvc.java.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 | * @param view view object
30 | */
31 | public ViewTestMediator3(Object view) {
32 | super(NAME, view);
33 | }
34 |
35 | public String[] listNotificationInterests() {
36 | // be sure that the mediator has some Observers created
37 | // in order to test removeMediator
38 | return new String[] {ViewTest.NOTE3};
39 | }
40 |
41 | @Override
42 | public void handleNotification(INotification notification) {
43 | getViewTest().lastNotification = notification.getName();
44 | }
45 |
46 | public ViewTest getViewTest() {
47 | return (ViewTest) viewComponent;
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/core/ViewTestMediator4.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.core;
9 |
10 | import org.puremvc.java.interfaces.IMediator;
11 | import org.puremvc.java.interfaces.INotification;
12 | import org.puremvc.java.patterns.mediator.Mediator;
13 |
14 | /**
15 | * A Mediator class used by ViewTest.
16 | *
17 | * @see ViewTest ViewTest
18 | */
19 | public class ViewTestMediator4 extends Mediator implements IMediator {
20 |
21 | /**
22 | * The Mediator name
23 | */
24 | public static final String NAME = "ViewTestMediator4";
25 |
26 | /**
27 | * Constructor
28 | *
29 | * @param view view object
30 | */
31 | public ViewTestMediator4(Object view) {
32 | super(NAME, view);
33 | }
34 |
35 | public ViewTest getViewTest() {
36 | return (ViewTest) viewComponent;
37 | }
38 |
39 | @Override
40 | public void onRegister() {
41 | getViewTest().onRegisterCalled = true;
42 | }
43 |
44 | @Override
45 | public void onRemove() {
46 | getViewTest().onRemoveCalled = true;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/core/ViewTestMediator5.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.core;
9 |
10 | import org.puremvc.java.interfaces.IMediator;
11 | import org.puremvc.java.interfaces.INotification;
12 | import org.puremvc.java.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 | * @param view view object
30 | */
31 | public ViewTestMediator5(Object view) {
32 | super(NAME, view);
33 | }
34 |
35 | @Override
36 | public String[] listNotificationInterests() {
37 | return new String[] {ViewTest.NOTE5};
38 | }
39 |
40 | @Override
41 | public void handleNotification(INotification notification) {
42 | getViewTest().counter++;
43 | }
44 |
45 | public ViewTest getViewTest() {
46 | return (ViewTest) viewComponent;
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/core/ViewTestMediator6.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.core;
9 |
10 | import org.puremvc.java.interfaces.INotification;
11 | import org.puremvc.java.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 | * @param name mediator name
29 | * @param view view object
30 | */
31 | public ViewTestMediator6(String name, Object view) {
32 | super(name, view);
33 | }
34 |
35 | @Override
36 | public String[] listNotificationInterests() {
37 | return new String[]{ViewTest.NOTE6};
38 | }
39 |
40 | @Override
41 | public void handleNotification(INotification notification) {
42 | facade.removeMediator(getMediatorName());
43 | }
44 |
45 | @Override
46 | public void onRemove() {
47 | viewTest().counter++;
48 | }
49 |
50 | public ViewTest viewTest() {
51 | return (ViewTest)viewComponent;
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/core/ViewTestNote.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.core;
9 |
10 | import org.puremvc.java.interfaces.INotification;
11 | import org.puremvc.java.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 | * @return INotification
44 | */
45 | public static INotification create(Object body) {
46 | return new ViewTestNote(NAME, body);
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/patterns/command/MacroCommandTest.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.patterns.command;
9 |
10 | import org.junit.jupiter.api.Assertions;
11 | import org.junit.jupiter.api.Test;
12 | import org.puremvc.java.interfaces.INotification;
13 | import org.puremvc.java.patterns.observer.Notification;
14 |
15 | /**
16 | * Test the PureMVC SimpleCommand class.
17 | *
18 | * @see org.puremvc.java.patterns.command.MacroCommandTestVO MacroCommandTestVO
19 | * @see org.puremvc.java.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 | */
51 | @Test
52 | public void testMacroCommandExecute() {
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 |
62 | // Execute the SimpleCommand
63 | command.execute(note);
64 |
65 | // test assertions
66 | Assertions.assertTrue(vo.result1 == 10, "Expecting vo.result1 == 10 " + vo.result1);
67 | Assertions.assertTrue(vo.result2 == 25, "Expecing vo.result2 == 25");
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/patterns/command/MacroCommandTestCommand.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.patterns.command;
9 |
10 | /**
11 | * A MacroCommand subclass used by MacroCommandTest.
12 | *
13 | * @see MacroCommandTest MacroCommandTest
14 | * @see org.puremvc.java.patterns.command.MacroCommandTestSub1Command MacroCommandTestSub1Command
15 | * @see org.puremvc.java.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/patterns/command/MacroCommandTestSub1Command.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.patterns.command;
9 |
10 | import org.puremvc.java.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 INotification
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/patterns/command/MacroCommandTestSub2Command.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.patterns.command;
9 |
10 | import org.puremvc.java.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 INotification
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/patterns/command/MacroCommandTestVO.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.patterns.command;
9 |
10 | /**
11 | * A utility class used by MacroCommandTest.
12 | *
13 | * @see MacroCommandTest MacroCommandTest
14 | * @see org.puremvc.java.patterns.command.MacroCommandTestCommand MacroCommandTestCommand
15 | * @see org.puremvc.java.patterns.command.MacroCommandTestSub1Command MacroCommandTestSub1Command
16 | * @see org.puremvc.java.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/patterns/command/SimpleCommandTest.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.patterns.command;
9 |
10 | import org.junit.jupiter.api.Assertions;
11 | import org.junit.jupiter.api.Test;
12 | import org.puremvc.java.patterns.observer.Notification;
13 |
14 | /**
15 | * Test the PureMVC SimpleCommand class.
16 | *
17 | * @see org.puremvc.java.patterns.command.SimpleCommandTestVO SimpleCommandTestVO
18 | * @see org.puremvc.java.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/patterns/command/SimpleCommandTestCommand.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.patterns.command;
9 |
10 | import org.puremvc.java.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 | @Override
26 | public void execute(INotification notification) {
27 | SimpleCommandTestVO vo = (SimpleCommandTestVO) notification.getBody();
28 |
29 | // Fabricate a result
30 | vo.result = 2 * vo.input;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/patterns/command/SimpleCommandTestVO.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.patterns.command;
9 |
10 | /**
11 | * A utility class used by SimpleCommandTest.
12 | *
13 | * @see SimpleCommandTest SimpleCommandTest
14 | * @see org.puremvc.java.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/patterns/facade/FacadeTestCommand.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.patterns.facade;
9 |
10 | import org.puremvc.java.interfaces.INotification;
11 | import org.puremvc.java.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/patterns/facade/FacadeTestVO.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.patterns.facade;
9 |
10 | /**
11 | * A utility class used by FacadeTest.
12 | *
13 | * @see FacadeTest FacadeTest
14 | * @see org.puremvc.java.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/patterns/mediator/MediatorTest.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.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.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/patterns/observer/NotificationTest.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.patterns.observer;
9 |
10 | import org.junit.jupiter.api.Assertions;
11 | import org.junit.jupiter.api.Test;
12 | import org.puremvc.java.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/patterns/observer/ObserverTest.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.patterns.observer;
9 |
10 | import org.junit.jupiter.api.Assertions;
11 | import org.junit.jupiter.api.Test;
12 | import org.puremvc.java.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 | // test assertions
88 | Object negTestObj = new Object();
89 |
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 | * @param notification notification
100 | */
101 | public void handleNotification(INotification notification) {
102 | observerTestVar = (int)notification.getBody();
103 | }
104 |
105 | }
106 |
--------------------------------------------------------------------------------
/src/test/java/org/puremvc/java/patterns/proxy/ProxyTest.java:
--------------------------------------------------------------------------------
1 | //
2 | // PureMVC Java Standard
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.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.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 | // Create a new Proxy and use accessors to set the proxy name
27 | Proxy proxy = new Proxy("TestProxy");
28 |
29 | // test assertions
30 | Assertions.assertTrue(proxy.getProxyName() == "TestProxy", "Expecting proxy.getProxyName == 'TestProxy'");
31 | }
32 |
33 | /**
34 | * Tests setting and getting the data using Proxy class accessor methods.
35 | */
36 | @Test
37 | public void testDataAccessors() {
38 | // Create a new Proxy and use accessors to set the data
39 | Proxy proxy = new Proxy("colors");
40 | proxy.setData(new String[]{"red", "green", "blue"});
41 |
42 | String[] data = (String[]) proxy.getData();
43 |
44 | // test assertions
45 | Assertions.assertTrue(data.length == 3, "Expecting data.length == 3");
46 | Assertions.assertTrue(data[0] == "red", "Expecting data[0] == 'red'");
47 | Assertions.assertTrue(data[1] == "green", "Expecting data[1] == 'green'");
48 | Assertions.assertTrue(data[2] == "blue", "Expecting data[2] == 'blue'");
49 | }
50 |
51 | /**
52 | * Tests setting the name and body using the Notification class Constructor.
53 | */
54 | @Test
55 | public void testConstructor() {
56 | // Create a new Proxy using the Constructor to set the name and data
57 | Proxy proxy = new Proxy("colors", new String[] {"red", "green", "blue"});
58 | String[] data = (String[]) proxy.getData();
59 |
60 | // test assertions
61 | Assertions.assertNotNull(proxy, "Expecting proxy not null");
62 | Assertions.assertTrue(proxy.getProxyName() == "colors", "Expecting proxy.getProxyName() == 'color'");
63 | Assertions.assertTrue(data.length == 3, "Expecting data.length == 3");
64 | Assertions.assertTrue(data[0] == "red", "Expecting data[0] == 'red'");
65 | Assertions.assertTrue(data[1] == "green", "Expecting data[1] == 'green'");
66 | Assertions.assertTrue(data[2] == "blue", "Expecting data[2] == 'blue'");
67 | }
68 |
69 | }
70 |
--------------------------------------------------------------------------------