001    /*
002     * Copyright 2002-2004 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.command;
017    
018    import java.awt.event.ActionEvent;
019    import java.beans.PropertyChangeListener;
020    
021    import javax.swing.Action;
022    
023    import junit.framework.Assert;
024    import junit.framework.TestCase;
025    
026    
027    /**
028     * Provides a suite of unit tests for the {@link SwingActionAdapter} class.
029     *
030     * @author Kevin Stembridge
031     * @since 0.3
032     *
033     */
034    public class SwingActionAdapterTests extends TestCase {
035    
036        /**
037         * Test method for {@link SwingActionAdapter#SwingActionAdapter(ActionCommand)}.
038         */
039        public final void testConstructor() {
040            
041            try {
042                new SwingActionAdapter(null);
043                Assert.fail("Should have thrown an IllegalArgumentException");
044            } catch (IllegalArgumentException e) {
045                //do nothing, test succeeded
046            }
047            
048            //can't use EasyMock here because ActionCommand is not an interface
049            MockActionCommand command = new MockActionCommand();
050            command.setActionCommand("bogusActionCommand");
051            command.setCaption("bogusCaption");
052            
053            SwingActionAdapter adapter = new SwingActionAdapter(command);
054            
055            Assert.assertEquals("bogusActionCommand", adapter.getValue(Action.ACTION_COMMAND_KEY));
056            Assert.assertEquals("bogusCaption", adapter.getValue(Action.SHORT_DESCRIPTION));
057            
058            command.setCaption("newCaption");
059            
060            adapter.update();
061            
062            Assert.assertEquals("newCaption", adapter.getValue(Action.SHORT_DESCRIPTION));
063            
064        }
065    
066        /**
067         * Test method for {@link SwingActionAdapter#actionPerformed(java.awt.event.ActionEvent)}.
068         */
069        public final void testActionPerformed() {
070            
071            MockActionCommand command = new MockActionCommand();
072            SwingActionAdapter adapter = new SwingActionAdapter(command);
073            adapter.actionPerformed(new ActionEvent(command, 1, "bogus"));
074            
075            Assert.assertEquals(1, command.getActionPerformedCount());
076            
077        }
078    
079        private class MockActionCommand extends ActionCommand {
080            
081            private int actionPerformedCount;
082            private PropertyChangeListener enabledListener;
083            private PropertyChangeListener propertyChangeListener;
084    
085            /**
086             * {@inheritDoc}
087             */
088            protected void doExecuteCommand() {
089                this.actionPerformedCount++;
090            }
091            
092            /**
093             * {@inheritDoc}
094             */
095            public void addEnabledListener(PropertyChangeListener listener) {
096                this.enabledListener = listener;
097            }
098            
099            /**
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    }