View Javadoc

1   // Copyright (C) 2004 - 2009 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.console.swingui;
23  
24  import junit.framework.TestCase;
25  
26  import java.awt.event.ActionEvent;
27  import javax.swing.Action;
28  
29  import net.grinder.testutility.RandomStubFactory;
30  
31  
32  /**
33   * Unit test for {@link TeeAction}.
34   *
35   * @author Philip Aston
36   */
37  public class TestTeeAction extends TestCase {
38  
39    public void testTeeAction() throws Exception {
40      final ActionStubFactory action1StubFactory = new ActionStubFactory();
41      final Action action1 = action1StubFactory.getStub();
42  
43      final ActionStubFactory action2StubFactory = new ActionStubFactory();
44      final Action action2 = action2StubFactory.getStub();
45  
46      final TeeAction teeAction = new TeeAction(action1, action2);
47  
48      final ActionEvent actionEvent =
49        new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "foo");
50  
51      assertTrue(!teeAction.isEnabled());
52  
53      action1StubFactory.assertSuccess("isEnabled");
54      action2StubFactory.assertSuccess("isEnabled");
55  
56      teeAction.actionPerformed(actionEvent);
57  
58      action1StubFactory.assertSuccess("isEnabled");
59      action1StubFactory.assertNoMoreCalls();
60      action2StubFactory.assertSuccess("isEnabled");
61      action2StubFactory.assertNoMoreCalls();
62  
63      action2StubFactory.setEnabled(true);
64  
65      assertTrue(teeAction.isEnabled());
66  
67      action1StubFactory.resetCallHistory();
68      action2StubFactory.resetCallHistory();
69  
70      teeAction.actionPerformed(actionEvent);
71  
72      action1StubFactory.assertSuccess("isEnabled");
73      action1StubFactory.assertNoMoreCalls();
74      action2StubFactory.assertSuccess("isEnabled");
75      action2StubFactory.assertSuccess("actionPerformed", actionEvent);
76      action2StubFactory.assertNoMoreCalls();
77  
78      action1StubFactory.setEnabled(true);
79  
80      assertTrue(teeAction.isEnabled());
81  
82      action1StubFactory.resetCallHistory();
83      action2StubFactory.resetCallHistory();
84  
85      teeAction.actionPerformed(actionEvent);
86  
87      action1StubFactory.assertSuccess("isEnabled");
88      action1StubFactory.assertSuccess("actionPerformed", actionEvent);
89      action1StubFactory.assertNoMoreCalls();
90      action2StubFactory.assertSuccess("isEnabled");
91      action2StubFactory.assertSuccess("actionPerformed", actionEvent);
92      action2StubFactory.assertNoMoreCalls();
93    }
94  
95    public final static class ActionStubFactory
96      extends RandomStubFactory<Action> {
97  
98      private boolean m_isEnabled = false;
99  
100     public ActionStubFactory() {
101       super(Action.class);
102     }
103 
104     public boolean override_isEnabled(Object proxy) {
105       return m_isEnabled;
106     }
107 
108     public void setEnabled(boolean b) {
109       m_isEnabled = b;
110     }
111   }
112 }