1   /*
2    * Copyright 2002-2004 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.springframework.richclient.command;
17  
18  import java.awt.event.ActionEvent;
19  import java.beans.PropertyChangeListener;
20  
21  import javax.swing.Action;
22  
23  import junit.framework.Assert;
24  import junit.framework.TestCase;
25  
26  
27  /**
28   * Provides a suite of unit tests for the {@link SwingActionAdapter} class.
29   *
30   * @author Kevin Stembridge
31   * @since 0.3
32   *
33   */
34  public class SwingActionAdapterTests extends TestCase {
35  
36      /**
37       * Test method for {@link SwingActionAdapter#SwingActionAdapter(ActionCommand)}.
38       */
39      public final void testConstructor() {
40          
41          try {
42              new SwingActionAdapter(null);
43              Assert.fail("Should have thrown an IllegalArgumentException");
44          } catch (IllegalArgumentException e) {
45              //do nothing, test succeeded
46          }
47          
48          //can't use EasyMock here because ActionCommand is not an interface
49          MockActionCommand command = new MockActionCommand();
50          command.setActionCommand("bogusActionCommand");
51          command.setCaption("bogusCaption");
52          
53          SwingActionAdapter adapter = new SwingActionAdapter(command);
54          
55          Assert.assertEquals("bogusActionCommand", adapter.getValue(Action.ACTION_COMMAND_KEY));
56          Assert.assertEquals("bogusCaption", adapter.getValue(Action.SHORT_DESCRIPTION));
57          
58          command.setCaption("newCaption");
59          
60          adapter.update();
61          
62          Assert.assertEquals("newCaption", adapter.getValue(Action.SHORT_DESCRIPTION));
63          
64      }
65  
66      /**
67       * Test method for {@link SwingActionAdapter#actionPerformed(java.awt.event.ActionEvent)}.
68       */
69      public final void testActionPerformed() {
70          
71          MockActionCommand command = new MockActionCommand();
72          SwingActionAdapter adapter = new SwingActionAdapter(command);
73          adapter.actionPerformed(new ActionEvent(command, 1, "bogus"));
74          
75          Assert.assertEquals(1, command.getActionPerformedCount());
76          
77      }
78  
79      private class MockActionCommand extends ActionCommand {
80          
81          private int actionPerformedCount;
82          private PropertyChangeListener enabledListener;
83          private PropertyChangeListener propertyChangeListener;
84  
85          /**
86           * {@inheritDoc}
87           */
88          protected void doExecuteCommand() {
89              this.actionPerformedCount++;
90          }
91          
92          /**
93           * {@inheritDoc}
94           */
95          public void addEnabledListener(PropertyChangeListener listener) {
96              this.enabledListener = listener;
97          }
98          
99          /**
100          * Returns the number of times the actionPerformed method has been called.
101          * @return actionPerformedCount
102          */
103         public int getActionPerformedCount() {
104             return this.actionPerformedCount;
105         }
106 
107     }
108 
109 }