1 //----------------------------------------------------------------------
2 //
3 // PerfectJPattern: "Design patterns are good but components are better!"
4 // TestInvoker.java Copyright (c) 2009 Giovanni Azua Garcia
5 // bravegag@hotmail.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 3
10 // of the License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, see <http://www.gnu.org/licenses/>.
19 //
20 //----------------------------------------------------------------------
21 package org.perfectjpattern.core.behavioral.command;
22
23 import junit.framework.*;
24
25 import org.perfectjpattern.core.api.behavioral.command.*;
26 import org.slf4j.*;
27
28
29 /**
30 * Test Suite for <code>Invoker</code> implementation.
31 *
32 * @see ICommand
33 * @see IInvoker
34 * @see Command
35 * @see Invoker
36 *
37 * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
38 * @version $Revision: 1.0 $ $Date: Jun 30, 2007 12:47:40 PM $
39 */
40 public
41 class TestInvoker
42 extends TestCase
43 {
44 //------------------------------------------------------------------------
45 // public
46 //------------------------------------------------------------------------
47 /**
48 * Test that Command Pattern lifecycle methods are executed in the
49 * right order.
50 */
51 public void
52 testInvokerInvalidStateDetected()
53 {
54 theLogger.debug("Running assertions ... ");
55
56 // create a new Invoker
57 IInvoker<Object, Object> myInvoker = new Invoker<Object, Object>();
58
59 try
60 {
61 myInvoker.invoke();
62 fail("Invoker implementation did not detect missing ICommand");
63 }
64 catch (IllegalStateException anException)
65 {
66 // ok
67 }
68
69 try
70 {
71 myInvoker.getResult();
72 fail("Invoker implementation did not detect missing ICommand");
73 }
74 catch (IllegalStateException anException)
75 {
76 // ok
77 }
78
79 // create a new Command
80 ICommand<Object, Object> myCommand = new ICommand<Object, Object>()
81 {
82 //----------------------------------------------------------------
83 public void
84 execute()
85 throws IllegalStateException
86 {
87 // do nothing
88 }
89
90 //----------------------------------------------------------------
91 public Object
92 getResult()
93 throws IllegalStateException
94 {
95 return null;
96 }
97
98 //----------------------------------------------------------------
99 public void
100 setParameter(Object aParameter)
101 throws IllegalArgumentException
102 {
103 // do nothing
104 }
105
106 //----------------------------------------------------------------
107 public void
108 setReceiver(IReceiver<Object, Object> aReceiver)
109 throws IllegalArgumentException
110 {
111 // do nothing
112 }
113 };
114
115 myInvoker.setCommand(myCommand);
116 try
117 {
118 myInvoker.getResult();
119 fail("Invoker implementation did not detect null IResult");
120 }
121 catch (IllegalStateException anException)
122 {
123 // ok
124 }
125
126 theLogger.debug("Completed test");
127 }
128
129 //------------------------------------------------------------------------
130 // members
131 //------------------------------------------------------------------------
132 /**
133 * Provides logging services for this class.
134 */
135 private final Logger theLogger = LoggerFactory.getLogger(this.getClass());
136 }